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