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