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