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