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