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