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