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