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