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