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