xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf32-i386.c (revision a8c74629f602faa0ccf8a463757d7baf858bbf3a)
1 /* Intel 80386/80486-specific support for 32-bit ELF
2    Copyright (C) 1993-2018 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 (bfd *abfd ATTRIBUTE_UNUSED, 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 (abfd, 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 (abfd, from_type);
1160       to = elf_i386_rtype_to_howto (abfd, 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 = link_info->call_nop_byte;
1352 		  if (link_info->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 	  if (h->type == STT_GNU_IFUNC)
1585 	    elf_tdata (info->output_bfd)->has_gnu_symbols
1586 	      |= elf_gnu_symbol_ifunc;
1587 	}
1588 
1589       if (r_type == R_386_GOT32X
1590 	  && (h == NULL || h->type != STT_GNU_IFUNC))
1591 	{
1592 	  Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1593 	  if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1594 					    &r_type, irel, h,
1595 					    &converted, info))
1596 	    goto error_return;
1597 	}
1598 
1599       if (! elf_i386_tls_transition (info, abfd, sec, contents,
1600 				     symtab_hdr, sym_hashes,
1601 				     &r_type, GOT_UNKNOWN,
1602 				     rel, rel_end, h, r_symndx, FALSE))
1603 	goto error_return;
1604 
1605       /* Check if _GLOBAL_OFFSET_TABLE_ is referenced.  */
1606       if (h == htab->elf.hgot)
1607 	htab->got_referenced = TRUE;
1608 
1609       switch (r_type)
1610 	{
1611 	case R_386_TLS_LDM:
1612 	  htab->tls_ld_or_ldm_got.refcount = 1;
1613 	  goto create_got;
1614 
1615 	case R_386_PLT32:
1616 	  /* This symbol requires a procedure linkage table entry.  We
1617 	     actually build the entry in adjust_dynamic_symbol,
1618 	     because this might be a case of linking PIC code which is
1619 	     never referenced by a dynamic object, in which case we
1620 	     don't need to generate a procedure linkage table entry
1621 	     after all.  */
1622 
1623 	  /* If this is a local symbol, we resolve it directly without
1624 	     creating a procedure linkage table entry.  */
1625 	  if (h == NULL)
1626 	    continue;
1627 
1628 	  eh->zero_undefweak &= 0x2;
1629 	  h->needs_plt = 1;
1630 	  h->plt.refcount = 1;
1631 	  break;
1632 
1633 	case R_386_SIZE32:
1634 	  size_reloc = TRUE;
1635 	  goto do_size;
1636 
1637 	case R_386_TLS_IE_32:
1638 	case R_386_TLS_IE:
1639 	case R_386_TLS_GOTIE:
1640 	  if (!bfd_link_executable (info))
1641 	    info->flags |= DF_STATIC_TLS;
1642 	  /* Fall through */
1643 
1644 	case R_386_GOT32:
1645 	case R_386_GOT32X:
1646 	case R_386_TLS_GD:
1647 	case R_386_TLS_GOTDESC:
1648 	case R_386_TLS_DESC_CALL:
1649 	  /* This symbol requires a global offset table entry.  */
1650 	  {
1651 	    int tls_type, old_tls_type;
1652 
1653 	    switch (r_type)
1654 	      {
1655 	      default:
1656 	      case R_386_GOT32:
1657 	      case R_386_GOT32X:
1658 		tls_type = GOT_NORMAL;
1659 		break;
1660 	      case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1661 	      case R_386_TLS_GOTDESC:
1662 	      case R_386_TLS_DESC_CALL:
1663 		tls_type = GOT_TLS_GDESC; break;
1664 	      case R_386_TLS_IE_32:
1665 		if (ELF32_R_TYPE (rel->r_info) == r_type)
1666 		  tls_type = GOT_TLS_IE_NEG;
1667 		else
1668 		  /* If this is a GD->IE transition, we may use either of
1669 		     R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
1670 		  tls_type = GOT_TLS_IE;
1671 		break;
1672 	      case R_386_TLS_IE:
1673 	      case R_386_TLS_GOTIE:
1674 		tls_type = GOT_TLS_IE_POS; break;
1675 	      }
1676 
1677 	    if (h != NULL)
1678 	      {
1679 		h->got.refcount = 1;
1680 		old_tls_type = elf_x86_hash_entry (h)->tls_type;
1681 	      }
1682 	    else
1683 	      {
1684 		bfd_signed_vma *local_got_refcounts;
1685 
1686 		/* This is a global offset table entry for a local symbol.  */
1687 		local_got_refcounts = elf_local_got_refcounts (abfd);
1688 		if (local_got_refcounts == NULL)
1689 		  {
1690 		    bfd_size_type size;
1691 
1692 		    size = symtab_hdr->sh_info;
1693 		    size *= (sizeof (bfd_signed_vma)
1694 			     + sizeof (bfd_vma) + sizeof(char));
1695 		    local_got_refcounts = (bfd_signed_vma *)
1696 			bfd_zalloc (abfd, size);
1697 		    if (local_got_refcounts == NULL)
1698 		      goto error_return;
1699 		    elf_local_got_refcounts (abfd) = local_got_refcounts;
1700 		    elf_x86_local_tlsdesc_gotent (abfd)
1701 		      = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1702 		    elf_x86_local_got_tls_type (abfd)
1703 		      = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1704 		  }
1705 		local_got_refcounts[r_symndx] = 1;
1706 		old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1707 	      }
1708 
1709 	    if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1710 	      tls_type |= old_tls_type;
1711 	    /* If a TLS symbol is accessed using IE at least once,
1712 	       there is no point to use dynamic model for it.  */
1713 	    else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1714 		     && (! GOT_TLS_GD_ANY_P (old_tls_type)
1715 			 || (tls_type & GOT_TLS_IE) == 0))
1716 	      {
1717 		if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1718 		  tls_type = old_tls_type;
1719 		else if (GOT_TLS_GD_ANY_P (old_tls_type)
1720 			 && GOT_TLS_GD_ANY_P (tls_type))
1721 		  tls_type |= old_tls_type;
1722 		else
1723 		  {
1724 		    if (h)
1725 		      name = h->root.root.string;
1726 		    else
1727 		      name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1728 					     NULL);
1729 		    _bfd_error_handler
1730 		      /* xgettext:c-format */
1731 		      (_("%pB: `%s' accessed both as normal and "
1732 			 "thread local symbol"),
1733 		       abfd, name);
1734 		    bfd_set_error (bfd_error_bad_value);
1735 		    goto error_return;
1736 		  }
1737 	      }
1738 
1739 	    if (old_tls_type != tls_type)
1740 	      {
1741 		if (h != NULL)
1742 		  elf_x86_hash_entry (h)->tls_type = tls_type;
1743 		else
1744 		  elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1745 	      }
1746 	  }
1747 	  /* Fall through */
1748 
1749 	case R_386_GOTOFF:
1750 	case R_386_GOTPC:
1751 create_got:
1752 	  if (r_type != R_386_TLS_IE)
1753 	    {
1754 	      if (eh != NULL)
1755 		{
1756 		  eh->zero_undefweak &= 0x2;
1757 
1758 		  /* Need GOT to resolve undefined weak symbol to 0.  */
1759 		  if (r_type == R_386_GOTOFF
1760 		      && h->root.type == bfd_link_hash_undefweak
1761 		      && bfd_link_executable (info))
1762 		    htab->got_referenced = TRUE;
1763 		}
1764 	      break;
1765 	    }
1766 	  /* Fall through */
1767 
1768 	case R_386_TLS_LE_32:
1769 	case R_386_TLS_LE:
1770 	  if (eh != NULL)
1771 	    eh->zero_undefweak &= 0x2;
1772 	  if (bfd_link_executable (info))
1773 	    break;
1774 	  info->flags |= DF_STATIC_TLS;
1775 	  goto do_relocation;
1776 
1777 	case R_386_32:
1778 	case R_386_PC32:
1779 	  if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1780 	    eh->zero_undefweak |= 0x2;
1781 do_relocation:
1782 	  /* We are called after all symbols have been resolved.  Only
1783 	     relocation against STT_GNU_IFUNC symbol must go through
1784 	     PLT.  */
1785 	  if (h != NULL
1786 	      && (bfd_link_executable (info)
1787 		  || h->type == STT_GNU_IFUNC))
1788 	    {
1789 	      bfd_boolean func_pointer_ref = FALSE;
1790 
1791 	      if (r_type == R_386_PC32)
1792 		{
1793 		  /* Since something like ".long foo - ." may be used
1794 		     as pointer, make sure that PLT is used if foo is
1795 		     a function defined in a shared library.  */
1796 		  if ((sec->flags & SEC_CODE) == 0)
1797 		    h->pointer_equality_needed = 1;
1798 		  else if (h->type == STT_GNU_IFUNC
1799 			   && bfd_link_pic (info))
1800 		    {
1801 		      _bfd_error_handler
1802 			/* xgettext:c-format */
1803 			(_("%pB: unsupported non-PIC call to IFUNC `%s'"),
1804 			 abfd, h->root.root.string);
1805 		      bfd_set_error (bfd_error_bad_value);
1806 		      goto error_return;
1807 		    }
1808 		}
1809 	      else
1810 		{
1811 		  h->pointer_equality_needed = 1;
1812 		  /* R_386_32 can be resolved at run-time.  */
1813 		  if (r_type == R_386_32
1814 		      && (sec->flags & SEC_READONLY) == 0)
1815 		    func_pointer_ref = TRUE;
1816 		}
1817 
1818 	      if (!func_pointer_ref)
1819 		{
1820 		  /* If this reloc is in a read-only section, we might
1821 		     need a copy reloc.  We can't check reliably at this
1822 		     stage whether the section is read-only, as input
1823 		     sections have not yet been mapped to output sections.
1824 		     Tentatively set the flag for now, and correct in
1825 		     adjust_dynamic_symbol.  */
1826 		  h->non_got_ref = 1;
1827 
1828 		  /* We may need a .plt entry if the symbol is a function
1829 		     defined in a shared lib or is a function referenced
1830 		     from the code or read-only section.  */
1831 		  if (!h->def_regular
1832 		      || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1833 		    h->plt.refcount = 1;
1834 		}
1835 	    }
1836 
1837 	  size_reloc = FALSE;
1838 do_size:
1839 	  if (NEED_DYNAMIC_RELOCATION_P (info, FALSE, h, sec, r_type,
1840 					 R_386_32))
1841 	    {
1842 	      struct elf_dyn_relocs *p;
1843 	      struct elf_dyn_relocs **head;
1844 
1845 	      /* We must copy these reloc types into the output file.
1846 		 Create a reloc section in dynobj and make room for
1847 		 this reloc.  */
1848 	      if (sreloc == NULL)
1849 		{
1850 		  sreloc = _bfd_elf_make_dynamic_reloc_section
1851 		    (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1852 
1853 		  if (sreloc == NULL)
1854 		    goto error_return;
1855 		}
1856 
1857 	      /* If this is a global symbol, we count the number of
1858 		 relocations we need for this symbol.  */
1859 	      if (h != NULL)
1860 		{
1861 		  head = &eh->dyn_relocs;
1862 		}
1863 	      else
1864 		{
1865 		  /* Track dynamic relocs needed for local syms too.
1866 		     We really need local syms available to do this
1867 		     easily.  Oh well.  */
1868 		  void **vpp;
1869 		  asection *s;
1870 
1871 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1872 						abfd, r_symndx);
1873 		  if (isym == NULL)
1874 		    goto error_return;
1875 
1876 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1877 		  if (s == NULL)
1878 		    s = sec;
1879 
1880 		  vpp = &elf_section_data (s)->local_dynrel;
1881 		  head = (struct elf_dyn_relocs **)vpp;
1882 		}
1883 
1884 	      p = *head;
1885 	      if (p == NULL || p->sec != sec)
1886 		{
1887 		  bfd_size_type amt = sizeof *p;
1888 		  p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1889 							   amt);
1890 		  if (p == NULL)
1891 		    goto error_return;
1892 		  p->next = *head;
1893 		  *head = p;
1894 		  p->sec = sec;
1895 		  p->count = 0;
1896 		  p->pc_count = 0;
1897 		}
1898 
1899 	      p->count += 1;
1900 	      /* Count size relocation as PC-relative relocation.  */
1901 	      if (r_type == R_386_PC32 || size_reloc)
1902 		p->pc_count += 1;
1903 	    }
1904 	  break;
1905 
1906 	  /* This relocation describes the C++ object vtable hierarchy.
1907 	     Reconstruct it for later use during GC.  */
1908 	case R_386_GNU_VTINHERIT:
1909 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1910 	    goto error_return;
1911 	  break;
1912 
1913 	  /* This relocation describes which C++ vtable entries are actually
1914 	     used.  Record for later use during GC.  */
1915 	case R_386_GNU_VTENTRY:
1916 	  BFD_ASSERT (h != NULL);
1917 	  if (h != NULL
1918 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1919 	    goto error_return;
1920 	  break;
1921 
1922 	default:
1923 	  break;
1924 	}
1925     }
1926 
1927   if (elf_section_data (sec)->this_hdr.contents != contents)
1928     {
1929       if (!converted && !info->keep_memory)
1930 	free (contents);
1931       else
1932 	{
1933 	  /* Cache the section contents for elf_link_input_bfd if any
1934 	     load is converted or --no-keep-memory isn't used.  */
1935 	  elf_section_data (sec)->this_hdr.contents = contents;
1936 	}
1937     }
1938 
1939   /* Cache relocations if any load is converted.  */
1940   if (elf_section_data (sec)->relocs != relocs && converted)
1941     elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1942 
1943   return TRUE;
1944 
1945 error_return:
1946   if (elf_section_data (sec)->this_hdr.contents != contents)
1947     free (contents);
1948   sec->check_relocs_failed = 1;
1949   return FALSE;
1950 }
1951 
1952 /* Set the correct type for an x86 ELF section.  We do this by the
1953    section name, which is a hack, but ought to work.  */
1954 
1955 static bfd_boolean
1956 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1957 			Elf_Internal_Shdr *hdr,
1958 			asection *sec)
1959 {
1960   const char *name;
1961 
1962   name = bfd_get_section_name (abfd, sec);
1963 
1964   /* This is an ugly, but unfortunately necessary hack that is
1965      needed when producing EFI binaries on x86. It tells
1966      elf.c:elf_fake_sections() not to consider ".reloc" as a section
1967      containing ELF relocation info.  We need this hack in order to
1968      be able to generate ELF binaries that can be translated into
1969      EFI applications (which are essentially COFF objects).  Those
1970      files contain a COFF ".reloc" section inside an ELFNN object,
1971      which would normally cause BFD to segfault because it would
1972      attempt to interpret this section as containing relocation
1973      entries for section "oc".  With this hack enabled, ".reloc"
1974      will be treated as a normal data section, which will avoid the
1975      segfault.  However, you won't be able to create an ELFNN binary
1976      with a section named "oc" that needs relocations, but that's
1977      the kind of ugly side-effects you get when detecting section
1978      types based on their names...  In practice, this limitation is
1979      unlikely to bite.  */
1980   if (strcmp (name, ".reloc") == 0)
1981     hdr->sh_type = SHT_PROGBITS;
1982 
1983   return TRUE;
1984 }
1985 
1986 /* Return the relocation value for @tpoff relocation
1987    if STT_TLS virtual address is ADDRESS.  */
1988 
1989 static bfd_vma
1990 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
1991 {
1992   struct elf_link_hash_table *htab = elf_hash_table (info);
1993   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
1994   bfd_vma static_tls_size;
1995 
1996   /* If tls_sec is NULL, we should have signalled an error already.  */
1997   if (htab->tls_sec == NULL)
1998     return 0;
1999 
2000   /* Consider special static TLS alignment requirements.  */
2001   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2002   return static_tls_size + htab->tls_sec->vma - address;
2003 }
2004 
2005 /* Relocate an i386 ELF section.  */
2006 
2007 static bfd_boolean
2008 elf_i386_relocate_section (bfd *output_bfd,
2009 			   struct bfd_link_info *info,
2010 			   bfd *input_bfd,
2011 			   asection *input_section,
2012 			   bfd_byte *contents,
2013 			   Elf_Internal_Rela *relocs,
2014 			   Elf_Internal_Sym *local_syms,
2015 			   asection **local_sections)
2016 {
2017   struct elf_x86_link_hash_table *htab;
2018   Elf_Internal_Shdr *symtab_hdr;
2019   struct elf_link_hash_entry **sym_hashes;
2020   bfd_vma *local_got_offsets;
2021   bfd_vma *local_tlsdesc_gotents;
2022   Elf_Internal_Rela *rel;
2023   Elf_Internal_Rela *wrel;
2024   Elf_Internal_Rela *relend;
2025   bfd_boolean is_vxworks_tls;
2026   unsigned plt_entry_size;
2027 
2028   /* Skip if check_relocs failed.  */
2029   if (input_section->check_relocs_failed)
2030     return FALSE;
2031 
2032   htab = elf_x86_hash_table (info, I386_ELF_DATA);
2033   if (htab == NULL)
2034     return FALSE;
2035 
2036   BFD_ASSERT (is_x86_elf (input_bfd, htab));
2037 
2038   symtab_hdr = &elf_symtab_hdr (input_bfd);
2039   sym_hashes = elf_sym_hashes (input_bfd);
2040   local_got_offsets = elf_local_got_offsets (input_bfd);
2041   local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2042   /* We have to handle relocations in vxworks .tls_vars sections
2043      specially, because the dynamic loader is 'weird'.  */
2044   is_vxworks_tls = (htab->target_os == is_vxworks
2045 		    && bfd_link_pic (info)
2046 		    && !strcmp (input_section->output_section->name,
2047 				".tls_vars"));
2048 
2049   _bfd_x86_elf_set_tls_module_base (info);
2050 
2051   plt_entry_size = htab->plt.plt_entry_size;
2052 
2053   rel = wrel = relocs;
2054   relend = relocs + input_section->reloc_count;
2055   for (; rel < relend; wrel++, rel++)
2056     {
2057       unsigned int r_type, r_type_tls;
2058       reloc_howto_type *howto;
2059       unsigned long r_symndx;
2060       struct elf_link_hash_entry *h;
2061       struct elf_x86_link_hash_entry *eh;
2062       Elf_Internal_Sym *sym;
2063       asection *sec;
2064       bfd_vma off, offplt, plt_offset;
2065       bfd_vma relocation;
2066       bfd_boolean unresolved_reloc;
2067       bfd_reloc_status_type r;
2068       unsigned int indx;
2069       int tls_type;
2070       bfd_vma st_size;
2071       asection *resolved_plt;
2072       bfd_boolean resolved_to_zero;
2073       bfd_boolean relative_reloc;
2074 
2075       r_type = ELF32_R_TYPE (rel->r_info);
2076       if (r_type == R_386_GNU_VTINHERIT
2077 	  || r_type == R_386_GNU_VTENTRY)
2078 	{
2079 	  if (wrel != rel)
2080 	    *wrel = *rel;
2081 	  continue;
2082 	}
2083 
2084       if ((indx = r_type) >= R_386_standard
2085 	  && ((indx = r_type - R_386_ext_offset) - R_386_standard
2086 	      >= R_386_ext - R_386_standard)
2087 	  && ((indx = r_type - R_386_tls_offset) - R_386_ext
2088 	      >= R_386_ext2 - R_386_ext))
2089 	return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2090 
2091       howto = elf_howto_table + indx;
2092 
2093       r_symndx = ELF32_R_SYM (rel->r_info);
2094       h = NULL;
2095       sym = NULL;
2096       sec = NULL;
2097       unresolved_reloc = FALSE;
2098       if (r_symndx < symtab_hdr->sh_info)
2099 	{
2100 	  sym = local_syms + r_symndx;
2101 	  sec = local_sections[r_symndx];
2102 	  relocation = (sec->output_section->vma
2103 			+ sec->output_offset
2104 			+ sym->st_value);
2105 	  st_size = sym->st_size;
2106 
2107 	  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2108 	      && ((sec->flags & SEC_MERGE) != 0
2109 		  || (bfd_link_relocatable (info)
2110 		      && sec->output_offset != 0)))
2111 	    {
2112 	      bfd_vma addend;
2113 	      bfd_byte *where = contents + rel->r_offset;
2114 
2115 	      switch (howto->size)
2116 		{
2117 		case 0:
2118 		  addend = bfd_get_8 (input_bfd, where);
2119 		  if (howto->pc_relative)
2120 		    {
2121 		      addend = (addend ^ 0x80) - 0x80;
2122 		      addend += 1;
2123 		    }
2124 		  break;
2125 		case 1:
2126 		  addend = bfd_get_16 (input_bfd, where);
2127 		  if (howto->pc_relative)
2128 		    {
2129 		      addend = (addend ^ 0x8000) - 0x8000;
2130 		      addend += 2;
2131 		    }
2132 		  break;
2133 		case 2:
2134 		  addend = bfd_get_32 (input_bfd, where);
2135 		  if (howto->pc_relative)
2136 		    {
2137 		      addend = (addend ^ 0x80000000) - 0x80000000;
2138 		      addend += 4;
2139 		    }
2140 		  break;
2141 		default:
2142 		  abort ();
2143 		}
2144 
2145 	      if (bfd_link_relocatable (info))
2146 		addend += sec->output_offset;
2147 	      else
2148 		{
2149 		  asection *msec = sec;
2150 		  addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2151 						   addend);
2152 		  addend -= relocation;
2153 		  addend += msec->output_section->vma + msec->output_offset;
2154 		}
2155 
2156 	      switch (howto->size)
2157 		{
2158 		case 0:
2159 		  /* FIXME: overflow checks.  */
2160 		  if (howto->pc_relative)
2161 		    addend -= 1;
2162 		  bfd_put_8 (input_bfd, addend, where);
2163 		  break;
2164 		case 1:
2165 		  if (howto->pc_relative)
2166 		    addend -= 2;
2167 		  bfd_put_16 (input_bfd, addend, where);
2168 		  break;
2169 		case 2:
2170 		  if (howto->pc_relative)
2171 		    addend -= 4;
2172 		  bfd_put_32 (input_bfd, addend, where);
2173 		  break;
2174 		}
2175 	    }
2176 	  else if (!bfd_link_relocatable (info)
2177 		   && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2178 	    {
2179 	      /* Relocate against local STT_GNU_IFUNC symbol.  */
2180 	      h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2181 						   FALSE);
2182 	      if (h == NULL)
2183 		abort ();
2184 
2185 	      /* Set STT_GNU_IFUNC symbol value.  */
2186 	      h->root.u.def.value = sym->st_value;
2187 	      h->root.u.def.section = sec;
2188 	    }
2189 	}
2190       else
2191 	{
2192 	  bfd_boolean warned ATTRIBUTE_UNUSED;
2193 	  bfd_boolean ignored ATTRIBUTE_UNUSED;
2194 
2195 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2196 				   r_symndx, symtab_hdr, sym_hashes,
2197 				   h, sec, relocation,
2198 				   unresolved_reloc, warned, ignored);
2199 	  st_size = h->size;
2200 	}
2201 
2202       if (sec != NULL && discarded_section (sec))
2203 	{
2204 	  _bfd_clear_contents (howto, input_bfd, input_section,
2205 			       contents + rel->r_offset);
2206 	  wrel->r_offset = rel->r_offset;
2207 	  wrel->r_info = 0;
2208 	  wrel->r_addend = 0;
2209 
2210 	  /* For ld -r, remove relocations in debug sections against
2211 	     sections defined in discarded sections.  Not done for
2212 	     eh_frame editing code expects to be present.  */
2213 	   if (bfd_link_relocatable (info)
2214 	       && (input_section->flags & SEC_DEBUGGING))
2215 	     wrel--;
2216 
2217 	   continue;
2218 	}
2219 
2220       if (bfd_link_relocatable (info))
2221 	{
2222 	  if (wrel != rel)
2223 	    *wrel = *rel;
2224 	  continue;
2225 	}
2226 
2227       eh = (struct elf_x86_link_hash_entry *) h;
2228 
2229       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2230 	 it here if it is defined in a non-shared object.  */
2231       if (h != NULL
2232 	  && h->type == STT_GNU_IFUNC
2233 	  && h->def_regular)
2234 	{
2235 	  asection *gotplt, *base_got;
2236 	  bfd_vma plt_index;
2237 	  const char *name;
2238 
2239 	  if ((input_section->flags & SEC_ALLOC) == 0)
2240 	    {
2241 	      /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2242 	         STT_GNU_IFUNC symbol as STT_FUNC.  */
2243 	      if (elf_section_type (input_section) == SHT_NOTE)
2244 		goto skip_ifunc;
2245 	      /* Dynamic relocs are not propagated for SEC_DEBUGGING
2246 		 sections because such sections are not SEC_ALLOC and
2247 		 thus ld.so will not process them.  */
2248 	      if ((input_section->flags & SEC_DEBUGGING) != 0)
2249 		continue;
2250 	      abort ();
2251 	    }
2252 
2253 	  /* STT_GNU_IFUNC symbol must go through PLT.  */
2254 	  if (htab->elf.splt != NULL)
2255 	    {
2256 	      if (htab->plt_second != NULL)
2257 		{
2258 		  resolved_plt = htab->plt_second;
2259 		  plt_offset = eh->plt_second.offset;
2260 		}
2261 	      else
2262 		{
2263 		  resolved_plt = htab->elf.splt;
2264 		  plt_offset = h->plt.offset;
2265 		}
2266 	      gotplt = htab->elf.sgotplt;
2267 	    }
2268 	  else
2269 	    {
2270 	      resolved_plt = htab->elf.iplt;
2271 	      plt_offset = h->plt.offset;
2272 	      gotplt = htab->elf.igotplt;
2273 	    }
2274 
2275 	  switch (r_type)
2276 	    {
2277 	    default:
2278 	      break;
2279 
2280 	    case R_386_GOT32:
2281 	    case R_386_GOT32X:
2282 	      base_got = htab->elf.sgot;
2283 	      off = h->got.offset;
2284 
2285 	      if (base_got == NULL)
2286 		abort ();
2287 
2288 	      if (off == (bfd_vma) -1)
2289 		{
2290 		  /* We can't use h->got.offset here to save state, or
2291 		     even just remember the offset, as finish_dynamic_symbol
2292 		     would use that as offset into .got.  */
2293 
2294 		  if (h->plt.offset == (bfd_vma) -1)
2295 		    abort ();
2296 
2297 		  if (htab->elf.splt != NULL)
2298 		    {
2299 		      plt_index = (h->plt.offset / plt_entry_size
2300 				   - htab->plt.has_plt0);
2301 		      off = (plt_index + 3) * 4;
2302 		      base_got = htab->elf.sgotplt;
2303 		    }
2304 		  else
2305 		    {
2306 		      plt_index = h->plt.offset / plt_entry_size;
2307 		      off = plt_index * 4;
2308 		      base_got = htab->elf.igotplt;
2309 		    }
2310 
2311 		  if (h->dynindx == -1
2312 		      || h->forced_local
2313 		      || info->symbolic)
2314 		    {
2315 		      /* This references the local defitionion.  We must
2316 			 initialize this entry in the global offset table.
2317 			 Since the offset must always be a multiple of 8,
2318 			 we use the least significant bit to record
2319 			 whether we have initialized it already.
2320 
2321 			 When doing a dynamic link, we create a .rela.got
2322 			 relocation entry to initialize the value.  This
2323 			 is done in the finish_dynamic_symbol routine.	 */
2324 		      if ((off & 1) != 0)
2325 			off &= ~1;
2326 		      else
2327 			{
2328 			  bfd_put_32 (output_bfd, relocation,
2329 				      base_got->contents + off);
2330 			  h->got.offset |= 1;
2331 			}
2332 		    }
2333 
2334 		  relocation = off;
2335 		}
2336 	      else
2337 		relocation = (base_got->output_section->vma
2338 			      + base_got->output_offset + off
2339 			      - gotplt->output_section->vma
2340 			      - gotplt->output_offset);
2341 
2342 	      if (rel->r_offset > 1
2343 		  && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2344 		  && *(contents + rel->r_offset - 2) != 0x8d)
2345 		{
2346 		  if (bfd_link_pic (info))
2347 		    goto disallow_got32;
2348 
2349 		  /* Add the GOT base if there is no base register.  */
2350 		  relocation += (gotplt->output_section->vma
2351 				 + gotplt->output_offset);
2352 		}
2353 	      else if (htab->elf.splt == NULL)
2354 		{
2355 		  /* Adjust for static executables.  */
2356 		  relocation += gotplt->output_offset;
2357 		}
2358 
2359 	      goto do_relocation;
2360 	    }
2361 
2362 	  if (h->plt.offset == (bfd_vma) -1)
2363 	    {
2364 	      /* Handle static pointers of STT_GNU_IFUNC symbols.  */
2365 	      if (r_type == R_386_32
2366 		  && (input_section->flags & SEC_CODE) == 0)
2367 		goto do_ifunc_pointer;
2368 	      goto bad_ifunc_reloc;
2369 	    }
2370 
2371 	  relocation = (resolved_plt->output_section->vma
2372 			+ resolved_plt->output_offset + plt_offset);
2373 
2374 	  switch (r_type)
2375 	    {
2376 	    default:
2377 bad_ifunc_reloc:
2378 	      if (h->root.root.string)
2379 		name = h->root.root.string;
2380 	      else
2381 		name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2382 					 NULL);
2383 	      _bfd_error_handler
2384 		/* xgettext:c-format */
2385 		(_("%pB: relocation %s against STT_GNU_IFUNC "
2386 		   "symbol `%s' isn't supported"), input_bfd,
2387 		 howto->name, name);
2388 	      bfd_set_error (bfd_error_bad_value);
2389 	      return FALSE;
2390 
2391 	    case R_386_32:
2392 	      /* Generate dynamic relcoation only when there is a
2393 		 non-GOT reference in a shared object.  */
2394 	      if ((bfd_link_pic (info) && h->non_got_ref)
2395 		  || h->plt.offset == (bfd_vma) -1)
2396 		{
2397 		  Elf_Internal_Rela outrel;
2398 		  asection *sreloc;
2399 		  bfd_vma offset;
2400 
2401 do_ifunc_pointer:
2402 		  /* Need a dynamic relocation to get the real function
2403 		     adddress.  */
2404 		  offset = _bfd_elf_section_offset (output_bfd,
2405 						    info,
2406 						    input_section,
2407 						    rel->r_offset);
2408 		  if (offset == (bfd_vma) -1
2409 		      || offset == (bfd_vma) -2)
2410 		    abort ();
2411 
2412 		  outrel.r_offset = (input_section->output_section->vma
2413 				     + input_section->output_offset
2414 				     + offset);
2415 
2416 		  if (POINTER_LOCAL_IFUNC_P (info, h))
2417 		    {
2418 		      info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2419 					      h->root.root.string,
2420 					      h->root.u.def.section->owner);
2421 
2422 		      /* This symbol is resolved locally.  */
2423 		      outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2424 		      bfd_put_32 (output_bfd,
2425 				  (h->root.u.def.value
2426 				   + h->root.u.def.section->output_section->vma
2427 				   + h->root.u.def.section->output_offset),
2428 				  contents + offset);
2429 		    }
2430 		  else
2431 		    outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2432 
2433 		  /* Dynamic relocations are stored in
2434 		     1. .rel.ifunc section in PIC object.
2435 		     2. .rel.got section in dynamic executable.
2436 		     3. .rel.iplt section in static executable.  */
2437 		  if (bfd_link_pic (info))
2438 		    sreloc = htab->elf.irelifunc;
2439 		  else if (htab->elf.splt != NULL)
2440 		    sreloc = htab->elf.srelgot;
2441 		  else
2442 		    sreloc = htab->elf.irelplt;
2443 		  elf_append_rel (output_bfd, sreloc, &outrel);
2444 
2445 		  /* If this reloc is against an external symbol, we
2446 		     do not want to fiddle with the addend.  Otherwise,
2447 		     we need to include the symbol value so that it
2448 		     becomes an addend for the dynamic reloc.  For an
2449 		     internal symbol, we have updated addend.  */
2450 		  continue;
2451 		}
2452 	      /* FALLTHROUGH */
2453 	    case R_386_PC32:
2454 	    case R_386_PLT32:
2455 	      goto do_relocation;
2456 
2457 	    case R_386_GOTOFF:
2458 	      relocation -= (gotplt->output_section->vma
2459 			     + gotplt->output_offset);
2460 	      goto do_relocation;
2461 	    }
2462 	}
2463 
2464 skip_ifunc:
2465       resolved_to_zero = (eh != NULL
2466 			  && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2467 
2468       switch (r_type)
2469 	{
2470 	case R_386_GOT32X:
2471 	case R_386_GOT32:
2472 	  /* Relocation is to the entry for this symbol in the global
2473 	     offset table.  */
2474 	  if (htab->elf.sgot == NULL)
2475 	    abort ();
2476 
2477 	  relative_reloc = FALSE;
2478 	  if (h != NULL)
2479 	    {
2480 	      off = h->got.offset;
2481 	      if (RESOLVED_LOCALLY_P (info, h, htab))
2482 		{
2483 		  /* We must initialize this entry in the global offset
2484 		     table.  Since the offset must always be a multiple
2485 		     of 4, we use the least significant bit to record
2486 		     whether we have initialized it already.
2487 
2488 		     When doing a dynamic link, we create a .rel.got
2489 		     relocation entry to initialize the value.  This
2490 		     is done in the finish_dynamic_symbol routine.  */
2491 		  if ((off & 1) != 0)
2492 		    off &= ~1;
2493 		  else
2494 		    {
2495 		      bfd_put_32 (output_bfd, relocation,
2496 				  htab->elf.sgot->contents + off);
2497 		      h->got.offset |= 1;
2498 
2499 		      if (GENERATE_RELATIVE_RELOC_P (info, h))
2500 			{
2501 			  /* PR ld/21402: If this symbol isn't dynamic
2502 			     in PIC, generate R_386_RELATIVE here.  */
2503 			  eh->no_finish_dynamic_symbol = 1;
2504 			  relative_reloc = TRUE;
2505 			}
2506 		    }
2507 		}
2508 	      else
2509 		unresolved_reloc = FALSE;
2510 	    }
2511 	  else
2512 	    {
2513 	      if (local_got_offsets == NULL)
2514 		abort ();
2515 
2516 	      off = local_got_offsets[r_symndx];
2517 
2518 	      /* The offset must always be a multiple of 4.  We use
2519 		 the least significant bit to record whether we have
2520 		 already generated the necessary reloc.  */
2521 	      if ((off & 1) != 0)
2522 		off &= ~1;
2523 	      else
2524 		{
2525 		  bfd_put_32 (output_bfd, relocation,
2526 			      htab->elf.sgot->contents + off);
2527 		  local_got_offsets[r_symndx] |= 1;
2528 
2529 		  if (bfd_link_pic (info))
2530 		    relative_reloc = TRUE;
2531 		}
2532 	    }
2533 
2534 	  if (relative_reloc)
2535 	    {
2536 	      asection *s;
2537 	      Elf_Internal_Rela outrel;
2538 
2539 	      s = htab->elf.srelgot;
2540 	      if (s == NULL)
2541 		abort ();
2542 
2543 	      outrel.r_offset = (htab->elf.sgot->output_section->vma
2544 				 + htab->elf.sgot->output_offset
2545 				 + off);
2546 	      outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2547 	      elf_append_rel (output_bfd, s, &outrel);
2548 	    }
2549 
2550 	  if (off >= (bfd_vma) -2)
2551 	    abort ();
2552 
2553 	  relocation = (htab->elf.sgot->output_section->vma
2554 			+ htab->elf.sgot->output_offset + off);
2555 	  if (rel->r_offset > 1
2556 	      && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2557 	      && *(contents + rel->r_offset - 2) != 0x8d)
2558 	    {
2559 	      if (bfd_link_pic (info))
2560 		{
2561 		  /* For PIC, disallow R_386_GOT32 without a base
2562 		     register, except for "lea foo@GOT, %reg", since
2563 		     we don't know what the GOT base is.  */
2564 		  const char *name;
2565 
2566 disallow_got32:
2567 		  if (h == NULL || h->root.root.string == NULL)
2568 		    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2569 					     NULL);
2570 		  else
2571 		    name = h->root.root.string;
2572 
2573 		  _bfd_error_handler
2574 		    /* xgettext:c-format */
2575 		    (_("%pB: direct GOT relocation %s against `%s'"
2576 		       " without base register can not be used"
2577 		       " when making a shared object"),
2578 		     input_bfd, howto->name, name);
2579 		  bfd_set_error (bfd_error_bad_value);
2580 		  return FALSE;
2581 		}
2582 	    }
2583 	  else
2584 	    {
2585 	      /* Subtract the .got.plt section address only with a base
2586 		 register.  */
2587 	      relocation -= (htab->elf.sgotplt->output_section->vma
2588 			     + htab->elf.sgotplt->output_offset);
2589 	    }
2590 
2591 	  break;
2592 
2593 	case R_386_GOTOFF:
2594 	  /* Relocation is relative to the start of the global offset
2595 	     table.  */
2596 
2597 	  /* Check to make sure it isn't a protected function or data
2598 	     symbol for shared library since it may not be local when
2599 	     used as function address or with copy relocation.  We also
2600 	     need to make sure that a symbol is referenced locally.  */
2601 	  if (!bfd_link_executable (info) && h)
2602 	    {
2603 	      if (!h->def_regular)
2604 		{
2605 		  const char *v;
2606 
2607 		  switch (ELF_ST_VISIBILITY (h->other))
2608 		    {
2609 		    case STV_HIDDEN:
2610 		      v = _("hidden symbol");
2611 		      break;
2612 		    case STV_INTERNAL:
2613 		      v = _("internal symbol");
2614 		      break;
2615 		    case STV_PROTECTED:
2616 		      v = _("protected symbol");
2617 		      break;
2618 		    default:
2619 		      v = _("symbol");
2620 		      break;
2621 		    }
2622 
2623 		  _bfd_error_handler
2624 		    /* xgettext:c-format */
2625 		    (_("%pB: relocation R_386_GOTOFF against undefined %s"
2626 		       " `%s' can not be used when making a shared object"),
2627 		     input_bfd, v, h->root.root.string);
2628 		  bfd_set_error (bfd_error_bad_value);
2629 		  return FALSE;
2630 		}
2631 	      else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2632 		       && (h->type == STT_FUNC
2633 			   || h->type == STT_OBJECT)
2634 		       && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2635 		{
2636 		  _bfd_error_handler
2637 		    /* xgettext:c-format */
2638 		    (_("%pB: relocation R_386_GOTOFF against protected %s"
2639 		       " `%s' can not be used when making a shared object"),
2640 		     input_bfd,
2641 		     h->type == STT_FUNC ? "function" : "data",
2642 		     h->root.root.string);
2643 		  bfd_set_error (bfd_error_bad_value);
2644 		  return FALSE;
2645 		}
2646 	    }
2647 
2648 	  /* Note that sgot is not involved in this
2649 	     calculation.  We always want the start of .got.plt.  If we
2650 	     defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2651 	     permitted by the ABI, we might have to change this
2652 	     calculation.  */
2653 	  relocation -= htab->elf.sgotplt->output_section->vma
2654 			+ htab->elf.sgotplt->output_offset;
2655 	  break;
2656 
2657 	case R_386_GOTPC:
2658 	  /* Use global offset table as symbol value.  */
2659 	  relocation = htab->elf.sgotplt->output_section->vma
2660 		       + htab->elf.sgotplt->output_offset;
2661 	  unresolved_reloc = FALSE;
2662 	  break;
2663 
2664 	case R_386_PLT32:
2665 	  /* Relocation is to the entry for this symbol in the
2666 	     procedure linkage table.  */
2667 
2668 	  /* Resolve a PLT32 reloc against a local symbol directly,
2669 	     without using the procedure linkage table.  */
2670 	  if (h == NULL)
2671 	    break;
2672 
2673 	  if ((h->plt.offset == (bfd_vma) -1
2674 	       && eh->plt_got.offset == (bfd_vma) -1)
2675 	      || htab->elf.splt == NULL)
2676 	    {
2677 	      /* We didn't make a PLT entry for this symbol.  This
2678 		 happens when statically linking PIC code, or when
2679 		 using -Bsymbolic.  */
2680 	      break;
2681 	    }
2682 
2683 	  if (h->plt.offset != (bfd_vma) -1)
2684 	    {
2685 	      if (htab->plt_second != NULL)
2686 		{
2687 		  resolved_plt = htab->plt_second;
2688 		  plt_offset = eh->plt_second.offset;
2689 		}
2690 	      else
2691 		{
2692 		  resolved_plt = htab->elf.splt;
2693 		  plt_offset = h->plt.offset;
2694 		}
2695 	    }
2696 	  else
2697 	    {
2698 	      resolved_plt = htab->plt_got;
2699 	      plt_offset = eh->plt_got.offset;
2700 	    }
2701 
2702 	  relocation = (resolved_plt->output_section->vma
2703 			+ resolved_plt->output_offset
2704 			+ plt_offset);
2705 	  unresolved_reloc = FALSE;
2706 	  break;
2707 
2708 	case R_386_SIZE32:
2709 	  /* Set to symbol size.  */
2710 	  relocation = st_size;
2711 	  /* Fall through.  */
2712 
2713 	case R_386_32:
2714 	case R_386_PC32:
2715 	  if ((input_section->flags & SEC_ALLOC) == 0
2716 	      || is_vxworks_tls)
2717 	    break;
2718 
2719 	  if (GENERATE_DYNAMIC_RELOCATION_P (info, eh, r_type,
2720 					     FALSE, resolved_to_zero,
2721 					     (r_type == R_386_PC32)))
2722 	    {
2723 	      Elf_Internal_Rela outrel;
2724 	      bfd_boolean skip, relocate;
2725 	      asection *sreloc;
2726 
2727 	      /* When generating a shared object, these relocations
2728 		 are copied into the output file to be resolved at run
2729 		 time.  */
2730 
2731 	      skip = FALSE;
2732 	      relocate = FALSE;
2733 
2734 	      outrel.r_offset =
2735 		_bfd_elf_section_offset (output_bfd, info, input_section,
2736 					 rel->r_offset);
2737 	      if (outrel.r_offset == (bfd_vma) -1)
2738 		skip = TRUE;
2739 	      else if (outrel.r_offset == (bfd_vma) -2)
2740 		skip = TRUE, relocate = TRUE;
2741 	      outrel.r_offset += (input_section->output_section->vma
2742 				  + input_section->output_offset);
2743 
2744 	      if (skip)
2745 		memset (&outrel, 0, sizeof outrel);
2746 	      else if (COPY_INPUT_RELOC_P (info, h, r_type))
2747 		outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2748 	      else
2749 		{
2750 		  /* This symbol is local, or marked to become local.  */
2751 		  relocate = TRUE;
2752 		  outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2753 		}
2754 
2755 	      sreloc = elf_section_data (input_section)->sreloc;
2756 
2757 	      if (sreloc == NULL || sreloc->contents == NULL)
2758 		{
2759 		  r = bfd_reloc_notsupported;
2760 		  goto check_relocation_error;
2761 		}
2762 
2763 	      elf_append_rel (output_bfd, sreloc, &outrel);
2764 
2765 	      /* If this reloc is against an external symbol, we do
2766 		 not want to fiddle with the addend.  Otherwise, we
2767 		 need to include the symbol value so that it becomes
2768 		 an addend for the dynamic reloc.  */
2769 	      if (! relocate)
2770 		continue;
2771 	    }
2772 	  break;
2773 
2774 	case R_386_TLS_IE:
2775 	  if (!bfd_link_executable (info))
2776 	    {
2777 	      Elf_Internal_Rela outrel;
2778 	      asection *sreloc;
2779 
2780 	      outrel.r_offset = rel->r_offset
2781 				+ input_section->output_section->vma
2782 				+ input_section->output_offset;
2783 	      outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2784 	      sreloc = elf_section_data (input_section)->sreloc;
2785 	      if (sreloc == NULL)
2786 		abort ();
2787 	      elf_append_rel (output_bfd, sreloc, &outrel);
2788 	    }
2789 	  /* Fall through */
2790 
2791 	case R_386_TLS_GD:
2792 	case R_386_TLS_GOTDESC:
2793 	case R_386_TLS_DESC_CALL:
2794 	case R_386_TLS_IE_32:
2795 	case R_386_TLS_GOTIE:
2796 	  tls_type = GOT_UNKNOWN;
2797 	  if (h == NULL && local_got_offsets)
2798 	    tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2799 	  else if (h != NULL)
2800 	    tls_type = elf_x86_hash_entry(h)->tls_type;
2801 	  if (tls_type == GOT_TLS_IE)
2802 	    tls_type = GOT_TLS_IE_NEG;
2803 
2804 	   r_type_tls = r_type;
2805 	  if (! elf_i386_tls_transition (info, input_bfd,
2806 					 input_section, contents,
2807 					 symtab_hdr, sym_hashes,
2808 					 &r_type_tls, tls_type, rel,
2809 					 relend, h, r_symndx, TRUE))
2810 	    return FALSE;
2811 
2812 	  if (r_type_tls == R_386_TLS_LE_32)
2813 	    {
2814 	      BFD_ASSERT (! unresolved_reloc);
2815 	      if (r_type == R_386_TLS_GD)
2816 		{
2817 		  unsigned int type;
2818 		  bfd_vma roff;
2819 
2820 		  /* GD->LE transition.  */
2821 		  type = *(contents + rel->r_offset - 2);
2822 		  if (type == 0x04)
2823 		    {
2824 		      /* Change
2825 				leal foo@tlsgd(,%ebx,1), %eax
2826 				call ___tls_get_addr@PLT
2827 			 into:
2828 				movl %gs:0, %eax
2829 				subl $foo@tpoff, %eax
2830 			 (6 byte form of subl).  */
2831 		      roff = rel->r_offset + 5;
2832 		    }
2833 		  else
2834 		    {
2835 		      /* Change
2836 				leal foo@tlsgd(%ebx), %eax
2837 				call ___tls_get_addr@PLT
2838 				nop
2839 			 or
2840 				leal foo@tlsgd(%reg), %eax
2841 				call *___tls_get_addr@GOT(%reg)
2842 				which may be converted to
2843 				addr32 call ___tls_get_addr
2844 			 into:
2845 				movl %gs:0, %eax; subl $foo@tpoff, %eax
2846 			 (6 byte form of subl).  */
2847 		      roff = rel->r_offset + 6;
2848 		    }
2849 		  memcpy (contents + roff - 8,
2850 			  "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2851 		  bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2852 			      contents + roff);
2853 		  /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X.  */
2854 		  rel++;
2855 		  wrel++;
2856 		  continue;
2857 		}
2858 	      else if (r_type == R_386_TLS_GOTDESC)
2859 		{
2860 		  /* GDesc -> LE transition.
2861 		     It's originally something like:
2862 		     leal x@tlsdesc(%ebx), %eax
2863 
2864 		     leal x@ntpoff, %eax
2865 
2866 		     Registers other than %eax may be set up here.  */
2867 
2868 		  unsigned int val;
2869 		  bfd_vma roff;
2870 
2871 		  roff = rel->r_offset;
2872 		  val = bfd_get_8 (input_bfd, contents + roff - 1);
2873 
2874 		  /* Now modify the instruction as appropriate.  */
2875 		  /* aoliva FIXME: remove the above and xor the byte
2876 		     below with 0x86.  */
2877 		  bfd_put_8 (output_bfd, val ^ 0x86,
2878 			     contents + roff - 1);
2879 		  bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2880 			      contents + roff);
2881 		  continue;
2882 		}
2883 	      else if (r_type == R_386_TLS_DESC_CALL)
2884 		{
2885 		  /* GDesc -> LE transition.
2886 		     It's originally:
2887 		     call *(%eax)
2888 		     Turn it into:
2889 		     xchg %ax,%ax  */
2890 
2891 		  bfd_vma roff;
2892 
2893 		  roff = rel->r_offset;
2894 		  bfd_put_8 (output_bfd, 0x66, contents + roff);
2895 		  bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2896 		  continue;
2897 		}
2898 	      else if (r_type == R_386_TLS_IE)
2899 		{
2900 		  unsigned int val;
2901 
2902 		  /* IE->LE transition:
2903 		     Originally it can be one of:
2904 		     movl foo, %eax
2905 		     movl foo, %reg
2906 		     addl foo, %reg
2907 		     We change it into:
2908 		     movl $foo, %eax
2909 		     movl $foo, %reg
2910 		     addl $foo, %reg.  */
2911 		  val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2912 		  if (val == 0xa1)
2913 		    {
2914 		      /* movl foo, %eax.  */
2915 		      bfd_put_8 (output_bfd, 0xb8,
2916 				 contents + rel->r_offset - 1);
2917 		    }
2918 		  else
2919 		    {
2920 		      unsigned int type;
2921 
2922 		      type = bfd_get_8 (input_bfd,
2923 					contents + rel->r_offset - 2);
2924 		      switch (type)
2925 			{
2926 			case 0x8b:
2927 			  /* movl */
2928 			  bfd_put_8 (output_bfd, 0xc7,
2929 				     contents + rel->r_offset - 2);
2930 			  bfd_put_8 (output_bfd,
2931 				     0xc0 | ((val >> 3) & 7),
2932 				     contents + rel->r_offset - 1);
2933 			  break;
2934 			case 0x03:
2935 			  /* addl */
2936 			  bfd_put_8 (output_bfd, 0x81,
2937 				     contents + rel->r_offset - 2);
2938 			  bfd_put_8 (output_bfd,
2939 				     0xc0 | ((val >> 3) & 7),
2940 				     contents + rel->r_offset - 1);
2941 			  break;
2942 			default:
2943 			  BFD_FAIL ();
2944 			  break;
2945 			}
2946 		    }
2947 		  bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2948 			      contents + rel->r_offset);
2949 		  continue;
2950 		}
2951 	      else
2952 		{
2953 		  unsigned int val, type;
2954 
2955 		  /* {IE_32,GOTIE}->LE transition:
2956 		     Originally it can be one of:
2957 		     subl foo(%reg1), %reg2
2958 		     movl foo(%reg1), %reg2
2959 		     addl foo(%reg1), %reg2
2960 		     We change it into:
2961 		     subl $foo, %reg2
2962 		     movl $foo, %reg2 (6 byte form)
2963 		     addl $foo, %reg2.  */
2964 		  type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2965 		  val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2966 		  if (type == 0x8b)
2967 		    {
2968 		      /* movl */
2969 		      bfd_put_8 (output_bfd, 0xc7,
2970 				 contents + rel->r_offset - 2);
2971 		      bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2972 				 contents + rel->r_offset - 1);
2973 		    }
2974 		  else if (type == 0x2b)
2975 		    {
2976 		      /* subl */
2977 		      bfd_put_8 (output_bfd, 0x81,
2978 				 contents + rel->r_offset - 2);
2979 		      bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2980 				 contents + rel->r_offset - 1);
2981 		    }
2982 		  else if (type == 0x03)
2983 		    {
2984 		      /* addl */
2985 		      bfd_put_8 (output_bfd, 0x81,
2986 				 contents + rel->r_offset - 2);
2987 		      bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2988 				 contents + rel->r_offset - 1);
2989 		    }
2990 		  else
2991 		    BFD_FAIL ();
2992 		  if (r_type == R_386_TLS_GOTIE)
2993 		    bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2994 				contents + rel->r_offset);
2995 		  else
2996 		    bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2997 				contents + rel->r_offset);
2998 		  continue;
2999 		}
3000 	    }
3001 
3002 	  if (htab->elf.sgot == NULL)
3003 	    abort ();
3004 
3005 	  if (h != NULL)
3006 	    {
3007 	      off = h->got.offset;
3008 	      offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3009 	    }
3010 	  else
3011 	    {
3012 	      if (local_got_offsets == NULL)
3013 		abort ();
3014 
3015 	      off = local_got_offsets[r_symndx];
3016 	      offplt = local_tlsdesc_gotents[r_symndx];
3017 	    }
3018 
3019 	  if ((off & 1) != 0)
3020 	    off &= ~1;
3021 	  else
3022 	    {
3023 	      Elf_Internal_Rela outrel;
3024 	      int dr_type;
3025 	      asection *sreloc;
3026 
3027 	      if (htab->elf.srelgot == NULL)
3028 		abort ();
3029 
3030 	      indx = h && h->dynindx != -1 ? h->dynindx : 0;
3031 
3032 	      if (GOT_TLS_GDESC_P (tls_type))
3033 		{
3034 		  bfd_byte *loc;
3035 		  outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3036 		  BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3037 			      <= htab->elf.sgotplt->size);
3038 		  outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3039 				     + htab->elf.sgotplt->output_offset
3040 				     + offplt
3041 				     + htab->sgotplt_jump_table_size);
3042 		  sreloc = htab->elf.srelplt;
3043 		  loc = sreloc->contents;
3044 		  loc += (htab->next_tls_desc_index++
3045 			  * sizeof (Elf32_External_Rel));
3046 		  BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3047 			      <= sreloc->contents + sreloc->size);
3048 		  bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3049 		  if (indx == 0)
3050 		    {
3051 		      BFD_ASSERT (! unresolved_reloc);
3052 		      bfd_put_32 (output_bfd,
3053 				  relocation - _bfd_x86_elf_dtpoff_base (info),
3054 				  htab->elf.sgotplt->contents + offplt
3055 				  + htab->sgotplt_jump_table_size + 4);
3056 		    }
3057 		  else
3058 		    {
3059 		      bfd_put_32 (output_bfd, 0,
3060 				  htab->elf.sgotplt->contents + offplt
3061 				  + htab->sgotplt_jump_table_size + 4);
3062 		    }
3063 		}
3064 
3065 	      sreloc = htab->elf.srelgot;
3066 
3067 	      outrel.r_offset = (htab->elf.sgot->output_section->vma
3068 				 + htab->elf.sgot->output_offset + off);
3069 
3070 	      if (GOT_TLS_GD_P (tls_type))
3071 		dr_type = R_386_TLS_DTPMOD32;
3072 	      else if (GOT_TLS_GDESC_P (tls_type))
3073 		goto dr_done;
3074 	      else if (tls_type == GOT_TLS_IE_POS)
3075 		dr_type = R_386_TLS_TPOFF;
3076 	      else
3077 		dr_type = R_386_TLS_TPOFF32;
3078 
3079 	      if (dr_type == R_386_TLS_TPOFF && indx == 0)
3080 		bfd_put_32 (output_bfd,
3081 			    relocation - _bfd_x86_elf_dtpoff_base (info),
3082 			    htab->elf.sgot->contents + off);
3083 	      else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3084 		bfd_put_32 (output_bfd,
3085 			    _bfd_x86_elf_dtpoff_base (info) - relocation,
3086 			    htab->elf.sgot->contents + off);
3087 	      else if (dr_type != R_386_TLS_DESC)
3088 		bfd_put_32 (output_bfd, 0,
3089 			    htab->elf.sgot->contents + off);
3090 	      outrel.r_info = ELF32_R_INFO (indx, dr_type);
3091 
3092 	      elf_append_rel (output_bfd, sreloc, &outrel);
3093 
3094 	      if (GOT_TLS_GD_P (tls_type))
3095 		{
3096 		  if (indx == 0)
3097 		    {
3098 		      BFD_ASSERT (! unresolved_reloc);
3099 		      bfd_put_32 (output_bfd,
3100 				  relocation - _bfd_x86_elf_dtpoff_base (info),
3101 				  htab->elf.sgot->contents + off + 4);
3102 		    }
3103 		  else
3104 		    {
3105 		      bfd_put_32 (output_bfd, 0,
3106 				  htab->elf.sgot->contents + off + 4);
3107 		      outrel.r_info = ELF32_R_INFO (indx,
3108 						    R_386_TLS_DTPOFF32);
3109 		      outrel.r_offset += 4;
3110 		      elf_append_rel (output_bfd, sreloc, &outrel);
3111 		    }
3112 		}
3113 	      else if (tls_type == GOT_TLS_IE_BOTH)
3114 		{
3115 		  bfd_put_32 (output_bfd,
3116 			      (indx == 0
3117 			       ? relocation - _bfd_x86_elf_dtpoff_base (info)
3118 			       : 0),
3119 			      htab->elf.sgot->contents + off + 4);
3120 		  outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3121 		  outrel.r_offset += 4;
3122 		  elf_append_rel (output_bfd, sreloc, &outrel);
3123 		}
3124 
3125 	    dr_done:
3126 	      if (h != NULL)
3127 		h->got.offset |= 1;
3128 	      else
3129 		local_got_offsets[r_symndx] |= 1;
3130 	    }
3131 
3132 	  if (off >= (bfd_vma) -2
3133 	      && ! GOT_TLS_GDESC_P (tls_type))
3134 	    abort ();
3135 	  if (r_type_tls == R_386_TLS_GOTDESC
3136 	      || r_type_tls == R_386_TLS_DESC_CALL)
3137 	    {
3138 	      relocation = htab->sgotplt_jump_table_size + offplt;
3139 	      unresolved_reloc = FALSE;
3140 	    }
3141 	  else if (r_type_tls == r_type)
3142 	    {
3143 	      bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3144 			      + htab->elf.sgotplt->output_offset;
3145 	      relocation = htab->elf.sgot->output_section->vma
3146 		+ htab->elf.sgot->output_offset + off - g_o_t;
3147 	      if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3148 		  && tls_type == GOT_TLS_IE_BOTH)
3149 		relocation += 4;
3150 	      if (r_type == R_386_TLS_IE)
3151 		relocation += g_o_t;
3152 	      unresolved_reloc = FALSE;
3153 	    }
3154 	  else if (r_type == R_386_TLS_GD)
3155 	    {
3156 	      unsigned int val, type;
3157 	      bfd_vma roff;
3158 
3159 	      /* GD->IE transition.  */
3160 	      type = *(contents + rel->r_offset - 2);
3161 	      val = *(contents + rel->r_offset - 1);
3162 	      if (type == 0x04)
3163 		{
3164 		  /* Change
3165 			leal foo@tlsgd(,%ebx,1), %eax
3166 			call ___tls_get_addr@PLT
3167 		     into:
3168 			movl %gs:0, %eax
3169 			subl $foo@gottpoff(%ebx), %eax.  */
3170 		  val >>= 3;
3171 		  roff = rel->r_offset - 3;
3172 		}
3173 	      else
3174 		{
3175 		  /* Change
3176 			leal foo@tlsgd(%ebx), %eax
3177 			call ___tls_get_addr@PLT
3178 			nop
3179 		     or
3180 			leal foo@tlsgd(%reg), %eax
3181 			call *___tls_get_addr@GOT(%reg)
3182 			which may be converted to
3183 			addr32 call ___tls_get_addr
3184 		     into:
3185 			movl %gs:0, %eax;
3186 			subl $foo@gottpoff(%reg), %eax.  */
3187 		  roff = rel->r_offset - 2;
3188 		}
3189 	      memcpy (contents + roff,
3190 		      "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3191 	      contents[roff + 7] = 0x80 | (val & 7);
3192 	      /* If foo is used only with foo@gotntpoff(%reg) and
3193 		 foo@indntpoff, but not with foo@gottpoff(%reg), change
3194 		 subl $foo@gottpoff(%reg), %eax
3195 		 into:
3196 		 addl $foo@gotntpoff(%reg), %eax.  */
3197 	      if (tls_type == GOT_TLS_IE_POS)
3198 		contents[roff + 6] = 0x03;
3199 	      bfd_put_32 (output_bfd,
3200 			  htab->elf.sgot->output_section->vma
3201 			  + htab->elf.sgot->output_offset + off
3202 			  - htab->elf.sgotplt->output_section->vma
3203 			  - htab->elf.sgotplt->output_offset,
3204 			  contents + roff + 8);
3205 	      /* Skip R_386_PLT32 and R_386_GOT32X.  */
3206 	      rel++;
3207 	      wrel++;
3208 	      continue;
3209 	    }
3210 	  else if (r_type == R_386_TLS_GOTDESC)
3211 	    {
3212 	      /* GDesc -> IE transition.
3213 		 It's originally something like:
3214 		 leal x@tlsdesc(%ebx), %eax
3215 
3216 		 Change it to:
3217 		 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3218 		 or:
3219 		 movl x@gottpoff(%ebx), %eax # before negl %eax
3220 
3221 		 Registers other than %eax may be set up here.  */
3222 
3223 	      bfd_vma roff;
3224 
3225 	      /* First, make sure it's a leal adding ebx to a 32-bit
3226 		 offset into any register, although it's probably
3227 		 almost always going to be eax.  */
3228 	      roff = rel->r_offset;
3229 
3230 	      /* Now modify the instruction as appropriate.  */
3231 	      /* To turn a leal into a movl in the form we use it, it
3232 		 suffices to change the first byte from 0x8d to 0x8b.
3233 		 aoliva FIXME: should we decide to keep the leal, all
3234 		 we have to do is remove the statement below, and
3235 		 adjust the relaxation of R_386_TLS_DESC_CALL.  */
3236 	      bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3237 
3238 	      if (tls_type == GOT_TLS_IE_BOTH)
3239 		off += 4;
3240 
3241 	      bfd_put_32 (output_bfd,
3242 			  htab->elf.sgot->output_section->vma
3243 			  + htab->elf.sgot->output_offset + off
3244 			  - htab->elf.sgotplt->output_section->vma
3245 			  - htab->elf.sgotplt->output_offset,
3246 			  contents + roff);
3247 	      continue;
3248 	    }
3249 	  else if (r_type == R_386_TLS_DESC_CALL)
3250 	    {
3251 	      /* GDesc -> IE transition.
3252 		 It's originally:
3253 		 call *(%eax)
3254 
3255 		 Change it to:
3256 		 xchg %ax,%ax
3257 		 or
3258 		 negl %eax
3259 		 depending on how we transformed the TLS_GOTDESC above.
3260 	      */
3261 
3262 	      bfd_vma roff;
3263 
3264 	      roff = rel->r_offset;
3265 
3266 	      /* Now modify the instruction as appropriate.  */
3267 	      if (tls_type != GOT_TLS_IE_NEG)
3268 		{
3269 		  /* xchg %ax,%ax */
3270 		  bfd_put_8 (output_bfd, 0x66, contents + roff);
3271 		  bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3272 		}
3273 	      else
3274 		{
3275 		  /* negl %eax */
3276 		  bfd_put_8 (output_bfd, 0xf7, contents + roff);
3277 		  bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3278 		}
3279 
3280 	      continue;
3281 	    }
3282 	  else
3283 	    BFD_ASSERT (FALSE);
3284 	  break;
3285 
3286 	case R_386_TLS_LDM:
3287 	  if (! elf_i386_tls_transition (info, input_bfd,
3288 					 input_section, contents,
3289 					 symtab_hdr, sym_hashes,
3290 					 &r_type, GOT_UNKNOWN, rel,
3291 					 relend, h, r_symndx, TRUE))
3292 	    return FALSE;
3293 
3294 	  if (r_type != R_386_TLS_LDM)
3295 	    {
3296 	      /* LD->LE transition.  Change
3297 			leal foo@tlsldm(%ebx) %eax
3298 			call ___tls_get_addr@PLT
3299 		 into:
3300 			movl %gs:0, %eax
3301 			nop
3302 			leal 0(%esi,1), %esi
3303 		 or change
3304 			leal foo@tlsldm(%reg) %eax
3305 			call *___tls_get_addr@GOT(%reg)
3306 			which may be converted to
3307 			addr32 call ___tls_get_addr
3308 		 into:
3309 			movl %gs:0, %eax
3310 			leal 0(%esi), %esi  */
3311 	      BFD_ASSERT (r_type == R_386_TLS_LE_32);
3312 	      if (*(contents + rel->r_offset + 4) == 0xff
3313 		  || *(contents + rel->r_offset + 4) == 0x67)
3314 		memcpy (contents + rel->r_offset - 2,
3315 			"\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3316 	      else
3317 		memcpy (contents + rel->r_offset - 2,
3318 			"\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3319 	      /* Skip R_386_PC32/R_386_PLT32.  */
3320 	      rel++;
3321 	      wrel++;
3322 	      continue;
3323 	    }
3324 
3325 	  if (htab->elf.sgot == NULL)
3326 	    abort ();
3327 
3328 	  off = htab->tls_ld_or_ldm_got.offset;
3329 	  if (off & 1)
3330 	    off &= ~1;
3331 	  else
3332 	    {
3333 	      Elf_Internal_Rela outrel;
3334 
3335 	      if (htab->elf.srelgot == NULL)
3336 		abort ();
3337 
3338 	      outrel.r_offset = (htab->elf.sgot->output_section->vma
3339 				 + htab->elf.sgot->output_offset + off);
3340 
3341 	      bfd_put_32 (output_bfd, 0,
3342 			  htab->elf.sgot->contents + off);
3343 	      bfd_put_32 (output_bfd, 0,
3344 			  htab->elf.sgot->contents + off + 4);
3345 	      outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3346 	      elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3347 	      htab->tls_ld_or_ldm_got.offset |= 1;
3348 	    }
3349 	  relocation = htab->elf.sgot->output_section->vma
3350 		       + htab->elf.sgot->output_offset + off
3351 		       - htab->elf.sgotplt->output_section->vma
3352 		       - htab->elf.sgotplt->output_offset;
3353 	  unresolved_reloc = FALSE;
3354 	  break;
3355 
3356 	case R_386_TLS_LDO_32:
3357 	  if (!bfd_link_executable (info)
3358 	      || (input_section->flags & SEC_CODE) == 0)
3359 	    relocation -= _bfd_x86_elf_dtpoff_base (info);
3360 	  else
3361 	    /* When converting LDO to LE, we must negate.  */
3362 	    relocation = -elf_i386_tpoff (info, relocation);
3363 	  break;
3364 
3365 	case R_386_TLS_LE_32:
3366 	case R_386_TLS_LE:
3367 	  if (!bfd_link_executable (info))
3368 	    {
3369 	      Elf_Internal_Rela outrel;
3370 	      asection *sreloc;
3371 
3372 	      outrel.r_offset = rel->r_offset
3373 				+ input_section->output_section->vma
3374 				+ input_section->output_offset;
3375 	      if (h != NULL && h->dynindx != -1)
3376 		indx = h->dynindx;
3377 	      else
3378 		indx = 0;
3379 	      if (r_type == R_386_TLS_LE_32)
3380 		outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3381 	      else
3382 		outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3383 	      sreloc = elf_section_data (input_section)->sreloc;
3384 	      if (sreloc == NULL)
3385 		abort ();
3386 	      elf_append_rel (output_bfd, sreloc, &outrel);
3387 	      if (indx)
3388 		continue;
3389 	      else if (r_type == R_386_TLS_LE_32)
3390 		relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3391 	      else
3392 		relocation -= _bfd_x86_elf_dtpoff_base (info);
3393 	    }
3394 	  else if (r_type == R_386_TLS_LE_32)
3395 	    relocation = elf_i386_tpoff (info, relocation);
3396 	  else
3397 	    relocation = -elf_i386_tpoff (info, relocation);
3398 	  break;
3399 
3400 	default:
3401 	  break;
3402 	}
3403 
3404       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3405 	 because such sections are not SEC_ALLOC and thus ld.so will
3406 	 not process them.  */
3407       if (unresolved_reloc
3408 	  && !((input_section->flags & SEC_DEBUGGING) != 0
3409 	       && h->def_dynamic)
3410 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
3411 				      rel->r_offset) != (bfd_vma) -1)
3412 	{
3413 	  _bfd_error_handler
3414 	    /* xgettext:c-format */
3415 	    (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
3416 	     input_bfd,
3417 	     input_section,
3418 	     (uint64_t) rel->r_offset,
3419 	     howto->name,
3420 	     h->root.root.string);
3421 	  return FALSE;
3422 	}
3423 
3424 do_relocation:
3425       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3426 				    contents, rel->r_offset,
3427 				    relocation, 0);
3428 
3429 check_relocation_error:
3430       if (r != bfd_reloc_ok)
3431 	{
3432 	  const char *name;
3433 
3434 	  if (h != NULL)
3435 	    name = h->root.root.string;
3436 	  else
3437 	    {
3438 	      name = bfd_elf_string_from_elf_section (input_bfd,
3439 						      symtab_hdr->sh_link,
3440 						      sym->st_name);
3441 	      if (name == NULL)
3442 		return FALSE;
3443 	      if (*name == '\0')
3444 		name = bfd_section_name (input_bfd, sec);
3445 	    }
3446 
3447 	  if (r == bfd_reloc_overflow)
3448 	    (*info->callbacks->reloc_overflow)
3449 	      (info, (h ? &h->root : NULL), name, howto->name,
3450 	       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3451 	  else
3452 	    {
3453 	      _bfd_error_handler
3454 		/* xgettext:c-format */
3455 		(_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3456 		 input_bfd, input_section,
3457 		 (uint64_t) rel->r_offset, name, (int) r);
3458 	      return FALSE;
3459 	    }
3460 	}
3461 
3462       if (wrel != rel)
3463 	*wrel = *rel;
3464     }
3465 
3466   if (wrel != rel)
3467     {
3468       Elf_Internal_Shdr *rel_hdr;
3469       size_t deleted = rel - wrel;
3470 
3471       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3472       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3473       if (rel_hdr->sh_size == 0)
3474 	{
3475 	  /* It is too late to remove an empty reloc section.  Leave
3476 	     one NONE reloc.
3477 	     ??? What is wrong with an empty section???  */
3478 	  rel_hdr->sh_size = rel_hdr->sh_entsize;
3479 	  deleted -= 1;
3480 	}
3481       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3482       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3483       input_section->reloc_count -= deleted;
3484     }
3485 
3486   return TRUE;
3487 }
3488 
3489 /* Finish up dynamic symbol handling.  We set the contents of various
3490    dynamic sections here.  */
3491 
3492 static bfd_boolean
3493 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3494 				struct bfd_link_info *info,
3495 				struct elf_link_hash_entry *h,
3496 				Elf_Internal_Sym *sym)
3497 {
3498   struct elf_x86_link_hash_table *htab;
3499   unsigned plt_entry_size;
3500   struct elf_x86_link_hash_entry *eh;
3501   bfd_boolean local_undefweak;
3502   bfd_boolean use_plt_second;
3503 
3504   htab = elf_x86_hash_table (info, I386_ELF_DATA);
3505   if (htab == NULL)
3506     return FALSE;
3507 
3508   plt_entry_size = htab->plt.plt_entry_size;
3509 
3510   /* Use the second PLT section only if there is .plt section.  */
3511   use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3512 
3513   eh = (struct elf_x86_link_hash_entry *) h;
3514   if (eh->no_finish_dynamic_symbol)
3515     abort ();
3516 
3517   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3518      resolved undefined weak symbols in executable so that their
3519      references have value 0 at run-time.  */
3520   local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3521 
3522   if (h->plt.offset != (bfd_vma) -1)
3523     {
3524       bfd_vma plt_index, plt_offset;
3525       bfd_vma got_offset;
3526       Elf_Internal_Rela rel;
3527       bfd_byte *loc;
3528       asection *plt, *resolved_plt, *gotplt, *relplt;
3529 
3530       /* When building a static executable, use .iplt, .igot.plt and
3531 	 .rel.iplt sections for STT_GNU_IFUNC symbols.  */
3532       if (htab->elf.splt != NULL)
3533 	{
3534 	  plt = htab->elf.splt;
3535 	  gotplt = htab->elf.sgotplt;
3536 	  relplt = htab->elf.srelplt;
3537 	}
3538       else
3539 	{
3540 	  plt = htab->elf.iplt;
3541 	  gotplt = htab->elf.igotplt;
3542 	  relplt = htab->elf.irelplt;
3543 	}
3544 
3545       VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3546 
3547       /* Get the index in the procedure linkage table which
3548 	 corresponds to this symbol.  This is the index of this symbol
3549 	 in all the symbols for which we are making plt entries.  The
3550 	 first entry in the procedure linkage table is reserved.
3551 
3552 	 Get the offset into the .got table of the entry that
3553 	 corresponds to this function.  Each .got entry is 4 bytes.
3554 	 The first three are reserved.
3555 
3556 	 For static executables, we don't reserve anything.  */
3557 
3558       if (plt == htab->elf.splt)
3559 	{
3560 	  got_offset = (h->plt.offset / plt_entry_size
3561 			- htab->plt.has_plt0);
3562 	  got_offset = (got_offset + 3) * 4;
3563 	}
3564       else
3565 	{
3566 	  got_offset = h->plt.offset / plt_entry_size;
3567 	  got_offset = got_offset * 4;
3568 	}
3569 
3570       /* Fill in the entry in the procedure linkage table and update
3571 	 the first slot.  */
3572       memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3573 	      plt_entry_size);
3574 
3575       if (use_plt_second)
3576 	{
3577 	  const bfd_byte *plt_entry;
3578 	  if (bfd_link_pic (info))
3579 	    plt_entry = htab->non_lazy_plt->pic_plt_entry;
3580 	  else
3581 	    plt_entry = htab->non_lazy_plt->plt_entry;
3582 	  memcpy (htab->plt_second->contents + eh->plt_second.offset,
3583 		  plt_entry, htab->non_lazy_plt->plt_entry_size);
3584 
3585 	  resolved_plt = htab->plt_second;
3586 	  plt_offset = eh->plt_second.offset;
3587 	}
3588       else
3589 	{
3590 	  resolved_plt = plt;
3591 	  plt_offset = h->plt.offset;
3592 	}
3593 
3594       if (! bfd_link_pic (info))
3595 	{
3596 	  bfd_put_32 (output_bfd,
3597 		      (gotplt->output_section->vma
3598 		       + gotplt->output_offset
3599 		       + got_offset),
3600 		      resolved_plt->contents + plt_offset
3601 		      + htab->plt.plt_got_offset);
3602 
3603 	  if (htab->target_os == is_vxworks)
3604 	    {
3605 	      int s, k, reloc_index;
3606 
3607 	      /* Create the R_386_32 relocation referencing the GOT
3608 		 for this PLT entry.  */
3609 
3610 	      /* S: Current slot number (zero-based).  */
3611 	      s = ((h->plt.offset - htab->plt.plt_entry_size)
3612 		   / htab->plt.plt_entry_size);
3613 	      /* K: Number of relocations for PLTResolve. */
3614 	      if (bfd_link_pic (info))
3615 		k = PLTRESOLVE_RELOCS_SHLIB;
3616 	      else
3617 		k = PLTRESOLVE_RELOCS;
3618 	      /* Skip the PLTresolve relocations, and the relocations for
3619 		 the other PLT slots. */
3620 	      reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3621 	      loc = (htab->srelplt2->contents + reloc_index
3622 		     * sizeof (Elf32_External_Rel));
3623 
3624 	      rel.r_offset = (plt->output_section->vma
3625 			      + plt->output_offset
3626 			      + h->plt.offset + 2),
3627 	      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3628 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3629 
3630 	      /* Create the R_386_32 relocation referencing the beginning of
3631 		 the PLT for this GOT entry.  */
3632 	      rel.r_offset = (htab->elf.sgotplt->output_section->vma
3633 			      + htab->elf.sgotplt->output_offset
3634 			      + got_offset);
3635 	      rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3636 	      bfd_elf32_swap_reloc_out (output_bfd, &rel,
3637 					loc + sizeof (Elf32_External_Rel));
3638 	    }
3639 	}
3640       else
3641 	{
3642 	  bfd_put_32 (output_bfd, got_offset,
3643 		      resolved_plt->contents + plt_offset
3644 		      + htab->plt.plt_got_offset);
3645 	}
3646 
3647       /* Fill in the entry in the global offset table.  Leave the entry
3648 	 as zero for undefined weak symbol in PIE.  No PLT relocation
3649 	 against undefined weak symbol in PIE.  */
3650       if (!local_undefweak)
3651 	{
3652 	  if (htab->plt.has_plt0)
3653 	    bfd_put_32 (output_bfd,
3654 			(plt->output_section->vma
3655 			 + plt->output_offset
3656 			 + h->plt.offset
3657 			 + htab->lazy_plt->plt_lazy_offset),
3658 			gotplt->contents + got_offset);
3659 
3660 	  /* Fill in the entry in the .rel.plt section.  */
3661 	  rel.r_offset = (gotplt->output_section->vma
3662 			  + gotplt->output_offset
3663 			  + got_offset);
3664 	  if (PLT_LOCAL_IFUNC_P (info, h))
3665 	    {
3666 	      info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3667 				      h->root.root.string,
3668 				      h->root.u.def.section->owner);
3669 
3670 	      /* If an STT_GNU_IFUNC symbol is locally defined, generate
3671 		 R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
3672 		 in the .got.plt section.  */
3673 	      bfd_put_32 (output_bfd,
3674 			  (h->root.u.def.value
3675 			   + h->root.u.def.section->output_section->vma
3676 			   + h->root.u.def.section->output_offset),
3677 			  gotplt->contents + got_offset);
3678 	      rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3679 	      /* R_386_IRELATIVE comes last.  */
3680 	      plt_index = htab->next_irelative_index--;
3681 	    }
3682 	  else
3683 	    {
3684 	      rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3685 	      plt_index = htab->next_jump_slot_index++;
3686 	    }
3687 
3688 	  loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3689 	  bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3690 
3691 	  /* Don't fill the second and third slots in PLT entry for
3692 	     static executables nor without PLT0.  */
3693 	  if (plt == htab->elf.splt && htab->plt.has_plt0)
3694 	    {
3695 	      bfd_put_32 (output_bfd,
3696 			  plt_index * sizeof (Elf32_External_Rel),
3697 			  plt->contents + h->plt.offset
3698 			  + htab->lazy_plt->plt_reloc_offset);
3699 	      bfd_put_32 (output_bfd,
3700 			  - (h->plt.offset
3701 			     + htab->lazy_plt->plt_plt_offset + 4),
3702 			  (plt->contents + h->plt.offset
3703 			   + htab->lazy_plt->plt_plt_offset));
3704 	    }
3705 	}
3706     }
3707   else if (eh->plt_got.offset != (bfd_vma) -1)
3708     {
3709       bfd_vma got_offset, plt_offset;
3710       asection *plt, *got, *gotplt;
3711       const bfd_byte *got_plt_entry;
3712 
3713       /* Set the entry in the GOT procedure linkage table.  */
3714       plt = htab->plt_got;
3715       got = htab->elf.sgot;
3716       gotplt = htab->elf.sgotplt;
3717       got_offset = h->got.offset;
3718 
3719       if (got_offset == (bfd_vma) -1
3720 	  || plt == NULL
3721 	  || got == NULL
3722 	  || gotplt == NULL)
3723 	abort ();
3724 
3725       /* Fill in the entry in the GOT procedure linkage table.  */
3726       if (! bfd_link_pic (info))
3727 	{
3728 	  got_plt_entry = htab->non_lazy_plt->plt_entry;
3729 	  got_offset += got->output_section->vma + got->output_offset;
3730 	}
3731       else
3732 	{
3733 	  got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3734 	  got_offset += (got->output_section->vma
3735 			 + got->output_offset
3736 			 - gotplt->output_section->vma
3737 			 - gotplt->output_offset);
3738 	}
3739 
3740       plt_offset = eh->plt_got.offset;
3741       memcpy (plt->contents + plt_offset, got_plt_entry,
3742 	      htab->non_lazy_plt->plt_entry_size);
3743       bfd_put_32 (output_bfd, got_offset,
3744 		  (plt->contents + plt_offset
3745 		   + htab->non_lazy_plt->plt_got_offset));
3746     }
3747 
3748   if (!local_undefweak
3749       && !h->def_regular
3750       && (h->plt.offset != (bfd_vma) -1
3751 	  || eh->plt_got.offset != (bfd_vma) -1))
3752     {
3753       /* Mark the symbol as undefined, rather than as defined in
3754 	 the .plt section.  Leave the value if there were any
3755 	 relocations where pointer equality matters (this is a clue
3756 	 for the dynamic linker, to make function pointer
3757 	 comparisons work between an application and shared
3758 	 library), otherwise set it to zero.  If a function is only
3759 	 called from a binary, there is no need to slow down
3760 	 shared libraries because of that.  */
3761       sym->st_shndx = SHN_UNDEF;
3762       if (!h->pointer_equality_needed)
3763 	sym->st_value = 0;
3764     }
3765 
3766   _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3767 
3768   /* Don't generate dynamic GOT relocation against undefined weak
3769      symbol in executable.  */
3770   if (h->got.offset != (bfd_vma) -1
3771       && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3772       && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3773       && !local_undefweak)
3774     {
3775       Elf_Internal_Rela rel;
3776       asection *relgot = htab->elf.srelgot;
3777 
3778       /* This symbol has an entry in the global offset table.  Set it
3779 	 up.  */
3780 
3781       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3782 	abort ();
3783 
3784       rel.r_offset = (htab->elf.sgot->output_section->vma
3785 		      + htab->elf.sgot->output_offset
3786 		      + (h->got.offset & ~(bfd_vma) 1));
3787 
3788       /* If this is a static link, or it is a -Bsymbolic link and the
3789 	 symbol is defined locally or was forced to be local because
3790 	 of a version file, we just want to emit a RELATIVE reloc.
3791 	 The entry in the global offset table will already have been
3792 	 initialized in the relocate_section function.  */
3793       if (h->def_regular
3794 	  && h->type == STT_GNU_IFUNC)
3795 	{
3796 	  if (h->plt.offset == (bfd_vma) -1)
3797 	    {
3798 	      /* STT_GNU_IFUNC is referenced without PLT.  */
3799 	      if (htab->elf.splt == NULL)
3800 		{
3801 		  /* use .rel[a].iplt section to store .got relocations
3802 		     in static executable.  */
3803 		  relgot = htab->elf.irelplt;
3804 		}
3805 	      if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3806 		{
3807 		  info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3808 					  h->root.root.string,
3809 					  h->root.u.def.section->owner);
3810 
3811 		  bfd_put_32 (output_bfd,
3812 			      (h->root.u.def.value
3813 			       + h->root.u.def.section->output_section->vma
3814 			       + h->root.u.def.section->output_offset),
3815 			      htab->elf.sgot->contents + h->got.offset);
3816 		  rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3817 		}
3818 	      else
3819 		goto do_glob_dat;
3820 	    }
3821 	  else if (bfd_link_pic (info))
3822 	    {
3823 	      /* Generate R_386_GLOB_DAT.  */
3824 	      goto do_glob_dat;
3825 	    }
3826 	  else
3827 	    {
3828 	      asection *plt;
3829 	      bfd_vma plt_offset;
3830 
3831 	      if (!h->pointer_equality_needed)
3832 		abort ();
3833 
3834 	      /* For non-shared object, we can't use .got.plt, which
3835 		 contains the real function addres if we need pointer
3836 		 equality.  We load the GOT entry with the PLT entry.  */
3837 	      if (htab->plt_second != NULL)
3838 		{
3839 		  plt = htab->plt_second;
3840 		  plt_offset = eh->plt_second.offset;
3841 		}
3842 	      else
3843 		{
3844 		  plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3845 		  plt_offset = h->plt.offset;
3846 		}
3847 	      bfd_put_32 (output_bfd,
3848 			  (plt->output_section->vma
3849 			   + plt->output_offset + plt_offset),
3850 			  htab->elf.sgot->contents + h->got.offset);
3851 	      return TRUE;
3852 	    }
3853 	}
3854       else if (bfd_link_pic (info)
3855 	       && SYMBOL_REFERENCES_LOCAL_P (info, h))
3856 	{
3857 	  BFD_ASSERT((h->got.offset & 1) != 0);
3858 	  rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3859 	}
3860       else
3861 	{
3862 	  BFD_ASSERT((h->got.offset & 1) == 0);
3863 do_glob_dat:
3864 	  bfd_put_32 (output_bfd, (bfd_vma) 0,
3865 		      htab->elf.sgot->contents + h->got.offset);
3866 	  rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3867 	}
3868 
3869       elf_append_rel (output_bfd, relgot, &rel);
3870     }
3871 
3872   if (h->needs_copy)
3873     {
3874       Elf_Internal_Rela rel;
3875       asection *s;
3876 
3877       /* This symbol needs a copy reloc.  Set it up.  */
3878       VERIFY_COPY_RELOC (h, htab)
3879 
3880       rel.r_offset = (h->root.u.def.value
3881 		      + h->root.u.def.section->output_section->vma
3882 		      + h->root.u.def.section->output_offset);
3883       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3884       if (h->root.u.def.section == htab->elf.sdynrelro)
3885 	s = htab->elf.sreldynrelro;
3886       else
3887 	s = htab->elf.srelbss;
3888       elf_append_rel (output_bfd, s, &rel);
3889     }
3890 
3891   return TRUE;
3892 }
3893 
3894 /* Finish up local dynamic symbol handling.  We set the contents of
3895    various dynamic sections here.  */
3896 
3897 static bfd_boolean
3898 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
3899 {
3900   struct elf_link_hash_entry *h
3901     = (struct elf_link_hash_entry *) *slot;
3902   struct bfd_link_info *info
3903     = (struct bfd_link_info *) inf;
3904 
3905   return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
3906 					 h, NULL);
3907 }
3908 
3909 /* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
3910    here since undefined weak symbol may not be dynamic and may not be
3911    called for elf_i386_finish_dynamic_symbol.  */
3912 
3913 static bfd_boolean
3914 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
3915 				      void *inf)
3916 {
3917   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
3918   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3919 
3920   if (h->root.type != bfd_link_hash_undefweak
3921       || h->dynindx != -1)
3922     return TRUE;
3923 
3924   return elf_i386_finish_dynamic_symbol (info->output_bfd,
3925 					 info, h, NULL);
3926 }
3927 
3928 /* Used to decide how to sort relocs in an optimal manner for the
3929    dynamic linker, before writing them out.  */
3930 
3931 static enum elf_reloc_type_class
3932 elf_i386_reloc_type_class (const struct bfd_link_info *info,
3933 			   const asection *rel_sec ATTRIBUTE_UNUSED,
3934 			   const Elf_Internal_Rela *rela)
3935 {
3936   bfd *abfd = info->output_bfd;
3937   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3938   struct elf_link_hash_table *htab = elf_hash_table (info);
3939 
3940   if (htab->dynsym != NULL
3941       && htab->dynsym->contents != NULL)
3942     {
3943       /* Check relocation against STT_GNU_IFUNC symbol if there are
3944 	 dynamic symbols.  */
3945       unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
3946       if (r_symndx != STN_UNDEF)
3947 	{
3948 	  Elf_Internal_Sym sym;
3949 	  if (!bed->s->swap_symbol_in (abfd,
3950 				       (htab->dynsym->contents
3951 					+ r_symndx * sizeof (Elf32_External_Sym)),
3952 				       0, &sym))
3953 	    abort ();
3954 
3955 	  if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3956 	    return reloc_class_ifunc;
3957 	}
3958     }
3959 
3960   switch (ELF32_R_TYPE (rela->r_info))
3961     {
3962     case R_386_IRELATIVE:
3963       return reloc_class_ifunc;
3964     case R_386_RELATIVE:
3965       return reloc_class_relative;
3966     case R_386_JUMP_SLOT:
3967       return reloc_class_plt;
3968     case R_386_COPY:
3969       return reloc_class_copy;
3970     default:
3971       return reloc_class_normal;
3972     }
3973 }
3974 
3975 /* Finish up the dynamic sections.  */
3976 
3977 static bfd_boolean
3978 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3979 				  struct bfd_link_info *info)
3980 {
3981   struct elf_x86_link_hash_table *htab;
3982 
3983   htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
3984   if (htab == NULL)
3985     return FALSE;
3986 
3987   if (!htab->elf.dynamic_sections_created)
3988     return TRUE;
3989 
3990   if (htab->elf.splt && htab->elf.splt->size > 0)
3991     {
3992       /* UnixWare sets the entsize of .plt to 4, although that doesn't
3993 	 really seem like the right value.  */
3994       elf_section_data (htab->elf.splt->output_section)
3995 	->this_hdr.sh_entsize = 4;
3996 
3997       if (htab->plt.has_plt0)
3998 	{
3999 	  /* Fill in the special first entry in the procedure linkage
4000 	     table.  */
4001 	  memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4002 		  htab->lazy_plt->plt0_entry_size);
4003 	  memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4004 		  htab->plt0_pad_byte,
4005 		  htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4006 	  if (!bfd_link_pic (info))
4007 	    {
4008 	      bfd_put_32 (output_bfd,
4009 			  (htab->elf.sgotplt->output_section->vma
4010 			   + htab->elf.sgotplt->output_offset
4011 			   + 4),
4012 			  htab->elf.splt->contents
4013 			  + htab->lazy_plt->plt0_got1_offset);
4014 	      bfd_put_32 (output_bfd,
4015 			  (htab->elf.sgotplt->output_section->vma
4016 			   + htab->elf.sgotplt->output_offset
4017 			   + 8),
4018 			  htab->elf.splt->contents
4019 			  + htab->lazy_plt->plt0_got2_offset);
4020 
4021 	      if (htab->target_os == is_vxworks)
4022 		{
4023 		  Elf_Internal_Rela rel;
4024 		  int num_plts = (htab->elf.splt->size
4025 				  / htab->plt.plt_entry_size) - 1;
4026 		  unsigned char *p;
4027 		  asection *srelplt2 = htab->srelplt2;
4028 
4029 		  /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4030 		     + 4.  On IA32 we use REL relocations so the
4031 		     addend goes in the PLT directly.  */
4032 		  rel.r_offset = (htab->elf.splt->output_section->vma
4033 				  + htab->elf.splt->output_offset
4034 				  + htab->lazy_plt->plt0_got1_offset);
4035 		  rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4036 					     R_386_32);
4037 		  bfd_elf32_swap_reloc_out (output_bfd, &rel,
4038 					    srelplt2->contents);
4039 		  /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4040 		     + 8.  */
4041 		  rel.r_offset = (htab->elf.splt->output_section->vma
4042 				  + htab->elf.splt->output_offset
4043 				  + htab->lazy_plt->plt0_got2_offset);
4044 		  rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4045 					     R_386_32);
4046 		  bfd_elf32_swap_reloc_out (output_bfd, &rel,
4047 					    srelplt2->contents +
4048 					    sizeof (Elf32_External_Rel));
4049 		  /* Correct the .rel.plt.unloaded relocations.  */
4050 		  p = srelplt2->contents;
4051 		  if (bfd_link_pic (info))
4052 		    p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4053 		  else
4054 		    p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4055 
4056 		  for (; num_plts; num_plts--)
4057 		    {
4058 		      bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4059 		      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4060 						 R_386_32);
4061 		      bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4062 		      p += sizeof (Elf32_External_Rel);
4063 
4064 		      bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4065 		      rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4066 						 R_386_32);
4067 		      bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4068 		      p += sizeof (Elf32_External_Rel);
4069 		    }
4070 		}
4071 	    }
4072 	}
4073     }
4074 
4075   /* Fill PLT entries for undefined weak symbols in PIE.  */
4076   if (bfd_link_pie (info))
4077     bfd_hash_traverse (&info->hash->table,
4078 		       elf_i386_pie_finish_undefweak_symbol,
4079 		       info);
4080 
4081   return TRUE;
4082 }
4083 
4084 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4085    STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4086    It has to be done before elf_link_sort_relocs is called so that
4087    dynamic relocations are properly sorted.  */
4088 
4089 static bfd_boolean
4090 elf_i386_output_arch_local_syms
4091   (bfd *output_bfd ATTRIBUTE_UNUSED,
4092    struct bfd_link_info *info,
4093    void *flaginfo ATTRIBUTE_UNUSED,
4094    int (*func) (void *, const char *,
4095 		Elf_Internal_Sym *,
4096 		asection *,
4097 		struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4098 {
4099   struct elf_x86_link_hash_table *htab
4100     = elf_x86_hash_table (info, I386_ELF_DATA);
4101   if (htab == NULL)
4102     return FALSE;
4103 
4104   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4105   htab_traverse (htab->loc_hash_table,
4106 		 elf_i386_finish_local_dynamic_symbol,
4107 		 info);
4108 
4109   return TRUE;
4110 }
4111 
4112 /* Forward declaration.  */
4113 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
4114 
4115 /* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
4116    dynamic relocations.   */
4117 
4118 static long
4119 elf_i386_get_synthetic_symtab (bfd *abfd,
4120 			       long symcount ATTRIBUTE_UNUSED,
4121 			       asymbol **syms ATTRIBUTE_UNUSED,
4122 			       long dynsymcount,
4123 			       asymbol **dynsyms,
4124 			       asymbol **ret)
4125 {
4126   long count, i, n;
4127   int j;
4128   bfd_byte *plt_contents;
4129   long relsize;
4130   const struct elf_x86_lazy_plt_layout *lazy_plt;
4131   const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4132   const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4133   const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4134   asection *plt;
4135   bfd_vma got_addr;
4136   enum elf_x86_plt_type plt_type;
4137   struct elf_x86_plt plts[] =
4138     {
4139       { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4140       { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4141       { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4142       { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4143     };
4144 
4145   *ret = NULL;
4146 
4147   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4148     return 0;
4149 
4150   if (dynsymcount <= 0)
4151     return 0;
4152 
4153   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4154   if (relsize <= 0)
4155     return -1;
4156 
4157   non_lazy_plt = NULL;
4158   /* Silence GCC 6.  */
4159   lazy_plt = NULL;
4160   non_lazy_ibt_plt = NULL;
4161   lazy_ibt_plt = NULL;
4162   switch (get_elf_x86_backend_data (abfd)->target_os)
4163     {
4164     case is_normal:
4165     case is_solaris:
4166       non_lazy_plt = &elf_i386_non_lazy_plt;
4167       lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4168       non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4169       /* Fall through */
4170     case is_vxworks:
4171       lazy_plt = &elf_i386_lazy_plt;
4172       break;
4173     case is_nacl:
4174       lazy_plt = &elf_i386_nacl_plt;
4175       break;
4176     }
4177 
4178   got_addr = 0;
4179 
4180   count = 0;
4181   for (j = 0; plts[j].name != NULL; j++)
4182     {
4183       plt = bfd_get_section_by_name (abfd, plts[j].name);
4184       if (plt == NULL || plt->size == 0)
4185 	continue;
4186 
4187       /* Get the PLT section contents.  */
4188       plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4189       if (plt_contents == NULL)
4190 	break;
4191       if (!bfd_get_section_contents (abfd, (asection *) plt,
4192 				     plt_contents, 0, plt->size))
4193 	{
4194 	  free (plt_contents);
4195 	  break;
4196 	}
4197 
4198       /* Check what kind of PLT it is.  */
4199       plt_type = plt_unknown;
4200       if (plts[j].type == plt_unknown
4201 	  && (plt->size >= (lazy_plt->plt0_entry_size
4202 			    + lazy_plt->plt_entry_size)))
4203 	{
4204 	  /* Match lazy PLT first.  */
4205 	  if (memcmp (plt_contents, lazy_plt->plt0_entry,
4206 		      lazy_plt->plt0_got1_offset) == 0)
4207 	    {
4208 	      /* The fist entry in the lazy IBT PLT is the same as the
4209 		 normal lazy PLT.  */
4210 	      if (lazy_ibt_plt != NULL
4211 		  && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4212 			      lazy_ibt_plt->plt_entry,
4213 			      lazy_ibt_plt->plt_got_offset) == 0))
4214 		plt_type = plt_lazy | plt_second;
4215 	      else
4216 		plt_type = plt_lazy;
4217 	    }
4218 	  else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4219 			   lazy_plt->plt0_got1_offset) == 0)
4220 	    {
4221 	      /* The fist entry in the PIC lazy IBT PLT is the same as
4222 		 the normal PIC lazy PLT.  */
4223 	      if (lazy_ibt_plt != NULL
4224 		  && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4225 			      lazy_ibt_plt->pic_plt_entry,
4226 			      lazy_ibt_plt->plt_got_offset) == 0))
4227 		plt_type = plt_lazy | plt_pic | plt_second;
4228 	      else
4229 		plt_type = plt_lazy | plt_pic;
4230 	    }
4231 	}
4232 
4233       if (non_lazy_plt != NULL
4234 	  && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4235 	  && plt->size >= non_lazy_plt->plt_entry_size)
4236 	{
4237 	  /* Match non-lazy PLT.  */
4238 	  if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4239 		      non_lazy_plt->plt_got_offset) == 0)
4240 	    plt_type = plt_non_lazy;
4241 	  else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4242 			   non_lazy_plt->plt_got_offset) == 0)
4243 	    plt_type = plt_pic;
4244 	}
4245 
4246       if ((non_lazy_ibt_plt != NULL)
4247 	  && (plt_type == plt_unknown || plt_type == plt_second)
4248 	  && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4249 	{
4250 	  if (memcmp (plt_contents,
4251 		      non_lazy_ibt_plt->plt_entry,
4252 		      non_lazy_ibt_plt->plt_got_offset) == 0)
4253 	    {
4254 	      /* Match IBT PLT.  */
4255 	      plt_type = plt_second;
4256 	      non_lazy_plt = non_lazy_ibt_plt;
4257 	    }
4258 	  else if (memcmp (plt_contents,
4259 			   non_lazy_ibt_plt->pic_plt_entry,
4260 			   non_lazy_ibt_plt->plt_got_offset) == 0)
4261 	    {
4262 	      /* Match PIC IBT PLT.  */
4263 	      plt_type = plt_second | plt_pic;
4264 	      non_lazy_plt = non_lazy_ibt_plt;
4265 	    }
4266 	}
4267 
4268       if (plt_type == plt_unknown)
4269 	{
4270 	  free (plt_contents);
4271 	  continue;
4272 	}
4273 
4274       plts[j].sec = plt;
4275       plts[j].type = plt_type;
4276 
4277       if ((plt_type & plt_lazy))
4278 	{
4279 	  plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4280 	  plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4281 	  /* Skip PLT0 in lazy PLT.  */
4282 	  i = 1;
4283 	}
4284       else
4285 	{
4286 	  plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4287 	  plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4288 	  i = 0;
4289 	}
4290 
4291       /* Skip lazy PLT when the second PLT is used.  */
4292       if ((plt_type & (plt_lazy | plt_second))
4293 	  == (plt_lazy | plt_second))
4294 	plts[j].count = 0;
4295       else
4296 	{
4297 	  n = plt->size / plts[j].plt_entry_size;
4298 	  plts[j].count = n;
4299 	  count += n - i;
4300 	}
4301 
4302       plts[j].contents = plt_contents;
4303 
4304       /* The _GLOBAL_OFFSET_TABLE_ address is needed.  */
4305       if ((plt_type & plt_pic))
4306 	got_addr = (bfd_vma) -1;
4307     }
4308 
4309   return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4310 					    got_addr, plts, dynsyms,
4311 					    ret);
4312 }
4313 
4314 /* Set up i386 GNU properties.  Return the first relocatable ELF input
4315    with GNU properties if found.  Otherwise, return NULL.  */
4316 
4317 static bfd *
4318 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4319 {
4320   struct elf_x86_init_table init_table;
4321 
4322   switch (get_elf_x86_backend_data (info->output_bfd)->target_os)
4323     {
4324     case is_normal:
4325     case is_solaris:
4326       init_table.plt0_pad_byte = 0x0;
4327       init_table.lazy_plt = &elf_i386_lazy_plt;
4328       init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4329       init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4330       init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4331       break;
4332     case is_vxworks:
4333       init_table.plt0_pad_byte = 0x90;
4334       init_table.lazy_plt = &elf_i386_lazy_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     case is_nacl:
4340       init_table.plt0_pad_byte = 0x90;
4341       init_table.lazy_plt = &elf_i386_nacl_plt;
4342       init_table.non_lazy_plt = NULL;
4343       init_table.lazy_ibt_plt = NULL;
4344       init_table.non_lazy_ibt_plt = NULL;
4345       break;
4346     }
4347 
4348   init_table.r_info = elf32_r_info;
4349   init_table.r_sym = elf32_r_sym;
4350 
4351   return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4352 }
4353 
4354 #define TARGET_LITTLE_SYM		i386_elf32_vec
4355 #define TARGET_LITTLE_NAME		"elf32-i386"
4356 #define ELF_ARCH			bfd_arch_i386
4357 #define ELF_TARGET_ID			I386_ELF_DATA
4358 #define ELF_MACHINE_CODE		EM_386
4359 #define ELF_MAXPAGESIZE			0x1000
4360 
4361 #define elf_backend_can_gc_sections	1
4362 #define elf_backend_can_refcount	1
4363 #define elf_backend_want_got_plt	1
4364 #define elf_backend_plt_readonly	1
4365 #define elf_backend_want_plt_sym	0
4366 #define elf_backend_got_header_size	12
4367 #define elf_backend_plt_alignment	4
4368 #define elf_backend_dtrel_excludes_plt	1
4369 #define elf_backend_extern_protected_data 1
4370 #define elf_backend_caches_rawsize	1
4371 #define elf_backend_want_dynrelro	1
4372 
4373 /* Support RELA for objdump of prelink objects.  */
4374 #define elf_info_to_howto		      elf_i386_info_to_howto_rel
4375 #define elf_info_to_howto_rel		      elf_i386_info_to_howto_rel
4376 
4377 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4378 #define bfd_elf32_bfd_reloc_type_lookup	      elf_i386_reloc_type_lookup
4379 #define bfd_elf32_bfd_reloc_name_lookup	      elf_i386_reloc_name_lookup
4380 #define bfd_elf32_get_synthetic_symtab	      elf_i386_get_synthetic_symtab
4381 
4382 #define elf_backend_relocs_compatible	      _bfd_elf_relocs_compatible
4383 #define elf_backend_check_relocs	      elf_i386_check_relocs
4384 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
4385 #define elf_backend_fake_sections	      elf_i386_fake_sections
4386 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4387 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4388 #define elf_backend_output_arch_local_syms     elf_i386_output_arch_local_syms
4389 #define elf_backend_grok_prstatus	      elf_i386_grok_prstatus
4390 #define elf_backend_grok_psinfo		      elf_i386_grok_psinfo
4391 #define elf_backend_reloc_type_class	      elf_i386_reloc_type_class
4392 #define elf_backend_relocate_section	      elf_i386_relocate_section
4393 #define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
4394 #define elf_backend_hide_symbol		      _bfd_x86_elf_hide_symbol
4395 
4396 #define elf_backend_linux_prpsinfo32_ugid16	TRUE
4397 
4398 #define	elf32_bed			      elf32_i386_bed
4399 
4400 #include "elf32-target.h"
4401 
4402 /* FreeBSD support.  */
4403 
4404 #undef	TARGET_LITTLE_SYM
4405 #define	TARGET_LITTLE_SYM		i386_elf32_fbsd_vec
4406 #undef	TARGET_LITTLE_NAME
4407 #define	TARGET_LITTLE_NAME		"elf32-i386-freebsd"
4408 #undef	ELF_OSABI
4409 #define	ELF_OSABI			ELFOSABI_FREEBSD
4410 
4411 /* The kernel recognizes executables as valid only if they carry a
4412    "FreeBSD" label in the ELF header.  So we put this label on all
4413    executables and (for simplicity) also all other object files.  */
4414 
4415 static void
4416 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4417 {
4418   _bfd_elf_post_process_headers (abfd, info);
4419 
4420 #ifdef OLD_FREEBSD_ABI_LABEL
4421   {
4422     /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4423     Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4424     memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4425   }
4426 #endif
4427 }
4428 
4429 #undef	elf_backend_post_process_headers
4430 #define	elf_backend_post_process_headers	elf_i386_fbsd_post_process_headers
4431 #undef	elf32_bed
4432 #define	elf32_bed				elf32_i386_fbsd_bed
4433 
4434 #undef elf_backend_add_symbol_hook
4435 
4436 #include "elf32-target.h"
4437 
4438 /* Solaris 2.  */
4439 
4440 #undef	TARGET_LITTLE_SYM
4441 #define	TARGET_LITTLE_SYM		i386_elf32_sol2_vec
4442 #undef	TARGET_LITTLE_NAME
4443 #define	TARGET_LITTLE_NAME		"elf32-i386-sol2"
4444 
4445 static const struct elf_x86_backend_data elf_i386_solaris_arch_bed =
4446   {
4447     is_solaris				/* os */
4448   };
4449 
4450 #undef	elf_backend_arch_data
4451 #define	elf_backend_arch_data		&elf_i386_solaris_arch_bed
4452 
4453 #undef elf_backend_post_process_headers
4454 
4455 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4456    objects won't be recognized.  */
4457 #undef ELF_OSABI
4458 
4459 #undef	elf32_bed
4460 #define	elf32_bed			elf32_i386_sol2_bed
4461 
4462 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4463    boundary.  */
4464 #undef  elf_backend_static_tls_alignment
4465 #define elf_backend_static_tls_alignment 8
4466 
4467 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4468 
4469    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4470    File, p.63.  */
4471 #undef  elf_backend_want_plt_sym
4472 #define elf_backend_want_plt_sym	1
4473 
4474 #undef  elf_backend_strtab_flags
4475 #define elf_backend_strtab_flags	SHF_STRINGS
4476 
4477 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4478    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised
4479    FALSE otherwise.  ISECTION is the best guess matching section from the
4480    input bfd IBFD, but it might be NULL.  */
4481 
4482 static bfd_boolean
4483 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4484 						bfd *obfd ATTRIBUTE_UNUSED,
4485 						const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4486 						Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4487 {
4488   /* PR 19938: FIXME: Need to add code for setting the sh_info
4489      and sh_link fields of Solaris specific section types.  */
4490   return FALSE;
4491 
4492   /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4493      Object File Format, Table 13-9  ELF sh_link and sh_info Interpretation:
4494 
4495 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4496 
4497      The following values should be set:
4498 
4499 Type		     Link			    Info
4500 -----------------------------------------------------------------------------
4501 SHT_SUNW_ancillary   The section header index of    0
4502  [0x6fffffee]	     the associated string table.
4503 
4504 SHT_SUNW_capinfo     The section header index of    For a dynamic object, the
4505  [0x6ffffff0]	     the associated symbol table.   section header index of
4506 						    the associated
4507 						    SHT_SUNW_capchain table,
4508 						    otherwise 0.
4509 
4510 SHT_SUNW_symsort     The section header index of    0
4511  [0x6ffffff1]	     the associated symbol table.
4512 
4513 SHT_SUNW_tlssort     The section header index of    0
4514  [0x6ffffff2]	     the associated symbol table.
4515 
4516 SHT_SUNW_LDYNSYM     The section header index of    One greater than the
4517  [0x6ffffff3]	     the associated string table.   symbol table index of the
4518 		     This index is the same string  last local symbol,
4519 		     table used by the SHT_DYNSYM   STB_LOCAL. Since
4520 		     section.			    SHT_SUNW_LDYNSYM only
4521 						    contains local symbols,
4522 						    sh_info is equivalent to
4523 						    the number of symbols in
4524 						    the table.
4525 
4526 SHT_SUNW_cap	     If symbol capabilities exist,  If any capabilities refer
4527  [0x6ffffff5]	     the section header index of    to named strings, the
4528 		     the associated		    section header index of
4529 		     SHT_SUNW_capinfo table,	    the associated string
4530 			  otherwise 0.		    table, otherwise 0.
4531 
4532 SHT_SUNW_move	     The section header index of    0
4533  [0x6ffffffa]	     the associated symbol table.
4534 
4535 SHT_SUNW_COMDAT	     0				    0
4536  [0x6ffffffb]
4537 
4538 SHT_SUNW_syminfo     The section header index of    The section header index
4539  [0x6ffffffc]	     the associated symbol table.   of the associated
4540 						    .dynamic section.
4541 
4542 SHT_SUNW_verdef	     The section header index of    The number of version
4543  [0x6ffffffd]	     the associated string table.   definitions within the
4544 						    section.
4545 
4546 SHT_SUNW_verneed     The section header index of    The number of version
4547  [0x6ffffffe]	     the associated string table.   dependencies within the
4548 						    section.
4549 
4550 SHT_SUNW_versym	     The section header index of    0
4551  [0x6fffffff]	     the associated symbol table.  */
4552 }
4553 
4554 #undef  elf_backend_copy_special_section_fields
4555 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4556 
4557 #include "elf32-target.h"
4558 
4559 /* Intel MCU support.  */
4560 
4561 static bfd_boolean
4562 elf32_iamcu_elf_object_p (bfd *abfd)
4563 {
4564   /* Set the right machine number for an IAMCU elf32 file.  */
4565   bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4566   return TRUE;
4567 }
4568 
4569 #undef  TARGET_LITTLE_SYM
4570 #define TARGET_LITTLE_SYM		iamcu_elf32_vec
4571 #undef  TARGET_LITTLE_NAME
4572 #define TARGET_LITTLE_NAME		"elf32-iamcu"
4573 #undef  ELF_ARCH
4574 #define ELF_ARCH			bfd_arch_iamcu
4575 
4576 #undef	ELF_MACHINE_CODE
4577 #define	ELF_MACHINE_CODE		EM_IAMCU
4578 
4579 #undef	elf_backend_arch_data
4580 #define	elf_backend_arch_data		&elf_i386_arch_bed
4581 
4582 #undef	ELF_OSABI
4583 
4584 #undef  elf32_bed
4585 #define elf32_bed			elf32_iamcu_bed
4586 
4587 #undef	elf_backend_object_p
4588 #define elf_backend_object_p		elf32_iamcu_elf_object_p
4589 
4590 #undef	elf_backend_static_tls_alignment
4591 
4592 #undef	elf_backend_want_plt_sym
4593 #define elf_backend_want_plt_sym	0
4594 
4595 #undef  elf_backend_strtab_flags
4596 #undef  elf_backend_copy_special_section_fields
4597 
4598 #include "elf32-target.h"
4599 
4600 /* Restore defaults.  */
4601 #undef	ELF_ARCH
4602 #define ELF_ARCH			bfd_arch_i386
4603 #undef	ELF_MACHINE_CODE
4604 #define ELF_MACHINE_CODE		EM_386
4605 
4606 /* Native Client support.  */
4607 
4608 #undef	TARGET_LITTLE_SYM
4609 #define	TARGET_LITTLE_SYM		i386_elf32_nacl_vec
4610 #undef	TARGET_LITTLE_NAME
4611 #define	TARGET_LITTLE_NAME		"elf32-i386-nacl"
4612 #undef	elf32_bed
4613 #define	elf32_bed			elf32_i386_nacl_bed
4614 
4615 #undef	ELF_MAXPAGESIZE
4616 #define	ELF_MAXPAGESIZE			0x10000
4617 
4618 /* Restore defaults.  */
4619 #undef	ELF_OSABI
4620 #undef	elf_backend_want_plt_sym
4621 #define elf_backend_want_plt_sym	0
4622 #undef	elf_backend_post_process_headers
4623 #undef	elf_backend_static_tls_alignment
4624 
4625 /* NaCl uses substantially different PLT entries for the same effects.  */
4626 
4627 #undef	elf_backend_plt_alignment
4628 #define elf_backend_plt_alignment	5
4629 #define NACL_PLT_ENTRY_SIZE		64
4630 #define	NACLMASK			0xe0 /* 32-byte alignment mask.  */
4631 
4632 static const bfd_byte elf_i386_nacl_plt0_entry[] =
4633   {
4634     0xff, 0x35,			  /* pushl contents of address */
4635     0, 0, 0, 0,			  /* replaced with address of .got + 4.  */
4636     0x8b, 0x0d,			  /* movl contents of address, %ecx */
4637     0, 0, 0, 0,			  /* replaced with address of .got + 8.  */
4638     0x83, 0xe1, NACLMASK,	  /* andl $NACLMASK, %ecx */
4639     0xff, 0xe1			  /* jmp *%ecx */
4640   };
4641 
4642 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
4643   {
4644     0x8b, 0x0d,				/* movl contents of address, %ecx */
4645     0, 0, 0, 0,				/* replaced with GOT slot address.  */
4646     0x83, 0xe1, NACLMASK,		/* andl $NACLMASK, %ecx */
4647     0xff, 0xe1,				/* jmp *%ecx */
4648 
4649     /* Pad to the next 32-byte boundary with nop instructions.  */
4650     0x90,
4651     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4652     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4653 
4654     /* Lazy GOT entries point here (32-byte aligned).  */
4655     0x68,			       /* pushl immediate */
4656     0, 0, 0, 0,			       /* replaced with reloc offset.  */
4657     0xe9,			       /* jmp relative */
4658     0, 0, 0, 0,			       /* replaced with offset to .plt.  */
4659 
4660     /* Pad to the next 32-byte boundary with nop instructions.  */
4661     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4662     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4663     0x90, 0x90
4664   };
4665 
4666 static const bfd_byte
4667 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
4668   {
4669     0xff, 0x73, 0x04,		/* pushl 4(%ebx) */
4670     0x8b, 0x4b, 0x08,		/* mov 0x8(%ebx), %ecx */
4671     0x83, 0xe1, 0xe0,		/* and $NACLMASK, %ecx */
4672     0xff, 0xe1,			/* jmp *%ecx */
4673 
4674     /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
4675        so pad to that size with nop instructions.  */
4676     0x90, 0x90, 0x90, 0x90, 0x90, 0x90
4677   };
4678 
4679 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
4680   {
4681     0x8b, 0x8b,		 /* movl offset(%ebx), %ecx */
4682     0, 0, 0, 0,		 /* replaced with offset of this symbol in .got.  */
4683     0x83, 0xe1, 0xe0,	 /* andl $NACLMASK, %ecx */
4684     0xff, 0xe1,		 /* jmp *%ecx */
4685 
4686     /* Pad to the next 32-byte boundary with nop instructions.  */
4687     0x90,
4688     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4689     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4690 
4691     /* Lazy GOT entries point here (32-byte aligned).  */
4692     0x68,		 /* pushl immediate */
4693     0, 0, 0, 0,		 /* replaced with offset into relocation table.  */
4694     0xe9,		 /* jmp relative */
4695     0, 0, 0, 0,		 /* replaced with offset to start of .plt.  */
4696 
4697     /* Pad to the next 32-byte boundary with nop instructions.  */
4698     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4699     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4700     0x90, 0x90
4701   };
4702 
4703 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
4704   {
4705 #if (PLT_CIE_LENGTH != 20				\
4706      || PLT_FDE_LENGTH != 36				\
4707      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8	\
4708      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
4709 # error "Need elf_x86_backend_data parameters for eh_frame_plt offsets!"
4710 #endif
4711     PLT_CIE_LENGTH, 0, 0, 0,		/* CIE length */
4712     0, 0, 0, 0,				/* CIE ID */
4713     1,					/* CIE version */
4714     'z', 'R', 0,			/* Augmentation string */
4715     1,					/* Code alignment factor */
4716     0x7c,				/* Data alignment factor: -4 */
4717     8,					/* Return address column */
4718     1,					/* Augmentation size */
4719     DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding */
4720     DW_CFA_def_cfa, 4, 4,		/* DW_CFA_def_cfa: r4 (esp) ofs 4 */
4721     DW_CFA_offset + 8, 1,		/* DW_CFA_offset: r8 (eip) at cfa-4 */
4722     DW_CFA_nop, DW_CFA_nop,
4723 
4724     PLT_FDE_LENGTH, 0, 0, 0,	 /* FDE length */
4725     PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
4726     0, 0, 0, 0,			 /* R_386_PC32 .plt goes here */
4727     0, 0, 0, 0,			 /* .plt size goes here */
4728     0,				 /* Augmentation size */
4729     DW_CFA_def_cfa_offset, 8,	 /* DW_CFA_def_cfa_offset: 8 */
4730     DW_CFA_advance_loc + 6,	 /* DW_CFA_advance_loc: 6 to __PLT__+6 */
4731     DW_CFA_def_cfa_offset, 12,	 /* DW_CFA_def_cfa_offset: 12 */
4732     DW_CFA_advance_loc + 58,	 /* DW_CFA_advance_loc: 58 to __PLT__+64 */
4733     DW_CFA_def_cfa_expression,	 /* DW_CFA_def_cfa_expression */
4734     13,				 /* Block length */
4735     DW_OP_breg4, 4,		 /* DW_OP_breg4 (esp): 4 */
4736     DW_OP_breg8, 0,		 /* DW_OP_breg8 (eip): 0 */
4737     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
4738     DW_OP_lit2, DW_OP_shl, DW_OP_plus,
4739     DW_CFA_nop, DW_CFA_nop
4740   };
4741 
4742 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
4743   {
4744     elf_i386_nacl_plt0_entry,		/* plt0_entry */
4745     sizeof (elf_i386_nacl_plt0_entry),	/* plt0_entry_size */
4746     elf_i386_nacl_plt_entry,		/* plt_entry */
4747     NACL_PLT_ENTRY_SIZE,		/* plt_entry_size */
4748     NULL,				/* plt_tlsdesc_entry */
4749     0,					/* plt_tlsdesc_entry_size*/
4750     0,					/* plt_tlsdesc_got1_offset */
4751     0,					/* plt_tlsdesc_got2_offset */
4752     0,					/* plt_tlsdesc_got1_insn_end */
4753     0,					/* plt_tlsdesc_got2_insn_end */
4754     2,					/* plt0_got1_offset */
4755     8,					/* plt0_got2_offset */
4756     0,					/* plt0_got2_insn_end */
4757     2,					/* plt_got_offset */
4758     33,					/* plt_reloc_offset */
4759     38,					/* plt_plt_offset */
4760     0,					/* plt_got_insn_size */
4761     0,					/* plt_plt_insn_end */
4762     32,					/* plt_lazy_offset */
4763     elf_i386_nacl_pic_plt0_entry,	/* pic_plt0_entry */
4764     elf_i386_nacl_pic_plt_entry,	/* pic_plt_entry */
4765     elf_i386_nacl_eh_frame_plt,		/* eh_frame_plt */
4766     sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
4767   };
4768 
4769 static const struct elf_x86_backend_data elf_i386_nacl_arch_bed =
4770   {
4771     is_nacl				/* os */
4772   };
4773 
4774 static bfd_boolean
4775 elf32_i386_nacl_elf_object_p (bfd *abfd)
4776 {
4777   /* Set the right machine number for a NaCl i386 ELF32 file.  */
4778   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
4779   return TRUE;
4780 }
4781 
4782 #undef	elf_backend_arch_data
4783 #define elf_backend_arch_data	&elf_i386_nacl_arch_bed
4784 
4785 #undef	elf_backend_object_p
4786 #define elf_backend_object_p			elf32_i386_nacl_elf_object_p
4787 #undef	elf_backend_modify_segment_map
4788 #define	elf_backend_modify_segment_map		nacl_modify_segment_map
4789 #undef	elf_backend_modify_program_headers
4790 #define	elf_backend_modify_program_headers	nacl_modify_program_headers
4791 #undef	elf_backend_final_write_processing
4792 #define elf_backend_final_write_processing	nacl_final_write_processing
4793 
4794 #include "elf32-target.h"
4795 
4796 /* Restore defaults.  */
4797 #undef	elf_backend_object_p
4798 #undef	elf_backend_modify_segment_map
4799 #undef	elf_backend_modify_program_headers
4800 #undef	elf_backend_final_write_processing
4801 
4802 /* VxWorks support.  */
4803 
4804 #undef	TARGET_LITTLE_SYM
4805 #define TARGET_LITTLE_SYM		i386_elf32_vxworks_vec
4806 #undef	TARGET_LITTLE_NAME
4807 #define TARGET_LITTLE_NAME		"elf32-i386-vxworks"
4808 #undef	ELF_OSABI
4809 #undef	ELF_MAXPAGESIZE
4810 #define ELF_MAXPAGESIZE			0x1000
4811 #undef	elf_backend_plt_alignment
4812 #define elf_backend_plt_alignment	4
4813 
4814 static const struct elf_x86_backend_data elf_i386_vxworks_arch_bed =
4815   {
4816     is_vxworks				/* os */
4817   };
4818 
4819 #undef	elf_backend_arch_data
4820 #define	elf_backend_arch_data	&elf_i386_vxworks_arch_bed
4821 
4822 #undef elf_backend_relocs_compatible
4823 #undef elf_backend_add_symbol_hook
4824 #define elf_backend_add_symbol_hook \
4825   elf_vxworks_add_symbol_hook
4826 #undef elf_backend_link_output_symbol_hook
4827 #define elf_backend_link_output_symbol_hook \
4828   elf_vxworks_link_output_symbol_hook
4829 #undef elf_backend_emit_relocs
4830 #define elf_backend_emit_relocs			elf_vxworks_emit_relocs
4831 #undef elf_backend_final_write_processing
4832 #define elf_backend_final_write_processing \
4833   elf_vxworks_final_write_processing
4834 #undef elf_backend_static_tls_alignment
4835 
4836 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4837    define it.  */
4838 #undef elf_backend_want_plt_sym
4839 #define elf_backend_want_plt_sym	1
4840 
4841 #undef	elf32_bed
4842 #define elf32_bed				elf32_i386_vxworks_bed
4843 
4844 #include "elf32-target.h"
4845