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