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