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