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