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