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