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