* testsuite/Makefile.am: Add -ffunction-sections to compile
[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 GD 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       /* Use strncmp to check ___tls_get_addr since ___tls_get_addr
1011          may be versioned.  */
1012       return (h != NULL
1013               && h->root.root.string != NULL
1014               && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
1015                   || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
1016               && (strncmp (h->root.root.string, "___tls_get_addr",
1017                            15) == 0));
1018
1019     case R_386_TLS_IE:
1020       /* Check transition from IE access model:
1021                 movl foo@indntpoff(%rip), %eax
1022                 movl foo@indntpoff(%rip), %reg
1023                 addl foo@indntpoff(%rip), %reg
1024        */
1025
1026       if (offset < 1 || (offset + 4) > sec->size)
1027         return FALSE;
1028
1029       /* Check "movl foo@tpoff(%rip), %eax" first.  */
1030       val = bfd_get_8 (abfd, contents + offset - 1);
1031       if (val == 0xa1)
1032         return TRUE;
1033
1034       if (offset < 2)
1035         return FALSE;
1036
1037       /* Check movl|addl foo@tpoff(%rip), %reg.   */
1038       type = bfd_get_8 (abfd, contents + offset - 2);
1039       return ((type == 0x8b || type == 0x03)
1040               && (val & 0xc7) == 0x05);
1041
1042     case R_386_TLS_GOTIE:
1043     case R_386_TLS_IE_32:
1044       /* Check transition from {IE_32,GOTIE} access model:
1045                 subl foo@{tpoff,gontoff}(%reg1), %reg2
1046                 movl foo@{tpoff,gontoff}(%reg1), %reg2
1047                 addl foo@{tpoff,gontoff}(%reg1), %reg2
1048        */
1049
1050       if (offset < 2 || (offset + 4) > sec->size)
1051         return FALSE;
1052
1053       val = bfd_get_8 (abfd, contents + offset - 1);
1054       if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1055         return FALSE;
1056
1057       type = bfd_get_8 (abfd, contents + offset - 2);
1058       return type == 0x8b || type == 0x2b || type == 0x03;
1059
1060     case R_386_TLS_GOTDESC:
1061       /* Check transition from GDesc access model:
1062                 leal x@tlsdesc(%ebx), %eax
1063
1064          Make sure it's a leal adding ebx to a 32-bit offset
1065          into any register, although it's probably almost always
1066          going to be eax.  */
1067
1068       if (offset < 2 || (offset + 4) > sec->size)
1069         return FALSE;
1070
1071       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1072         return FALSE;
1073
1074       val = bfd_get_8 (abfd, contents + offset - 1);
1075       return (val & 0xc7) == 0x83;
1076
1077     case R_386_TLS_DESC_CALL:
1078       /* Check transition from GDesc access model:
1079                 call *x@tlsdesc(%rax)
1080        */
1081       if (offset + 2 <= sec->size)
1082         {
1083           /* Make sure that it's a call *x@tlsdesc(%rax).  */
1084           static i386_opcode16 call = { { 0xff, 0x10 } };
1085           return bfd_get_16 (abfd, contents + offset) == call.i;
1086         }
1087
1088       return FALSE;
1089
1090     default:
1091       abort ();
1092     }
1093 }
1094
1095 /* Return TRUE if the TLS access transition is OK or no transition
1096    will be performed.  Update R_TYPE if there is a transition.  */
1097
1098 static bfd_boolean
1099 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1100                          asection *sec, bfd_byte *contents,
1101                          Elf_Internal_Shdr *symtab_hdr,
1102                          struct elf_link_hash_entry **sym_hashes,
1103                          unsigned int *r_type, int tls_type,
1104                          const Elf_Internal_Rela *rel,
1105                          const Elf_Internal_Rela *relend,
1106                          struct elf_link_hash_entry *h)
1107 {
1108   unsigned int from_type = *r_type;
1109   unsigned int to_type = from_type;
1110   bfd_boolean check = TRUE;
1111
1112   switch (from_type)
1113     {
1114     case R_386_TLS_GD:
1115     case R_386_TLS_GOTDESC:
1116     case R_386_TLS_DESC_CALL:
1117     case R_386_TLS_IE_32:
1118     case R_386_TLS_IE:
1119     case R_386_TLS_GOTIE:
1120       if (!info->shared)
1121         {
1122           if (h == NULL)
1123             to_type = R_386_TLS_LE_32;
1124           else if (from_type != R_386_TLS_IE
1125                    && from_type != R_386_TLS_GOTIE)
1126             to_type = R_386_TLS_IE_32;
1127         }
1128
1129       /* When we are called from elf_i386_relocate_section, CONTENTS
1130          isn't NULL and there may be additional transitions based on
1131          TLS_TYPE.  */
1132       if (contents != NULL)
1133         {
1134           unsigned int new_to_type = to_type;
1135
1136           if (!info->shared
1137               && h != NULL
1138               && h->dynindx == -1
1139               && (tls_type & GOT_TLS_IE))
1140             new_to_type = R_386_TLS_LE_32;
1141
1142           if (to_type == R_386_TLS_GD
1143               || to_type == R_386_TLS_GOTDESC
1144               || to_type == R_386_TLS_DESC_CALL)
1145             {
1146               if (tls_type == GOT_TLS_IE_POS)
1147                 new_to_type = R_386_TLS_GOTIE;
1148               else if (tls_type & GOT_TLS_IE)
1149                 new_to_type = R_386_TLS_IE_32;
1150             }
1151
1152           /* We checked the transition before when we were called from
1153              elf_i386_check_relocs.  We only want to check the new
1154              transition which hasn't been checked before.  */
1155           check = new_to_type != to_type && from_type == to_type;
1156           to_type = new_to_type;
1157         }
1158
1159       break;
1160
1161     case R_386_TLS_LDM:
1162       if (!info->shared)
1163         to_type = R_386_TLS_LE_32;
1164       break;
1165
1166     default:
1167       return TRUE;
1168     }
1169
1170   /* Return TRUE if there is no transition.  */
1171   if (from_type == to_type)
1172     return TRUE;
1173
1174   /* Check if the transition can be performed.  */
1175   if (check
1176       && ! elf_i386_check_tls_transition (abfd, sec, contents,
1177                                           symtab_hdr, sym_hashes,
1178                                           from_type, rel, relend))
1179     {
1180       reloc_howto_type *from, *to;
1181
1182       from = elf_i386_rtype_to_howto (abfd, from_type);
1183       to = elf_i386_rtype_to_howto (abfd, to_type);
1184
1185       (*_bfd_error_handler)
1186         (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1187            "in section `%A' failed"),
1188          abfd, sec, from->name, to->name,
1189          h ? h->root.root.string : "a local symbol",
1190          (unsigned long) rel->r_offset);
1191       bfd_set_error (bfd_error_bad_value);
1192       return FALSE;
1193     }
1194
1195   *r_type = to_type;
1196   return TRUE;
1197 }
1198
1199 /* Returns true if the hash entry refers to a symbol
1200    marked for indirect handling during reloc processing.  */
1201
1202 static bfd_boolean
1203 is_indirect_symbol (bfd * abfd, struct elf_link_hash_entry * h)
1204 {
1205   const struct elf_backend_data * bed;
1206
1207   if (abfd == NULL || h == NULL)
1208     return FALSE;
1209
1210   bed = get_elf_backend_data (abfd);
1211
1212   return h->type == STT_GNU_IFUNC
1213     && (bed->elf_osabi == ELFOSABI_LINUX
1214         /* GNU/Linux is still using the default value 0.  */
1215         || bed->elf_osabi == ELFOSABI_NONE);
1216 }
1217
1218 /* Look through the relocs for a section during the first phase, and
1219    calculate needed space in the global offset table, procedure linkage
1220    table, and dynamic reloc sections.  */
1221
1222 static bfd_boolean
1223 elf_i386_check_relocs (bfd *abfd,
1224                        struct bfd_link_info *info,
1225                        asection *sec,
1226                        const Elf_Internal_Rela *relocs)
1227 {
1228   struct elf_i386_link_hash_table *htab;
1229   Elf_Internal_Shdr *symtab_hdr;
1230   struct elf_link_hash_entry **sym_hashes;
1231   const Elf_Internal_Rela *rel;
1232   const Elf_Internal_Rela *rel_end;
1233   asection *sreloc;
1234
1235   if (info->relocatable)
1236     return TRUE;
1237
1238   BFD_ASSERT (is_i386_elf (abfd));
1239
1240   htab = elf_i386_hash_table (info);
1241   symtab_hdr = &elf_symtab_hdr (abfd);
1242   sym_hashes = elf_sym_hashes (abfd);
1243
1244   sreloc = NULL;
1245
1246   rel_end = relocs + sec->reloc_count;
1247   for (rel = relocs; rel < rel_end; rel++)
1248     {
1249       unsigned int r_type;
1250       unsigned long r_symndx;
1251       struct elf_link_hash_entry *h;
1252
1253       r_symndx = ELF32_R_SYM (rel->r_info);
1254       r_type = ELF32_R_TYPE (rel->r_info);
1255
1256       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1257         {
1258           (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1259                                  abfd,
1260                                  r_symndx);
1261           return FALSE;
1262         }
1263
1264       if (r_symndx < symtab_hdr->sh_info)
1265         h = NULL;
1266       else
1267         {
1268           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1269           while (h->root.type == bfd_link_hash_indirect
1270                  || h->root.type == bfd_link_hash_warning)
1271             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1272         }
1273
1274       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1275                                      symtab_hdr, sym_hashes,
1276                                      &r_type, GOT_UNKNOWN,
1277                                      rel, rel_end, h)) 
1278         return FALSE;
1279
1280       switch (r_type)
1281         {
1282         case R_386_TLS_LDM:
1283           htab->tls_ldm_got.refcount += 1;
1284           goto create_got;
1285
1286         case R_386_PLT32:
1287           /* This symbol requires a procedure linkage table entry.  We
1288              actually build the entry in adjust_dynamic_symbol,
1289              because this might be a case of linking PIC code which is
1290              never referenced by a dynamic object, in which case we
1291              don't need to generate a procedure linkage table entry
1292              after all.  */
1293
1294           /* If this is a local symbol, we resolve it directly without
1295              creating a procedure linkage table entry.  */
1296           if (h == NULL)
1297             continue;
1298
1299           h->needs_plt = 1;
1300           h->plt.refcount += 1;
1301           break;
1302
1303         case R_386_TLS_IE_32:
1304         case R_386_TLS_IE:
1305         case R_386_TLS_GOTIE:
1306           if (info->shared)
1307             info->flags |= DF_STATIC_TLS;
1308           /* Fall through */
1309
1310         case R_386_GOT32:
1311         case R_386_TLS_GD:
1312         case R_386_TLS_GOTDESC:
1313         case R_386_TLS_DESC_CALL:
1314           /* This symbol requires a global offset table entry.  */
1315           {
1316             int tls_type, old_tls_type;
1317
1318             switch (r_type)
1319               {
1320               default:
1321               case R_386_GOT32: tls_type = GOT_NORMAL; break;
1322               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1323               case R_386_TLS_GOTDESC:
1324               case R_386_TLS_DESC_CALL:
1325                 tls_type = GOT_TLS_GDESC; break;
1326               case R_386_TLS_IE_32:
1327                 if (ELF32_R_TYPE (rel->r_info) == r_type)
1328                   tls_type = GOT_TLS_IE_NEG;
1329                 else
1330                   /* If this is a GD->IE transition, we may use either of
1331                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
1332                   tls_type = GOT_TLS_IE;
1333                 break;
1334               case R_386_TLS_IE:
1335               case R_386_TLS_GOTIE:
1336                 tls_type = GOT_TLS_IE_POS; break;
1337               }
1338
1339             if (h != NULL)
1340               {
1341                 h->got.refcount += 1;
1342                 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1343               }
1344             else
1345               {
1346                 bfd_signed_vma *local_got_refcounts;
1347
1348                 /* This is a global offset table entry for a local symbol.  */
1349                 local_got_refcounts = elf_local_got_refcounts (abfd);
1350                 if (local_got_refcounts == NULL)
1351                   {
1352                     bfd_size_type size;
1353
1354                     size = symtab_hdr->sh_info;
1355                     size *= (sizeof (bfd_signed_vma)
1356                              + sizeof (bfd_vma) + sizeof(char));
1357                     local_got_refcounts = bfd_zalloc (abfd, size);
1358                     if (local_got_refcounts == NULL)
1359                       return FALSE;
1360                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1361                     elf_i386_local_tlsdesc_gotent (abfd)
1362                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1363                     elf_i386_local_got_tls_type (abfd)
1364                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1365                   }
1366                 local_got_refcounts[r_symndx] += 1;
1367                 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1368               }
1369
1370             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1371               tls_type |= old_tls_type;
1372             /* If a TLS symbol is accessed using IE at least once,
1373                there is no point to use dynamic model for it.  */
1374             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1375                      && (! GOT_TLS_GD_ANY_P (old_tls_type)
1376                          || (tls_type & GOT_TLS_IE) == 0))
1377               {
1378                 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1379                   tls_type = old_tls_type;
1380                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1381                          && GOT_TLS_GD_ANY_P (tls_type))
1382                   tls_type |= old_tls_type;
1383                 else
1384                   {
1385                     (*_bfd_error_handler)
1386                       (_("%B: `%s' accessed both as normal and "
1387                          "thread local symbol"),
1388                        abfd,
1389                        h ? h->root.root.string : "<local>");
1390                     return FALSE;
1391                   }
1392               }
1393
1394             if (old_tls_type != tls_type)
1395               {
1396                 if (h != NULL)
1397                   elf_i386_hash_entry (h)->tls_type = tls_type;
1398                 else
1399                   elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1400               }
1401           }
1402           /* Fall through */
1403
1404         case R_386_GOTOFF:
1405         case R_386_GOTPC:
1406         create_got:
1407           if (htab->sgot == NULL)
1408             {
1409               if (htab->elf.dynobj == NULL)
1410                 htab->elf.dynobj = abfd;
1411               if (!create_got_section (htab->elf.dynobj, info))
1412                 return FALSE;
1413             }
1414           if (r_type != R_386_TLS_IE)
1415             break;
1416           /* Fall through */
1417
1418         case R_386_TLS_LE_32:
1419         case R_386_TLS_LE:
1420           if (!info->shared)
1421             break;
1422           info->flags |= DF_STATIC_TLS;
1423           /* Fall through */
1424
1425         case R_386_32:
1426         case R_386_PC32:
1427           if (h != NULL && !info->shared)
1428             {
1429               /* If this reloc is in a read-only section, we might
1430                  need a copy reloc.  We can't check reliably at this
1431                  stage whether the section is read-only, as input
1432                  sections have not yet been mapped to output sections.
1433                  Tentatively set the flag for now, and correct in
1434                  adjust_dynamic_symbol.  */
1435               h->non_got_ref = 1;
1436
1437               /* We may need a .plt entry if the function this reloc
1438                  refers to is in a shared lib.  */
1439               h->plt.refcount += 1;
1440               if (r_type != R_386_PC32)
1441                 h->pointer_equality_needed = 1;
1442             }
1443
1444           /* If we are creating a shared library, and this is a reloc
1445              against a global symbol, or a non PC relative reloc
1446              against a local symbol, then we need to copy the reloc
1447              into the shared library.  However, if we are linking with
1448              -Bsymbolic, we do not need to copy a reloc against a
1449              global symbol which is defined in an object we are
1450              including in the link (i.e., DEF_REGULAR is set).  At
1451              this point we have not seen all the input files, so it is
1452              possible that DEF_REGULAR is not set now but will be set
1453              later (it is never cleared).  In case of a weak definition,
1454              DEF_REGULAR may be cleared later by a strong definition in
1455              a shared library.  We account for that possibility below by
1456              storing information in the relocs_copied field of the hash
1457              table entry.  A similar situation occurs when creating
1458              shared libraries and symbol visibility changes render the
1459              symbol local.
1460
1461              If on the other hand, we are creating an executable, we
1462              may need to keep relocations for symbols satisfied by a
1463              dynamic library if we manage to avoid copy relocs for the
1464              symbol.  */
1465           if ((info->shared
1466                && (sec->flags & SEC_ALLOC) != 0
1467                && (r_type != R_386_PC32
1468                    || (h != NULL
1469                        && (! SYMBOLIC_BIND (info, h)
1470                            || h->root.type == bfd_link_hash_defweak
1471                            || !h->def_regular))))
1472               || (ELIMINATE_COPY_RELOCS
1473                   && !info->shared
1474                   && (sec->flags & SEC_ALLOC) != 0
1475                   && h != NULL
1476                   && (h->root.type == bfd_link_hash_defweak
1477                       || !h->def_regular)))
1478             {
1479               struct elf_i386_dyn_relocs *p;
1480               struct elf_i386_dyn_relocs **head;
1481
1482               /* We must copy these reloc types into the output file.
1483                  Create a reloc section in dynobj and make room for
1484                  this reloc.  */
1485               if (sreloc == NULL)
1486                 {
1487                   if (htab->elf.dynobj == NULL)
1488                     htab->elf.dynobj = abfd;
1489
1490                   sreloc = _bfd_elf_make_dynamic_reloc_section
1491                     (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1492
1493                   if (sreloc == NULL)
1494                     return FALSE;
1495
1496                   /* Create the ifunc section as well, even if we have not encountered a
1497                      indirect function symbol yet.  We may not even see one in the input
1498                      object file, but we can still encounter them in libraries.  */
1499                   (void) _bfd_elf_make_ifunc_reloc_section
1500                     (abfd, sec, htab->elf.dynobj, 2);
1501                 }
1502
1503               /* If this is a global symbol, we count the number of
1504                  relocations we need for this symbol.  */
1505               if (h != NULL)
1506                 {
1507                   head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1508                 }
1509               else
1510                 {
1511                   void **vpp;
1512                   /* Track dynamic relocs needed for local syms too.
1513                      We really need local syms available to do this
1514                      easily.  Oh well.  */
1515
1516                   asection *s;
1517                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1518                                                  sec, r_symndx);
1519                   if (s == NULL)
1520                     return FALSE;
1521
1522                   vpp = &elf_section_data (s)->local_dynrel;
1523                   head = (struct elf_i386_dyn_relocs **)vpp;
1524                 }
1525
1526               p = *head;
1527               if (p == NULL || p->sec != sec)
1528                 {
1529                   bfd_size_type amt = sizeof *p;
1530                   p = bfd_alloc (htab->elf.dynobj, amt);
1531                   if (p == NULL)
1532                     return FALSE;
1533                   p->next = *head;
1534                   *head = p;
1535                   p->sec = sec;
1536                   p->count = 0;
1537                   p->pc_count = 0;
1538                 }
1539
1540               p->count += 1;
1541               if (r_type == R_386_PC32)
1542                 p->pc_count += 1;
1543             }
1544           break;
1545
1546           /* This relocation describes the C++ object vtable hierarchy.
1547              Reconstruct it for later use during GC.  */
1548         case R_386_GNU_VTINHERIT:
1549           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1550             return FALSE;
1551           break;
1552
1553           /* This relocation describes which C++ vtable entries are actually
1554              used.  Record for later use during GC.  */
1555         case R_386_GNU_VTENTRY:
1556           BFD_ASSERT (h != NULL);
1557           if (h != NULL
1558               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1559             return FALSE;
1560           break;
1561
1562         default:
1563           break;
1564         }
1565     }
1566
1567   return TRUE;
1568 }
1569
1570 /* Return the section that should be marked against GC for a given
1571    relocation.  */
1572
1573 static asection *
1574 elf_i386_gc_mark_hook (asection *sec,
1575                        struct bfd_link_info *info,
1576                        Elf_Internal_Rela *rel,
1577                        struct elf_link_hash_entry *h,
1578                        Elf_Internal_Sym *sym)
1579 {
1580   if (h != NULL)
1581     switch (ELF32_R_TYPE (rel->r_info))
1582       {
1583       case R_386_GNU_VTINHERIT:
1584       case R_386_GNU_VTENTRY:
1585         return NULL;
1586       }
1587
1588   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1589 }
1590
1591 /* Update the got entry reference counts for the section being removed.  */
1592
1593 static bfd_boolean
1594 elf_i386_gc_sweep_hook (bfd *abfd,
1595                         struct bfd_link_info *info,
1596                         asection *sec,
1597                         const Elf_Internal_Rela *relocs)
1598 {
1599   Elf_Internal_Shdr *symtab_hdr;
1600   struct elf_link_hash_entry **sym_hashes;
1601   bfd_signed_vma *local_got_refcounts;
1602   const Elf_Internal_Rela *rel, *relend;
1603
1604   if (info->relocatable)
1605     return TRUE;
1606
1607   elf_section_data (sec)->local_dynrel = NULL;
1608
1609   symtab_hdr = &elf_symtab_hdr (abfd);
1610   sym_hashes = elf_sym_hashes (abfd);
1611   local_got_refcounts = elf_local_got_refcounts (abfd);
1612
1613   relend = relocs + sec->reloc_count;
1614   for (rel = relocs; rel < relend; rel++)
1615     {
1616       unsigned long r_symndx;
1617       unsigned int r_type;
1618       struct elf_link_hash_entry *h = NULL;
1619
1620       r_symndx = ELF32_R_SYM (rel->r_info);
1621       if (r_symndx >= symtab_hdr->sh_info)
1622         {
1623           struct elf_i386_link_hash_entry *eh;
1624           struct elf_i386_dyn_relocs **pp;
1625           struct elf_i386_dyn_relocs *p;
1626
1627           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1628           while (h->root.type == bfd_link_hash_indirect
1629                  || h->root.type == bfd_link_hash_warning)
1630             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1631           eh = (struct elf_i386_link_hash_entry *) h;
1632
1633           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1634             if (p->sec == sec)
1635               {
1636                 /* Everything must go for SEC.  */
1637                 *pp = p->next;
1638                 break;
1639               }
1640         }
1641
1642       r_type = ELF32_R_TYPE (rel->r_info);
1643       if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1644                                      symtab_hdr, sym_hashes,
1645                                      &r_type, GOT_UNKNOWN,
1646                                      rel, relend, h)) 
1647         return FALSE;
1648
1649       switch (r_type)
1650         {
1651         case R_386_TLS_LDM:
1652           if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1653             elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1654           break;
1655
1656         case R_386_TLS_GD:
1657         case R_386_TLS_GOTDESC:
1658         case R_386_TLS_DESC_CALL:
1659         case R_386_TLS_IE_32:
1660         case R_386_TLS_IE:
1661         case R_386_TLS_GOTIE:
1662         case R_386_GOT32:
1663           if (h != NULL)
1664             {
1665               if (h->got.refcount > 0)
1666                 h->got.refcount -= 1;
1667             }
1668           else if (local_got_refcounts != NULL)
1669             {
1670               if (local_got_refcounts[r_symndx] > 0)
1671                 local_got_refcounts[r_symndx] -= 1;
1672             }
1673           break;
1674
1675         case R_386_32:
1676         case R_386_PC32:
1677           if (info->shared)
1678             break;
1679           /* Fall through */
1680
1681         case R_386_PLT32:
1682           if (h != NULL)
1683             {
1684               if (h->plt.refcount > 0)
1685                 h->plt.refcount -= 1;
1686             }
1687           break;
1688
1689         default:
1690           break;
1691         }
1692     }
1693
1694   return TRUE;
1695 }
1696
1697 /* Adjust a symbol defined by a dynamic object and referenced by a
1698    regular object.  The current definition is in some section of the
1699    dynamic object, but we're not including those sections.  We have to
1700    change the definition to something the rest of the link can
1701    understand.  */
1702
1703 static bfd_boolean
1704 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1705                                 struct elf_link_hash_entry *h)
1706 {
1707   struct elf_i386_link_hash_table *htab;
1708   asection *s;
1709
1710   /* If this is a function, put it in the procedure linkage table.  We
1711      will fill in the contents of the procedure linkage table later,
1712      when we know the address of the .got section.  */
1713   if (h->type == STT_FUNC
1714       || h->needs_plt)
1715     {
1716       if (h->plt.refcount <= 0
1717           || SYMBOL_CALLS_LOCAL (info, h)
1718           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1719               && h->root.type == bfd_link_hash_undefweak))
1720         {
1721           /* This case can occur if we saw a PLT32 reloc in an input
1722              file, but the symbol was never referred to by a dynamic
1723              object, or if all references were garbage collected.  In
1724              such a case, we don't actually need to build a procedure
1725              linkage table, and we can just do a PC32 reloc instead.  */
1726           h->plt.offset = (bfd_vma) -1;
1727           h->needs_plt = 0;
1728         }
1729
1730       return TRUE;
1731     }
1732   else
1733     /* It's possible that we incorrectly decided a .plt reloc was
1734        needed for an R_386_PC32 reloc to a non-function sym in
1735        check_relocs.  We can't decide accurately between function and
1736        non-function syms in check-relocs;  Objects loaded later in
1737        the link may change h->type.  So fix it now.  */
1738     h->plt.offset = (bfd_vma) -1;
1739
1740   /* If this is a weak symbol, and there is a real definition, the
1741      processor independent code will have arranged for us to see the
1742      real definition first, and we can just use the same value.  */
1743   if (h->u.weakdef != NULL)
1744     {
1745       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1746                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1747       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1748       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1749       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1750         h->non_got_ref = h->u.weakdef->non_got_ref;
1751       return TRUE;
1752     }
1753
1754   /* This is a reference to a symbol defined by a dynamic object which
1755      is not a function.  */
1756
1757   /* If we are creating a shared library, we must presume that the
1758      only references to the symbol are via the global offset table.
1759      For such cases we need not do anything here; the relocations will
1760      be handled correctly by relocate_section.  */
1761   if (info->shared)
1762     return TRUE;
1763
1764   /* If there are no references to this symbol that do not use the
1765      GOT, we don't need to generate a copy reloc.  */
1766   if (!h->non_got_ref)
1767     return TRUE;
1768
1769   /* If -z nocopyreloc was given, we won't generate them either.  */
1770   if (info->nocopyreloc)
1771     {
1772       h->non_got_ref = 0;
1773       return TRUE;
1774     }
1775
1776   htab = elf_i386_hash_table (info);
1777
1778   /* If there aren't any dynamic relocs in read-only sections, then
1779      we can keep the dynamic relocs and avoid the copy reloc.  This
1780      doesn't work on VxWorks, where we can not have dynamic relocations
1781      (other than copy and jump slot relocations) in an executable.  */
1782   if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1783     {
1784       struct elf_i386_link_hash_entry * eh;
1785       struct elf_i386_dyn_relocs *p;
1786
1787       eh = (struct elf_i386_link_hash_entry *) h;
1788       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1789         {
1790           s = p->sec->output_section;
1791           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1792             break;
1793         }
1794
1795       if (p == NULL)
1796         {
1797           h->non_got_ref = 0;
1798           return TRUE;
1799         }
1800     }
1801
1802   if (h->size == 0)
1803     {
1804       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1805                              h->root.root.string);
1806       return TRUE;
1807     }
1808
1809   /* We must allocate the symbol in our .dynbss section, which will
1810      become part of the .bss section of the executable.  There will be
1811      an entry for this symbol in the .dynsym section.  The dynamic
1812      object will contain position independent code, so all references
1813      from the dynamic object to this symbol will go through the global
1814      offset table.  The dynamic linker will use the .dynsym entry to
1815      determine the address it must put in the global offset table, so
1816      both the dynamic object and the regular object will refer to the
1817      same memory location for the variable.  */
1818
1819   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1820      copy the initial value out of the dynamic object and into the
1821      runtime process image.  */
1822   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1823     {
1824       htab->srelbss->size += sizeof (Elf32_External_Rel);
1825       h->needs_copy = 1;
1826     }
1827
1828   s = htab->sdynbss;
1829
1830   return _bfd_elf_adjust_dynamic_copy (h, s);
1831 }
1832
1833 /* Allocate space in .plt, .got and associated reloc sections for
1834    dynamic relocs.  */
1835
1836 static bfd_boolean
1837 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1838 {
1839   struct bfd_link_info *info;
1840   struct elf_i386_link_hash_table *htab;
1841   struct elf_i386_link_hash_entry *eh;
1842   struct elf_i386_dyn_relocs *p;
1843   bfd_boolean use_indirect_section = FALSE;
1844
1845   if (h->root.type == bfd_link_hash_indirect)
1846     return TRUE;
1847
1848   if (h->root.type == bfd_link_hash_warning)
1849     /* When warning symbols are created, they **replace** the "real"
1850        entry in the hash table, thus we never get to see the real
1851        symbol in a hash traversal.  So look at it now.  */
1852     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1853
1854   info = (struct bfd_link_info *) inf;
1855   htab = elf_i386_hash_table (info);
1856
1857   if (htab->elf.dynamic_sections_created
1858       && h->plt.refcount > 0)
1859     {
1860       /* Make sure this symbol is output as a dynamic symbol.
1861          Undefined weak syms won't yet be marked as dynamic.  */
1862       if (h->dynindx == -1
1863           && !h->forced_local)
1864         {
1865           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1866             return FALSE;
1867         }
1868
1869       if (info->shared
1870           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1871         {
1872           asection *s = htab->splt;
1873
1874           /* If this is the first .plt entry, make room for the special
1875              first entry.  */
1876           if (s->size == 0)
1877             s->size += PLT_ENTRY_SIZE;
1878
1879           h->plt.offset = s->size;
1880
1881           /* If this symbol is not defined in a regular file, and we are
1882              not generating a shared library, then set the symbol to this
1883              location in the .plt.  This is required to make function
1884              pointers compare as equal between the normal executable and
1885              the shared library.  */
1886           if (! info->shared
1887               && !h->def_regular)
1888             {
1889               h->root.u.def.section = s;
1890               h->root.u.def.value = h->plt.offset;
1891             }
1892
1893           /* Make room for this entry.  */
1894           s->size += PLT_ENTRY_SIZE;
1895
1896           /* We also need to make an entry in the .got.plt section, which
1897              will be placed in the .got section by the linker script.  */
1898           htab->sgotplt->size += 4;
1899
1900           /* We also need to make an entry in the .rel.plt section.  */
1901           htab->srelplt->size += sizeof (Elf32_External_Rel);
1902           htab->next_tls_desc_index++;
1903
1904           if (htab->is_vxworks && !info->shared)
1905             {
1906               /* VxWorks has a second set of relocations for each PLT entry
1907                  in executables.  They go in a separate relocation section,
1908                  which is processed by the kernel loader.  */
1909
1910               /* There are two relocations for the initial PLT entry: an
1911                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
1912                  R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
1913
1914               if (h->plt.offset == PLT_ENTRY_SIZE)
1915                 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1916
1917               /* There are two extra relocations for each subsequent PLT entry:
1918                  an R_386_32 relocation for the GOT entry, and an R_386_32
1919                  relocation for the PLT entry.  */
1920
1921               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
1922             }
1923         }
1924       else
1925         {
1926           h->plt.offset = (bfd_vma) -1;
1927           h->needs_plt = 0;
1928         }
1929     }
1930   else
1931     {
1932       h->plt.offset = (bfd_vma) -1;
1933       h->needs_plt = 0;
1934     }
1935
1936   eh = (struct elf_i386_link_hash_entry *) h;
1937   eh->tlsdesc_got = (bfd_vma) -1;
1938
1939   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1940      make it a R_386_TLS_LE_32 requiring no TLS entry.  */
1941   if (h->got.refcount > 0
1942       && !info->shared
1943       && h->dynindx == -1
1944       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1945     h->got.offset = (bfd_vma) -1;
1946   else if (h->got.refcount > 0)
1947     {
1948       asection *s;
1949       bfd_boolean dyn;
1950       int tls_type = elf_i386_hash_entry(h)->tls_type;
1951
1952       /* Make sure this symbol is output as a dynamic symbol.
1953          Undefined weak syms won't yet be marked as dynamic.  */
1954       if (h->dynindx == -1
1955           && !h->forced_local)
1956         {
1957           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1958             return FALSE;
1959         }
1960
1961       s = htab->sgot;
1962       if (GOT_TLS_GDESC_P (tls_type))
1963         {
1964           eh->tlsdesc_got = htab->sgotplt->size
1965             - elf_i386_compute_jump_table_size (htab);
1966           htab->sgotplt->size += 8;
1967           h->got.offset = (bfd_vma) -2;
1968         }
1969       if (! GOT_TLS_GDESC_P (tls_type)
1970           || GOT_TLS_GD_P (tls_type))
1971         {
1972           h->got.offset = s->size;
1973           s->size += 4;
1974           /* R_386_TLS_GD needs 2 consecutive GOT slots.  */
1975           if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
1976             s->size += 4;
1977         }
1978       dyn = htab->elf.dynamic_sections_created;
1979       /* R_386_TLS_IE_32 needs one dynamic relocation,
1980          R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1981          (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1982          need two), R_386_TLS_GD needs one if local symbol and two if
1983          global.  */
1984       if (tls_type == GOT_TLS_IE_BOTH)
1985         htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1986       else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1987                || (tls_type & GOT_TLS_IE))
1988         htab->srelgot->size += sizeof (Elf32_External_Rel);
1989       else if (GOT_TLS_GD_P (tls_type))
1990         htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1991       else if (! GOT_TLS_GDESC_P (tls_type)
1992                && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1993                    || h->root.type != bfd_link_hash_undefweak)
1994                && (info->shared
1995                    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1996         htab->srelgot->size += sizeof (Elf32_External_Rel);
1997       if (GOT_TLS_GDESC_P (tls_type))
1998         htab->srelplt->size += sizeof (Elf32_External_Rel);
1999     }
2000   else
2001     h->got.offset = (bfd_vma) -1;
2002
2003   if (eh->dyn_relocs == NULL)
2004     return TRUE;
2005
2006   /* In the shared -Bsymbolic case, discard space allocated for
2007      dynamic pc-relative relocs against symbols which turn out to be
2008      defined in regular objects.  For the normal shared case, discard
2009      space for pc-relative relocs that have become local due to symbol
2010      visibility changes.  */
2011
2012   if (info->shared)
2013     {
2014       /* The only reloc that uses pc_count is R_386_PC32, which will
2015          appear on a call or on something like ".long foo - .".  We
2016          want calls to protected symbols to resolve directly to the
2017          function rather than going via the plt.  If people want
2018          function pointer comparisons to work as expected then they
2019          should avoid writing assembly like ".long foo - .".  */
2020       if (SYMBOL_CALLS_LOCAL (info, h))
2021         {
2022           struct elf_i386_dyn_relocs **pp;
2023
2024           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2025             {
2026               p->count -= p->pc_count;
2027               p->pc_count = 0;
2028               if (p->count == 0)
2029                 *pp = p->next;
2030               else
2031                 pp = &p->next;
2032             }
2033         }
2034
2035       if (htab->is_vxworks)
2036         {
2037           struct elf_i386_dyn_relocs **pp;
2038           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2039             {
2040               if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2041                 *pp = p->next;
2042               else
2043                 pp = &p->next;
2044             }
2045         }
2046
2047       /* Also discard relocs on undefined weak syms with non-default
2048          visibility.  */
2049       if (eh->dyn_relocs != NULL
2050           && h->root.type == bfd_link_hash_undefweak)
2051         {
2052           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2053             eh->dyn_relocs = NULL;
2054
2055           /* Make sure undefined weak symbols are output as a dynamic
2056              symbol in PIEs.  */
2057           else if (h->dynindx == -1
2058                    && !h->forced_local)
2059             {
2060               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2061                 return FALSE;
2062             }
2063         }
2064     }
2065   else if (is_indirect_symbol (info->output_bfd, h)
2066            && h->dynindx == -1
2067            && ! h->forced_local)
2068     {
2069       if (bfd_elf_link_record_dynamic_symbol (info, h)
2070           && h->dynindx != -1)
2071         use_indirect_section = TRUE;
2072       else
2073         return FALSE;
2074     }
2075   else if (ELIMINATE_COPY_RELOCS)
2076     {
2077       /* For the non-shared case, discard space for relocs against
2078          symbols which turn out to need copy relocs or are not
2079          dynamic.  */
2080
2081       if (!h->non_got_ref
2082           && ((h->def_dynamic
2083                && !h->def_regular)
2084               || (htab->elf.dynamic_sections_created
2085                   && (h->root.type == bfd_link_hash_undefweak
2086                       || h->root.type == bfd_link_hash_undefined))))
2087         {
2088           /* Make sure this symbol is output as a dynamic symbol.
2089              Undefined weak syms won't yet be marked as dynamic.  */
2090           if (h->dynindx == -1
2091               && !h->forced_local)
2092             {
2093               if (! bfd_elf_link_record_dynamic_symbol (info, h))
2094                 return FALSE;
2095             }
2096
2097           /* If that succeeded, we know we'll be keeping all the
2098              relocs.  */
2099           if (h->dynindx != -1)
2100             goto keep;
2101         }
2102
2103       eh->dyn_relocs = NULL;
2104
2105     keep: ;
2106     }
2107
2108   /* Finally, allocate space.  */
2109   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2110     {
2111       asection *sreloc;
2112
2113       if (use_indirect_section)
2114         sreloc = elf_section_data (p->sec)->indirect_relocs;
2115       else
2116         sreloc = elf_section_data (p->sec)->sreloc;
2117
2118       BFD_ASSERT (sreloc != NULL);
2119       sreloc->size += p->count * sizeof (Elf32_External_Rel);
2120     }
2121
2122   return TRUE;
2123 }
2124
2125 /* Find any dynamic relocs that apply to read-only sections.  */
2126
2127 static bfd_boolean
2128 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2129 {
2130   struct elf_i386_link_hash_entry *eh;
2131   struct elf_i386_dyn_relocs *p;
2132
2133   if (h->root.type == bfd_link_hash_warning)
2134     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2135
2136   eh = (struct elf_i386_link_hash_entry *) h;
2137   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2138     {
2139       asection *s = p->sec->output_section;
2140
2141       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2142         {
2143           struct bfd_link_info *info = (struct bfd_link_info *) inf;
2144
2145           info->flags |= DF_TEXTREL;
2146
2147           /* Not an error, just cut short the traversal.  */
2148           return FALSE;
2149         }
2150     }
2151   return TRUE;
2152 }
2153
2154 /* Set the sizes of the dynamic sections.  */
2155
2156 static bfd_boolean
2157 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2158                                 struct bfd_link_info *info)
2159 {
2160   struct elf_i386_link_hash_table *htab;
2161   bfd *dynobj;
2162   asection *s;
2163   bfd_boolean relocs;
2164   bfd *ibfd;
2165
2166   htab = elf_i386_hash_table (info);
2167   dynobj = htab->elf.dynobj;
2168   if (dynobj == NULL)
2169     abort ();
2170
2171   if (htab->elf.dynamic_sections_created)
2172     {
2173       /* Set the contents of the .interp section to the interpreter.  */
2174       if (info->executable)
2175         {
2176           s = bfd_get_section_by_name (dynobj, ".interp");
2177           if (s == NULL)
2178             abort ();
2179           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2180           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2181         }
2182     }
2183
2184   /* Set up .got offsets for local syms, and space for local dynamic
2185      relocs.  */
2186   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2187     {
2188       bfd_signed_vma *local_got;
2189       bfd_signed_vma *end_local_got;
2190       char *local_tls_type;
2191       bfd_vma *local_tlsdesc_gotent;
2192       bfd_size_type locsymcount;
2193       Elf_Internal_Shdr *symtab_hdr;
2194       asection *srel;
2195
2196       if (! is_i386_elf (ibfd))
2197         continue;
2198
2199       for (s = ibfd->sections; s != NULL; s = s->next)
2200         {
2201           struct elf_i386_dyn_relocs *p;
2202
2203           for (p = ((struct elf_i386_dyn_relocs *)
2204                      elf_section_data (s)->local_dynrel);
2205                p != NULL;
2206                p = p->next)
2207             {
2208               if (!bfd_is_abs_section (p->sec)
2209                   && bfd_is_abs_section (p->sec->output_section))
2210                 {
2211                   /* Input section has been discarded, either because
2212                      it is a copy of a linkonce section or due to
2213                      linker script /DISCARD/, so we'll be discarding
2214                      the relocs too.  */
2215                 }
2216               else if (htab->is_vxworks
2217                        && strcmp (p->sec->output_section->name,
2218                                   ".tls_vars") == 0)
2219                 {
2220                   /* Relocations in vxworks .tls_vars sections are
2221                      handled specially by the loader.  */
2222                 }
2223               else if (p->count != 0)
2224                 {
2225                   srel = elf_section_data (p->sec)->sreloc;
2226                   srel->size += p->count * sizeof (Elf32_External_Rel);
2227                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2228                     info->flags |= DF_TEXTREL;
2229                 }
2230             }
2231         }
2232
2233       local_got = elf_local_got_refcounts (ibfd);
2234       if (!local_got)
2235         continue;
2236
2237       symtab_hdr = &elf_symtab_hdr (ibfd);
2238       locsymcount = symtab_hdr->sh_info;
2239       end_local_got = local_got + locsymcount;
2240       local_tls_type = elf_i386_local_got_tls_type (ibfd);
2241       local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2242       s = htab->sgot;
2243       srel = htab->srelgot;
2244       for (; local_got < end_local_got;
2245            ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2246         {
2247           *local_tlsdesc_gotent = (bfd_vma) -1;
2248           if (*local_got > 0)
2249             {
2250               if (GOT_TLS_GDESC_P (*local_tls_type))
2251                 {
2252                   *local_tlsdesc_gotent = htab->sgotplt->size
2253                     - elf_i386_compute_jump_table_size (htab);
2254                   htab->sgotplt->size += 8;
2255                   *local_got = (bfd_vma) -2;
2256                 }
2257               if (! GOT_TLS_GDESC_P (*local_tls_type)
2258                   || GOT_TLS_GD_P (*local_tls_type))
2259                 {
2260                   *local_got = s->size;
2261                   s->size += 4;
2262                   if (GOT_TLS_GD_P (*local_tls_type)
2263                       || *local_tls_type == GOT_TLS_IE_BOTH)
2264                     s->size += 4;
2265                 }
2266               if (info->shared
2267                   || GOT_TLS_GD_ANY_P (*local_tls_type)
2268                   || (*local_tls_type & GOT_TLS_IE))
2269                 {
2270                   if (*local_tls_type == GOT_TLS_IE_BOTH)
2271                     srel->size += 2 * sizeof (Elf32_External_Rel);
2272                   else if (GOT_TLS_GD_P (*local_tls_type)
2273                            || ! GOT_TLS_GDESC_P (*local_tls_type))
2274                     srel->size += sizeof (Elf32_External_Rel);
2275                   if (GOT_TLS_GDESC_P (*local_tls_type))
2276                     htab->srelplt->size += sizeof (Elf32_External_Rel);
2277                 }
2278             }
2279           else
2280             *local_got = (bfd_vma) -1;
2281         }
2282     }
2283
2284   if (htab->tls_ldm_got.refcount > 0)
2285     {
2286       /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2287          relocs.  */
2288       htab->tls_ldm_got.offset = htab->sgot->size;
2289       htab->sgot->size += 8;
2290       htab->srelgot->size += sizeof (Elf32_External_Rel);
2291     }
2292   else
2293     htab->tls_ldm_got.offset = -1;
2294
2295   /* Allocate global sym .plt and .got entries, and space for global
2296      sym dynamic relocs.  */
2297   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2298
2299   /* For every jump slot reserved in the sgotplt, reloc_count is
2300      incremented.  However, when we reserve space for TLS descriptors,
2301      it's not incremented, so in order to compute the space reserved
2302      for them, it suffices to multiply the reloc count by the jump
2303      slot size.  */
2304   if (htab->srelplt)
2305     htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2306
2307   /* We now have determined the sizes of the various dynamic sections.
2308      Allocate memory for them.  */
2309   relocs = FALSE;
2310   for (s = dynobj->sections; s != NULL; s = s->next)
2311     {
2312       bfd_boolean strip_section = TRUE;
2313
2314       if ((s->flags & SEC_LINKER_CREATED) == 0)
2315         continue;
2316
2317       if (s == htab->splt
2318           || s == htab->sgot
2319           || s == htab->sgotplt
2320           || s == htab->sdynbss)
2321         {
2322           /* Strip this section if we don't need it; see the
2323              comment below.  */
2324           /* We'd like to strip these sections if they aren't needed, but if
2325              we've exported dynamic symbols from them we must leave them.
2326              It's too late to tell BFD to get rid of the symbols.  */
2327
2328           if (htab->elf.hplt != NULL)
2329             strip_section = FALSE;
2330         }
2331       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2332         {
2333           if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2334             relocs = TRUE;
2335
2336           /* We use the reloc_count field as a counter if we need
2337              to copy relocs into the output file.  */
2338           s->reloc_count = 0;
2339         }
2340       else
2341         {
2342           /* It's not one of our sections, so don't allocate space.  */
2343           continue;
2344         }
2345
2346       if (s->size == 0)
2347         {
2348           /* If we don't need this section, strip it from the
2349              output file.  This is mostly to handle .rel.bss and
2350              .rel.plt.  We must create both sections in
2351              create_dynamic_sections, because they must be created
2352              before the linker maps input sections to output
2353              sections.  The linker does that before
2354              adjust_dynamic_symbol is called, and it is that
2355              function which decides whether anything needs to go
2356              into these sections.  */
2357           if (strip_section)
2358             s->flags |= SEC_EXCLUDE;
2359           continue;
2360         }
2361
2362       if ((s->flags & SEC_HAS_CONTENTS) == 0)
2363         continue;
2364
2365       /* Allocate memory for the section contents.  We use bfd_zalloc
2366          here in case unused entries are not reclaimed before the
2367          section's contents are written out.  This should not happen,
2368          but this way if it does, we get a R_386_NONE reloc instead
2369          of garbage.  */
2370       s->contents = bfd_zalloc (dynobj, s->size);
2371       if (s->contents == NULL)
2372         return FALSE;
2373     }
2374
2375   if (htab->elf.dynamic_sections_created)
2376     {
2377       /* Add some entries to the .dynamic section.  We fill in the
2378          values later, in elf_i386_finish_dynamic_sections, but we
2379          must add the entries now so that we get the correct size for
2380          the .dynamic section.  The DT_DEBUG entry is filled in by the
2381          dynamic linker and used by the debugger.  */
2382 #define add_dynamic_entry(TAG, VAL) \
2383   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2384
2385       if (info->executable)
2386         {
2387           if (!add_dynamic_entry (DT_DEBUG, 0))
2388             return FALSE;
2389         }
2390
2391       if (htab->splt->size != 0)
2392         {
2393           if (!add_dynamic_entry (DT_PLTGOT, 0)
2394               || !add_dynamic_entry (DT_PLTRELSZ, 0)
2395               || !add_dynamic_entry (DT_PLTREL, DT_REL)
2396               || !add_dynamic_entry (DT_JMPREL, 0))
2397             return FALSE;
2398         }
2399
2400       if (relocs)
2401         {
2402           if (!add_dynamic_entry (DT_REL, 0)
2403               || !add_dynamic_entry (DT_RELSZ, 0)
2404               || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2405             return FALSE;
2406
2407           /* If any dynamic relocs apply to a read-only section,
2408              then we need a DT_TEXTREL entry.  */
2409           if ((info->flags & DF_TEXTREL) == 0)
2410             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2411
2412           if ((info->flags & DF_TEXTREL) != 0)
2413             {
2414               if (!add_dynamic_entry (DT_TEXTREL, 0))
2415                 return FALSE;
2416             }
2417         }
2418       if (htab->is_vxworks
2419           && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2420         return FALSE;
2421     }
2422 #undef add_dynamic_entry
2423
2424   return TRUE;
2425 }
2426
2427 static bfd_boolean
2428 elf_i386_always_size_sections (bfd *output_bfd,
2429                                struct bfd_link_info *info)
2430 {
2431   asection *tls_sec = elf_hash_table (info)->tls_sec;
2432
2433   if (tls_sec)
2434     {
2435       struct elf_link_hash_entry *tlsbase;
2436
2437       tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2438                                       "_TLS_MODULE_BASE_",
2439                                       FALSE, FALSE, FALSE);
2440
2441       if (tlsbase && tlsbase->type == STT_TLS)
2442         {
2443           struct bfd_link_hash_entry *bh = NULL;
2444           const struct elf_backend_data *bed
2445             = get_elf_backend_data (output_bfd);
2446
2447           if (!(_bfd_generic_link_add_one_symbol
2448                 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2449                  tls_sec, 0, NULL, FALSE,
2450                  bed->collect, &bh)))
2451             return FALSE;
2452
2453           elf_i386_hash_table (info)->tls_module_base = bh;
2454
2455           tlsbase = (struct elf_link_hash_entry *)bh;
2456           tlsbase->def_regular = 1;
2457           tlsbase->other = STV_HIDDEN;
2458           (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2459         }
2460     }
2461
2462   return TRUE;
2463 }
2464
2465 /* Set the correct type for an x86 ELF section.  We do this by the
2466    section name, which is a hack, but ought to work.  */
2467
2468 static bfd_boolean
2469 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2470                         Elf_Internal_Shdr *hdr,
2471                         asection *sec)
2472 {
2473   register const char *name;
2474
2475   name = bfd_get_section_name (abfd, sec);
2476
2477   /* This is an ugly, but unfortunately necessary hack that is
2478      needed when producing EFI binaries on x86. It tells
2479      elf.c:elf_fake_sections() not to consider ".reloc" as a section
2480      containing ELF relocation info.  We need this hack in order to
2481      be able to generate ELF binaries that can be translated into
2482      EFI applications (which are essentially COFF objects).  Those
2483      files contain a COFF ".reloc" section inside an ELFNN object,
2484      which would normally cause BFD to segfault because it would
2485      attempt to interpret this section as containing relocation
2486      entries for section "oc".  With this hack enabled, ".reloc"
2487      will be treated as a normal data section, which will avoid the
2488      segfault.  However, you won't be able to create an ELFNN binary
2489      with a section named "oc" that needs relocations, but that's
2490      the kind of ugly side-effects you get when detecting section
2491      types based on their names...  In practice, this limitation is
2492      unlikely to bite.  */
2493   if (strcmp (name, ".reloc") == 0)
2494     hdr->sh_type = SHT_PROGBITS;
2495
2496   return TRUE;
2497 }
2498
2499 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2500    executables.  Rather than setting it to the beginning of the TLS
2501    section, we have to set it to the end.    This function may be called
2502    multiple times, it is idempotent.  */
2503
2504 static void
2505 set_tls_module_base (struct bfd_link_info *info)
2506 {
2507   struct bfd_link_hash_entry *base;
2508
2509   if (!info->executable)
2510     return;
2511
2512   base = elf_i386_hash_table (info)->tls_module_base;
2513
2514   if (!base)
2515     return;
2516
2517   base->u.def.value = elf_hash_table (info)->tls_size;
2518 }
2519
2520 /* Return the base VMA address which should be subtracted from real addresses
2521    when resolving @dtpoff relocation.
2522    This is PT_TLS segment p_vaddr.  */
2523
2524 static bfd_vma
2525 dtpoff_base (struct bfd_link_info *info)
2526 {
2527   /* If tls_sec is NULL, we should have signalled an error already.  */
2528   if (elf_hash_table (info)->tls_sec == NULL)
2529     return 0;
2530   return elf_hash_table (info)->tls_sec->vma;
2531 }
2532
2533 /* Return the relocation value for @tpoff relocation
2534    if STT_TLS virtual address is ADDRESS.  */
2535
2536 static bfd_vma
2537 tpoff (struct bfd_link_info *info, bfd_vma address)
2538 {
2539   struct elf_link_hash_table *htab = elf_hash_table (info);
2540
2541   /* If tls_sec is NULL, we should have signalled an error already.  */
2542   if (htab->tls_sec == NULL)
2543     return 0;
2544   return htab->tls_size + htab->tls_sec->vma - address;
2545 }
2546
2547 /* Relocate an i386 ELF section.  */
2548
2549 static bfd_boolean
2550 elf_i386_relocate_section (bfd *output_bfd,
2551                            struct bfd_link_info *info,
2552                            bfd *input_bfd,
2553                            asection *input_section,
2554                            bfd_byte *contents,
2555                            Elf_Internal_Rela *relocs,
2556                            Elf_Internal_Sym *local_syms,
2557                            asection **local_sections)
2558 {
2559   struct elf_i386_link_hash_table *htab;
2560   Elf_Internal_Shdr *symtab_hdr;
2561   struct elf_link_hash_entry **sym_hashes;
2562   bfd_vma *local_got_offsets;
2563   bfd_vma *local_tlsdesc_gotents;
2564   Elf_Internal_Rela *rel;
2565   Elf_Internal_Rela *relend;
2566   bfd_boolean is_vxworks_tls;
2567
2568   BFD_ASSERT (is_i386_elf (input_bfd));
2569   
2570   htab = elf_i386_hash_table (info);
2571   symtab_hdr = &elf_symtab_hdr (input_bfd);
2572   sym_hashes = elf_sym_hashes (input_bfd);
2573   local_got_offsets = elf_local_got_offsets (input_bfd);
2574   local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2575   /* We have to handle relocations in vxworks .tls_vars sections
2576      specially, because the dynamic loader is 'weird'.  */
2577   is_vxworks_tls = (htab->is_vxworks && info->shared
2578                     && !strcmp (input_section->output_section->name,
2579                                 ".tls_vars"));
2580
2581   set_tls_module_base (info);
2582
2583   rel = relocs;
2584   relend = relocs + input_section->reloc_count;
2585   for (; rel < relend; rel++)
2586     {
2587       unsigned int r_type;
2588       reloc_howto_type *howto;
2589       unsigned long r_symndx;
2590       struct elf_link_hash_entry *h;
2591       Elf_Internal_Sym *sym;
2592       asection *sec;
2593       bfd_vma off, offplt;
2594       bfd_vma relocation;
2595       bfd_boolean unresolved_reloc;
2596       bfd_reloc_status_type r;
2597       unsigned int indx;
2598       int tls_type;
2599
2600       r_type = ELF32_R_TYPE (rel->r_info);
2601       if (r_type == R_386_GNU_VTINHERIT
2602           || r_type == R_386_GNU_VTENTRY)
2603         continue;
2604
2605       if ((indx = r_type) >= R_386_standard
2606           && ((indx = r_type - R_386_ext_offset) - R_386_standard
2607               >= R_386_ext - R_386_standard)
2608           && ((indx = r_type - R_386_tls_offset) - R_386_ext
2609               >= R_386_tls - R_386_ext))
2610         {
2611           (*_bfd_error_handler)
2612             (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2613              input_bfd, input_section, r_type);
2614           bfd_set_error (bfd_error_bad_value);
2615           return FALSE;
2616         }
2617       howto = elf_howto_table + indx;
2618
2619       r_symndx = ELF32_R_SYM (rel->r_info);
2620       h = NULL;
2621       sym = NULL;
2622       sec = NULL;
2623       unresolved_reloc = FALSE;
2624       if (r_symndx < symtab_hdr->sh_info)
2625         {
2626           sym = local_syms + r_symndx;
2627           sec = local_sections[r_symndx];
2628           relocation = (sec->output_section->vma
2629                         + sec->output_offset
2630                         + sym->st_value);
2631
2632           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2633               && ((sec->flags & SEC_MERGE) != 0
2634                   || (info->relocatable
2635                       && sec->output_offset != 0)))
2636             {
2637               bfd_vma addend;
2638               bfd_byte *where = contents + rel->r_offset;
2639
2640               switch (howto->size)
2641                 {
2642                 case 0:
2643                   addend = bfd_get_8 (input_bfd, where);
2644                   if (howto->pc_relative)
2645                     {
2646                       addend = (addend ^ 0x80) - 0x80;
2647                       addend += 1;
2648                     }
2649                   break;
2650                 case 1:
2651                   addend = bfd_get_16 (input_bfd, where);
2652                   if (howto->pc_relative)
2653                     {
2654                       addend = (addend ^ 0x8000) - 0x8000;
2655                       addend += 2;
2656                     }
2657                   break;
2658                 case 2:
2659                   addend = bfd_get_32 (input_bfd, where);
2660                   if (howto->pc_relative)
2661                     {
2662                       addend = (addend ^ 0x80000000) - 0x80000000;
2663                       addend += 4;
2664                     }
2665                   break;
2666                 default:
2667                   abort ();
2668                 }
2669
2670               if (info->relocatable)
2671                 addend += sec->output_offset;
2672               else
2673                 {
2674                   asection *msec = sec;
2675                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2676                                                    addend);
2677                   addend -= relocation;
2678                   addend += msec->output_section->vma + msec->output_offset;
2679                 }
2680
2681               switch (howto->size)
2682                 {
2683                 case 0:
2684                   /* FIXME: overflow checks.  */
2685                   if (howto->pc_relative)
2686                     addend -= 1;
2687                   bfd_put_8 (input_bfd, addend, where);
2688                   break;
2689                 case 1:
2690                   if (howto->pc_relative)
2691                     addend -= 2;
2692                   bfd_put_16 (input_bfd, addend, where);
2693                   break;
2694                 case 2:
2695                   if (howto->pc_relative)
2696                     addend -= 4;
2697                   bfd_put_32 (input_bfd, addend, where);
2698                   break;
2699                 }
2700             }
2701         }
2702       else
2703         {
2704           bfd_boolean warned;
2705
2706           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2707                                    r_symndx, symtab_hdr, sym_hashes,
2708                                    h, sec, relocation,
2709                                    unresolved_reloc, warned);
2710         }
2711
2712       if (sec != NULL && elf_discarded_section (sec))
2713         {
2714           /* For relocs against symbols from removed linkonce sections,
2715              or sections discarded by a linker script, we just want the
2716              section contents zeroed.  Avoid any special processing.  */
2717           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2718           rel->r_info = 0;
2719           rel->r_addend = 0;
2720           continue;
2721         }
2722
2723       if (info->relocatable)
2724         continue;
2725
2726       switch (r_type)
2727         {
2728         case R_386_GOT32:
2729           /* Relocation is to the entry for this symbol in the global
2730              offset table.  */
2731           if (htab->sgot == NULL)
2732             abort ();
2733
2734           if (h != NULL)
2735             {
2736               bfd_boolean dyn;
2737
2738               off = h->got.offset;
2739               dyn = htab->elf.dynamic_sections_created;
2740               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2741                   || (info->shared
2742                       && SYMBOL_REFERENCES_LOCAL (info, h))
2743                   || (ELF_ST_VISIBILITY (h->other)
2744                       && h->root.type == bfd_link_hash_undefweak))
2745                 {
2746                   /* This is actually a static link, or it is a
2747                      -Bsymbolic link and the symbol is defined
2748                      locally, or the symbol was forced to be local
2749                      because of a version file.  We must initialize
2750                      this entry in the global offset table.  Since the
2751                      offset must always be a multiple of 4, we use the
2752                      least significant bit to record whether we have
2753                      initialized it already.
2754
2755                      When doing a dynamic link, we create a .rel.got
2756                      relocation entry to initialize the value.  This
2757                      is done in the finish_dynamic_symbol routine.  */
2758                   if ((off & 1) != 0)
2759                     off &= ~1;
2760                   else
2761                     {
2762                       bfd_put_32 (output_bfd, relocation,
2763                                   htab->sgot->contents + off);
2764                       h->got.offset |= 1;
2765                     }
2766                 }
2767               else
2768                 unresolved_reloc = FALSE;
2769             }
2770           else
2771             {
2772               if (local_got_offsets == NULL)
2773                 abort ();
2774
2775               off = local_got_offsets[r_symndx];
2776
2777               /* The offset must always be a multiple of 4.  We use
2778                  the least significant bit to record whether we have
2779                  already generated the necessary reloc.  */
2780               if ((off & 1) != 0)
2781                 off &= ~1;
2782               else
2783                 {
2784                   bfd_put_32 (output_bfd, relocation,
2785                               htab->sgot->contents + off);
2786
2787                   if (info->shared)
2788                     {
2789                       asection *s;
2790                       Elf_Internal_Rela outrel;
2791                       bfd_byte *loc;
2792
2793                       s = htab->srelgot;
2794                       if (s == NULL)
2795                         abort ();
2796
2797                       outrel.r_offset = (htab->sgot->output_section->vma
2798                                          + htab->sgot->output_offset
2799                                          + off);
2800                       outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2801                       loc = s->contents;
2802                       loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2803                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2804                     }
2805
2806                   local_got_offsets[r_symndx] |= 1;
2807                 }
2808             }
2809
2810           if (off >= (bfd_vma) -2)
2811             abort ();
2812
2813           relocation = htab->sgot->output_section->vma
2814                        + htab->sgot->output_offset + off
2815                        - htab->sgotplt->output_section->vma
2816                        - htab->sgotplt->output_offset;
2817           break;
2818
2819         case R_386_GOTOFF:
2820           /* Relocation is relative to the start of the global offset
2821              table.  */
2822
2823           /* Check to make sure it isn't a protected function symbol
2824              for shared library since it may not be local when used
2825              as function address.  We also need to make sure that a
2826              symbol is defined locally.  */
2827           if (info->shared && h)
2828             {
2829               if (!h->def_regular)
2830                 {
2831                   const char *v;
2832
2833                   switch (ELF_ST_VISIBILITY (h->other))
2834                     {
2835                     case STV_HIDDEN:
2836                       v = _("hidden symbol");
2837                       break;
2838                     case STV_INTERNAL:
2839                       v = _("internal symbol");
2840                       break;
2841                     case STV_PROTECTED:
2842                       v = _("protected symbol");
2843                       break;
2844                     default:
2845                       v = _("symbol");
2846                       break;
2847                     }
2848
2849                   (*_bfd_error_handler)
2850                     (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
2851                      input_bfd, v, h->root.root.string);
2852                   bfd_set_error (bfd_error_bad_value);
2853                   return FALSE;
2854                 }
2855               else if (!info->executable
2856                        && h->type == STT_FUNC
2857                        && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2858                 {
2859                   (*_bfd_error_handler)
2860                     (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2861                      input_bfd, h->root.root.string);
2862                   bfd_set_error (bfd_error_bad_value);
2863                   return FALSE;
2864                 }
2865             }
2866
2867           /* Note that sgot is not involved in this
2868              calculation.  We always want the start of .got.plt.  If we
2869              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2870              permitted by the ABI, we might have to change this
2871              calculation.  */
2872           relocation -= htab->sgotplt->output_section->vma
2873                         + htab->sgotplt->output_offset;
2874           break;
2875
2876         case R_386_GOTPC:
2877           /* Use global offset table as symbol value.  */
2878           relocation = htab->sgotplt->output_section->vma
2879                        + htab->sgotplt->output_offset;
2880           unresolved_reloc = FALSE;
2881           break;
2882
2883         case R_386_PLT32:
2884           /* Relocation is to the entry for this symbol in the
2885              procedure linkage table.  */
2886
2887           /* Resolve a PLT32 reloc against a local symbol directly,
2888              without using the procedure linkage table.  */
2889           if (h == NULL)
2890             break;
2891
2892           if (h->plt.offset == (bfd_vma) -1
2893               || htab->splt == NULL)
2894             {
2895               /* We didn't make a PLT entry for this symbol.  This
2896                  happens when statically linking PIC code, or when
2897                  using -Bsymbolic.  */
2898               break;
2899             }
2900
2901           relocation = (htab->splt->output_section->vma
2902                         + htab->splt->output_offset
2903                         + h->plt.offset);
2904           unresolved_reloc = FALSE;
2905           break;
2906
2907         case R_386_32:
2908         case R_386_PC32:
2909           if ((input_section->flags & SEC_ALLOC) == 0
2910               || is_vxworks_tls)
2911             break;
2912
2913           if ((info->shared
2914                && (h == NULL
2915                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2916                    || h->root.type != bfd_link_hash_undefweak)
2917                && (r_type != R_386_PC32
2918                    || !SYMBOL_CALLS_LOCAL (info, h)))
2919               || (! info->shared
2920                   && h != NULL
2921                   && h->dynindx != -1
2922                   && ! h->forced_local
2923                   && ((struct elf_i386_link_hash_entry *) h)->dyn_relocs != NULL
2924                   && is_indirect_symbol (output_bfd, h))
2925               || (ELIMINATE_COPY_RELOCS
2926                   && !info->shared
2927                   && h != NULL
2928                   && h->dynindx != -1
2929                   && !h->non_got_ref
2930                   && ((h->def_dynamic
2931                        && !h->def_regular)
2932                       || h->root.type == bfd_link_hash_undefweak
2933                       || h->root.type == bfd_link_hash_undefined)))
2934             {
2935               Elf_Internal_Rela outrel;
2936               bfd_byte *loc;
2937               bfd_boolean skip, relocate;
2938               asection *sreloc;
2939
2940               /* When generating a shared object, these relocations
2941                  are copied into the output file to be resolved at run
2942                  time.  */
2943
2944               skip = FALSE;
2945               relocate = FALSE;
2946
2947               outrel.r_offset =
2948                 _bfd_elf_section_offset (output_bfd, info, input_section,
2949                                          rel->r_offset);
2950               if (outrel.r_offset == (bfd_vma) -1)
2951                 skip = TRUE;
2952               else if (outrel.r_offset == (bfd_vma) -2)
2953                 skip = TRUE, relocate = TRUE;
2954               outrel.r_offset += (input_section->output_section->vma
2955                                   + input_section->output_offset);
2956
2957               if (skip)
2958                 memset (&outrel, 0, sizeof outrel);
2959               else if (h != NULL
2960                        && h->dynindx != -1
2961                        && (r_type == R_386_PC32
2962                            || !info->shared
2963                            || !SYMBOLIC_BIND (info, h)
2964                            || !h->def_regular))
2965                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2966               else
2967                 {
2968                   /* This symbol is local, or marked to become local.  */
2969                   relocate = TRUE;
2970                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2971                 }
2972
2973               if (! info->shared
2974                   && h != NULL
2975                   && h->dynindx != -1
2976                   && ! h->forced_local
2977                   && is_indirect_symbol (output_bfd, h)
2978                   && elf_section_data (input_section)->indirect_relocs != NULL
2979                   && elf_section_data (input_section)->indirect_relocs->contents != NULL)
2980                 sreloc = elf_section_data (input_section)->indirect_relocs;
2981               else
2982                 sreloc = elf_section_data (input_section)->sreloc;
2983
2984               BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
2985
2986               loc = sreloc->contents;
2987               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2988
2989               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2990
2991               /* If this reloc is against an external symbol, we do
2992                  not want to fiddle with the addend.  Otherwise, we
2993                  need to include the symbol value so that it becomes
2994                  an addend for the dynamic reloc.  */
2995               if (! relocate)
2996                 continue;
2997             }
2998           break;
2999
3000         case R_386_TLS_IE:
3001           if (info->shared)
3002             {
3003               Elf_Internal_Rela outrel;
3004               bfd_byte *loc;
3005               asection *sreloc;
3006
3007               outrel.r_offset = rel->r_offset
3008                                 + input_section->output_section->vma
3009                                 + input_section->output_offset;
3010               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3011               sreloc = elf_section_data (input_section)->sreloc;
3012               if (sreloc == NULL)
3013                 abort ();
3014               loc = sreloc->contents;
3015               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3016               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3017             }
3018           /* Fall through */
3019
3020         case R_386_TLS_GD:
3021         case R_386_TLS_GOTDESC:
3022         case R_386_TLS_DESC_CALL:
3023         case R_386_TLS_IE_32:
3024         case R_386_TLS_GOTIE:
3025           tls_type = GOT_UNKNOWN;
3026           if (h == NULL && local_got_offsets)
3027             tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3028           else if (h != NULL)
3029             tls_type = elf_i386_hash_entry(h)->tls_type;
3030           if (tls_type == GOT_TLS_IE)
3031             tls_type = GOT_TLS_IE_NEG;
3032
3033           if (! elf_i386_tls_transition (info, input_bfd,
3034                                          input_section, contents,
3035                                          symtab_hdr, sym_hashes,
3036                                          &r_type, tls_type, rel,
3037                                          relend, h))
3038             return FALSE;
3039
3040           if (r_type == R_386_TLS_LE_32)
3041             {
3042               BFD_ASSERT (! unresolved_reloc);
3043               if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3044                 {
3045                   unsigned int type;
3046                   bfd_vma roff;
3047
3048                   /* GD->LE transition.  */
3049                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3050                   if (type == 0x04)
3051                     {
3052                       /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3053                          Change it into:
3054                          movl %gs:0, %eax; subl $foo@tpoff, %eax
3055                          (6 byte form of subl).  */
3056                       memcpy (contents + rel->r_offset - 3,
3057                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3058                       roff = rel->r_offset + 5;
3059                     }
3060                   else
3061                     {
3062                       /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3063                          Change it into:
3064                          movl %gs:0, %eax; subl $foo@tpoff, %eax
3065                          (6 byte form of subl).  */
3066                       memcpy (contents + rel->r_offset - 2,
3067                               "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3068                       roff = rel->r_offset + 6;
3069                     }
3070                   bfd_put_32 (output_bfd, tpoff (info, relocation),
3071                               contents + roff);
3072                   /* Skip R_386_PC32/R_386_PLT32.  */
3073                   rel++;
3074                   continue;
3075                 }
3076               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3077                 {
3078                   /* GDesc -> LE transition.
3079                      It's originally something like:
3080                      leal x@tlsdesc(%ebx), %eax
3081
3082                      leal x@ntpoff, %eax
3083
3084                      Registers other than %eax may be set up here.  */
3085
3086                   unsigned int val;
3087                   bfd_vma roff;
3088
3089                   roff = rel->r_offset;
3090                   val = bfd_get_8 (input_bfd, contents + roff - 1);
3091
3092                   /* Now modify the instruction as appropriate.  */
3093                   /* aoliva FIXME: remove the above and xor the byte
3094                      below with 0x86.  */
3095                   bfd_put_8 (output_bfd, val ^ 0x86,
3096                              contents + roff - 1);
3097                   bfd_put_32 (output_bfd, -tpoff (info, relocation),
3098                               contents + roff);
3099                   continue;
3100                 }
3101               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3102                 {
3103                   /* GDesc -> LE transition.
3104                      It's originally:
3105                      call *(%eax)
3106                      Turn it into:
3107                      xchg %ax,%ax  */
3108
3109                   bfd_vma roff;
3110                   
3111                   roff = rel->r_offset;
3112                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3113                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3114                   continue;
3115                 }
3116               else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3117                 {
3118                   unsigned int val;
3119
3120                   /* IE->LE transition:
3121                      Originally it can be one of:
3122                      movl foo, %eax
3123                      movl foo, %reg
3124                      addl foo, %reg
3125                      We change it into:
3126                      movl $foo, %eax
3127                      movl $foo, %reg
3128                      addl $foo, %reg.  */
3129                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3130                   if (val == 0xa1)
3131                     {
3132                       /* movl foo, %eax.  */
3133                       bfd_put_8 (output_bfd, 0xb8,
3134                                  contents + rel->r_offset - 1);
3135                     }
3136                   else
3137                     {
3138                       unsigned int type;
3139
3140                       type = bfd_get_8 (input_bfd,
3141                                         contents + rel->r_offset - 2);
3142                       switch (type)
3143                         {
3144                         case 0x8b:
3145                           /* movl */
3146                           bfd_put_8 (output_bfd, 0xc7,
3147                                      contents + rel->r_offset - 2);
3148                           bfd_put_8 (output_bfd,
3149                                      0xc0 | ((val >> 3) & 7),
3150                                      contents + rel->r_offset - 1);
3151                           break;
3152                         case 0x03:
3153                           /* addl */
3154                           bfd_put_8 (output_bfd, 0x81,
3155                                      contents + rel->r_offset - 2);
3156                           bfd_put_8 (output_bfd,
3157                                      0xc0 | ((val >> 3) & 7),
3158                                      contents + rel->r_offset - 1);
3159                           break;
3160                         default:
3161                           BFD_FAIL ();
3162                           break;
3163                         }
3164                     }
3165                   bfd_put_32 (output_bfd, -tpoff (info, relocation),
3166                               contents + rel->r_offset);
3167                   continue;
3168                 }
3169               else
3170                 {
3171                   unsigned int val, type;
3172
3173                   /* {IE_32,GOTIE}->LE transition:
3174                      Originally it can be one of:
3175                      subl foo(%reg1), %reg2
3176                      movl foo(%reg1), %reg2
3177                      addl foo(%reg1), %reg2
3178                      We change it into:
3179                      subl $foo, %reg2
3180                      movl $foo, %reg2 (6 byte form)
3181                      addl $foo, %reg2.  */
3182                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3183                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3184                   if (type == 0x8b)
3185                     {
3186                       /* movl */
3187                       bfd_put_8 (output_bfd, 0xc7,
3188                                  contents + rel->r_offset - 2);
3189                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3190                                  contents + rel->r_offset - 1);
3191                     }
3192                   else if (type == 0x2b)
3193                     {
3194                       /* subl */
3195                       bfd_put_8 (output_bfd, 0x81,
3196                                  contents + rel->r_offset - 2);
3197                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3198                                  contents + rel->r_offset - 1);
3199                     }
3200                   else if (type == 0x03)
3201                     {
3202                       /* addl */
3203                       bfd_put_8 (output_bfd, 0x81,
3204                                  contents + rel->r_offset - 2);
3205                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3206                                  contents + rel->r_offset - 1);
3207                     }
3208                   else
3209                     BFD_FAIL ();
3210                   if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3211                     bfd_put_32 (output_bfd, -tpoff (info, relocation),
3212                                 contents + rel->r_offset);
3213                   else
3214                     bfd_put_32 (output_bfd, tpoff (info, relocation),
3215                                 contents + rel->r_offset);
3216                   continue;
3217                 }
3218             }
3219
3220           if (htab->sgot == NULL)
3221             abort ();
3222
3223           if (h != NULL)
3224             {
3225               off = h->got.offset;
3226               offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3227             }
3228           else
3229             {
3230               if (local_got_offsets == NULL)
3231                 abort ();
3232
3233               off = local_got_offsets[r_symndx];
3234               offplt = local_tlsdesc_gotents[r_symndx];
3235             }
3236
3237           if ((off & 1) != 0)
3238             off &= ~1;
3239           else
3240             {
3241               Elf_Internal_Rela outrel;
3242               bfd_byte *loc;
3243               int dr_type, indx;
3244               asection *sreloc;
3245
3246               if (htab->srelgot == NULL)
3247                 abort ();
3248
3249               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3250
3251               if (GOT_TLS_GDESC_P (tls_type))
3252                 {
3253                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3254                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3255                               <= htab->sgotplt->size);
3256                   outrel.r_offset = (htab->sgotplt->output_section->vma
3257                                      + htab->sgotplt->output_offset
3258                                      + offplt
3259                                      + htab->sgotplt_jump_table_size);
3260                   sreloc = htab->srelplt;
3261                   loc = sreloc->contents;
3262                   loc += (htab->next_tls_desc_index++
3263                           * sizeof (Elf32_External_Rel));
3264                   BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3265                               <= sreloc->contents + sreloc->size);
3266                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3267                   if (indx == 0)
3268                     {
3269                       BFD_ASSERT (! unresolved_reloc);
3270                       bfd_put_32 (output_bfd,
3271                                   relocation - dtpoff_base (info),
3272                                   htab->sgotplt->contents + offplt
3273                                   + htab->sgotplt_jump_table_size + 4);
3274                     }
3275                   else
3276                     {
3277                       bfd_put_32 (output_bfd, 0,
3278                                   htab->sgotplt->contents + offplt
3279                                   + htab->sgotplt_jump_table_size + 4);
3280                     }
3281                 }
3282
3283               sreloc = htab->srelgot;
3284
3285               outrel.r_offset = (htab->sgot->output_section->vma
3286                                  + htab->sgot->output_offset + off);
3287
3288               if (GOT_TLS_GD_P (tls_type))
3289                 dr_type = R_386_TLS_DTPMOD32;
3290               else if (GOT_TLS_GDESC_P (tls_type))
3291                 goto dr_done;
3292               else if (tls_type == GOT_TLS_IE_POS)
3293                 dr_type = R_386_TLS_TPOFF;
3294               else
3295                 dr_type = R_386_TLS_TPOFF32;
3296
3297               if (dr_type == R_386_TLS_TPOFF && indx == 0)
3298                 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
3299                             htab->sgot->contents + off);
3300               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3301                 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
3302                             htab->sgot->contents + off);
3303               else if (dr_type != R_386_TLS_DESC)
3304                 bfd_put_32 (output_bfd, 0,
3305                             htab->sgot->contents + off);
3306               outrel.r_info = ELF32_R_INFO (indx, dr_type);
3307
3308               loc = sreloc->contents;
3309               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3310               BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3311                           <= sreloc->contents + sreloc->size);
3312               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3313
3314               if (GOT_TLS_GD_P (tls_type))
3315                 {
3316                   if (indx == 0)
3317                     {
3318                       BFD_ASSERT (! unresolved_reloc);
3319                       bfd_put_32 (output_bfd,
3320                                   relocation - dtpoff_base (info),
3321                                   htab->sgot->contents + off + 4);
3322                     }
3323                   else
3324                     {
3325                       bfd_put_32 (output_bfd, 0,
3326                                   htab->sgot->contents + off + 4);
3327                       outrel.r_info = ELF32_R_INFO (indx,
3328                                                     R_386_TLS_DTPOFF32);
3329                       outrel.r_offset += 4;
3330                       sreloc->reloc_count++;
3331                       loc += sizeof (Elf32_External_Rel);
3332                       BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3333                                   <= sreloc->contents + sreloc->size);
3334                       bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3335                     }
3336                 }
3337               else if (tls_type == GOT_TLS_IE_BOTH)
3338                 {
3339                   bfd_put_32 (output_bfd,
3340                               indx == 0 ? relocation - dtpoff_base (info) : 0,
3341                               htab->sgot->contents + off + 4);
3342                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3343                   outrel.r_offset += 4;
3344                   sreloc->reloc_count++;
3345                   loc += sizeof (Elf32_External_Rel);
3346                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3347                 }
3348
3349             dr_done:
3350               if (h != NULL)
3351                 h->got.offset |= 1;
3352               else
3353                 local_got_offsets[r_symndx] |= 1;
3354             }
3355
3356           if (off >= (bfd_vma) -2
3357               && ! GOT_TLS_GDESC_P (tls_type))
3358             abort ();
3359           if (r_type == R_386_TLS_GOTDESC
3360               || r_type == R_386_TLS_DESC_CALL)
3361             {
3362               relocation = htab->sgotplt_jump_table_size + offplt;
3363               unresolved_reloc = FALSE;
3364             }
3365           else if (r_type == ELF32_R_TYPE (rel->r_info))
3366             {
3367               bfd_vma g_o_t = htab->sgotplt->output_section->vma
3368                               + htab->sgotplt->output_offset;
3369               relocation = htab->sgot->output_section->vma
3370                 + htab->sgot->output_offset + off - g_o_t;
3371               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3372                   && tls_type == GOT_TLS_IE_BOTH)
3373                 relocation += 4;
3374               if (r_type == R_386_TLS_IE)
3375                 relocation += g_o_t;
3376               unresolved_reloc = FALSE;
3377             }
3378           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3379             {
3380               unsigned int val, type;
3381               bfd_vma roff;
3382
3383               /* GD->IE transition.  */
3384               type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3385               val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3386               if (type == 0x04)
3387                 {
3388                   /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3389                      Change it into:
3390                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
3391                   val >>= 3;
3392                   roff = rel->r_offset - 3;
3393                 }
3394               else
3395                 {
3396                   /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3397                      Change it into:
3398                      movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax.  */
3399                   roff = rel->r_offset - 2;
3400                 }
3401               memcpy (contents + roff,
3402                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3403               contents[roff + 7] = 0x80 | (val & 7);
3404               /* If foo is used only with foo@gotntpoff(%reg) and
3405                  foo@indntpoff, but not with foo@gottpoff(%reg), change
3406                  subl $foo@gottpoff(%reg), %eax
3407                  into:
3408                  addl $foo@gotntpoff(%reg), %eax.  */
3409               if (tls_type == GOT_TLS_IE_POS)
3410                 contents[roff + 6] = 0x03;
3411               bfd_put_32 (output_bfd,
3412                           htab->sgot->output_section->vma
3413                           + htab->sgot->output_offset + off
3414                           - htab->sgotplt->output_section->vma
3415                           - htab->sgotplt->output_offset,
3416                           contents + roff + 8);
3417               /* Skip R_386_PLT32.  */
3418               rel++;
3419               continue;
3420             }
3421           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3422             {
3423               /* GDesc -> IE transition.
3424                  It's originally something like:
3425                  leal x@tlsdesc(%ebx), %eax
3426
3427                  Change it to:
3428                  movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3429                  or:
3430                  movl x@gottpoff(%ebx), %eax # before negl %eax
3431
3432                  Registers other than %eax may be set up here.  */
3433
3434               bfd_vma roff;
3435
3436               /* First, make sure it's a leal adding ebx to a 32-bit
3437                  offset into any register, although it's probably
3438                  almost always going to be eax.  */
3439               roff = rel->r_offset;
3440
3441               /* Now modify the instruction as appropriate.  */
3442               /* To turn a leal into a movl in the form we use it, it
3443                  suffices to change the first byte from 0x8d to 0x8b.
3444                  aoliva FIXME: should we decide to keep the leal, all
3445                  we have to do is remove the statement below, and
3446                  adjust the relaxation of R_386_TLS_DESC_CALL.  */
3447               bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3448
3449               if (tls_type == GOT_TLS_IE_BOTH)
3450                 off += 4;
3451
3452               bfd_put_32 (output_bfd,
3453                           htab->sgot->output_section->vma
3454                           + htab->sgot->output_offset + off
3455                           - htab->sgotplt->output_section->vma
3456                           - htab->sgotplt->output_offset,
3457                           contents + roff);
3458               continue;
3459             }
3460           else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3461             {
3462               /* GDesc -> IE transition.
3463                  It's originally:
3464                  call *(%eax)
3465
3466                  Change it to:
3467                  xchg %ax,%ax
3468                  or
3469                  negl %eax
3470                  depending on how we transformed the TLS_GOTDESC above.
3471               */
3472
3473               bfd_vma roff;
3474
3475               roff = rel->r_offset;
3476
3477               /* Now modify the instruction as appropriate.  */
3478               if (tls_type != GOT_TLS_IE_NEG)
3479                 {
3480                   /* xchg %ax,%ax */
3481                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3482                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3483                 }
3484               else
3485                 {
3486                   /* negl %eax */
3487                   bfd_put_8 (output_bfd, 0xf7, contents + roff);
3488                   bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3489                 }
3490
3491               continue;
3492             }
3493           else
3494             BFD_ASSERT (FALSE);
3495           break;
3496
3497         case R_386_TLS_LDM:
3498           if (! elf_i386_tls_transition (info, input_bfd,
3499                                          input_section, contents,
3500                                          symtab_hdr, sym_hashes,
3501                                          &r_type, GOT_UNKNOWN, rel,
3502                                          relend, h))
3503             return FALSE;
3504
3505           if (r_type != R_386_TLS_LDM)
3506             {
3507               /* LD->LE transition:
3508                  leal foo(%reg), %eax; call ___tls_get_addr.
3509                  We change it into:
3510                  movl %gs:0, %eax; nop; leal 0(%esi,1), %esi.  */
3511               BFD_ASSERT (r_type == R_386_TLS_LE_32);
3512               memcpy (contents + rel->r_offset - 2,
3513                       "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3514               /* Skip R_386_PC32/R_386_PLT32.  */
3515               rel++;
3516               continue;
3517             }
3518
3519           if (htab->sgot == NULL)
3520             abort ();
3521
3522           off = htab->tls_ldm_got.offset;
3523           if (off & 1)
3524             off &= ~1;
3525           else
3526             {
3527               Elf_Internal_Rela outrel;
3528               bfd_byte *loc;
3529
3530               if (htab->srelgot == NULL)
3531                 abort ();
3532
3533               outrel.r_offset = (htab->sgot->output_section->vma
3534                                  + htab->sgot->output_offset + off);
3535
3536               bfd_put_32 (output_bfd, 0,
3537                           htab->sgot->contents + off);
3538               bfd_put_32 (output_bfd, 0,
3539                           htab->sgot->contents + off + 4);
3540               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3541               loc = htab->srelgot->contents;
3542               loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3543               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3544               htab->tls_ldm_got.offset |= 1;
3545             }
3546           relocation = htab->sgot->output_section->vma
3547                        + htab->sgot->output_offset + off
3548                        - htab->sgotplt->output_section->vma
3549                        - htab->sgotplt->output_offset;
3550           unresolved_reloc = FALSE;
3551           break;
3552
3553         case R_386_TLS_LDO_32:
3554           if (info->shared || (input_section->flags & SEC_CODE) == 0)
3555             relocation -= dtpoff_base (info);
3556           else
3557             /* When converting LDO to LE, we must negate.  */
3558             relocation = -tpoff (info, relocation);
3559           break;
3560
3561         case R_386_TLS_LE_32:
3562         case R_386_TLS_LE:
3563           if (info->shared)
3564             {
3565               Elf_Internal_Rela outrel;
3566               asection *sreloc;
3567               bfd_byte *loc;
3568               int indx;
3569
3570               outrel.r_offset = rel->r_offset
3571                                 + input_section->output_section->vma
3572                                 + input_section->output_offset;
3573               if (h != NULL && h->dynindx != -1)
3574                 indx = h->dynindx;
3575               else
3576                 indx = 0;
3577               if (r_type == R_386_TLS_LE_32)
3578                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3579               else
3580                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3581               sreloc = elf_section_data (input_section)->sreloc;
3582               if (sreloc == NULL)
3583                 abort ();
3584               loc = sreloc->contents;
3585               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3586               bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3587               if (indx)
3588                 continue;
3589               else if (r_type == R_386_TLS_LE_32)
3590                 relocation = dtpoff_base (info) - relocation;
3591               else
3592                 relocation -= dtpoff_base (info);
3593             }
3594           else if (r_type == R_386_TLS_LE_32)
3595             relocation = tpoff (info, relocation);
3596           else
3597             relocation = -tpoff (info, relocation);
3598           break;
3599
3600         default:
3601           break;
3602         }
3603
3604       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3605          because such sections are not SEC_ALLOC and thus ld.so will
3606          not process them.  */
3607       if (unresolved_reloc
3608           && !((input_section->flags & SEC_DEBUGGING) != 0
3609                && h->def_dynamic))
3610         {
3611           (*_bfd_error_handler)
3612             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3613              input_bfd,
3614              input_section,
3615              (long) rel->r_offset,
3616              howto->name,
3617              h->root.root.string);
3618           return FALSE;
3619         }
3620
3621       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3622                                     contents, rel->r_offset,
3623                                     relocation, 0);
3624
3625       if (r != bfd_reloc_ok)
3626         {
3627           const char *name;
3628
3629           if (h != NULL)
3630             name = h->root.root.string;
3631           else
3632             {
3633               name = bfd_elf_string_from_elf_section (input_bfd,
3634                                                       symtab_hdr->sh_link,
3635                                                       sym->st_name);
3636               if (name == NULL)
3637                 return FALSE;
3638               if (*name == '\0')
3639                 name = bfd_section_name (input_bfd, sec);
3640             }
3641
3642           if (r == bfd_reloc_overflow)
3643             {
3644               if (! ((*info->callbacks->reloc_overflow)
3645                      (info, (h ? &h->root : NULL), name, howto->name,
3646                       (bfd_vma) 0, input_bfd, input_section,
3647                       rel->r_offset)))
3648                 return FALSE;
3649             }
3650           else
3651             {
3652               (*_bfd_error_handler)
3653                 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3654                  input_bfd, input_section,
3655                  (long) rel->r_offset, name, (int) r);
3656               return FALSE;
3657             }
3658         }
3659     }
3660
3661   return TRUE;
3662 }
3663
3664 /* Finish up dynamic symbol handling.  We set the contents of various
3665    dynamic sections here.  */
3666
3667 static bfd_boolean
3668 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3669                                 struct bfd_link_info *info,
3670                                 struct elf_link_hash_entry *h,
3671                                 Elf_Internal_Sym *sym)
3672 {
3673   struct elf_i386_link_hash_table *htab;
3674
3675   htab = elf_i386_hash_table (info);
3676
3677   if (h->plt.offset != (bfd_vma) -1)
3678     {
3679       bfd_vma plt_index;
3680       bfd_vma got_offset;
3681       Elf_Internal_Rela rel;
3682       bfd_byte *loc;
3683
3684       /* This symbol has an entry in the procedure linkage table.  Set
3685          it up.  */
3686
3687       if (h->dynindx == -1
3688           || htab->splt == NULL
3689           || htab->sgotplt == NULL
3690           || htab->srelplt == NULL)
3691         abort ();
3692
3693       /* Get the index in the procedure linkage table which
3694          corresponds to this symbol.  This is the index of this symbol
3695          in all the symbols for which we are making plt entries.  The
3696          first entry in the procedure linkage table is reserved.  */
3697       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3698
3699       /* Get the offset into the .got table of the entry that
3700          corresponds to this function.  Each .got entry is 4 bytes.
3701          The first three are reserved.  */
3702       got_offset = (plt_index + 3) * 4;
3703
3704       /* Fill in the entry in the procedure linkage table.  */
3705       if (! info->shared)
3706         {
3707           memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3708                   PLT_ENTRY_SIZE);
3709           bfd_put_32 (output_bfd,
3710                       (htab->sgotplt->output_section->vma
3711                        + htab->sgotplt->output_offset
3712                        + got_offset),
3713                       htab->splt->contents + h->plt.offset + 2);
3714
3715           if (htab->is_vxworks)
3716             {
3717               int s, k, reloc_index;
3718
3719               /* Create the R_386_32 relocation referencing the GOT
3720                  for this PLT entry.  */
3721
3722               /* S: Current slot number (zero-based).  */
3723               s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
3724               /* K: Number of relocations for PLTResolve. */
3725               if (info->shared)
3726                 k = PLTRESOLVE_RELOCS_SHLIB;
3727               else
3728                 k = PLTRESOLVE_RELOCS;
3729               /* Skip the PLTresolve relocations, and the relocations for
3730                  the other PLT slots. */
3731               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3732               loc = (htab->srelplt2->contents + reloc_index
3733                      * sizeof (Elf32_External_Rel));
3734
3735               rel.r_offset = (htab->splt->output_section->vma
3736                               + htab->splt->output_offset
3737                               + h->plt.offset + 2),
3738               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3739               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3740
3741               /* Create the R_386_32 relocation referencing the beginning of
3742                  the PLT for this GOT entry.  */
3743               rel.r_offset = (htab->sgotplt->output_section->vma
3744                               + htab->sgotplt->output_offset
3745                               + got_offset);
3746               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3747               bfd_elf32_swap_reloc_out (output_bfd, &rel,
3748               loc + sizeof (Elf32_External_Rel));
3749             }
3750         }
3751       else
3752         {
3753           memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3754                   PLT_ENTRY_SIZE);
3755           bfd_put_32 (output_bfd, got_offset,
3756                       htab->splt->contents + h->plt.offset + 2);
3757         }
3758
3759       bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3760                   htab->splt->contents + h->plt.offset + 7);
3761       bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3762                   htab->splt->contents + h->plt.offset + 12);
3763
3764       /* Fill in the entry in the global offset table.  */
3765       bfd_put_32 (output_bfd,
3766                   (htab->splt->output_section->vma
3767                    + htab->splt->output_offset
3768                    + h->plt.offset
3769                    + 6),
3770                   htab->sgotplt->contents + got_offset);
3771
3772       /* Fill in the entry in the .rel.plt section.  */
3773       rel.r_offset = (htab->sgotplt->output_section->vma
3774                       + htab->sgotplt->output_offset
3775                       + got_offset);
3776       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3777       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3778       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3779
3780       if (!h->def_regular)
3781         {
3782           /* Mark the symbol as undefined, rather than as defined in
3783              the .plt section.  Leave the value if there were any
3784              relocations where pointer equality matters (this is a clue
3785              for the dynamic linker, to make function pointer
3786              comparisons work between an application and shared
3787              library), otherwise set it to zero.  If a function is only
3788              called from a binary, there is no need to slow down
3789              shared libraries because of that.  */
3790           sym->st_shndx = SHN_UNDEF;
3791           if (!h->pointer_equality_needed)
3792             sym->st_value = 0;
3793         }
3794     }
3795
3796   if (h->got.offset != (bfd_vma) -1
3797       && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
3798       && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3799     {
3800       Elf_Internal_Rela rel;
3801       bfd_byte *loc;
3802
3803       /* This symbol has an entry in the global offset table.  Set it
3804          up.  */
3805
3806       if (htab->sgot == NULL || htab->srelgot == NULL)
3807         abort ();
3808
3809       rel.r_offset = (htab->sgot->output_section->vma
3810                       + htab->sgot->output_offset
3811                       + (h->got.offset & ~(bfd_vma) 1));
3812
3813       /* If this is a static link, or it is a -Bsymbolic link and the
3814          symbol is defined locally or was forced to be local because
3815          of a version file, we just want to emit a RELATIVE reloc.
3816          The entry in the global offset table will already have been
3817          initialized in the relocate_section function.  */
3818       if (info->shared
3819           && SYMBOL_REFERENCES_LOCAL (info, h))
3820         {
3821           BFD_ASSERT((h->got.offset & 1) != 0);
3822           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3823         }
3824       else
3825         {
3826           BFD_ASSERT((h->got.offset & 1) == 0);
3827           bfd_put_32 (output_bfd, (bfd_vma) 0,
3828                       htab->sgot->contents + h->got.offset);
3829           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3830         }
3831
3832       loc = htab->srelgot->contents;
3833       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3834       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3835     }
3836
3837   if (h->needs_copy)
3838     {
3839       Elf_Internal_Rela rel;
3840       bfd_byte *loc;
3841
3842       /* This symbol needs a copy reloc.  Set it up.  */
3843
3844       if (h->dynindx == -1
3845           || (h->root.type != bfd_link_hash_defined
3846               && h->root.type != bfd_link_hash_defweak)
3847           || htab->srelbss == NULL)
3848         abort ();
3849
3850       rel.r_offset = (h->root.u.def.value
3851                       + h->root.u.def.section->output_section->vma
3852                       + h->root.u.def.section->output_offset);
3853       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3854       loc = htab->srelbss->contents;
3855       loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3856       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3857     }
3858
3859   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
3860      On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
3861      is relative to the ".got" section.  */
3862   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3863       || (!htab->is_vxworks && h == htab->elf.hgot))
3864     sym->st_shndx = SHN_ABS;
3865
3866   return TRUE;
3867 }
3868
3869 /* Used to decide how to sort relocs in an optimal manner for the
3870    dynamic linker, before writing them out.  */
3871
3872 static enum elf_reloc_type_class
3873 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3874 {
3875   switch (ELF32_R_TYPE (rela->r_info))
3876     {
3877     case R_386_RELATIVE:
3878       return reloc_class_relative;
3879     case R_386_JUMP_SLOT:
3880       return reloc_class_plt;
3881     case R_386_COPY:
3882       return reloc_class_copy;
3883     default:
3884       return reloc_class_normal;
3885     }
3886 }
3887
3888 /* Finish up the dynamic sections.  */
3889
3890 static bfd_boolean
3891 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3892                                   struct bfd_link_info *info)
3893 {
3894   struct elf_i386_link_hash_table *htab;
3895   bfd *dynobj;
3896   asection *sdyn;
3897
3898   htab = elf_i386_hash_table (info);
3899   dynobj = htab->elf.dynobj;
3900   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3901
3902   if (htab->elf.dynamic_sections_created)
3903     {
3904       Elf32_External_Dyn *dyncon, *dynconend;
3905
3906       if (sdyn == NULL || htab->sgot == NULL)
3907         abort ();
3908
3909       dyncon = (Elf32_External_Dyn *) sdyn->contents;
3910       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3911       for (; dyncon < dynconend; dyncon++)
3912         {
3913           Elf_Internal_Dyn dyn;
3914           asection *s;
3915
3916           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3917
3918           switch (dyn.d_tag)
3919             {
3920             default:
3921               if (htab->is_vxworks
3922                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
3923                 break;
3924               continue;
3925
3926             case DT_PLTGOT:
3927               s = htab->sgotplt;
3928               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3929               break;
3930
3931             case DT_JMPREL:
3932               s = htab->srelplt;
3933               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3934               break;
3935
3936             case DT_PLTRELSZ:
3937               s = htab->srelplt;
3938               dyn.d_un.d_val = s->size;
3939               break;
3940
3941             case DT_RELSZ:
3942               /* My reading of the SVR4 ABI indicates that the
3943                  procedure linkage table relocs (DT_JMPREL) should be
3944                  included in the overall relocs (DT_REL).  This is
3945                  what Solaris does.  However, UnixWare can not handle
3946                  that case.  Therefore, we override the DT_RELSZ entry
3947                  here to make it not include the JMPREL relocs.  */
3948               s = htab->srelplt;
3949               if (s == NULL)
3950                 continue;
3951               dyn.d_un.d_val -= s->size;
3952               break;
3953
3954             case DT_REL:
3955               /* We may not be using the standard ELF linker script.
3956                  If .rel.plt is the first .rel section, we adjust
3957                  DT_REL to not include it.  */
3958               s = htab->srelplt;
3959               if (s == NULL)
3960                 continue;
3961               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3962                 continue;
3963               dyn.d_un.d_ptr += s->size;
3964               break;
3965             }
3966
3967           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3968         }
3969
3970       /* Fill in the first entry in the procedure linkage table.  */
3971       if (htab->splt && htab->splt->size > 0)
3972         {
3973           if (info->shared)
3974             {
3975               memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
3976                       sizeof (elf_i386_pic_plt0_entry));
3977               memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
3978                       htab->plt0_pad_byte,
3979                       PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
3980             }
3981           else
3982             {
3983               memcpy (htab->splt->contents, elf_i386_plt0_entry,
3984                       sizeof(elf_i386_plt0_entry));
3985               memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
3986                       htab->plt0_pad_byte,
3987                       PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
3988               bfd_put_32 (output_bfd,
3989                           (htab->sgotplt->output_section->vma
3990                            + htab->sgotplt->output_offset
3991                            + 4),
3992                           htab->splt->contents + 2);
3993               bfd_put_32 (output_bfd,
3994                           (htab->sgotplt->output_section->vma
3995                            + htab->sgotplt->output_offset
3996                            + 8),
3997                           htab->splt->contents + 8);
3998
3999               if (htab->is_vxworks)
4000                 {
4001                   Elf_Internal_Rela rel;
4002
4003                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
4004                      On IA32 we use REL relocations so the addend goes in
4005                      the PLT directly.  */
4006                   rel.r_offset = (htab->splt->output_section->vma
4007                                   + htab->splt->output_offset
4008                                   + 2);
4009                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4010                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
4011                                             htab->srelplt2->contents);
4012                   /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
4013                   rel.r_offset = (htab->splt->output_section->vma
4014                                   + htab->splt->output_offset
4015                                   + 8);
4016                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4017                   bfd_elf32_swap_reloc_out (output_bfd, &rel,
4018                                             htab->srelplt2->contents +
4019                                             sizeof (Elf32_External_Rel));
4020                 }
4021             }
4022
4023           /* UnixWare sets the entsize of .plt to 4, although that doesn't
4024              really seem like the right value.  */
4025           elf_section_data (htab->splt->output_section)
4026             ->this_hdr.sh_entsize = 4;
4027
4028           /* Correct the .rel.plt.unloaded relocations.  */
4029           if (htab->is_vxworks && !info->shared)
4030             {
4031               int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
4032               unsigned char *p;
4033
4034               p = htab->srelplt2->contents;
4035               if (info->shared)
4036                 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4037               else
4038                 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4039
4040               for (; num_plts; num_plts--)
4041                 {
4042                   Elf_Internal_Rela rel;
4043                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4044                   rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4045                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4046                   p += sizeof (Elf32_External_Rel);
4047
4048                   bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4049                   rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4050                   bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4051                   p += sizeof (Elf32_External_Rel);
4052                 }
4053             }
4054         }
4055     }
4056
4057   if (htab->sgotplt)
4058     {
4059       /* Fill in the first three entries in the global offset table.  */
4060       if (htab->sgotplt->size > 0)
4061         {
4062           bfd_put_32 (output_bfd,
4063                       (sdyn == NULL ? 0
4064                        : sdyn->output_section->vma + sdyn->output_offset),
4065                       htab->sgotplt->contents);
4066           bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
4067           bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
4068         }
4069
4070       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
4071     }
4072
4073   if (htab->sgot && htab->sgot->size > 0)
4074     elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
4075
4076   return TRUE;
4077 }
4078
4079 /* Return address for Ith PLT stub in section PLT, for relocation REL
4080    or (bfd_vma) -1 if it should not be included.  */
4081
4082 static bfd_vma
4083 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4084                       const arelent *rel ATTRIBUTE_UNUSED)
4085 {
4086   return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4087 }
4088
4089 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
4090
4091 static bfd_boolean
4092 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4093 {
4094   if (h->plt.offset != (bfd_vma) -1
4095       && !h->def_regular
4096       && !h->pointer_equality_needed)
4097     return FALSE;
4098
4099   return _bfd_elf_hash_symbol (h);
4100 }
4101
4102 /* Hook called by the linker routine which adds symbols from an object
4103    file.  */
4104
4105 static bfd_boolean
4106 elf_i386_add_symbol_hook (bfd * abfd ATTRIBUTE_UNUSED,
4107                           struct bfd_link_info * info ATTRIBUTE_UNUSED,
4108                           Elf_Internal_Sym * sym,
4109                           const char ** namep ATTRIBUTE_UNUSED,
4110                           flagword * flagsp ATTRIBUTE_UNUSED,
4111                           asection ** secp ATTRIBUTE_UNUSED,
4112                           bfd_vma * valp ATTRIBUTE_UNUSED)
4113 {
4114   if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4115     elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4116
4117   return TRUE;
4118 }
4119
4120 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vec
4121 #define TARGET_LITTLE_NAME              "elf32-i386"
4122 #define ELF_ARCH                        bfd_arch_i386
4123 #define ELF_MACHINE_CODE                EM_386
4124 #define ELF_MAXPAGESIZE                 0x1000
4125
4126 #define elf_backend_can_gc_sections     1
4127 #define elf_backend_can_refcount        1
4128 #define elf_backend_want_got_plt        1
4129 #define elf_backend_plt_readonly        1
4130 #define elf_backend_want_plt_sym        0
4131 #define elf_backend_got_header_size     12
4132
4133 /* Support RELA for objdump of prelink objects.  */
4134 #define elf_info_to_howto                     elf_i386_info_to_howto_rel
4135 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
4136
4137 #define bfd_elf32_mkobject                    elf_i386_mkobject
4138
4139 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4140 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
4141 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
4142 #define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
4143
4144 #define elf_backend_adjust_dynamic_symbol     elf_i386_adjust_dynamic_symbol
4145 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
4146 #define elf_backend_check_relocs              elf_i386_check_relocs
4147 #define elf_backend_copy_indirect_symbol      elf_i386_copy_indirect_symbol
4148 #define elf_backend_create_dynamic_sections   elf_i386_create_dynamic_sections
4149 #define elf_backend_fake_sections             elf_i386_fake_sections
4150 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4151 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4152 #define elf_backend_gc_mark_hook              elf_i386_gc_mark_hook
4153 #define elf_backend_gc_sweep_hook             elf_i386_gc_sweep_hook
4154 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
4155 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
4156 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
4157 #define elf_backend_relocate_section          elf_i386_relocate_section
4158 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
4159 #define elf_backend_always_size_sections      elf_i386_always_size_sections
4160 #define elf_backend_omit_section_dynsym \
4161   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4162 #define elf_backend_plt_sym_val               elf_i386_plt_sym_val
4163 #define elf_backend_hash_symbol               elf_i386_hash_symbol
4164 #define elf_backend_add_symbol_hook           elf_i386_add_symbol_hook
4165 #undef  elf_backend_post_process_headers
4166 #define elf_backend_post_process_headers        _bfd_elf_set_osabi
4167
4168 #include "elf32-target.h"
4169
4170 /* FreeBSD support.  */
4171
4172 #undef  TARGET_LITTLE_SYM
4173 #define TARGET_LITTLE_SYM               bfd_elf32_i386_freebsd_vec
4174 #undef  TARGET_LITTLE_NAME
4175 #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
4176 #undef  ELF_OSABI
4177 #define ELF_OSABI                       ELFOSABI_FREEBSD
4178
4179 /* The kernel recognizes executables as valid only if they carry a
4180    "FreeBSD" label in the ELF header.  So we put this label on all
4181    executables and (for simplicity) also all other object files.  */
4182
4183 static void
4184 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4185 {
4186   _bfd_elf_set_osabi (abfd, info);
4187
4188 #ifdef OLD_FREEBSD_ABI_LABEL
4189   /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4190   memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4191 #endif
4192 }
4193
4194 #undef  elf_backend_post_process_headers
4195 #define elf_backend_post_process_headers        elf_i386_fbsd_post_process_headers
4196 #undef  elf32_bed
4197 #define elf32_bed                               elf32_i386_fbsd_bed
4198
4199 #undef elf_backend_add_symbol_hook
4200
4201 #include "elf32-target.h"
4202
4203 /* VxWorks support.  */
4204
4205 #undef  TARGET_LITTLE_SYM
4206 #define TARGET_LITTLE_SYM               bfd_elf32_i386_vxworks_vec
4207 #undef  TARGET_LITTLE_NAME
4208 #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
4209 #undef  ELF_OSABI
4210
4211 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks.  */
4212
4213 static struct bfd_link_hash_table *
4214 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4215 {
4216   struct bfd_link_hash_table *ret;
4217   struct elf_i386_link_hash_table *htab;
4218
4219   ret = elf_i386_link_hash_table_create (abfd);
4220   if (ret)
4221     {
4222       htab = (struct elf_i386_link_hash_table *) ret;
4223       htab->is_vxworks = 1;
4224       htab->plt0_pad_byte = 0x90;
4225     }
4226
4227   return ret;
4228 }
4229
4230
4231 #undef elf_backend_relocs_compatible
4232 #undef elf_backend_post_process_headers
4233 #undef bfd_elf32_bfd_link_hash_table_create
4234 #define bfd_elf32_bfd_link_hash_table_create \
4235   elf_i386_vxworks_link_hash_table_create
4236 #undef elf_backend_add_symbol_hook
4237 #define elf_backend_add_symbol_hook \
4238   elf_vxworks_add_symbol_hook
4239 #undef elf_backend_link_output_symbol_hook
4240 #define elf_backend_link_output_symbol_hook \
4241   elf_vxworks_link_output_symbol_hook
4242 #undef elf_backend_emit_relocs
4243 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
4244 #undef elf_backend_final_write_processing
4245 #define elf_backend_final_write_processing \
4246   elf_vxworks_final_write_processing
4247
4248 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4249    define it.  */
4250 #undef elf_backend_want_plt_sym
4251 #define elf_backend_want_plt_sym        1
4252
4253 #undef  elf32_bed
4254 #define elf32_bed                               elf32_i386_vxworks_bed
4255
4256 #include "elf32-target.h"