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