bfd_error_handler bfd_vma and bfd_size_type args
[external/binutils.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2    Copyright (C) 1993-2017 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf-nacl.h"
27 #include "elf-vxworks.h"
28 #include "bfd_stdint.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
31 #include "dwarf2.h"
32 #include "opcode/i386.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, 3, 0, FALSE, 0, complain_overflow_dont,
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   HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
136         bfd_elf_generic_reloc, "R_386_SIZE32",
137         TRUE, 0xffffffff, 0xffffffff, FALSE),
138   HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
139         bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
140         TRUE, 0xffffffff, 0xffffffff, FALSE),
141   HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
142         bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
143         FALSE, 0, 0, FALSE),
144   HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
145         bfd_elf_generic_reloc, "R_386_TLS_DESC",
146         TRUE, 0xffffffff, 0xffffffff, FALSE),
147   HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
148         bfd_elf_generic_reloc, "R_386_IRELATIVE",
149         TRUE, 0xffffffff, 0xffffffff, FALSE),
150   HOWTO(R_386_GOT32X, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
151         bfd_elf_generic_reloc, "R_386_GOT32X",
152         TRUE, 0xffffffff, 0xffffffff, FALSE),
153
154   /* Another gap.  */
155 #define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
156 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
157
158 /* GNU extension to record C++ vtable hierarchy.  */
159   HOWTO (R_386_GNU_VTINHERIT,   /* type */
160          0,                     /* rightshift */
161          2,                     /* size (0 = byte, 1 = short, 2 = long) */
162          0,                     /* bitsize */
163          FALSE,                 /* pc_relative */
164          0,                     /* bitpos */
165          complain_overflow_dont, /* complain_on_overflow */
166          NULL,                  /* special_function */
167          "R_386_GNU_VTINHERIT", /* name */
168          FALSE,                 /* partial_inplace */
169          0,                     /* src_mask */
170          0,                     /* dst_mask */
171          FALSE),                /* pcrel_offset */
172
173 /* GNU extension to record C++ vtable member usage.  */
174   HOWTO (R_386_GNU_VTENTRY,     /* type */
175          0,                     /* rightshift */
176          2,                     /* size (0 = byte, 1 = short, 2 = long) */
177          0,                     /* bitsize */
178          FALSE,                 /* pc_relative */
179          0,                     /* bitpos */
180          complain_overflow_dont, /* complain_on_overflow */
181          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
182          "R_386_GNU_VTENTRY",   /* name */
183          FALSE,                 /* partial_inplace */
184          0,                     /* src_mask */
185          0,                     /* dst_mask */
186          FALSE)                 /* pcrel_offset */
187
188 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
189
190 };
191
192 #ifdef DEBUG_GEN_RELOC
193 #define TRACE(str) \
194   fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
195 #else
196 #define TRACE(str)
197 #endif
198
199 static reloc_howto_type *
200 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
201                             bfd_reloc_code_real_type code)
202 {
203   switch (code)
204     {
205     case BFD_RELOC_NONE:
206       TRACE ("BFD_RELOC_NONE");
207       return &elf_howto_table[R_386_NONE];
208
209     case BFD_RELOC_32:
210       TRACE ("BFD_RELOC_32");
211       return &elf_howto_table[R_386_32];
212
213     case BFD_RELOC_CTOR:
214       TRACE ("BFD_RELOC_CTOR");
215       return &elf_howto_table[R_386_32];
216
217     case BFD_RELOC_32_PCREL:
218       TRACE ("BFD_RELOC_PC32");
219       return &elf_howto_table[R_386_PC32];
220
221     case BFD_RELOC_386_GOT32:
222       TRACE ("BFD_RELOC_386_GOT32");
223       return &elf_howto_table[R_386_GOT32];
224
225     case BFD_RELOC_386_PLT32:
226       TRACE ("BFD_RELOC_386_PLT32");
227       return &elf_howto_table[R_386_PLT32];
228
229     case BFD_RELOC_386_COPY:
230       TRACE ("BFD_RELOC_386_COPY");
231       return &elf_howto_table[R_386_COPY];
232
233     case BFD_RELOC_386_GLOB_DAT:
234       TRACE ("BFD_RELOC_386_GLOB_DAT");
235       return &elf_howto_table[R_386_GLOB_DAT];
236
237     case BFD_RELOC_386_JUMP_SLOT:
238       TRACE ("BFD_RELOC_386_JUMP_SLOT");
239       return &elf_howto_table[R_386_JUMP_SLOT];
240
241     case BFD_RELOC_386_RELATIVE:
242       TRACE ("BFD_RELOC_386_RELATIVE");
243       return &elf_howto_table[R_386_RELATIVE];
244
245     case BFD_RELOC_386_GOTOFF:
246       TRACE ("BFD_RELOC_386_GOTOFF");
247       return &elf_howto_table[R_386_GOTOFF];
248
249     case BFD_RELOC_386_GOTPC:
250       TRACE ("BFD_RELOC_386_GOTPC");
251       return &elf_howto_table[R_386_GOTPC];
252
253       /* These relocs are a GNU extension.  */
254     case BFD_RELOC_386_TLS_TPOFF:
255       TRACE ("BFD_RELOC_386_TLS_TPOFF");
256       return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
257
258     case BFD_RELOC_386_TLS_IE:
259       TRACE ("BFD_RELOC_386_TLS_IE");
260       return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
261
262     case BFD_RELOC_386_TLS_GOTIE:
263       TRACE ("BFD_RELOC_386_TLS_GOTIE");
264       return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
265
266     case BFD_RELOC_386_TLS_LE:
267       TRACE ("BFD_RELOC_386_TLS_LE");
268       return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
269
270     case BFD_RELOC_386_TLS_GD:
271       TRACE ("BFD_RELOC_386_TLS_GD");
272       return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
273
274     case BFD_RELOC_386_TLS_LDM:
275       TRACE ("BFD_RELOC_386_TLS_LDM");
276       return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
277
278     case BFD_RELOC_16:
279       TRACE ("BFD_RELOC_16");
280       return &elf_howto_table[R_386_16 - R_386_ext_offset];
281
282     case BFD_RELOC_16_PCREL:
283       TRACE ("BFD_RELOC_16_PCREL");
284       return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
285
286     case BFD_RELOC_8:
287       TRACE ("BFD_RELOC_8");
288       return &elf_howto_table[R_386_8 - R_386_ext_offset];
289
290     case BFD_RELOC_8_PCREL:
291       TRACE ("BFD_RELOC_8_PCREL");
292       return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
293
294     /* Common with Sun TLS implementation.  */
295     case BFD_RELOC_386_TLS_LDO_32:
296       TRACE ("BFD_RELOC_386_TLS_LDO_32");
297       return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
298
299     case BFD_RELOC_386_TLS_IE_32:
300       TRACE ("BFD_RELOC_386_TLS_IE_32");
301       return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
302
303     case BFD_RELOC_386_TLS_LE_32:
304       TRACE ("BFD_RELOC_386_TLS_LE_32");
305       return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
306
307     case BFD_RELOC_386_TLS_DTPMOD32:
308       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
309       return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
310
311     case BFD_RELOC_386_TLS_DTPOFF32:
312       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
313       return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
314
315     case BFD_RELOC_386_TLS_TPOFF32:
316       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
317       return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
318
319     case BFD_RELOC_SIZE32:
320       TRACE ("BFD_RELOC_SIZE32");
321       return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
322
323     case BFD_RELOC_386_TLS_GOTDESC:
324       TRACE ("BFD_RELOC_386_TLS_GOTDESC");
325       return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
326
327     case BFD_RELOC_386_TLS_DESC_CALL:
328       TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
329       return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
330
331     case BFD_RELOC_386_TLS_DESC:
332       TRACE ("BFD_RELOC_386_TLS_DESC");
333       return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
334
335     case BFD_RELOC_386_IRELATIVE:
336       TRACE ("BFD_RELOC_386_IRELATIVE");
337       return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
338
339     case BFD_RELOC_386_GOT32X:
340       TRACE ("BFD_RELOC_386_GOT32X");
341       return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
342
343     case BFD_RELOC_VTABLE_INHERIT:
344       TRACE ("BFD_RELOC_VTABLE_INHERIT");
345       return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
346
347     case BFD_RELOC_VTABLE_ENTRY:
348       TRACE ("BFD_RELOC_VTABLE_ENTRY");
349       return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
350
351     default:
352       break;
353     }
354
355   TRACE ("Unknown");
356   return 0;
357 }
358
359 static reloc_howto_type *
360 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
361                             const char *r_name)
362 {
363   unsigned int i;
364
365   for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
366     if (elf_howto_table[i].name != NULL
367         && strcasecmp (elf_howto_table[i].name, r_name) == 0)
368       return &elf_howto_table[i];
369
370   return NULL;
371 }
372
373 static reloc_howto_type *
374 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
375 {
376   unsigned int indx;
377
378   if ((indx = r_type) >= R_386_standard
379       && ((indx = r_type - R_386_ext_offset) - R_386_standard
380           >= R_386_ext - R_386_standard)
381       && ((indx = r_type - R_386_tls_offset) - R_386_ext
382           >= R_386_ext2 - R_386_ext)
383       && ((indx = r_type - R_386_vt_offset) - R_386_ext2
384           >= R_386_vt - R_386_ext2))
385     {
386       /* xgettext:c-format */
387       _bfd_error_handler (_("%B: invalid relocation type %d"),
388                           abfd, (int) r_type);
389       indx = R_386_NONE;
390     }
391   /* PR 17512: file: 0f67f69d.  */
392   if (elf_howto_table [indx].type != r_type)
393     return NULL;
394   return &elf_howto_table[indx];
395 }
396
397 static void
398 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
399                             arelent *cache_ptr,
400                             Elf_Internal_Rela *dst)
401 {
402   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
403   cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
404 }
405
406 /* Return whether a symbol name implies a local label.  The UnixWare
407    2.1 cc generates temporary symbols that start with .X, so we
408    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
409    If so, we should move the .X recognition into
410    _bfd_elf_is_local_label_name.  */
411
412 static bfd_boolean
413 elf_i386_is_local_label_name (bfd *abfd, const char *name)
414 {
415   if (name[0] == '.' && name[1] == 'X')
416     return TRUE;
417
418   return _bfd_elf_is_local_label_name (abfd, name);
419 }
420 \f
421 /* Support for core dump NOTE sections.  */
422
423 static bfd_boolean
424 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
425 {
426   int offset;
427   size_t size;
428
429   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
430     {
431       int pr_version = bfd_get_32 (abfd, note->descdata);
432
433       if (pr_version != 1)
434         return FALSE;
435
436       /* pr_cursig */
437       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
438
439       /* pr_pid */
440       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
441
442       /* pr_reg */
443       offset = 28;
444       size = bfd_get_32 (abfd, note->descdata + 8);
445     }
446   else
447     {
448       switch (note->descsz)
449         {
450         default:
451           return FALSE;
452
453         case 144:               /* Linux/i386 */
454           /* pr_cursig */
455           elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
456
457           /* pr_pid */
458           elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
459
460           /* pr_reg */
461           offset = 72;
462           size = 68;
463
464           break;
465         }
466     }
467
468   /* Make a ".reg/999" section.  */
469   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
470                                           size, note->descpos + offset);
471 }
472
473 static bfd_boolean
474 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
475 {
476   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
477     {
478       int pr_version = bfd_get_32 (abfd, note->descdata);
479
480       if (pr_version != 1)
481         return FALSE;
482
483       elf_tdata (abfd)->core->program
484         = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
485       elf_tdata (abfd)->core->command
486         = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
487     }
488   else
489     {
490       switch (note->descsz)
491         {
492         default:
493           return FALSE;
494
495         case 124:               /* Linux/i386 elf_prpsinfo.  */
496           elf_tdata (abfd)->core->pid
497             = bfd_get_32 (abfd, note->descdata + 12);
498           elf_tdata (abfd)->core->program
499             = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
500           elf_tdata (abfd)->core->command
501             = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
502         }
503     }
504
505   /* Note that for some reason, a spurious space is tacked
506      onto the end of the args in some (at least one anyway)
507      implementations, so strip it off if it exists.  */
508   {
509     char *command = elf_tdata (abfd)->core->command;
510     int n = strlen (command);
511
512     if (0 < n && command[n - 1] == ' ')
513       command[n - 1] = '\0';
514   }
515
516   return TRUE;
517 }
518 \f
519 /* Functions for the i386 ELF linker.
520
521    In order to gain some understanding of code in this file without
522    knowing all the intricate details of the linker, note the
523    following:
524
525    Functions named elf_i386_* are called by external routines, other
526    functions are only called locally.  elf_i386_* functions appear
527    in this file more or less in the order in which they are called
528    from external routines.  eg. elf_i386_check_relocs is called
529    early in the link process, elf_i386_finish_dynamic_sections is
530    one of the last functions.  */
531
532
533 /* The name of the dynamic interpreter.  This is put in the .interp
534    section.  */
535
536 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
537
538 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
539    copying dynamic variables from a shared lib into an app's dynbss
540    section, and instead use a dynamic relocation to point into the
541    shared lib.  */
542 #define ELIMINATE_COPY_RELOCS 1
543
544 /* The size in bytes of an entry in the lazy procedure linkage table.  */
545
546 #define LAZY_PLT_ENTRY_SIZE 16
547
548 /* The size in bytes of an entry in the non-lazy procedure linkage
549    table.  */
550
551 #define NON_LAZY_PLT_ENTRY_SIZE 8
552
553 /* The first entry in an absolute lazy procedure linkage table looks
554    like this.  See the SVR4 ABI i386 supplement to see how this works.
555    Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte.  */
556
557 static const bfd_byte elf_i386_lazy_plt0_entry[12] =
558 {
559   0xff, 0x35,   /* pushl contents of address */
560   0, 0, 0, 0,   /* replaced with address of .got + 4.  */
561   0xff, 0x25,   /* jmp indirect */
562   0, 0, 0, 0    /* replaced with address of .got + 8.  */
563 };
564
565 /* Subsequent entries in an absolute lazy procedure linkage table look
566    like this.  */
567
568 static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
569 {
570   0xff, 0x25,   /* jmp indirect */
571   0, 0, 0, 0,   /* replaced with address 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 /* The first entry in a PIC lazy procedure linkage table look like
579    this.  Will be padded to LAZY_PLT_ENTRY_SIZE with
580    lazy_plt->plt0_pad_byte.  */
581
582 static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
583 {
584   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx) */
585   0xff, 0xa3, 8, 0, 0, 0        /* jmp *8(%ebx) */
586 };
587
588 /* Subsequent entries in a PIC lazy procedure linkage table look like
589    this.  */
590
591 static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
592 {
593   0xff, 0xa3,   /* jmp *offset(%ebx) */
594   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
595   0x68,         /* pushl immediate */
596   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
597   0xe9,         /* jmp relative */
598   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
599 };
600
601 /* Entries in the non-lazy procedure linkage table look like this.  */
602
603 static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
604 {
605   0xff, 0x25,   /* jmp indirect */
606   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
607   0x66, 0x90    /* xchg %ax,%ax  */
608 };
609
610 /* Entries in the PIC non-lazy procedure linkage table look like
611    this.  */
612
613 static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
614 {
615   0xff, 0xa3,   /* jmp *offset(%ebx)  */
616   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
617   0x66, 0x90    /* xchg %ax,%ax  */
618 };
619
620 /* The first entry in an absolute IBT-enabled lazy procedure linkage
621    table looks like this.  */
622
623 static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
624 {
625   0xff, 0x35, 0, 0, 0, 0,       /* pushl GOT[1]       */
626   0xff, 0x25, 0, 0, 0, 0,       /* jmp *GOT[2]        */
627   0x0f, 0x1f, 0x40, 0x00        /* nopl 0(%rax)       */
628 };
629
630 /* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
631    table look like this.  Subsequent entries for a PIC IBT-enabled lazy
632    procedure linkage table are the same.  */
633
634 static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
635 {
636   0xf3, 0x0f, 0x1e, 0xfb,       /* endbr32                    */
637   0x68, 0, 0, 0, 0,             /* pushl immediate            */
638   0xe9, 0, 0, 0, 0,             /* jmp relative               */
639   0x66, 0x90                    /* xchg %ax,%ax               */
640 };
641
642 /* The first entry in a PIC IBT-enabled lazy procedure linkage table
643    look like.  */
644
645 static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
646 {
647   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx)      */
648   0xff, 0xa3, 8, 0, 0, 0,       /* jmp *8(%ebx)       */
649   0x0f, 0x1f, 0x40, 0x00        /* nopl 0(%rax)       */
650 };
651
652 /* Entries for branches with IBT-enabled in the absolute non-lazey
653    procedure linkage table look like this.  They have the same size
654    as the lazy PLT entry.  */
655
656 static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
657 {
658   0xf3, 0x0f, 0x1e, 0xfb,            /* endbr32               */
659   0xff, 0x25, 0, 0, 0, 0,            /* jmp *name@GOT         */
660   0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
661 };
662
663 /* Entries for branches with IBT-enabled in the PIC non-lazey procedure
664    linkage table look like this.  They have the same size as the lazy
665    PLT entry.  */
666
667 static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
668 {
669   0xf3, 0x0f, 0x1e, 0xfb,            /* endbr32               */
670   0xff, 0xa3, 0, 0, 0, 0,            /* jmp *name@GOT(%ebx)   */
671   0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
672 };
673
674 /* .eh_frame covering the lazy .plt section.  */
675
676 static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
677 {
678 #define PLT_CIE_LENGTH          20
679 #define PLT_FDE_LENGTH          36
680 #define PLT_FDE_START_OFFSET    4 + PLT_CIE_LENGTH + 8
681 #define PLT_FDE_LEN_OFFSET      4 + PLT_CIE_LENGTH + 12
682   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
683   0, 0, 0, 0,                   /* CIE ID */
684   1,                            /* CIE version */
685   'z', 'R', 0,                  /* Augmentation string */
686   1,                            /* Code alignment factor */
687   0x7c,                         /* Data alignment factor */
688   8,                            /* Return address column */
689   1,                            /* Augmentation size */
690   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
691   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
692   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
693   DW_CFA_nop, DW_CFA_nop,
694
695   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
696   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
697   0, 0, 0, 0,                   /* R_386_PC32 .plt goes here */
698   0, 0, 0, 0,                   /* .plt size goes here */
699   0,                            /* Augmentation size */
700   DW_CFA_def_cfa_offset, 8,     /* DW_CFA_def_cfa_offset: 8 */
701   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
702   DW_CFA_def_cfa_offset, 12,    /* DW_CFA_def_cfa_offset: 12 */
703   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
704   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
705   11,                           /* Block length */
706   DW_OP_breg4, 4,               /* DW_OP_breg4 (esp): 4 */
707   DW_OP_breg8, 0,               /* DW_OP_breg8 (eip): 0 */
708   DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
709   DW_OP_lit2, DW_OP_shl, DW_OP_plus,
710   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
711 };
712
713 /* .eh_frame covering the lazy .plt section with IBT-enabled.  */
714
715 static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
716 {
717   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
718   0, 0, 0, 0,                   /* CIE ID */
719   1,                            /* CIE version */
720   'z', 'R', 0,                  /* Augmentation string */
721   1,                            /* Code alignment factor */
722   0x7c,                         /* Data alignment factor */
723   8,                            /* Return address column */
724   1,                            /* Augmentation size */
725   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
726   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
727   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
728   DW_CFA_nop, DW_CFA_nop,
729
730   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
731   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
732   0, 0, 0, 0,                   /* R_386_PC32 .plt goes here */
733   0, 0, 0, 0,                   /* .plt size goes here */
734   0,                            /* Augmentation size */
735   DW_CFA_def_cfa_offset, 8,     /* DW_CFA_def_cfa_offset: 8 */
736   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
737   DW_CFA_def_cfa_offset, 12,    /* DW_CFA_def_cfa_offset: 12 */
738   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
739   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
740   11,                           /* Block length */
741   DW_OP_breg4, 4,               /* DW_OP_breg4 (esp): 4 */
742   DW_OP_breg8, 0,               /* DW_OP_breg8 (eip): 0 */
743   DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
744   DW_OP_lit2, DW_OP_shl, DW_OP_plus,
745   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
746 };
747
748 /* .eh_frame covering the non-lazy .plt section.  */
749
750 static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
751 {
752 #define PLT_GOT_FDE_LENGTH              16
753   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
754   0, 0, 0, 0,                   /* CIE ID */
755   1,                            /* CIE version */
756   'z', 'R', 0,                  /* Augmentation string */
757   1,                            /* Code alignment factor */
758   0x7c,                         /* Data alignment factor */
759   8,                            /* Return address column */
760   1,                            /* Augmentation size */
761   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
762   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
763   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
764   DW_CFA_nop, DW_CFA_nop,
765
766   PLT_GOT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
767   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
768   0, 0, 0, 0,                   /* the start of non-lazy .plt goes here */
769   0, 0, 0, 0,                   /* non-lazy .plt size goes here */
770   0,                            /* Augmentation size */
771   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
772 };
773
774 struct elf_i386_lazy_plt_layout
775 {
776   /* The first entry in an absolute lazy procedure linkage table looks
777      like this.  */
778   const bfd_byte *plt0_entry;
779   unsigned int plt0_entry_size;
780
781   /* Offsets into plt0_entry that are to be replaced with GOT[1] and
782      GOT[2].  */
783   unsigned int plt0_got1_offset;
784   unsigned int plt0_got2_offset;
785
786   /* Later entries in an absolute lazy procedure linkage table look
787      like this.  */
788   const bfd_byte *plt_entry;
789   unsigned int plt_entry_size;
790
791   /* Offsets into plt_entry that are to be replaced with...  */
792   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
793   unsigned int plt_reloc_offset;  /* ... offset into relocation table. */
794   unsigned int plt_plt_offset;    /* ... offset to start of .plt. */
795
796   /* Offset into plt_entry where the initial value of the GOT entry
797      points.  */
798   unsigned int plt_lazy_offset;
799
800   /* The first entry in a PIC lazy procedure linkage table looks like
801      this.  */
802   const bfd_byte *pic_plt0_entry;
803
804   /* Subsequent entries in a PIC lazy procedure linkage table look
805      like this.  */
806   const bfd_byte *pic_plt_entry;
807
808   /* .eh_frame covering the lazy .plt section.  */
809   const bfd_byte *eh_frame_plt;
810   unsigned int eh_frame_plt_size;
811 };
812
813 struct elf_i386_non_lazy_plt_layout
814 {
815   /* Entries in an absolute non-lazy procedure linkage table look like
816      this.  */
817   const bfd_byte *plt_entry;
818   /* Entries in a PIC non-lazy procedure linkage table look like this.  */
819   const bfd_byte *pic_plt_entry;
820
821   unsigned int plt_entry_size;
822
823   /* Offsets into plt_entry that are to be replaced with...  */
824   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
825
826   /* .eh_frame covering the non-lazy .plt section.  */
827   const bfd_byte *eh_frame_plt;
828   unsigned int eh_frame_plt_size;
829 };
830
831 struct elf_i386_plt_layout
832 {
833   /* The first entry in a lazy procedure linkage table looks like this.  */
834   const bfd_byte *plt0_entry;
835   /* Entries in a procedure linkage table look like this.  */
836   const bfd_byte *plt_entry;
837   unsigned int plt_entry_size;
838
839   /* 1 has PLT0.  */
840   unsigned int has_plt0;
841
842   /* Offsets into plt_entry that are to be replaced with...  */
843   unsigned int plt_got_offset;    /* ... address of this symbol in .got. */
844
845   /* .eh_frame covering the .plt section.  */
846   const bfd_byte *eh_frame_plt;
847   unsigned int eh_frame_plt_size;
848 };
849
850 /* These are the standard parameters.  */
851 static const struct elf_i386_lazy_plt_layout elf_i386_lazy_plt =
852   {
853     elf_i386_lazy_plt0_entry,           /* plt0_entry */
854     sizeof (elf_i386_lazy_plt0_entry),  /* plt0_entry_size */
855     2,                                  /* plt0_got1_offset */
856     8,                                  /* plt0_got2_offset */
857     elf_i386_lazy_plt_entry,            /* plt_entry */
858     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
859     2,                                  /* plt_got_offset */
860     7,                                  /* plt_reloc_offset */
861     12,                                 /* plt_plt_offset */
862     6,                                  /* plt_lazy_offset */
863     elf_i386_pic_lazy_plt0_entry,       /* pic_plt0_entry */
864     elf_i386_pic_lazy_plt_entry,        /* pic_plt_entry */
865     elf_i386_eh_frame_lazy_plt,         /* eh_frame_plt */
866     sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
867   };
868
869 static const struct elf_i386_non_lazy_plt_layout elf_i386_non_lazy_plt =
870   {
871     elf_i386_non_lazy_plt_entry,        /* plt_entry */
872     elf_i386_pic_non_lazy_plt_entry,    /* pic_plt_entry */
873     NON_LAZY_PLT_ENTRY_SIZE,            /* plt_entry_size */
874     2,                                  /* plt_got_offset */
875     elf_i386_eh_frame_non_lazy_plt,     /* eh_frame_plt */
876     sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
877   };
878
879 static const struct elf_i386_lazy_plt_layout elf_i386_lazy_ibt_plt =
880   {
881     elf_i386_lazy_ibt_plt0_entry,       /* plt0_entry */
882     sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
883     2,                                  /* plt0_got1_offset */
884     8,                                  /* plt0_got2_offset */
885     elf_i386_lazy_ibt_plt_entry,        /* plt_entry */
886     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
887     4+2,                                /* plt_got_offset */
888     4+1,                                /* plt_reloc_offset */
889     4+6,                                /* plt_plt_offset */
890     0,                                  /* plt_lazy_offset */
891     elf_i386_pic_lazy_ibt_plt0_entry,   /* pic_plt0_entry */
892     elf_i386_lazy_ibt_plt_entry,        /* pic_plt_entry */
893     elf_i386_eh_frame_lazy_ibt_plt,     /* eh_frame_plt */
894     sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
895   };
896
897 static const struct elf_i386_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
898   {
899     elf_i386_non_lazy_ibt_plt_entry,    /* plt_entry */
900     elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
901     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
902     4+2,                                /* plt_got_offset */
903     elf_i386_eh_frame_non_lazy_plt,     /* eh_frame_plt */
904     sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
905   };
906 \f
907
908 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
909    for the PLTResolve stub and then for each PLT entry.  */
910 #define PLTRESOLVE_RELOCS_SHLIB 0
911 #define PLTRESOLVE_RELOCS 2
912 #define PLT_NON_JUMP_SLOT_RELOCS 2
913
914 /* Architecture-specific backend data for i386.  */
915
916 struct elf_i386_backend_data
917 {
918   /* Value used to fill the unused bytes of the first PLT entry.  */
919   bfd_byte plt0_pad_byte;
920
921   /* Target system.  */
922   enum
923     {
924       is_normal,
925       is_vxworks,
926       is_nacl
927     } os;
928 };
929
930 #define get_elf_i386_backend_data(abfd) \
931   ((const struct elf_i386_backend_data *) \
932    get_elf_backend_data (abfd)->arch_data)
933
934 /* These are the standard parameters.  */
935 static const struct elf_i386_backend_data elf_i386_arch_bed =
936   {
937     0,                                  /* plt0_pad_byte */
938     is_normal                           /* os */
939   };
940
941 #define elf_backend_arch_data   &elf_i386_arch_bed
942
943 /* Is a undefined weak symbol which is resolved to 0.  Reference to an
944    undefined weak symbol is resolved to 0 when building executable if
945    it isn't dynamic and
946    1. Has non-GOT/non-PLT relocations in text section.  Or
947    2. Has no GOT/PLT relocation.
948    Local undefined weak symbol is always resolved to 0.
949  */
950 #define UNDEFINED_WEAK_RESOLVED_TO_ZERO(INFO, GOT_RELOC, EH)    \
951   ((EH)->elf.root.type == bfd_link_hash_undefweak               \
952    && ((EH)->elf.forced_local                                   \
953        || (bfd_link_executable (INFO)                           \
954            && (elf_i386_hash_table (INFO)->interp == NULL       \
955                || !(GOT_RELOC)                                  \
956                || (EH)->has_non_got_reloc                       \
957                || !(INFO)->dynamic_undefined_weak))))
958
959 /* i386 ELF linker hash entry.  */
960
961 struct elf_i386_link_hash_entry
962 {
963   struct elf_link_hash_entry elf;
964
965   /* Track dynamic relocs copied for this symbol.  */
966   struct elf_dyn_relocs *dyn_relocs;
967
968 #define GOT_UNKNOWN     0
969 #define GOT_NORMAL      1
970 #define GOT_TLS_GD      2
971 #define GOT_TLS_IE      4
972 #define GOT_TLS_IE_POS  5
973 #define GOT_TLS_IE_NEG  6
974 #define GOT_TLS_IE_BOTH 7
975 #define GOT_TLS_GDESC   8
976 #define GOT_TLS_GD_BOTH_P(type)                                         \
977   ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
978 #define GOT_TLS_GD_P(type)                                              \
979   ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
980 #define GOT_TLS_GDESC_P(type)                                           \
981   ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
982 #define GOT_TLS_GD_ANY_P(type)                                          \
983   (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
984   unsigned char tls_type;
985
986   /* Symbol is referenced by R_386_GOTOFF relocation.  */
987   unsigned int gotoff_ref : 1;
988
989   /* Symbol has GOT or PLT relocations.  */
990   unsigned int has_got_reloc : 1;
991
992   /* Symbol has non-GOT/non-PLT relocations in text sections.  */
993   unsigned int has_non_got_reloc : 1;
994
995   /* Don't call finish_dynamic_symbol on this symbol.  */
996   unsigned int no_finish_dynamic_symbol : 1;
997
998   /* 0: symbol isn't ___tls_get_addr.
999      1: symbol is ___tls_get_addr.
1000      2: symbol is unknown.  */
1001   unsigned int tls_get_addr : 2;
1002
1003   /* Reference count of C/C++ function pointer relocations in read-write
1004      section which can be resolved at run-time.  */
1005   bfd_signed_vma func_pointer_refcount;
1006
1007   /* Information about the GOT PLT entry. Filled when there are both
1008      GOT and PLT relocations against the same function.  */
1009   union gotplt_union plt_got;
1010
1011   /* Information about the second PLT entry.   */
1012   union gotplt_union plt_second;
1013
1014   /* Offset of the GOTPLT entry reserved for the TLS descriptor,
1015      starting at the end of the jump table.  */
1016   bfd_vma tlsdesc_got;
1017 };
1018
1019 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
1020
1021 struct elf_i386_obj_tdata
1022 {
1023   struct elf_obj_tdata root;
1024
1025   /* tls_type for each local got entry.  */
1026   char *local_got_tls_type;
1027
1028   /* GOTPLT entries for TLS descriptors.  */
1029   bfd_vma *local_tlsdesc_gotent;
1030 };
1031
1032 #define elf_i386_tdata(abfd) \
1033   ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
1034
1035 #define elf_i386_local_got_tls_type(abfd) \
1036   (elf_i386_tdata (abfd)->local_got_tls_type)
1037
1038 #define elf_i386_local_tlsdesc_gotent(abfd) \
1039   (elf_i386_tdata (abfd)->local_tlsdesc_gotent)
1040
1041 #define is_i386_elf(bfd)                                \
1042   (bfd_get_flavour (bfd) == bfd_target_elf_flavour      \
1043    && elf_tdata (bfd) != NULL                           \
1044    && elf_object_id (bfd) == I386_ELF_DATA)
1045
1046 static bfd_boolean
1047 elf_i386_mkobject (bfd *abfd)
1048 {
1049   return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata),
1050                                   I386_ELF_DATA);
1051 }
1052
1053 /* i386 ELF linker hash table.  */
1054
1055 struct elf_i386_link_hash_table
1056 {
1057   struct elf_link_hash_table elf;
1058
1059   /* Short-cuts to get to dynamic linker sections.  */
1060   asection *interp;
1061   asection *plt_eh_frame;
1062   asection *plt_second;
1063   asection *plt_second_eh_frame;
1064   asection *plt_got;
1065   asection *plt_got_eh_frame;
1066
1067   /* Parameters describing PLT generation.  */
1068   struct elf_i386_plt_layout plt;
1069
1070   /* Parameters describing lazy PLT generation.  */
1071   const struct elf_i386_lazy_plt_layout *lazy_plt;
1072
1073   /* Parameters describing non-lazy PLT generation.  */
1074   const struct elf_i386_non_lazy_plt_layout *non_lazy_plt;
1075
1076   union
1077   {
1078     bfd_signed_vma refcount;
1079     bfd_vma offset;
1080   } tls_ldm_got;
1081
1082   /* The amount of space used by the reserved portion of the sgotplt
1083      section, plus whatever space is used by the jump slots.  */
1084   bfd_vma sgotplt_jump_table_size;
1085
1086   /* Small local sym cache.  */
1087   struct sym_cache sym_cache;
1088
1089   /* _TLS_MODULE_BASE_ symbol.  */
1090   struct bfd_link_hash_entry *tls_module_base;
1091
1092   /* Used by local STT_GNU_IFUNC symbols.  */
1093   htab_t loc_hash_table;
1094   void * loc_hash_memory;
1095
1096   /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.  */
1097   asection *srelplt2;
1098
1099   /* The index of the next unused R_386_TLS_DESC slot in .rel.plt.  */
1100   bfd_vma next_tls_desc_index;
1101
1102   /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt.  */
1103   bfd_vma next_jump_slot_index;
1104
1105   /* The index of the next unused R_386_IRELATIVE slot in .rel.plt.  */
1106   bfd_vma next_irelative_index;
1107
1108   /* TRUE if there are dynamic relocs against IFUNC symbols that apply
1109      to read-only sections.  */
1110   bfd_boolean readonly_dynrelocs_against_ifunc;
1111 };
1112
1113 /* Get the i386 ELF linker hash table from a link_info structure.  */
1114
1115 #define elf_i386_hash_table(p) \
1116   (elf_hash_table_id  ((struct elf_link_hash_table *) ((p)->hash)) \
1117   == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL)
1118
1119 #define elf_i386_compute_jump_table_size(htab) \
1120   ((htab)->elf.srelplt->reloc_count * 4)
1121
1122 /* Create an entry in an i386 ELF linker hash table.  */
1123
1124 static struct bfd_hash_entry *
1125 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry,
1126                             struct bfd_hash_table *table,
1127                             const char *string)
1128 {
1129   /* Allocate the structure if it has not already been allocated by a
1130      subclass.  */
1131   if (entry == NULL)
1132     {
1133       entry = (struct bfd_hash_entry *)
1134           bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry));
1135       if (entry == NULL)
1136         return entry;
1137     }
1138
1139   /* Call the allocation method of the superclass.  */
1140   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
1141   if (entry != NULL)
1142     {
1143       struct elf_i386_link_hash_entry *eh;
1144
1145       eh = (struct elf_i386_link_hash_entry *) entry;
1146       eh->dyn_relocs = NULL;
1147       eh->tls_type = GOT_UNKNOWN;
1148       eh->gotoff_ref = 0;
1149       eh->has_got_reloc = 0;
1150       eh->has_non_got_reloc = 0;
1151       eh->no_finish_dynamic_symbol = 0;
1152       eh->tls_get_addr = 2;
1153       eh->func_pointer_refcount = 0;
1154       eh->plt_got.offset = (bfd_vma) -1;
1155       eh->tlsdesc_got = (bfd_vma) -1;
1156     }
1157
1158   return entry;
1159 }
1160
1161 /* Compute a hash of a local hash entry.  We use elf_link_hash_entry
1162   for local symbol so that we can handle local STT_GNU_IFUNC symbols
1163   as global symbol.  We reuse indx and dynstr_index for local symbol
1164   hash since they aren't used by global symbols in this backend.  */
1165
1166 static hashval_t
1167 elf_i386_local_htab_hash (const void *ptr)
1168 {
1169   struct elf_link_hash_entry *h
1170     = (struct elf_link_hash_entry *) ptr;
1171   return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
1172 }
1173
1174 /* Compare local hash entries.  */
1175
1176 static int
1177 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2)
1178 {
1179   struct elf_link_hash_entry *h1
1180      = (struct elf_link_hash_entry *) ptr1;
1181   struct elf_link_hash_entry *h2
1182     = (struct elf_link_hash_entry *) ptr2;
1183
1184   return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
1185 }
1186
1187 /* Find and/or create a hash entry for local symbol.  */
1188
1189 static struct elf_link_hash_entry *
1190 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab,
1191                              bfd *abfd, const Elf_Internal_Rela *rel,
1192                              bfd_boolean create)
1193 {
1194   struct elf_i386_link_hash_entry e, *ret;
1195   asection *sec = abfd->sections;
1196   hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
1197                                        ELF32_R_SYM (rel->r_info));
1198   void **slot;
1199
1200   e.elf.indx = sec->id;
1201   e.elf.dynstr_index = ELF32_R_SYM (rel->r_info);
1202   slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
1203                                    create ? INSERT : NO_INSERT);
1204
1205   if (!slot)
1206     return NULL;
1207
1208   if (*slot)
1209     {
1210       ret = (struct elf_i386_link_hash_entry *) *slot;
1211       return &ret->elf;
1212     }
1213
1214   ret = (struct elf_i386_link_hash_entry *)
1215         objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
1216                         sizeof (struct elf_i386_link_hash_entry));
1217   if (ret)
1218     {
1219       memset (ret, 0, sizeof (*ret));
1220       ret->elf.indx = sec->id;
1221       ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info);
1222       ret->elf.dynindx = -1;
1223       ret->func_pointer_refcount = 0;
1224       ret->plt_got.offset = (bfd_vma) -1;
1225       *slot = ret;
1226     }
1227   return &ret->elf;
1228 }
1229
1230 /* Destroy an i386 ELF linker hash table.  */
1231
1232 static void
1233 elf_i386_link_hash_table_free (bfd *obfd)
1234 {
1235   struct elf_i386_link_hash_table *htab
1236     = (struct elf_i386_link_hash_table *) obfd->link.hash;
1237
1238   if (htab->loc_hash_table)
1239     htab_delete (htab->loc_hash_table);
1240   if (htab->loc_hash_memory)
1241     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
1242   _bfd_elf_link_hash_table_free (obfd);
1243 }
1244
1245 /* Create an i386 ELF linker hash table.  */
1246
1247 static struct bfd_link_hash_table *
1248 elf_i386_link_hash_table_create (bfd *abfd)
1249 {
1250   struct elf_i386_link_hash_table *ret;
1251   bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
1252
1253   ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt);
1254   if (ret == NULL)
1255     return NULL;
1256
1257   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1258                                       elf_i386_link_hash_newfunc,
1259                                       sizeof (struct elf_i386_link_hash_entry),
1260                                       I386_ELF_DATA))
1261     {
1262       free (ret);
1263       return NULL;
1264     }
1265
1266   ret->loc_hash_table = htab_try_create (1024,
1267                                          elf_i386_local_htab_hash,
1268                                          elf_i386_local_htab_eq,
1269                                          NULL);
1270   ret->loc_hash_memory = objalloc_create ();
1271   if (!ret->loc_hash_table || !ret->loc_hash_memory)
1272     {
1273       elf_i386_link_hash_table_free (abfd);
1274       return NULL;
1275     }
1276   ret->elf.root.hash_table_free = elf_i386_link_hash_table_free;
1277
1278   return &ret->elf.root;
1279 }
1280
1281 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1282
1283 static void
1284 elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
1285                                struct elf_link_hash_entry *dir,
1286                                struct elf_link_hash_entry *ind)
1287 {
1288   struct elf_i386_link_hash_entry *edir, *eind;
1289
1290   edir = (struct elf_i386_link_hash_entry *) dir;
1291   eind = (struct elf_i386_link_hash_entry *) ind;
1292
1293   if (eind->dyn_relocs != NULL)
1294     {
1295       if (edir->dyn_relocs != NULL)
1296         {
1297           struct elf_dyn_relocs **pp;
1298           struct elf_dyn_relocs *p;
1299
1300           /* Add reloc counts against the indirect sym to the direct sym
1301              list.  Merge any entries against the same section.  */
1302           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1303             {
1304               struct elf_dyn_relocs *q;
1305
1306               for (q = edir->dyn_relocs; q != NULL; q = q->next)
1307                 if (q->sec == p->sec)
1308                   {
1309                     q->pc_count += p->pc_count;
1310                     q->count += p->count;
1311                     *pp = p->next;
1312                     break;
1313                   }
1314               if (q == NULL)
1315                 pp = &p->next;
1316             }
1317           *pp = edir->dyn_relocs;
1318         }
1319
1320       edir->dyn_relocs = eind->dyn_relocs;
1321       eind->dyn_relocs = NULL;
1322     }
1323
1324   if (ind->root.type == bfd_link_hash_indirect
1325       && dir->got.refcount <= 0)
1326     {
1327       edir->tls_type = eind->tls_type;
1328       eind->tls_type = GOT_UNKNOWN;
1329     }
1330
1331   /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
1332      generate a R_386_COPY reloc.  */
1333   edir->gotoff_ref |= eind->gotoff_ref;
1334
1335   edir->has_got_reloc |= eind->has_got_reloc;
1336   edir->has_non_got_reloc |= eind->has_non_got_reloc;
1337
1338   if (ELIMINATE_COPY_RELOCS
1339       && ind->root.type != bfd_link_hash_indirect
1340       && dir->dynamic_adjusted)
1341     {
1342       /* If called to transfer flags for a weakdef during processing
1343          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1344          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
1345       if (dir->versioned != versioned_hidden)
1346         dir->ref_dynamic |= ind->ref_dynamic;
1347       dir->ref_regular |= ind->ref_regular;
1348       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1349       dir->needs_plt |= ind->needs_plt;
1350       dir->pointer_equality_needed |= ind->pointer_equality_needed;
1351     }
1352   else
1353     {
1354       if (eind->func_pointer_refcount > 0)
1355         {
1356           edir->func_pointer_refcount += eind->func_pointer_refcount;
1357           eind->func_pointer_refcount = 0;
1358         }
1359
1360       _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1361     }
1362 }
1363
1364 /* Return TRUE if the TLS access code sequence support transition
1365    from R_TYPE.  */
1366
1367 static bfd_boolean
1368 elf_i386_check_tls_transition (asection *sec,
1369                                bfd_byte *contents,
1370                                Elf_Internal_Shdr *symtab_hdr,
1371                                struct elf_link_hash_entry **sym_hashes,
1372                                unsigned int r_type,
1373                                const Elf_Internal_Rela *rel,
1374                                const Elf_Internal_Rela *relend)
1375 {
1376   unsigned int val, type, reg;
1377   unsigned long r_symndx;
1378   struct elf_link_hash_entry *h;
1379   bfd_vma offset;
1380   bfd_byte *call;
1381   bfd_boolean indirect_call, tls_get_addr;
1382
1383   offset = rel->r_offset;
1384   switch (r_type)
1385     {
1386     case R_386_TLS_GD:
1387     case R_386_TLS_LDM:
1388       if (offset < 2 || (rel + 1) >= relend)
1389         return FALSE;
1390
1391       indirect_call = FALSE;
1392       call = contents + offset + 4;
1393       val = *(call - 5);
1394       type = *(call - 6);
1395       if (r_type == R_386_TLS_GD)
1396         {
1397           /* Check transition from GD access model.  Only
1398                 leal foo@tlsgd(,%ebx,1), %eax
1399                 call ___tls_get_addr@PLT
1400              or
1401                 leal foo@tlsgd(%ebx) %eax
1402                 call ___tls_get_addr@PLT
1403                 nop
1404              or
1405                 leal foo@tlsgd(%reg), %eax
1406                 call *___tls_get_addr@GOT(%reg)
1407                 which may be converted to
1408                 addr32 call ___tls_get_addr
1409              can transit to different access model.  */
1410           if ((offset + 10) > sec->size
1411               || (type != 0x8d && type != 0x04))
1412             return FALSE;
1413
1414           if (type == 0x04)
1415             {
1416               /* leal foo@tlsgd(,%ebx,1), %eax
1417                  call ___tls_get_addr@PLT  */
1418               if (offset < 3)
1419                 return FALSE;
1420
1421               if (*(call - 7) != 0x8d
1422                   || val != 0x1d
1423                   || call[0] != 0xe8)
1424                 return FALSE;
1425             }
1426           else
1427             {
1428               /* This must be
1429                         leal foo@tlsgd(%ebx), %eax
1430                         call ___tls_get_addr@PLT
1431                         nop
1432                  or
1433                         leal foo@tlsgd(%reg), %eax
1434                         call *___tls_get_addr@GOT(%reg)
1435                         which may be converted to
1436                         addr32 call ___tls_get_addr
1437
1438                  %eax can't be used as the GOT base register since it
1439                  is used to pass parameter to ___tls_get_addr.  */
1440               reg = val & 7;
1441               if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1442                 return FALSE;
1443
1444               indirect_call = call[0] == 0xff;
1445               if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
1446                   && !(call[0] == 0x67 && call[1] == 0xe8)
1447                   && !(indirect_call
1448                        && (call[1] & 0xf8) == 0x90
1449                        && (call[1] & 0x7) == reg))
1450                 return FALSE;
1451             }
1452         }
1453       else
1454         {
1455           /* Check transition from LD access model.  Only
1456                 leal foo@tlsldm(%ebx), %eax
1457                 call ___tls_get_addr@PLT
1458              or
1459                 leal foo@tlsldm(%reg), %eax
1460                 call *___tls_get_addr@GOT(%reg)
1461                 which may be converted to
1462                 addr32 call ___tls_get_addr
1463              can transit to different access model.  */
1464           if (type != 0x8d || (offset + 9) > sec->size)
1465             return FALSE;
1466
1467           /* %eax can't be used as the GOT base register since it is
1468              used to pass parameter to ___tls_get_addr.  */
1469           reg = val & 7;
1470           if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
1471             return FALSE;
1472
1473           indirect_call = call[0] == 0xff;
1474           if (!(reg == 3 && call[0] == 0xe8)
1475               && !(call[0] == 0x67 && call[1] == 0xe8)
1476               && !(indirect_call
1477                    && (call[1] & 0xf8) == 0x90
1478                    && (call[1] & 0x7) == reg))
1479             return FALSE;
1480         }
1481
1482       r_symndx = ELF32_R_SYM (rel[1].r_info);
1483       if (r_symndx < symtab_hdr->sh_info)
1484         return FALSE;
1485
1486       tls_get_addr = FALSE;
1487       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1488       if (h != NULL && h->root.root.string != NULL)
1489         {
1490           struct elf_i386_link_hash_entry *eh
1491             = (struct elf_i386_link_hash_entry *) h;
1492           tls_get_addr = eh->tls_get_addr == 1;
1493           if (eh->tls_get_addr > 1)
1494             {
1495               /* Use strncmp to check ___tls_get_addr since
1496                  ___tls_get_addr may be versioned.  */
1497               if (strncmp (h->root.root.string, "___tls_get_addr", 15)
1498                   == 0)
1499                 {
1500                   eh->tls_get_addr = 1;
1501                   tls_get_addr = TRUE;
1502                 }
1503               else
1504                 eh->tls_get_addr = 0;
1505             }
1506         }
1507
1508       if (!tls_get_addr)
1509         return FALSE;
1510       else if (indirect_call)
1511         return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
1512       else
1513         return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1514                 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
1515
1516     case R_386_TLS_IE:
1517       /* Check transition from IE access model:
1518                 movl foo@indntpoff(%rip), %eax
1519                 movl foo@indntpoff(%rip), %reg
1520                 addl foo@indntpoff(%rip), %reg
1521        */
1522
1523       if (offset < 1 || (offset + 4) > sec->size)
1524         return FALSE;
1525
1526       /* Check "movl foo@tpoff(%rip), %eax" first.  */
1527       val = bfd_get_8 (abfd, contents + offset - 1);
1528       if (val == 0xa1)
1529         return TRUE;
1530
1531       if (offset < 2)
1532         return FALSE;
1533
1534       /* Check movl|addl foo@tpoff(%rip), %reg.   */
1535       type = bfd_get_8 (abfd, contents + offset - 2);
1536       return ((type == 0x8b || type == 0x03)
1537               && (val & 0xc7) == 0x05);
1538
1539     case R_386_TLS_GOTIE:
1540     case R_386_TLS_IE_32:
1541       /* Check transition from {IE_32,GOTIE} access model:
1542                 subl foo@{tpoff,gontoff}(%reg1), %reg2
1543                 movl foo@{tpoff,gontoff}(%reg1), %reg2
1544                 addl foo@{tpoff,gontoff}(%reg1), %reg2
1545        */
1546
1547       if (offset < 2 || (offset + 4) > sec->size)
1548         return FALSE;
1549
1550       val = bfd_get_8 (abfd, contents + offset - 1);
1551       if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1552         return FALSE;
1553
1554       type = bfd_get_8 (abfd, contents + offset - 2);
1555       return type == 0x8b || type == 0x2b || type == 0x03;
1556
1557     case R_386_TLS_GOTDESC:
1558       /* Check transition from GDesc access model:
1559                 leal x@tlsdesc(%ebx), %eax
1560
1561          Make sure it's a leal adding ebx to a 32-bit offset
1562          into any register, although it's probably almost always
1563          going to be eax.  */
1564
1565       if (offset < 2 || (offset + 4) > sec->size)
1566         return FALSE;
1567
1568       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1569         return FALSE;
1570
1571       val = bfd_get_8 (abfd, contents + offset - 1);
1572       return (val & 0xc7) == 0x83;
1573
1574     case R_386_TLS_DESC_CALL:
1575       /* Check transition from GDesc access model:
1576                 call *x@tlsdesc(%eax)
1577        */
1578       if (offset + 2 <= sec->size)
1579         {
1580           /* Make sure that it's a call *x@tlsdesc(%eax).  */
1581           call = contents + offset;
1582           return call[0] == 0xff && call[1] == 0x10;
1583         }
1584
1585       return FALSE;
1586
1587     default:
1588       abort ();
1589     }
1590 }
1591
1592 /* Return TRUE if the TLS access transition is OK or no transition
1593    will be performed.  Update R_TYPE if there is a transition.  */
1594
1595 static bfd_boolean
1596 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1597                          asection *sec, bfd_byte *contents,
1598                          Elf_Internal_Shdr *symtab_hdr,
1599                          struct elf_link_hash_entry **sym_hashes,
1600                          unsigned int *r_type, int tls_type,
1601                          const Elf_Internal_Rela *rel,
1602                          const Elf_Internal_Rela *relend,
1603                          struct elf_link_hash_entry *h,
1604                          unsigned long r_symndx,
1605                          bfd_boolean from_relocate_section)
1606 {
1607   unsigned int from_type = *r_type;
1608   unsigned int to_type = from_type;
1609   bfd_boolean check = TRUE;
1610
1611   /* Skip TLS transition for functions.  */
1612   if (h != NULL
1613       && (h->type == STT_FUNC
1614           || h->type == STT_GNU_IFUNC))
1615     return TRUE;
1616
1617   switch (from_type)
1618     {
1619     case R_386_TLS_GD:
1620     case R_386_TLS_GOTDESC:
1621     case R_386_TLS_DESC_CALL:
1622     case R_386_TLS_IE_32:
1623     case R_386_TLS_IE:
1624     case R_386_TLS_GOTIE:
1625       if (bfd_link_executable (info))
1626         {
1627           if (h == NULL)
1628             to_type = R_386_TLS_LE_32;
1629           else if (from_type != R_386_TLS_IE
1630                    && from_type != R_386_TLS_GOTIE)
1631             to_type = R_386_TLS_IE_32;
1632         }
1633
1634       /* When we are called from elf_i386_relocate_section, there may
1635          be additional transitions based on TLS_TYPE.  */
1636       if (from_relocate_section)
1637         {
1638           unsigned int new_to_type = to_type;
1639
1640           if (bfd_link_executable (info)
1641               && h != NULL
1642               && h->dynindx == -1
1643               && (tls_type & GOT_TLS_IE))
1644             new_to_type = R_386_TLS_LE_32;
1645
1646           if (to_type == R_386_TLS_GD
1647               || to_type == R_386_TLS_GOTDESC
1648               || to_type == R_386_TLS_DESC_CALL)
1649             {
1650               if (tls_type == GOT_TLS_IE_POS)
1651                 new_to_type = R_386_TLS_GOTIE;
1652               else if (tls_type & GOT_TLS_IE)
1653                 new_to_type = R_386_TLS_IE_32;
1654             }
1655
1656           /* We checked the transition before when we were called from
1657              elf_i386_check_relocs.  We only want to check the new
1658              transition which hasn't been checked before.  */
1659           check = new_to_type != to_type && from_type == to_type;
1660           to_type = new_to_type;
1661         }
1662
1663       break;
1664
1665     case R_386_TLS_LDM:
1666       if (bfd_link_executable (info))
1667         to_type = R_386_TLS_LE_32;
1668       break;
1669
1670     default:
1671       return TRUE;
1672     }
1673
1674   /* Return TRUE if there is no transition.  */
1675   if (from_type == to_type)
1676     return TRUE;
1677
1678   /* Check if the transition can be performed.  */
1679   if (check
1680       && ! elf_i386_check_tls_transition (sec, contents,
1681                                           symtab_hdr, sym_hashes,
1682                                           from_type, rel, relend))
1683     {
1684       reloc_howto_type *from, *to;
1685       const char *name;
1686
1687       from = elf_i386_rtype_to_howto (abfd, from_type);
1688       to = elf_i386_rtype_to_howto (abfd, to_type);
1689
1690       if (h)
1691         name = h->root.root.string;
1692       else
1693         {
1694           struct elf_i386_link_hash_table *htab;
1695
1696           htab = elf_i386_hash_table (info);
1697           if (htab == NULL)
1698             name = "*unknown*";
1699           else
1700             {
1701               Elf_Internal_Sym *isym;
1702
1703               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1704                                             abfd, r_symndx);
1705               name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1706             }
1707         }
1708
1709       _bfd_error_handler
1710         /* xgettext:c-format */
1711         (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
1712            "in section `%A' failed"),
1713          abfd, from->name, to->name, name,
1714          rel->r_offset, sec);
1715       bfd_set_error (bfd_error_bad_value);
1716       return FALSE;
1717     }
1718
1719   *r_type = to_type;
1720   return TRUE;
1721 }
1722
1723 /* With the local symbol, foo, we convert
1724    mov foo@GOT[(%reg1)], %reg2
1725    to
1726    lea foo[@GOTOFF(%reg1)], %reg2
1727    and convert
1728    call/jmp *foo@GOT[(%reg)]
1729    to
1730    nop call foo/jmp foo nop
1731    When PIC is false, convert
1732    test %reg1, foo@GOT[(%reg2)]
1733    to
1734    test $foo, %reg1
1735    and convert
1736    binop foo@GOT[(%reg1)], %reg2
1737    to
1738    binop $foo, %reg2
1739    where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1740    instructions.  */
1741
1742 static
1743 bfd_boolean
1744 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1745                              bfd_byte *contents,
1746                              Elf_Internal_Rela *irel,
1747                              struct elf_link_hash_entry *h,
1748                              bfd_boolean *converted,
1749                              struct bfd_link_info *link_info)
1750 {
1751   struct elf_i386_link_hash_table *htab;
1752   unsigned int opcode;
1753   unsigned int modrm;
1754   bfd_boolean baseless;
1755   Elf_Internal_Sym *isym;
1756   unsigned int addend;
1757   unsigned int nop;
1758   bfd_vma nop_offset;
1759   bfd_boolean is_pic;
1760   bfd_boolean to_reloc_32;
1761   unsigned int r_type;
1762   unsigned int r_symndx;
1763   bfd_vma roff = irel->r_offset;
1764
1765   if (roff < 2)
1766     return TRUE;
1767
1768   /* Addend for R_386_GOT32X relocations must be 0.  */
1769   addend = bfd_get_32 (abfd, contents + roff);
1770   if (addend != 0)
1771     return TRUE;
1772
1773   htab = elf_i386_hash_table (link_info);
1774   is_pic = bfd_link_pic (link_info);
1775
1776   r_type = ELF32_R_TYPE (irel->r_info);
1777   r_symndx = ELF32_R_SYM (irel->r_info);
1778
1779   modrm = bfd_get_8 (abfd, contents + roff - 1);
1780   baseless = (modrm & 0xc7) == 0x5;
1781
1782   if (baseless && is_pic)
1783     {
1784       /* For PIC, disallow R_386_GOT32X without a base register
1785          since we don't know what the GOT base is.  */
1786       const char *name;
1787
1788       if (h == NULL)
1789         {
1790           isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1791                                         r_symndx);
1792           name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1793         }
1794       else
1795         name = h->root.root.string;
1796
1797       _bfd_error_handler
1798         /* xgettext:c-format */
1799         (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1800            " register can not be used when making a shared object"),
1801          abfd, name);
1802       return FALSE;
1803     }
1804
1805   opcode = bfd_get_8 (abfd, contents + roff - 2);
1806
1807   /* Convert to R_386_32 if PIC is false or there is no base
1808      register.  */
1809   to_reloc_32 = !is_pic || baseless;
1810
1811   /* Try to convert R_386_GOT32X.  Get the symbol referred to by the
1812      reloc.  */
1813   if (h == NULL)
1814     {
1815       if (opcode == 0x0ff)
1816         /* Convert "call/jmp *foo@GOT[(%reg)]".  */
1817         goto convert_branch;
1818       else
1819         /* Convert "mov foo@GOT[(%reg1)], %reg2",
1820            "test %reg1, foo@GOT(%reg2)" and
1821            "binop foo@GOT[(%reg1)], %reg2". */
1822         goto convert_load;
1823     }
1824
1825   /* Undefined weak symbol is only bound locally in executable
1826      and its reference is resolved as 0.  */
1827   if (UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info, TRUE,
1828                                        elf_i386_hash_entry (h)))
1829     {
1830       if (opcode == 0xff)
1831         {
1832           /* No direct branch to 0 for PIC.  */
1833           if (is_pic)
1834             return TRUE;
1835           else
1836             goto convert_branch;
1837         }
1838       else
1839         {
1840           /* We can convert load of address 0 to R_386_32.  */
1841           to_reloc_32 = TRUE;
1842           goto convert_load;
1843         }
1844     }
1845
1846   if (opcode == 0xff)
1847     {
1848       /* We have "call/jmp *foo@GOT[(%reg)]".  */
1849       if ((h->root.type == bfd_link_hash_defined
1850            || h->root.type == bfd_link_hash_defweak)
1851           && SYMBOL_REFERENCES_LOCAL (link_info, h))
1852         {
1853           /* The function is locally defined.   */
1854 convert_branch:
1855           /* Convert R_386_GOT32X to R_386_PC32.  */
1856           if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1857             {
1858               struct elf_i386_link_hash_entry *eh
1859                 = (struct elf_i386_link_hash_entry *) h;
1860
1861               /* Convert to "nop call foo".  ADDR_PREFIX_OPCODE
1862                  is a nop prefix.  */
1863               modrm = 0xe8;
1864               /* To support TLS optimization, always use addr32 prefix
1865                  for "call *___tls_get_addr@GOT(%reg)".  */
1866               if (eh && eh->tls_get_addr == 1)
1867                 {
1868                   nop = 0x67;
1869                   nop_offset = irel->r_offset - 2;
1870                 }
1871               else
1872                 {
1873                   nop = link_info->call_nop_byte;
1874                   if (link_info->call_nop_as_suffix)
1875                     {
1876                       nop_offset = roff + 3;
1877                       irel->r_offset -= 1;
1878                     }
1879                   else
1880                     nop_offset = roff - 2;
1881                 }
1882             }
1883           else
1884             {
1885               /* Convert to "jmp foo nop".  */
1886               modrm = 0xe9;
1887               nop = NOP_OPCODE;
1888               nop_offset = roff + 3;
1889               irel->r_offset -= 1;
1890             }
1891
1892           bfd_put_8 (abfd, nop, contents + nop_offset);
1893           bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1894           /* When converting to PC-relative relocation, we
1895              need to adjust addend by -4.  */
1896           bfd_put_32 (abfd, -4, contents + irel->r_offset);
1897           irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1898
1899           *converted = TRUE;
1900         }
1901     }
1902   else
1903     {
1904       /* We have "mov foo@GOT[(%re1g)], %reg2",
1905          "test %reg1, foo@GOT(%reg2)" and
1906          "binop foo@GOT[(%reg1)], %reg2".
1907
1908          Avoid optimizing _DYNAMIC since ld.so may use its
1909          link-time address.  */
1910       if (h == htab->elf.hdynamic)
1911         return TRUE;
1912
1913       /* def_regular is set by an assignment in a linker script in
1914          bfd_elf_record_link_assignment.  start_stop is set on
1915          __start_SECNAME/__stop_SECNAME which mark section SECNAME.  */
1916       if (h->start_stop
1917           || ((h->def_regular
1918                || h->root.type == bfd_link_hash_defined
1919                || h->root.type == bfd_link_hash_defweak)
1920               && SYMBOL_REFERENCES_LOCAL (link_info, h)))
1921         {
1922 convert_load:
1923           if (opcode == 0x8b)
1924             {
1925               if (to_reloc_32)
1926                 {
1927                   /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1928                      "mov $foo, %reg2" with R_386_32.  */
1929                   r_type = R_386_32;
1930                   modrm = 0xc0 | (modrm & 0x38) >> 3;
1931                   bfd_put_8 (abfd, modrm, contents + roff - 1);
1932                   opcode = 0xc7;
1933                 }
1934               else
1935                 {
1936                   /* Convert "mov foo@GOT(%reg1), %reg2" to
1937                      "lea foo@GOTOFF(%reg1), %reg2".  */
1938                   r_type = R_386_GOTOFF;
1939                   opcode = 0x8d;
1940                 }
1941             }
1942           else
1943             {
1944               /* Only R_386_32 is supported.  */
1945               if (!to_reloc_32)
1946                 return TRUE;
1947
1948               if (opcode == 0x85)
1949                 {
1950                   /* Convert "test %reg1, foo@GOT(%reg2)" to
1951                      "test $foo, %reg1".  */
1952                   modrm = 0xc0 | (modrm & 0x38) >> 3;
1953                   opcode = 0xf7;
1954                 }
1955               else
1956                 {
1957                   /* Convert "binop foo@GOT(%reg1), %reg2" to
1958                      "binop $foo, %reg2".  */
1959                   modrm = (0xc0
1960                            | (modrm & 0x38) >> 3
1961                            | (opcode & 0x3c));
1962                   opcode = 0x81;
1963                 }
1964               bfd_put_8 (abfd, modrm, contents + roff - 1);
1965               r_type = R_386_32;
1966             }
1967
1968           bfd_put_8 (abfd, opcode, contents + roff - 2);
1969           irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1970
1971           *converted = TRUE;
1972         }
1973     }
1974
1975   return TRUE;
1976 }
1977
1978 /* Rename some of the generic section flags to better document how they
1979    are used here.  */
1980 #define need_convert_load       sec_flg0
1981 #define check_relocs_failed     sec_flg1
1982
1983 /* Look through the relocs for a section during the first phase, and
1984    calculate needed space in the global offset table, procedure linkage
1985    table, and dynamic reloc sections.  */
1986
1987 static bfd_boolean
1988 elf_i386_check_relocs (bfd *abfd,
1989                        struct bfd_link_info *info,
1990                        asection *sec,
1991                        const Elf_Internal_Rela *relocs)
1992 {
1993   struct elf_i386_link_hash_table *htab;
1994   Elf_Internal_Shdr *symtab_hdr;
1995   struct elf_link_hash_entry **sym_hashes;
1996   const Elf_Internal_Rela *rel;
1997   const Elf_Internal_Rela *rel_end;
1998   asection *sreloc;
1999   bfd_byte *contents;
2000
2001   if (bfd_link_relocatable (info))
2002     return TRUE;
2003
2004   /* Don't do anything special with non-loaded, non-alloced sections.
2005      In particular, any relocs in such sections should not affect GOT
2006      and PLT reference counting (ie. we don't allow them to create GOT
2007      or PLT entries), there's no possibility or desire to optimize TLS
2008      relocs, and there's not much point in propagating relocs to shared
2009      libs that the dynamic linker won't relocate.  */
2010   if ((sec->flags & SEC_ALLOC) == 0)
2011     return TRUE;
2012
2013   BFD_ASSERT (is_i386_elf (abfd));
2014
2015   htab = elf_i386_hash_table (info);
2016   if (htab == NULL)
2017     {
2018       sec->check_relocs_failed = 1;
2019       return FALSE;
2020     }
2021
2022   /* Get the section contents.  */
2023   if (elf_section_data (sec)->this_hdr.contents != NULL)
2024     contents = elf_section_data (sec)->this_hdr.contents;
2025   else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2026     {
2027       sec->check_relocs_failed = 1;
2028       return FALSE;
2029     }
2030
2031   symtab_hdr = &elf_symtab_hdr (abfd);
2032   sym_hashes = elf_sym_hashes (abfd);
2033
2034   sreloc = NULL;
2035
2036   rel_end = relocs + sec->reloc_count;
2037   for (rel = relocs; rel < rel_end; rel++)
2038     {
2039       unsigned int r_type;
2040       unsigned int r_symndx;
2041       struct elf_link_hash_entry *h;
2042       struct elf_i386_link_hash_entry *eh;
2043       Elf_Internal_Sym *isym;
2044       const char *name;
2045       bfd_boolean size_reloc;
2046
2047       r_symndx = ELF32_R_SYM (rel->r_info);
2048       r_type = ELF32_R_TYPE (rel->r_info);
2049
2050       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2051         {
2052           /* xgettext:c-format */
2053           _bfd_error_handler (_("%B: bad symbol index: %d"),
2054                               abfd, r_symndx);
2055           goto error_return;
2056         }
2057
2058       if (r_symndx < symtab_hdr->sh_info)
2059         {
2060           /* A local symbol.  */
2061           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2062                                         abfd, r_symndx);
2063           if (isym == NULL)
2064             goto error_return;
2065
2066           /* Check relocation against local STT_GNU_IFUNC symbol.  */
2067           if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2068             {
2069               h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE);
2070               if (h == NULL)
2071                 goto error_return;
2072
2073               /* Fake a STT_GNU_IFUNC symbol.  */
2074               h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
2075                                                       isym, NULL);
2076               h->type = STT_GNU_IFUNC;
2077               h->def_regular = 1;
2078               h->ref_regular = 1;
2079               h->forced_local = 1;
2080               h->root.type = bfd_link_hash_defined;
2081             }
2082           else
2083             h = NULL;
2084         }
2085       else
2086         {
2087           isym = NULL;
2088           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2089           while (h->root.type == bfd_link_hash_indirect
2090                  || h->root.type == bfd_link_hash_warning)
2091             h = (struct elf_link_hash_entry *) h->root.u.i.link;
2092         }
2093
2094       eh = (struct elf_i386_link_hash_entry *) h;
2095       if (h != NULL)
2096         {
2097           if (r_type == R_386_GOTOFF)
2098             eh->gotoff_ref = 1;
2099
2100           /* It is referenced by a non-shared object. */
2101           h->ref_regular = 1;
2102           h->root.non_ir_ref_regular = 1;
2103
2104           if (h->type == STT_GNU_IFUNC)
2105             elf_tdata (info->output_bfd)->has_gnu_symbols
2106               |= elf_gnu_symbol_ifunc;
2107         }
2108
2109       if (! elf_i386_tls_transition (info, abfd, sec, contents,
2110                                      symtab_hdr, sym_hashes,
2111                                      &r_type, GOT_UNKNOWN,
2112                                      rel, rel_end, h, r_symndx, FALSE))
2113         goto error_return;
2114
2115       switch (r_type)
2116         {
2117         case R_386_TLS_LDM:
2118           htab->tls_ldm_got.refcount += 1;
2119           goto create_got;
2120
2121         case R_386_PLT32:
2122           /* This symbol requires a procedure linkage table entry.  We
2123              actually build the entry in adjust_dynamic_symbol,
2124              because this might be a case of linking PIC code which is
2125              never referenced by a dynamic object, in which case we
2126              don't need to generate a procedure linkage table entry
2127              after all.  */
2128
2129           /* If this is a local symbol, we resolve it directly without
2130              creating a procedure linkage table entry.  */
2131           if (h == NULL)
2132             continue;
2133
2134           eh->has_got_reloc = 1;
2135           h->needs_plt = 1;
2136           h->plt.refcount += 1;
2137           break;
2138
2139         case R_386_SIZE32:
2140           size_reloc = TRUE;
2141           goto do_size;
2142
2143         case R_386_TLS_IE_32:
2144         case R_386_TLS_IE:
2145         case R_386_TLS_GOTIE:
2146           if (!bfd_link_executable (info))
2147             info->flags |= DF_STATIC_TLS;
2148           /* Fall through */
2149
2150         case R_386_GOT32:
2151         case R_386_GOT32X:
2152         case R_386_TLS_GD:
2153         case R_386_TLS_GOTDESC:
2154         case R_386_TLS_DESC_CALL:
2155           /* This symbol requires a global offset table entry.  */
2156           {
2157             int tls_type, old_tls_type;
2158
2159             switch (r_type)
2160               {
2161               default:
2162               case R_386_GOT32:
2163               case R_386_GOT32X:
2164                 tls_type = GOT_NORMAL;
2165                 break;
2166               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
2167               case R_386_TLS_GOTDESC:
2168               case R_386_TLS_DESC_CALL:
2169                 tls_type = GOT_TLS_GDESC; break;
2170               case R_386_TLS_IE_32:
2171                 if (ELF32_R_TYPE (rel->r_info) == r_type)
2172                   tls_type = GOT_TLS_IE_NEG;
2173                 else
2174                   /* If this is a GD->IE transition, we may use either of
2175                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
2176                   tls_type = GOT_TLS_IE;
2177                 break;
2178               case R_386_TLS_IE:
2179               case R_386_TLS_GOTIE:
2180                 tls_type = GOT_TLS_IE_POS; break;
2181               }
2182
2183             if (h != NULL)
2184               {
2185                 h->got.refcount += 1;
2186                 old_tls_type = elf_i386_hash_entry(h)->tls_type;
2187               }
2188             else
2189               {
2190                 bfd_signed_vma *local_got_refcounts;
2191
2192                 /* This is a global offset table entry for a local symbol.  */
2193                 local_got_refcounts = elf_local_got_refcounts (abfd);
2194                 if (local_got_refcounts == NULL)
2195                   {
2196                     bfd_size_type size;
2197
2198                     size = symtab_hdr->sh_info;
2199                     size *= (sizeof (bfd_signed_vma)
2200                              + sizeof (bfd_vma) + sizeof(char));
2201                     local_got_refcounts = (bfd_signed_vma *)
2202                         bfd_zalloc (abfd, size);
2203                     if (local_got_refcounts == NULL)
2204                       goto error_return;
2205                     elf_local_got_refcounts (abfd) = local_got_refcounts;
2206                     elf_i386_local_tlsdesc_gotent (abfd)
2207                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2208                     elf_i386_local_got_tls_type (abfd)
2209                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2210                   }
2211                 local_got_refcounts[r_symndx] += 1;
2212                 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
2213               }
2214
2215             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
2216               tls_type |= old_tls_type;
2217             /* If a TLS symbol is accessed using IE at least once,
2218                there is no point to use dynamic model for it.  */
2219             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2220                      && (! GOT_TLS_GD_ANY_P (old_tls_type)
2221                          || (tls_type & GOT_TLS_IE) == 0))
2222               {
2223                 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
2224                   tls_type = old_tls_type;
2225                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2226                          && GOT_TLS_GD_ANY_P (tls_type))
2227                   tls_type |= old_tls_type;
2228                 else
2229                   {
2230                     if (h)
2231                       name = h->root.root.string;
2232                     else
2233                       name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2234                                              NULL);
2235                     _bfd_error_handler
2236                       /* xgettext:c-format */
2237                       (_("%B: `%s' accessed both as normal and "
2238                          "thread local symbol"),
2239                        abfd, name);
2240                     bfd_set_error (bfd_error_bad_value);
2241                     goto error_return;
2242                   }
2243               }
2244
2245             if (old_tls_type != tls_type)
2246               {
2247                 if (h != NULL)
2248                   elf_i386_hash_entry (h)->tls_type = tls_type;
2249                 else
2250                   elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
2251               }
2252           }
2253           /* Fall through */
2254
2255         case R_386_GOTOFF:
2256         case R_386_GOTPC:
2257         create_got:
2258           if (r_type != R_386_TLS_IE)
2259             {
2260               if (eh != NULL)
2261                 eh->has_got_reloc = 1;
2262               break;
2263             }
2264           /* Fall through */
2265
2266         case R_386_TLS_LE_32:
2267         case R_386_TLS_LE:
2268           if (eh != NULL)
2269             eh->has_got_reloc = 1;
2270           if (bfd_link_executable (info))
2271             break;
2272           info->flags |= DF_STATIC_TLS;
2273           goto do_relocation;
2274
2275         case R_386_32:
2276         case R_386_PC32:
2277           if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2278             eh->has_non_got_reloc = 1;
2279 do_relocation:
2280           /* We are called after all symbols have been resolved.  Only
2281              relocation against STT_GNU_IFUNC symbol must go through
2282              PLT.  */
2283           if (h != NULL
2284               && (bfd_link_executable (info)
2285                   || h->type == STT_GNU_IFUNC))
2286             {
2287               /* If this reloc is in a read-only section, we might
2288                  need a copy reloc.  We can't check reliably at this
2289                  stage whether the section is read-only, as input
2290                  sections have not yet been mapped to output sections.
2291                  Tentatively set the flag for now, and correct in
2292                  adjust_dynamic_symbol.  */
2293               h->non_got_ref = 1;
2294
2295               /* We may need a .plt entry if the symbol is a function
2296                  defined in a shared lib or is a STT_GNU_IFUNC function
2297                  referenced from the code or read-only section.  */
2298               if (!h->def_regular
2299                   || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2300                 h->plt.refcount += 1;
2301
2302               if (r_type == R_386_PC32)
2303                 {
2304                   /* Since something like ".long foo - ." may be used
2305                      as pointer, make sure that PLT is used if foo is
2306                      a function defined in a shared library.  */
2307                   if ((sec->flags & SEC_CODE) == 0)
2308                     h->pointer_equality_needed = 1;
2309                   else if (h->type == STT_GNU_IFUNC
2310                            && bfd_link_pic (info))
2311                     {
2312                       _bfd_error_handler
2313                         /* xgettext:c-format */
2314                         (_("%B: unsupported non-PIC call to IFUNC `%s'"),
2315                          abfd, h->root.root.string);
2316                       bfd_set_error (bfd_error_bad_value);
2317                       goto error_return;
2318                     }
2319                 }
2320               else
2321                 {
2322                   h->pointer_equality_needed = 1;
2323                   /* R_386_32 can be resolved at run-time.  */
2324                   if (r_type == R_386_32
2325                       && (sec->flags & SEC_READONLY) == 0)
2326                     eh->func_pointer_refcount += 1;
2327                 }
2328             }
2329
2330           size_reloc = FALSE;
2331 do_size:
2332           /* If we are creating a shared library, and this is a reloc
2333              against a global symbol, or a non PC relative reloc
2334              against a local symbol, then we need to copy the reloc
2335              into the shared library.  However, if we are linking with
2336              -Bsymbolic, we do not need to copy a reloc against a
2337              global symbol which is defined in an object we are
2338              including in the link (i.e., DEF_REGULAR is set).  At
2339              this point we have not seen all the input files, so it is
2340              possible that DEF_REGULAR is not set now but will be set
2341              later (it is never cleared).  In case of a weak definition,
2342              DEF_REGULAR may be cleared later by a strong definition in
2343              a shared library.  We account for that possibility below by
2344              storing information in the relocs_copied field of the hash
2345              table entry.  A similar situation occurs when creating
2346              shared libraries and symbol visibility changes render the
2347              symbol local.
2348
2349              If on the other hand, we are creating an executable, we
2350              may need to keep relocations for symbols satisfied by a
2351              dynamic library if we manage to avoid copy relocs for the
2352              symbol.
2353
2354              Generate dynamic pointer relocation against STT_GNU_IFUNC
2355              symbol in the non-code section.  */
2356           if ((bfd_link_pic (info)
2357                && (r_type != R_386_PC32
2358                    || (h != NULL
2359                        && (! (bfd_link_pie (info)
2360                               || SYMBOLIC_BIND (info, h))
2361                            || h->root.type == bfd_link_hash_defweak
2362                            || !h->def_regular))))
2363               || (h != NULL
2364                   && h->type == STT_GNU_IFUNC
2365                   && r_type == R_386_32
2366                   && (sec->flags & SEC_CODE) == 0)
2367               || (ELIMINATE_COPY_RELOCS
2368                   && !bfd_link_pic (info)
2369                   && h != NULL
2370                   && (h->root.type == bfd_link_hash_defweak
2371                       || !h->def_regular)))
2372             {
2373               struct elf_dyn_relocs *p;
2374               struct elf_dyn_relocs **head;
2375
2376               /* We must copy these reloc types into the output file.
2377                  Create a reloc section in dynobj and make room for
2378                  this reloc.  */
2379               if (sreloc == NULL)
2380                 {
2381                   sreloc = _bfd_elf_make_dynamic_reloc_section
2382                     (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
2383
2384                   if (sreloc == NULL)
2385                     goto error_return;
2386                 }
2387
2388               /* If this is a global symbol, we count the number of
2389                  relocations we need for this symbol.  */
2390               if (h != NULL)
2391                 {
2392                   head = &eh->dyn_relocs;
2393                 }
2394               else
2395                 {
2396                   /* Track dynamic relocs needed for local syms too.
2397                      We really need local syms available to do this
2398                      easily.  Oh well.  */
2399                   void **vpp;
2400                   asection *s;
2401
2402                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2403                                                 abfd, r_symndx);
2404                   if (isym == NULL)
2405                     goto error_return;
2406
2407                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2408                   if (s == NULL)
2409                     s = sec;
2410
2411                   vpp = &elf_section_data (s)->local_dynrel;
2412                   head = (struct elf_dyn_relocs **)vpp;
2413                 }
2414
2415               p = *head;
2416               if (p == NULL || p->sec != sec)
2417                 {
2418                   bfd_size_type amt = sizeof *p;
2419                   p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
2420                                                            amt);
2421                   if (p == NULL)
2422                     goto error_return;
2423                   p->next = *head;
2424                   *head = p;
2425                   p->sec = sec;
2426                   p->count = 0;
2427                   p->pc_count = 0;
2428                 }
2429
2430               p->count += 1;
2431               /* Count size relocation as PC-relative relocation.  */
2432               if (r_type == R_386_PC32 || size_reloc)
2433                 p->pc_count += 1;
2434             }
2435           break;
2436
2437           /* This relocation describes the C++ object vtable hierarchy.
2438              Reconstruct it for later use during GC.  */
2439         case R_386_GNU_VTINHERIT:
2440           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2441             goto error_return;
2442           break;
2443
2444           /* This relocation describes which C++ vtable entries are actually
2445              used.  Record for later use during GC.  */
2446         case R_386_GNU_VTENTRY:
2447           BFD_ASSERT (h != NULL);
2448           if (h != NULL
2449               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
2450             goto error_return;
2451           break;
2452
2453         default:
2454           break;
2455         }
2456
2457       if (r_type == R_386_GOT32X
2458           && (h == NULL || h->type != STT_GNU_IFUNC))
2459         sec->need_convert_load = 1;
2460     }
2461
2462   if (elf_section_data (sec)->this_hdr.contents != contents)
2463     {
2464       if (!info->keep_memory)
2465         free (contents);
2466       else
2467         {
2468           /* Cache the section contents for elf_link_input_bfd.  */
2469           elf_section_data (sec)->this_hdr.contents = contents;
2470         }
2471     }
2472
2473   return TRUE;
2474
2475 error_return:
2476   if (elf_section_data (sec)->this_hdr.contents != contents)
2477     free (contents);
2478   sec->check_relocs_failed = 1;
2479   return FALSE;
2480 }
2481
2482 /* Return the section that should be marked against GC for a given
2483    relocation.  */
2484
2485 static asection *
2486 elf_i386_gc_mark_hook (asection *sec,
2487                        struct bfd_link_info *info,
2488                        Elf_Internal_Rela *rel,
2489                        struct elf_link_hash_entry *h,
2490                        Elf_Internal_Sym *sym)
2491 {
2492   if (h != NULL)
2493     switch (ELF32_R_TYPE (rel->r_info))
2494       {
2495       case R_386_GNU_VTINHERIT:
2496       case R_386_GNU_VTENTRY:
2497         return NULL;
2498       }
2499
2500   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2501 }
2502
2503 /* Remove undefined weak symbol from the dynamic symbol table if it
2504    is resolved to 0.   */
2505
2506 static bfd_boolean
2507 elf_i386_fixup_symbol (struct bfd_link_info *info,
2508                        struct elf_link_hash_entry *h)
2509 {
2510   if (h->dynindx != -1
2511       && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2512                                           elf_i386_hash_entry (h)->has_got_reloc,
2513                                           elf_i386_hash_entry (h)))
2514     {
2515       h->dynindx = -1;
2516       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2517                               h->dynstr_index);
2518     }
2519   return TRUE;
2520 }
2521
2522 /* Adjust a symbol defined by a dynamic object and referenced by a
2523    regular object.  The current definition is in some section of the
2524    dynamic object, but we're not including those sections.  We have to
2525    change the definition to something the rest of the link can
2526    understand.  */
2527
2528 static bfd_boolean
2529 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
2530                                 struct elf_link_hash_entry *h)
2531 {
2532   struct elf_i386_link_hash_table *htab;
2533   asection *s, *srel;
2534   struct elf_i386_link_hash_entry *eh;
2535   struct elf_dyn_relocs *p;
2536
2537   /* STT_GNU_IFUNC symbol must go through PLT. */
2538   if (h->type == STT_GNU_IFUNC)
2539     {
2540       /* All local STT_GNU_IFUNC references must be treate as local
2541          calls via local PLT.  */
2542       if (h->ref_regular
2543           && SYMBOL_CALLS_LOCAL (info, h))
2544         {
2545           bfd_size_type pc_count = 0, count = 0;
2546           struct elf_dyn_relocs **pp;
2547
2548           eh = (struct elf_i386_link_hash_entry *) h;
2549           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2550             {
2551               pc_count += p->pc_count;
2552               p->count -= p->pc_count;
2553               p->pc_count = 0;
2554               count += p->count;
2555               if (p->count == 0)
2556                 *pp = p->next;
2557               else
2558                 pp = &p->next;
2559             }
2560
2561           if (pc_count || count)
2562             {
2563               h->non_got_ref = 1;
2564               if (pc_count)
2565                 {
2566                   /* Increment PLT reference count only for PC-relative
2567                      references.  */
2568                   h->needs_plt = 1;
2569                   if (h->plt.refcount <= 0)
2570                     h->plt.refcount = 1;
2571                   else
2572                     h->plt.refcount += 1;
2573                 }
2574             }
2575         }
2576
2577       if (h->plt.refcount <= 0)
2578         {
2579           h->plt.offset = (bfd_vma) -1;
2580           h->needs_plt = 0;
2581         }
2582       return TRUE;
2583     }
2584
2585   /* If this is a function, put it in the procedure linkage table.  We
2586      will fill in the contents of the procedure linkage table later,
2587      when we know the address of the .got section.  */
2588   if (h->type == STT_FUNC
2589       || h->needs_plt)
2590     {
2591       if (h->plt.refcount <= 0
2592           || SYMBOL_CALLS_LOCAL (info, h)
2593           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2594               && h->root.type == bfd_link_hash_undefweak))
2595         {
2596           /* This case can occur if we saw a PLT32 reloc in an input
2597              file, but the symbol was never referred to by a dynamic
2598              object, or if all references were garbage collected.  In
2599              such a case, we don't actually need to build a procedure
2600              linkage table, and we can just do a PC32 reloc instead.  */
2601           h->plt.offset = (bfd_vma) -1;
2602           h->needs_plt = 0;
2603         }
2604
2605       return TRUE;
2606     }
2607   else
2608     /* It's possible that we incorrectly decided a .plt reloc was
2609        needed for an R_386_PC32 reloc to a non-function sym in
2610        check_relocs.  We can't decide accurately between function and
2611        non-function syms in check-relocs;  Objects loaded later in
2612        the link may change h->type.  So fix it now.  */
2613     h->plt.offset = (bfd_vma) -1;
2614
2615   /* If this is a weak symbol, and there is a real definition, the
2616      processor independent code will have arranged for us to see the
2617      real definition first, and we can just use the same value.  */
2618   if (h->u.weakdef != NULL)
2619     {
2620       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2621                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
2622       h->root.u.def.section = h->u.weakdef->root.u.def.section;
2623       h->root.u.def.value = h->u.weakdef->root.u.def.value;
2624       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2625         h->non_got_ref = h->u.weakdef->non_got_ref;
2626       return TRUE;
2627     }
2628
2629   /* This is a reference to a symbol defined by a dynamic object which
2630      is not a function.  */
2631
2632   /* If we are creating a shared library, we must presume that the
2633      only references to the symbol are via the global offset table.
2634      For such cases we need not do anything here; the relocations will
2635      be handled correctly by relocate_section.  */
2636   if (!bfd_link_executable (info))
2637     return TRUE;
2638
2639   /* If there are no references to this symbol that do not use the
2640      GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
2641      reloc.  */
2642   eh = (struct elf_i386_link_hash_entry *) h;
2643   if (!h->non_got_ref && !eh->gotoff_ref)
2644     return TRUE;
2645
2646   /* If -z nocopyreloc was given, we won't generate them either.  */
2647   if (info->nocopyreloc)
2648     {
2649       h->non_got_ref = 0;
2650       return TRUE;
2651     }
2652
2653   htab = elf_i386_hash_table (info);
2654   if (htab == NULL)
2655     return FALSE;
2656
2657   /* If there aren't any dynamic relocs in read-only sections nor
2658      R_386_GOTOFF relocation, then we can keep the dynamic relocs and
2659      avoid the copy reloc.  This doesn't work on VxWorks, where we can
2660      not have dynamic relocations (other than copy and jump slot
2661      relocations) in an executable.  */
2662   if (ELIMINATE_COPY_RELOCS
2663       && !eh->gotoff_ref
2664       && get_elf_i386_backend_data (info->output_bfd)->os != is_vxworks)
2665     {
2666       for (p = eh->dyn_relocs; p != NULL; p = p->next)
2667         {
2668           s = p->sec->output_section;
2669           if (s != NULL && (s->flags & SEC_READONLY) != 0)
2670             break;
2671         }
2672
2673       if (p == NULL)
2674         {
2675           h->non_got_ref = 0;
2676           return TRUE;
2677         }
2678     }
2679
2680   /* We must allocate the symbol in our .dynbss section, which will
2681      become part of the .bss section of the executable.  There will be
2682      an entry for this symbol in the .dynsym section.  The dynamic
2683      object will contain position independent code, so all references
2684      from the dynamic object to this symbol will go through the global
2685      offset table.  The dynamic linker will use the .dynsym entry to
2686      determine the address it must put in the global offset table, so
2687      both the dynamic object and the regular object will refer to the
2688      same memory location for the variable.  */
2689
2690   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
2691      copy the initial value out of the dynamic object and into the
2692      runtime process image.  */
2693   if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2694     {
2695       s = htab->elf.sdynrelro;
2696       srel = htab->elf.sreldynrelro;
2697     }
2698   else
2699     {
2700       s = htab->elf.sdynbss;
2701       srel = htab->elf.srelbss;
2702     }
2703   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2704     {
2705       srel->size += sizeof (Elf32_External_Rel);
2706       h->needs_copy = 1;
2707     }
2708
2709   return _bfd_elf_adjust_dynamic_copy (info, h, s);
2710 }
2711
2712 /* Allocate space in .plt, .got and associated reloc sections for
2713    dynamic relocs.  */
2714
2715 static bfd_boolean
2716 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2717 {
2718   struct bfd_link_info *info;
2719   struct elf_i386_link_hash_table *htab;
2720   struct elf_i386_link_hash_entry *eh;
2721   struct elf_dyn_relocs *p;
2722   unsigned plt_entry_size;
2723   bfd_boolean resolved_to_zero;
2724   const struct elf_i386_backend_data *bed;
2725
2726   if (h->root.type == bfd_link_hash_indirect)
2727     return TRUE;
2728
2729   eh = (struct elf_i386_link_hash_entry *) h;
2730
2731   info = (struct bfd_link_info *) inf;
2732   htab = elf_i386_hash_table (info);
2733   if (htab == NULL)
2734     return FALSE;
2735
2736   bed = get_elf_i386_backend_data (info->output_bfd);
2737
2738   plt_entry_size = htab->plt.plt_entry_size;
2739
2740   resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2741                                                       eh->has_got_reloc,
2742                                                       eh);
2743
2744   /* Clear the reference count of function pointer relocations if
2745      symbol isn't a normal function.  */
2746   if (h->type != STT_FUNC)
2747     eh->func_pointer_refcount = 0;
2748
2749   /* We can't use the GOT PLT if pointer equality is needed since
2750      finish_dynamic_symbol won't clear symbol value and the dynamic
2751      linker won't update the GOT slot.  We will get into an infinite
2752      loop at run-time.  */
2753   if (htab->plt_got != NULL
2754       && h->type != STT_GNU_IFUNC
2755       && !h->pointer_equality_needed
2756       && h->plt.refcount > 0
2757       && h->got.refcount > 0)
2758     {
2759       /* Don't use the regular PLT if there are both GOT and GOTPLT
2760          reloctions.  */
2761       h->plt.offset = (bfd_vma) -1;
2762
2763       /* Use the GOT PLT.  */
2764       eh->plt_got.refcount = 1;
2765     }
2766
2767   /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2768      here if it is defined and referenced in a non-shared object.  */
2769   if (h->type == STT_GNU_IFUNC
2770       && h->def_regular)
2771     {
2772       if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs,
2773                                               &htab->readonly_dynrelocs_against_ifunc,
2774                                               plt_entry_size,
2775                                               (htab->plt.has_plt0 *
2776                                                 plt_entry_size),
2777                                                4, TRUE))
2778         {
2779           asection *s = htab->plt_second;
2780           if (h->plt.offset != (bfd_vma) -1 && s != NULL)
2781             {
2782               /* Use the second PLT section if it is created.  */
2783               eh->plt_second.offset = s->size;
2784
2785               /* Make room for this entry in the second PLT section.  */
2786               s->size += htab->non_lazy_plt->plt_entry_size;
2787             }
2788
2789           return TRUE;
2790         }
2791       else
2792         return FALSE;
2793     }
2794   /* Don't create the PLT entry if there are only function pointer
2795      relocations which can be resolved at run-time.  */
2796   else if (htab->elf.dynamic_sections_created
2797            && (h->plt.refcount > eh->func_pointer_refcount
2798                || eh->plt_got.refcount > 0))
2799     {
2800       bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
2801
2802       /* Clear the reference count of function pointer relocations
2803          if PLT is used.  */
2804       eh->func_pointer_refcount = 0;
2805
2806       /* Make sure this symbol is output as a dynamic symbol.
2807          Undefined weak syms won't yet be marked as dynamic.  */
2808       if (h->dynindx == -1
2809           && !h->forced_local
2810           && !resolved_to_zero
2811           && h->root.type == bfd_link_hash_undefweak)
2812         {
2813           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2814             return FALSE;
2815         }
2816
2817       if (bfd_link_pic (info)
2818           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2819         {
2820           asection *s = htab->elf.splt;
2821           asection *second_s = htab->plt_second;
2822           asection *got_s = htab->plt_got;
2823
2824           /* If this is the first .plt entry, make room for the special
2825              first entry.  The .plt section is used by prelink to undo
2826              prelinking for dynamic relocations.  */
2827           if (s->size == 0)
2828             s->size = htab->plt.has_plt0 * plt_entry_size;
2829
2830           if (use_plt_got)
2831             eh->plt_got.offset = got_s->size;
2832           else
2833             {
2834               h->plt.offset = s->size;
2835               if (second_s)
2836                 eh->plt_second.offset = second_s->size;
2837             }
2838
2839           /* If this symbol is not defined in a regular file, and we are
2840              not generating a shared library, then set the symbol to this
2841              location in the .plt.  This is required to make function
2842              pointers compare as equal between the normal executable and
2843              the shared library.  */
2844           if (! bfd_link_pic (info)
2845               && !h->def_regular)
2846             {
2847               if (use_plt_got)
2848                 {
2849                   /* We need to make a call to the entry of the GOT PLT
2850                      instead of regular PLT entry.  */
2851                   h->root.u.def.section = got_s;
2852                   h->root.u.def.value = eh->plt_got.offset;
2853                 }
2854               else
2855                 {
2856                   if (second_s)
2857                     {
2858                       /* We need to make a call to the entry of the
2859                          second PLT instead of regular PLT entry.  */
2860                       h->root.u.def.section = second_s;
2861                       h->root.u.def.value = eh->plt_second.offset;
2862                     }
2863                   else
2864                     {
2865                       h->root.u.def.section = s;
2866                       h->root.u.def.value = h->plt.offset;
2867                     }
2868                 }
2869             }
2870
2871           /* Make room for this entry.  */
2872           if (use_plt_got)
2873             got_s->size += htab->non_lazy_plt->plt_entry_size;
2874           else
2875             {
2876               s->size += plt_entry_size;
2877               if (second_s)
2878                 second_s->size += htab->non_lazy_plt->plt_entry_size;
2879
2880               /* We also need to make an entry in the .got.plt section,
2881                  which will be placed in the .got section by the linker
2882                  script.  */
2883               htab->elf.sgotplt->size += 4;
2884
2885               /* There should be no PLT relocation against resolved
2886                  undefined weak symbol in executable.  */
2887               if (!resolved_to_zero)
2888                 {
2889                   /* We also need to make an entry in the .rel.plt
2890                      section.  */
2891                   htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2892                   htab->elf.srelplt->reloc_count++;
2893                 }
2894             }
2895
2896           if (bed->os == is_vxworks && !bfd_link_pic (info))
2897             {
2898               /* VxWorks has a second set of relocations for each PLT entry
2899                  in executables.  They go in a separate relocation section,
2900                  which is processed by the kernel loader.  */
2901
2902               /* There are two relocations for the initial PLT entry: an
2903                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2904                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
2905
2906               if (h->plt.offset == plt_entry_size)
2907                 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2908
2909               /* There are two extra relocations for each subsequent PLT entry:
2910                  an R_386_32 relocation for the GOT entry, and an R_386_32
2911                  relocation for the PLT entry.  */
2912
2913               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2914             }
2915         }
2916       else
2917         {
2918           eh->plt_got.offset = (bfd_vma) -1;
2919           h->plt.offset = (bfd_vma) -1;
2920           h->needs_plt = 0;
2921         }
2922     }
2923   else
2924     {
2925       eh->plt_got.offset = (bfd_vma) -1;
2926       h->plt.offset = (bfd_vma) -1;
2927       h->needs_plt = 0;
2928     }
2929
2930   eh->tlsdesc_got = (bfd_vma) -1;
2931
2932   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2933      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
2934   if (h->got.refcount > 0
2935       && bfd_link_executable (info)
2936       && h->dynindx == -1
2937       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2938     h->got.offset = (bfd_vma) -1;
2939   else if (h->got.refcount > 0)
2940     {
2941       asection *s;
2942       bfd_boolean dyn;
2943       int tls_type = elf_i386_hash_entry(h)->tls_type;
2944
2945       /* Make sure this symbol is output as a dynamic symbol.
2946          Undefined weak syms won't yet be marked as dynamic.  */
2947       if (h->dynindx == -1
2948           && !h->forced_local
2949           && !resolved_to_zero
2950           && h->root.type == bfd_link_hash_undefweak)
2951         {
2952           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2953             return FALSE;
2954         }
2955
2956       s = htab->elf.sgot;
2957       if (GOT_TLS_GDESC_P (tls_type))
2958         {
2959           eh->tlsdesc_got = htab->elf.sgotplt->size
2960             - elf_i386_compute_jump_table_size (htab);
2961           htab->elf.sgotplt->size += 8;
2962           h->got.offset = (bfd_vma) -2;
2963         }
2964       if (! GOT_TLS_GDESC_P (tls_type)
2965           || GOT_TLS_GD_P (tls_type))
2966         {
2967           h->got.offset = s->size;
2968           s->size += 4;
2969           /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
2970           if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2971             s->size += 4;
2972         }
2973       dyn = htab->elf.dynamic_sections_created;
2974       /* R_386_TLS_IE_32 needs one dynamic relocation,
2975          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2976          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2977          need two), R_386_TLS_GD needs one if local symbol and two if
2978          global.  No dynamic relocation against resolved undefined weak
2979          symbol in executable.  */
2980       if (tls_type == GOT_TLS_IE_BOTH)
2981         htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2982       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2983                || (tls_type & GOT_TLS_IE))
2984         htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2985       else if (GOT_TLS_GD_P (tls_type))
2986         htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel);
2987       else if (! GOT_TLS_GDESC_P (tls_type)
2988                && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2989                     && !resolved_to_zero)
2990                    || h->root.type != bfd_link_hash_undefweak)
2991                && (bfd_link_pic (info)
2992                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2993         htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
2994       if (GOT_TLS_GDESC_P (tls_type))
2995         htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
2996     }
2997   else
2998     h->got.offset = (bfd_vma) -1;
2999
3000   if (eh->dyn_relocs == NULL)
3001     return TRUE;
3002
3003   /* In the shared -Bsymbolic case, discard space allocated for
3004      dynamic pc-relative relocs against symbols which turn out to be
3005      defined in regular objects.  For the normal shared case, discard
3006      space for pc-relative relocs that have become local due to symbol
3007      visibility changes.  */
3008
3009   if (bfd_link_pic (info))
3010     {
3011       /* The only reloc that uses pc_count is R_386_PC32, which will
3012          appear on a call or on something like ".long foo - .".  We
3013          want calls to protected symbols to resolve directly to the
3014          function rather than going via the plt.  If people want
3015          function pointer comparisons to work as expected then they
3016          should avoid writing assembly like ".long foo - .".  */
3017       if (SYMBOL_CALLS_LOCAL (info, h))
3018         {
3019           struct elf_dyn_relocs **pp;
3020
3021           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3022             {
3023               p->count -= p->pc_count;
3024               p->pc_count = 0;
3025               if (p->count == 0)
3026                 *pp = p->next;
3027               else
3028                 pp = &p->next;
3029             }
3030         }
3031
3032       if (bed->os == is_vxworks)
3033         {
3034           struct elf_dyn_relocs **pp;
3035           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3036             {
3037               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
3038                 *pp = p->next;
3039               else
3040                 pp = &p->next;
3041             }
3042         }
3043
3044       /* Also discard relocs on undefined weak syms with non-default
3045          visibility or in PIE.  */
3046       if (eh->dyn_relocs != NULL
3047           && h->root.type == bfd_link_hash_undefweak)
3048         {
3049           /* Undefined weak symbol is never bound locally in shared
3050              library.  */
3051           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3052               || resolved_to_zero)
3053             {
3054               if (h->non_got_ref)
3055                 {
3056                   /* Keep dynamic non-GOT/non-PLT relocation so that we
3057                      can branch to 0 without PLT.  */
3058                   struct elf_dyn_relocs **pp;
3059
3060                   for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3061                     if (p->pc_count == 0)
3062                       *pp = p->next;
3063                     else
3064                       {
3065                         /* Remove non-R_386_PC32 relocation.  */
3066                         p->count = p->pc_count;
3067                         pp = &p->next;
3068                       }
3069
3070                   if (eh->dyn_relocs != NULL)
3071                     {
3072                       /* Make sure undefined weak symbols are output
3073                          as dynamic symbols in PIEs for dynamic non-GOT
3074                          non-PLT reloations.  */
3075                       if (! bfd_elf_link_record_dynamic_symbol (info, h))
3076                         return FALSE;
3077                     }
3078                 }
3079               else
3080                 eh->dyn_relocs = NULL;
3081             }
3082           else if (h->dynindx == -1
3083                    && !h->forced_local)
3084             {
3085               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3086                 return FALSE;
3087             }
3088         }
3089     }
3090   else if (ELIMINATE_COPY_RELOCS)
3091     {
3092       /* For the non-shared case, discard space for relocs against
3093          symbols which turn out to need copy relocs or are not
3094          dynamic.  Keep dynamic relocations for run-time function
3095          pointer initialization.  */
3096
3097       if ((!h->non_got_ref
3098            || eh->func_pointer_refcount > 0
3099            || (h->root.type == bfd_link_hash_undefweak
3100                && !resolved_to_zero))
3101           && ((h->def_dynamic
3102                && !h->def_regular)
3103               || (htab->elf.dynamic_sections_created
3104                   && (h->root.type == bfd_link_hash_undefweak
3105                       || h->root.type == bfd_link_hash_undefined))))
3106         {
3107           /* Make sure this symbol is output as a dynamic symbol.
3108              Undefined weak syms won't yet be marked as dynamic.  */
3109           if (h->dynindx == -1
3110               && !h->forced_local
3111               && !resolved_to_zero
3112               && h->root.type == bfd_link_hash_undefweak)
3113             {
3114               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3115                 return FALSE;
3116             }
3117
3118           /* If that succeeded, we know we'll be keeping all the
3119              relocs.  */
3120           if (h->dynindx != -1)
3121             goto keep;
3122         }
3123
3124       eh->dyn_relocs = NULL;
3125       eh->func_pointer_refcount = 0;
3126
3127     keep: ;
3128     }
3129
3130   /* Finally, allocate space.  */
3131   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3132     {
3133       asection *sreloc;
3134
3135       sreloc = elf_section_data (p->sec)->sreloc;
3136
3137       BFD_ASSERT (sreloc != NULL);
3138       sreloc->size += p->count * sizeof (Elf32_External_Rel);
3139     }
3140
3141   return TRUE;
3142 }
3143
3144 /* Allocate space in .plt, .got and associated reloc sections for
3145    local dynamic relocs.  */
3146
3147 static bfd_boolean
3148 elf_i386_allocate_local_dynrelocs (void **slot, void *inf)
3149 {
3150   struct elf_link_hash_entry *h
3151     = (struct elf_link_hash_entry *) *slot;
3152
3153   if (h->type != STT_GNU_IFUNC
3154       || !h->def_regular
3155       || !h->ref_regular
3156       || !h->forced_local
3157       || h->root.type != bfd_link_hash_defined)
3158     abort ();
3159
3160   return elf_i386_allocate_dynrelocs (h, inf);
3161 }
3162
3163 /* Find any dynamic relocs that apply to read-only sections.  */
3164
3165 static bfd_boolean
3166 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3167 {
3168   struct elf_i386_link_hash_entry *eh;
3169   struct elf_dyn_relocs *p;
3170
3171   /* Skip local IFUNC symbols. */
3172   if (h->forced_local && h->type == STT_GNU_IFUNC)
3173     return TRUE;
3174
3175   eh = (struct elf_i386_link_hash_entry *) h;
3176   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3177     {
3178       asection *s = p->sec->output_section;
3179
3180       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3181         {
3182           struct bfd_link_info *info = (struct bfd_link_info *) inf;
3183
3184           info->flags |= DF_TEXTREL;
3185
3186           if ((info->warn_shared_textrel && bfd_link_pic (info))
3187               || info->error_textrel)
3188             /* xgettext:c-format */
3189             info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'\n"),
3190                                     p->sec->owner, h->root.root.string,
3191                                     p->sec);
3192
3193           /* Not an error, just cut short the traversal.  */
3194           return FALSE;
3195         }
3196     }
3197   return TRUE;
3198 }
3199
3200 /* Convert load via the GOT slot to load immediate.  */
3201
3202 static bfd_boolean
3203 elf_i386_convert_load (bfd *abfd, asection *sec,
3204                        struct bfd_link_info *link_info)
3205 {
3206   struct elf_i386_link_hash_table *htab;
3207   Elf_Internal_Shdr *symtab_hdr;
3208   Elf_Internal_Rela *internal_relocs;
3209   Elf_Internal_Rela *irel, *irelend;
3210   bfd_byte *contents;
3211   bfd_boolean changed;
3212   bfd_signed_vma *local_got_refcounts;
3213
3214   /* Don't even try to convert non-ELF outputs.  */
3215   if (!is_elf_hash_table (link_info->hash))
3216     return FALSE;
3217
3218   /* Nothing to do if there is no need or no output.  */
3219   if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3220       || sec->need_convert_load == 0
3221       || bfd_is_abs_section (sec->output_section))
3222     return TRUE;
3223
3224   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3225
3226   /* Load the relocations for this section.  */
3227   internal_relocs = (_bfd_elf_link_read_relocs
3228                      (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3229                       link_info->keep_memory));
3230   if (internal_relocs == NULL)
3231     return FALSE;
3232
3233   changed = FALSE;
3234   htab = elf_i386_hash_table (link_info);
3235   local_got_refcounts = elf_local_got_refcounts (abfd);
3236
3237   /* Get the section contents.  */
3238   if (elf_section_data (sec)->this_hdr.contents != NULL)
3239     contents = elf_section_data (sec)->this_hdr.contents;
3240   else
3241     {
3242       if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3243         goto error_return;
3244     }
3245
3246   irelend = internal_relocs + sec->reloc_count;
3247   for (irel = internal_relocs; irel < irelend; irel++)
3248     {
3249       unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3250       unsigned int r_symndx;
3251       struct elf_link_hash_entry *h;
3252       bfd_boolean converted;
3253
3254       /* Don't convert R_386_GOT32 since we can't tell if it is applied
3255          to "mov $foo@GOT, %reg" which isn't a load via GOT.  */
3256       if (r_type != R_386_GOT32X)
3257         continue;
3258
3259       r_symndx = ELF32_R_SYM (irel->r_info);
3260       if (r_symndx < symtab_hdr->sh_info)
3261         h = elf_i386_get_local_sym_hash (htab, sec->owner,
3262                                          (const Elf_Internal_Rela *) irel,
3263                                          FALSE);
3264       else
3265         {
3266           h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3267            while (h->root.type == bfd_link_hash_indirect
3268                   || h->root.type == bfd_link_hash_warning)
3269              h = (struct elf_link_hash_entry *) h->root.u.i.link;
3270         }
3271
3272       /* STT_GNU_IFUNC must keep GOT32 relocations.  */
3273       if (h != NULL && h->type == STT_GNU_IFUNC)
3274         continue;
3275
3276       converted = FALSE;
3277       if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
3278                                         irel, h, &converted, link_info))
3279         goto error_return;
3280
3281       if (converted)
3282         {
3283           changed = converted;
3284           if (h)
3285             {
3286               if (h->got.refcount > 0)
3287                 h->got.refcount -= 1;
3288             }
3289           else
3290             {
3291               if (local_got_refcounts != NULL
3292                   && local_got_refcounts[r_symndx] > 0)
3293                 local_got_refcounts[r_symndx] -= 1;
3294             }
3295         }
3296     }
3297
3298   if (contents != NULL
3299       && elf_section_data (sec)->this_hdr.contents != contents)
3300     {
3301       if (!changed && !link_info->keep_memory)
3302         free (contents);
3303       else
3304         {
3305           /* Cache the section contents for elf_link_input_bfd.  */
3306           elf_section_data (sec)->this_hdr.contents = contents;
3307         }
3308     }
3309
3310   if (elf_section_data (sec)->relocs != internal_relocs)
3311     {
3312       if (!changed)
3313         free (internal_relocs);
3314       else
3315         elf_section_data (sec)->relocs = internal_relocs;
3316     }
3317
3318   return TRUE;
3319
3320  error_return:
3321   if (contents != NULL
3322       && elf_section_data (sec)->this_hdr.contents != contents)
3323     free (contents);
3324   if (internal_relocs != NULL
3325       && elf_section_data (sec)->relocs != internal_relocs)
3326     free (internal_relocs);
3327   return FALSE;
3328 }
3329
3330 /* Set the sizes of the dynamic sections.  */
3331
3332 static bfd_boolean
3333 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3334 {
3335   struct elf_i386_link_hash_table *htab;
3336   bfd *dynobj;
3337   asection *s;
3338   bfd_boolean relocs;
3339   bfd *ibfd;
3340
3341   htab = elf_i386_hash_table (info);
3342   if (htab == NULL)
3343     return FALSE;
3344   dynobj = htab->elf.dynobj;
3345   if (dynobj == NULL)
3346     abort ();
3347
3348   /* Set up .got offsets for local syms, and space for local dynamic
3349      relocs.  */
3350   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3351     {
3352       bfd_signed_vma *local_got;
3353       bfd_signed_vma *end_local_got;
3354       char *local_tls_type;
3355       bfd_vma *local_tlsdesc_gotent;
3356       bfd_size_type locsymcount;
3357       Elf_Internal_Shdr *symtab_hdr;
3358       asection *srel;
3359
3360       if (! is_i386_elf (ibfd))
3361         continue;
3362
3363       for (s = ibfd->sections; s != NULL; s = s->next)
3364         {
3365           struct elf_dyn_relocs *p;
3366
3367           if (!elf_i386_convert_load (ibfd, s, info))
3368             return FALSE;
3369
3370           for (p = ((struct elf_dyn_relocs *)
3371                      elf_section_data (s)->local_dynrel);
3372                p != NULL;
3373                p = p->next)
3374             {
3375               if (!bfd_is_abs_section (p->sec)
3376                   && bfd_is_abs_section (p->sec->output_section))
3377                 {
3378                   /* Input section has been discarded, either because
3379                      it is a copy of a linkonce section or due to
3380                      linker script /DISCARD/, so we'll be discarding
3381                      the relocs too.  */
3382                 }
3383               else if ((get_elf_i386_backend_data (output_bfd)->os
3384                         == is_vxworks)
3385                        && strcmp (p->sec->output_section->name,
3386                                   ".tls_vars") == 0)
3387                 {
3388                   /* Relocations in vxworks .tls_vars sections are
3389                      handled specially by the loader.  */
3390                 }
3391               else if (p->count != 0)
3392                 {
3393                   srel = elf_section_data (p->sec)->sreloc;
3394                   srel->size += p->count * sizeof (Elf32_External_Rel);
3395                   if ((p->sec->output_section->flags & SEC_READONLY) != 0
3396                       && (info->flags & DF_TEXTREL) == 0)
3397                     {
3398                       info->flags |= DF_TEXTREL;
3399                       if ((info->warn_shared_textrel && bfd_link_pic (info))
3400                           || info->error_textrel)
3401                         /* xgettext:c-format */
3402                         info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3403                                                 p->sec->owner, p->sec);
3404                     }
3405                 }
3406             }
3407         }
3408
3409       local_got = elf_local_got_refcounts (ibfd);
3410       if (!local_got)
3411         continue;
3412
3413       symtab_hdr = &elf_symtab_hdr (ibfd);
3414       locsymcount = symtab_hdr->sh_info;
3415       end_local_got = local_got + locsymcount;
3416       local_tls_type = elf_i386_local_got_tls_type (ibfd);
3417       local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
3418       s = htab->elf.sgot;
3419       srel = htab->elf.srelgot;
3420       for (; local_got < end_local_got;
3421            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3422         {
3423           *local_tlsdesc_gotent = (bfd_vma) -1;
3424           if (*local_got > 0)
3425             {
3426               if (GOT_TLS_GDESC_P (*local_tls_type))
3427                 {
3428                   *local_tlsdesc_gotent = htab->elf.sgotplt->size
3429                     - elf_i386_compute_jump_table_size (htab);
3430                   htab->elf.sgotplt->size += 8;
3431                   *local_got = (bfd_vma) -2;
3432                 }
3433               if (! GOT_TLS_GDESC_P (*local_tls_type)
3434                   || GOT_TLS_GD_P (*local_tls_type))
3435                 {
3436                   *local_got = s->size;
3437                   s->size += 4;
3438                   if (GOT_TLS_GD_P (*local_tls_type)
3439                       || *local_tls_type == GOT_TLS_IE_BOTH)
3440                     s->size += 4;
3441                 }
3442               if (bfd_link_pic (info)
3443                   || GOT_TLS_GD_ANY_P (*local_tls_type)
3444                   || (*local_tls_type & GOT_TLS_IE))
3445                 {
3446                   if (*local_tls_type == GOT_TLS_IE_BOTH)
3447                     srel->size += 2 * sizeof (Elf32_External_Rel);
3448                   else if (GOT_TLS_GD_P (*local_tls_type)
3449                            || ! GOT_TLS_GDESC_P (*local_tls_type))
3450                     srel->size += sizeof (Elf32_External_Rel);
3451                   if (GOT_TLS_GDESC_P (*local_tls_type))
3452                     htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
3453                 }
3454             }
3455           else
3456             *local_got = (bfd_vma) -1;
3457         }
3458     }
3459
3460   if (htab->tls_ldm_got.refcount > 0)
3461     {
3462       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
3463          relocs.  */
3464       htab->tls_ldm_got.offset = htab->elf.sgot->size;
3465       htab->elf.sgot->size += 8;
3466       htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
3467     }
3468   else
3469     htab->tls_ldm_got.offset = -1;
3470
3471   /* Allocate global sym .plt and .got entries, and space for global
3472      sym dynamic relocs.  */
3473   elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
3474
3475   /* Allocate .plt and .got entries, and space for local symbols.  */
3476   htab_traverse (htab->loc_hash_table,
3477                  elf_i386_allocate_local_dynrelocs,
3478                  info);
3479
3480   /* For every jump slot reserved in the sgotplt, reloc_count is
3481      incremented.  However, when we reserve space for TLS descriptors,
3482      it's not incremented, so in order to compute the space reserved
3483      for them, it suffices to multiply the reloc count by the jump
3484      slot size.
3485
3486      PR ld/13302: We start next_irelative_index at the end of .rela.plt
3487      so that R_386_IRELATIVE entries come last.  */
3488   if (htab->elf.srelplt)
3489     {
3490       htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
3491       htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
3492       htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3493     }
3494   else if (htab->elf.irelplt)
3495     htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3496
3497
3498   if (htab->elf.sgotplt)
3499     {
3500       /* Don't allocate .got.plt section if there are no GOT nor PLT
3501          entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
3502       if ((htab->elf.hgot == NULL
3503            || !htab->elf.hgot->ref_regular_nonweak)
3504           && (htab->elf.sgotplt->size
3505               == get_elf_backend_data (output_bfd)->got_header_size)
3506           && (htab->elf.splt == NULL
3507               || htab->elf.splt->size == 0)
3508           && (htab->elf.sgot == NULL
3509               || htab->elf.sgot->size == 0)
3510           && (htab->elf.iplt == NULL
3511               || htab->elf.iplt->size == 0)
3512           && (htab->elf.igotplt == NULL
3513               || htab->elf.igotplt->size == 0))
3514         htab->elf.sgotplt->size = 0;
3515     }
3516
3517   if (_bfd_elf_eh_frame_present (info))
3518     {
3519       if (htab->plt_eh_frame != NULL
3520           && htab->elf.splt != NULL
3521           && htab->elf.splt->size != 0
3522           && !bfd_is_abs_section (htab->elf.splt->output_section))
3523         htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
3524
3525       if (htab->plt_got_eh_frame != NULL
3526           && htab->plt_got != NULL
3527           && htab->plt_got->size != 0
3528           && !bfd_is_abs_section (htab->plt_got->output_section))
3529         htab->plt_got_eh_frame->size
3530           = htab->non_lazy_plt->eh_frame_plt_size;
3531
3532       /* Unwind info for the second PLT and .plt.got sections are
3533          identical.  */
3534       if (htab->plt_second_eh_frame != NULL
3535           && htab->plt_second != NULL
3536           && htab->plt_second->size != 0
3537           && !bfd_is_abs_section (htab->plt_second->output_section))
3538         htab->plt_second_eh_frame->size
3539           = htab->non_lazy_plt->eh_frame_plt_size;
3540     }
3541
3542   /* We now have determined the sizes of the various dynamic sections.
3543      Allocate memory for them.  */
3544   relocs = FALSE;
3545   for (s = dynobj->sections; s != NULL; s = s->next)
3546     {
3547       bfd_boolean strip_section = TRUE;
3548
3549       if ((s->flags & SEC_LINKER_CREATED) == 0)
3550         continue;
3551
3552       if (s == htab->elf.splt
3553           || s == htab->elf.sgot)
3554         {
3555           /* Strip this section if we don't need it; see the
3556              comment below.  */
3557           /* We'd like to strip these sections if they aren't needed, but if
3558              we've exported dynamic symbols from them we must leave them.
3559              It's too late to tell BFD to get rid of the symbols.  */
3560
3561           if (htab->elf.hplt != NULL)
3562             strip_section = FALSE;
3563         }
3564       else if (s == htab->elf.sgotplt
3565                || s == htab->elf.iplt
3566                || s == htab->elf.igotplt
3567                || s == htab->plt_second
3568                || s == htab->plt_got
3569                || s == htab->plt_eh_frame
3570                || s == htab->plt_got_eh_frame
3571                || s == htab->plt_second_eh_frame
3572                || s == htab->elf.sdynbss
3573                || s == htab->elf.sdynrelro)
3574         {
3575           /* Strip these too.  */
3576         }
3577       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
3578         {
3579           if (s->size != 0
3580               && s != htab->elf.srelplt
3581               && s != htab->srelplt2)
3582             relocs = TRUE;
3583
3584           /* We use the reloc_count field as a counter if we need
3585              to copy relocs into the output file.  */
3586           s->reloc_count = 0;
3587         }
3588       else
3589         {
3590           /* It's not one of our sections, so don't allocate space.  */
3591           continue;
3592         }
3593
3594       if (s->size == 0)
3595         {
3596           /* If we don't need this section, strip it from the
3597              output file.  This is mostly to handle .rel.bss and
3598              .rel.plt.  We must create both sections in
3599              create_dynamic_sections, because they must be created
3600              before the linker maps input sections to output
3601              sections.  The linker does that before
3602              adjust_dynamic_symbol is called, and it is that
3603              function which decides whether anything needs to go
3604              into these sections.  */
3605           if (strip_section)
3606             s->flags |= SEC_EXCLUDE;
3607           continue;
3608         }
3609
3610       if ((s->flags & SEC_HAS_CONTENTS) == 0)
3611         continue;
3612
3613       /* Allocate memory for the section contents.  We use bfd_zalloc
3614          here in case unused entries are not reclaimed before the
3615          section's contents are written out.  This should not happen,
3616          but this way if it does, we get a R_386_NONE reloc instead
3617          of garbage.  */
3618       s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
3619       if (s->contents == NULL)
3620         return FALSE;
3621     }
3622
3623   if (htab->plt_eh_frame != NULL
3624       && htab->plt_eh_frame->contents != NULL)
3625     {
3626       memcpy (htab->plt_eh_frame->contents,
3627               htab->plt.eh_frame_plt,
3628               htab->plt_eh_frame->size);
3629       bfd_put_32 (dynobj, htab->elf.splt->size,
3630                   htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3631     }
3632
3633   if (htab->plt_got_eh_frame != NULL
3634       && htab->plt_got_eh_frame->contents != NULL)
3635     {
3636       memcpy (htab->plt_got_eh_frame->contents,
3637               htab->non_lazy_plt->eh_frame_plt,
3638               htab->plt_got_eh_frame->size);
3639       bfd_put_32 (dynobj, htab->plt_got->size,
3640                   (htab->plt_got_eh_frame->contents
3641                    + PLT_FDE_LEN_OFFSET));
3642     }
3643
3644   if (htab->plt_second_eh_frame != NULL
3645       && htab->plt_second_eh_frame->contents != NULL)
3646     {
3647       memcpy (htab->plt_second_eh_frame->contents,
3648               htab->non_lazy_plt->eh_frame_plt,
3649               htab->plt_second_eh_frame->size);
3650       bfd_put_32 (dynobj, htab->plt_second->size,
3651                   (htab->plt_second_eh_frame->contents
3652                    + PLT_FDE_LEN_OFFSET));
3653     }
3654
3655   if (htab->elf.dynamic_sections_created)
3656     {
3657       /* Add some entries to the .dynamic section.  We fill in the
3658          values later, in elf_i386_finish_dynamic_sections, but we
3659          must add the entries now so that we get the correct size for
3660          the .dynamic section.  The DT_DEBUG entry is filled in by the
3661          dynamic linker and used by the debugger.  */
3662 #define add_dynamic_entry(TAG, VAL) \
3663   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3664
3665       if (bfd_link_executable (info))
3666         {
3667           if (!add_dynamic_entry (DT_DEBUG, 0))
3668             return FALSE;
3669         }
3670
3671       if (htab->elf.splt->size != 0)
3672         {
3673           /* DT_PLTGOT is used by prelink even if there is no PLT
3674              relocation.  */
3675           if (!add_dynamic_entry (DT_PLTGOT, 0))
3676             return FALSE;
3677         }
3678
3679       if (htab->elf.srelplt->size != 0)
3680         {
3681           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3682               || !add_dynamic_entry (DT_PLTREL, DT_REL)
3683               || !add_dynamic_entry (DT_JMPREL, 0))
3684             return FALSE;
3685         }
3686
3687       if (relocs)
3688         {
3689           if (!add_dynamic_entry (DT_REL, 0)
3690               || !add_dynamic_entry (DT_RELSZ, 0)
3691               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
3692             return FALSE;
3693
3694           /* If any dynamic relocs apply to a read-only section,
3695              then we need a DT_TEXTREL entry.  */
3696           if ((info->flags & DF_TEXTREL) == 0)
3697             elf_link_hash_traverse (&htab->elf,
3698                                     elf_i386_readonly_dynrelocs, info);
3699
3700           if ((info->flags & DF_TEXTREL) != 0)
3701             {
3702               if (htab->readonly_dynrelocs_against_ifunc)
3703                 {
3704                   info->callbacks->einfo
3705                     (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3706                   bfd_set_error (bfd_error_bad_value);
3707                   return FALSE;
3708                 }
3709
3710               if (!add_dynamic_entry (DT_TEXTREL, 0))
3711                 return FALSE;
3712             }
3713         }
3714       if (get_elf_i386_backend_data (output_bfd)->os == is_vxworks
3715           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
3716         return FALSE;
3717     }
3718 #undef add_dynamic_entry
3719
3720   return TRUE;
3721 }
3722
3723 static bfd_boolean
3724 elf_i386_always_size_sections (bfd *output_bfd,
3725                                struct bfd_link_info *info)
3726 {
3727   asection *tls_sec = elf_hash_table (info)->tls_sec;
3728
3729   if (tls_sec)
3730     {
3731       struct elf_link_hash_entry *tlsbase;
3732
3733       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3734                                       "_TLS_MODULE_BASE_",
3735                                       FALSE, FALSE, FALSE);
3736
3737       if (tlsbase && tlsbase->type == STT_TLS)
3738         {
3739           struct elf_i386_link_hash_table *htab;
3740           struct bfd_link_hash_entry *bh = NULL;
3741           const struct elf_backend_data *bed
3742             = get_elf_backend_data (output_bfd);
3743
3744           htab = elf_i386_hash_table (info);
3745           if (htab == NULL)
3746             return FALSE;
3747
3748           if (!(_bfd_generic_link_add_one_symbol
3749                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3750                  tls_sec, 0, NULL, FALSE,
3751                  bed->collect, &bh)))
3752             return FALSE;
3753
3754           htab->tls_module_base = bh;
3755
3756           tlsbase = (struct elf_link_hash_entry *)bh;
3757           tlsbase->def_regular = 1;
3758           tlsbase->other = STV_HIDDEN;
3759           tlsbase->root.linker_def = 1;
3760           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3761         }
3762     }
3763
3764   return TRUE;
3765 }
3766
3767 /* Set the correct type for an x86 ELF section.  We do this by the
3768    section name, which is a hack, but ought to work.  */
3769
3770 static bfd_boolean
3771 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
3772                         Elf_Internal_Shdr *hdr,
3773                         asection *sec)
3774 {
3775   const char *name;
3776
3777   name = bfd_get_section_name (abfd, sec);
3778
3779   /* This is an ugly, but unfortunately necessary hack that is
3780      needed when producing EFI binaries on x86. It tells
3781      elf.c:elf_fake_sections() not to consider ".reloc" as a section
3782      containing ELF relocation info.  We need this hack in order to
3783      be able to generate ELF binaries that can be translated into
3784      EFI applications (which are essentially COFF objects).  Those
3785      files contain a COFF ".reloc" section inside an ELFNN object,
3786      which would normally cause BFD to segfault because it would
3787      attempt to interpret this section as containing relocation
3788      entries for section "oc".  With this hack enabled, ".reloc"
3789      will be treated as a normal data section, which will avoid the
3790      segfault.  However, you won't be able to create an ELFNN binary
3791      with a section named "oc" that needs relocations, but that's
3792      the kind of ugly side-effects you get when detecting section
3793      types based on their names...  In practice, this limitation is
3794      unlikely to bite.  */
3795   if (strcmp (name, ".reloc") == 0)
3796     hdr->sh_type = SHT_PROGBITS;
3797
3798   return TRUE;
3799 }
3800
3801 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
3802    executables.  Rather than setting it to the beginning of the TLS
3803    section, we have to set it to the end.    This function may be called
3804    multiple times, it is idempotent.  */
3805
3806 static void
3807 elf_i386_set_tls_module_base (struct bfd_link_info *info)
3808 {
3809   struct elf_i386_link_hash_table *htab;
3810   struct bfd_link_hash_entry *base;
3811
3812   if (!bfd_link_executable (info))
3813     return;
3814
3815   htab = elf_i386_hash_table (info);
3816   if (htab == NULL)
3817     return;
3818
3819   base = htab->tls_module_base;
3820   if (base == NULL)
3821     return;
3822
3823   base->u.def.value = htab->elf.tls_size;
3824 }
3825
3826 /* Return the base VMA address which should be subtracted from real addresses
3827    when resolving @dtpoff relocation.
3828    This is PT_TLS segment p_vaddr.  */
3829
3830 static bfd_vma
3831 elf_i386_dtpoff_base (struct bfd_link_info *info)
3832 {
3833   /* If tls_sec is NULL, we should have signalled an error already.  */
3834   if (elf_hash_table (info)->tls_sec == NULL)
3835     return 0;
3836   return elf_hash_table (info)->tls_sec->vma;
3837 }
3838
3839 /* Return the relocation value for @tpoff relocation
3840    if STT_TLS virtual address is ADDRESS.  */
3841
3842 static bfd_vma
3843 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
3844 {
3845   struct elf_link_hash_table *htab = elf_hash_table (info);
3846   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3847   bfd_vma static_tls_size;
3848
3849   /* If tls_sec is NULL, we should have signalled an error already.  */
3850   if (htab->tls_sec == NULL)
3851     return 0;
3852
3853   /* Consider special static TLS alignment requirements.  */
3854   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3855   return static_tls_size + htab->tls_sec->vma - address;
3856 }
3857
3858 /* Relocate an i386 ELF section.  */
3859
3860 static bfd_boolean
3861 elf_i386_relocate_section (bfd *output_bfd,
3862                            struct bfd_link_info *info,
3863                            bfd *input_bfd,
3864                            asection *input_section,
3865                            bfd_byte *contents,
3866                            Elf_Internal_Rela *relocs,
3867                            Elf_Internal_Sym *local_syms,
3868                            asection **local_sections)
3869 {
3870   struct elf_i386_link_hash_table *htab;
3871   Elf_Internal_Shdr *symtab_hdr;
3872   struct elf_link_hash_entry **sym_hashes;
3873   bfd_vma *local_got_offsets;
3874   bfd_vma *local_tlsdesc_gotents;
3875   Elf_Internal_Rela *rel;
3876   Elf_Internal_Rela *wrel;
3877   Elf_Internal_Rela *relend;
3878   bfd_boolean is_vxworks_tls;
3879   unsigned plt_entry_size;
3880
3881   BFD_ASSERT (is_i386_elf (input_bfd));
3882
3883   /* Skip if check_relocs failed.  */
3884   if (input_section->check_relocs_failed)
3885     return FALSE;
3886
3887   htab = elf_i386_hash_table (info);
3888   if (htab == NULL)
3889     return FALSE;
3890   symtab_hdr = &elf_symtab_hdr (input_bfd);
3891   sym_hashes = elf_sym_hashes (input_bfd);
3892   local_got_offsets = elf_local_got_offsets (input_bfd);
3893   local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
3894   /* We have to handle relocations in vxworks .tls_vars sections
3895      specially, because the dynamic loader is 'weird'.  */
3896   is_vxworks_tls = ((get_elf_i386_backend_data (output_bfd)->os
3897                      == is_vxworks)
3898                     && bfd_link_pic (info)
3899                     && !strcmp (input_section->output_section->name,
3900                                 ".tls_vars"));
3901
3902   elf_i386_set_tls_module_base (info);
3903
3904   plt_entry_size = htab->plt.plt_entry_size;
3905
3906   rel = wrel = relocs;
3907   relend = relocs + input_section->reloc_count;
3908   for (; rel < relend; wrel++, rel++)
3909     {
3910       unsigned int r_type;
3911       reloc_howto_type *howto;
3912       unsigned long r_symndx;
3913       struct elf_link_hash_entry *h;
3914       struct elf_i386_link_hash_entry *eh;
3915       Elf_Internal_Sym *sym;
3916       asection *sec;
3917       bfd_vma off, offplt, plt_offset;
3918       bfd_vma relocation;
3919       bfd_boolean unresolved_reloc;
3920       bfd_reloc_status_type r;
3921       unsigned int indx;
3922       int tls_type;
3923       bfd_vma st_size;
3924       asection *resolved_plt;
3925       bfd_boolean resolved_to_zero;
3926       bfd_boolean relative_reloc;
3927
3928       r_type = ELF32_R_TYPE (rel->r_info);
3929       if (r_type == R_386_GNU_VTINHERIT
3930           || r_type == R_386_GNU_VTENTRY)
3931         {
3932           if (wrel != rel)
3933             *wrel = *rel;
3934           continue;
3935         }
3936
3937       if ((indx = r_type) >= R_386_standard
3938           && ((indx = r_type - R_386_ext_offset) - R_386_standard
3939               >= R_386_ext - R_386_standard)
3940           && ((indx = r_type - R_386_tls_offset) - R_386_ext
3941               >= R_386_ext2 - R_386_ext))
3942         {
3943           _bfd_error_handler
3944             /* xgettext:c-format */
3945             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3946              input_bfd, r_type, input_section);
3947           bfd_set_error (bfd_error_bad_value);
3948           return FALSE;
3949         }
3950       howto = elf_howto_table + indx;
3951
3952       r_symndx = ELF32_R_SYM (rel->r_info);
3953       h = NULL;
3954       sym = NULL;
3955       sec = NULL;
3956       unresolved_reloc = FALSE;
3957       if (r_symndx < symtab_hdr->sh_info)
3958         {
3959           sym = local_syms + r_symndx;
3960           sec = local_sections[r_symndx];
3961           relocation = (sec->output_section->vma
3962                         + sec->output_offset
3963                         + sym->st_value);
3964           st_size = sym->st_size;
3965
3966           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
3967               && ((sec->flags & SEC_MERGE) != 0
3968                   || (bfd_link_relocatable (info)
3969                       && sec->output_offset != 0)))
3970             {
3971               bfd_vma addend;
3972               bfd_byte *where = contents + rel->r_offset;
3973
3974               switch (howto->size)
3975                 {
3976                 case 0:
3977                   addend = bfd_get_8 (input_bfd, where);
3978                   if (howto->pc_relative)
3979                     {
3980                       addend = (addend ^ 0x80) - 0x80;
3981                       addend += 1;
3982                     }
3983                   break;
3984                 case 1:
3985                   addend = bfd_get_16 (input_bfd, where);
3986                   if (howto->pc_relative)
3987                     {
3988                       addend = (addend ^ 0x8000) - 0x8000;
3989                       addend += 2;
3990                     }
3991                   break;
3992                 case 2:
3993                   addend = bfd_get_32 (input_bfd, where);
3994                   if (howto->pc_relative)
3995                     {
3996                       addend = (addend ^ 0x80000000) - 0x80000000;
3997                       addend += 4;
3998                     }
3999                   break;
4000                 default:
4001                   abort ();
4002                 }
4003
4004               if (bfd_link_relocatable (info))
4005                 addend += sec->output_offset;
4006               else
4007                 {
4008                   asection *msec = sec;
4009                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
4010                                                    addend);
4011                   addend -= relocation;
4012                   addend += msec->output_section->vma + msec->output_offset;
4013                 }
4014
4015               switch (howto->size)
4016                 {
4017                 case 0:
4018                   /* FIXME: overflow checks.  */
4019                   if (howto->pc_relative)
4020                     addend -= 1;
4021                   bfd_put_8 (input_bfd, addend, where);
4022                   break;
4023                 case 1:
4024                   if (howto->pc_relative)
4025                     addend -= 2;
4026                   bfd_put_16 (input_bfd, addend, where);
4027                   break;
4028                 case 2:
4029                   if (howto->pc_relative)
4030                     addend -= 4;
4031                   bfd_put_32 (input_bfd, addend, where);
4032                   break;
4033                 }
4034             }
4035           else if (!bfd_link_relocatable (info)
4036                    && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4037             {
4038               /* Relocate against local STT_GNU_IFUNC symbol.  */
4039               h = elf_i386_get_local_sym_hash (htab, input_bfd, rel,
4040                                                FALSE);
4041               if (h == NULL)
4042                 abort ();
4043
4044               /* Set STT_GNU_IFUNC symbol value.  */
4045               h->root.u.def.value = sym->st_value;
4046               h->root.u.def.section = sec;
4047             }
4048         }
4049       else
4050         {
4051           bfd_boolean warned ATTRIBUTE_UNUSED;
4052           bfd_boolean ignored ATTRIBUTE_UNUSED;
4053
4054           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4055                                    r_symndx, symtab_hdr, sym_hashes,
4056                                    h, sec, relocation,
4057                                    unresolved_reloc, warned, ignored);
4058           st_size = h->size;
4059         }
4060
4061       if (sec != NULL && discarded_section (sec))
4062         {
4063           _bfd_clear_contents (howto, input_bfd, input_section,
4064                                contents + rel->r_offset);
4065           wrel->r_offset = rel->r_offset;
4066           wrel->r_info = 0;
4067           wrel->r_addend = 0;
4068
4069           /* For ld -r, remove relocations in debug sections against
4070              sections defined in discarded sections.  Not done for
4071              eh_frame editing code expects to be present.  */
4072            if (bfd_link_relocatable (info)
4073                && (input_section->flags & SEC_DEBUGGING))
4074              wrel--;
4075
4076            continue;
4077         }
4078
4079       if (bfd_link_relocatable (info))
4080         {
4081           if (wrel != rel)
4082             *wrel = *rel;
4083           continue;
4084         }
4085
4086       eh = (struct elf_i386_link_hash_entry *) h;
4087
4088       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
4089          it here if it is defined in a non-shared object.  */
4090       if (h != NULL
4091           && h->type == STT_GNU_IFUNC
4092           && h->def_regular)
4093         {
4094           asection *gotplt, *base_got;
4095           bfd_vma plt_index;
4096           const char *name;
4097
4098           if ((input_section->flags & SEC_ALLOC) == 0)
4099             {
4100               /* Dynamic relocs are not propagated for SEC_DEBUGGING
4101                  sections because such sections are not SEC_ALLOC and
4102                  thus ld.so will not process them.  */
4103               if ((input_section->flags & SEC_DEBUGGING) != 0)
4104                 continue;
4105               abort ();
4106             }
4107
4108           /* STT_GNU_IFUNC symbol must go through PLT.  */
4109           if (htab->elf.splt != NULL)
4110             {
4111               if (htab->plt_second != NULL)
4112                 {
4113                   resolved_plt = htab->plt_second;
4114                   plt_offset = eh->plt_second.offset;
4115                 }
4116               else
4117                 {
4118                   resolved_plt = htab->elf.splt;
4119                   plt_offset = h->plt.offset;
4120                 }
4121               gotplt = htab->elf.sgotplt;
4122             }
4123           else
4124             {
4125               resolved_plt = htab->elf.iplt;
4126               plt_offset = h->plt.offset;
4127               gotplt = htab->elf.igotplt;
4128             }
4129
4130           switch (r_type)
4131             {
4132             default:
4133               break;
4134
4135             case R_386_GOT32:
4136             case R_386_GOT32X:
4137               base_got = htab->elf.sgot;
4138               off = h->got.offset;
4139
4140               if (base_got == NULL)
4141                 abort ();
4142
4143               if (off == (bfd_vma) -1)
4144                 {
4145                   /* We can't use h->got.offset here to save state, or
4146                      even just remember the offset, as finish_dynamic_symbol
4147                      would use that as offset into .got.  */
4148
4149                   if (h->plt.offset == (bfd_vma) -1)
4150                     abort ();
4151
4152                   if (htab->elf.splt != NULL)
4153                     {
4154                       plt_index = (h->plt.offset / plt_entry_size
4155                                    - htab->plt.has_plt0);
4156                       off = (plt_index + 3) * 4;
4157                       base_got = htab->elf.sgotplt;
4158                     }
4159                   else
4160                     {
4161                       plt_index = h->plt.offset / plt_entry_size;
4162                       off = plt_index * 4;
4163                       base_got = htab->elf.igotplt;
4164                     }
4165
4166                   if (h->dynindx == -1
4167                       || h->forced_local
4168                       || info->symbolic)
4169                     {
4170                       /* This references the local defitionion.  We must
4171                          initialize this entry in the global offset table.
4172                          Since the offset must always be a multiple of 8,
4173                          we use the least significant bit to record
4174                          whether we have initialized it already.
4175
4176                          When doing a dynamic link, we create a .rela.got
4177                          relocation entry to initialize the value.  This
4178                          is done in the finish_dynamic_symbol routine.   */
4179                       if ((off & 1) != 0)
4180                         off &= ~1;
4181                       else
4182                         {
4183                           bfd_put_32 (output_bfd, relocation,
4184                                       base_got->contents + off);
4185                           h->got.offset |= 1;
4186                         }
4187                     }
4188
4189                   relocation = off;
4190                 }
4191               else
4192                 relocation = (base_got->output_section->vma
4193                               + base_got->output_offset + off
4194                               - gotplt->output_section->vma
4195                               - gotplt->output_offset);
4196
4197               if (rel->r_offset > 1
4198                   && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
4199                   && *(contents + rel->r_offset - 2) != 0x8d)
4200                 {
4201                   if (bfd_link_pic (info))
4202                     goto disallow_got32;
4203
4204                   /* Add the GOT base if there is no base register.  */
4205                   relocation += (gotplt->output_section->vma
4206                                  + gotplt->output_offset);
4207                 }
4208               else if (htab->elf.splt == NULL)
4209                 {
4210                   /* Adjust for static executables.  */
4211                   relocation += gotplt->output_offset;
4212                 }
4213
4214               goto do_relocation;
4215             }
4216
4217           if (h->plt.offset == (bfd_vma) -1)
4218             {
4219               /* Handle static pointers of STT_GNU_IFUNC symbols.  */
4220               if (r_type == R_386_32
4221                   && (input_section->flags & SEC_CODE) == 0)
4222                 goto do_ifunc_pointer;
4223               goto bad_ifunc_reloc;
4224             }
4225
4226           relocation = (resolved_plt->output_section->vma
4227                         + resolved_plt->output_offset + plt_offset);
4228
4229           switch (r_type)
4230             {
4231             default:
4232 bad_ifunc_reloc:
4233               if (h->root.root.string)
4234                 name = h->root.root.string;
4235               else
4236                 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4237                                          NULL);
4238               _bfd_error_handler
4239                 /* xgettext:c-format */
4240                 (_("%B: relocation %s against STT_GNU_IFUNC "
4241                    "symbol `%s' isn't supported"), input_bfd,
4242                  howto->name, name);
4243               bfd_set_error (bfd_error_bad_value);
4244               return FALSE;
4245
4246             case R_386_32:
4247               /* Generate dynamic relcoation only when there is a
4248                  non-GOT reference in a shared object.  */
4249               if ((bfd_link_pic (info) && h->non_got_ref)
4250                   || h->plt.offset == (bfd_vma) -1)
4251                 {
4252                   Elf_Internal_Rela outrel;
4253                   asection *sreloc;
4254                   bfd_vma offset;
4255
4256 do_ifunc_pointer:
4257                   /* Need a dynamic relocation to get the real function
4258                      adddress.  */
4259                   offset = _bfd_elf_section_offset (output_bfd,
4260                                                     info,
4261                                                     input_section,
4262                                                     rel->r_offset);
4263                   if (offset == (bfd_vma) -1
4264                       || offset == (bfd_vma) -2)
4265                     abort ();
4266
4267                   outrel.r_offset = (input_section->output_section->vma
4268                                      + input_section->output_offset
4269                                      + offset);
4270
4271                   if (h->dynindx == -1
4272                       || h->forced_local
4273                       || bfd_link_executable (info))
4274                     {
4275                       info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
4276                                               h->root.root.string,
4277                                               h->root.u.def.section->owner);
4278
4279                       /* This symbol is resolved locally.  */
4280                       outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4281                       bfd_put_32 (output_bfd,
4282                                   (h->root.u.def.value
4283                                    + h->root.u.def.section->output_section->vma
4284                                    + h->root.u.def.section->output_offset),
4285                                   contents + offset);
4286                     }
4287                   else
4288                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4289
4290                   /* Dynamic relocations are stored in
4291                      1. .rel.ifunc section in PIC object.
4292                      2. .rel.got section in dynamic executable.
4293                      3. .rel.iplt section in static executable.  */
4294                   if (bfd_link_pic (info))
4295                     sreloc = htab->elf.irelifunc;
4296                   else if (htab->elf.splt != NULL)
4297                     sreloc = htab->elf.srelgot;
4298                   else
4299                     sreloc = htab->elf.irelplt;
4300                   elf_append_rel (output_bfd, sreloc, &outrel);
4301
4302                   /* If this reloc is against an external symbol, we
4303                      do not want to fiddle with the addend.  Otherwise,
4304                      we need to include the symbol value so that it
4305                      becomes an addend for the dynamic reloc.  For an
4306                      internal symbol, we have updated addend.  */
4307                   continue;
4308                 }
4309               /* FALLTHROUGH */
4310             case R_386_PC32:
4311             case R_386_PLT32:
4312               goto do_relocation;
4313
4314             case R_386_GOTOFF:
4315               relocation -= (gotplt->output_section->vma
4316                              + gotplt->output_offset);
4317               goto do_relocation;
4318             }
4319         }
4320
4321       resolved_to_zero = (eh != NULL
4322                           && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4323                                                               eh->has_got_reloc,
4324                                                               eh));
4325
4326       switch (r_type)
4327         {
4328         case R_386_GOT32X:
4329           /* Avoid optimizing _DYNAMIC since ld.so may use its
4330              link-time address.  */
4331           if (h == htab->elf.hdynamic)
4332             goto r_386_got32;
4333
4334           if (bfd_link_pic (info))
4335             {
4336               /* It is OK to convert mov to lea and convert indirect
4337                  branch to direct branch.  It is OK to convert adc,
4338                  add, and, cmp, or, sbb, sub, test, xor only when PIC
4339                  is false.   */
4340               unsigned int opcode, addend;
4341               addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
4342               if (addend != 0)
4343                 goto r_386_got32;
4344               opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4345               if (opcode != 0x8b && opcode != 0xff)
4346                 goto r_386_got32;
4347             }
4348
4349           /* Resolve "mov GOT[(%reg)], %reg",
4350              "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
4351              and "binop foo@GOT[(%reg)], %reg".  */
4352           if (h == NULL
4353               || (h->plt.offset == (bfd_vma) -1
4354                   && h->got.offset == (bfd_vma) -1)
4355               || htab->elf.sgotplt == NULL)
4356             abort ();
4357
4358           offplt = (htab->elf.sgotplt->output_section->vma
4359                     + htab->elf.sgotplt->output_offset);
4360
4361           /* It is relative to .got.plt section.  */
4362           if (h->got.offset != (bfd_vma) -1)
4363             /* Use GOT entry.  Mask off the least significant bit in
4364                GOT offset which may be set by R_386_GOT32 processing
4365                below.  */
4366             relocation = (htab->elf.sgot->output_section->vma
4367                           + htab->elf.sgot->output_offset
4368                           + (h->got.offset & ~1) - offplt);
4369           else
4370             /* Use GOTPLT entry.  */
4371             relocation = (h->plt.offset / plt_entry_size
4372                           - htab->plt.has_plt0 + 3) * 4;
4373
4374           if (!bfd_link_pic (info))
4375             {
4376               /* If not PIC, add the .got.plt section address for
4377                  baseless addressing.  */
4378               unsigned int modrm;
4379               modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4380               if ((modrm & 0xc7) == 0x5)
4381                 relocation += offplt;
4382             }
4383
4384           unresolved_reloc = FALSE;
4385           break;
4386
4387         case R_386_GOT32:
4388 r_386_got32:
4389           /* Relocation is to the entry for this symbol in the global
4390              offset table.  */
4391           if (htab->elf.sgot == NULL)
4392             abort ();
4393
4394           relative_reloc = FALSE;
4395           if (h != NULL)
4396             {
4397               bfd_boolean dyn;
4398
4399               off = h->got.offset;
4400               dyn = htab->elf.dynamic_sections_created;
4401               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4402                                                      bfd_link_pic (info),
4403                                                      h)
4404                   || (bfd_link_pic (info)
4405                       && SYMBOL_REFERENCES_LOCAL (info, h))
4406                   || (ELF_ST_VISIBILITY (h->other)
4407                       && h->root.type == bfd_link_hash_undefweak))
4408                 {
4409                   /* This is actually a static link, or it is a
4410                      -Bsymbolic link and the symbol is defined
4411                      locally, or the symbol was forced to be local
4412                      because of a version file.  We must initialize
4413                      this entry in the global offset table.  Since the
4414                      offset must always be a multiple of 4, we use the
4415                      least significant bit to record whether we have
4416                      initialized it already.
4417
4418                      When doing a dynamic link, we create a .rel.got
4419                      relocation entry to initialize the value.  This
4420                      is done in the finish_dynamic_symbol routine.  */
4421                   if ((off & 1) != 0)
4422                     off &= ~1;
4423                   else
4424                     {
4425                       bfd_put_32 (output_bfd, relocation,
4426                                   htab->elf.sgot->contents + off);
4427                       h->got.offset |= 1;
4428
4429                       if (h->dynindx == -1
4430                           && !h->forced_local
4431                           && h->root.type != bfd_link_hash_undefweak
4432                           && bfd_link_pic (info))
4433                         {
4434                           /* PR ld/21402: If this symbol isn't dynamic
4435                              in PIC, generate R_386_RELATIVE here.  */
4436                           eh->no_finish_dynamic_symbol = 1;
4437                           relative_reloc = TRUE;
4438                         }
4439                     }
4440                 }
4441               else
4442                 unresolved_reloc = FALSE;
4443             }
4444           else
4445             {
4446               if (local_got_offsets == NULL)
4447                 abort ();
4448
4449               off = local_got_offsets[r_symndx];
4450
4451               /* The offset must always be a multiple of 4.  We use
4452                  the least significant bit to record whether we have
4453                  already generated the necessary reloc.  */
4454               if ((off & 1) != 0)
4455                 off &= ~1;
4456               else
4457                 {
4458                   bfd_put_32 (output_bfd, relocation,
4459                               htab->elf.sgot->contents + off);
4460                   local_got_offsets[r_symndx] |= 1;
4461
4462                   if (bfd_link_pic (info))
4463                     relative_reloc = TRUE;
4464                 }
4465             }
4466
4467           if (relative_reloc)
4468             {
4469               asection *s;
4470               Elf_Internal_Rela outrel;
4471
4472               s = htab->elf.srelgot;
4473               if (s == NULL)
4474                 abort ();
4475
4476               outrel.r_offset = (htab->elf.sgot->output_section->vma
4477                                  + htab->elf.sgot->output_offset
4478                                  + off);
4479               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4480               elf_append_rel (output_bfd, s, &outrel);
4481             }
4482
4483           if (off >= (bfd_vma) -2)
4484             abort ();
4485
4486           relocation = (htab->elf.sgot->output_section->vma
4487                         + htab->elf.sgot->output_offset + off);
4488           if (rel->r_offset > 1
4489               && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
4490               && *(contents + rel->r_offset - 2) != 0x8d)
4491             {
4492               if (bfd_link_pic (info))
4493                 {
4494                   /* For PIC, disallow R_386_GOT32 without a base
4495                      register, except for "lea foo@GOT, %reg", since
4496                      we don't know what the GOT base is.  */
4497                   const char *name;
4498
4499 disallow_got32:
4500                   if (h == NULL || h->root.root.string == NULL)
4501                     name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4502                                              NULL);
4503                   else
4504                     name = h->root.root.string;
4505
4506                   _bfd_error_handler
4507                     /* xgettext:c-format */
4508                     (_("%B: direct GOT relocation %s against `%s'"
4509                        " without base register can not be used"
4510                        " when making a shared object"),
4511                      input_bfd, howto->name, name);
4512                   bfd_set_error (bfd_error_bad_value);
4513                   return FALSE;
4514                 }
4515             }
4516           else
4517             {
4518               /* Subtract the .got.plt section address only with a base
4519                  register.  */
4520               relocation -= (htab->elf.sgotplt->output_section->vma
4521                              + htab->elf.sgotplt->output_offset);
4522             }
4523
4524           break;
4525
4526         case R_386_GOTOFF:
4527           /* Relocation is relative to the start of the global offset
4528              table.  */
4529
4530           /* Check to make sure it isn't a protected function or data
4531              symbol for shared library since it may not be local when
4532              used as function address or with copy relocation.  We also
4533              need to make sure that a symbol is referenced locally.  */
4534           if (!bfd_link_executable (info) && h)
4535             {
4536               if (!h->def_regular)
4537                 {
4538                   const char *v;
4539
4540                   switch (ELF_ST_VISIBILITY (h->other))
4541                     {
4542                     case STV_HIDDEN:
4543                       v = _("hidden symbol");
4544                       break;
4545                     case STV_INTERNAL:
4546                       v = _("internal symbol");
4547                       break;
4548                     case STV_PROTECTED:
4549                       v = _("protected symbol");
4550                       break;
4551                     default:
4552                       v = _("symbol");
4553                       break;
4554                     }
4555
4556                   _bfd_error_handler
4557                     /* xgettext:c-format */
4558                     (_("%B: relocation R_386_GOTOFF against undefined %s"
4559                        " `%s' can not be used when making a shared object"),
4560                      input_bfd, v, h->root.root.string);
4561                   bfd_set_error (bfd_error_bad_value);
4562                   return FALSE;
4563                 }
4564               else if (!SYMBOL_REFERENCES_LOCAL (info, h)
4565                        && (h->type == STT_FUNC
4566                            || h->type == STT_OBJECT)
4567                        && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4568                 {
4569                   _bfd_error_handler
4570                     /* xgettext:c-format */
4571                     (_("%B: relocation R_386_GOTOFF against protected %s"
4572                        " `%s' can not be used when making a shared object"),
4573                      input_bfd,
4574                      h->type == STT_FUNC ? "function" : "data",
4575                      h->root.root.string);
4576                   bfd_set_error (bfd_error_bad_value);
4577                   return FALSE;
4578                 }
4579             }
4580
4581           /* Note that sgot is not involved in this
4582              calculation.  We always want the start of .got.plt.  If we
4583              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4584              permitted by the ABI, we might have to change this
4585              calculation.  */
4586           relocation -= htab->elf.sgotplt->output_section->vma
4587                         + htab->elf.sgotplt->output_offset;
4588           break;
4589
4590         case R_386_GOTPC:
4591           /* Use global offset table as symbol value.  */
4592           relocation = htab->elf.sgotplt->output_section->vma
4593                        + htab->elf.sgotplt->output_offset;
4594           unresolved_reloc = FALSE;
4595           break;
4596
4597         case R_386_PLT32:
4598           /* Relocation is to the entry for this symbol in the
4599              procedure linkage table.  */
4600
4601           /* Resolve a PLT32 reloc against a local symbol directly,
4602              without using the procedure linkage table.  */
4603           if (h == NULL)
4604             break;
4605
4606           if ((h->plt.offset == (bfd_vma) -1
4607                && eh->plt_got.offset == (bfd_vma) -1)
4608               || htab->elf.splt == NULL)
4609             {
4610               /* We didn't make a PLT entry for this symbol.  This
4611                  happens when statically linking PIC code, or when
4612                  using -Bsymbolic.  */
4613               break;
4614             }
4615
4616           if (h->plt.offset != (bfd_vma) -1)
4617             {
4618               if (htab->plt_second != NULL)
4619                 {
4620                   resolved_plt = htab->plt_second;
4621                   plt_offset = eh->plt_second.offset;
4622                 }
4623               else
4624                 {
4625                   resolved_plt = htab->elf.splt;
4626                   plt_offset = h->plt.offset;
4627                 }
4628             }
4629           else
4630             {
4631               resolved_plt = htab->plt_got;
4632               plt_offset = eh->plt_got.offset;
4633             }
4634
4635           relocation = (resolved_plt->output_section->vma
4636                         + resolved_plt->output_offset
4637                         + plt_offset);
4638           unresolved_reloc = FALSE;
4639           break;
4640
4641         case R_386_SIZE32:
4642           /* Set to symbol size.  */
4643           relocation = st_size;
4644           /* Fall through.  */
4645
4646         case R_386_32:
4647         case R_386_PC32:
4648           if ((input_section->flags & SEC_ALLOC) == 0
4649               || is_vxworks_tls)
4650             break;
4651
4652           /* Copy dynamic function pointer relocations.  Don't generate
4653              dynamic relocations against resolved undefined weak symbols
4654              in PIE, except for R_386_PC32.  */
4655           if ((bfd_link_pic (info)
4656                && (h == NULL
4657                    || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4658                         && (!resolved_to_zero
4659                             || r_type == R_386_PC32))
4660                        || h->root.type != bfd_link_hash_undefweak))
4661                && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
4662                    || !SYMBOL_CALLS_LOCAL (info, h)))
4663               || (ELIMINATE_COPY_RELOCS
4664                   && !bfd_link_pic (info)
4665                   && h != NULL
4666                   && h->dynindx != -1
4667                   && (!h->non_got_ref
4668                       || eh->func_pointer_refcount > 0
4669                       || (h->root.type == bfd_link_hash_undefweak
4670                           && !resolved_to_zero))
4671                   && ((h->def_dynamic && !h->def_regular)
4672                       /* Undefined weak symbol is bound locally when
4673                          PIC is false.  */
4674                       || h->root.type == bfd_link_hash_undefweak)))
4675             {
4676               Elf_Internal_Rela outrel;
4677               bfd_boolean skip, relocate;
4678               asection *sreloc;
4679
4680               /* When generating a shared object, these relocations
4681                  are copied into the output file to be resolved at run
4682                  time.  */
4683
4684               skip = FALSE;
4685               relocate = FALSE;
4686
4687               outrel.r_offset =
4688                 _bfd_elf_section_offset (output_bfd, info, input_section,
4689                                          rel->r_offset);
4690               if (outrel.r_offset == (bfd_vma) -1)
4691                 skip = TRUE;
4692               else if (outrel.r_offset == (bfd_vma) -2)
4693                 skip = TRUE, relocate = TRUE;
4694               outrel.r_offset += (input_section->output_section->vma
4695                                   + input_section->output_offset);
4696
4697               if (skip)
4698                 memset (&outrel, 0, sizeof outrel);
4699               else if (h != NULL
4700                        && h->dynindx != -1
4701                        && (r_type == R_386_PC32
4702                            || !(bfd_link_executable (info)
4703                                 || SYMBOLIC_BIND (info, h))
4704                            || !h->def_regular))
4705                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4706               else
4707                 {
4708                   /* This symbol is local, or marked to become local.  */
4709                   relocate = TRUE;
4710                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4711                 }
4712
4713               sreloc = elf_section_data (input_section)->sreloc;
4714
4715               if (sreloc == NULL || sreloc->contents == NULL)
4716                 {
4717                   r = bfd_reloc_notsupported;
4718                   goto check_relocation_error;
4719                 }
4720
4721               elf_append_rel (output_bfd, sreloc, &outrel);
4722
4723               /* If this reloc is against an external symbol, we do
4724                  not want to fiddle with the addend.  Otherwise, we
4725                  need to include the symbol value so that it becomes
4726                  an addend for the dynamic reloc.  */
4727               if (! relocate)
4728                 continue;
4729             }
4730           break;
4731
4732         case R_386_TLS_IE:
4733           if (!bfd_link_executable (info))
4734             {
4735               Elf_Internal_Rela outrel;
4736               asection *sreloc;
4737
4738               outrel.r_offset = rel->r_offset
4739                                 + input_section->output_section->vma
4740                                 + input_section->output_offset;
4741               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4742               sreloc = elf_section_data (input_section)->sreloc;
4743               if (sreloc == NULL)
4744                 abort ();
4745               elf_append_rel (output_bfd, sreloc, &outrel);
4746             }
4747           /* Fall through */
4748
4749         case R_386_TLS_GD:
4750         case R_386_TLS_GOTDESC:
4751         case R_386_TLS_DESC_CALL:
4752         case R_386_TLS_IE_32:
4753         case R_386_TLS_GOTIE:
4754           tls_type = GOT_UNKNOWN;
4755           if (h == NULL && local_got_offsets)
4756             tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
4757           else if (h != NULL)
4758             tls_type = elf_i386_hash_entry(h)->tls_type;
4759           if (tls_type == GOT_TLS_IE)
4760             tls_type = GOT_TLS_IE_NEG;
4761
4762           if (! elf_i386_tls_transition (info, input_bfd,
4763                                          input_section, contents,
4764                                          symtab_hdr, sym_hashes,
4765                                          &r_type, tls_type, rel,
4766                                          relend, h, r_symndx, TRUE))
4767             return FALSE;
4768
4769           if (r_type == R_386_TLS_LE_32)
4770             {
4771               BFD_ASSERT (! unresolved_reloc);
4772               if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
4773                 {
4774                   unsigned int type;
4775                   bfd_vma roff;
4776
4777                   /* GD->LE transition.  */
4778                   type = *(contents + rel->r_offset - 2);
4779                   if (type == 0x04)
4780                     {
4781                       /* Change
4782                                 leal foo@tlsgd(,%ebx,1), %eax
4783                                 call ___tls_get_addr@PLT
4784                          into:
4785                                 movl %gs:0, %eax
4786                                 subl $foo@tpoff, %eax
4787                          (6 byte form of subl).  */
4788                       roff = rel->r_offset + 5;
4789                     }
4790                   else
4791                     {
4792                       /* Change
4793                                 leal foo@tlsgd(%ebx), %eax
4794                                 call ___tls_get_addr@PLT
4795                                 nop
4796                          or
4797                                 leal foo@tlsgd(%reg), %eax
4798                                 call *___tls_get_addr@GOT(%reg)
4799                                 which may be converted to
4800                                 addr32 call ___tls_get_addr
4801                          into:
4802                                 movl %gs:0, %eax; subl $foo@tpoff, %eax
4803                          (6 byte form of subl).  */
4804                       roff = rel->r_offset + 6;
4805                     }
4806                   memcpy (contents + roff - 8,
4807                           "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
4808                   bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4809                               contents + roff);
4810                   /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X.  */
4811                   rel++;
4812                   wrel++;
4813                   continue;
4814                 }
4815               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
4816                 {
4817                   /* GDesc -> LE transition.
4818                      It's originally something like:
4819                      leal x@tlsdesc(%ebx), %eax
4820
4821                      leal x@ntpoff, %eax
4822
4823                      Registers other than %eax may be set up here.  */
4824
4825                   unsigned int val;
4826                   bfd_vma roff;
4827
4828                   roff = rel->r_offset;
4829                   val = bfd_get_8 (input_bfd, contents + roff - 1);
4830
4831                   /* Now modify the instruction as appropriate.  */
4832                   /* aoliva FIXME: remove the above and xor the byte
4833                      below with 0x86.  */
4834                   bfd_put_8 (output_bfd, val ^ 0x86,
4835                              contents + roff - 1);
4836                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4837                               contents + roff);
4838                   continue;
4839                 }
4840               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
4841                 {
4842                   /* GDesc -> LE transition.
4843                      It's originally:
4844                      call *(%eax)
4845                      Turn it into:
4846                      xchg %ax,%ax  */
4847
4848                   bfd_vma roff;
4849
4850                   roff = rel->r_offset;
4851                   bfd_put_8 (output_bfd, 0x66, contents + roff);
4852                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4853                   continue;
4854                 }
4855               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
4856                 {
4857                   unsigned int val;
4858
4859                   /* IE->LE transition:
4860                      Originally it can be one of:
4861                      movl foo, %eax
4862                      movl foo, %reg
4863                      addl foo, %reg
4864                      We change it into:
4865                      movl $foo, %eax
4866                      movl $foo, %reg
4867                      addl $foo, %reg.  */
4868                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4869                   if (val == 0xa1)
4870                     {
4871                       /* movl foo, %eax.  */
4872                       bfd_put_8 (output_bfd, 0xb8,
4873                                  contents + rel->r_offset - 1);
4874                     }
4875                   else
4876                     {
4877                       unsigned int type;
4878
4879                       type = bfd_get_8 (input_bfd,
4880                                         contents + rel->r_offset - 2);
4881                       switch (type)
4882                         {
4883                         case 0x8b:
4884                           /* movl */
4885                           bfd_put_8 (output_bfd, 0xc7,
4886                                      contents + rel->r_offset - 2);
4887                           bfd_put_8 (output_bfd,
4888                                      0xc0 | ((val >> 3) & 7),
4889                                      contents + rel->r_offset - 1);
4890                           break;
4891                         case 0x03:
4892                           /* addl */
4893                           bfd_put_8 (output_bfd, 0x81,
4894                                      contents + rel->r_offset - 2);
4895                           bfd_put_8 (output_bfd,
4896                                      0xc0 | ((val >> 3) & 7),
4897                                      contents + rel->r_offset - 1);
4898                           break;
4899                         default:
4900                           BFD_FAIL ();
4901                           break;
4902                         }
4903                     }
4904                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4905                               contents + rel->r_offset);
4906                   continue;
4907                 }
4908               else
4909                 {
4910                   unsigned int val, type;
4911
4912                   /* {IE_32,GOTIE}->LE transition:
4913                      Originally it can be one of:
4914                      subl foo(%reg1), %reg2
4915                      movl foo(%reg1), %reg2
4916                      addl foo(%reg1), %reg2
4917                      We change it into:
4918                      subl $foo, %reg2
4919                      movl $foo, %reg2 (6 byte form)
4920                      addl $foo, %reg2.  */
4921                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
4922                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
4923                   if (type == 0x8b)
4924                     {
4925                       /* movl */
4926                       bfd_put_8 (output_bfd, 0xc7,
4927                                  contents + rel->r_offset - 2);
4928                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4929                                  contents + rel->r_offset - 1);
4930                     }
4931                   else if (type == 0x2b)
4932                     {
4933                       /* subl */
4934                       bfd_put_8 (output_bfd, 0x81,
4935                                  contents + rel->r_offset - 2);
4936                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
4937                                  contents + rel->r_offset - 1);
4938                     }
4939                   else if (type == 0x03)
4940                     {
4941                       /* addl */
4942                       bfd_put_8 (output_bfd, 0x81,
4943                                  contents + rel->r_offset - 2);
4944                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4945                                  contents + rel->r_offset - 1);
4946                     }
4947                   else
4948                     BFD_FAIL ();
4949                   if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
4950                     bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
4951                                 contents + rel->r_offset);
4952                   else
4953                     bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
4954                                 contents + rel->r_offset);
4955                   continue;
4956                 }
4957             }
4958
4959           if (htab->elf.sgot == NULL)
4960             abort ();
4961
4962           if (h != NULL)
4963             {
4964               off = h->got.offset;
4965               offplt = elf_i386_hash_entry (h)->tlsdesc_got;
4966             }
4967           else
4968             {
4969               if (local_got_offsets == NULL)
4970                 abort ();
4971
4972               off = local_got_offsets[r_symndx];
4973               offplt = local_tlsdesc_gotents[r_symndx];
4974             }
4975
4976           if ((off & 1) != 0)
4977             off &= ~1;
4978           else
4979             {
4980               Elf_Internal_Rela outrel;
4981               int dr_type;
4982               asection *sreloc;
4983
4984               if (htab->elf.srelgot == NULL)
4985                 abort ();
4986
4987               indx = h && h->dynindx != -1 ? h->dynindx : 0;
4988
4989               if (GOT_TLS_GDESC_P (tls_type))
4990                 {
4991                   bfd_byte *loc;
4992                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
4993                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
4994                               <= htab->elf.sgotplt->size);
4995                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4996                                      + htab->elf.sgotplt->output_offset
4997                                      + offplt
4998                                      + htab->sgotplt_jump_table_size);
4999                   sreloc = htab->elf.srelplt;
5000                   loc = sreloc->contents;
5001                   loc += (htab->next_tls_desc_index++
5002                           * sizeof (Elf32_External_Rel));
5003                   BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
5004                               <= sreloc->contents + sreloc->size);
5005                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
5006                   if (indx == 0)
5007                     {
5008                       BFD_ASSERT (! unresolved_reloc);
5009                       bfd_put_32 (output_bfd,
5010                                   relocation - elf_i386_dtpoff_base (info),
5011                                   htab->elf.sgotplt->contents + offplt
5012                                   + htab->sgotplt_jump_table_size + 4);
5013                     }
5014                   else
5015                     {
5016                       bfd_put_32 (output_bfd, 0,
5017                                   htab->elf.sgotplt->contents + offplt
5018                                   + htab->sgotplt_jump_table_size + 4);
5019                     }
5020                 }
5021
5022               sreloc = htab->elf.srelgot;
5023
5024               outrel.r_offset = (htab->elf.sgot->output_section->vma
5025                                  + htab->elf.sgot->output_offset + off);
5026
5027               if (GOT_TLS_GD_P (tls_type))
5028                 dr_type = R_386_TLS_DTPMOD32;
5029               else if (GOT_TLS_GDESC_P (tls_type))
5030                 goto dr_done;
5031               else if (tls_type == GOT_TLS_IE_POS)
5032                 dr_type = R_386_TLS_TPOFF;
5033               else
5034                 dr_type = R_386_TLS_TPOFF32;
5035
5036               if (dr_type == R_386_TLS_TPOFF && indx == 0)
5037                 bfd_put_32 (output_bfd,
5038                             relocation - elf_i386_dtpoff_base (info),
5039                             htab->elf.sgot->contents + off);
5040               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
5041                 bfd_put_32 (output_bfd,
5042                             elf_i386_dtpoff_base (info) - relocation,
5043                             htab->elf.sgot->contents + off);
5044               else if (dr_type != R_386_TLS_DESC)
5045                 bfd_put_32 (output_bfd, 0,
5046                             htab->elf.sgot->contents + off);
5047               outrel.r_info = ELF32_R_INFO (indx, dr_type);
5048
5049               elf_append_rel (output_bfd, sreloc, &outrel);
5050
5051               if (GOT_TLS_GD_P (tls_type))
5052                 {
5053                   if (indx == 0)
5054                     {
5055                       BFD_ASSERT (! unresolved_reloc);
5056                       bfd_put_32 (output_bfd,
5057                                   relocation - elf_i386_dtpoff_base (info),
5058                                   htab->elf.sgot->contents + off + 4);
5059                     }
5060                   else
5061                     {
5062                       bfd_put_32 (output_bfd, 0,
5063                                   htab->elf.sgot->contents + off + 4);
5064                       outrel.r_info = ELF32_R_INFO (indx,
5065                                                     R_386_TLS_DTPOFF32);
5066                       outrel.r_offset += 4;
5067                       elf_append_rel (output_bfd, sreloc, &outrel);
5068                     }
5069                 }
5070               else if (tls_type == GOT_TLS_IE_BOTH)
5071                 {
5072                   bfd_put_32 (output_bfd,
5073                               (indx == 0
5074                                ? relocation - elf_i386_dtpoff_base (info)
5075                                : 0),
5076                               htab->elf.sgot->contents + off + 4);
5077                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
5078                   outrel.r_offset += 4;
5079                   elf_append_rel (output_bfd, sreloc, &outrel);
5080                 }
5081
5082             dr_done:
5083               if (h != NULL)
5084                 h->got.offset |= 1;
5085               else
5086                 local_got_offsets[r_symndx] |= 1;
5087             }
5088
5089           if (off >= (bfd_vma) -2
5090               && ! GOT_TLS_GDESC_P (tls_type))
5091             abort ();
5092           if (r_type == R_386_TLS_GOTDESC
5093               || r_type == R_386_TLS_DESC_CALL)
5094             {
5095               relocation = htab->sgotplt_jump_table_size + offplt;
5096               unresolved_reloc = FALSE;
5097             }
5098           else if (r_type == ELF32_R_TYPE (rel->r_info))
5099             {
5100               bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
5101                               + htab->elf.sgotplt->output_offset;
5102               relocation = htab->elf.sgot->output_section->vma
5103                 + htab->elf.sgot->output_offset + off - g_o_t;
5104               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
5105                   && tls_type == GOT_TLS_IE_BOTH)
5106                 relocation += 4;
5107               if (r_type == R_386_TLS_IE)
5108                 relocation += g_o_t;
5109               unresolved_reloc = FALSE;
5110             }
5111           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
5112             {
5113               unsigned int val, type;
5114               bfd_vma roff;
5115
5116               /* GD->IE transition.  */
5117               type = *(contents + rel->r_offset - 2);
5118               val = *(contents + rel->r_offset - 1);
5119               if (type == 0x04)
5120                 {
5121                   /* Change
5122                         leal foo@tlsgd(,%ebx,1), %eax
5123                         call ___tls_get_addr@PLT
5124                      into:
5125                         movl %gs:0, %eax
5126                         subl $foo@gottpoff(%ebx), %eax.  */
5127                   val >>= 3;
5128                   roff = rel->r_offset - 3;
5129                 }
5130               else
5131                 {
5132                   /* Change
5133                         leal foo@tlsgd(%ebx), %eax
5134                         call ___tls_get_addr@PLT
5135                         nop
5136                      or
5137                         leal foo@tlsgd(%reg), %eax
5138                         call *___tls_get_addr@GOT(%reg)
5139                         which may be converted to
5140                         addr32 call ___tls_get_addr
5141                      into:
5142                         movl %gs:0, %eax;
5143                         subl $foo@gottpoff(%reg), %eax.  */
5144                   roff = rel->r_offset - 2;
5145                 }
5146               memcpy (contents + roff,
5147                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
5148               contents[roff + 7] = 0x80 | (val & 7);
5149               /* If foo is used only with foo@gotntpoff(%reg) and
5150                  foo@indntpoff, but not with foo@gottpoff(%reg), change
5151                  subl $foo@gottpoff(%reg), %eax
5152                  into:
5153                  addl $foo@gotntpoff(%reg), %eax.  */
5154               if (tls_type == GOT_TLS_IE_POS)
5155                 contents[roff + 6] = 0x03;
5156               bfd_put_32 (output_bfd,
5157                           htab->elf.sgot->output_section->vma
5158                           + htab->elf.sgot->output_offset + off
5159                           - htab->elf.sgotplt->output_section->vma
5160                           - htab->elf.sgotplt->output_offset,
5161                           contents + roff + 8);
5162               /* Skip R_386_PLT32 and R_386_GOT32X.  */
5163               rel++;
5164               wrel++;
5165               continue;
5166             }
5167           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
5168             {
5169               /* GDesc -> IE transition.
5170                  It's originally something like:
5171                  leal x@tlsdesc(%ebx), %eax
5172
5173                  Change it to:
5174                  movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
5175                  or:
5176                  movl x@gottpoff(%ebx), %eax # before negl %eax
5177
5178                  Registers other than %eax may be set up here.  */
5179
5180               bfd_vma roff;
5181
5182               /* First, make sure it's a leal adding ebx to a 32-bit
5183                  offset into any register, although it's probably
5184                  almost always going to be eax.  */
5185               roff = rel->r_offset;
5186
5187               /* Now modify the instruction as appropriate.  */
5188               /* To turn a leal into a movl in the form we use it, it
5189                  suffices to change the first byte from 0x8d to 0x8b.
5190                  aoliva FIXME: should we decide to keep the leal, all
5191                  we have to do is remove the statement below, and
5192                  adjust the relaxation of R_386_TLS_DESC_CALL.  */
5193               bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5194
5195               if (tls_type == GOT_TLS_IE_BOTH)
5196                 off += 4;
5197
5198               bfd_put_32 (output_bfd,
5199                           htab->elf.sgot->output_section->vma
5200                           + htab->elf.sgot->output_offset + off
5201                           - htab->elf.sgotplt->output_section->vma
5202                           - htab->elf.sgotplt->output_offset,
5203                           contents + roff);
5204               continue;
5205             }
5206           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
5207             {
5208               /* GDesc -> IE transition.
5209                  It's originally:
5210                  call *(%eax)
5211
5212                  Change it to:
5213                  xchg %ax,%ax
5214                  or
5215                  negl %eax
5216                  depending on how we transformed the TLS_GOTDESC above.
5217               */
5218
5219               bfd_vma roff;
5220
5221               roff = rel->r_offset;
5222
5223               /* Now modify the instruction as appropriate.  */
5224               if (tls_type != GOT_TLS_IE_NEG)
5225                 {
5226                   /* xchg %ax,%ax */
5227                   bfd_put_8 (output_bfd, 0x66, contents + roff);
5228                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5229                 }
5230               else
5231                 {
5232                   /* negl %eax */
5233                   bfd_put_8 (output_bfd, 0xf7, contents + roff);
5234                   bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
5235                 }
5236
5237               continue;
5238             }
5239           else
5240             BFD_ASSERT (FALSE);
5241           break;
5242
5243         case R_386_TLS_LDM:
5244           if (! elf_i386_tls_transition (info, input_bfd,
5245                                          input_section, contents,
5246                                          symtab_hdr, sym_hashes,
5247                                          &r_type, GOT_UNKNOWN, rel,
5248                                          relend, h, r_symndx, TRUE))
5249             return FALSE;
5250
5251           if (r_type != R_386_TLS_LDM)
5252             {
5253               /* LD->LE transition.  Change
5254                         leal foo@tlsldm(%ebx) %eax
5255                         call ___tls_get_addr@PLT
5256                  into:
5257                         movl %gs:0, %eax
5258                         nop
5259                         leal 0(%esi,1), %esi
5260                  or change
5261                         leal foo@tlsldm(%reg) %eax
5262                         call *___tls_get_addr@GOT(%reg)
5263                         which may be converted to
5264                         addr32 call ___tls_get_addr
5265                  into:
5266                         movl %gs:0, %eax
5267                         leal 0(%esi), %esi  */
5268               BFD_ASSERT (r_type == R_386_TLS_LE_32);
5269               if (*(contents + rel->r_offset + 4) == 0xff
5270                   || *(contents + rel->r_offset + 4) == 0x67)
5271                 memcpy (contents + rel->r_offset - 2,
5272                         "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
5273               else
5274                 memcpy (contents + rel->r_offset - 2,
5275                         "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
5276               /* Skip R_386_PC32/R_386_PLT32.  */
5277               rel++;
5278               wrel++;
5279               continue;
5280             }
5281
5282           if (htab->elf.sgot == NULL)
5283             abort ();
5284
5285           off = htab->tls_ldm_got.offset;
5286           if (off & 1)
5287             off &= ~1;
5288           else
5289             {
5290               Elf_Internal_Rela outrel;
5291
5292               if (htab->elf.srelgot == NULL)
5293                 abort ();
5294
5295               outrel.r_offset = (htab->elf.sgot->output_section->vma
5296                                  + htab->elf.sgot->output_offset + off);
5297
5298               bfd_put_32 (output_bfd, 0,
5299                           htab->elf.sgot->contents + off);
5300               bfd_put_32 (output_bfd, 0,
5301                           htab->elf.sgot->contents + off + 4);
5302               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
5303               elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
5304               htab->tls_ldm_got.offset |= 1;
5305             }
5306           relocation = htab->elf.sgot->output_section->vma
5307                        + htab->elf.sgot->output_offset + off
5308                        - htab->elf.sgotplt->output_section->vma
5309                        - htab->elf.sgotplt->output_offset;
5310           unresolved_reloc = FALSE;
5311           break;
5312
5313         case R_386_TLS_LDO_32:
5314           if (!bfd_link_executable (info)
5315               || (input_section->flags & SEC_CODE) == 0)
5316             relocation -= elf_i386_dtpoff_base (info);
5317           else
5318             /* When converting LDO to LE, we must negate.  */
5319             relocation = -elf_i386_tpoff (info, relocation);
5320           break;
5321
5322         case R_386_TLS_LE_32:
5323         case R_386_TLS_LE:
5324           if (!bfd_link_executable (info))
5325             {
5326               Elf_Internal_Rela outrel;
5327               asection *sreloc;
5328
5329               outrel.r_offset = rel->r_offset
5330                                 + input_section->output_section->vma
5331                                 + input_section->output_offset;
5332               if (h != NULL && h->dynindx != -1)
5333                 indx = h->dynindx;
5334               else
5335                 indx = 0;
5336               if (r_type == R_386_TLS_LE_32)
5337                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
5338               else
5339                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
5340               sreloc = elf_section_data (input_section)->sreloc;
5341               if (sreloc == NULL)
5342                 abort ();
5343               elf_append_rel (output_bfd, sreloc, &outrel);
5344               if (indx)
5345                 continue;
5346               else if (r_type == R_386_TLS_LE_32)
5347                 relocation = elf_i386_dtpoff_base (info) - relocation;
5348               else
5349                 relocation -= elf_i386_dtpoff_base (info);
5350             }
5351           else if (r_type == R_386_TLS_LE_32)
5352             relocation = elf_i386_tpoff (info, relocation);
5353           else
5354             relocation = -elf_i386_tpoff (info, relocation);
5355           break;
5356
5357         default:
5358           break;
5359         }
5360
5361       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5362          because such sections are not SEC_ALLOC and thus ld.so will
5363          not process them.  */
5364       if (unresolved_reloc
5365           && !((input_section->flags & SEC_DEBUGGING) != 0
5366                && h->def_dynamic)
5367           && _bfd_elf_section_offset (output_bfd, info, input_section,
5368                                       rel->r_offset) != (bfd_vma) -1)
5369         {
5370           _bfd_error_handler
5371             /* xgettext:c-format */
5372             (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
5373              input_bfd,
5374              input_section,
5375              rel->r_offset,
5376              howto->name,
5377              h->root.root.string);
5378           return FALSE;
5379         }
5380
5381 do_relocation:
5382       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5383                                     contents, rel->r_offset,
5384                                     relocation, 0);
5385
5386 check_relocation_error:
5387       if (r != bfd_reloc_ok)
5388         {
5389           const char *name;
5390
5391           if (h != NULL)
5392             name = h->root.root.string;
5393           else
5394             {
5395               name = bfd_elf_string_from_elf_section (input_bfd,
5396                                                       symtab_hdr->sh_link,
5397                                                       sym->st_name);
5398               if (name == NULL)
5399                 return FALSE;
5400               if (*name == '\0')
5401                 name = bfd_section_name (input_bfd, sec);
5402             }
5403
5404           if (r == bfd_reloc_overflow)
5405             (*info->callbacks->reloc_overflow)
5406               (info, (h ? &h->root : NULL), name, howto->name,
5407                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5408           else
5409             {
5410               _bfd_error_handler
5411                 /* xgettext:c-format */
5412                 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
5413                  input_bfd, input_section,
5414                  rel->r_offset, name, (int) r);
5415               return FALSE;
5416             }
5417         }
5418
5419       if (wrel != rel)
5420         *wrel = *rel;
5421     }
5422
5423   if (wrel != rel)
5424     {
5425       Elf_Internal_Shdr *rel_hdr;
5426       size_t deleted = rel - wrel;
5427
5428       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5429       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5430       if (rel_hdr->sh_size == 0)
5431         {
5432           /* It is too late to remove an empty reloc section.  Leave
5433              one NONE reloc.
5434              ??? What is wrong with an empty section???  */
5435           rel_hdr->sh_size = rel_hdr->sh_entsize;
5436           deleted -= 1;
5437         }
5438       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5439       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5440       input_section->reloc_count -= deleted;
5441     }
5442
5443   return TRUE;
5444 }
5445
5446 /* Finish up dynamic symbol handling.  We set the contents of various
5447    dynamic sections here.  */
5448
5449 static bfd_boolean
5450 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
5451                                 struct bfd_link_info *info,
5452                                 struct elf_link_hash_entry *h,
5453                                 Elf_Internal_Sym *sym)
5454 {
5455   struct elf_i386_link_hash_table *htab;
5456   unsigned plt_entry_size;
5457   const struct elf_i386_backend_data *abed;
5458   struct elf_i386_link_hash_entry *eh;
5459   bfd_boolean local_undefweak;
5460   bfd_boolean use_plt_second;
5461
5462   htab = elf_i386_hash_table (info);
5463   if (htab == NULL)
5464     return FALSE;
5465
5466   abed = get_elf_i386_backend_data (output_bfd);
5467   plt_entry_size = htab->plt.plt_entry_size;
5468
5469   /* Use the second PLT section only if there is .plt section.  */
5470   use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
5471
5472   eh = (struct elf_i386_link_hash_entry *) h;
5473   if (eh->no_finish_dynamic_symbol)
5474     abort ();
5475
5476   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5477      resolved undefined weak symbols in executable so that their
5478      references have value 0 at run-time.  */
5479   local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5480                                                      eh->has_got_reloc,
5481                                                      eh);
5482
5483   if (h->plt.offset != (bfd_vma) -1)
5484     {
5485       bfd_vma plt_index, plt_offset;
5486       bfd_vma got_offset;
5487       Elf_Internal_Rela rel;
5488       bfd_byte *loc;
5489       asection *plt, *resolved_plt, *gotplt, *relplt;
5490
5491       /* When building a static executable, use .iplt, .igot.plt and
5492          .rel.iplt sections for STT_GNU_IFUNC symbols.  */
5493       if (htab->elf.splt != NULL)
5494         {
5495           plt = htab->elf.splt;
5496           gotplt = htab->elf.sgotplt;
5497           relplt = htab->elf.srelplt;
5498         }
5499       else
5500         {
5501           plt = htab->elf.iplt;
5502           gotplt = htab->elf.igotplt;
5503           relplt = htab->elf.irelplt;
5504         }
5505
5506       /* This symbol has an entry in the procedure linkage table.  Set
5507          it up.  */
5508
5509       if ((h->dynindx == -1
5510            && !local_undefweak
5511            && !((h->forced_local || bfd_link_executable (info))
5512                 && h->def_regular
5513                 && h->type == STT_GNU_IFUNC))
5514           || plt == NULL
5515           || gotplt == NULL
5516           || relplt == NULL)
5517         abort ();
5518
5519       /* Get the index in the procedure linkage table which
5520          corresponds to this symbol.  This is the index of this symbol
5521          in all the symbols for which we are making plt entries.  The
5522          first entry in the procedure linkage table is reserved.
5523
5524          Get the offset into the .got table of the entry that
5525          corresponds to this function.  Each .got entry is 4 bytes.
5526          The first three are reserved.
5527
5528          For static executables, we don't reserve anything.  */
5529
5530       if (plt == htab->elf.splt)
5531         {
5532           got_offset = (h->plt.offset / plt_entry_size
5533                         - htab->plt.has_plt0);
5534           got_offset = (got_offset + 3) * 4;
5535         }
5536       else
5537         {
5538           got_offset = h->plt.offset / plt_entry_size;
5539           got_offset = got_offset * 4;
5540         }
5541
5542       /* Fill in the entry in the procedure linkage table and update
5543          the first slot.  */
5544       memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
5545               plt_entry_size);
5546
5547       if (use_plt_second)
5548         {
5549           const bfd_byte *plt_entry;
5550           if (bfd_link_pic (info))
5551             plt_entry = htab->non_lazy_plt->pic_plt_entry;
5552           else
5553             plt_entry = htab->non_lazy_plt->plt_entry;
5554           memcpy (htab->plt_second->contents + eh->plt_second.offset,
5555                   plt_entry, htab->non_lazy_plt->plt_entry_size);
5556
5557           resolved_plt = htab->plt_second;
5558           plt_offset = eh->plt_second.offset;
5559         }
5560       else
5561         {
5562           resolved_plt = plt;
5563           plt_offset = h->plt.offset;
5564         }
5565
5566       if (! bfd_link_pic (info))
5567         {
5568           bfd_put_32 (output_bfd,
5569                       (gotplt->output_section->vma
5570                        + gotplt->output_offset
5571                        + got_offset),
5572                       resolved_plt->contents + plt_offset
5573                       + htab->plt.plt_got_offset);
5574
5575           if (abed->os == is_vxworks)
5576             {
5577               int s, k, reloc_index;
5578
5579               /* Create the R_386_32 relocation referencing the GOT
5580                  for this PLT entry.  */
5581
5582               /* S: Current slot number (zero-based).  */
5583               s = ((h->plt.offset - htab->plt.plt_entry_size)
5584                    / htab->plt.plt_entry_size);
5585               /* K: Number of relocations for PLTResolve. */
5586               if (bfd_link_pic (info))
5587                 k = PLTRESOLVE_RELOCS_SHLIB;
5588               else
5589                 k = PLTRESOLVE_RELOCS;
5590               /* Skip the PLTresolve relocations, and the relocations for
5591                  the other PLT slots. */
5592               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
5593               loc = (htab->srelplt2->contents + reloc_index
5594                      * sizeof (Elf32_External_Rel));
5595
5596               rel.r_offset = (plt->output_section->vma
5597                               + plt->output_offset
5598                               + h->plt.offset + 2),
5599               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
5600               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5601
5602               /* Create the R_386_32 relocation referencing the beginning of
5603                  the PLT for this GOT entry.  */
5604               rel.r_offset = (htab->elf.sgotplt->output_section->vma
5605                               + htab->elf.sgotplt->output_offset
5606                               + got_offset);
5607               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
5608               bfd_elf32_swap_reloc_out (output_bfd, &rel,
5609                                         loc + sizeof (Elf32_External_Rel));
5610             }
5611         }
5612       else
5613         {
5614           bfd_put_32 (output_bfd, got_offset,
5615                       resolved_plt->contents + plt_offset
5616                       + htab->plt.plt_got_offset);
5617         }
5618
5619       /* Fill in the entry in the global offset table.  Leave the entry
5620          as zero for undefined weak symbol in PIE.  No PLT relocation
5621          against undefined weak symbol in PIE.  */
5622       if (!local_undefweak)
5623         {
5624           if (htab->plt.has_plt0)
5625             bfd_put_32 (output_bfd,
5626                         (plt->output_section->vma
5627                          + plt->output_offset
5628                          + h->plt.offset
5629                          + htab->lazy_plt->plt_lazy_offset),
5630                         gotplt->contents + got_offset);
5631
5632           /* Fill in the entry in the .rel.plt section.  */
5633           rel.r_offset = (gotplt->output_section->vma
5634                           + gotplt->output_offset
5635                           + got_offset);
5636           if (h->dynindx == -1
5637               || ((bfd_link_executable (info)
5638                    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5639                   && h->def_regular
5640                   && h->type == STT_GNU_IFUNC))
5641             {
5642               info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5643                                       h->root.root.string,
5644                                       h->root.u.def.section->owner);
5645
5646               /* If an STT_GNU_IFUNC symbol is locally defined, generate
5647                  R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
5648                  in the .got.plt section.  */
5649               bfd_put_32 (output_bfd,
5650                           (h->root.u.def.value
5651                            + h->root.u.def.section->output_section->vma
5652                            + h->root.u.def.section->output_offset),
5653                           gotplt->contents + got_offset);
5654               rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5655               /* R_386_IRELATIVE comes last.  */
5656               plt_index = htab->next_irelative_index--;
5657             }
5658           else
5659             {
5660               rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
5661               plt_index = htab->next_jump_slot_index++;
5662             }
5663
5664           loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
5665           bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
5666
5667           /* Don't fill the second and third slots in PLT entry for
5668              static executables nor without PLT0.  */
5669           if (plt == htab->elf.splt && htab->plt.has_plt0)
5670             {
5671               bfd_put_32 (output_bfd,
5672                           plt_index * sizeof (Elf32_External_Rel),
5673                           plt->contents + h->plt.offset
5674                           + htab->lazy_plt->plt_reloc_offset);
5675               bfd_put_32 (output_bfd,
5676                           - (h->plt.offset
5677                              + htab->lazy_plt->plt_plt_offset + 4),
5678                           (plt->contents + h->plt.offset
5679                            + htab->lazy_plt->plt_plt_offset));
5680             }
5681         }
5682     }
5683   else if (eh->plt_got.offset != (bfd_vma) -1)
5684     {
5685       bfd_vma got_offset, plt_offset;
5686       asection *plt, *got, *gotplt;
5687       const bfd_byte *got_plt_entry;
5688
5689       /* Set the entry in the GOT procedure linkage table.  */
5690       plt = htab->plt_got;
5691       got = htab->elf.sgot;
5692       gotplt = htab->elf.sgotplt;
5693       got_offset = h->got.offset;
5694
5695       if (got_offset == (bfd_vma) -1
5696           || plt == NULL
5697           || got == NULL
5698           || gotplt == NULL)
5699         abort ();
5700
5701       /* Fill in the entry in the GOT procedure linkage table.  */
5702       if (! bfd_link_pic (info))
5703         {
5704           got_plt_entry = htab->non_lazy_plt->plt_entry;
5705           got_offset += got->output_section->vma + got->output_offset;
5706         }
5707       else
5708         {
5709           got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
5710           got_offset += (got->output_section->vma
5711                          + got->output_offset
5712                          - gotplt->output_section->vma
5713                          - gotplt->output_offset);
5714         }
5715
5716       plt_offset = eh->plt_got.offset;
5717       memcpy (plt->contents + plt_offset, got_plt_entry,
5718               htab->non_lazy_plt->plt_entry_size);
5719       bfd_put_32 (output_bfd, got_offset,
5720                   (plt->contents + plt_offset
5721                    + htab->non_lazy_plt->plt_got_offset));
5722     }
5723
5724   if (!local_undefweak
5725       && !h->def_regular
5726       && (h->plt.offset != (bfd_vma) -1
5727           || eh->plt_got.offset != (bfd_vma) -1))
5728     {
5729       /* Mark the symbol as undefined, rather than as defined in
5730          the .plt section.  Leave the value if there were any
5731          relocations where pointer equality matters (this is a clue
5732          for the dynamic linker, to make function pointer
5733          comparisons work between an application and shared
5734          library), otherwise set it to zero.  If a function is only
5735          called from a binary, there is no need to slow down
5736          shared libraries because of that.  */
5737       sym->st_shndx = SHN_UNDEF;
5738       if (!h->pointer_equality_needed)
5739         sym->st_value = 0;
5740     }
5741
5742   /* Don't generate dynamic GOT relocation against undefined weak
5743      symbol in executable.  */
5744   if (h->got.offset != (bfd_vma) -1
5745       && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
5746       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
5747       && !local_undefweak)
5748     {
5749       Elf_Internal_Rela rel;
5750       asection *relgot = htab->elf.srelgot;
5751
5752       /* This symbol has an entry in the global offset table.  Set it
5753          up.  */
5754
5755       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5756         abort ();
5757
5758       rel.r_offset = (htab->elf.sgot->output_section->vma
5759                       + htab->elf.sgot->output_offset
5760                       + (h->got.offset & ~(bfd_vma) 1));
5761
5762       /* If this is a static link, or it is a -Bsymbolic link and the
5763          symbol is defined locally or was forced to be local because
5764          of a version file, we just want to emit a RELATIVE reloc.
5765          The entry in the global offset table will already have been
5766          initialized in the relocate_section function.  */
5767       if (h->def_regular
5768           && h->type == STT_GNU_IFUNC)
5769         {
5770           if (h->plt.offset == (bfd_vma) -1)
5771             {
5772               /* STT_GNU_IFUNC is referenced without PLT.  */
5773               if (htab->elf.splt == NULL)
5774                 {
5775                   /* use .rel[a].iplt section to store .got relocations
5776                      in static executable.  */
5777                   relgot = htab->elf.irelplt;
5778                 }
5779               if (SYMBOL_REFERENCES_LOCAL (info, h))
5780                 {
5781                   info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5782                                           h->root.root.string,
5783                                           h->root.u.def.section->owner);
5784
5785                   bfd_put_32 (output_bfd,
5786                               (h->root.u.def.value
5787                                + h->root.u.def.section->output_section->vma
5788                                + h->root.u.def.section->output_offset),
5789                               htab->elf.sgot->contents + h->got.offset);
5790                   rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
5791                 }
5792               else
5793                 goto do_glob_dat;
5794             }
5795           else if (bfd_link_pic (info))
5796             {
5797               /* Generate R_386_GLOB_DAT.  */
5798               goto do_glob_dat;
5799             }
5800           else
5801             {
5802               asection *plt;
5803               bfd_vma plt_offset;
5804
5805               if (!h->pointer_equality_needed)
5806                 abort ();
5807
5808               /* For non-shared object, we can't use .got.plt, which
5809                  contains the real function addres if we need pointer
5810                  equality.  We load the GOT entry with the PLT entry.  */
5811               if (htab->plt_second != NULL)
5812                 {
5813                   plt = htab->plt_second;
5814                   plt_offset = eh->plt_second.offset;
5815                 }
5816               else
5817                 {
5818                   plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5819                   plt_offset = h->plt.offset;
5820                 }
5821               bfd_put_32 (output_bfd,
5822                           (plt->output_section->vma
5823                            + plt->output_offset + plt_offset),
5824                           htab->elf.sgot->contents + h->got.offset);
5825               return TRUE;
5826             }
5827         }
5828       else if (bfd_link_pic (info)
5829                && SYMBOL_REFERENCES_LOCAL (info, h))
5830         {
5831           BFD_ASSERT((h->got.offset & 1) != 0);
5832           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
5833         }
5834       else
5835         {
5836           BFD_ASSERT((h->got.offset & 1) == 0);
5837 do_glob_dat:
5838           bfd_put_32 (output_bfd, (bfd_vma) 0,
5839                       htab->elf.sgot->contents + h->got.offset);
5840           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
5841         }
5842
5843       elf_append_rel (output_bfd, relgot, &rel);
5844     }
5845
5846   if (h->needs_copy)
5847     {
5848       Elf_Internal_Rela rel;
5849       asection *s;
5850
5851       /* This symbol needs a copy reloc.  Set it up.  */
5852
5853       if (h->dynindx == -1
5854           || (h->root.type != bfd_link_hash_defined
5855               && h->root.type != bfd_link_hash_defweak)
5856           || htab->elf.srelbss == NULL
5857           || htab->elf.sreldynrelro == NULL)
5858         abort ();
5859
5860       rel.r_offset = (h->root.u.def.value
5861                       + h->root.u.def.section->output_section->vma
5862                       + h->root.u.def.section->output_offset);
5863       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
5864       if (h->root.u.def.section == htab->elf.sdynrelro)
5865         s = htab->elf.sreldynrelro;
5866       else
5867         s = htab->elf.srelbss;
5868       elf_append_rel (output_bfd, s, &rel);
5869     }
5870
5871   return TRUE;
5872 }
5873
5874 /* Finish up local dynamic symbol handling.  We set the contents of
5875    various dynamic sections here.  */
5876
5877 static bfd_boolean
5878 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
5879 {
5880   struct elf_link_hash_entry *h
5881     = (struct elf_link_hash_entry *) *slot;
5882   struct bfd_link_info *info
5883     = (struct bfd_link_info *) inf;
5884
5885   return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
5886                                          h, NULL);
5887 }
5888
5889 /* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
5890    here since undefined weak symbol may not be dynamic and may not be
5891    called for elf_i386_finish_dynamic_symbol.  */
5892
5893 static bfd_boolean
5894 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5895                                       void *inf)
5896 {
5897   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5898   struct bfd_link_info *info = (struct bfd_link_info *) inf;
5899
5900   if (h->root.type != bfd_link_hash_undefweak
5901       || h->dynindx != -1)
5902     return TRUE;
5903
5904   return elf_i386_finish_dynamic_symbol (info->output_bfd,
5905                                              info, h, NULL);
5906 }
5907
5908 /* Used to decide how to sort relocs in an optimal manner for the
5909    dynamic linker, before writing them out.  */
5910
5911 static enum elf_reloc_type_class
5912 elf_i386_reloc_type_class (const struct bfd_link_info *info,
5913                            const asection *rel_sec ATTRIBUTE_UNUSED,
5914                            const Elf_Internal_Rela *rela)
5915 {
5916   bfd *abfd = info->output_bfd;
5917   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5918   struct elf_link_hash_table *htab = elf_hash_table (info);
5919
5920   if (htab->dynsym != NULL
5921       && htab->dynsym->contents != NULL)
5922     {
5923       /* Check relocation against STT_GNU_IFUNC symbol if there are
5924          dynamic symbols.  */
5925       unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
5926       if (r_symndx != STN_UNDEF)
5927         {
5928           Elf_Internal_Sym sym;
5929           if (!bed->s->swap_symbol_in (abfd,
5930                                        (htab->dynsym->contents
5931                                         + r_symndx * sizeof (Elf32_External_Sym)),
5932                                        0, &sym))
5933             abort ();
5934
5935           if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5936             return reloc_class_ifunc;
5937         }
5938     }
5939
5940   switch (ELF32_R_TYPE (rela->r_info))
5941     {
5942     case R_386_IRELATIVE:
5943       return reloc_class_ifunc;
5944     case R_386_RELATIVE:
5945       return reloc_class_relative;
5946     case R_386_JUMP_SLOT:
5947       return reloc_class_plt;
5948     case R_386_COPY:
5949       return reloc_class_copy;
5950     default:
5951       return reloc_class_normal;
5952     }
5953 }
5954
5955 /* Finish up the dynamic sections.  */
5956
5957 static bfd_boolean
5958 elf_i386_finish_dynamic_sections (bfd *output_bfd,
5959                                   struct bfd_link_info *info)
5960 {
5961   struct elf_i386_link_hash_table *htab;
5962   bfd *dynobj;
5963   asection *sdyn;
5964   const struct elf_i386_backend_data *abed;
5965
5966   htab = elf_i386_hash_table (info);
5967   if (htab == NULL)
5968     return FALSE;
5969
5970   dynobj = htab->elf.dynobj;
5971   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5972   abed = get_elf_i386_backend_data (output_bfd);
5973
5974   if (htab->elf.dynamic_sections_created)
5975     {
5976       Elf32_External_Dyn *dyncon, *dynconend;
5977
5978       if (sdyn == NULL || htab->elf.sgot == NULL)
5979         abort ();
5980
5981       dyncon = (Elf32_External_Dyn *) sdyn->contents;
5982       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5983       for (; dyncon < dynconend; dyncon++)
5984         {
5985           Elf_Internal_Dyn dyn;
5986           asection *s;
5987
5988           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5989
5990           switch (dyn.d_tag)
5991             {
5992             default:
5993               if (abed->os == is_vxworks
5994                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
5995                 break;
5996               continue;
5997
5998             case DT_PLTGOT:
5999               s = htab->elf.sgotplt;
6000               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6001               break;
6002
6003             case DT_JMPREL:
6004               s = htab->elf.srelplt;
6005               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6006               break;
6007
6008             case DT_PLTRELSZ:
6009               s = htab->elf.srelplt;
6010               dyn.d_un.d_val = s->size;
6011               break;
6012             }
6013
6014           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6015         }
6016
6017       if (htab->elf.splt && htab->elf.splt->size > 0)
6018         {
6019           /* UnixWare sets the entsize of .plt to 4, although that doesn't
6020              really seem like the right value.  */
6021           elf_section_data (htab->elf.splt->output_section)
6022             ->this_hdr.sh_entsize = 4;
6023
6024           if (htab->plt.has_plt0)
6025             {
6026               /* Fill in the special first entry in the procedure linkage
6027                  table.  */
6028               memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
6029                       htab->lazy_plt->plt0_entry_size);
6030               memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
6031                       abed->plt0_pad_byte,
6032                       htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
6033               if (!bfd_link_pic (info))
6034                 {
6035                   bfd_put_32 (output_bfd,
6036                               (htab->elf.sgotplt->output_section->vma
6037                                + htab->elf.sgotplt->output_offset
6038                                + 4),
6039                               htab->elf.splt->contents
6040                               + htab->lazy_plt->plt0_got1_offset);
6041                   bfd_put_32 (output_bfd,
6042                               (htab->elf.sgotplt->output_section->vma
6043                                + htab->elf.sgotplt->output_offset
6044                                + 8),
6045                               htab->elf.splt->contents
6046                               + htab->lazy_plt->plt0_got2_offset);
6047
6048                   if (abed->os == is_vxworks)
6049                     {
6050                       Elf_Internal_Rela rel;
6051                       int num_plts = (htab->elf.splt->size
6052                                       / htab->plt.plt_entry_size) - 1;
6053                       unsigned char *p;
6054
6055                       /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
6056                          + 4.  On IA32 we use REL relocations so the
6057                          addend goes in the PLT directly.  */
6058                       rel.r_offset = (htab->elf.splt->output_section->vma
6059                                       + htab->elf.splt->output_offset
6060                                       + htab->lazy_plt->plt0_got1_offset);
6061                       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
6062                                                  R_386_32);
6063                       bfd_elf32_swap_reloc_out (output_bfd, &rel,
6064                                                 htab->srelplt2->contents);
6065                       /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
6066                          + 8.  */
6067                       rel.r_offset = (htab->elf.splt->output_section->vma
6068                                       + htab->elf.splt->output_offset
6069                                       + htab->lazy_plt->plt0_got2_offset);
6070                       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
6071                                                  R_386_32);
6072                       bfd_elf32_swap_reloc_out (output_bfd, &rel,
6073                                                 htab->srelplt2->contents +
6074                                                 sizeof (Elf32_External_Rel));
6075                       /* Correct the .rel.plt.unloaded relocations.  */
6076                       p = htab->srelplt2->contents;
6077                       if (bfd_link_pic (info))
6078                         p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
6079                       else
6080                         p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
6081
6082                       for (; num_plts; num_plts--)
6083                         {
6084                           bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
6085                           rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
6086                                                      R_386_32);
6087                           bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
6088                           p += sizeof (Elf32_External_Rel);
6089
6090                           bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
6091                           rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
6092                                                      R_386_32);
6093                           bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
6094                           p += sizeof (Elf32_External_Rel);
6095                         }
6096                     }
6097                 }
6098             }
6099         }
6100
6101       if (htab->plt_got != NULL && htab->plt_got->size > 0)
6102         elf_section_data (htab->plt_got->output_section)
6103           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
6104
6105       if (htab->plt_second != NULL && htab->plt_second->size > 0)
6106         elf_section_data (htab->plt_second->output_section)
6107           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
6108     }
6109
6110   /* Fill in the first three entries in the global offset table.  */
6111   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
6112     {
6113       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
6114         {
6115           _bfd_error_handler
6116             (_("discarded output section: `%A'"), htab->elf.sgotplt);
6117           return FALSE;
6118         }
6119
6120       bfd_put_32 (output_bfd,
6121                   (sdyn == NULL ? 0
6122                    : sdyn->output_section->vma + sdyn->output_offset),
6123                   htab->elf.sgotplt->contents);
6124       bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
6125       bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
6126
6127       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
6128     }
6129
6130   /* Adjust .eh_frame for .plt section.  */
6131   if (htab->plt_eh_frame != NULL
6132       && htab->plt_eh_frame->contents != NULL)
6133     {
6134       if (htab->elf.splt != NULL
6135           && htab->elf.splt->size != 0
6136           && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
6137           && htab->elf.splt->output_section != NULL
6138           && htab->plt_eh_frame->output_section != NULL)
6139         {
6140           bfd_vma plt_start = htab->elf.splt->output_section->vma;
6141           bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
6142                                    + htab->plt_eh_frame->output_offset
6143                                    + PLT_FDE_START_OFFSET;
6144           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6145                              htab->plt_eh_frame->contents
6146                              + PLT_FDE_START_OFFSET);
6147         }
6148       if (htab->plt_eh_frame->sec_info_type
6149           == SEC_INFO_TYPE_EH_FRAME)
6150         {
6151           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6152                                                  htab->plt_eh_frame,
6153                                                  htab->plt_eh_frame->contents))
6154             return FALSE;
6155         }
6156     }
6157
6158   /* Adjust .eh_frame for .plt.got section.  */
6159   if (htab->plt_got_eh_frame != NULL
6160       && htab->plt_got_eh_frame->contents != NULL)
6161     {
6162       if (htab->plt_got != NULL
6163           && htab->plt_got->size != 0
6164           && (htab->plt_got->flags & SEC_EXCLUDE) == 0
6165           && htab->plt_got->output_section != NULL
6166           && htab->plt_got_eh_frame->output_section != NULL)
6167         {
6168           bfd_vma plt_start = htab->plt_got->output_section->vma;
6169           bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
6170                                    + htab->plt_got_eh_frame->output_offset
6171                                    + PLT_FDE_START_OFFSET;
6172           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6173                              htab->plt_got_eh_frame->contents
6174                              + PLT_FDE_START_OFFSET);
6175         }
6176       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6177         {
6178           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6179                                                  htab->plt_got_eh_frame,
6180                                                  htab->plt_got_eh_frame->contents))
6181             return FALSE;
6182         }
6183     }
6184
6185   /* Adjust .eh_frame for the second PLT section.  */
6186   if (htab->plt_second_eh_frame != NULL
6187       && htab->plt_second_eh_frame->contents != NULL)
6188     {
6189       if (htab->plt_second != NULL
6190           && htab->plt_second->size != 0
6191           && (htab->plt_second->flags & SEC_EXCLUDE) == 0
6192           && htab->plt_second->output_section != NULL
6193           && htab->plt_second_eh_frame->output_section != NULL)
6194         {
6195           bfd_vma plt_start = htab->plt_second->output_section->vma;
6196           bfd_vma eh_frame_start
6197             = (htab->plt_second_eh_frame->output_section->vma
6198                + htab->plt_second_eh_frame->output_offset
6199                + PLT_FDE_START_OFFSET);
6200           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6201                              htab->plt_second_eh_frame->contents
6202                              + PLT_FDE_START_OFFSET);
6203         }
6204       if (htab->plt_second_eh_frame->sec_info_type
6205           == SEC_INFO_TYPE_EH_FRAME)
6206         {
6207           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6208                                                  htab->plt_second_eh_frame,
6209                                                  htab->plt_second_eh_frame->contents))
6210             return FALSE;
6211         }
6212     }
6213
6214   if (htab->elf.sgot && htab->elf.sgot->size > 0)
6215     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
6216
6217   /* Fill PLT entries for undefined weak symbols in PIE.  */
6218   if (bfd_link_pie (info))
6219     bfd_hash_traverse (&info->hash->table,
6220                        elf_i386_pie_finish_undefweak_symbol,
6221                        info);
6222
6223   return TRUE;
6224 }
6225
6226 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6227    STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6228    It has to be done before elf_link_sort_relocs is called so that
6229    dynamic relocations are properly sorted.  */
6230
6231 static bfd_boolean
6232 elf_i386_output_arch_local_syms
6233   (bfd *output_bfd ATTRIBUTE_UNUSED,
6234    struct bfd_link_info *info,
6235    void *flaginfo ATTRIBUTE_UNUSED,
6236    int (*func) (void *, const char *,
6237                 Elf_Internal_Sym *,
6238                 asection *,
6239                 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6240 {
6241   struct elf_i386_link_hash_table *htab = elf_i386_hash_table (info);
6242   if (htab == NULL)
6243     return FALSE;
6244
6245   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
6246   htab_traverse (htab->loc_hash_table,
6247                  elf_i386_finish_local_dynamic_symbol,
6248                  info);
6249
6250   return TRUE;
6251 }
6252
6253 /* Sort relocs into address order.  */
6254
6255 static int
6256 compare_relocs (const void *ap, const void *bp)
6257 {
6258   const arelent *a = * (const arelent **) ap;
6259   const arelent *b = * (const arelent **) bp;
6260
6261   if (a->address > b->address)
6262     return 1;
6263   else if (a->address < b->address)
6264     return -1;
6265   else
6266     return 0;
6267 }
6268
6269 enum elf_i386_plt_type
6270 {
6271   plt_non_lazy = 0,
6272   plt_lazy = 1 << 0,
6273   plt_pic = 1 << 1,
6274   plt_second = 1 << 2,
6275   plt_unknown = -1
6276 };
6277
6278 struct elf_i386_plt
6279 {
6280   const char *name;
6281   asection *sec;
6282   bfd_byte *contents;
6283   enum elf_i386_plt_type type;
6284   unsigned int plt_got_offset;
6285   unsigned int plt_entry_size;
6286   long count;
6287 };
6288
6289 /* Forward declaration.  */
6290 static const struct elf_i386_lazy_plt_layout elf_i386_nacl_plt;
6291
6292 /* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
6293    dynamic relocations.   */
6294
6295 static long
6296 elf_i386_get_synthetic_symtab (bfd *abfd,
6297                                long symcount ATTRIBUTE_UNUSED,
6298                                asymbol **syms ATTRIBUTE_UNUSED,
6299                                long dynsymcount,
6300                                asymbol **dynsyms,
6301                                asymbol **ret)
6302 {
6303   long size, count, i, n;
6304   int j;
6305   unsigned int plt_got_offset, plt_entry_size;
6306   asymbol *s;
6307   bfd_byte *plt_contents;
6308   long dynrelcount, relsize;
6309   arelent **dynrelbuf;
6310   const struct elf_i386_lazy_plt_layout *lazy_plt;
6311   const struct elf_i386_non_lazy_plt_layout *non_lazy_plt;
6312   const struct elf_i386_lazy_plt_layout *lazy_ibt_plt;
6313   const struct elf_i386_non_lazy_plt_layout *non_lazy_ibt_plt;
6314   asection *plt;
6315   bfd_vma got_addr;
6316   char *names;
6317   enum elf_i386_plt_type plt_type;
6318   struct elf_i386_plt plts[] =
6319     {
6320       { ".plt", NULL, NULL, plt_unknown, 0, 0, 0 },
6321       { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0 },
6322       { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0 },
6323       { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0 }
6324     };
6325
6326   *ret = NULL;
6327
6328   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
6329     return 0;
6330
6331   if (dynsymcount <= 0)
6332     return 0;
6333
6334   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
6335   if (relsize <= 0)
6336     return -1;
6337
6338   dynrelbuf = (arelent **) bfd_malloc (relsize);
6339   if (dynrelbuf == NULL)
6340     return -1;
6341
6342   dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
6343                                                 dynsyms);
6344
6345   /* Sort the relocs by address.  */
6346   qsort (dynrelbuf, dynrelcount, sizeof (arelent *), compare_relocs);
6347
6348   non_lazy_plt = NULL;
6349   /* Silence GCC 6.  */
6350   lazy_plt = NULL;
6351   non_lazy_ibt_plt = NULL;
6352   lazy_ibt_plt = NULL;
6353   switch (get_elf_i386_backend_data (abfd)->os)
6354     {
6355     case is_normal:
6356       non_lazy_plt = &elf_i386_non_lazy_plt;
6357       lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
6358       non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
6359       /* Fall through */
6360     case is_vxworks:
6361       lazy_plt = &elf_i386_lazy_plt;
6362       break;
6363     case is_nacl:
6364       lazy_plt = &elf_i386_nacl_plt;
6365       break;
6366     }
6367
6368   got_addr = 0;
6369
6370   count = 0;
6371   for (j = 0; plts[j].name != NULL; j++)
6372     {
6373       plt = bfd_get_section_by_name (abfd, plts[j].name);
6374       if (plt == NULL)
6375         continue;
6376
6377       /* Get the PLT section contents.  */
6378       plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6379       if (plt_contents == NULL)
6380         break;
6381       if (!bfd_get_section_contents (abfd, (asection *) plt,
6382                                      plt_contents, 0, plt->size))
6383         {
6384           free (plt_contents);
6385           break;
6386         }
6387
6388       /* Check what kind of PLT it is.  */
6389       plt_type = plt_unknown;
6390       if (plts[j].type == plt_unknown)
6391         {
6392           /* Match lazy PLT first.  */
6393           if (memcmp (plt_contents, lazy_plt->plt0_entry,
6394                       lazy_plt->plt0_got1_offset) == 0)
6395             {
6396               /* The fist entry in the lazy IBT PLT is the same as the
6397                  normal lazy PLT.  */
6398               if (lazy_ibt_plt != NULL
6399                   && (memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
6400                               lazy_ibt_plt->plt_entry,
6401                               lazy_ibt_plt->plt_got_offset) == 0))
6402                 plt_type = plt_lazy | plt_second;
6403               else
6404                 plt_type = plt_lazy;
6405             }
6406           else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
6407                            lazy_plt->plt0_got1_offset) == 0)
6408             {
6409               /* The fist entry in the PIC lazy IBT PLT is the same as
6410                  the normal PIC lazy PLT.  */
6411               if (lazy_ibt_plt != NULL
6412                   && (memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
6413                               lazy_ibt_plt->pic_plt_entry,
6414                               lazy_ibt_plt->plt_got_offset) == 0))
6415                 plt_type = plt_lazy | plt_pic | plt_second;
6416               else
6417                 plt_type = plt_lazy | plt_pic;
6418             }
6419         }
6420
6421       if (non_lazy_plt != NULL
6422           && (plt_type == plt_unknown || plt_type == plt_non_lazy))
6423         {
6424           /* Match non-lazy PLT.  */
6425           if (memcmp (plt_contents, non_lazy_plt->plt_entry,
6426                       non_lazy_plt->plt_got_offset) == 0)
6427             plt_type = plt_non_lazy;
6428           else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
6429                            non_lazy_plt->plt_got_offset) == 0)
6430             plt_type = plt_pic;
6431         }
6432
6433       if ((non_lazy_ibt_plt != NULL)
6434           && (plt_type == plt_unknown || plt_type == plt_second))
6435         {
6436           if (memcmp (plt_contents,
6437                       non_lazy_ibt_plt->plt_entry,
6438                       non_lazy_ibt_plt->plt_got_offset) == 0)
6439             {
6440               /* Match IBT PLT.  */
6441               plt_type = plt_second;
6442               non_lazy_plt = non_lazy_ibt_plt;
6443             }
6444           else if (memcmp (plt_contents,
6445                            non_lazy_ibt_plt->pic_plt_entry,
6446                            non_lazy_ibt_plt->plt_got_offset) == 0)
6447             {
6448               /* Match PIC IBT PLT.  */
6449               plt_type = plt_second | plt_pic;
6450               non_lazy_plt = non_lazy_ibt_plt;
6451             }
6452         }
6453
6454       if (plt_type == plt_unknown)
6455         continue;
6456
6457       plts[j].sec = plt;
6458       plts[j].type = plt_type;
6459
6460       if ((plt_type & plt_lazy))
6461         {
6462           plts[j].plt_got_offset = lazy_plt->plt_got_offset;
6463           plts[j].plt_entry_size = lazy_plt->plt_entry_size;
6464           /* Skip PLT0 in lazy PLT.  */
6465           i = 1;
6466         }
6467       else
6468         {
6469           plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
6470           plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
6471           i = 0;
6472         }
6473
6474       /* Skip lazy PLT when the second PLT is used.  */
6475       if ((plt_type & (plt_lazy | plt_second))
6476           == (plt_lazy | plt_second))
6477         plts[j].count = 0;
6478       else
6479         {
6480           n = plt->size / plts[j].plt_entry_size;
6481           plts[j].count = n;
6482           count += n - i;
6483         }
6484
6485       plts[j].contents = plt_contents;
6486
6487       /* The _GLOBAL_OFFSET_TABLE_ address is needed.  */
6488       if ((plt_type & plt_pic))
6489         got_addr = (bfd_vma) -1;
6490     }
6491
6492   size = count * sizeof (asymbol);
6493   s = *ret = (asymbol *) bfd_zmalloc (size);
6494   if (s == NULL)
6495     {
6496 bad_return:
6497       for (j = 0; plts[j].name != NULL; j++)
6498         if (plts[j].contents != NULL)
6499           free (plts[j].contents);
6500       free (dynrelbuf);
6501       return -1;
6502     }
6503
6504   if (got_addr)
6505     {
6506       /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
6507          address.  */
6508       asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
6509       if (sec != NULL)
6510         got_addr = sec->vma;
6511       else
6512         {
6513           sec = bfd_get_section_by_name (abfd, ".got");
6514           if (sec != NULL)
6515             got_addr = sec->vma;
6516         }
6517
6518       if (got_addr == (bfd_vma) -1)
6519         goto bad_return;
6520     }
6521
6522   /* Check for each PLT section.  */
6523   size = 0;
6524   n = 0;
6525   for (j = 0; plts[j].name != NULL; j++)
6526     if ((plt_contents = plts[j].contents) != NULL)
6527       {
6528         long k;
6529         bfd_vma offset;
6530
6531         plt_got_offset = plts[j].plt_got_offset;
6532         plt_entry_size = plts[j].plt_entry_size;
6533
6534         plt = plts[j].sec;
6535
6536         if ((plts[j].type & plt_lazy))
6537           {
6538             /* Skip PLT0 in lazy PLT.  */
6539             k = 1;
6540             offset = plt_entry_size;
6541           }
6542         else
6543           {
6544             k = 0;
6545             offset = 0;
6546           }
6547
6548         /* Check each PLT entry against dynamic relocations.  */
6549         for (; k < plts[j].count; k++)
6550           {
6551             int off;
6552             bfd_vma got_vma;
6553             long min, max, mid;
6554             arelent *p;
6555
6556             /* Get the GOT offset, a signed 32-bit integer.  */
6557             off = H_GET_32 (abfd, (plt_contents + offset
6558                                    + plt_got_offset));
6559             got_vma = got_addr + off;
6560
6561             /* Binary search.  */
6562             p = dynrelbuf[0];
6563             min = 0;
6564             max = dynrelcount;
6565             while ((min + 1) < max)
6566               {
6567                 arelent *r;
6568
6569                 mid = (min + max) / 2;
6570                 r = dynrelbuf[mid];
6571                 if (got_vma > r->address)
6572                   min = mid;
6573                 else if (got_vma < r->address)
6574                   max = mid;
6575                 else
6576                   {
6577                     p = r;
6578                     break;
6579                   }
6580               }
6581
6582             /* Skip unknown relocation.  PR 17512: file: bc9d6cf5.  */
6583             if (got_vma == p->address
6584                 && p->howto != NULL
6585                 && (p->howto->type == R_386_JUMP_SLOT
6586                     || p->howto->type == R_386_GLOB_DAT
6587                     || p->howto->type == R_386_IRELATIVE))
6588               {
6589                 *s = **p->sym_ptr_ptr;
6590                 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
6591                    set.  Since we are defining a symbol, ensure one
6592                    of them is set.  */
6593                 if ((s->flags & BSF_LOCAL) == 0)
6594                   s->flags |= BSF_GLOBAL;
6595                 s->flags |= BSF_SYNTHETIC;
6596                 /* This is no longer a section symbol.  */
6597                 s->flags &= ~BSF_SECTION_SYM;
6598                 s->section = plt;
6599                 s->the_bfd = plt->owner;
6600                 s->value = offset;
6601                 /* Store relocation for later use.  */
6602                 s->udata.p = p;
6603                 /* Add @plt to function name later.  */
6604                 size += strlen (s->name) + sizeof ("@plt");
6605                 if (p->addend != 0)
6606                   size += sizeof ("+0x") - 1 + 8;
6607                 n++;
6608                 s++;
6609               }
6610             offset += plt_entry_size;
6611           }
6612       }
6613
6614   /* PLT entries with R_386_TLS_DESC relocations are skipped.  */
6615   if (n == 0)
6616     goto bad_return;
6617
6618   count = n;
6619
6620   /* Allocate space for @plt suffixes.  */
6621   names = (char *) bfd_malloc (size);
6622   if (s == NULL)
6623     goto bad_return;
6624
6625   s = *ret;
6626   for (i = 0; i < count; i++)
6627     {
6628       /* Add @plt to function name.  */
6629       arelent *p = (arelent *) s->udata.p;
6630       /* Clear it now.  */
6631       s->udata.p = NULL;
6632       size = strlen (s->name);
6633       memcpy (names, s->name, size);
6634       s->name = names;
6635       names += size;
6636       if (p->addend != 0)
6637         {
6638           char buf[30], *a;
6639
6640           memcpy (names, "+0x", sizeof ("+0x") - 1);
6641           names += sizeof ("+0x") - 1;
6642           bfd_sprintf_vma (abfd, buf, p->addend);
6643           for (a = buf; *a == '0'; ++a)
6644             ;
6645           size = strlen (a);
6646           memcpy (names, a, size);
6647           names += size;
6648         }
6649       memcpy (names, "@plt", sizeof ("@plt"));
6650       names += sizeof ("@plt");
6651       s++;
6652     }
6653
6654   for (j = 0; plts[j].name != NULL; j++)
6655     if (plts[j].contents != NULL)
6656       free (plts[j].contents);
6657
6658   free (dynrelbuf);
6659
6660   return count;
6661 }
6662
6663 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
6664
6665 static bfd_boolean
6666 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
6667 {
6668   if (h->plt.offset != (bfd_vma) -1
6669       && !h->def_regular
6670       && !h->pointer_equality_needed)
6671     return FALSE;
6672
6673   return _bfd_elf_hash_symbol (h);
6674 }
6675
6676 /* Parse i386 GNU properties.  */
6677
6678 static enum elf_property_kind
6679 elf_i386_parse_gnu_properties (bfd *abfd, unsigned int type,
6680                                bfd_byte *ptr, unsigned int datasz)
6681 {
6682   elf_property *prop;
6683
6684   switch (type)
6685     {
6686     case GNU_PROPERTY_X86_ISA_1_USED:
6687     case GNU_PROPERTY_X86_ISA_1_NEEDED:
6688     case GNU_PROPERTY_X86_FEATURE_1_AND:
6689       if (datasz != 4)
6690         {
6691           _bfd_error_handler
6692             ((type == GNU_PROPERTY_X86_ISA_1_USED
6693               ? _("error: %B: <corrupt x86 ISA used size: 0x%x>")
6694               : (type == GNU_PROPERTY_X86_ISA_1_NEEDED
6695                  ? _("error: %B: <corrupt x86 ISA needed size: 0x%x>")
6696                  : _("error: %B: <corrupt x86 feature size: 0x%x>"))),
6697              abfd, datasz);
6698           return property_corrupt;
6699         }
6700       prop = _bfd_elf_get_property (abfd, type, datasz);
6701       /* Combine properties of the same type.  */
6702       prop->u.number |= bfd_h_get_32 (abfd, ptr);
6703       prop->pr_kind = property_number;
6704       break;
6705
6706     default:
6707       return property_ignored;
6708     }
6709
6710   return property_number;
6711 }
6712
6713 /* Merge i386 GNU property BPROP with APROP.  If APROP isn't NULL,
6714    return TRUE if APROP is updated.  Otherwise, return TRUE if BPROP
6715    should be merged with ABFD.  */
6716
6717 static bfd_boolean
6718 elf_i386_merge_gnu_properties (struct bfd_link_info *info,
6719                                bfd *abfd ATTRIBUTE_UNUSED,
6720                                elf_property *aprop,
6721                                elf_property *bprop)
6722 {
6723   unsigned int number, features;
6724   bfd_boolean updated = FALSE;
6725   unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
6726
6727   switch (pr_type)
6728     {
6729     case GNU_PROPERTY_X86_ISA_1_USED:
6730     case GNU_PROPERTY_X86_ISA_1_NEEDED:
6731       if (aprop != NULL && bprop != NULL)
6732         {
6733           number = aprop->u.number;
6734           aprop->u.number = number | bprop->u.number;
6735           updated = number != (unsigned int) aprop->u.number;
6736         }
6737       else
6738         {
6739           /* Return TRUE if APROP is NULL to indicate that BPROP should
6740              be added to ABFD.  */
6741           updated = aprop == NULL;
6742         }
6743       break;
6744
6745     case GNU_PROPERTY_X86_FEATURE_1_AND:
6746       /* Only one of APROP and BPROP can be NULL:
6747          1. APROP & BPROP when both APROP and BPROP aren't NULL.
6748          2. If APROP is NULL, remove x86 feature.
6749          3. Otherwise, do nothing.
6750        */
6751       if (aprop != NULL && bprop != NULL)
6752         {
6753           features = 0;
6754           if (info->ibt)
6755             features = GNU_PROPERTY_X86_FEATURE_1_IBT;
6756           if (info->shstk)
6757             features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
6758           number = aprop->u.number;
6759           /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
6760              GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
6761           aprop->u.number = (number & bprop->u.number) | features;
6762           updated = number != (unsigned int) aprop->u.number;
6763           /* Remove the property if all feature bits are cleared.  */
6764           if (aprop->u.number == 0)
6765             aprop->pr_kind = property_remove;
6766         }
6767       else
6768         {
6769           features = 0;
6770           if (info->ibt)
6771             features = GNU_PROPERTY_X86_FEATURE_1_IBT;
6772           if (info->shstk)
6773             features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
6774           if (features)
6775             {
6776               /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
6777                  GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
6778               if (aprop != NULL)
6779                 {
6780                   number = aprop->u.number;
6781                   aprop->u.number = number | features;
6782                   updated = number != (unsigned int) aprop->u.number;
6783                 }
6784               else
6785                 {
6786                   bprop->u.number |= features;
6787                   updated = TRUE;
6788                 }
6789             }
6790           else if (aprop != NULL)
6791             {
6792               aprop->pr_kind = property_remove;
6793               updated = TRUE;
6794             }
6795         }
6796       break;
6797
6798     default:
6799       /* Never should happen.  */
6800       abort ();
6801     }
6802
6803   return updated;
6804 }
6805
6806 /* Set up i386 GNU properties.  Return the first relocatable ELF input
6807    with GNU properties if found.  Otherwise, return NULL.  */
6808
6809 static bfd *
6810 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
6811 {
6812   bfd_boolean normal_target;
6813   bfd_boolean lazy_plt;
6814   asection *sec, *pltsec;
6815   bfd *dynobj;
6816   bfd_boolean use_ibt_plt;
6817   unsigned int plt_alignment, features;
6818   struct elf_i386_link_hash_table *htab;
6819   bfd *pbfd;
6820
6821   features = 0;
6822   if (info->ibt)
6823     features = GNU_PROPERTY_X86_FEATURE_1_IBT;
6824   if (info->shstk)
6825     features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
6826   if (features)
6827     {
6828       /* Turn on GNU_PROPERTY_X86_FEATURE_1_IBT and
6829          GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
6830       bfd *ebfd = NULL;
6831       elf_property *prop;
6832
6833       for (pbfd = info->input_bfds;
6834            pbfd != NULL;
6835            pbfd = pbfd->link.next)
6836         if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
6837             && bfd_count_sections (pbfd) != 0)
6838           {
6839             ebfd = pbfd;
6840
6841             if (elf_properties (pbfd) != NULL)
6842               {
6843                 /* Find a normal input file with GNU property note.  */
6844                 prop = _bfd_elf_get_property (pbfd,
6845                                               GNU_PROPERTY_X86_FEATURE_1_AND,
6846                                               4);
6847                 /* Add GNU_PROPERTY_X86_FEATURE_1_IBT and
6848                    GNU_PROPERTY_X86_FEATURE_1_SHSTK.  */
6849                 prop->u.number |= features;
6850                 prop->pr_kind = property_number;
6851                 break;
6852               }
6853           }
6854
6855       if (pbfd == NULL && ebfd != NULL)
6856         {
6857           /* Create GNU_PROPERTY_X86_FEATURE_1_IBT if needed.  */
6858           prop = _bfd_elf_get_property (ebfd,
6859                                         GNU_PROPERTY_X86_FEATURE_1_AND,
6860                                         4);
6861           prop->u.number = features;
6862           prop->pr_kind = property_number;
6863
6864           sec = bfd_make_section_with_flags (ebfd,
6865                                              NOTE_GNU_PROPERTY_SECTION_NAME,
6866                                              (SEC_ALLOC
6867                                               | SEC_LOAD
6868                                               | SEC_IN_MEMORY
6869                                               | SEC_READONLY
6870                                               | SEC_HAS_CONTENTS
6871                                               | SEC_DATA));
6872           if (sec == NULL)
6873             info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
6874
6875           if (!bfd_set_section_alignment (ebfd, sec, 2))
6876             goto error_alignment;
6877
6878           elf_section_type (sec) = SHT_NOTE;
6879         }
6880     }
6881
6882   pbfd = _bfd_elf_link_setup_gnu_properties (info);
6883
6884   if (bfd_link_relocatable (info))
6885     return pbfd;
6886
6887   htab = elf_i386_hash_table (info);
6888   if (htab == NULL)
6889     return pbfd;
6890
6891   use_ibt_plt = info->ibtplt || info->ibt;
6892   if (!use_ibt_plt && pbfd != NULL)
6893     {
6894       /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on.  */
6895       elf_property_list *p;
6896
6897       /* The property list is sorted in order of type.  */
6898       for (p = elf_properties (pbfd); p; p = p->next)
6899         {
6900           if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
6901             {
6902               use_ibt_plt = !!(p->property.u.number
6903                                & GNU_PROPERTY_X86_FEATURE_1_IBT);
6904               break;
6905             }
6906           else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
6907             break;
6908         }
6909     }
6910
6911   dynobj = htab->elf.dynobj;
6912
6913   /* Set htab->elf.dynobj here so that there is no need to check and
6914      set it in check_relocs.  */
6915   if (dynobj == NULL)
6916     {
6917       bfd *abfd;
6918
6919       /* Find a normal input file to hold linker created
6920          sections.  */
6921       for (abfd = info->input_bfds;
6922            abfd != NULL;
6923            abfd = abfd->link.next)
6924         if ((abfd->flags
6925              & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6926           {
6927             htab->elf.dynobj = abfd;
6928             dynobj = abfd;
6929             break;
6930           }
6931     }
6932
6933   /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
6934      still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
6935      canonical function address.  */
6936   htab->plt.has_plt0 = 1;
6937   normal_target = FALSE;
6938
6939   switch (get_elf_i386_backend_data (info->output_bfd)->os)
6940     {
6941     case is_normal:
6942       if (use_ibt_plt)
6943         {
6944           htab->lazy_plt = &elf_i386_lazy_ibt_plt;
6945           htab->non_lazy_plt = &elf_i386_non_lazy_ibt_plt;
6946         }
6947       else
6948         {
6949           htab->lazy_plt = &elf_i386_lazy_plt;
6950           htab->non_lazy_plt = &elf_i386_non_lazy_plt;
6951         }
6952       normal_target = TRUE;
6953       break;
6954     case is_vxworks:
6955       htab->lazy_plt = &elf_i386_lazy_plt;
6956       htab->non_lazy_plt = NULL;
6957       if (!elf_vxworks_create_dynamic_sections (dynobj, info,
6958                                                 &htab->srelplt2))
6959         info->callbacks->einfo (_("%F: failed to create VxWorks dynamic sections\n"));
6960       break;
6961     case is_nacl:
6962       htab->lazy_plt = &elf_i386_nacl_plt;
6963       htab->non_lazy_plt = NULL;
6964       break;
6965     }
6966
6967   pltsec = htab->elf.splt;
6968
6969   /* If the non-lazy PLT is available, use it for all PLT entries if
6970      there are no PLT0 or no .plt section.  */
6971   if (htab->non_lazy_plt != NULL
6972       && (!htab->plt.has_plt0 || pltsec == NULL))
6973     {
6974       lazy_plt = FALSE;
6975       if (bfd_link_pic (info))
6976         htab->plt.plt_entry
6977           = htab->non_lazy_plt->pic_plt_entry;
6978       else
6979         htab->plt.plt_entry
6980           = htab->non_lazy_plt->plt_entry;
6981       htab->plt.plt_entry_size
6982         = htab->non_lazy_plt->plt_entry_size;
6983       htab->plt.plt_got_offset
6984         = htab->non_lazy_plt->plt_got_offset;
6985       htab->plt.eh_frame_plt_size
6986         = htab->non_lazy_plt->eh_frame_plt_size;
6987       htab->plt.eh_frame_plt
6988         = htab->non_lazy_plt->eh_frame_plt;
6989     }
6990   else
6991     {
6992       lazy_plt = TRUE;
6993       if (bfd_link_pic (info))
6994         {
6995           htab->plt.plt0_entry
6996             = htab->lazy_plt->pic_plt0_entry;
6997           htab->plt.plt_entry
6998             = htab->lazy_plt->pic_plt_entry;
6999         }
7000       else
7001         {
7002           htab->plt.plt0_entry
7003             = htab->lazy_plt->plt0_entry;
7004           htab->plt.plt_entry
7005             = htab->lazy_plt->plt_entry;
7006         }
7007       htab->plt.plt_entry_size
7008         = htab->lazy_plt->plt_entry_size;
7009       htab->plt.plt_got_offset
7010         = htab->lazy_plt->plt_got_offset;
7011       htab->plt.eh_frame_plt_size
7012         = htab->lazy_plt->eh_frame_plt_size;
7013       htab->plt.eh_frame_plt
7014         = htab->lazy_plt->eh_frame_plt;
7015     }
7016
7017   /* Return if there are no normal input files.  */
7018   if (dynobj == NULL)
7019     return pbfd;
7020
7021   /* Since create_dynamic_sections isn't always called, but GOT
7022      relocations need GOT relocations, create them here so that we
7023      don't need to do it in check_relocs.  */
7024   if (htab->elf.sgot == NULL
7025       && !_bfd_elf_create_got_section (dynobj, info))
7026     info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
7027
7028   /* Create the ifunc sections here so that check_relocs can be
7029      simplified.  */
7030   if (!_bfd_elf_create_ifunc_sections (dynobj, info))
7031     info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
7032
7033   plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
7034
7035   if (pltsec != NULL)
7036     {
7037       /* Whe creating executable, set the contents of the .interp
7038          section to the interpreter.  */
7039       if (bfd_link_executable (info) && !info->nointerp)
7040         {
7041           asection *s = bfd_get_linker_section (dynobj, ".interp");
7042           if (s == NULL)
7043             abort ();
7044           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7045           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7046           htab->interp = s;
7047         }
7048
7049       /* Don't change PLT section alignment for NaCl since it uses
7050          64-byte PLT entry and sets PLT section alignment to 32
7051          bytes.  */
7052       if (normal_target)
7053         {
7054           const struct elf_backend_data *bed
7055             = get_elf_backend_data (dynobj);
7056           flagword pltflags = (bed->dynamic_sec_flags
7057                                | SEC_ALLOC
7058                                | SEC_CODE
7059                                | SEC_LOAD
7060                                | SEC_READONLY);
7061           unsigned int non_lazy_plt_alignment
7062             = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
7063
7064           sec = pltsec;
7065           if (!bfd_set_section_alignment (sec->owner, sec,
7066                                           plt_alignment))
7067             goto error_alignment;
7068
7069           /* Create the GOT procedure linkage table.  */
7070           sec = bfd_make_section_anyway_with_flags (dynobj,
7071                                                     ".plt.got",
7072                                                     pltflags);
7073           if (sec == NULL)
7074             info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
7075
7076           if (!bfd_set_section_alignment (dynobj, sec,
7077                                           non_lazy_plt_alignment))
7078             goto error_alignment;
7079
7080           htab->plt_got = sec;
7081
7082           if (lazy_plt)
7083             {
7084               sec = NULL;
7085
7086               if (use_ibt_plt)
7087                 {
7088                   /* Create the second PLT for Intel IBT support.  IBT
7089                      PLT is supported only for non-NaCl target and is
7090                      is needed only for lazy binding.  */
7091                   sec = bfd_make_section_anyway_with_flags (dynobj,
7092                                                             ".plt.sec",
7093                                                             pltflags);
7094                   if (sec == NULL)
7095                     info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
7096
7097                   if (!bfd_set_section_alignment (dynobj, sec,
7098                                                   plt_alignment))
7099                     goto error_alignment;
7100                 }
7101
7102               htab->plt_second = sec;
7103             }
7104         }
7105
7106       if (!info->no_ld_generated_unwind_info)
7107         {
7108           flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
7109                             | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7110                             | SEC_LINKER_CREATED);
7111
7112           sec = bfd_make_section_anyway_with_flags (dynobj,
7113                                                     ".eh_frame",
7114                                                     flags);
7115           if (sec == NULL)
7116             info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
7117
7118           if (!bfd_set_section_alignment (dynobj, sec, 2))
7119             goto error_alignment;
7120
7121           htab->plt_eh_frame = sec;
7122
7123           if (htab->plt_got != NULL)
7124             {
7125               sec = bfd_make_section_anyway_with_flags (dynobj,
7126                                                         ".eh_frame",
7127                                                         flags);
7128               if (sec == NULL)
7129                 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
7130
7131               if (!bfd_set_section_alignment (dynobj, sec, 2))
7132                 goto error_alignment;
7133
7134               htab->plt_got_eh_frame = sec;
7135             }
7136         }
7137     }
7138
7139   if (normal_target)
7140     {
7141       /* The .iplt section is used for IFUNC symbols in static
7142          executables.  */
7143       sec = htab->elf.iplt;
7144       if (sec != NULL
7145           && !bfd_set_section_alignment (sec->owner, sec,
7146                                          plt_alignment))
7147         {
7148 error_alignment:
7149           info->callbacks->einfo (_("%F%A: failed to align section\n"),
7150                                   sec);
7151         }
7152     }
7153
7154   return pbfd;
7155 }
7156
7157 #define TARGET_LITTLE_SYM               i386_elf32_vec
7158 #define TARGET_LITTLE_NAME              "elf32-i386"
7159 #define ELF_ARCH                        bfd_arch_i386
7160 #define ELF_TARGET_ID                   I386_ELF_DATA
7161 #define ELF_MACHINE_CODE                EM_386
7162 #define ELF_MAXPAGESIZE                 0x1000
7163
7164 #define elf_backend_can_gc_sections     1
7165 #define elf_backend_can_refcount        1
7166 #define elf_backend_want_got_plt        1
7167 #define elf_backend_plt_readonly        1
7168 #define elf_backend_want_plt_sym        0
7169 #define elf_backend_got_header_size     12
7170 #define elf_backend_plt_alignment       4
7171 #define elf_backend_dtrel_excludes_plt  1
7172 #define elf_backend_extern_protected_data 1
7173 #define elf_backend_caches_rawsize      1
7174 #define elf_backend_want_dynrelro       1
7175
7176 /* Support RELA for objdump of prelink objects.  */
7177 #define elf_info_to_howto                     elf_i386_info_to_howto_rel
7178 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
7179
7180 #define bfd_elf32_mkobject                    elf_i386_mkobject
7181
7182 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
7183 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
7184 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
7185 #define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
7186 #define bfd_elf32_get_synthetic_symtab        elf_i386_get_synthetic_symtab
7187
7188 #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
7189 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
7190 #define elf_backend_check_relocs              elf_i386_check_relocs
7191 #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
7192 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
7193 #define elf_backend_fake_sections             elf_i386_fake_sections
7194 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
7195 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
7196 #define elf_backend_output_arch_local_syms     elf_i386_output_arch_local_syms
7197 #define elf_backend_gc_mark_hook              elf_i386_gc_mark_hook
7198 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
7199 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
7200 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
7201 #define elf_backend_relocate_section          elf_i386_relocate_section
7202 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
7203 #define elf_backend_always_size_sections      elf_i386_always_size_sections
7204 #define elf_backend_omit_section_dynsym \
7205   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
7206 #define elf_backend_hash_symbol               elf_i386_hash_symbol
7207 #define elf_backend_fixup_symbol              elf_i386_fixup_symbol
7208 #define elf_backend_parse_gnu_properties      elf_i386_parse_gnu_properties
7209 #define elf_backend_merge_gnu_properties      elf_i386_merge_gnu_properties
7210 #define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
7211
7212 #include "elf32-target.h"
7213
7214 /* FreeBSD support.  */
7215
7216 #undef  TARGET_LITTLE_SYM
7217 #define TARGET_LITTLE_SYM               i386_elf32_fbsd_vec
7218 #undef  TARGET_LITTLE_NAME
7219 #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
7220 #undef  ELF_OSABI
7221 #define ELF_OSABI                       ELFOSABI_FREEBSD
7222
7223 /* The kernel recognizes executables as valid only if they carry a
7224    "FreeBSD" label in the ELF header.  So we put this label on all
7225    executables and (for simplicity) also all other object files.  */
7226
7227 static void
7228 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
7229 {
7230   _bfd_elf_post_process_headers (abfd, info);
7231
7232 #ifdef OLD_FREEBSD_ABI_LABEL
7233   {
7234     /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
7235     Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
7236     memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
7237   }
7238 #endif
7239 }
7240
7241 #undef  elf_backend_post_process_headers
7242 #define elf_backend_post_process_headers        elf_i386_fbsd_post_process_headers
7243 #undef  elf32_bed
7244 #define elf32_bed                               elf32_i386_fbsd_bed
7245
7246 #undef elf_backend_add_symbol_hook
7247
7248 #include "elf32-target.h"
7249
7250 /* Solaris 2.  */
7251
7252 #undef  TARGET_LITTLE_SYM
7253 #define TARGET_LITTLE_SYM               i386_elf32_sol2_vec
7254 #undef  TARGET_LITTLE_NAME
7255 #define TARGET_LITTLE_NAME              "elf32-i386-sol2"
7256
7257 #undef elf_backend_post_process_headers
7258
7259 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
7260    objects won't be recognized.  */
7261 #undef ELF_OSABI
7262
7263 #undef  elf32_bed
7264 #define elf32_bed                       elf32_i386_sol2_bed
7265
7266 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
7267    boundary.  */
7268 #undef  elf_backend_static_tls_alignment
7269 #define elf_backend_static_tls_alignment 8
7270
7271 /* The Solaris 2 ABI requires a plt symbol on all platforms.
7272
7273    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
7274    File, p.63.  */
7275 #undef  elf_backend_want_plt_sym
7276 #define elf_backend_want_plt_sym        1
7277
7278 #undef  elf_backend_strtab_flags
7279 #define elf_backend_strtab_flags        SHF_STRINGS
7280
7281 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
7282    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised 
7283    FALSE otherwise.  ISECTION is the best guess matching section from the
7284    input bfd IBFD, but it might be NULL.  */
7285
7286 static bfd_boolean
7287 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
7288                                                 bfd *obfd ATTRIBUTE_UNUSED,
7289                                                 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
7290                                                 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
7291 {
7292   /* PR 19938: FIXME: Need to add code for setting the sh_info
7293      and sh_link fields of Solaris specific section types.  */
7294   return FALSE;
7295
7296   /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
7297      Object File Format, Table 13-9  ELF sh_link and sh_info Interpretation:
7298
7299 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
7300
7301      The following values should be set:
7302      
7303 Type                 Link                           Info
7304 -----------------------------------------------------------------------------
7305 SHT_SUNW_ancillary   The section header index of    0
7306  [0x6fffffee]        the associated string table.
7307         
7308 SHT_SUNW_capinfo     The section header index of    For a dynamic object, the
7309  [0x6ffffff0]        the associated symbol table.   section header index of
7310                                                     the associated
7311                                                     SHT_SUNW_capchain table,
7312                                                     otherwise 0.
7313
7314 SHT_SUNW_symsort     The section header index of    0
7315  [0x6ffffff1]        the associated symbol table.
7316
7317 SHT_SUNW_tlssort     The section header index of    0
7318  [0x6ffffff2]        the associated symbol table.
7319         
7320 SHT_SUNW_LDYNSYM     The section header index of    One greater than the 
7321  [0x6ffffff3]        the associated string table.   symbol table index of the
7322                      This index is the same string  last local symbol, 
7323                      table used by the SHT_DYNSYM   STB_LOCAL. Since
7324                      section.                       SHT_SUNW_LDYNSYM only
7325                                                     contains local symbols,
7326                                                     sh_info is equivalent to
7327                                                     the number of symbols in
7328                                                     the table.
7329
7330 SHT_SUNW_cap         If symbol capabilities exist,  If any capabilities refer
7331  [0x6ffffff5]        the section header index of    to named strings, the
7332                      the associated                 section header index of
7333                      SHT_SUNW_capinfo table,        the associated string 
7334                           otherwise 0.              table, otherwise 0.
7335
7336 SHT_SUNW_move        The section header index of    0
7337  [0x6ffffffa]        the associated symbol table.
7338         
7339 SHT_SUNW_COMDAT      0                              0
7340  [0x6ffffffb]
7341
7342 SHT_SUNW_syminfo     The section header index of    The section header index
7343  [0x6ffffffc]        the associated symbol table.   of the associated
7344                                                     .dynamic section.
7345
7346 SHT_SUNW_verdef      The section header index of    The number of version 
7347  [0x6ffffffd]        the associated string table.   definitions within the
7348                                                     section.
7349
7350 SHT_SUNW_verneed     The section header index of    The number of version
7351  [0x6ffffffe]        the associated string table.   dependencies within the
7352                                                     section.
7353
7354 SHT_SUNW_versym      The section header index of    0
7355  [0x6fffffff]        the associated symbol table.  */
7356 }
7357
7358 #undef  elf_backend_copy_special_section_fields
7359 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
7360
7361 #include "elf32-target.h"
7362
7363 /* Intel MCU support.  */
7364
7365 static bfd_boolean
7366 elf32_iamcu_elf_object_p (bfd *abfd)
7367 {
7368   /* Set the right machine number for an IAMCU elf32 file.  */
7369   bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
7370   return TRUE;
7371 }
7372
7373 #undef  TARGET_LITTLE_SYM
7374 #define TARGET_LITTLE_SYM               iamcu_elf32_vec
7375 #undef  TARGET_LITTLE_NAME
7376 #define TARGET_LITTLE_NAME              "elf32-iamcu"
7377 #undef  ELF_ARCH
7378 #define ELF_ARCH                        bfd_arch_iamcu
7379
7380 #undef  ELF_MACHINE_CODE
7381 #define ELF_MACHINE_CODE                EM_IAMCU
7382
7383 #undef  ELF_OSABI
7384
7385 #undef  elf32_bed
7386 #define elf32_bed                       elf32_iamcu_bed
7387
7388 #undef  elf_backend_object_p
7389 #define elf_backend_object_p            elf32_iamcu_elf_object_p
7390
7391 #undef  elf_backend_static_tls_alignment
7392
7393 #undef  elf_backend_want_plt_sym
7394 #define elf_backend_want_plt_sym            0
7395
7396 #undef  elf_backend_strtab_flags
7397 #undef  elf_backend_copy_special_section_fields
7398
7399 #include "elf32-target.h"
7400
7401 /* Restore defaults.  */
7402 #undef  ELF_ARCH
7403 #define ELF_ARCH                        bfd_arch_i386
7404 #undef  ELF_MACHINE_CODE
7405 #define ELF_MACHINE_CODE                EM_386
7406
7407 /* Native Client support.  */
7408
7409 #undef  TARGET_LITTLE_SYM
7410 #define TARGET_LITTLE_SYM               i386_elf32_nacl_vec
7411 #undef  TARGET_LITTLE_NAME
7412 #define TARGET_LITTLE_NAME              "elf32-i386-nacl"
7413 #undef  elf32_bed
7414 #define elf32_bed                       elf32_i386_nacl_bed
7415
7416 #undef  ELF_MAXPAGESIZE
7417 #define ELF_MAXPAGESIZE                 0x10000
7418
7419 /* Restore defaults.  */
7420 #undef  ELF_OSABI
7421 #undef  elf_backend_want_plt_sym
7422 #define elf_backend_want_plt_sym        0
7423 #undef  elf_backend_post_process_headers
7424 #undef  elf_backend_static_tls_alignment
7425
7426 /* NaCl uses substantially different PLT entries for the same effects.  */
7427
7428 #undef  elf_backend_plt_alignment
7429 #define elf_backend_plt_alignment       5
7430 #define NACL_PLT_ENTRY_SIZE             64
7431 #define NACLMASK                        0xe0 /* 32-byte alignment mask.  */
7432
7433 static const bfd_byte elf_i386_nacl_plt0_entry[] =
7434   {
7435     0xff, 0x35,                   /* pushl contents of address */
7436     0, 0, 0, 0,                   /* replaced with address of .got + 4.  */
7437     0x8b, 0x0d,                   /* movl contents of address, %ecx */
7438     0, 0, 0, 0,                   /* replaced with address of .got + 8.  */
7439     0x83, 0xe1, NACLMASK,         /* andl $NACLMASK, %ecx */
7440     0xff, 0xe1                    /* jmp *%ecx */
7441   };
7442
7443 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
7444   {
7445     0x8b, 0x0d,                         /* movl contents of address, %ecx */
7446     0, 0, 0, 0,                         /* replaced with GOT slot address.  */
7447     0x83, 0xe1, NACLMASK,               /* andl $NACLMASK, %ecx */
7448     0xff, 0xe1,                         /* jmp *%ecx */
7449
7450     /* Pad to the next 32-byte boundary with nop instructions.  */
7451     0x90,
7452     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7453     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7454
7455     /* Lazy GOT entries point here (32-byte aligned).  */
7456     0x68,                              /* pushl immediate */
7457     0, 0, 0, 0,                        /* replaced with reloc offset.  */
7458     0xe9,                              /* jmp relative */
7459     0, 0, 0, 0,                        /* replaced with offset to .plt.  */
7460
7461     /* Pad to the next 32-byte boundary with nop instructions.  */
7462     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7463     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7464     0x90, 0x90
7465   };
7466
7467 static const bfd_byte
7468 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
7469   {
7470     0xff, 0x73, 0x04,           /* pushl 4(%ebx) */
7471     0x8b, 0x4b, 0x08,           /* mov 0x8(%ebx), %ecx */
7472     0x83, 0xe1, 0xe0,           /* and $NACLMASK, %ecx */
7473     0xff, 0xe1,                 /* jmp *%ecx */
7474
7475     /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
7476        so pad to that size with nop instructions.  */
7477     0x90, 0x90, 0x90, 0x90, 0x90, 0x90
7478   };
7479
7480 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
7481   {
7482     0x8b, 0x8b,          /* movl offset(%ebx), %ecx */
7483     0, 0, 0, 0,          /* replaced with offset of this symbol in .got.  */
7484     0x83, 0xe1, 0xe0,    /* andl $NACLMASK, %ecx */
7485     0xff, 0xe1,          /* jmp *%ecx */
7486
7487     /* Pad to the next 32-byte boundary with nop instructions.  */
7488     0x90,
7489     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7490     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7491
7492     /* Lazy GOT entries point here (32-byte aligned).  */
7493     0x68,                /* pushl immediate */
7494     0, 0, 0, 0,          /* replaced with offset into relocation table.  */
7495     0xe9,                /* jmp relative */
7496     0, 0, 0, 0,          /* replaced with offset to start of .plt.  */
7497
7498     /* Pad to the next 32-byte boundary with nop instructions.  */
7499     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7500     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
7501     0x90, 0x90
7502   };
7503
7504 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
7505   {
7506 #if (PLT_CIE_LENGTH != 20                               \
7507      || PLT_FDE_LENGTH != 36                            \
7508      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8  \
7509      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
7510 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
7511 #endif
7512     PLT_CIE_LENGTH, 0, 0, 0,            /* CIE length */
7513     0, 0, 0, 0,                         /* CIE ID */
7514     1,                                  /* CIE version */
7515     'z', 'R', 0,                        /* Augmentation string */
7516     1,                                  /* Code alignment factor */
7517     0x7c,                               /* Data alignment factor: -4 */
7518     8,                                  /* Return address column */
7519     1,                                  /* Augmentation size */
7520     DW_EH_PE_pcrel | DW_EH_PE_sdata4,   /* FDE encoding */
7521     DW_CFA_def_cfa, 4, 4,               /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
7522     DW_CFA_offset + 8, 1,               /* DW_CFA_offset: r8 (eip) at cfa-4 */
7523     DW_CFA_nop, DW_CFA_nop,
7524
7525     PLT_FDE_LENGTH, 0, 0, 0,     /* FDE length */
7526     PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
7527     0, 0, 0, 0,                  /* R_386_PC32 .plt goes here */
7528     0, 0, 0, 0,                  /* .plt size goes here */
7529     0,                           /* Augmentation size */
7530     DW_CFA_def_cfa_offset, 8,    /* DW_CFA_def_cfa_offset: 8 */
7531     DW_CFA_advance_loc + 6,      /* DW_CFA_advance_loc: 6 to __PLT__+6 */
7532     DW_CFA_def_cfa_offset, 12,   /* DW_CFA_def_cfa_offset: 12 */
7533     DW_CFA_advance_loc + 58,     /* DW_CFA_advance_loc: 58 to __PLT__+64 */
7534     DW_CFA_def_cfa_expression,   /* DW_CFA_def_cfa_expression */
7535     13,                          /* Block length */
7536     DW_OP_breg4, 4,              /* DW_OP_breg4 (esp): 4 */
7537     DW_OP_breg8, 0,              /* DW_OP_breg8 (eip): 0 */
7538     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
7539     DW_OP_lit2, DW_OP_shl, DW_OP_plus,
7540     DW_CFA_nop, DW_CFA_nop
7541   };
7542
7543 static const struct elf_i386_lazy_plt_layout elf_i386_nacl_plt =
7544   {
7545     elf_i386_nacl_plt0_entry,           /* plt0_entry */
7546     sizeof (elf_i386_nacl_plt0_entry),  /* plt0_entry_size */
7547     2,                                  /* plt0_got1_offset */
7548     8,                                  /* plt0_got2_offset */
7549     elf_i386_nacl_plt_entry,            /* plt_entry */
7550     NACL_PLT_ENTRY_SIZE,                /* plt_entry_size */
7551     2,                                  /* plt_got_offset */
7552     33,                                 /* plt_reloc_offset */
7553     38,                                 /* plt_plt_offset */
7554     32,                                 /* plt_lazy_offset */
7555     elf_i386_nacl_pic_plt0_entry,       /* pic_plt0_entry */
7556     elf_i386_nacl_pic_plt_entry,        /* pic_plt_entry */
7557     elf_i386_nacl_eh_frame_plt,         /* eh_frame_plt */
7558     sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
7559   };
7560
7561 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
7562   {
7563     0x90,                               /* plt0_pad_byte: nop insn */
7564     is_nacl                             /* os */
7565   };
7566
7567 static bfd_boolean
7568 elf32_i386_nacl_elf_object_p (bfd *abfd)
7569 {
7570   /* Set the right machine number for a NaCl i386 ELF32 file.  */
7571   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
7572   return TRUE;
7573 }
7574
7575 #undef  elf_backend_arch_data
7576 #define elf_backend_arch_data   &elf_i386_nacl_arch_bed
7577
7578 #undef  elf_backend_object_p
7579 #define elf_backend_object_p                    elf32_i386_nacl_elf_object_p
7580 #undef  elf_backend_modify_segment_map
7581 #define elf_backend_modify_segment_map          nacl_modify_segment_map
7582 #undef  elf_backend_modify_program_headers
7583 #define elf_backend_modify_program_headers      nacl_modify_program_headers
7584 #undef  elf_backend_final_write_processing
7585 #define elf_backend_final_write_processing      nacl_final_write_processing
7586
7587 #include "elf32-target.h"
7588
7589 /* Restore defaults.  */
7590 #undef  elf_backend_object_p
7591 #undef  elf_backend_modify_segment_map
7592 #undef  elf_backend_modify_program_headers
7593 #undef  elf_backend_final_write_processing
7594
7595 /* VxWorks support.  */
7596
7597 #undef  TARGET_LITTLE_SYM
7598 #define TARGET_LITTLE_SYM               i386_elf32_vxworks_vec
7599 #undef  TARGET_LITTLE_NAME
7600 #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
7601 #undef  ELF_OSABI
7602 #undef  ELF_MAXPAGESIZE
7603 #define ELF_MAXPAGESIZE                 0x1000
7604 #undef  elf_backend_plt_alignment
7605 #define elf_backend_plt_alignment       4
7606
7607 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
7608   {
7609     0x90,                               /* plt0_pad_byte */
7610     is_vxworks                          /* os */
7611   };
7612
7613 #undef  elf_backend_arch_data
7614 #define elf_backend_arch_data   &elf_i386_vxworks_arch_bed
7615
7616 #undef elf_backend_relocs_compatible
7617 #undef elf_backend_add_symbol_hook
7618 #define elf_backend_add_symbol_hook \
7619   elf_vxworks_add_symbol_hook
7620 #undef elf_backend_link_output_symbol_hook
7621 #define elf_backend_link_output_symbol_hook \
7622   elf_vxworks_link_output_symbol_hook
7623 #undef elf_backend_emit_relocs
7624 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
7625 #undef elf_backend_final_write_processing
7626 #define elf_backend_final_write_processing \
7627   elf_vxworks_final_write_processing
7628 #undef elf_backend_static_tls_alignment
7629
7630 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
7631    define it.  */
7632 #undef elf_backend_want_plt_sym
7633 #define elf_backend_want_plt_sym        1
7634
7635 #undef  elf32_bed
7636 #define elf32_bed                               elf32_i386_vxworks_bed
7637
7638 #include "elf32-target.h"