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