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