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