bfd/
[external/binutils.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005 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 2 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, MA 02110-1301, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf-vxworks.h"
27
28 /* 386 uses REL relocations instead of RELA.  */
29 #define USE_REL 1
30
31 #include "elf/i386.h"
32
33 static reloc_howto_type elf_howto_table[]=
34 {
35   HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
36         bfd_elf_generic_reloc, "R_386_NONE",
37         TRUE, 0x00000000, 0x00000000, FALSE),
38   HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
39         bfd_elf_generic_reloc, "R_386_32",
40         TRUE, 0xffffffff, 0xffffffff, FALSE),
41   HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
42         bfd_elf_generic_reloc, "R_386_PC32",
43         TRUE, 0xffffffff, 0xffffffff, TRUE),
44   HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
45         bfd_elf_generic_reloc, "R_386_GOT32",
46         TRUE, 0xffffffff, 0xffffffff, FALSE),
47   HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
48         bfd_elf_generic_reloc, "R_386_PLT32",
49         TRUE, 0xffffffff, 0xffffffff, TRUE),
50   HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
51         bfd_elf_generic_reloc, "R_386_COPY",
52         TRUE, 0xffffffff, 0xffffffff, FALSE),
53   HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
54         bfd_elf_generic_reloc, "R_386_GLOB_DAT",
55         TRUE, 0xffffffff, 0xffffffff, FALSE),
56   HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
57         bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
58         TRUE, 0xffffffff, 0xffffffff, FALSE),
59   HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
60         bfd_elf_generic_reloc, "R_386_RELATIVE",
61         TRUE, 0xffffffff, 0xffffffff, FALSE),
62   HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
63         bfd_elf_generic_reloc, "R_386_GOTOFF",
64         TRUE, 0xffffffff, 0xffffffff, FALSE),
65   HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
66         bfd_elf_generic_reloc, "R_386_GOTPC",
67         TRUE, 0xffffffff, 0xffffffff, TRUE),
68
69   /* We have a gap in the reloc numbers here.
70      R_386_standard counts the number up to this point, and
71      R_386_ext_offset is the value to subtract from a reloc type of
72      R_386_16 thru R_386_PC8 to form an index into this table.  */
73 #define R_386_standard (R_386_GOTPC + 1)
74 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
75
76   /* These relocs are a GNU extension.  */
77   HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
78         bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
79         TRUE, 0xffffffff, 0xffffffff, FALSE),
80   HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
81         bfd_elf_generic_reloc, "R_386_TLS_IE",
82         TRUE, 0xffffffff, 0xffffffff, FALSE),
83   HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
84         bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
85         TRUE, 0xffffffff, 0xffffffff, FALSE),
86   HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
87         bfd_elf_generic_reloc, "R_386_TLS_LE",
88         TRUE, 0xffffffff, 0xffffffff, FALSE),
89   HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
90         bfd_elf_generic_reloc, "R_386_TLS_GD",
91         TRUE, 0xffffffff, 0xffffffff, FALSE),
92   HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
93         bfd_elf_generic_reloc, "R_386_TLS_LDM",
94         TRUE, 0xffffffff, 0xffffffff, FALSE),
95   HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
96         bfd_elf_generic_reloc, "R_386_16",
97         TRUE, 0xffff, 0xffff, FALSE),
98   HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
99         bfd_elf_generic_reloc, "R_386_PC16",
100         TRUE, 0xffff, 0xffff, TRUE),
101   HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
102         bfd_elf_generic_reloc, "R_386_8",
103         TRUE, 0xff, 0xff, FALSE),
104   HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
105         bfd_elf_generic_reloc, "R_386_PC8",
106         TRUE, 0xff, 0xff, TRUE),
107
108 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
109 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
110   /* These are common with Solaris TLS implementation.  */
111   HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
112         bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
113         TRUE, 0xffffffff, 0xffffffff, FALSE),
114   HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
115         bfd_elf_generic_reloc, "R_386_TLS_IE_32",
116         TRUE, 0xffffffff, 0xffffffff, FALSE),
117   HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
118         bfd_elf_generic_reloc, "R_386_TLS_LE_32",
119         TRUE, 0xffffffff, 0xffffffff, FALSE),
120   HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
121         bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
122         TRUE, 0xffffffff, 0xffffffff, FALSE),
123   HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
124         bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
125         TRUE, 0xffffffff, 0xffffffff, FALSE),
126   HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
127         bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
128         TRUE, 0xffffffff, 0xffffffff, FALSE),
129
130   /* Another gap.  */
131 #define R_386_tls (R_386_TLS_TPOFF32 + 1 - R_386_tls_offset)
132 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_tls)
133
134 /* GNU extension to record C++ vtable hierarchy.  */
135   HOWTO (R_386_GNU_VTINHERIT,   /* type */
136          0,                     /* rightshift */
137          2,                     /* size (0 = byte, 1 = short, 2 = long) */
138          0,                     /* bitsize */
139          FALSE,                 /* pc_relative */
140          0,                     /* bitpos */
141          complain_overflow_dont, /* complain_on_overflow */
142          NULL,                  /* special_function */
143          "R_386_GNU_VTINHERIT", /* name */
144          FALSE,                 /* partial_inplace */
145          0,                     /* src_mask */
146          0,                     /* dst_mask */
147          FALSE),                /* pcrel_offset */
148
149 /* GNU extension to record C++ vtable member usage.  */
150   HOWTO (R_386_GNU_VTENTRY,     /* type */
151          0,                     /* rightshift */
152          2,                     /* size (0 = byte, 1 = short, 2 = long) */
153          0,                     /* bitsize */
154          FALSE,                 /* pc_relative */
155          0,                     /* bitpos */
156          complain_overflow_dont, /* complain_on_overflow */
157          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
158          "R_386_GNU_VTENTRY",   /* name */
159          FALSE,                 /* partial_inplace */
160          0,                     /* src_mask */
161          0,                     /* dst_mask */
162          FALSE)                 /* pcrel_offset */
163
164 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
165
166 };
167
168 #ifdef DEBUG_GEN_RELOC
169 #define TRACE(str) \
170   fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
171 #else
172 #define TRACE(str)
173 #endif
174
175 static reloc_howto_type *
176 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
177                             bfd_reloc_code_real_type code)
178 {
179   switch (code)
180     {
181     case BFD_RELOC_NONE:
182       TRACE ("BFD_RELOC_NONE");
183       return &elf_howto_table[R_386_NONE];
184
185     case BFD_RELOC_32:
186       TRACE ("BFD_RELOC_32");
187       return &elf_howto_table[R_386_32];
188
189     case BFD_RELOC_CTOR:
190       TRACE ("BFD_RELOC_CTOR");
191       return &elf_howto_table[R_386_32];
192
193     case BFD_RELOC_32_PCREL:
194       TRACE ("BFD_RELOC_PC32");
195       return &elf_howto_table[R_386_PC32];
196
197     case BFD_RELOC_386_GOT32:
198       TRACE ("BFD_RELOC_386_GOT32");
199       return &elf_howto_table[R_386_GOT32];
200
201     case BFD_RELOC_386_PLT32:
202       TRACE ("BFD_RELOC_386_PLT32");
203       return &elf_howto_table[R_386_PLT32];
204
205     case BFD_RELOC_386_COPY:
206       TRACE ("BFD_RELOC_386_COPY");
207       return &elf_howto_table[R_386_COPY];
208
209     case BFD_RELOC_386_GLOB_DAT:
210       TRACE ("BFD_RELOC_386_GLOB_DAT");
211       return &elf_howto_table[R_386_GLOB_DAT];
212
213     case BFD_RELOC_386_JUMP_SLOT:
214       TRACE ("BFD_RELOC_386_JUMP_SLOT");
215       return &elf_howto_table[R_386_JUMP_SLOT];
216
217     case BFD_RELOC_386_RELATIVE:
218       TRACE ("BFD_RELOC_386_RELATIVE");
219       return &elf_howto_table[R_386_RELATIVE];
220
221     case BFD_RELOC_386_GOTOFF:
222       TRACE ("BFD_RELOC_386_GOTOFF");
223       return &elf_howto_table[R_386_GOTOFF];
224
225     case BFD_RELOC_386_GOTPC:
226       TRACE ("BFD_RELOC_386_GOTPC");
227       return &elf_howto_table[R_386_GOTPC];
228
229       /* These relocs are a GNU extension.  */
230     case BFD_RELOC_386_TLS_TPOFF:
231       TRACE ("BFD_RELOC_386_TLS_TPOFF");
232       return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
233
234     case BFD_RELOC_386_TLS_IE:
235       TRACE ("BFD_RELOC_386_TLS_IE");
236       return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
237
238     case BFD_RELOC_386_TLS_GOTIE:
239       TRACE ("BFD_RELOC_386_TLS_GOTIE");
240       return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
241
242     case BFD_RELOC_386_TLS_LE:
243       TRACE ("BFD_RELOC_386_TLS_LE");
244       return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
245
246     case BFD_RELOC_386_TLS_GD:
247       TRACE ("BFD_RELOC_386_TLS_GD");
248       return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
249
250     case BFD_RELOC_386_TLS_LDM:
251       TRACE ("BFD_RELOC_386_TLS_LDM");
252       return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
253
254     case BFD_RELOC_16:
255       TRACE ("BFD_RELOC_16");
256       return &elf_howto_table[R_386_16 - R_386_ext_offset];
257
258     case BFD_RELOC_16_PCREL:
259       TRACE ("BFD_RELOC_16_PCREL");
260       return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
261
262     case BFD_RELOC_8:
263       TRACE ("BFD_RELOC_8");
264       return &elf_howto_table[R_386_8 - R_386_ext_offset];
265
266     case BFD_RELOC_8_PCREL:
267       TRACE ("BFD_RELOC_8_PCREL");
268       return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
269
270     /* Common with Sun TLS implementation.  */
271     case BFD_RELOC_386_TLS_LDO_32:
272       TRACE ("BFD_RELOC_386_TLS_LDO_32");
273       return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
274
275     case BFD_RELOC_386_TLS_IE_32:
276       TRACE ("BFD_RELOC_386_TLS_IE_32");
277       return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
278
279     case BFD_RELOC_386_TLS_LE_32:
280       TRACE ("BFD_RELOC_386_TLS_LE_32");
281       return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
282
283     case BFD_RELOC_386_TLS_DTPMOD32:
284       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
285       return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
286
287     case BFD_RELOC_386_TLS_DTPOFF32:
288       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
289       return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
290
291     case BFD_RELOC_386_TLS_TPOFF32:
292       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
293       return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
294
295     case BFD_RELOC_VTABLE_INHERIT:
296       TRACE ("BFD_RELOC_VTABLE_INHERIT");
297       return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
298
299     case BFD_RELOC_VTABLE_ENTRY:
300       TRACE ("BFD_RELOC_VTABLE_ENTRY");
301       return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
302
303     default:
304       break;
305     }
306
307   TRACE ("Unknown");
308   return 0;
309 }
310
311 static void
312 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
313                             arelent *cache_ptr,
314                             Elf_Internal_Rela *dst)
315 {
316   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
317   unsigned int indx;
318
319   if ((indx = r_type) >= R_386_standard
320       && ((indx = r_type - R_386_ext_offset) - R_386_standard
321           >= R_386_ext - R_386_standard)
322       && ((indx = r_type - R_386_tls_offset) - R_386_ext
323           >= R_386_tls - R_386_ext)
324       && ((indx = r_type - R_386_vt_offset) - R_386_tls
325           >= R_386_vt - R_386_tls))
326     {
327       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
328                              abfd, (int) r_type);
329       indx = R_386_NONE;
330     }
331   cache_ptr->howto = &elf_howto_table[indx];
332 }
333
334 /* Return whether a symbol name implies a local label.  The UnixWare
335    2.1 cc generates temporary symbols that start with .X, so we
336    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
337    If so, we should move the .X recognition into
338    _bfd_elf_is_local_label_name.  */
339
340 static bfd_boolean
341 elf_i386_is_local_label_name (bfd *abfd, const char *name)
342 {
343   if (name[0] == '.' && name[1] == 'X')
344     return TRUE;
345
346   return _bfd_elf_is_local_label_name (abfd, name);
347 }
348 \f
349 /* Support for core dump NOTE sections.  */
350
351 static bfd_boolean
352 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
353 {
354   int offset;
355   size_t size;
356
357   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
358     {
359       int pr_version = bfd_get_32 (abfd, note->descdata);
360
361       if (pr_version != 1)
362         return FALSE;
363
364       /* pr_cursig */
365       elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
366
367       /* pr_pid */
368       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
369
370       /* pr_reg */
371       offset = 28;
372       size = bfd_get_32 (abfd, note->descdata + 8);
373     }
374   else
375     {
376       switch (note->descsz)
377         {
378         default:
379           return FALSE;
380
381         case 144:               /* Linux/i386 */
382           /* pr_cursig */
383           elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
384
385           /* pr_pid */
386           elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
387
388           /* pr_reg */
389           offset = 72;
390           size = 68;
391
392           break;
393         }
394     }
395
396   /* Make a ".reg/999" section.  */
397   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
398                                           size, note->descpos + offset);
399 }
400
401 static bfd_boolean
402 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
403 {
404   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
405     {
406       int pr_version = bfd_get_32 (abfd, note->descdata);
407
408       if (pr_version != 1)
409         return FALSE;
410
411       elf_tdata (abfd)->core_program
412         = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
413       elf_tdata (abfd)->core_command
414         = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
415     }
416   else
417     {
418       switch (note->descsz)
419         {
420         default:
421           return FALSE;
422
423         case 124:               /* Linux/i386 elf_prpsinfo.  */
424           elf_tdata (abfd)->core_program
425             = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
426           elf_tdata (abfd)->core_command
427             = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
428         }
429     }
430
431   /* Note that for some reason, a spurious space is tacked
432      onto the end of the args in some (at least one anyway)
433      implementations, so strip it off if it exists.  */
434   {
435     char *command = elf_tdata (abfd)->core_command;
436     int n = strlen (command);
437
438     if (0 < n && command[n - 1] == ' ')
439       command[n - 1] = '\0';
440   }
441
442   return TRUE;
443 }
444 \f
445 /* Functions for the i386 ELF linker.
446
447    In order to gain some understanding of code in this file without
448    knowing all the intricate details of the linker, note the
449    following:
450
451    Functions named elf_i386_* are called by external routines, other
452    functions are only called locally.  elf_i386_* functions appear
453    in this file more or less in the order in which they are called
454    from external routines.  eg. elf_i386_check_relocs is called
455    early in the link process, elf_i386_finish_dynamic_sections is
456    one of the last functions.  */
457
458
459 /* The name of the dynamic interpreter.  This is put in the .interp
460    section.  */
461
462 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
463
464 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
465    copying dynamic variables from a shared lib into an app's dynbss
466    section, and instead use a dynamic relocation to point into the
467    shared lib.  */
468 #define ELIMINATE_COPY_RELOCS 1
469
470 /* The size in bytes of an entry in the procedure linkage table.  */
471
472 #define PLT_ENTRY_SIZE 16
473
474 /* The first entry in an absolute procedure linkage table looks like
475    this.  See the SVR4 ABI i386 supplement to see how this works.
476    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
477
478 static const bfd_byte elf_i386_plt0_entry[12] =
479 {
480   0xff, 0x35,   /* pushl contents of address */
481   0, 0, 0, 0,   /* replaced with address of .got + 4.  */
482   0xff, 0x25,   /* jmp indirect */
483   0, 0, 0, 0    /* replaced with address of .got + 8.  */
484 };
485
486 /* Subsequent entries in an absolute procedure linkage table look like
487    this.  */
488
489 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
490 {
491   0xff, 0x25,   /* jmp indirect */
492   0, 0, 0, 0,   /* replaced with address of this symbol in .got.  */
493   0x68,         /* pushl immediate */
494   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
495   0xe9,         /* jmp relative */
496   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
497 };
498
499 /* The first entry in a PIC procedure linkage table look like this.
500    Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
501
502 static const bfd_byte elf_i386_pic_plt0_entry[12] =
503 {
504   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx) */
505   0xff, 0xa3, 8, 0, 0, 0        /* jmp *8(%ebx) */
506 };
507
508 /* Subsequent entries in a PIC procedure linkage table look like this.  */
509
510 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
511 {
512   0xff, 0xa3,   /* jmp *offset(%ebx) */
513   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
514   0x68,         /* pushl immediate */
515   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
516   0xe9,         /* jmp relative */
517   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
518 };
519
520 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
521    for the PLTResolve stub and then for each PLT entry.  */
522 #define PLTRESOLVE_RELOCS_SHLIB 0
523 #define PLTRESOLVE_RELOCS 2
524 #define PLT_NON_JUMP_SLOT_RELOCS 2
525
526 /* The i386 linker needs to keep track of the number of relocs that it
527    decides to copy as dynamic relocs in check_relocs for each symbol.
528    This is so that it can later discard them if they are found to be
529    unnecessary.  We store the information in a field extending the
530    regular ELF linker hash table.  */
531
532 struct elf_i386_dyn_relocs
533 {
534   struct elf_i386_dyn_relocs *next;
535
536   /* The input section of the reloc.  */
537   asection *sec;
538
539   /* Total number of relocs copied for the input section.  */
540   bfd_size_type count;
541
542   /* Number of pc-relative relocs copied for the input section.  */
543   bfd_size_type pc_count;
544 };
545
546 /* i386 ELF linker hash entry.  */
547
548 struct elf_i386_link_hash_entry
549 {
550   struct elf_link_hash_entry elf;
551
552   /* Track dynamic relocs copied for this symbol.  */
553   struct elf_i386_dyn_relocs *dyn_relocs;
554
555 #define GOT_UNKNOWN     0
556 #define GOT_NORMAL      1
557 #define GOT_TLS_GD      2
558 #define GOT_TLS_IE      4
559 #define GOT_TLS_IE_POS  5
560 #define GOT_TLS_IE_NEG  6
561 #define GOT_TLS_IE_BOTH 7
562   unsigned char tls_type;
563 };
564
565 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
566
567 struct elf_i386_obj_tdata
568 {
569   struct elf_obj_tdata root;
570
571   /* tls_type for each local got entry.  */
572   char *local_got_tls_type;
573 };
574
575 #define elf_i386_tdata(abfd) \
576   ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
577
578 #define elf_i386_local_got_tls_type(abfd) \
579   (elf_i386_tdata (abfd)->local_got_tls_type)
580
581 static bfd_boolean
582 elf_i386_mkobject (bfd *abfd)
583 {
584   bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
585   abfd->tdata.any = bfd_zalloc (abfd, amt);
586   if (abfd->tdata.any == NULL)
587     return FALSE;
588   return TRUE;
589 }
590
591 /* i386 ELF linker hash table.  */
592
593 struct elf_i386_link_hash_table
594 {
595   struct elf_link_hash_table elf;
596
597   /* Short-cuts to get to dynamic linker sections.  */
598   asection *sgot;
599   asection *sgotplt;
600   asection *srelgot;
601   asection *splt;
602   asection *srelplt;
603   asection *sdynbss;
604   asection *srelbss;
605
606   /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.  */
607   asection *srelplt2;
608
609   /* Short-cuts to frequently used symbols for VxWorks targets.  */
610   struct elf_link_hash_entry *hgot, *hplt;
611
612   /* True if the target system is VxWorks.  */
613   int is_vxworks;
614
615   /* Value used to fill the last word of the first plt entry.  */
616   bfd_byte plt0_pad_byte;
617
618   union {
619     bfd_signed_vma refcount;
620     bfd_vma offset;
621   } tls_ldm_got;
622
623   /* Small local sym to section mapping cache.  */
624   struct sym_sec_cache sym_sec;
625 };
626
627 /* Get the i386 ELF linker hash table from a link_info structure.  */
628
629 #define elf_i386_hash_table(p) \
630   ((struct elf_i386_link_hash_table *) ((p)->hash))
631
632 /* Create an entry in an i386 ELF linker hash table.  */
633
634 static struct bfd_hash_entry *
635 link_hash_newfunc (struct bfd_hash_entry *entry,
636                    struct bfd_hash_table *table,
637                    const char *string)
638 {
639   /* Allocate the structure if it has not already been allocated by a
640      subclass.  */
641   if (entry == NULL)
642     {
643       entry = bfd_hash_allocate (table,
644                                  sizeof (struct elf_i386_link_hash_entry));
645       if (entry == NULL)
646         return entry;
647     }
648
649   /* Call the allocation method of the superclass.  */
650   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
651   if (entry != NULL)
652     {
653       struct elf_i386_link_hash_entry *eh;
654
655       eh = (struct elf_i386_link_hash_entry *) entry;
656       eh->dyn_relocs = NULL;
657       eh->tls_type = GOT_UNKNOWN;
658     }
659
660   return entry;
661 }
662
663 /* Create an i386 ELF linker hash table.  */
664
665 static struct bfd_link_hash_table *
666 elf_i386_link_hash_table_create (bfd *abfd)
667 {
668   struct elf_i386_link_hash_table *ret;
669   bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
670
671   ret = bfd_malloc (amt);
672   if (ret == NULL)
673     return NULL;
674
675   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
676     {
677       free (ret);
678       return NULL;
679     }
680
681   ret->sgot = NULL;
682   ret->sgotplt = NULL;
683   ret->srelgot = NULL;
684   ret->splt = NULL;
685   ret->srelplt = NULL;
686   ret->sdynbss = NULL;
687   ret->srelbss = NULL;
688   ret->tls_ldm_got.refcount = 0;
689   ret->sym_sec.abfd = NULL;
690   ret->is_vxworks = 0;
691   ret->srelplt2 = NULL;
692   ret->hgot = NULL;
693   ret->hplt = NULL;
694   ret->plt0_pad_byte = 0;
695
696   return &ret->elf.root;
697 }
698
699 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
700    shortcuts to them in our hash table.  */
701
702 static bfd_boolean
703 create_got_section (bfd *dynobj, struct bfd_link_info *info)
704 {
705   struct elf_i386_link_hash_table *htab;
706
707   if (! _bfd_elf_create_got_section (dynobj, info))
708     return FALSE;
709
710   htab = elf_i386_hash_table (info);
711   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
712   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
713   if (!htab->sgot || !htab->sgotplt)
714     abort ();
715
716   htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
717                                                (SEC_ALLOC | SEC_LOAD
718                                                 | SEC_HAS_CONTENTS
719                                                 | SEC_IN_MEMORY
720                                                 | SEC_LINKER_CREATED
721                                                 | SEC_READONLY));
722   if (htab->srelgot == NULL
723       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
724     return FALSE;
725   return TRUE;
726 }
727
728 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
729    .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
730    hash table.  */
731
732 static bfd_boolean
733 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
734 {
735   struct elf_i386_link_hash_table *htab;
736   asection * s;
737   int flags;
738   const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
739
740   htab = elf_i386_hash_table (info);
741   if (!htab->sgot && !create_got_section (dynobj, info))
742     return FALSE;
743
744   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
745     return FALSE;
746
747   htab->splt = bfd_get_section_by_name (dynobj, ".plt");
748   htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
749   htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
750   if (!info->shared)
751     htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
752
753   if (!htab->splt || !htab->srelplt || !htab->sdynbss
754       || (!info->shared && !htab->srelbss))
755     abort ();
756
757   if (htab->is_vxworks && !info->shared)
758     {
759       s = bfd_make_section (dynobj, ".rel.plt.unloaded");
760       flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY
761               | SEC_LINKER_CREATED);
762       if (s == NULL
763          || ! bfd_set_section_flags (dynobj, s, flags)
764          || ! bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
765        return FALSE;
766       htab->srelplt2 = s;
767     }
768
769   return TRUE;
770 }
771
772 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
773
774 static void
775 elf_i386_copy_indirect_symbol (const struct elf_backend_data *bed,
776                                struct elf_link_hash_entry *dir,
777                                struct elf_link_hash_entry *ind)
778 {
779   struct elf_i386_link_hash_entry *edir, *eind;
780
781   edir = (struct elf_i386_link_hash_entry *) dir;
782   eind = (struct elf_i386_link_hash_entry *) ind;
783
784   if (eind->dyn_relocs != NULL)
785     {
786       if (edir->dyn_relocs != NULL)
787         {
788           struct elf_i386_dyn_relocs **pp;
789           struct elf_i386_dyn_relocs *p;
790
791           if (ind->root.type == bfd_link_hash_indirect)
792             abort ();
793
794           /* Add reloc counts against the weak sym to the strong sym
795              list.  Merge any entries against the same section.  */
796           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
797             {
798               struct elf_i386_dyn_relocs *q;
799
800               for (q = edir->dyn_relocs; q != NULL; q = q->next)
801                 if (q->sec == p->sec)
802                   {
803                     q->pc_count += p->pc_count;
804                     q->count += p->count;
805                     *pp = p->next;
806                     break;
807                   }
808               if (q == NULL)
809                 pp = &p->next;
810             }
811           *pp = edir->dyn_relocs;
812         }
813
814       edir->dyn_relocs = eind->dyn_relocs;
815       eind->dyn_relocs = NULL;
816     }
817
818   if (ind->root.type == bfd_link_hash_indirect
819       && dir->got.refcount <= 0)
820     {
821       edir->tls_type = eind->tls_type;
822       eind->tls_type = GOT_UNKNOWN;
823     }
824
825   if (ELIMINATE_COPY_RELOCS
826       && ind->root.type != bfd_link_hash_indirect
827       && dir->dynamic_adjusted)
828     {
829       /* If called to transfer flags for a weakdef during processing
830          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
831          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
832       dir->ref_dynamic |= ind->ref_dynamic;
833       dir->ref_regular |= ind->ref_regular;
834       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
835       dir->needs_plt |= ind->needs_plt;
836       dir->pointer_equality_needed |= ind->pointer_equality_needed;
837     }
838   else
839     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
840 }
841
842 static int
843 elf_i386_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
844 {
845   if (info->shared)
846     return r_type;
847
848   switch (r_type)
849     {
850     case R_386_TLS_GD:
851     case R_386_TLS_IE_32:
852       if (is_local)
853         return R_386_TLS_LE_32;
854       return R_386_TLS_IE_32;
855     case R_386_TLS_IE:
856     case R_386_TLS_GOTIE:
857       if (is_local)
858         return R_386_TLS_LE_32;
859       return r_type;
860     case R_386_TLS_LDM:
861       return R_386_TLS_LE_32;
862     }
863
864   return r_type;
865 }
866
867 /* Look through the relocs for a section during the first phase, and
868    calculate needed space in the global offset table, procedure linkage
869    table, and dynamic reloc sections.  */
870
871 static bfd_boolean
872 elf_i386_check_relocs (bfd *abfd,
873                        struct bfd_link_info *info,
874                        asection *sec,
875                        const Elf_Internal_Rela *relocs)
876 {
877   struct elf_i386_link_hash_table *htab;
878   Elf_Internal_Shdr *symtab_hdr;
879   struct elf_link_hash_entry **sym_hashes;
880   const Elf_Internal_Rela *rel;
881   const Elf_Internal_Rela *rel_end;
882   asection *sreloc;
883
884   if (info->relocatable)
885     return TRUE;
886
887   htab = elf_i386_hash_table (info);
888   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
889   sym_hashes = elf_sym_hashes (abfd);
890
891   sreloc = NULL;
892
893   rel_end = relocs + sec->reloc_count;
894   for (rel = relocs; rel < rel_end; rel++)
895     {
896       unsigned int r_type;
897       unsigned long r_symndx;
898       struct elf_link_hash_entry *h;
899
900       r_symndx = ELF32_R_SYM (rel->r_info);
901       r_type = ELF32_R_TYPE (rel->r_info);
902
903       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
904         {
905           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
906                                  abfd,
907                                  r_symndx);
908           return FALSE;
909         }
910
911       if (r_symndx < symtab_hdr->sh_info)
912         h = NULL;
913       else
914         {
915           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
916           while (h->root.type == bfd_link_hash_indirect
917                  || h->root.type == bfd_link_hash_warning)
918             h = (struct elf_link_hash_entry *) h->root.u.i.link;
919         }
920
921       r_type = elf_i386_tls_transition (info, r_type, h == NULL);
922
923       switch (r_type)
924         {
925         case R_386_TLS_LDM:
926           htab->tls_ldm_got.refcount += 1;
927           goto create_got;
928
929         case R_386_PLT32:
930           /* This symbol requires a procedure linkage table entry.  We
931              actually build the entry in adjust_dynamic_symbol,
932              because this might be a case of linking PIC code which is
933              never referenced by a dynamic object, in which case we
934              don't need to generate a procedure linkage table entry
935              after all.  */
936
937           /* If this is a local symbol, we resolve it directly without
938              creating a procedure linkage table entry.  */
939           if (h == NULL)
940             continue;
941
942           h->needs_plt = 1;
943           h->plt.refcount += 1;
944           break;
945
946         case R_386_TLS_IE_32:
947         case R_386_TLS_IE:
948         case R_386_TLS_GOTIE:
949           if (info->shared)
950             info->flags |= DF_STATIC_TLS;
951           /* Fall through */
952
953         case R_386_GOT32:
954         case R_386_TLS_GD:
955           /* This symbol requires a global offset table entry.  */
956           {
957             int tls_type, old_tls_type;
958
959             switch (r_type)
960               {
961               default:
962               case R_386_GOT32: tls_type = GOT_NORMAL; break;
963               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
964               case R_386_TLS_IE_32:
965                 if (ELF32_R_TYPE (rel->r_info) == r_type)
966                   tls_type = GOT_TLS_IE_NEG;
967                 else
968                   /* If this is a GD->IE transition, we may use either of
969                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
970                   tls_type = GOT_TLS_IE;
971                 break;
972               case R_386_TLS_IE:
973               case R_386_TLS_GOTIE:
974                 tls_type = GOT_TLS_IE_POS; break;
975               }
976
977             if (h != NULL)
978               {
979                 h->got.refcount += 1;
980                 old_tls_type = elf_i386_hash_entry(h)->tls_type;
981               }
982             else
983               {
984                 bfd_signed_vma *local_got_refcounts;
985
986                 /* This is a global offset table entry for a local symbol.  */
987                 local_got_refcounts = elf_local_got_refcounts (abfd);
988                 if (local_got_refcounts == NULL)
989                   {
990                     bfd_size_type size;
991
992                     size = symtab_hdr->sh_info;
993                     size *= (sizeof (bfd_signed_vma) + sizeof(char));
994                     local_got_refcounts = bfd_zalloc (abfd, size);
995                     if (local_got_refcounts == NULL)
996                       return FALSE;
997                     elf_local_got_refcounts (abfd) = local_got_refcounts;
998                     elf_i386_local_got_tls_type (abfd)
999                       = (char *) (local_got_refcounts + symtab_hdr->sh_info);
1000                   }
1001                 local_got_refcounts[r_symndx] += 1;
1002                 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1003               }
1004
1005             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1006               tls_type |= old_tls_type;
1007             /* If a TLS symbol is accessed using IE at least once,
1008                there is no point to use dynamic model for it.  */
1009             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1010                      && (old_tls_type != GOT_TLS_GD
1011                          || (tls_type & GOT_TLS_IE) == 0))
1012               {
1013                 if ((old_tls_type & GOT_TLS_IE) && tls_type == GOT_TLS_GD)
1014                   tls_type = old_tls_type;
1015                 else
1016                   {
1017                     (*_bfd_error_handler)
1018                       (_("%B: `%s' accessed both as normal and "
1019                          "thread local symbol"),
1020                        abfd,
1021                        h ? h->root.root.string : "<local>");
1022                     return FALSE;
1023                   }
1024               }
1025
1026             if (old_tls_type != tls_type)
1027               {
1028                 if (h != NULL)
1029                   elf_i386_hash_entry (h)->tls_type = tls_type;
1030                 else
1031                   elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1032               }
1033           }
1034           /* Fall through */
1035
1036         case R_386_GOTOFF:
1037         case R_386_GOTPC:
1038         create_got:
1039           if (htab->sgot == NULL)
1040             {
1041               if (htab->elf.dynobj == NULL)
1042                 htab->elf.dynobj = abfd;
1043               if (!create_got_section (htab->elf.dynobj, info))
1044                 return FALSE;
1045             }
1046           if (r_type != R_386_TLS_IE)
1047             break;
1048           /* Fall through */
1049
1050         case R_386_TLS_LE_32:
1051         case R_386_TLS_LE:
1052           if (!info->shared)
1053             break;
1054           info->flags |= DF_STATIC_TLS;
1055           /* Fall through */
1056
1057         case R_386_32:
1058         case R_386_PC32:
1059           if (h != NULL && !info->shared)
1060             {
1061               /* If this reloc is in a read-only section, we might
1062                  need a copy reloc.  We can't check reliably at this
1063                  stage whether the section is read-only, as input
1064                  sections have not yet been mapped to output sections.
1065                  Tentatively set the flag for now, and correct in
1066                  adjust_dynamic_symbol.  */
1067               h->non_got_ref = 1;
1068
1069               /* We may need a .plt entry if the function this reloc
1070                  refers to is in a shared lib.  */
1071               h->plt.refcount += 1;
1072               if (r_type != R_386_PC32)
1073                 h->pointer_equality_needed = 1;
1074             }
1075
1076           /* If we are creating a shared library, and this is a reloc
1077              against a global symbol, or a non PC relative reloc
1078              against a local symbol, then we need to copy the reloc
1079              into the shared library.  However, if we are linking with
1080              -Bsymbolic, we do not need to copy a reloc against a
1081              global symbol which is defined in an object we are
1082              including in the link (i.e., DEF_REGULAR is set).  At
1083              this point we have not seen all the input files, so it is
1084              possible that DEF_REGULAR is not set now but will be set
1085              later (it is never cleared).  In case of a weak definition,
1086              DEF_REGULAR may be cleared later by a strong definition in
1087              a shared library.  We account for that possibility below by
1088              storing information in the relocs_copied field of the hash
1089              table entry.  A similar situation occurs when creating
1090              shared libraries and symbol visibility changes render the
1091              symbol local.
1092
1093              If on the other hand, we are creating an executable, we
1094              may need to keep relocations for symbols satisfied by a
1095              dynamic library if we manage to avoid copy relocs for the
1096              symbol.  */
1097           if ((info->shared
1098                && (sec->flags & SEC_ALLOC) != 0
1099                && (r_type != R_386_PC32
1100                    || (h != NULL
1101                        && (! info->symbolic
1102                            || h->root.type == bfd_link_hash_defweak
1103                            || !h->def_regular))))
1104               || (ELIMINATE_COPY_RELOCS
1105                   && !info->shared
1106                   && (sec->flags & SEC_ALLOC) != 0
1107                   && h != NULL
1108                   && (h->root.type == bfd_link_hash_defweak
1109                       || !h->def_regular)))
1110             {
1111               struct elf_i386_dyn_relocs *p;
1112               struct elf_i386_dyn_relocs **head;
1113
1114               /* We must copy these reloc types into the output file.
1115                  Create a reloc section in dynobj and make room for
1116                  this reloc.  */
1117               if (sreloc == NULL)
1118                 {
1119                   const char *name;
1120                   bfd *dynobj;
1121                   unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1122                   unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1123
1124                   name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1125                   if (name == NULL)
1126                     return FALSE;
1127
1128                   if (strncmp (name, ".rel", 4) != 0
1129                       || strcmp (bfd_get_section_name (abfd, sec),
1130                                  name + 4) != 0)
1131                     {
1132                       (*_bfd_error_handler)
1133                         (_("%B: bad relocation section name `%s\'"),
1134                          abfd, name);
1135                     }
1136
1137                   if (htab->elf.dynobj == NULL)
1138                     htab->elf.dynobj = abfd;
1139
1140                   dynobj = htab->elf.dynobj;
1141                   sreloc = bfd_get_section_by_name (dynobj, name);
1142                   if (sreloc == NULL)
1143                     {
1144                       flagword flags;
1145
1146                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
1147                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1148                       if ((sec->flags & SEC_ALLOC) != 0)
1149                         flags |= SEC_ALLOC | SEC_LOAD;
1150                       sreloc = bfd_make_section_with_flags (dynobj,
1151                                                             name,
1152                                                             flags);
1153                       if (sreloc == NULL
1154                           || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1155                         return FALSE;
1156                     }
1157                   elf_section_data (sec)->sreloc = sreloc;
1158                 }
1159
1160               /* If this is a global symbol, we count the number of
1161                  relocations we need for this symbol.  */
1162               if (h != NULL)
1163                 {
1164                   head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1165                 }
1166               else
1167                 {
1168                   /* Track dynamic relocs needed for local syms too.
1169                      We really need local syms available to do this
1170                      easily.  Oh well.  */
1171
1172                   asection *s;
1173                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1174                                                  sec, r_symndx);
1175                   if (s == NULL)
1176                     return FALSE;
1177
1178                   head = ((struct elf_i386_dyn_relocs **)
1179                           &elf_section_data (s)->local_dynrel);
1180                 }
1181
1182               p = *head;
1183               if (p == NULL || p->sec != sec)
1184                 {
1185                   bfd_size_type amt = sizeof *p;
1186                   p = bfd_alloc (htab->elf.dynobj, amt);
1187                   if (p == NULL)
1188                     return FALSE;
1189                   p->next = *head;
1190                   *head = p;
1191                   p->sec = sec;
1192                   p->count = 0;
1193                   p->pc_count = 0;
1194                 }
1195
1196               p->count += 1;
1197               if (r_type == R_386_PC32)
1198                 p->pc_count += 1;
1199             }
1200           break;
1201
1202           /* This relocation describes the C++ object vtable hierarchy.
1203              Reconstruct it for later use during GC.  */
1204         case R_386_GNU_VTINHERIT:
1205           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1206             return FALSE;
1207           break;
1208
1209           /* This relocation describes which C++ vtable entries are actually
1210              used.  Record for later use during GC.  */
1211         case R_386_GNU_VTENTRY:
1212           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1213             return FALSE;
1214           break;
1215
1216         default:
1217           break;
1218         }
1219     }
1220
1221   return TRUE;
1222 }
1223
1224 /* Return the section that should be marked against GC for a given
1225    relocation.  */
1226
1227 static asection *
1228 elf_i386_gc_mark_hook (asection *sec,
1229                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
1230                        Elf_Internal_Rela *rel,
1231                        struct elf_link_hash_entry *h,
1232                        Elf_Internal_Sym *sym)
1233 {
1234   if (h != NULL)
1235     {
1236       switch (ELF32_R_TYPE (rel->r_info))
1237         {
1238         case R_386_GNU_VTINHERIT:
1239         case R_386_GNU_VTENTRY:
1240           break;
1241
1242         default:
1243           switch (h->root.type)
1244             {
1245             case bfd_link_hash_defined:
1246             case bfd_link_hash_defweak:
1247               return h->root.u.def.section;
1248
1249             case bfd_link_hash_common:
1250               return h->root.u.c.p->section;
1251
1252             default:
1253               break;
1254             }
1255         }
1256     }
1257   else
1258     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1259
1260   return NULL;
1261 }
1262
1263 /* Update the got entry reference counts for the section being removed.  */
1264
1265 static bfd_boolean
1266 elf_i386_gc_sweep_hook (bfd *abfd,
1267                         struct bfd_link_info *info,
1268                         asection *sec,
1269                         const Elf_Internal_Rela *relocs)
1270 {
1271   Elf_Internal_Shdr *symtab_hdr;
1272   struct elf_link_hash_entry **sym_hashes;
1273   bfd_signed_vma *local_got_refcounts;
1274   const Elf_Internal_Rela *rel, *relend;
1275
1276   elf_section_data (sec)->local_dynrel = NULL;
1277
1278   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1279   sym_hashes = elf_sym_hashes (abfd);
1280   local_got_refcounts = elf_local_got_refcounts (abfd);
1281
1282   relend = relocs + sec->reloc_count;
1283   for (rel = relocs; rel < relend; rel++)
1284     {
1285       unsigned long r_symndx;
1286       unsigned int r_type;
1287       struct elf_link_hash_entry *h = NULL;
1288
1289       r_symndx = ELF32_R_SYM (rel->r_info);
1290       if (r_symndx >= symtab_hdr->sh_info)
1291         {
1292           struct elf_i386_link_hash_entry *eh;
1293           struct elf_i386_dyn_relocs **pp;
1294           struct elf_i386_dyn_relocs *p;
1295
1296           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1297           while (h->root.type == bfd_link_hash_indirect
1298                  || h->root.type == bfd_link_hash_warning)
1299             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1300           eh = (struct elf_i386_link_hash_entry *) h;
1301
1302           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1303             if (p->sec == sec)
1304               {
1305                 /* Everything must go for SEC.  */
1306                 *pp = p->next;
1307                 break;
1308               }
1309         }
1310
1311       r_type = ELF32_R_TYPE (rel->r_info);
1312       r_type = elf_i386_tls_transition (info, r_type, h != NULL);
1313       switch (r_type)
1314         {
1315         case R_386_TLS_LDM:
1316           if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1317             elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1318           break;
1319
1320         case R_386_TLS_GD:
1321         case R_386_TLS_IE_32:
1322         case R_386_TLS_IE:
1323         case R_386_TLS_GOTIE:
1324         case R_386_GOT32:
1325           if (h != NULL)
1326             {
1327               if (h->got.refcount > 0)
1328                 h->got.refcount -= 1;
1329             }
1330           else if (local_got_refcounts != NULL)
1331             {
1332               if (local_got_refcounts[r_symndx] > 0)
1333                 local_got_refcounts[r_symndx] -= 1;
1334             }
1335           break;
1336
1337         case R_386_32:
1338         case R_386_PC32:
1339           if (info->shared)
1340             break;
1341           /* Fall through */
1342
1343         case R_386_PLT32:
1344           if (h != NULL)
1345             {
1346               if (h->plt.refcount > 0)
1347                 h->plt.refcount -= 1;
1348             }
1349           break;
1350
1351         default:
1352           break;
1353         }
1354     }
1355
1356   return TRUE;
1357 }
1358
1359 /* Adjust a symbol defined by a dynamic object and referenced by a
1360    regular object.  The current definition is in some section of the
1361    dynamic object, but we're not including those sections.  We have to
1362    change the definition to something the rest of the link can
1363    understand.  */
1364
1365 static bfd_boolean
1366 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1367                                 struct elf_link_hash_entry *h)
1368 {
1369   struct elf_i386_link_hash_table *htab;
1370   asection *s;
1371   unsigned int power_of_two;
1372
1373   /* If this is a function, put it in the procedure linkage table.  We
1374      will fill in the contents of the procedure linkage table later,
1375      when we know the address of the .got section.  */
1376   if (h->type == STT_FUNC
1377       || h->needs_plt)
1378     {
1379       if (h->plt.refcount <= 0
1380           || SYMBOL_CALLS_LOCAL (info, h)
1381           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1382               && h->root.type == bfd_link_hash_undefweak))
1383         {
1384           /* This case can occur if we saw a PLT32 reloc in an input
1385              file, but the symbol was never referred to by a dynamic
1386              object, or if all references were garbage collected.  In
1387              such a case, we don't actually need to build a procedure
1388              linkage table, and we can just do a PC32 reloc instead.  */
1389           h->plt.offset = (bfd_vma) -1;
1390           h->needs_plt = 0;
1391         }
1392
1393       return TRUE;
1394     }
1395   else
1396     /* It's possible that we incorrectly decided a .plt reloc was
1397        needed for an R_386_PC32 reloc to a non-function sym in
1398        check_relocs.  We can't decide accurately between function and
1399        non-function syms in check-relocs;  Objects loaded later in
1400        the link may change h->type.  So fix it now.  */
1401     h->plt.offset = (bfd_vma) -1;
1402
1403   /* If this is a weak symbol, and there is a real definition, the
1404      processor independent code will have arranged for us to see the
1405      real definition first, and we can just use the same value.  */
1406   if (h->u.weakdef != NULL)
1407     {
1408       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1409                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1410       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1411       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1412       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1413         h->non_got_ref = h->u.weakdef->non_got_ref;
1414       return TRUE;
1415     }
1416
1417   /* This is a reference to a symbol defined by a dynamic object which
1418      is not a function.  */
1419
1420   /* If we are creating a shared library, we must presume that the
1421      only references to the symbol are via the global offset table.
1422      For such cases we need not do anything here; the relocations will
1423      be handled correctly by relocate_section.  */
1424   if (info->shared)
1425     return TRUE;
1426
1427   /* If there are no references to this symbol that do not use the
1428      GOT, we don't need to generate a copy reloc.  */
1429   if (!h->non_got_ref)
1430     return TRUE;
1431
1432   /* If -z nocopyreloc was given, we won't generate them either.  */
1433   if (info->nocopyreloc)
1434     {
1435       h->non_got_ref = 0;
1436       return TRUE;
1437     }
1438
1439   htab = elf_i386_hash_table (info);
1440
1441   /* If there aren't any dynamic relocs in read-only sections, then
1442      we can keep the dynamic relocs and avoid the copy reloc.  This
1443      doesn't work on VxWorks, where we can not have dynamic relocations
1444      (other than copy and jump slot relocations) in an executable.  */
1445   if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1446     {
1447       struct elf_i386_link_hash_entry * eh;
1448       struct elf_i386_dyn_relocs *p;
1449
1450       eh = (struct elf_i386_link_hash_entry *) h;
1451       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1452         {
1453           s = p->sec->output_section;
1454           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1455             break;
1456         }
1457
1458       if (p == NULL)
1459         {
1460           h->non_got_ref = 0;
1461           return TRUE;
1462         }
1463     }
1464
1465   /* We must allocate the symbol in our .dynbss section, which will
1466      become part of the .bss section of the executable.  There will be
1467      an entry for this symbol in the .dynsym section.  The dynamic
1468      object will contain position independent code, so all references
1469      from the dynamic object to this symbol will go through the global
1470      offset table.  The dynamic linker will use the .dynsym entry to
1471      determine the address it must put in the global offset table, so
1472      both the dynamic object and the regular object will refer to the
1473      same memory location for the variable.  */
1474
1475   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1476      copy the initial value out of the dynamic object and into the
1477      runtime process image.  */
1478   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1479     {
1480       htab->srelbss->size += sizeof (Elf32_External_Rel);
1481       h->needs_copy = 1;
1482     }
1483
1484   /* We need to figure out the alignment required for this symbol.  I
1485      have no idea how ELF linkers handle this.  */
1486   power_of_two = bfd_log2 (h->size);
1487   if (power_of_two > 3)
1488     power_of_two = 3;
1489
1490   /* Apply the required alignment.  */
1491   s = htab->sdynbss;
1492   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1493   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1494     {
1495       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1496         return FALSE;
1497     }
1498
1499   /* Define the symbol as being at this point in the section.  */
1500   h->root.u.def.section = s;
1501   h->root.u.def.value = s->size;
1502
1503   /* Increment the section size to make room for the symbol.  */
1504   s->size += h->size;
1505
1506   return TRUE;
1507 }
1508
1509 /* Allocate space in .plt, .got and associated reloc sections for
1510    dynamic relocs.  */
1511
1512 static bfd_boolean
1513 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1514 {
1515   struct bfd_link_info *info;
1516   struct elf_i386_link_hash_table *htab;
1517   struct elf_i386_link_hash_entry *eh;
1518   struct elf_i386_dyn_relocs *p;
1519
1520   if (h->root.type == bfd_link_hash_indirect)
1521     return TRUE;
1522
1523   if (h->root.type == bfd_link_hash_warning)
1524     /* When warning symbols are created, they **replace** the "real"
1525        entry in the hash table, thus we never get to see the real
1526        symbol in a hash traversal.  So look at it now.  */
1527     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1528
1529   info = (struct bfd_link_info *) inf;
1530   htab = elf_i386_hash_table (info);
1531
1532   if (htab->elf.dynamic_sections_created
1533       && h->plt.refcount > 0)
1534     {
1535       /* Make sure this symbol is output as a dynamic symbol.
1536          Undefined weak syms won't yet be marked as dynamic.  */
1537       if (h->dynindx == -1
1538           && !h->forced_local)
1539         {
1540           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1541             return FALSE;
1542         }
1543
1544       if (info->shared
1545           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1546         {
1547           asection *s = htab->splt;
1548
1549           /* If this is the first .plt entry, make room for the special
1550              first entry.  */
1551           if (s->size == 0)
1552             s->size += PLT_ENTRY_SIZE;
1553
1554           h->plt.offset = s->size;
1555
1556           /* If this symbol is not defined in a regular file, and we are
1557              not generating a shared library, then set the symbol to this
1558              location in the .plt.  This is required to make function
1559              pointers compare as equal between the normal executable and
1560              the shared library.  */
1561           if (! info->shared
1562               && !h->def_regular)
1563             {
1564               h->root.u.def.section = s;
1565               h->root.u.def.value = h->plt.offset;
1566             }
1567
1568           /* Make room for this entry.  */
1569           s->size += PLT_ENTRY_SIZE;
1570
1571           /* We also need to make an entry in the .got.plt section, which
1572              will be placed in the .got section by the linker script.  */
1573           htab->sgotplt->size += 4;
1574
1575           /* We also need to make an entry in the .rel.plt section.  */
1576           htab->srelplt->size += sizeof (Elf32_External_Rel);
1577
1578           if (htab->is_vxworks && !info->shared)
1579             {
1580               /* VxWorks has a second set of relocations for each PLT entry
1581                  in executables.  They go in a separate relocation section,
1582                  which is processed by the kernel loader.  */
1583
1584               /* There are two relocations for the initial PLT entry: an
1585                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1586                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
1587
1588               if (h->plt.offset == PLT_ENTRY_SIZE)
1589                 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1590
1591               /* There are two extra relocations for each subsequent PLT entry:
1592                  an R_386_32 relocation for the GOT entry, and an R_386_32
1593                  relocation for the PLT entry.  */
1594
1595               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1596             }
1597         }
1598       else
1599         {
1600           h->plt.offset = (bfd_vma) -1;
1601           h->needs_plt = 0;
1602         }
1603     }
1604   else
1605     {
1606       h->plt.offset = (bfd_vma) -1;
1607       h->needs_plt = 0;
1608     }
1609
1610   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1611      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
1612   if (h->got.refcount > 0
1613       && !info->shared
1614       && h->dynindx == -1
1615       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1616     h->got.offset = (bfd_vma) -1;
1617   else if (h->got.refcount > 0)
1618     {
1619       asection *s;
1620       bfd_boolean dyn;
1621       int tls_type = elf_i386_hash_entry(h)->tls_type;
1622
1623       /* Make sure this symbol is output as a dynamic symbol.
1624          Undefined weak syms won't yet be marked as dynamic.  */
1625       if (h->dynindx == -1
1626           && !h->forced_local)
1627         {
1628           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1629             return FALSE;
1630         }
1631
1632       s = htab->sgot;
1633       h->got.offset = s->size;
1634       s->size += 4;
1635       /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
1636       if (tls_type == GOT_TLS_GD || tls_type == GOT_TLS_IE_BOTH)
1637         s->size += 4;
1638       dyn = htab->elf.dynamic_sections_created;
1639       /* R_386_TLS_IE_32 needs one dynamic relocation,
1640          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1641          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1642          need two), R_386_TLS_GD needs one if local symbol and two if
1643          global.  */
1644       if (tls_type == GOT_TLS_IE_BOTH)
1645         htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1646       else if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1647                || (tls_type & GOT_TLS_IE))
1648         htab->srelgot->size += sizeof (Elf32_External_Rel);
1649       else if (tls_type == GOT_TLS_GD)
1650         htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1651       else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1652                 || h->root.type != bfd_link_hash_undefweak)
1653                && (info->shared
1654                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1655         htab->srelgot->size += sizeof (Elf32_External_Rel);
1656     }
1657   else
1658     h->got.offset = (bfd_vma) -1;
1659
1660   eh = (struct elf_i386_link_hash_entry *) h;
1661   if (eh->dyn_relocs == NULL)
1662     return TRUE;
1663
1664   /* In the shared -Bsymbolic case, discard space allocated for
1665      dynamic pc-relative relocs against symbols which turn out to be
1666      defined in regular objects.  For the normal shared case, discard
1667      space for pc-relative relocs that have become local due to symbol
1668      visibility changes.  */
1669
1670   if (info->shared)
1671     {
1672       /* The only reloc that uses pc_count is R_386_PC32, which will
1673          appear on a call or on something like ".long foo - .".  We
1674          want calls to protected symbols to resolve directly to the
1675          function rather than going via the plt.  If people want
1676          function pointer comparisons to work as expected then they
1677          should avoid writing assembly like ".long foo - .".  */
1678       if (SYMBOL_CALLS_LOCAL (info, h))
1679         {
1680           struct elf_i386_dyn_relocs **pp;
1681
1682           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1683             {
1684               p->count -= p->pc_count;
1685               p->pc_count = 0;
1686               if (p->count == 0)
1687                 *pp = p->next;
1688               else
1689                 pp = &p->next;
1690             }
1691         }
1692
1693       /* Also discard relocs on undefined weak syms with non-default
1694          visibility.  */
1695       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1696           && h->root.type == bfd_link_hash_undefweak)
1697         eh->dyn_relocs = NULL;
1698     }
1699   else if (ELIMINATE_COPY_RELOCS)
1700     {
1701       /* For the non-shared case, discard space for relocs against
1702          symbols which turn out to need copy relocs or are not
1703          dynamic.  */
1704
1705       if (!h->non_got_ref
1706           && ((h->def_dynamic
1707                && !h->def_regular)
1708               || (htab->elf.dynamic_sections_created
1709                   && (h->root.type == bfd_link_hash_undefweak
1710                       || h->root.type == bfd_link_hash_undefined))))
1711         {
1712           /* Make sure this symbol is output as a dynamic symbol.
1713              Undefined weak syms won't yet be marked as dynamic.  */
1714           if (h->dynindx == -1
1715               && !h->forced_local)
1716             {
1717               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1718                 return FALSE;
1719             }
1720
1721           /* If that succeeded, we know we'll be keeping all the
1722              relocs.  */
1723           if (h->dynindx != -1)
1724             goto keep;
1725         }
1726
1727       eh->dyn_relocs = NULL;
1728
1729     keep: ;
1730     }
1731
1732   /* Finally, allocate space.  */
1733   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1734     {
1735       asection *sreloc = elf_section_data (p->sec)->sreloc;
1736       sreloc->size += p->count * sizeof (Elf32_External_Rel);
1737     }
1738
1739   return TRUE;
1740 }
1741
1742 /* Find any dynamic relocs that apply to read-only sections.  */
1743
1744 static bfd_boolean
1745 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1746 {
1747   struct elf_i386_link_hash_entry *eh;
1748   struct elf_i386_dyn_relocs *p;
1749
1750   if (h->root.type == bfd_link_hash_warning)
1751     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1752
1753   eh = (struct elf_i386_link_hash_entry *) h;
1754   for (p = eh->dyn_relocs; p != NULL; p = p->next)
1755     {
1756       asection *s = p->sec->output_section;
1757
1758       if (s != NULL && (s->flags & SEC_READONLY) != 0)
1759         {
1760           struct bfd_link_info *info = (struct bfd_link_info *) inf;
1761
1762           info->flags |= DF_TEXTREL;
1763
1764           /* Not an error, just cut short the traversal.  */
1765           return FALSE;
1766         }
1767     }
1768   return TRUE;
1769 }
1770
1771 /* Set the sizes of the dynamic sections.  */
1772
1773 static bfd_boolean
1774 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1775                                 struct bfd_link_info *info)
1776 {
1777   struct elf_i386_link_hash_table *htab;
1778   bfd *dynobj;
1779   asection *s;
1780   bfd_boolean relocs;
1781   bfd *ibfd;
1782
1783   htab = elf_i386_hash_table (info);
1784   dynobj = htab->elf.dynobj;
1785   if (dynobj == NULL)
1786     abort ();
1787
1788   if (htab->elf.dynamic_sections_created)
1789     {
1790       /* Set the contents of the .interp section to the interpreter.  */
1791       if (info->executable)
1792         {
1793           s = bfd_get_section_by_name (dynobj, ".interp");
1794           if (s == NULL)
1795             abort ();
1796           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1797           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1798         }
1799     }
1800
1801   /* Set up .got offsets for local syms, and space for local dynamic
1802      relocs.  */
1803   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1804     {
1805       bfd_signed_vma *local_got;
1806       bfd_signed_vma *end_local_got;
1807       char *local_tls_type;
1808       bfd_size_type locsymcount;
1809       Elf_Internal_Shdr *symtab_hdr;
1810       asection *srel;
1811
1812       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1813         continue;
1814
1815       for (s = ibfd->sections; s != NULL; s = s->next)
1816         {
1817           struct elf_i386_dyn_relocs *p;
1818
1819           for (p = *((struct elf_i386_dyn_relocs **)
1820                      &elf_section_data (s)->local_dynrel);
1821                p != NULL;
1822                p = p->next)
1823             {
1824               if (!bfd_is_abs_section (p->sec)
1825                   && bfd_is_abs_section (p->sec->output_section))
1826                 {
1827                   /* Input section has been discarded, either because
1828                      it is a copy of a linkonce section or due to
1829                      linker script /DISCARD/, so we'll be discarding
1830                      the relocs too.  */
1831                 }
1832               else if (p->count != 0)
1833                 {
1834                   srel = elf_section_data (p->sec)->sreloc;
1835                   srel->size += p->count * sizeof (Elf32_External_Rel);
1836                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1837                     info->flags |= DF_TEXTREL;
1838                 }
1839             }
1840         }
1841
1842       local_got = elf_local_got_refcounts (ibfd);
1843       if (!local_got)
1844         continue;
1845
1846       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1847       locsymcount = symtab_hdr->sh_info;
1848       end_local_got = local_got + locsymcount;
1849       local_tls_type = elf_i386_local_got_tls_type (ibfd);
1850       s = htab->sgot;
1851       srel = htab->srelgot;
1852       for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1853         {
1854           if (*local_got > 0)
1855             {
1856               *local_got = s->size;
1857               s->size += 4;
1858               if (*local_tls_type == GOT_TLS_GD
1859                   || *local_tls_type == GOT_TLS_IE_BOTH)
1860                 s->size += 4;
1861               if (info->shared
1862                   || *local_tls_type == GOT_TLS_GD
1863                   || (*local_tls_type & GOT_TLS_IE))
1864                 {
1865                   if (*local_tls_type == GOT_TLS_IE_BOTH)
1866                     srel->size += 2 * sizeof (Elf32_External_Rel);
1867                   else
1868                     srel->size += sizeof (Elf32_External_Rel);
1869                 }
1870             }
1871           else
1872             *local_got = (bfd_vma) -1;
1873         }
1874     }
1875
1876   if (htab->tls_ldm_got.refcount > 0)
1877     {
1878       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1879          relocs.  */
1880       htab->tls_ldm_got.offset = htab->sgot->size;
1881       htab->sgot->size += 8;
1882       htab->srelgot->size += sizeof (Elf32_External_Rel);
1883     }
1884   else
1885     htab->tls_ldm_got.offset = -1;
1886
1887   if (htab->is_vxworks)
1888     {
1889       /* Save the GOT and PLT symbols in the hash table for easy access.
1890          Mark them as having relocations; they might not, but we won't
1891          know for sure until we build the GOT in finish_dynamic_symbol.  */
1892
1893       htab->hgot = elf_link_hash_lookup (elf_hash_table (info),
1894                                         "_GLOBAL_OFFSET_TABLE_",
1895                                         FALSE, FALSE, FALSE);
1896       if (htab->hgot)
1897         htab->hgot->indx = -2;
1898       htab->hplt = elf_link_hash_lookup (elf_hash_table (info),
1899                                         "_PROCEDURE_LINKAGE_TABLE_",
1900                                         FALSE, FALSE, FALSE);
1901       if (htab->hplt)
1902         htab->hplt->indx = -2;
1903
1904       if (htab->is_vxworks && htab->hplt && htab->splt->flags & SEC_CODE)
1905         htab->hplt->type = STT_FUNC;
1906     }
1907
1908   /* Allocate global sym .plt and .got entries, and space for global
1909      sym dynamic relocs.  */
1910   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1911
1912   /* We now have determined the sizes of the various dynamic sections.
1913      Allocate memory for them.  */
1914   relocs = FALSE;
1915   for (s = dynobj->sections; s != NULL; s = s->next)
1916     {
1917       bfd_boolean strip_section = TRUE;
1918
1919       if ((s->flags & SEC_LINKER_CREATED) == 0)
1920         continue;
1921
1922       if (s == htab->splt
1923           || s == htab->sgot
1924           || s == htab->sgotplt
1925           || s == htab->sdynbss)
1926         {
1927           /* Strip this section if we don't need it; see the
1928              comment below.  */
1929           /* We'd like to strip these sections if they aren't needed, but if
1930              we've exported dynamic symbols from them we must leave them.
1931              It's too late to tell BFD to get rid of the symbols.  */
1932
1933           if (htab->hplt != NULL)
1934             strip_section = FALSE;
1935         }
1936       else if (strncmp (bfd_get_section_name (dynobj, s), ".rel", 4) == 0)
1937         {
1938           if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
1939             relocs = TRUE;
1940
1941           /* We use the reloc_count field as a counter if we need
1942              to copy relocs into the output file.  */
1943           s->reloc_count = 0;
1944         }
1945       else
1946         {
1947           /* It's not one of our sections, so don't allocate space.  */
1948           continue;
1949         }
1950
1951       if (s->size == 0)
1952         {
1953           /* If we don't need this section, strip it from the
1954              output file.  This is mostly to handle .rel.bss and
1955              .rel.plt.  We must create both sections in
1956              create_dynamic_sections, because they must be created
1957              before the linker maps input sections to output
1958              sections.  The linker does that before
1959              adjust_dynamic_symbol is called, and it is that
1960              function which decides whether anything needs to go
1961              into these sections.  */
1962           if (strip_section)
1963             s->flags |= SEC_EXCLUDE;
1964           continue;
1965         }
1966
1967       if ((s->flags & SEC_HAS_CONTENTS) == 0)
1968         continue;
1969
1970       /* Allocate memory for the section contents.  We use bfd_zalloc
1971          here in case unused entries are not reclaimed before the
1972          section's contents are written out.  This should not happen,
1973          but this way if it does, we get a R_386_NONE reloc instead
1974          of garbage.  */
1975       s->contents = bfd_zalloc (dynobj, s->size);
1976       if (s->contents == NULL)
1977         return FALSE;
1978     }
1979
1980   if (htab->elf.dynamic_sections_created)
1981     {
1982       /* Add some entries to the .dynamic section.  We fill in the
1983          values later, in elf_i386_finish_dynamic_sections, but we
1984          must add the entries now so that we get the correct size for
1985          the .dynamic section.  The DT_DEBUG entry is filled in by the
1986          dynamic linker and used by the debugger.  */
1987 #define add_dynamic_entry(TAG, VAL) \
1988   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1989
1990       if (info->executable)
1991         {
1992           if (!add_dynamic_entry (DT_DEBUG, 0))
1993             return FALSE;
1994         }
1995
1996       if (htab->splt->size != 0)
1997         {
1998           if (!add_dynamic_entry (DT_PLTGOT, 0)
1999               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2000               || !add_dynamic_entry (DT_PLTREL, DT_REL)
2001               || !add_dynamic_entry (DT_JMPREL, 0))
2002             return FALSE;
2003         }
2004
2005       if (relocs)
2006         {
2007           if (!add_dynamic_entry (DT_REL, 0)
2008               || !add_dynamic_entry (DT_RELSZ, 0)
2009               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2010             return FALSE;
2011
2012           /* If any dynamic relocs apply to a read-only section,
2013              then we need a DT_TEXTREL entry.  */
2014           if ((info->flags & DF_TEXTREL) == 0)
2015             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
2016                                     (PTR) info);
2017
2018           if ((info->flags & DF_TEXTREL) != 0)
2019             {
2020               if (!add_dynamic_entry (DT_TEXTREL, 0))
2021                 return FALSE;
2022             }
2023         }
2024     }
2025 #undef add_dynamic_entry
2026
2027   return TRUE;
2028 }
2029
2030 /* Set the correct type for an x86 ELF section.  We do this by the
2031    section name, which is a hack, but ought to work.  */
2032
2033 static bfd_boolean
2034 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2035                         Elf_Internal_Shdr *hdr,
2036                         asection *sec)
2037 {
2038   register const char *name;
2039
2040   name = bfd_get_section_name (abfd, sec);
2041
2042   /* This is an ugly, but unfortunately necessary hack that is
2043      needed when producing EFI binaries on x86. It tells
2044      elf.c:elf_fake_sections() not to consider ".reloc" as a section
2045      containing ELF relocation info.  We need this hack in order to
2046      be able to generate ELF binaries that can be translated into
2047      EFI applications (which are essentially COFF objects).  Those
2048      files contain a COFF ".reloc" section inside an ELFNN object,
2049      which would normally cause BFD to segfault because it would
2050      attempt to interpret this section as containing relocation
2051      entries for section "oc".  With this hack enabled, ".reloc"
2052      will be treated as a normal data section, which will avoid the
2053      segfault.  However, you won't be able to create an ELFNN binary
2054      with a section named "oc" that needs relocations, but that's
2055      the kind of ugly side-effects you get when detecting section
2056      types based on their names...  In practice, this limitation is
2057      unlikely to bite.  */
2058   if (strcmp (name, ".reloc") == 0)
2059     hdr->sh_type = SHT_PROGBITS;
2060
2061   return TRUE;
2062 }
2063
2064 /* Return the base VMA address which should be subtracted from real addresses
2065    when resolving @dtpoff relocation.
2066    This is PT_TLS segment p_vaddr.  */
2067
2068 static bfd_vma
2069 dtpoff_base (struct bfd_link_info *info)
2070 {
2071   /* If tls_sec is NULL, we should have signalled an error already.  */
2072   if (elf_hash_table (info)->tls_sec == NULL)
2073     return 0;
2074   return elf_hash_table (info)->tls_sec->vma;
2075 }
2076
2077 /* Return the relocation value for @tpoff relocation
2078    if STT_TLS virtual address is ADDRESS.  */
2079
2080 static bfd_vma
2081 tpoff (struct bfd_link_info *info, bfd_vma address)
2082 {
2083   struct elf_link_hash_table *htab = elf_hash_table (info);
2084
2085   /* If tls_sec is NULL, we should have signalled an error already.  */
2086   if (htab->tls_sec == NULL)
2087     return 0;
2088   return htab->tls_size + htab->tls_sec->vma - address;
2089 }
2090
2091 /* Relocate an i386 ELF section.  */
2092
2093 static bfd_boolean
2094 elf_i386_relocate_section (bfd *output_bfd,
2095                            struct bfd_link_info *info,
2096                            bfd *input_bfd,
2097                            asection *input_section,
2098                            bfd_byte *contents,
2099                            Elf_Internal_Rela *relocs,
2100                            Elf_Internal_Sym *local_syms,
2101                            asection **local_sections)
2102 {
2103   struct elf_i386_link_hash_table *htab;
2104   Elf_Internal_Shdr *symtab_hdr;
2105   struct elf_link_hash_entry **sym_hashes;
2106   bfd_vma *local_got_offsets;
2107   Elf_Internal_Rela *rel;
2108   Elf_Internal_Rela *relend;
2109
2110   htab = elf_i386_hash_table (info);
2111   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2112   sym_hashes = elf_sym_hashes (input_bfd);
2113   local_got_offsets = elf_local_got_offsets (input_bfd);
2114
2115   rel = relocs;
2116   relend = relocs + input_section->reloc_count;
2117   for (; rel < relend; rel++)
2118     {
2119       unsigned int r_type;
2120       reloc_howto_type *howto;
2121       unsigned long r_symndx;
2122       struct elf_link_hash_entry *h;
2123       Elf_Internal_Sym *sym;
2124       asection *sec;
2125       bfd_vma off;
2126       bfd_vma relocation;
2127       bfd_boolean unresolved_reloc;
2128       bfd_reloc_status_type r;
2129       unsigned int indx;
2130       int tls_type;
2131
2132       r_type = ELF32_R_TYPE (rel->r_info);
2133       if (r_type == R_386_GNU_VTINHERIT
2134           || r_type == R_386_GNU_VTENTRY)
2135         continue;
2136
2137       if ((indx = r_type) >= R_386_standard
2138           && ((indx = r_type - R_386_ext_offset) - R_386_standard
2139               >= R_386_ext - R_386_standard)
2140           && ((indx = r_type - R_386_tls_offset) - R_386_ext
2141               >= R_386_tls - R_386_ext))
2142         {
2143           (*_bfd_error_handler)
2144             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2145              input_bfd, input_section, r_type);
2146           bfd_set_error (bfd_error_bad_value);
2147           return FALSE;
2148         }
2149       howto = elf_howto_table + indx;
2150
2151       r_symndx = ELF32_R_SYM (rel->r_info);
2152
2153       if (info->relocatable)
2154         {
2155           bfd_vma val;
2156           bfd_byte *where;
2157
2158           /* This is a relocatable link.  We don't have to change
2159              anything, unless the reloc is against a section symbol,
2160              in which case we have to adjust according to where the
2161              section symbol winds up in the output section.  */
2162           if (r_symndx >= symtab_hdr->sh_info)
2163             continue;
2164
2165           sym = local_syms + r_symndx;
2166           if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2167             continue;
2168
2169           sec = local_sections[r_symndx];
2170           val = sec->output_offset;
2171           if (val == 0)
2172             continue;
2173
2174           where = contents + rel->r_offset;
2175           switch (howto->size)
2176             {
2177               /* FIXME: overflow checks.  */
2178             case 0:
2179               val += bfd_get_8 (input_bfd, where);
2180               bfd_put_8 (input_bfd, val, where);
2181               break;
2182             case 1:
2183               val += bfd_get_16 (input_bfd, where);
2184               bfd_put_16 (input_bfd, val, where);
2185               break;
2186             case 2:
2187               val += bfd_get_32 (input_bfd, where);
2188               bfd_put_32 (input_bfd, val, where);
2189               break;
2190             default:
2191               abort ();
2192             }
2193           continue;
2194         }
2195
2196       /* This is a final link.  */
2197       h = NULL;
2198       sym = NULL;
2199       sec = NULL;
2200       unresolved_reloc = FALSE;
2201       if (r_symndx < symtab_hdr->sh_info)
2202         {
2203           sym = local_syms + r_symndx;
2204           sec = local_sections[r_symndx];
2205           relocation = (sec->output_section->vma
2206                         + sec->output_offset
2207                         + sym->st_value);
2208           if ((sec->flags & SEC_MERGE)
2209               && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2210             {
2211               asection *msec;
2212               bfd_vma addend;
2213               bfd_byte *where = contents + rel->r_offset;
2214
2215               switch (howto->size)
2216                 {
2217                 case 0:
2218                   addend = bfd_get_8 (input_bfd, where);
2219                   if (howto->pc_relative)
2220                     {
2221                       addend = (addend ^ 0x80) - 0x80;
2222                       addend += 1;
2223                     }
2224                   break;
2225                 case 1:
2226                   addend = bfd_get_16 (input_bfd, where);
2227                   if (howto->pc_relative)
2228                     {
2229                       addend = (addend ^ 0x8000) - 0x8000;
2230                       addend += 2;
2231                     }
2232                   break;
2233                 case 2:
2234                   addend = bfd_get_32 (input_bfd, where);
2235                   if (howto->pc_relative)
2236                     {
2237                       addend = (addend ^ 0x80000000) - 0x80000000;
2238                       addend += 4;
2239                     }
2240                   break;
2241                 default:
2242                   abort ();
2243                 }
2244
2245               msec = sec;
2246               addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend);
2247               addend -= relocation;
2248               addend += msec->output_section->vma + msec->output_offset;
2249
2250               switch (howto->size)
2251                 {
2252                 case 0:
2253                   /* FIXME: overflow checks.  */
2254                   if (howto->pc_relative)
2255                     addend -= 1;
2256                   bfd_put_8 (input_bfd, addend, where);
2257                   break;
2258                 case 1:
2259                   if (howto->pc_relative)
2260                     addend -= 2;
2261                   bfd_put_16 (input_bfd, addend, where);
2262                   break;
2263                 case 2:
2264                   if (howto->pc_relative)
2265                     addend -= 4;
2266                   bfd_put_32 (input_bfd, addend, where);
2267                   break;
2268                 }
2269             }
2270         }
2271       else
2272         {
2273           bfd_boolean warned;
2274
2275           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2276                                    r_symndx, symtab_hdr, sym_hashes,
2277                                    h, sec, relocation,
2278                                    unresolved_reloc, warned);
2279         }
2280
2281       if (r_symndx == 0)
2282         {
2283         /* r_symndx will be zero only for relocs against symbols from
2284            removed linkonce sections, or sections discarded by a linker
2285            script.  For these relocs, we just want the section contents
2286            zeroed.  Avoid any special processing in the switch below.  */
2287           r_type = R_386_NONE;
2288
2289           relocation = 0;
2290           if (howto->pc_relative)
2291             relocation = (input_section->output_section->vma
2292                           + input_section->output_offset
2293                           + rel->r_offset);
2294         }
2295
2296       switch (r_type)
2297         {
2298         case R_386_GOT32:
2299           /* Relocation is to the entry for this symbol in the global
2300              offset table.  */
2301           if (htab->sgot == NULL)
2302             abort ();
2303
2304           if (h != NULL)
2305             {
2306               bfd_boolean dyn;
2307
2308               off = h->got.offset;
2309               dyn = htab->elf.dynamic_sections_created;
2310               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2311                   || (info->shared
2312                       && SYMBOL_REFERENCES_LOCAL (info, h))
2313                   || (ELF_ST_VISIBILITY (h->other)
2314                       && h->root.type == bfd_link_hash_undefweak))
2315                 {
2316                   /* This is actually a static link, or it is a
2317                      -Bsymbolic link and the symbol is defined
2318                      locally, or the symbol was forced to be local
2319                      because of a version file.  We must initialize
2320                      this entry in the global offset table.  Since the
2321                      offset must always be a multiple of 4, we use the
2322                      least significant bit to record whether we have
2323                      initialized it already.
2324
2325                      When doing a dynamic link, we create a .rel.got
2326                      relocation entry to initialize the value.  This
2327                      is done in the finish_dynamic_symbol routine.  */
2328                   if ((off & 1) != 0)
2329                     off &= ~1;
2330                   else
2331                     {
2332                       bfd_put_32 (output_bfd, relocation,
2333                                   htab->sgot->contents + off);
2334                       h->got.offset |= 1;
2335                     }
2336                 }
2337               else
2338                 unresolved_reloc = FALSE;
2339             }
2340           else
2341             {
2342               if (local_got_offsets == NULL)
2343                 abort ();
2344
2345               off = local_got_offsets[r_symndx];
2346
2347               /* The offset must always be a multiple of 4.  We use
2348                  the least significant bit to record whether we have
2349                  already generated the necessary reloc.  */
2350               if ((off & 1) != 0)
2351                 off &= ~1;
2352               else
2353                 {
2354                   bfd_put_32 (output_bfd, relocation,
2355                               htab->sgot->contents + off);
2356
2357                   if (info->shared)
2358                     {
2359                       asection *s;
2360                       Elf_Internal_Rela outrel;
2361                       bfd_byte *loc;
2362
2363                       s = htab->srelgot;
2364                       if (s == NULL)
2365                         abort ();
2366
2367                       outrel.r_offset = (htab->sgot->output_section->vma
2368                                          + htab->sgot->output_offset
2369                                          + off);
2370                       outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2371                       loc = s->contents;
2372                       loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2373                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2374                     }
2375
2376                   local_got_offsets[r_symndx] |= 1;
2377                 }
2378             }
2379
2380           if (off >= (bfd_vma) -2)
2381             abort ();
2382
2383           relocation = htab->sgot->output_section->vma
2384                        + htab->sgot->output_offset + off
2385                        - htab->sgotplt->output_section->vma
2386                        - htab->sgotplt->output_offset;
2387           break;
2388
2389         case R_386_GOTOFF:
2390           /* Relocation is relative to the start of the global offset
2391              table.  */
2392
2393           /* Check to make sure it isn't a protected function symbol
2394              for shared library since it may not be local when used
2395              as function address.  */
2396           if (info->shared
2397               && !info->executable
2398               && h
2399               && h->def_regular
2400               && h->type == STT_FUNC
2401               && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2402             {
2403               (*_bfd_error_handler)
2404                 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2405                  input_bfd, h->root.root.string);
2406               bfd_set_error (bfd_error_bad_value);
2407               return FALSE;
2408             }
2409
2410           /* Note that sgot is not involved in this
2411              calculation.  We always want the start of .got.plt.  If we
2412              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2413              permitted by the ABI, we might have to change this
2414              calculation.  */
2415           relocation -= htab->sgotplt->output_section->vma
2416                         + htab->sgotplt->output_offset;
2417           break;
2418
2419         case R_386_GOTPC:
2420           /* Use global offset table as symbol value.  */
2421           relocation = htab->sgotplt->output_section->vma
2422                        + htab->sgotplt->output_offset;
2423           unresolved_reloc = FALSE;
2424           break;
2425
2426         case R_386_PLT32:
2427           /* Relocation is to the entry for this symbol in the
2428              procedure linkage table.  */
2429
2430           /* Resolve a PLT32 reloc against a local symbol directly,
2431              without using the procedure linkage table.  */
2432           if (h == NULL)
2433             break;
2434
2435           if (h->plt.offset == (bfd_vma) -1
2436               || htab->splt == NULL)
2437             {
2438               /* We didn't make a PLT entry for this symbol.  This
2439                  happens when statically linking PIC code, or when
2440                  using -Bsymbolic.  */
2441               break;
2442             }
2443
2444           relocation = (htab->splt->output_section->vma
2445                         + htab->splt->output_offset
2446                         + h->plt.offset);
2447           unresolved_reloc = FALSE;
2448           break;
2449
2450         case R_386_32:
2451         case R_386_PC32:
2452           if ((input_section->flags & SEC_ALLOC) == 0)
2453             break;
2454
2455           if ((info->shared
2456                && (h == NULL
2457                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2458                    || h->root.type != bfd_link_hash_undefweak)
2459                && (r_type != R_386_PC32
2460                    || !SYMBOL_CALLS_LOCAL (info, h)))
2461               || (ELIMINATE_COPY_RELOCS
2462                   && !info->shared
2463                   && h != NULL
2464                   && h->dynindx != -1
2465                   && !h->non_got_ref
2466                   && ((h->def_dynamic
2467                        && !h->def_regular)
2468                       || h->root.type == bfd_link_hash_undefweak
2469                       || h->root.type == bfd_link_hash_undefined)))
2470             {
2471               Elf_Internal_Rela outrel;
2472               bfd_byte *loc;
2473               bfd_boolean skip, relocate;
2474               asection *sreloc;
2475
2476               /* When generating a shared object, these relocations
2477                  are copied into the output file to be resolved at run
2478                  time.  */
2479
2480               skip = FALSE;
2481               relocate = FALSE;
2482
2483               outrel.r_offset =
2484                 _bfd_elf_section_offset (output_bfd, info, input_section,
2485                                          rel->r_offset);
2486               if (outrel.r_offset == (bfd_vma) -1)
2487                 skip = TRUE;
2488               else if (outrel.r_offset == (bfd_vma) -2)
2489                 skip = TRUE, relocate = TRUE;
2490               outrel.r_offset += (input_section->output_section->vma
2491                                   + input_section->output_offset);
2492
2493               if (skip)
2494                 memset (&outrel, 0, sizeof outrel);
2495               else if (h != NULL
2496                        && h->dynindx != -1
2497                        && (r_type == R_386_PC32
2498                            || !info->shared
2499                            || !info->symbolic
2500                            || !h->def_regular))
2501                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2502               else
2503                 {
2504                   /* This symbol is local, or marked to become local.  */
2505                   relocate = TRUE;
2506                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2507                 }
2508
2509               sreloc = elf_section_data (input_section)->sreloc;
2510               if (sreloc == NULL)
2511                 abort ();
2512
2513               loc = sreloc->contents;
2514               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2515               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2516
2517               /* If this reloc is against an external symbol, we do
2518                  not want to fiddle with the addend.  Otherwise, we
2519                  need to include the symbol value so that it becomes
2520                  an addend for the dynamic reloc.  */
2521               if (! relocate)
2522                 continue;
2523             }
2524           break;
2525
2526         case R_386_TLS_IE:
2527           if (info->shared)
2528             {
2529               Elf_Internal_Rela outrel;
2530               bfd_byte *loc;
2531               asection *sreloc;
2532
2533               outrel.r_offset = rel->r_offset
2534                                 + input_section->output_section->vma
2535                                 + input_section->output_offset;
2536               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2537               sreloc = elf_section_data (input_section)->sreloc;
2538               if (sreloc == NULL)
2539                 abort ();
2540               loc = sreloc->contents;
2541               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2542               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2543             }
2544           /* Fall through */
2545
2546         case R_386_TLS_GD:
2547         case R_386_TLS_IE_32:
2548         case R_386_TLS_GOTIE:
2549           r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2550           tls_type = GOT_UNKNOWN;
2551           if (h == NULL && local_got_offsets)
2552             tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2553           else if (h != NULL)
2554             {
2555               tls_type = elf_i386_hash_entry(h)->tls_type;
2556               if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2557                 r_type = R_386_TLS_LE_32;
2558             }
2559           if (tls_type == GOT_TLS_IE)
2560             tls_type = GOT_TLS_IE_NEG;
2561           if (r_type == R_386_TLS_GD)
2562             {
2563               if (tls_type == GOT_TLS_IE_POS)
2564                 r_type = R_386_TLS_GOTIE;
2565               else if (tls_type & GOT_TLS_IE)
2566                 r_type = R_386_TLS_IE_32;
2567             }
2568
2569           if (r_type == R_386_TLS_LE_32)
2570             {
2571               BFD_ASSERT (! unresolved_reloc);
2572               if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2573                 {
2574                   unsigned int val, type;
2575                   bfd_vma roff;
2576
2577                   /* GD->LE transition.  */
2578                   BFD_ASSERT (rel->r_offset >= 2);
2579                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2580                   BFD_ASSERT (type == 0x8d || type == 0x04);
2581                   BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2582                   BFD_ASSERT (bfd_get_8 (input_bfd,
2583                                          contents + rel->r_offset + 4)
2584                               == 0xe8);
2585                   BFD_ASSERT (rel + 1 < relend);
2586                   BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2587                   roff = rel->r_offset + 5;
2588                   val = bfd_get_8 (input_bfd,
2589                                    contents + rel->r_offset - 1);
2590                   if (type == 0x04)
2591                     {
2592                       /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2593                          Change it into:
2594                          movl %gs:0, %eax; subl $foo@tpoff, %eax
2595                          (6 byte form of subl).  */
2596                       BFD_ASSERT (rel->r_offset >= 3);
2597                       BFD_ASSERT (bfd_get_8 (input_bfd,
2598                                              contents + rel->r_offset - 3)
2599                                   == 0x8d);
2600                       BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2601                       memcpy (contents + rel->r_offset - 3,
2602                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2603                     }
2604                   else
2605                     {
2606                       BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2607                       if (rel->r_offset + 10 <= input_section->size
2608                           && bfd_get_8 (input_bfd,
2609                                         contents + rel->r_offset + 9) == 0x90)
2610                         {
2611                           /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2612                              Change it into:
2613                              movl %gs:0, %eax; subl $foo@tpoff, %eax
2614                              (6 byte form of subl).  */
2615                           memcpy (contents + rel->r_offset - 2,
2616                                   "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2617                           roff = rel->r_offset + 6;
2618                         }
2619                       else
2620                         {
2621                           /* leal foo(%reg), %eax; call ___tls_get_addr
2622                              Change it into:
2623                              movl %gs:0, %eax; subl $foo@tpoff, %eax
2624                              (5 byte form of subl).  */
2625                           memcpy (contents + rel->r_offset - 2,
2626                                   "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2627                         }
2628                     }
2629                   bfd_put_32 (output_bfd, tpoff (info, relocation),
2630                               contents + roff);
2631                   /* Skip R_386_PLT32.  */
2632                   rel++;
2633                   continue;
2634                 }
2635               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2636                 {
2637                   unsigned int val, type;
2638
2639                   /* IE->LE transition:
2640                      Originally it can be one of:
2641                      movl foo, %eax
2642                      movl foo, %reg
2643                      addl foo, %reg
2644                      We change it into:
2645                      movl $foo, %eax
2646                      movl $foo, %reg
2647                      addl $foo, %reg.  */
2648                   BFD_ASSERT (rel->r_offset >= 1);
2649                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2650                   BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2651                   if (val == 0xa1)
2652                     {
2653                       /* movl foo, %eax.  */
2654                       bfd_put_8 (output_bfd, 0xb8,
2655                                  contents + rel->r_offset - 1);
2656                     }
2657                   else
2658                     {
2659                       BFD_ASSERT (rel->r_offset >= 2);
2660                       type = bfd_get_8 (input_bfd,
2661                                         contents + rel->r_offset - 2);
2662                       switch (type)
2663                         {
2664                         case 0x8b:
2665                           /* movl */
2666                           BFD_ASSERT ((val & 0xc7) == 0x05);
2667                           bfd_put_8 (output_bfd, 0xc7,
2668                                      contents + rel->r_offset - 2);
2669                           bfd_put_8 (output_bfd,
2670                                      0xc0 | ((val >> 3) & 7),
2671                                      contents + rel->r_offset - 1);
2672                           break;
2673                         case 0x03:
2674                           /* addl */
2675                           BFD_ASSERT ((val & 0xc7) == 0x05);
2676                           bfd_put_8 (output_bfd, 0x81,
2677                                      contents + rel->r_offset - 2);
2678                           bfd_put_8 (output_bfd,
2679                                      0xc0 | ((val >> 3) & 7),
2680                                      contents + rel->r_offset - 1);
2681                           break;
2682                         default:
2683                           BFD_FAIL ();
2684                           break;
2685                         }
2686                     }
2687                   bfd_put_32 (output_bfd, -tpoff (info, relocation),
2688                               contents + rel->r_offset);
2689                   continue;
2690                 }
2691               else
2692                 {
2693                   unsigned int val, type;
2694
2695                   /* {IE_32,GOTIE}->LE transition:
2696                      Originally it can be one of:
2697                      subl foo(%reg1), %reg2
2698                      movl foo(%reg1), %reg2
2699                      addl foo(%reg1), %reg2
2700                      We change it into:
2701                      subl $foo, %reg2
2702                      movl $foo, %reg2 (6 byte form)
2703                      addl $foo, %reg2.  */
2704                   BFD_ASSERT (rel->r_offset >= 2);
2705                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2706                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2707                   BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2708                   BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
2709                   if (type == 0x8b)
2710                     {
2711                       /* movl */
2712                       bfd_put_8 (output_bfd, 0xc7,
2713                                  contents + rel->r_offset - 2);
2714                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2715                                  contents + rel->r_offset - 1);
2716                     }
2717                   else if (type == 0x2b)
2718                     {
2719                       /* subl */
2720                       bfd_put_8 (output_bfd, 0x81,
2721                                  contents + rel->r_offset - 2);
2722                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2723                                  contents + rel->r_offset - 1);
2724                     }
2725                   else if (type == 0x03)
2726                     {
2727                       /* addl */
2728                       bfd_put_8 (output_bfd, 0x81,
2729                                  contents + rel->r_offset - 2);
2730                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2731                                  contents + rel->r_offset - 1);
2732                     }
2733                   else
2734                     BFD_FAIL ();
2735                   if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
2736                     bfd_put_32 (output_bfd, -tpoff (info, relocation),
2737                                 contents + rel->r_offset);
2738                   else
2739                     bfd_put_32 (output_bfd, tpoff (info, relocation),
2740                                 contents + rel->r_offset);
2741                   continue;
2742                 }
2743             }
2744
2745           if (htab->sgot == NULL)
2746             abort ();
2747
2748           if (h != NULL)
2749             off = h->got.offset;
2750           else
2751             {
2752               if (local_got_offsets == NULL)
2753                 abort ();
2754
2755               off = local_got_offsets[r_symndx];
2756             }
2757
2758           if ((off & 1) != 0)
2759             off &= ~1;
2760           else
2761             {
2762               Elf_Internal_Rela outrel;
2763               bfd_byte *loc;
2764               int dr_type, indx;
2765
2766               if (htab->srelgot == NULL)
2767                 abort ();
2768
2769               outrel.r_offset = (htab->sgot->output_section->vma
2770                                  + htab->sgot->output_offset + off);
2771
2772               indx = h && h->dynindx != -1 ? h->dynindx : 0;
2773               if (r_type == R_386_TLS_GD)
2774                 dr_type = R_386_TLS_DTPMOD32;
2775               else if (tls_type == GOT_TLS_IE_POS)
2776                 dr_type = R_386_TLS_TPOFF;
2777               else
2778                 dr_type = R_386_TLS_TPOFF32;
2779               if (dr_type == R_386_TLS_TPOFF && indx == 0)
2780                 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
2781                             htab->sgot->contents + off);
2782               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
2783                 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
2784                             htab->sgot->contents + off);
2785               else
2786                 bfd_put_32 (output_bfd, 0,
2787                             htab->sgot->contents + off);
2788               outrel.r_info = ELF32_R_INFO (indx, dr_type);
2789               loc = htab->srelgot->contents;
2790               loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
2791               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2792
2793               if (r_type == R_386_TLS_GD)
2794                 {
2795                   if (indx == 0)
2796                     {
2797                       BFD_ASSERT (! unresolved_reloc);
2798                       bfd_put_32 (output_bfd,
2799                                   relocation - dtpoff_base (info),
2800                                   htab->sgot->contents + off + 4);
2801                     }
2802                   else
2803                     {
2804                       bfd_put_32 (output_bfd, 0,
2805                                   htab->sgot->contents + off + 4);
2806                       outrel.r_info = ELF32_R_INFO (indx,
2807                                                     R_386_TLS_DTPOFF32);
2808                       outrel.r_offset += 4;
2809                       htab->srelgot->reloc_count++;
2810                       loc += sizeof (Elf32_External_Rel);
2811                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2812                     }
2813                 }
2814               else if (tls_type == GOT_TLS_IE_BOTH)
2815                 {
2816                   bfd_put_32 (output_bfd,
2817                               indx == 0 ? relocation - dtpoff_base (info) : 0,
2818                               htab->sgot->contents + off + 4);
2819                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2820                   outrel.r_offset += 4;
2821                   htab->srelgot->reloc_count++;
2822                   loc += sizeof (Elf32_External_Rel);
2823                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2824                 }
2825
2826               if (h != NULL)
2827                 h->got.offset |= 1;
2828               else
2829                 local_got_offsets[r_symndx] |= 1;
2830             }
2831
2832           if (off >= (bfd_vma) -2)
2833             abort ();
2834           if (r_type == ELF32_R_TYPE (rel->r_info))
2835             {
2836               bfd_vma g_o_t = htab->sgotplt->output_section->vma
2837                               + htab->sgotplt->output_offset;
2838               relocation = htab->sgot->output_section->vma
2839                            + htab->sgot->output_offset + off - g_o_t;
2840               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
2841                   && tls_type == GOT_TLS_IE_BOTH)
2842                 relocation += 4;
2843               if (r_type == R_386_TLS_IE)
2844                 relocation += g_o_t;
2845               unresolved_reloc = FALSE;
2846             }
2847           else
2848             {
2849               unsigned int val, type;
2850               bfd_vma roff;
2851
2852               /* GD->IE transition.  */
2853               BFD_ASSERT (rel->r_offset >= 2);
2854               type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2855               BFD_ASSERT (type == 0x8d || type == 0x04);
2856               BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2857               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2858                           == 0xe8);
2859               BFD_ASSERT (rel + 1 < relend);
2860               BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2861               roff = rel->r_offset - 3;
2862               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2863               if (type == 0x04)
2864                 {
2865                   /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2866                      Change it into:
2867                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
2868                   BFD_ASSERT (rel->r_offset >= 3);
2869                   BFD_ASSERT (bfd_get_8 (input_bfd,
2870                                          contents + rel->r_offset - 3)
2871                               == 0x8d);
2872                   BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2873                   val >>= 3;
2874                 }
2875               else
2876                 {
2877                   /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2878                      Change it into:
2879                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
2880                   BFD_ASSERT (rel->r_offset + 10 <= input_section->size);
2881                   BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2882                   BFD_ASSERT (bfd_get_8 (input_bfd,
2883                                          contents + rel->r_offset + 9)
2884                               == 0x90);
2885                   roff = rel->r_offset - 2;
2886                 }
2887               memcpy (contents + roff,
2888                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
2889               contents[roff + 7] = 0x80 | (val & 7);
2890               /* If foo is used only with foo@gotntpoff(%reg) and
2891                  foo@indntpoff, but not with foo@gottpoff(%reg), change
2892                  subl $foo@gottpoff(%reg), %eax
2893                  into:
2894                  addl $foo@gotntpoff(%reg), %eax.  */
2895               if (r_type == R_386_TLS_GOTIE)
2896                 {
2897                   contents[roff + 6] = 0x03;
2898                   if (tls_type == GOT_TLS_IE_BOTH)
2899                     off += 4;
2900                 }
2901               bfd_put_32 (output_bfd,
2902                           htab->sgot->output_section->vma
2903                           + htab->sgot->output_offset + off
2904                           - htab->sgotplt->output_section->vma
2905                           - htab->sgotplt->output_offset,
2906                           contents + roff + 8);
2907               /* Skip R_386_PLT32.  */
2908               rel++;
2909               continue;
2910             }
2911           break;
2912
2913         case R_386_TLS_LDM:
2914           if (! info->shared)
2915             {
2916               unsigned int val;
2917
2918               /* LD->LE transition:
2919                  Ensure it is:
2920                  leal foo(%reg), %eax; call ___tls_get_addr.
2921                  We change it into:
2922                  movl %gs:0, %eax; nop; leal 0(%esi,1), %esi.  */
2923               BFD_ASSERT (rel->r_offset >= 2);
2924               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2925                           == 0x8d);
2926               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2927               BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2928               BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2929               BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2930                           == 0xe8);
2931               BFD_ASSERT (rel + 1 < relend);
2932               BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2933               memcpy (contents + rel->r_offset - 2,
2934                       "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
2935               /* Skip R_386_PLT32.  */
2936               rel++;
2937               continue;
2938             }
2939
2940           if (htab->sgot == NULL)
2941             abort ();
2942
2943           off = htab->tls_ldm_got.offset;
2944           if (off & 1)
2945             off &= ~1;
2946           else
2947             {
2948               Elf_Internal_Rela outrel;
2949               bfd_byte *loc;
2950
2951               if (htab->srelgot == NULL)
2952                 abort ();
2953
2954               outrel.r_offset = (htab->sgot->output_section->vma
2955                                  + htab->sgot->output_offset + off);
2956
2957               bfd_put_32 (output_bfd, 0,
2958                           htab->sgot->contents + off);
2959               bfd_put_32 (output_bfd, 0,
2960                           htab->sgot->contents + off + 4);
2961               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
2962               loc = htab->srelgot->contents;
2963               loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
2964               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2965               htab->tls_ldm_got.offset |= 1;
2966             }
2967           relocation = htab->sgot->output_section->vma
2968                        + htab->sgot->output_offset + off
2969                        - htab->sgotplt->output_section->vma
2970                        - htab->sgotplt->output_offset;
2971           unresolved_reloc = FALSE;
2972           break;
2973
2974         case R_386_TLS_LDO_32:
2975           if (info->shared || (input_section->flags & SEC_CODE) == 0)
2976             relocation -= dtpoff_base (info);
2977           else
2978             /* When converting LDO to LE, we must negate.  */
2979             relocation = -tpoff (info, relocation);
2980           break;
2981
2982         case R_386_TLS_LE_32:
2983         case R_386_TLS_LE:
2984           if (info->shared)
2985             {
2986               Elf_Internal_Rela outrel;
2987               asection *sreloc;
2988               bfd_byte *loc;
2989               int indx;
2990
2991               outrel.r_offset = rel->r_offset
2992                                 + input_section->output_section->vma
2993                                 + input_section->output_offset;
2994               if (h != NULL && h->dynindx != -1)
2995                 indx = h->dynindx;
2996               else
2997                 indx = 0;
2998               if (r_type == R_386_TLS_LE_32)
2999                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3000               else
3001                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3002               sreloc = elf_section_data (input_section)->sreloc;
3003               if (sreloc == NULL)
3004                 abort ();
3005               loc = sreloc->contents;
3006               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3007               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3008               if (indx)
3009                 continue;
3010               else if (r_type == R_386_TLS_LE_32)
3011                 relocation = dtpoff_base (info) - relocation;
3012               else
3013                 relocation -= dtpoff_base (info);
3014             }
3015           else if (r_type == R_386_TLS_LE_32)
3016             relocation = tpoff (info, relocation);
3017           else
3018             relocation = -tpoff (info, relocation);
3019           break;
3020
3021         default:
3022           break;
3023         }
3024
3025       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3026          because such sections are not SEC_ALLOC and thus ld.so will
3027          not process them.  */
3028       if (unresolved_reloc
3029           && !((input_section->flags & SEC_DEBUGGING) != 0
3030                && h->def_dynamic))
3031         {
3032           (*_bfd_error_handler)
3033             (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
3034              input_bfd,
3035              input_section,
3036              (long) rel->r_offset,
3037              h->root.root.string);
3038           return FALSE;
3039         }
3040
3041       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3042                                     contents, rel->r_offset,
3043                                     relocation, 0);
3044
3045       if (r != bfd_reloc_ok)
3046         {
3047           const char *name;
3048
3049           if (h != NULL)
3050             name = h->root.root.string;
3051           else
3052             {
3053               name = bfd_elf_string_from_elf_section (input_bfd,
3054                                                       symtab_hdr->sh_link,
3055                                                       sym->st_name);
3056               if (name == NULL)
3057                 return FALSE;
3058               if (*name == '\0')
3059                 name = bfd_section_name (input_bfd, sec);
3060             }
3061
3062           if (r == bfd_reloc_overflow)
3063             {
3064               if (! ((*info->callbacks->reloc_overflow)
3065                      (info, (h ? &h->root : NULL), name, howto->name,
3066                       (bfd_vma) 0, input_bfd, input_section,
3067                       rel->r_offset)))
3068                 return FALSE;
3069             }
3070           else
3071             {
3072               (*_bfd_error_handler)
3073                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3074                  input_bfd, input_section,
3075                  (long) rel->r_offset, name, (int) r);
3076               return FALSE;
3077             }
3078         }
3079     }
3080
3081   return TRUE;
3082 }
3083
3084 /* Finish up dynamic symbol handling.  We set the contents of various
3085    dynamic sections here.  */
3086
3087 static bfd_boolean
3088 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3089                                 struct bfd_link_info *info,
3090                                 struct elf_link_hash_entry *h,
3091                                 Elf_Internal_Sym *sym)
3092 {
3093   struct elf_i386_link_hash_table *htab;
3094
3095   htab = elf_i386_hash_table (info);
3096
3097   if (h->plt.offset != (bfd_vma) -1)
3098     {
3099       bfd_vma plt_index;
3100       bfd_vma got_offset;
3101       Elf_Internal_Rela rel;
3102       bfd_byte *loc;
3103
3104       /* This symbol has an entry in the procedure linkage table.  Set
3105          it up.  */
3106
3107       if (h->dynindx == -1
3108           || htab->splt == NULL
3109           || htab->sgotplt == NULL
3110           || htab->srelplt == NULL)
3111         abort ();
3112
3113       /* Get the index in the procedure linkage table which
3114          corresponds to this symbol.  This is the index of this symbol
3115          in all the symbols for which we are making plt entries.  The
3116          first entry in the procedure linkage table is reserved.  */
3117       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3118
3119       /* Get the offset into the .got table of the entry that
3120          corresponds to this function.  Each .got entry is 4 bytes.
3121          The first three are reserved.  */
3122       got_offset = (plt_index + 3) * 4;
3123
3124       /* Fill in the entry in the procedure linkage table.  */
3125       if (! info->shared)
3126         {
3127           memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3128                   PLT_ENTRY_SIZE);
3129           bfd_put_32 (output_bfd,
3130                       (htab->sgotplt->output_section->vma
3131                        + htab->sgotplt->output_offset
3132                        + got_offset),
3133                       htab->splt->contents + h->plt.offset + 2);
3134
3135           if (htab->is_vxworks)
3136             {
3137               int s, k, reloc_index;
3138
3139               /* Create the R_386_32 relocation referencing the GOT
3140                  for this PLT entry.  */
3141
3142               /* S: Current slot number (zero-based).  */
3143               s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3144               /* K: Number of relocations for PLTResolve. */
3145               if (info->shared)
3146                 k = PLTRESOLVE_RELOCS_SHLIB;
3147               else
3148                 k = PLTRESOLVE_RELOCS;
3149               /* Skip the PLTresolve relocations, and the relocations for
3150                  the other PLT slots. */
3151               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3152               loc = (htab->srelplt2->contents + reloc_index
3153                      * sizeof (Elf32_External_Rel));
3154
3155               rel.r_offset = (htab->splt->output_section->vma
3156                               + htab->splt->output_offset
3157                               + h->plt.offset + 2),
3158               rel.r_info = ELF32_R_INFO (htab->hgot->indx, R_386_32);
3159               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3160
3161               /* Create the R_386_32 relocation referencing the beginning of
3162                  the PLT for this GOT entry.  */
3163               rel.r_offset = (htab->sgotplt->output_section->vma
3164                               + htab->sgotplt->output_offset
3165                               + got_offset);
3166               rel.r_info = ELF32_R_INFO (htab->hplt->indx, R_386_32);
3167               bfd_elf32_swap_reloc_out (output_bfd, &rel,
3168               loc + sizeof (Elf32_External_Rel));
3169             }
3170         }
3171       else
3172         {
3173           memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3174                   PLT_ENTRY_SIZE);
3175           bfd_put_32 (output_bfd, got_offset,
3176                       htab->splt->contents + h->plt.offset + 2);
3177         }
3178
3179       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3180                   htab->splt->contents + h->plt.offset + 7);
3181       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3182                   htab->splt->contents + h->plt.offset + 12);
3183
3184       /* Fill in the entry in the global offset table.  */
3185       bfd_put_32 (output_bfd,
3186                   (htab->splt->output_section->vma
3187                    + htab->splt->output_offset
3188                    + h->plt.offset
3189                    + 6),
3190                   htab->sgotplt->contents + got_offset);
3191
3192       /* Fill in the entry in the .rel.plt section.  */
3193       rel.r_offset = (htab->sgotplt->output_section->vma
3194                       + htab->sgotplt->output_offset
3195                       + got_offset);
3196       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3197       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3198       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3199
3200       if (!h->def_regular)
3201         {
3202           /* Mark the symbol as undefined, rather than as defined in
3203              the .plt section.  Leave the value if there were any
3204              relocations where pointer equality matters (this is a clue
3205              for the dynamic linker, to make function pointer
3206              comparisons work between an application and shared
3207              library), otherwise set it to zero.  If a function is only
3208              called from a binary, there is no need to slow down
3209              shared libraries because of that.  */
3210           sym->st_shndx = SHN_UNDEF;
3211           if (!h->pointer_equality_needed)
3212             sym->st_value = 0;
3213         }
3214     }
3215
3216   if (h->got.offset != (bfd_vma) -1
3217       && elf_i386_hash_entry(h)->tls_type != GOT_TLS_GD
3218       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3219     {
3220       Elf_Internal_Rela rel;
3221       bfd_byte *loc;
3222
3223       /* This symbol has an entry in the global offset table.  Set it
3224          up.  */
3225
3226       if (htab->sgot == NULL || htab->srelgot == NULL)
3227         abort ();
3228
3229       rel.r_offset = (htab->sgot->output_section->vma
3230                       + htab->sgot->output_offset
3231                       + (h->got.offset & ~(bfd_vma) 1));
3232
3233       /* If this is a static link, or it is a -Bsymbolic link and the
3234          symbol is defined locally or was forced to be local because
3235          of a version file, we just want to emit a RELATIVE reloc.
3236          The entry in the global offset table will already have been
3237          initialized in the relocate_section function.  */
3238       if (info->shared
3239           && SYMBOL_REFERENCES_LOCAL (info, h))
3240         {
3241           BFD_ASSERT((h->got.offset & 1) != 0);
3242           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3243         }
3244       else
3245         {
3246           BFD_ASSERT((h->got.offset & 1) == 0);
3247           bfd_put_32 (output_bfd, (bfd_vma) 0,
3248                       htab->sgot->contents + h->got.offset);
3249           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3250         }
3251
3252       loc = htab->srelgot->contents;
3253       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3254       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3255     }
3256
3257   if (h->needs_copy)
3258     {
3259       Elf_Internal_Rela rel;
3260       bfd_byte *loc;
3261
3262       /* This symbol needs a copy reloc.  Set it up.  */
3263
3264       if (h->dynindx == -1
3265           || (h->root.type != bfd_link_hash_defined
3266               && h->root.type != bfd_link_hash_defweak)
3267           || htab->srelbss == NULL)
3268         abort ();
3269
3270       rel.r_offset = (h->root.u.def.value
3271                       + h->root.u.def.section->output_section->vma
3272                       + h->root.u.def.section->output_offset);
3273       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3274       loc = htab->srelbss->contents;
3275       loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3276       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3277     }
3278
3279   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3280      On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3281      is relative to the ".got" section.  */
3282   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3283       || (strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3284           && !htab->is_vxworks))
3285     sym->st_shndx = SHN_ABS;
3286
3287   return TRUE;
3288 }
3289
3290 /* Used to decide how to sort relocs in an optimal manner for the
3291    dynamic linker, before writing them out.  */
3292
3293 static enum elf_reloc_type_class
3294 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3295 {
3296   switch (ELF32_R_TYPE (rela->r_info))
3297     {
3298     case R_386_RELATIVE:
3299       return reloc_class_relative;
3300     case R_386_JUMP_SLOT:
3301       return reloc_class_plt;
3302     case R_386_COPY:
3303       return reloc_class_copy;
3304     default:
3305       return reloc_class_normal;
3306     }
3307 }
3308
3309 /* Finish up the dynamic sections.  */
3310
3311 static bfd_boolean
3312 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3313                                   struct bfd_link_info *info)
3314 {
3315   struct elf_i386_link_hash_table *htab;
3316   bfd *dynobj;
3317   asection *sdyn;
3318
3319   htab = elf_i386_hash_table (info);
3320   dynobj = htab->elf.dynobj;
3321   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3322
3323   if (htab->elf.dynamic_sections_created)
3324     {
3325       Elf32_External_Dyn *dyncon, *dynconend;
3326
3327       if (sdyn == NULL || htab->sgot == NULL)
3328         abort ();
3329
3330       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3331       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3332       for (; dyncon < dynconend; dyncon++)
3333         {
3334           Elf_Internal_Dyn dyn;
3335           asection *s;
3336
3337           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3338
3339           switch (dyn.d_tag)
3340             {
3341             default:
3342               continue;
3343
3344             case DT_PLTGOT:
3345               s = htab->sgotplt;
3346               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3347               break;
3348
3349             case DT_JMPREL:
3350               s = htab->srelplt;
3351               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3352               break;
3353
3354             case DT_PLTRELSZ:
3355               s = htab->srelplt;
3356               dyn.d_un.d_val = s->size;
3357               break;
3358
3359             case DT_RELSZ:
3360               /* My reading of the SVR4 ABI indicates that the
3361                  procedure linkage table relocs (DT_JMPREL) should be
3362                  included in the overall relocs (DT_REL).  This is
3363                  what Solaris does.  However, UnixWare can not handle
3364                  that case.  Therefore, we override the DT_RELSZ entry
3365                  here to make it not include the JMPREL relocs.  */
3366               s = htab->srelplt;
3367               if (s == NULL)
3368                 continue;
3369               dyn.d_un.d_val -= s->size;
3370               break;
3371
3372             case DT_REL:
3373               /* We may not be using the standard ELF linker script.
3374                  If .rel.plt is the first .rel section, we adjust
3375                  DT_REL to not include it.  */
3376               s = htab->srelplt;
3377               if (s == NULL)
3378                 continue;
3379               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3380                 continue;
3381               dyn.d_un.d_ptr += s->size;
3382               break;
3383             }
3384
3385           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3386         }
3387
3388       /* Fill in the first entry in the procedure linkage table.  */
3389       if (htab->splt && htab->splt->size > 0)
3390         {
3391           if (info->shared)
3392             {
3393               memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3394                       sizeof (elf_i386_pic_plt0_entry));
3395               memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3396                       htab->plt0_pad_byte,
3397                       PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3398             }
3399           else
3400             {
3401               memcpy (htab->splt->contents, elf_i386_plt0_entry,
3402                       sizeof(elf_i386_plt0_entry));
3403               memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3404                       htab->plt0_pad_byte,
3405                       PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3406               bfd_put_32 (output_bfd,
3407                           (htab->sgotplt->output_section->vma
3408                            + htab->sgotplt->output_offset
3409                            + 4),
3410                           htab->splt->contents + 2);
3411               bfd_put_32 (output_bfd,
3412                           (htab->sgotplt->output_section->vma
3413                            + htab->sgotplt->output_offset
3414                            + 8),
3415                           htab->splt->contents + 8);
3416
3417               if (htab->is_vxworks)
3418                 {
3419                   Elf_Internal_Rela rel;
3420                   struct elf_link_hash_entry *hgot;
3421
3422                   /* The VxWorks GOT is relocated by the dynamic linker.
3423                      Therefore, we must emit relocations rather than
3424                      simply computing the values now.  */
3425                   hgot = elf_link_hash_lookup (elf_hash_table (info),
3426                                                "_GLOBAL_OFFSET_TABLE_",
3427                                                FALSE, FALSE, FALSE);
3428                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
3429                      On IA32 we use REL relocations so the addend goes in
3430                      the PLT directly.  */
3431                   rel.r_offset = (htab->splt->output_section->vma
3432                                   + htab->splt->output_offset
3433                                   + 2);
3434                   rel.r_info = ELF32_R_INFO (hgot->indx, R_386_32);
3435                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
3436                                             htab->srelplt2->contents);
3437                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
3438                   rel.r_offset = (htab->splt->output_section->vma
3439                                   + htab->splt->output_offset
3440                                   + 8);
3441                   rel.r_info = ELF32_R_INFO (hgot->indx, R_386_32);
3442                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
3443                                             htab->srelplt2->contents +
3444                                             sizeof (Elf32_External_Rel));
3445                 }
3446             }
3447
3448           /* UnixWare sets the entsize of .plt to 4, although that doesn't
3449              really seem like the right value.  */
3450           elf_section_data (htab->splt->output_section)
3451             ->this_hdr.sh_entsize = 4;
3452
3453           /* Correct the .rel.plt.unloaded relocations.  */
3454           if (htab->is_vxworks && !info->shared)
3455             {
3456               int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
3457               unsigned char *p;
3458
3459               p = htab->srelplt2->contents;
3460               if (info->shared)
3461                 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
3462               else
3463                 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
3464
3465               for (; num_plts; num_plts--)
3466                 {
3467                   Elf_Internal_Rela rel;
3468                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3469                   rel.r_info = ELF32_R_INFO (htab->hgot->indx, R_386_32);
3470                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3471                   p += sizeof (Elf32_External_Rel);
3472
3473                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
3474                   rel.r_info = ELF32_R_INFO (htab->hplt->indx, R_386_32);
3475                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
3476                   p += sizeof (Elf32_External_Rel);
3477                 }
3478             }
3479         }
3480     }
3481
3482   if (htab->sgotplt)
3483     {
3484       /* Fill in the first three entries in the global offset table.  */
3485       if (htab->sgotplt->size > 0)
3486         {
3487           bfd_put_32 (output_bfd,
3488                       (sdyn == NULL ? 0
3489                        : sdyn->output_section->vma + sdyn->output_offset),
3490                       htab->sgotplt->contents);
3491           bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
3492           bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
3493         }
3494
3495       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3496     }
3497
3498   if (htab->sgot && htab->sgot->size > 0)
3499     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3500
3501   return TRUE;
3502 }
3503
3504 /* Return address for Ith PLT stub in section PLT, for relocation REL
3505    or (bfd_vma) -1 if it should not be included.  */
3506
3507 static bfd_vma
3508 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
3509                       const arelent *rel ATTRIBUTE_UNUSED)
3510 {
3511   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3512 }
3513
3514
3515 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vec
3516 #define TARGET_LITTLE_NAME              "elf32-i386"
3517 #define ELF_ARCH                        bfd_arch_i386
3518 #define ELF_MACHINE_CODE                EM_386
3519 #define ELF_MAXPAGESIZE                 0x1000
3520
3521 #define elf_backend_can_gc_sections     1
3522 #define elf_backend_can_refcount        1
3523 #define elf_backend_want_got_plt        1
3524 #define elf_backend_plt_readonly        1
3525 #define elf_backend_want_plt_sym        0
3526 #define elf_backend_got_header_size     12
3527
3528 /* Support RELA for objdump of prelink objects.  */
3529 #define elf_info_to_howto                     elf_i386_info_to_howto_rel
3530 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
3531
3532 #define bfd_elf32_mkobject                    elf_i386_mkobject
3533
3534 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
3535 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
3536 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
3537
3538 #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
3539 #define elf_backend_check_relocs              elf_i386_check_relocs
3540 #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
3541 #define elf_backend_create_dynamic_sections   elf_i386_create_dynamic_sections
3542 #define elf_backend_fake_sections             elf_i386_fake_sections
3543 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
3544 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
3545 #define elf_backend_gc_mark_hook              elf_i386_gc_mark_hook
3546 #define elf_backend_gc_sweep_hook             elf_i386_gc_sweep_hook
3547 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
3548 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
3549 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
3550 #define elf_backend_relocate_section          elf_i386_relocate_section
3551 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
3552 #define elf_backend_plt_sym_val               elf_i386_plt_sym_val
3553
3554 #include "elf32-target.h"
3555
3556 /* FreeBSD support.  */
3557
3558 #undef  TARGET_LITTLE_SYM
3559 #define TARGET_LITTLE_SYM               bfd_elf32_i386_freebsd_vec
3560 #undef  TARGET_LITTLE_NAME
3561 #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
3562
3563 /* The kernel recognizes executables as valid only if they carry a
3564    "FreeBSD" label in the ELF header.  So we put this label on all
3565    executables and (for simplicity) also all other object files.  */
3566
3567 static void
3568 elf_i386_post_process_headers (bfd *abfd,
3569                                struct bfd_link_info *info ATTRIBUTE_UNUSED)
3570 {
3571   Elf_Internal_Ehdr *i_ehdrp;
3572
3573   i_ehdrp = elf_elfheader (abfd);
3574
3575   /* Put an ABI label supported by FreeBSD >= 4.1.  */
3576   i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
3577 #ifdef OLD_FREEBSD_ABI_LABEL
3578   /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
3579   memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
3580 #endif
3581 }
3582
3583 #undef  elf_backend_post_process_headers
3584 #define elf_backend_post_process_headers        elf_i386_post_process_headers
3585 #undef  elf32_bed
3586 #define elf32_bed                               elf32_i386_fbsd_bed
3587
3588 #include "elf32-target.h"
3589
3590 /* VxWorks support.  */
3591
3592 #undef  TARGET_LITTLE_SYM
3593 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vxworks_vec
3594 #undef  TARGET_LITTLE_NAME
3595 #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
3596
3597
3598 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks.  */
3599
3600 static struct bfd_link_hash_table *
3601 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
3602 {
3603   struct bfd_link_hash_table *ret;
3604   struct elf_i386_link_hash_table *htab;
3605
3606   ret = elf_i386_link_hash_table_create (abfd);
3607   if (ret)
3608     {
3609       htab = (struct elf_i386_link_hash_table *) ret;
3610       htab->is_vxworks = 1;
3611       htab->plt0_pad_byte = 0x90;
3612     }
3613
3614   return ret;
3615 }
3616
3617
3618 /* Tweak magic VxWorks symbols as they are written to the output file.  */
3619 static bfd_boolean
3620 elf_i386_vxworks_link_output_symbol_hook (struct bfd_link_info *info
3621                                             ATTRIBUTE_UNUSED,
3622                                           const char *name,
3623                                           Elf_Internal_Sym *sym,
3624                                           asection *input_sec ATTRIBUTE_UNUSED,
3625                                           struct elf_link_hash_entry *h
3626                                             ATTRIBUTE_UNUSED)
3627 {
3628   /* Ignore the first dummy symbol.  */
3629   if (!name)
3630     return TRUE;
3631
3632   return elf_vxworks_link_output_symbol_hook (name, sym);
3633 }
3634
3635 #undef  elf_backend_post_process_headers
3636 #undef bfd_elf32_bfd_link_hash_table_create
3637 #define bfd_elf32_bfd_link_hash_table_create \
3638   elf_i386_vxworks_link_hash_table_create
3639 #undef elf_backend_add_symbol_hook
3640 #define elf_backend_add_symbol_hook \
3641   elf_vxworks_add_symbol_hook
3642 #undef elf_backend_link_output_symbol_hook
3643 #define elf_backend_link_output_symbol_hook \
3644   elf_i386_vxworks_link_output_symbol_hook
3645 #undef elf_backend_emit_relocs
3646 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
3647 #undef elf_backend_final_write_processing
3648 #define elf_backend_final_write_processing \
3649   elf_vxworks_final_write_processing
3650
3651 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
3652    define it.  */
3653 #undef elf_backend_want_plt_sym
3654 #define elf_backend_want_plt_sym        1
3655
3656 #undef  elf32_bed
3657 #define elf32_bed                               elf32_i386_vxworks_bed
3658
3659 #include "elf32-target.h"