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