]> Git Repo - binutils.git/blob - bfd/elf32-i386.c
x86: Add TLS_TRANSITION_IE_TO_LE_P
[binutils.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2    Copyright (C) 1993-2017 Free Software Foundation, Inc.
3
4    This file is part of BFD, the Binary File Descriptor library.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20
21 #include "elfxx-x86.h"
22 #include "elf-nacl.h"
23 #include "elf-vxworks.h"
24 #include "dwarf2.h"
25 #include "opcode/i386.h"
26
27 /* 386 uses REL relocations instead of RELA.  */
28 #define USE_REL 1
29
30 #include "elf/i386.h"
31
32 static reloc_howto_type elf_howto_table[]=
33 {
34   HOWTO(R_386_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
35         bfd_elf_generic_reloc, "R_386_NONE",
36         TRUE, 0x00000000, 0x00000000, FALSE),
37   HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
38         bfd_elf_generic_reloc, "R_386_32",
39         TRUE, 0xffffffff, 0xffffffff, FALSE),
40   HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
41         bfd_elf_generic_reloc, "R_386_PC32",
42         TRUE, 0xffffffff, 0xffffffff, TRUE),
43   HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
44         bfd_elf_generic_reloc, "R_386_GOT32",
45         TRUE, 0xffffffff, 0xffffffff, FALSE),
46   HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
47         bfd_elf_generic_reloc, "R_386_PLT32",
48         TRUE, 0xffffffff, 0xffffffff, TRUE),
49   HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
50         bfd_elf_generic_reloc, "R_386_COPY",
51         TRUE, 0xffffffff, 0xffffffff, FALSE),
52   HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
53         bfd_elf_generic_reloc, "R_386_GLOB_DAT",
54         TRUE, 0xffffffff, 0xffffffff, FALSE),
55   HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56         bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
57         TRUE, 0xffffffff, 0xffffffff, FALSE),
58   HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
59         bfd_elf_generic_reloc, "R_386_RELATIVE",
60         TRUE, 0xffffffff, 0xffffffff, FALSE),
61   HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
62         bfd_elf_generic_reloc, "R_386_GOTOFF",
63         TRUE, 0xffffffff, 0xffffffff, FALSE),
64   HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
65         bfd_elf_generic_reloc, "R_386_GOTPC",
66         TRUE, 0xffffffff, 0xffffffff, TRUE),
67
68   /* We have a gap in the reloc numbers here.
69      R_386_standard counts the number up to this point, and
70      R_386_ext_offset is the value to subtract from a reloc type of
71      R_386_16 thru R_386_PC8 to form an index into this table.  */
72 #define R_386_standard (R_386_GOTPC + 1)
73 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
74
75   /* These relocs are a GNU extension.  */
76   HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
77         bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
78         TRUE, 0xffffffff, 0xffffffff, FALSE),
79   HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
80         bfd_elf_generic_reloc, "R_386_TLS_IE",
81         TRUE, 0xffffffff, 0xffffffff, FALSE),
82   HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
83         bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
84         TRUE, 0xffffffff, 0xffffffff, FALSE),
85   HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
86         bfd_elf_generic_reloc, "R_386_TLS_LE",
87         TRUE, 0xffffffff, 0xffffffff, FALSE),
88   HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
89         bfd_elf_generic_reloc, "R_386_TLS_GD",
90         TRUE, 0xffffffff, 0xffffffff, FALSE),
91   HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
92         bfd_elf_generic_reloc, "R_386_TLS_LDM",
93         TRUE, 0xffffffff, 0xffffffff, FALSE),
94   HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
95         bfd_elf_generic_reloc, "R_386_16",
96         TRUE, 0xffff, 0xffff, FALSE),
97   HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
98         bfd_elf_generic_reloc, "R_386_PC16",
99         TRUE, 0xffff, 0xffff, TRUE),
100   HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
101         bfd_elf_generic_reloc, "R_386_8",
102         TRUE, 0xff, 0xff, FALSE),
103   HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
104         bfd_elf_generic_reloc, "R_386_PC8",
105         TRUE, 0xff, 0xff, TRUE),
106
107 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
108 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
109   /* These are common with Solaris TLS implementation.  */
110   HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
111         bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
112         TRUE, 0xffffffff, 0xffffffff, FALSE),
113   HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
114         bfd_elf_generic_reloc, "R_386_TLS_IE_32",
115         TRUE, 0xffffffff, 0xffffffff, FALSE),
116   HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
117         bfd_elf_generic_reloc, "R_386_TLS_LE_32",
118         TRUE, 0xffffffff, 0xffffffff, FALSE),
119   HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
120         bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
121         TRUE, 0xffffffff, 0xffffffff, FALSE),
122   HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
123         bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
124         TRUE, 0xffffffff, 0xffffffff, FALSE),
125   HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
126         bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
127         TRUE, 0xffffffff, 0xffffffff, FALSE),
128   HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
129         bfd_elf_generic_reloc, "R_386_SIZE32",
130         TRUE, 0xffffffff, 0xffffffff, FALSE),
131   HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
132         bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
133         TRUE, 0xffffffff, 0xffffffff, FALSE),
134   HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont,
135         bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
136         FALSE, 0, 0, FALSE),
137   HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
138         bfd_elf_generic_reloc, "R_386_TLS_DESC",
139         TRUE, 0xffffffff, 0xffffffff, FALSE),
140   HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
141         bfd_elf_generic_reloc, "R_386_IRELATIVE",
142         TRUE, 0xffffffff, 0xffffffff, FALSE),
143   HOWTO(R_386_GOT32X, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
144         bfd_elf_generic_reloc, "R_386_GOT32X",
145         TRUE, 0xffffffff, 0xffffffff, FALSE),
146
147   /* Another gap.  */
148 #define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
149 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
150
151 /* GNU extension to record C++ vtable hierarchy.  */
152   HOWTO (R_386_GNU_VTINHERIT,   /* type */
153          0,                     /* rightshift */
154          2,                     /* size (0 = byte, 1 = short, 2 = long) */
155          0,                     /* bitsize */
156          FALSE,                 /* pc_relative */
157          0,                     /* bitpos */
158          complain_overflow_dont, /* complain_on_overflow */
159          NULL,                  /* special_function */
160          "R_386_GNU_VTINHERIT", /* name */
161          FALSE,                 /* partial_inplace */
162          0,                     /* src_mask */
163          0,                     /* dst_mask */
164          FALSE),                /* pcrel_offset */
165
166 /* GNU extension to record C++ vtable member usage.  */
167   HOWTO (R_386_GNU_VTENTRY,     /* type */
168          0,                     /* rightshift */
169          2,                     /* size (0 = byte, 1 = short, 2 = long) */
170          0,                     /* bitsize */
171          FALSE,                 /* pc_relative */
172          0,                     /* bitpos */
173          complain_overflow_dont, /* complain_on_overflow */
174          _bfd_elf_rel_vtable_reloc_fn, /* special_function */
175          "R_386_GNU_VTENTRY",   /* name */
176          FALSE,                 /* partial_inplace */
177          0,                     /* src_mask */
178          0,                     /* dst_mask */
179          FALSE)                 /* pcrel_offset */
180
181 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
182
183 };
184
185 #ifdef DEBUG_GEN_RELOC
186 #define TRACE(str) \
187   fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
188 #else
189 #define TRACE(str)
190 #endif
191
192 static reloc_howto_type *
193 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
194                             bfd_reloc_code_real_type code)
195 {
196   switch (code)
197     {
198     case BFD_RELOC_NONE:
199       TRACE ("BFD_RELOC_NONE");
200       return &elf_howto_table[R_386_NONE];
201
202     case BFD_RELOC_32:
203       TRACE ("BFD_RELOC_32");
204       return &elf_howto_table[R_386_32];
205
206     case BFD_RELOC_CTOR:
207       TRACE ("BFD_RELOC_CTOR");
208       return &elf_howto_table[R_386_32];
209
210     case BFD_RELOC_32_PCREL:
211       TRACE ("BFD_RELOC_PC32");
212       return &elf_howto_table[R_386_PC32];
213
214     case BFD_RELOC_386_GOT32:
215       TRACE ("BFD_RELOC_386_GOT32");
216       return &elf_howto_table[R_386_GOT32];
217
218     case BFD_RELOC_386_PLT32:
219       TRACE ("BFD_RELOC_386_PLT32");
220       return &elf_howto_table[R_386_PLT32];
221
222     case BFD_RELOC_386_COPY:
223       TRACE ("BFD_RELOC_386_COPY");
224       return &elf_howto_table[R_386_COPY];
225
226     case BFD_RELOC_386_GLOB_DAT:
227       TRACE ("BFD_RELOC_386_GLOB_DAT");
228       return &elf_howto_table[R_386_GLOB_DAT];
229
230     case BFD_RELOC_386_JUMP_SLOT:
231       TRACE ("BFD_RELOC_386_JUMP_SLOT");
232       return &elf_howto_table[R_386_JUMP_SLOT];
233
234     case BFD_RELOC_386_RELATIVE:
235       TRACE ("BFD_RELOC_386_RELATIVE");
236       return &elf_howto_table[R_386_RELATIVE];
237
238     case BFD_RELOC_386_GOTOFF:
239       TRACE ("BFD_RELOC_386_GOTOFF");
240       return &elf_howto_table[R_386_GOTOFF];
241
242     case BFD_RELOC_386_GOTPC:
243       TRACE ("BFD_RELOC_386_GOTPC");
244       return &elf_howto_table[R_386_GOTPC];
245
246       /* These relocs are a GNU extension.  */
247     case BFD_RELOC_386_TLS_TPOFF:
248       TRACE ("BFD_RELOC_386_TLS_TPOFF");
249       return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
250
251     case BFD_RELOC_386_TLS_IE:
252       TRACE ("BFD_RELOC_386_TLS_IE");
253       return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
254
255     case BFD_RELOC_386_TLS_GOTIE:
256       TRACE ("BFD_RELOC_386_TLS_GOTIE");
257       return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
258
259     case BFD_RELOC_386_TLS_LE:
260       TRACE ("BFD_RELOC_386_TLS_LE");
261       return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
262
263     case BFD_RELOC_386_TLS_GD:
264       TRACE ("BFD_RELOC_386_TLS_GD");
265       return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
266
267     case BFD_RELOC_386_TLS_LDM:
268       TRACE ("BFD_RELOC_386_TLS_LDM");
269       return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
270
271     case BFD_RELOC_16:
272       TRACE ("BFD_RELOC_16");
273       return &elf_howto_table[R_386_16 - R_386_ext_offset];
274
275     case BFD_RELOC_16_PCREL:
276       TRACE ("BFD_RELOC_16_PCREL");
277       return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
278
279     case BFD_RELOC_8:
280       TRACE ("BFD_RELOC_8");
281       return &elf_howto_table[R_386_8 - R_386_ext_offset];
282
283     case BFD_RELOC_8_PCREL:
284       TRACE ("BFD_RELOC_8_PCREL");
285       return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
286
287     /* Common with Sun TLS implementation.  */
288     case BFD_RELOC_386_TLS_LDO_32:
289       TRACE ("BFD_RELOC_386_TLS_LDO_32");
290       return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
291
292     case BFD_RELOC_386_TLS_IE_32:
293       TRACE ("BFD_RELOC_386_TLS_IE_32");
294       return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
295
296     case BFD_RELOC_386_TLS_LE_32:
297       TRACE ("BFD_RELOC_386_TLS_LE_32");
298       return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
299
300     case BFD_RELOC_386_TLS_DTPMOD32:
301       TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
302       return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
303
304     case BFD_RELOC_386_TLS_DTPOFF32:
305       TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
306       return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
307
308     case BFD_RELOC_386_TLS_TPOFF32:
309       TRACE ("BFD_RELOC_386_TLS_TPOFF32");
310       return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
311
312     case BFD_RELOC_SIZE32:
313       TRACE ("BFD_RELOC_SIZE32");
314       return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
315
316     case BFD_RELOC_386_TLS_GOTDESC:
317       TRACE ("BFD_RELOC_386_TLS_GOTDESC");
318       return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
319
320     case BFD_RELOC_386_TLS_DESC_CALL:
321       TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
322       return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
323
324     case BFD_RELOC_386_TLS_DESC:
325       TRACE ("BFD_RELOC_386_TLS_DESC");
326       return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
327
328     case BFD_RELOC_386_IRELATIVE:
329       TRACE ("BFD_RELOC_386_IRELATIVE");
330       return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
331
332     case BFD_RELOC_386_GOT32X:
333       TRACE ("BFD_RELOC_386_GOT32X");
334       return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
335
336     case BFD_RELOC_VTABLE_INHERIT:
337       TRACE ("BFD_RELOC_VTABLE_INHERIT");
338       return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
339
340     case BFD_RELOC_VTABLE_ENTRY:
341       TRACE ("BFD_RELOC_VTABLE_ENTRY");
342       return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
343
344     default:
345       break;
346     }
347
348   TRACE ("Unknown");
349   return 0;
350 }
351
352 static reloc_howto_type *
353 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
354                             const char *r_name)
355 {
356   unsigned int i;
357
358   for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
359     if (elf_howto_table[i].name != NULL
360         && strcasecmp (elf_howto_table[i].name, r_name) == 0)
361       return &elf_howto_table[i];
362
363   return NULL;
364 }
365
366 static reloc_howto_type *
367 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
368 {
369   unsigned int indx;
370
371   if ((indx = r_type) >= R_386_standard
372       && ((indx = r_type - R_386_ext_offset) - R_386_standard
373           >= R_386_ext - R_386_standard)
374       && ((indx = r_type - R_386_tls_offset) - R_386_ext
375           >= R_386_ext2 - R_386_ext)
376       && ((indx = r_type - R_386_vt_offset) - R_386_ext2
377           >= R_386_vt - R_386_ext2))
378     {
379       /* xgettext:c-format */
380       _bfd_error_handler (_("%B: invalid relocation type %d"),
381                           abfd, (int) r_type);
382       indx = R_386_NONE;
383     }
384   /* PR 17512: file: 0f67f69d.  */
385   if (elf_howto_table [indx].type != r_type)
386     return NULL;
387   return &elf_howto_table[indx];
388 }
389
390 static void
391 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
392                             arelent *cache_ptr,
393                             Elf_Internal_Rela *dst)
394 {
395   unsigned int r_type = ELF32_R_TYPE (dst->r_info);
396   cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type);
397 }
398
399 /* Return whether a symbol name implies a local label.  The UnixWare
400    2.1 cc generates temporary symbols that start with .X, so we
401    recognize them here.  FIXME: do other SVR4 compilers also use .X?.
402    If so, we should move the .X recognition into
403    _bfd_elf_is_local_label_name.  */
404
405 static bfd_boolean
406 elf_i386_is_local_label_name (bfd *abfd, const char *name)
407 {
408   if (name[0] == '.' && name[1] == 'X')
409     return TRUE;
410
411   return _bfd_elf_is_local_label_name (abfd, name);
412 }
413 \f
414 /* Support for core dump NOTE sections.  */
415
416 static bfd_boolean
417 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
418 {
419   int offset;
420   size_t size;
421
422   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
423     {
424       int pr_version = bfd_get_32 (abfd, note->descdata);
425
426       if (pr_version != 1)
427         return FALSE;
428
429       /* pr_cursig */
430       elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
431
432       /* pr_pid */
433       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
434
435       /* pr_reg */
436       offset = 28;
437       size = bfd_get_32 (abfd, note->descdata + 8);
438     }
439   else
440     {
441       switch (note->descsz)
442         {
443         default:
444           return FALSE;
445
446         case 144:               /* Linux/i386 */
447           /* pr_cursig */
448           elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
449
450           /* pr_pid */
451           elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
452
453           /* pr_reg */
454           offset = 72;
455           size = 68;
456
457           break;
458         }
459     }
460
461   /* Make a ".reg/999" section.  */
462   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
463                                           size, note->descpos + offset);
464 }
465
466 static bfd_boolean
467 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
468 {
469   if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
470     {
471       int pr_version = bfd_get_32 (abfd, note->descdata);
472
473       if (pr_version != 1)
474         return FALSE;
475
476       elf_tdata (abfd)->core->program
477         = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
478       elf_tdata (abfd)->core->command
479         = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
480     }
481   else
482     {
483       switch (note->descsz)
484         {
485         default:
486           return FALSE;
487
488         case 124:               /* Linux/i386 elf_prpsinfo.  */
489           elf_tdata (abfd)->core->pid
490             = bfd_get_32 (abfd, note->descdata + 12);
491           elf_tdata (abfd)->core->program
492             = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
493           elf_tdata (abfd)->core->command
494             = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
495         }
496     }
497
498   /* Note that for some reason, a spurious space is tacked
499      onto the end of the args in some (at least one anyway)
500      implementations, so strip it off if it exists.  */
501   {
502     char *command = elf_tdata (abfd)->core->command;
503     int n = strlen (command);
504
505     if (0 < n && command[n - 1] == ' ')
506       command[n - 1] = '\0';
507   }
508
509   return TRUE;
510 }
511 \f
512 /* Functions for the i386 ELF linker.
513
514    In order to gain some understanding of code in this file without
515    knowing all the intricate details of the linker, note the
516    following:
517
518    Functions named elf_i386_* are called by external routines, other
519    functions are only called locally.  elf_i386_* functions appear
520    in this file more or less in the order in which they are called
521    from external routines.  eg. elf_i386_check_relocs is called
522    early in the link process, elf_i386_finish_dynamic_sections is
523    one of the last functions.  */
524
525 /* The size in bytes of an entry in the lazy procedure linkage table.  */
526
527 #define LAZY_PLT_ENTRY_SIZE 16
528
529 /* The size in bytes of an entry in the non-lazy procedure linkage
530    table.  */
531
532 #define NON_LAZY_PLT_ENTRY_SIZE 8
533
534 /* The first entry in an absolute lazy procedure linkage table looks
535    like this.  See the SVR4 ABI i386 supplement to see how this works.
536    Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte.  */
537
538 static const bfd_byte elf_i386_lazy_plt0_entry[12] =
539 {
540   0xff, 0x35,   /* pushl contents of address */
541   0, 0, 0, 0,   /* replaced with address of .got + 4.  */
542   0xff, 0x25,   /* jmp indirect */
543   0, 0, 0, 0    /* replaced with address of .got + 8.  */
544 };
545
546 /* Subsequent entries in an absolute lazy procedure linkage table look
547    like this.  */
548
549 static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
550 {
551   0xff, 0x25,   /* jmp indirect */
552   0, 0, 0, 0,   /* replaced with address of this symbol in .got.  */
553   0x68,         /* pushl immediate */
554   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
555   0xe9,         /* jmp relative */
556   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
557 };
558
559 /* The first entry in a PIC lazy procedure linkage table look like
560    this.  Will be padded to LAZY_PLT_ENTRY_SIZE with
561    lazy_plt->plt0_pad_byte.  */
562
563 static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
564 {
565   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx) */
566   0xff, 0xa3, 8, 0, 0, 0        /* jmp *8(%ebx) */
567 };
568
569 /* Subsequent entries in a PIC lazy procedure linkage table look like
570    this.  */
571
572 static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
573 {
574   0xff, 0xa3,   /* jmp *offset(%ebx) */
575   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
576   0x68,         /* pushl immediate */
577   0, 0, 0, 0,   /* replaced with offset into relocation table.  */
578   0xe9,         /* jmp relative */
579   0, 0, 0, 0    /* replaced with offset to start of .plt.  */
580 };
581
582 /* Entries in the non-lazy procedure linkage table look like this.  */
583
584 static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
585 {
586   0xff, 0x25,   /* jmp indirect */
587   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
588   0x66, 0x90    /* xchg %ax,%ax  */
589 };
590
591 /* Entries in the PIC non-lazy procedure linkage table look like
592    this.  */
593
594 static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
595 {
596   0xff, 0xa3,   /* jmp *offset(%ebx)  */
597   0, 0, 0, 0,   /* replaced with offset of this symbol in .got.  */
598   0x66, 0x90    /* xchg %ax,%ax  */
599 };
600
601 /* The first entry in an absolute IBT-enabled lazy procedure linkage
602    table looks like this.  */
603
604 static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
605 {
606   0xff, 0x35, 0, 0, 0, 0,       /* pushl GOT[1]       */
607   0xff, 0x25, 0, 0, 0, 0,       /* jmp *GOT[2]        */
608   0x0f, 0x1f, 0x40, 0x00        /* nopl 0(%rax)       */
609 };
610
611 /* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
612    table look like this.  Subsequent entries for a PIC IBT-enabled lazy
613    procedure linkage table are the same.  */
614
615 static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
616 {
617   0xf3, 0x0f, 0x1e, 0xfb,       /* endbr32                    */
618   0x68, 0, 0, 0, 0,             /* pushl immediate            */
619   0xe9, 0, 0, 0, 0,             /* jmp relative               */
620   0x66, 0x90                    /* xchg %ax,%ax               */
621 };
622
623 /* The first entry in a PIC IBT-enabled lazy procedure linkage table
624    look like.  */
625
626 static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
627 {
628   0xff, 0xb3, 4, 0, 0, 0,       /* pushl 4(%ebx)      */
629   0xff, 0xa3, 8, 0, 0, 0,       /* jmp *8(%ebx)       */
630   0x0f, 0x1f, 0x40, 0x00        /* nopl 0(%rax)       */
631 };
632
633 /* Entries for branches with IBT-enabled in the absolute non-lazey
634    procedure linkage table look like this.  They have the same size
635    as the lazy PLT entry.  */
636
637 static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
638 {
639   0xf3, 0x0f, 0x1e, 0xfb,            /* endbr32               */
640   0xff, 0x25, 0, 0, 0, 0,            /* jmp *name@GOT         */
641   0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
642 };
643
644 /* Entries for branches with IBT-enabled in the PIC non-lazey procedure
645    linkage table look like this.  They have the same size as the lazy
646    PLT entry.  */
647
648 static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
649 {
650   0xf3, 0x0f, 0x1e, 0xfb,            /* endbr32               */
651   0xff, 0xa3, 0, 0, 0, 0,            /* jmp *name@GOT(%ebx)   */
652   0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
653 };
654
655 /* .eh_frame covering the lazy .plt section.  */
656
657 static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
658 {
659   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
660   0, 0, 0, 0,                   /* CIE ID */
661   1,                            /* CIE version */
662   'z', 'R', 0,                  /* Augmentation string */
663   1,                            /* Code alignment factor */
664   0x7c,                         /* Data alignment factor */
665   8,                            /* Return address column */
666   1,                            /* Augmentation size */
667   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
668   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
669   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
670   DW_CFA_nop, DW_CFA_nop,
671
672   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
673   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
674   0, 0, 0, 0,                   /* R_386_PC32 .plt goes here */
675   0, 0, 0, 0,                   /* .plt size goes here */
676   0,                            /* Augmentation size */
677   DW_CFA_def_cfa_offset, 8,     /* DW_CFA_def_cfa_offset: 8 */
678   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
679   DW_CFA_def_cfa_offset, 12,    /* DW_CFA_def_cfa_offset: 12 */
680   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
681   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
682   11,                           /* Block length */
683   DW_OP_breg4, 4,               /* DW_OP_breg4 (esp): 4 */
684   DW_OP_breg8, 0,               /* DW_OP_breg8 (eip): 0 */
685   DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
686   DW_OP_lit2, DW_OP_shl, DW_OP_plus,
687   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
688 };
689
690 /* .eh_frame covering the lazy .plt section with IBT-enabled.  */
691
692 static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
693 {
694   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
695   0, 0, 0, 0,                   /* CIE ID */
696   1,                            /* CIE version */
697   'z', 'R', 0,                  /* Augmentation string */
698   1,                            /* Code alignment factor */
699   0x7c,                         /* Data alignment factor */
700   8,                            /* Return address column */
701   1,                            /* Augmentation size */
702   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
703   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
704   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
705   DW_CFA_nop, DW_CFA_nop,
706
707   PLT_FDE_LENGTH, 0, 0, 0,      /* FDE length */
708   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
709   0, 0, 0, 0,                   /* R_386_PC32 .plt goes here */
710   0, 0, 0, 0,                   /* .plt size goes here */
711   0,                            /* Augmentation size */
712   DW_CFA_def_cfa_offset, 8,     /* DW_CFA_def_cfa_offset: 8 */
713   DW_CFA_advance_loc + 6,       /* DW_CFA_advance_loc: 6 to __PLT__+6 */
714   DW_CFA_def_cfa_offset, 12,    /* DW_CFA_def_cfa_offset: 12 */
715   DW_CFA_advance_loc + 10,      /* DW_CFA_advance_loc: 10 to __PLT__+16 */
716   DW_CFA_def_cfa_expression,    /* DW_CFA_def_cfa_expression */
717   11,                           /* Block length */
718   DW_OP_breg4, 4,               /* DW_OP_breg4 (esp): 4 */
719   DW_OP_breg8, 0,               /* DW_OP_breg8 (eip): 0 */
720   DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
721   DW_OP_lit2, DW_OP_shl, DW_OP_plus,
722   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
723 };
724
725 /* .eh_frame covering the non-lazy .plt section.  */
726
727 static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
728 {
729 #define PLT_GOT_FDE_LENGTH              16
730   PLT_CIE_LENGTH, 0, 0, 0,      /* CIE length */
731   0, 0, 0, 0,                   /* CIE ID */
732   1,                            /* CIE version */
733   'z', 'R', 0,                  /* Augmentation string */
734   1,                            /* Code alignment factor */
735   0x7c,                         /* Data alignment factor */
736   8,                            /* Return address column */
737   1,                            /* Augmentation size */
738   DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
739   DW_CFA_def_cfa, 4, 4,         /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
740   DW_CFA_offset + 8, 1,         /* DW_CFA_offset: r8 (eip) at cfa-4 */
741   DW_CFA_nop, DW_CFA_nop,
742
743   PLT_GOT_FDE_LENGTH, 0, 0, 0,  /* FDE length */
744   PLT_CIE_LENGTH + 8, 0, 0, 0,  /* CIE pointer */
745   0, 0, 0, 0,                   /* the start of non-lazy .plt goes here */
746   0, 0, 0, 0,                   /* non-lazy .plt size goes here */
747   0,                            /* Augmentation size */
748   DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
749 };
750
751 /* These are the standard parameters.  */
752 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
753   {
754     elf_i386_lazy_plt0_entry,           /* plt0_entry */
755     sizeof (elf_i386_lazy_plt0_entry),  /* plt0_entry_size */
756     elf_i386_lazy_plt_entry,            /* plt_entry */
757     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
758     2,                                  /* plt0_got1_offset */
759     8,                                  /* plt0_got2_offset */
760     0,                                  /* plt0_got2_insn_end */
761     2,                                  /* plt_got_offset */
762     7,                                  /* plt_reloc_offset */
763     12,                                 /* plt_plt_offset */
764     0,                                  /* plt_got_insn_size */
765     0,                                  /* plt_plt_insn_end */
766     6,                                  /* plt_lazy_offset */
767     elf_i386_pic_lazy_plt0_entry,       /* pic_plt0_entry */
768     elf_i386_pic_lazy_plt_entry,        /* pic_plt_entry */
769     elf_i386_eh_frame_lazy_plt,         /* eh_frame_plt */
770     sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
771   };
772
773 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
774   {
775     elf_i386_non_lazy_plt_entry,        /* plt_entry */
776     elf_i386_pic_non_lazy_plt_entry,    /* pic_plt_entry */
777     NON_LAZY_PLT_ENTRY_SIZE,            /* plt_entry_size */
778     2,                                  /* plt_got_offset */
779     0,                                  /* plt_got_insn_size */
780     elf_i386_eh_frame_non_lazy_plt,     /* eh_frame_plt */
781     sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
782   };
783
784 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
785   {
786     elf_i386_lazy_ibt_plt0_entry,       /* plt0_entry */
787     sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
788     elf_i386_lazy_ibt_plt_entry,        /* plt_entry */
789     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
790     2,                                  /* plt0_got1_offset */
791     8,                                  /* plt0_got2_offset */
792     0,                                  /* plt0_got2_insn_end */
793     4+2,                                /* plt_got_offset */
794     4+1,                                /* plt_reloc_offset */
795     4+6,                                /* plt_plt_offset */
796     0,                                  /* plt_got_insn_size */
797     0,                                  /* plt_plt_insn_end */
798     0,                                  /* plt_lazy_offset */
799     elf_i386_pic_lazy_ibt_plt0_entry,   /* pic_plt0_entry */
800     elf_i386_lazy_ibt_plt_entry,        /* pic_plt_entry */
801     elf_i386_eh_frame_lazy_ibt_plt,     /* eh_frame_plt */
802     sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
803   };
804
805 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
806   {
807     elf_i386_non_lazy_ibt_plt_entry,    /* plt_entry */
808     elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
809     LAZY_PLT_ENTRY_SIZE,                /* plt_entry_size */
810     4+2,                                /* plt_got_offset */
811     0,                                  /* plt_got_insn_size */
812     elf_i386_eh_frame_non_lazy_plt,     /* eh_frame_plt */
813     sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
814   };
815 \f
816
817 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
818    for the PLTResolve stub and then for each PLT entry.  */
819 #define PLTRESOLVE_RELOCS_SHLIB 0
820 #define PLTRESOLVE_RELOCS 2
821 #define PLT_NON_JUMP_SLOT_RELOCS 2
822
823 /* Architecture-specific backend data for i386.  */
824
825 struct elf_i386_backend_data
826 {
827   /* Value used to fill the unused bytes of the first PLT entry.  */
828   bfd_byte plt0_pad_byte;
829
830   /* Target system.  */
831   enum
832     {
833       is_normal,
834       is_vxworks,
835       is_nacl
836     } os;
837 };
838
839 #define get_elf_i386_backend_data(abfd) \
840   ((const struct elf_i386_backend_data *) \
841    get_elf_backend_data (abfd)->arch_data)
842
843 /* These are the standard parameters.  */
844 static const struct elf_i386_backend_data elf_i386_arch_bed =
845   {
846     0,                                  /* plt0_pad_byte */
847     is_normal                           /* os */
848   };
849
850 #define elf_backend_arch_data   &elf_i386_arch_bed
851
852 /* Return TRUE if the TLS access code sequence support transition
853    from R_TYPE.  */
854
855 static bfd_boolean
856 elf_i386_check_tls_transition (asection *sec,
857                                bfd_byte *contents,
858                                Elf_Internal_Shdr *symtab_hdr,
859                                struct elf_link_hash_entry **sym_hashes,
860                                unsigned int r_type,
861                                const Elf_Internal_Rela *rel,
862                                const Elf_Internal_Rela *relend)
863 {
864   unsigned int val, type, reg;
865   unsigned long r_symndx;
866   struct elf_link_hash_entry *h;
867   bfd_vma offset;
868   bfd_byte *call;
869   bfd_boolean indirect_call;
870
871   offset = rel->r_offset;
872   switch (r_type)
873     {
874     case R_386_TLS_GD:
875     case R_386_TLS_LDM:
876       if (offset < 2 || (rel + 1) >= relend)
877         return FALSE;
878
879       indirect_call = FALSE;
880       call = contents + offset + 4;
881       val = *(call - 5);
882       type = *(call - 6);
883       if (r_type == R_386_TLS_GD)
884         {
885           /* Check transition from GD access model.  Only
886                 leal foo@tlsgd(,%ebx,1), %eax
887                 call ___tls_get_addr@PLT
888              or
889                 leal foo@tlsgd(%ebx) %eax
890                 call ___tls_get_addr@PLT
891                 nop
892              or
893                 leal foo@tlsgd(%reg), %eax
894                 call *___tls_get_addr@GOT(%reg)
895                 which may be converted to
896                 addr32 call ___tls_get_addr
897              can transit to different access model.  */
898           if ((offset + 10) > sec->size
899               || (type != 0x8d && type != 0x04))
900             return FALSE;
901
902           if (type == 0x04)
903             {
904               /* leal foo@tlsgd(,%ebx,1), %eax
905                  call ___tls_get_addr@PLT  */
906               if (offset < 3)
907                 return FALSE;
908
909               if (*(call - 7) != 0x8d
910                   || val != 0x1d
911                   || call[0] != 0xe8)
912                 return FALSE;
913             }
914           else
915             {
916               /* This must be
917                         leal foo@tlsgd(%ebx), %eax
918                         call ___tls_get_addr@PLT
919                         nop
920                  or
921                         leal foo@tlsgd(%reg), %eax
922                         call *___tls_get_addr@GOT(%reg)
923                         which may be converted to
924                         addr32 call ___tls_get_addr
925
926                  %eax can't be used as the GOT base register since it
927                  is used to pass parameter to ___tls_get_addr.  */
928               reg = val & 7;
929               if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
930                 return FALSE;
931
932               indirect_call = call[0] == 0xff;
933               if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
934                   && !(call[0] == 0x67 && call[1] == 0xe8)
935                   && !(indirect_call
936                        && (call[1] & 0xf8) == 0x90
937                        && (call[1] & 0x7) == reg))
938                 return FALSE;
939             }
940         }
941       else
942         {
943           /* Check transition from LD access model.  Only
944                 leal foo@tlsldm(%ebx), %eax
945                 call ___tls_get_addr@PLT
946              or
947                 leal foo@tlsldm(%reg), %eax
948                 call *___tls_get_addr@GOT(%reg)
949                 which may be converted to
950                 addr32 call ___tls_get_addr
951              can transit to different access model.  */
952           if (type != 0x8d || (offset + 9) > sec->size)
953             return FALSE;
954
955           /* %eax can't be used as the GOT base register since it is
956              used to pass parameter to ___tls_get_addr.  */
957           reg = val & 7;
958           if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
959             return FALSE;
960
961           indirect_call = call[0] == 0xff;
962           if (!(reg == 3 && call[0] == 0xe8)
963               && !(call[0] == 0x67 && call[1] == 0xe8)
964               && !(indirect_call
965                    && (call[1] & 0xf8) == 0x90
966                    && (call[1] & 0x7) == reg))
967             return FALSE;
968         }
969
970       r_symndx = ELF32_R_SYM (rel[1].r_info);
971       if (r_symndx < symtab_hdr->sh_info)
972         return FALSE;
973
974       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
975       if (h == NULL
976           || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
977         return FALSE;
978       else if (indirect_call)
979         return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
980       else
981         return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
982                 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
983
984     case R_386_TLS_IE:
985       /* Check transition from IE access model:
986                 movl foo@indntpoff(%rip), %eax
987                 movl foo@indntpoff(%rip), %reg
988                 addl foo@indntpoff(%rip), %reg
989        */
990
991       if (offset < 1 || (offset + 4) > sec->size)
992         return FALSE;
993
994       /* Check "movl foo@tpoff(%rip), %eax" first.  */
995       val = bfd_get_8 (abfd, contents + offset - 1);
996       if (val == 0xa1)
997         return TRUE;
998
999       if (offset < 2)
1000         return FALSE;
1001
1002       /* Check movl|addl foo@tpoff(%rip), %reg.   */
1003       type = bfd_get_8 (abfd, contents + offset - 2);
1004       return ((type == 0x8b || type == 0x03)
1005               && (val & 0xc7) == 0x05);
1006
1007     case R_386_TLS_GOTIE:
1008     case R_386_TLS_IE_32:
1009       /* Check transition from {IE_32,GOTIE} access model:
1010                 subl foo@{tpoff,gontoff}(%reg1), %reg2
1011                 movl foo@{tpoff,gontoff}(%reg1), %reg2
1012                 addl foo@{tpoff,gontoff}(%reg1), %reg2
1013        */
1014
1015       if (offset < 2 || (offset + 4) > sec->size)
1016         return FALSE;
1017
1018       val = bfd_get_8 (abfd, contents + offset - 1);
1019       if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1020         return FALSE;
1021
1022       type = bfd_get_8 (abfd, contents + offset - 2);
1023       return type == 0x8b || type == 0x2b || type == 0x03;
1024
1025     case R_386_TLS_GOTDESC:
1026       /* Check transition from GDesc access model:
1027                 leal x@tlsdesc(%ebx), %eax
1028
1029          Make sure it's a leal adding ebx to a 32-bit offset
1030          into any register, although it's probably almost always
1031          going to be eax.  */
1032
1033       if (offset < 2 || (offset + 4) > sec->size)
1034         return FALSE;
1035
1036       if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1037         return FALSE;
1038
1039       val = bfd_get_8 (abfd, contents + offset - 1);
1040       return (val & 0xc7) == 0x83;
1041
1042     case R_386_TLS_DESC_CALL:
1043       /* Check transition from GDesc access model:
1044                 call *x@tlsdesc(%eax)
1045        */
1046       if (offset + 2 <= sec->size)
1047         {
1048           /* Make sure that it's a call *x@tlsdesc(%eax).  */
1049           call = contents + offset;
1050           return call[0] == 0xff && call[1] == 0x10;
1051         }
1052
1053       return FALSE;
1054
1055     default:
1056       abort ();
1057     }
1058 }
1059
1060 /* Return TRUE if the TLS access transition is OK or no transition
1061    will be performed.  Update R_TYPE if there is a transition.  */
1062
1063 static bfd_boolean
1064 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1065                          asection *sec, bfd_byte *contents,
1066                          Elf_Internal_Shdr *symtab_hdr,
1067                          struct elf_link_hash_entry **sym_hashes,
1068                          unsigned int *r_type, int tls_type,
1069                          const Elf_Internal_Rela *rel,
1070                          const Elf_Internal_Rela *relend,
1071                          struct elf_link_hash_entry *h,
1072                          unsigned long r_symndx,
1073                          bfd_boolean from_relocate_section)
1074 {
1075   unsigned int from_type = *r_type;
1076   unsigned int to_type = from_type;
1077   bfd_boolean check = TRUE;
1078
1079   /* Skip TLS transition for functions.  */
1080   if (h != NULL
1081       && (h->type == STT_FUNC
1082           || h->type == STT_GNU_IFUNC))
1083     return TRUE;
1084
1085   switch (from_type)
1086     {
1087     case R_386_TLS_GD:
1088     case R_386_TLS_GOTDESC:
1089     case R_386_TLS_DESC_CALL:
1090     case R_386_TLS_IE_32:
1091     case R_386_TLS_IE:
1092     case R_386_TLS_GOTIE:
1093       if (bfd_link_executable (info))
1094         {
1095           if (h == NULL)
1096             to_type = R_386_TLS_LE_32;
1097           else if (from_type != R_386_TLS_IE
1098                    && from_type != R_386_TLS_GOTIE)
1099             to_type = R_386_TLS_IE_32;
1100         }
1101
1102       /* When we are called from elf_i386_relocate_section, there may
1103          be additional transitions based on TLS_TYPE.  */
1104       if (from_relocate_section)
1105         {
1106           unsigned int new_to_type = to_type;
1107
1108           if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1109             new_to_type = R_386_TLS_LE_32;
1110
1111           if (to_type == R_386_TLS_GD
1112               || to_type == R_386_TLS_GOTDESC
1113               || to_type == R_386_TLS_DESC_CALL)
1114             {
1115               if (tls_type == GOT_TLS_IE_POS)
1116                 new_to_type = R_386_TLS_GOTIE;
1117               else if (tls_type & GOT_TLS_IE)
1118                 new_to_type = R_386_TLS_IE_32;
1119             }
1120
1121           /* We checked the transition before when we were called from
1122              elf_i386_check_relocs.  We only want to check the new
1123              transition which hasn't been checked before.  */
1124           check = new_to_type != to_type && from_type == to_type;
1125           to_type = new_to_type;
1126         }
1127
1128       break;
1129
1130     case R_386_TLS_LDM:
1131       if (bfd_link_executable (info))
1132         to_type = R_386_TLS_LE_32;
1133       break;
1134
1135     default:
1136       return TRUE;
1137     }
1138
1139   /* Return TRUE if there is no transition.  */
1140   if (from_type == to_type)
1141     return TRUE;
1142
1143   /* Check if the transition can be performed.  */
1144   if (check
1145       && ! elf_i386_check_tls_transition (sec, contents,
1146                                           symtab_hdr, sym_hashes,
1147                                           from_type, rel, relend))
1148     {
1149       reloc_howto_type *from, *to;
1150       const char *name;
1151
1152       from = elf_i386_rtype_to_howto (abfd, from_type);
1153       to = elf_i386_rtype_to_howto (abfd, to_type);
1154
1155       if (h)
1156         name = h->root.root.string;
1157       else
1158         {
1159           struct elf_x86_link_hash_table *htab;
1160
1161           htab = elf_x86_hash_table (info, I386_ELF_DATA);
1162           if (htab == NULL)
1163             name = "*unknown*";
1164           else
1165             {
1166               Elf_Internal_Sym *isym;
1167
1168               isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1169                                             abfd, r_symndx);
1170               name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1171             }
1172         }
1173
1174       _bfd_error_handler
1175         /* xgettext:c-format */
1176         (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
1177            "in section `%A' failed"),
1178          abfd, from->name, to->name, name,
1179          rel->r_offset, sec);
1180       bfd_set_error (bfd_error_bad_value);
1181       return FALSE;
1182     }
1183
1184   *r_type = to_type;
1185   return TRUE;
1186 }
1187
1188 /* With the local symbol, foo, we convert
1189    mov foo@GOT[(%reg1)], %reg2
1190    to
1191    lea foo[@GOTOFF(%reg1)], %reg2
1192    and convert
1193    call/jmp *foo@GOT[(%reg)]
1194    to
1195    nop call foo/jmp foo nop
1196    When PIC is false, convert
1197    test %reg1, foo@GOT[(%reg2)]
1198    to
1199    test $foo, %reg1
1200    and convert
1201    binop foo@GOT[(%reg1)], %reg2
1202    to
1203    binop $foo, %reg2
1204    where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1205    instructions.  */
1206
1207 static
1208 bfd_boolean
1209 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1210                              bfd_byte *contents,
1211                              unsigned int *r_type_p,
1212                              Elf_Internal_Rela *irel,
1213                              struct elf_link_hash_entry *h,
1214                              bfd_boolean *converted,
1215                              struct bfd_link_info *link_info)
1216 {
1217   struct elf_x86_link_hash_table *htab;
1218   unsigned int opcode;
1219   unsigned int modrm;
1220   bfd_boolean baseless;
1221   Elf_Internal_Sym *isym;
1222   unsigned int addend;
1223   unsigned int nop;
1224   bfd_vma nop_offset;
1225   bfd_boolean is_pic;
1226   bfd_boolean to_reloc_32;
1227   unsigned int r_type;
1228   unsigned int r_symndx;
1229   bfd_vma roff = irel->r_offset;
1230   bfd_boolean local_ref;
1231   struct elf_x86_link_hash_entry *eh;
1232
1233   if (roff < 2)
1234     return TRUE;
1235
1236   /* Addend for R_386_GOT32X relocations must be 0.  */
1237   addend = bfd_get_32 (abfd, contents + roff);
1238   if (addend != 0)
1239     return TRUE;
1240
1241   htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1242   is_pic = bfd_link_pic (link_info);
1243
1244   r_type = *r_type_p;
1245   r_symndx = ELF32_R_SYM (irel->r_info);
1246
1247   modrm = bfd_get_8 (abfd, contents + roff - 1);
1248   baseless = (modrm & 0xc7) == 0x5;
1249
1250   if (baseless && is_pic)
1251     {
1252       /* For PIC, disallow R_386_GOT32X without a base register
1253          since we don't know what the GOT base is.  */
1254       const char *name;
1255
1256       if (h == NULL)
1257         {
1258           isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1259                                         r_symndx);
1260           name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1261         }
1262       else
1263         name = h->root.root.string;
1264
1265       _bfd_error_handler
1266         /* xgettext:c-format */
1267         (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1268            " register can not be used when making a shared object"),
1269          abfd, name);
1270       return FALSE;
1271     }
1272
1273   opcode = bfd_get_8 (abfd, contents + roff - 2);
1274
1275   /* Convert to R_386_32 if PIC is false or there is no base
1276      register.  */
1277   to_reloc_32 = !is_pic || baseless;
1278
1279   eh = elf_x86_hash_entry (h);
1280
1281   /* Try to convert R_386_GOT32X.  Get the symbol referred to by the
1282      reloc.  */
1283   if (h == NULL)
1284     {
1285       if (opcode == 0x0ff)
1286         /* Convert "call/jmp *foo@GOT[(%reg)]".  */
1287         goto convert_branch;
1288       else
1289         /* Convert "mov foo@GOT[(%reg1)], %reg2",
1290            "test %reg1, foo@GOT(%reg2)" and
1291            "binop foo@GOT[(%reg1)], %reg2". */
1292         goto convert_load;
1293     }
1294
1295   /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P.  */
1296   local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1297
1298   /* Undefined weak symbol is only bound locally in executable
1299      and its reference is resolved as 0.  */
1300   if (h->root.type == bfd_link_hash_undefweak
1301       && !eh->linker_def
1302       && local_ref)
1303     {
1304       if (opcode == 0xff)
1305         {
1306           /* No direct branch to 0 for PIC.  */
1307           if (is_pic)
1308             return TRUE;
1309           else
1310             goto convert_branch;
1311         }
1312       else
1313         {
1314           /* We can convert load of address 0 to R_386_32.  */
1315           to_reloc_32 = TRUE;
1316           goto convert_load;
1317         }
1318     }
1319
1320   if (opcode == 0xff)
1321     {
1322       /* We have "call/jmp *foo@GOT[(%reg)]".  */
1323       if ((h->root.type == bfd_link_hash_defined
1324            || h->root.type == bfd_link_hash_defweak)
1325           && local_ref)
1326         {
1327           /* The function is locally defined.   */
1328 convert_branch:
1329           /* Convert R_386_GOT32X to R_386_PC32.  */
1330           if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1331             {
1332               /* Convert to "nop call foo".  ADDR_PREFIX_OPCODE
1333                  is a nop prefix.  */
1334               modrm = 0xe8;
1335               /* To support TLS optimization, always use addr32 prefix
1336                  for "call *___tls_get_addr@GOT(%reg)".  */
1337               if (eh && eh->tls_get_addr)
1338                 {
1339                   nop = 0x67;
1340                   nop_offset = irel->r_offset - 2;
1341                 }
1342               else
1343                 {
1344                   nop = link_info->call_nop_byte;
1345                   if (link_info->call_nop_as_suffix)
1346                     {
1347                       nop_offset = roff + 3;
1348                       irel->r_offset -= 1;
1349                     }
1350                   else
1351                     nop_offset = roff - 2;
1352                 }
1353             }
1354           else
1355             {
1356               /* Convert to "jmp foo nop".  */
1357               modrm = 0xe9;
1358               nop = NOP_OPCODE;
1359               nop_offset = roff + 3;
1360               irel->r_offset -= 1;
1361             }
1362
1363           bfd_put_8 (abfd, nop, contents + nop_offset);
1364           bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1365           /* When converting to PC-relative relocation, we
1366              need to adjust addend by -4.  */
1367           bfd_put_32 (abfd, -4, contents + irel->r_offset);
1368           irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1369           *r_type_p = R_386_PC32;
1370           *converted = TRUE;
1371         }
1372     }
1373   else
1374     {
1375       /* We have "mov foo@GOT[(%re1g)], %reg2",
1376          "test %reg1, foo@GOT(%reg2)" and
1377          "binop foo@GOT[(%reg1)], %reg2".
1378
1379          Avoid optimizing _DYNAMIC since ld.so may use its
1380          link-time address.  */
1381       if (h == htab->elf.hdynamic)
1382         return TRUE;
1383
1384       /* def_regular is set by an assignment in a linker script in
1385          bfd_elf_record_link_assignment.  start_stop is set on
1386          __start_SECNAME/__stop_SECNAME which mark section SECNAME.  */
1387       if (h->start_stop
1388           || eh->linker_def
1389           || ((h->def_regular
1390                || h->root.type == bfd_link_hash_defined
1391                || h->root.type == bfd_link_hash_defweak)
1392               && local_ref))
1393         {
1394 convert_load:
1395           if (opcode == 0x8b)
1396             {
1397               if (to_reloc_32)
1398                 {
1399                   /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1400                      "mov $foo, %reg2" with R_386_32.  */
1401                   r_type = R_386_32;
1402                   modrm = 0xc0 | (modrm & 0x38) >> 3;
1403                   bfd_put_8 (abfd, modrm, contents + roff - 1);
1404                   opcode = 0xc7;
1405                 }
1406               else
1407                 {
1408                   /* Convert "mov foo@GOT(%reg1), %reg2" to
1409                      "lea foo@GOTOFF(%reg1), %reg2".  */
1410                   r_type = R_386_GOTOFF;
1411                   opcode = 0x8d;
1412                 }
1413             }
1414           else
1415             {
1416               /* Only R_386_32 is supported.  */
1417               if (!to_reloc_32)
1418                 return TRUE;
1419
1420               if (opcode == 0x85)
1421                 {
1422                   /* Convert "test %reg1, foo@GOT(%reg2)" to
1423                      "test $foo, %reg1".  */
1424                   modrm = 0xc0 | (modrm & 0x38) >> 3;
1425                   opcode = 0xf7;
1426                 }
1427               else
1428                 {
1429                   /* Convert "binop foo@GOT(%reg1), %reg2" to
1430                      "binop $foo, %reg2".  */
1431                   modrm = (0xc0
1432                            | (modrm & 0x38) >> 3
1433                            | (opcode & 0x3c));
1434                   opcode = 0x81;
1435                 }
1436               bfd_put_8 (abfd, modrm, contents + roff - 1);
1437               r_type = R_386_32;
1438             }
1439
1440           bfd_put_8 (abfd, opcode, contents + roff - 2);
1441           irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1442           *r_type_p = r_type;
1443           *converted = TRUE;
1444         }
1445     }
1446
1447   return TRUE;
1448 }
1449
1450 /* Rename some of the generic section flags to better document how they
1451    are used here.  */
1452 #define check_relocs_failed     sec_flg0
1453
1454 /* Look through the relocs for a section during the first phase, and
1455    calculate needed space in the global offset table, procedure linkage
1456    table, and dynamic reloc sections.  */
1457
1458 static bfd_boolean
1459 elf_i386_check_relocs (bfd *abfd,
1460                        struct bfd_link_info *info,
1461                        asection *sec,
1462                        const Elf_Internal_Rela *relocs)
1463 {
1464   struct elf_x86_link_hash_table *htab;
1465   Elf_Internal_Shdr *symtab_hdr;
1466   struct elf_link_hash_entry **sym_hashes;
1467   const Elf_Internal_Rela *rel;
1468   const Elf_Internal_Rela *rel_end;
1469   asection *sreloc;
1470   bfd_byte *contents;
1471   bfd_boolean converted;
1472
1473   if (bfd_link_relocatable (info))
1474     return TRUE;
1475
1476   /* Don't do anything special with non-loaded, non-alloced sections.
1477      In particular, any relocs in such sections should not affect GOT
1478      and PLT reference counting (ie. we don't allow them to create GOT
1479      or PLT entries), there's no possibility or desire to optimize TLS
1480      relocs, and there's not much point in propagating relocs to shared
1481      libs that the dynamic linker won't relocate.  */
1482   if ((sec->flags & SEC_ALLOC) == 0)
1483     return TRUE;
1484
1485   htab = elf_x86_hash_table (info, I386_ELF_DATA);
1486   if (htab == NULL)
1487     {
1488       sec->check_relocs_failed = 1;
1489       return FALSE;
1490     }
1491
1492   BFD_ASSERT (is_x86_elf (abfd, htab));
1493
1494   /* Get the section contents.  */
1495   if (elf_section_data (sec)->this_hdr.contents != NULL)
1496     contents = elf_section_data (sec)->this_hdr.contents;
1497   else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1498     {
1499       sec->check_relocs_failed = 1;
1500       return FALSE;
1501     }
1502
1503   symtab_hdr = &elf_symtab_hdr (abfd);
1504   sym_hashes = elf_sym_hashes (abfd);
1505
1506   converted = FALSE;
1507
1508   sreloc = NULL;
1509
1510   rel_end = relocs + sec->reloc_count;
1511   for (rel = relocs; rel < rel_end; rel++)
1512     {
1513       unsigned int r_type;
1514       unsigned int r_symndx;
1515       struct elf_link_hash_entry *h;
1516       struct elf_x86_link_hash_entry *eh;
1517       Elf_Internal_Sym *isym;
1518       const char *name;
1519       bfd_boolean size_reloc;
1520
1521       r_symndx = ELF32_R_SYM (rel->r_info);
1522       r_type = ELF32_R_TYPE (rel->r_info);
1523
1524       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1525         {
1526           /* xgettext:c-format */
1527           _bfd_error_handler (_("%B: bad symbol index: %d"),
1528                               abfd, r_symndx);
1529           goto error_return;
1530         }
1531
1532       if (r_symndx < symtab_hdr->sh_info)
1533         {
1534           /* A local symbol.  */
1535           isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1536                                         abfd, r_symndx);
1537           if (isym == NULL)
1538             goto error_return;
1539
1540           /* Check relocation against local STT_GNU_IFUNC symbol.  */
1541           if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1542             {
1543               h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, TRUE);
1544               if (h == NULL)
1545                 goto error_return;
1546
1547               /* Fake a STT_GNU_IFUNC symbol.  */
1548               h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1549                                                       isym, NULL);
1550               h->type = STT_GNU_IFUNC;
1551               h->def_regular = 1;
1552               h->ref_regular = 1;
1553               h->forced_local = 1;
1554               h->root.type = bfd_link_hash_defined;
1555             }
1556           else
1557             h = NULL;
1558         }
1559       else
1560         {
1561           isym = NULL;
1562           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1563           while (h->root.type == bfd_link_hash_indirect
1564                  || h->root.type == bfd_link_hash_warning)
1565             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1566         }
1567
1568       eh = (struct elf_x86_link_hash_entry *) h;
1569       if (h != NULL)
1570         {
1571           if (r_type == R_386_GOTOFF)
1572             eh->gotoff_ref = 1;
1573
1574           /* It is referenced by a non-shared object. */
1575           h->ref_regular = 1;
1576           h->root.non_ir_ref_regular = 1;
1577
1578           if (h->type == STT_GNU_IFUNC)
1579             elf_tdata (info->output_bfd)->has_gnu_symbols
1580               |= elf_gnu_symbol_ifunc;
1581         }
1582
1583       if (r_type == R_386_GOT32X
1584           && (h == NULL || h->type != STT_GNU_IFUNC))
1585         {
1586           Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1587           if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1588                                             &r_type, irel, h,
1589                                             &converted, info))
1590             goto error_return;
1591         }
1592
1593       if (! elf_i386_tls_transition (info, abfd, sec, contents,
1594                                      symtab_hdr, sym_hashes,
1595                                      &r_type, GOT_UNKNOWN,
1596                                      rel, rel_end, h, r_symndx, FALSE))
1597         goto error_return;
1598
1599       switch (r_type)
1600         {
1601         case R_386_TLS_LDM:
1602           htab->tls_ld_or_ldm_got.refcount += 1;
1603           goto create_got;
1604
1605         case R_386_PLT32:
1606           /* This symbol requires a procedure linkage table entry.  We
1607              actually build the entry in adjust_dynamic_symbol,
1608              because this might be a case of linking PIC code which is
1609              never referenced by a dynamic object, in which case we
1610              don't need to generate a procedure linkage table entry
1611              after all.  */
1612
1613           /* If this is a local symbol, we resolve it directly without
1614              creating a procedure linkage table entry.  */
1615           if (h == NULL)
1616             continue;
1617
1618           eh->has_got_reloc = 1;
1619           h->needs_plt = 1;
1620           h->plt.refcount += 1;
1621           break;
1622
1623         case R_386_SIZE32:
1624           size_reloc = TRUE;
1625           goto do_size;
1626
1627         case R_386_TLS_IE_32:
1628         case R_386_TLS_IE:
1629         case R_386_TLS_GOTIE:
1630           if (!bfd_link_executable (info))
1631             info->flags |= DF_STATIC_TLS;
1632           /* Fall through */
1633
1634         case R_386_GOT32:
1635         case R_386_GOT32X:
1636         case R_386_TLS_GD:
1637         case R_386_TLS_GOTDESC:
1638         case R_386_TLS_DESC_CALL:
1639           /* This symbol requires a global offset table entry.  */
1640           {
1641             int tls_type, old_tls_type;
1642
1643             switch (r_type)
1644               {
1645               default:
1646               case R_386_GOT32:
1647               case R_386_GOT32X:
1648                 tls_type = GOT_NORMAL;
1649                 break;
1650               case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1651               case R_386_TLS_GOTDESC:
1652               case R_386_TLS_DESC_CALL:
1653                 tls_type = GOT_TLS_GDESC; break;
1654               case R_386_TLS_IE_32:
1655                 if (ELF32_R_TYPE (rel->r_info) == r_type)
1656                   tls_type = GOT_TLS_IE_NEG;
1657                 else
1658                   /* If this is a GD->IE transition, we may use either of
1659                      R_386_TLS_TPOFF and R_386_TLS_TPOFF32.  */
1660                   tls_type = GOT_TLS_IE;
1661                 break;
1662               case R_386_TLS_IE:
1663               case R_386_TLS_GOTIE:
1664                 tls_type = GOT_TLS_IE_POS; break;
1665               }
1666
1667             if (h != NULL)
1668               {
1669                 h->got.refcount += 1;
1670                 old_tls_type = elf_x86_hash_entry (h)->tls_type;
1671               }
1672             else
1673               {
1674                 bfd_signed_vma *local_got_refcounts;
1675
1676                 /* This is a global offset table entry for a local symbol.  */
1677                 local_got_refcounts = elf_local_got_refcounts (abfd);
1678                 if (local_got_refcounts == NULL)
1679                   {
1680                     bfd_size_type size;
1681
1682                     size = symtab_hdr->sh_info;
1683                     size *= (sizeof (bfd_signed_vma)
1684                              + sizeof (bfd_vma) + sizeof(char));
1685                     local_got_refcounts = (bfd_signed_vma *)
1686                         bfd_zalloc (abfd, size);
1687                     if (local_got_refcounts == NULL)
1688                       goto error_return;
1689                     elf_local_got_refcounts (abfd) = local_got_refcounts;
1690                     elf_x86_local_tlsdesc_gotent (abfd)
1691                       = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1692                     elf_x86_local_got_tls_type (abfd)
1693                       = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1694                   }
1695                 local_got_refcounts[r_symndx] += 1;
1696                 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1697               }
1698
1699             if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1700               tls_type |= old_tls_type;
1701             /* If a TLS symbol is accessed using IE at least once,
1702                there is no point to use dynamic model for it.  */
1703             else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1704                      && (! GOT_TLS_GD_ANY_P (old_tls_type)
1705                          || (tls_type & GOT_TLS_IE) == 0))
1706               {
1707                 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1708                   tls_type = old_tls_type;
1709                 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1710                          && GOT_TLS_GD_ANY_P (tls_type))
1711                   tls_type |= old_tls_type;
1712                 else
1713                   {
1714                     if (h)
1715                       name = h->root.root.string;
1716                     else
1717                       name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1718                                              NULL);
1719                     _bfd_error_handler
1720                       /* xgettext:c-format */
1721                       (_("%B: `%s' accessed both as normal and "
1722                          "thread local symbol"),
1723                        abfd, name);
1724                     bfd_set_error (bfd_error_bad_value);
1725                     goto error_return;
1726                   }
1727               }
1728
1729             if (old_tls_type != tls_type)
1730               {
1731                 if (h != NULL)
1732                   elf_x86_hash_entry (h)->tls_type = tls_type;
1733                 else
1734                   elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1735               }
1736           }
1737           /* Fall through */
1738
1739         case R_386_GOTOFF:
1740         case R_386_GOTPC:
1741         create_got:
1742           if (r_type != R_386_TLS_IE)
1743             {
1744               if (eh != NULL)
1745                 eh->has_got_reloc = 1;
1746               break;
1747             }
1748           /* Fall through */
1749
1750         case R_386_TLS_LE_32:
1751         case R_386_TLS_LE:
1752           if (eh != NULL)
1753             eh->has_got_reloc = 1;
1754           if (bfd_link_executable (info))
1755             break;
1756           info->flags |= DF_STATIC_TLS;
1757           goto do_relocation;
1758
1759         case R_386_32:
1760         case R_386_PC32:
1761           if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1762             eh->has_non_got_reloc = 1;
1763 do_relocation:
1764           /* We are called after all symbols have been resolved.  Only
1765              relocation against STT_GNU_IFUNC symbol must go through
1766              PLT.  */
1767           if (h != NULL
1768               && (bfd_link_executable (info)
1769                   || h->type == STT_GNU_IFUNC))
1770             {
1771               /* If this reloc is in a read-only section, we might
1772                  need a copy reloc.  We can't check reliably at this
1773                  stage whether the section is read-only, as input
1774                  sections have not yet been mapped to output sections.
1775                  Tentatively set the flag for now, and correct in
1776                  adjust_dynamic_symbol.  */
1777               h->non_got_ref = 1;
1778
1779               /* We may need a .plt entry if the symbol is a function
1780                  defined in a shared lib or is a STT_GNU_IFUNC function
1781                  referenced from the code or read-only section.  */
1782               if (!h->def_regular
1783                   || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1784                 h->plt.refcount += 1;
1785
1786               if (r_type == R_386_PC32)
1787                 {
1788                   /* Since something like ".long foo - ." may be used
1789                      as pointer, make sure that PLT is used if foo is
1790                      a function defined in a shared library.  */
1791                   if ((sec->flags & SEC_CODE) == 0)
1792                     h->pointer_equality_needed = 1;
1793                   else if (h->type == STT_GNU_IFUNC
1794                            && bfd_link_pic (info))
1795                     {
1796                       _bfd_error_handler
1797                         /* xgettext:c-format */
1798                         (_("%B: unsupported non-PIC call to IFUNC `%s'"),
1799                          abfd, h->root.root.string);
1800                       bfd_set_error (bfd_error_bad_value);
1801                       goto error_return;
1802                     }
1803                 }
1804               else
1805                 {
1806                   h->pointer_equality_needed = 1;
1807                   /* R_386_32 can be resolved at run-time.  */
1808                   if (r_type == R_386_32
1809                       && (sec->flags & SEC_READONLY) == 0)
1810                     eh->func_pointer_refcount += 1;
1811                 }
1812             }
1813
1814           size_reloc = FALSE;
1815 do_size:
1816           /* If we are creating a shared library, and this is a reloc
1817              against a global symbol, or a non PC relative reloc
1818              against a local symbol, then we need to copy the reloc
1819              into the shared library.  However, if we are linking with
1820              -Bsymbolic, we do not need to copy a reloc against a
1821              global symbol which is defined in an object we are
1822              including in the link (i.e., DEF_REGULAR is set).  At
1823              this point we have not seen all the input files, so it is
1824              possible that DEF_REGULAR is not set now but will be set
1825              later (it is never cleared).  In case of a weak definition,
1826              DEF_REGULAR may be cleared later by a strong definition in
1827              a shared library.  We account for that possibility below by
1828              storing information in the relocs_copied field of the hash
1829              table entry.  A similar situation occurs when creating
1830              shared libraries and symbol visibility changes render the
1831              symbol local.
1832
1833              If on the other hand, we are creating an executable, we
1834              may need to keep relocations for symbols satisfied by a
1835              dynamic library if we manage to avoid copy relocs for the
1836              symbol.
1837
1838              Generate dynamic pointer relocation against STT_GNU_IFUNC
1839              symbol in the non-code section.  */
1840           if ((bfd_link_pic (info)
1841                && (r_type != R_386_PC32
1842                    || (h != NULL
1843                        && (! (bfd_link_pie (info)
1844                               || SYMBOLIC_BIND (info, h))
1845                            || h->root.type == bfd_link_hash_defweak
1846                            || !h->def_regular))))
1847               || (h != NULL
1848                   && h->type == STT_GNU_IFUNC
1849                   && r_type == R_386_32
1850                   && (sec->flags & SEC_CODE) == 0)
1851               || (ELIMINATE_COPY_RELOCS
1852                   && !bfd_link_pic (info)
1853                   && h != NULL
1854                   && (h->root.type == bfd_link_hash_defweak
1855                       || !h->def_regular)))
1856             {
1857               struct elf_dyn_relocs *p;
1858               struct elf_dyn_relocs **head;
1859
1860               /* We must copy these reloc types into the output file.
1861                  Create a reloc section in dynobj and make room for
1862                  this reloc.  */
1863               if (sreloc == NULL)
1864                 {
1865                   sreloc = _bfd_elf_make_dynamic_reloc_section
1866                     (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1867
1868                   if (sreloc == NULL)
1869                     goto error_return;
1870                 }
1871
1872               /* If this is a global symbol, we count the number of
1873                  relocations we need for this symbol.  */
1874               if (h != NULL)
1875                 {
1876                   head = &eh->dyn_relocs;
1877                 }
1878               else
1879                 {
1880                   /* Track dynamic relocs needed for local syms too.
1881                      We really need local syms available to do this
1882                      easily.  Oh well.  */
1883                   void **vpp;
1884                   asection *s;
1885
1886                   isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1887                                                 abfd, r_symndx);
1888                   if (isym == NULL)
1889                     goto error_return;
1890
1891                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1892                   if (s == NULL)
1893                     s = sec;
1894
1895                   vpp = &elf_section_data (s)->local_dynrel;
1896                   head = (struct elf_dyn_relocs **)vpp;
1897                 }
1898
1899               p = *head;
1900               if (p == NULL || p->sec != sec)
1901                 {
1902                   bfd_size_type amt = sizeof *p;
1903                   p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1904                                                            amt);
1905                   if (p == NULL)
1906                     goto error_return;
1907                   p->next = *head;
1908                   *head = p;
1909                   p->sec = sec;
1910                   p->count = 0;
1911                   p->pc_count = 0;
1912                 }
1913
1914               p->count += 1;
1915               /* Count size relocation as PC-relative relocation.  */
1916               if (r_type == R_386_PC32 || size_reloc)
1917                 p->pc_count += 1;
1918             }
1919           break;
1920
1921           /* This relocation describes the C++ object vtable hierarchy.
1922              Reconstruct it for later use during GC.  */
1923         case R_386_GNU_VTINHERIT:
1924           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1925             goto error_return;
1926           break;
1927
1928           /* This relocation describes which C++ vtable entries are actually
1929              used.  Record for later use during GC.  */
1930         case R_386_GNU_VTENTRY:
1931           BFD_ASSERT (h != NULL);
1932           if (h != NULL
1933               && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1934             goto error_return;
1935           break;
1936
1937         default:
1938           break;
1939         }
1940     }
1941
1942   if (elf_section_data (sec)->this_hdr.contents != contents)
1943     {
1944       if (!converted && !info->keep_memory)
1945         free (contents);
1946       else
1947         {
1948           /* Cache the section contents for elf_link_input_bfd if any
1949              load is converted or --no-keep-memory isn't used.  */
1950           elf_section_data (sec)->this_hdr.contents = contents;
1951         }
1952     }
1953
1954   /* Cache relocations if any load is converted.  */
1955   if (elf_section_data (sec)->relocs != relocs && converted)
1956     elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1957
1958   return TRUE;
1959
1960 error_return:
1961   if (elf_section_data (sec)->this_hdr.contents != contents)
1962     free (contents);
1963   sec->check_relocs_failed = 1;
1964   return FALSE;
1965 }
1966
1967 /* Set the correct type for an x86 ELF section.  We do this by the
1968    section name, which is a hack, but ought to work.  */
1969
1970 static bfd_boolean
1971 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1972                         Elf_Internal_Shdr *hdr,
1973                         asection *sec)
1974 {
1975   const char *name;
1976
1977   name = bfd_get_section_name (abfd, sec);
1978
1979   /* This is an ugly, but unfortunately necessary hack that is
1980      needed when producing EFI binaries on x86. It tells
1981      elf.c:elf_fake_sections() not to consider ".reloc" as a section
1982      containing ELF relocation info.  We need this hack in order to
1983      be able to generate ELF binaries that can be translated into
1984      EFI applications (which are essentially COFF objects).  Those
1985      files contain a COFF ".reloc" section inside an ELFNN object,
1986      which would normally cause BFD to segfault because it would
1987      attempt to interpret this section as containing relocation
1988      entries for section "oc".  With this hack enabled, ".reloc"
1989      will be treated as a normal data section, which will avoid the
1990      segfault.  However, you won't be able to create an ELFNN binary
1991      with a section named "oc" that needs relocations, but that's
1992      the kind of ugly side-effects you get when detecting section
1993      types based on their names...  In practice, this limitation is
1994      unlikely to bite.  */
1995   if (strcmp (name, ".reloc") == 0)
1996     hdr->sh_type = SHT_PROGBITS;
1997
1998   return TRUE;
1999 }
2000
2001 /* Return the relocation value for @tpoff relocation
2002    if STT_TLS virtual address is ADDRESS.  */
2003
2004 static bfd_vma
2005 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2006 {
2007   struct elf_link_hash_table *htab = elf_hash_table (info);
2008   const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2009   bfd_vma static_tls_size;
2010
2011   /* If tls_sec is NULL, we should have signalled an error already.  */
2012   if (htab->tls_sec == NULL)
2013     return 0;
2014
2015   /* Consider special static TLS alignment requirements.  */
2016   static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2017   return static_tls_size + htab->tls_sec->vma - address;
2018 }
2019
2020 /* Relocate an i386 ELF section.  */
2021
2022 static bfd_boolean
2023 elf_i386_relocate_section (bfd *output_bfd,
2024                            struct bfd_link_info *info,
2025                            bfd *input_bfd,
2026                            asection *input_section,
2027                            bfd_byte *contents,
2028                            Elf_Internal_Rela *relocs,
2029                            Elf_Internal_Sym *local_syms,
2030                            asection **local_sections)
2031 {
2032   struct elf_x86_link_hash_table *htab;
2033   Elf_Internal_Shdr *symtab_hdr;
2034   struct elf_link_hash_entry **sym_hashes;
2035   bfd_vma *local_got_offsets;
2036   bfd_vma *local_tlsdesc_gotents;
2037   Elf_Internal_Rela *rel;
2038   Elf_Internal_Rela *wrel;
2039   Elf_Internal_Rela *relend;
2040   bfd_boolean is_vxworks_tls;
2041   unsigned plt_entry_size;
2042
2043   /* Skip if check_relocs failed.  */
2044   if (input_section->check_relocs_failed)
2045     return FALSE;
2046
2047   htab = elf_x86_hash_table (info, I386_ELF_DATA);
2048   if (htab == NULL)
2049     return FALSE;
2050
2051   BFD_ASSERT (is_x86_elf (input_bfd, htab));
2052
2053   symtab_hdr = &elf_symtab_hdr (input_bfd);
2054   sym_hashes = elf_sym_hashes (input_bfd);
2055   local_got_offsets = elf_local_got_offsets (input_bfd);
2056   local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2057   /* We have to handle relocations in vxworks .tls_vars sections
2058      specially, because the dynamic loader is 'weird'.  */
2059   is_vxworks_tls = (htab->is_vxworks
2060                     && bfd_link_pic (info)
2061                     && !strcmp (input_section->output_section->name,
2062                                 ".tls_vars"));
2063
2064   _bfd_x86_elf_set_tls_module_base (info);
2065
2066   plt_entry_size = htab->plt.plt_entry_size;
2067
2068   rel = wrel = relocs;
2069   relend = relocs + input_section->reloc_count;
2070   for (; rel < relend; wrel++, rel++)
2071     {
2072       unsigned int r_type, r_type_tls;
2073       reloc_howto_type *howto;
2074       unsigned long r_symndx;
2075       struct elf_link_hash_entry *h;
2076       struct elf_x86_link_hash_entry *eh;
2077       Elf_Internal_Sym *sym;
2078       asection *sec;
2079       bfd_vma off, offplt, plt_offset;
2080       bfd_vma relocation;
2081       bfd_boolean unresolved_reloc;
2082       bfd_reloc_status_type r;
2083       unsigned int indx;
2084       int tls_type;
2085       bfd_vma st_size;
2086       asection *resolved_plt;
2087       bfd_boolean resolved_to_zero;
2088       bfd_boolean relative_reloc;
2089
2090       r_type = ELF32_R_TYPE (rel->r_info);
2091       if (r_type == R_386_GNU_VTINHERIT
2092           || r_type == R_386_GNU_VTENTRY)
2093         {
2094           if (wrel != rel)
2095             *wrel = *rel;
2096           continue;
2097         }
2098
2099       if ((indx = r_type) >= R_386_standard
2100           && ((indx = r_type - R_386_ext_offset) - R_386_standard
2101               >= R_386_ext - R_386_standard)
2102           && ((indx = r_type - R_386_tls_offset) - R_386_ext
2103               >= R_386_ext2 - R_386_ext))
2104         return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2105
2106       howto = elf_howto_table + indx;
2107
2108       r_symndx = ELF32_R_SYM (rel->r_info);
2109       h = NULL;
2110       sym = NULL;
2111       sec = NULL;
2112       unresolved_reloc = FALSE;
2113       if (r_symndx < symtab_hdr->sh_info)
2114         {
2115           sym = local_syms + r_symndx;
2116           sec = local_sections[r_symndx];
2117           relocation = (sec->output_section->vma
2118                         + sec->output_offset
2119                         + sym->st_value);
2120           st_size = sym->st_size;
2121
2122           if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2123               && ((sec->flags & SEC_MERGE) != 0
2124                   || (bfd_link_relocatable (info)
2125                       && sec->output_offset != 0)))
2126             {
2127               bfd_vma addend;
2128               bfd_byte *where = contents + rel->r_offset;
2129
2130               switch (howto->size)
2131                 {
2132                 case 0:
2133                   addend = bfd_get_8 (input_bfd, where);
2134                   if (howto->pc_relative)
2135                     {
2136                       addend = (addend ^ 0x80) - 0x80;
2137                       addend += 1;
2138                     }
2139                   break;
2140                 case 1:
2141                   addend = bfd_get_16 (input_bfd, where);
2142                   if (howto->pc_relative)
2143                     {
2144                       addend = (addend ^ 0x8000) - 0x8000;
2145                       addend += 2;
2146                     }
2147                   break;
2148                 case 2:
2149                   addend = bfd_get_32 (input_bfd, where);
2150                   if (howto->pc_relative)
2151                     {
2152                       addend = (addend ^ 0x80000000) - 0x80000000;
2153                       addend += 4;
2154                     }
2155                   break;
2156                 default:
2157                   abort ();
2158                 }
2159
2160               if (bfd_link_relocatable (info))
2161                 addend += sec->output_offset;
2162               else
2163                 {
2164                   asection *msec = sec;
2165                   addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2166                                                    addend);
2167                   addend -= relocation;
2168                   addend += msec->output_section->vma + msec->output_offset;
2169                 }
2170
2171               switch (howto->size)
2172                 {
2173                 case 0:
2174                   /* FIXME: overflow checks.  */
2175                   if (howto->pc_relative)
2176                     addend -= 1;
2177                   bfd_put_8 (input_bfd, addend, where);
2178                   break;
2179                 case 1:
2180                   if (howto->pc_relative)
2181                     addend -= 2;
2182                   bfd_put_16 (input_bfd, addend, where);
2183                   break;
2184                 case 2:
2185                   if (howto->pc_relative)
2186                     addend -= 4;
2187                   bfd_put_32 (input_bfd, addend, where);
2188                   break;
2189                 }
2190             }
2191           else if (!bfd_link_relocatable (info)
2192                    && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2193             {
2194               /* Relocate against local STT_GNU_IFUNC symbol.  */
2195               h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2196                                                    FALSE);
2197               if (h == NULL)
2198                 abort ();
2199
2200               /* Set STT_GNU_IFUNC symbol value.  */
2201               h->root.u.def.value = sym->st_value;
2202               h->root.u.def.section = sec;
2203             }
2204         }
2205       else
2206         {
2207           bfd_boolean warned ATTRIBUTE_UNUSED;
2208           bfd_boolean ignored ATTRIBUTE_UNUSED;
2209
2210           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2211                                    r_symndx, symtab_hdr, sym_hashes,
2212                                    h, sec, relocation,
2213                                    unresolved_reloc, warned, ignored);
2214           st_size = h->size;
2215         }
2216
2217       if (sec != NULL && discarded_section (sec))
2218         {
2219           _bfd_clear_contents (howto, input_bfd, input_section,
2220                                contents + rel->r_offset);
2221           wrel->r_offset = rel->r_offset;
2222           wrel->r_info = 0;
2223           wrel->r_addend = 0;
2224
2225           /* For ld -r, remove relocations in debug sections against
2226              sections defined in discarded sections.  Not done for
2227              eh_frame editing code expects to be present.  */
2228            if (bfd_link_relocatable (info)
2229                && (input_section->flags & SEC_DEBUGGING))
2230              wrel--;
2231
2232            continue;
2233         }
2234
2235       if (bfd_link_relocatable (info))
2236         {
2237           if (wrel != rel)
2238             *wrel = *rel;
2239           continue;
2240         }
2241
2242       eh = (struct elf_x86_link_hash_entry *) h;
2243
2244       /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2245          it here if it is defined in a non-shared object.  */
2246       if (h != NULL
2247           && h->type == STT_GNU_IFUNC
2248           && h->def_regular)
2249         {
2250           asection *gotplt, *base_got;
2251           bfd_vma plt_index;
2252           const char *name;
2253
2254           if ((input_section->flags & SEC_ALLOC) == 0)
2255             {
2256               /* Dynamic relocs are not propagated for SEC_DEBUGGING
2257                  sections because such sections are not SEC_ALLOC and
2258                  thus ld.so will not process them.  */
2259               if ((input_section->flags & SEC_DEBUGGING) != 0)
2260                 continue;
2261               abort ();
2262             }
2263
2264           /* STT_GNU_IFUNC symbol must go through PLT.  */
2265           if (htab->elf.splt != NULL)
2266             {
2267               if (htab->plt_second != NULL)
2268                 {
2269                   resolved_plt = htab->plt_second;
2270                   plt_offset = eh->plt_second.offset;
2271                 }
2272               else
2273                 {
2274                   resolved_plt = htab->elf.splt;
2275                   plt_offset = h->plt.offset;
2276                 }
2277               gotplt = htab->elf.sgotplt;
2278             }
2279           else
2280             {
2281               resolved_plt = htab->elf.iplt;
2282               plt_offset = h->plt.offset;
2283               gotplt = htab->elf.igotplt;
2284             }
2285
2286           switch (r_type)
2287             {
2288             default:
2289               break;
2290
2291             case R_386_GOT32:
2292             case R_386_GOT32X:
2293               base_got = htab->elf.sgot;
2294               off = h->got.offset;
2295
2296               if (base_got == NULL)
2297                 abort ();
2298
2299               if (off == (bfd_vma) -1)
2300                 {
2301                   /* We can't use h->got.offset here to save state, or
2302                      even just remember the offset, as finish_dynamic_symbol
2303                      would use that as offset into .got.  */
2304
2305                   if (h->plt.offset == (bfd_vma) -1)
2306                     abort ();
2307
2308                   if (htab->elf.splt != NULL)
2309                     {
2310                       plt_index = (h->plt.offset / plt_entry_size
2311                                    - htab->plt.has_plt0);
2312                       off = (plt_index + 3) * 4;
2313                       base_got = htab->elf.sgotplt;
2314                     }
2315                   else
2316                     {
2317                       plt_index = h->plt.offset / plt_entry_size;
2318                       off = plt_index * 4;
2319                       base_got = htab->elf.igotplt;
2320                     }
2321
2322                   if (h->dynindx == -1
2323                       || h->forced_local
2324                       || info->symbolic)
2325                     {
2326                       /* This references the local defitionion.  We must
2327                          initialize this entry in the global offset table.
2328                          Since the offset must always be a multiple of 8,
2329                          we use the least significant bit to record
2330                          whether we have initialized it already.
2331
2332                          When doing a dynamic link, we create a .rela.got
2333                          relocation entry to initialize the value.  This
2334                          is done in the finish_dynamic_symbol routine.   */
2335                       if ((off & 1) != 0)
2336                         off &= ~1;
2337                       else
2338                         {
2339                           bfd_put_32 (output_bfd, relocation,
2340                                       base_got->contents + off);
2341                           h->got.offset |= 1;
2342                         }
2343                     }
2344
2345                   relocation = off;
2346                 }
2347               else
2348                 relocation = (base_got->output_section->vma
2349                               + base_got->output_offset + off
2350                               - gotplt->output_section->vma
2351                               - gotplt->output_offset);
2352
2353               if (rel->r_offset > 1
2354                   && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2355                   && *(contents + rel->r_offset - 2) != 0x8d)
2356                 {
2357                   if (bfd_link_pic (info))
2358                     goto disallow_got32;
2359
2360                   /* Add the GOT base if there is no base register.  */
2361                   relocation += (gotplt->output_section->vma
2362                                  + gotplt->output_offset);
2363                 }
2364               else if (htab->elf.splt == NULL)
2365                 {
2366                   /* Adjust for static executables.  */
2367                   relocation += gotplt->output_offset;
2368                 }
2369
2370               goto do_relocation;
2371             }
2372
2373           if (h->plt.offset == (bfd_vma) -1)
2374             {
2375               /* Handle static pointers of STT_GNU_IFUNC symbols.  */
2376               if (r_type == R_386_32
2377                   && (input_section->flags & SEC_CODE) == 0)
2378                 goto do_ifunc_pointer;
2379               goto bad_ifunc_reloc;
2380             }
2381
2382           relocation = (resolved_plt->output_section->vma
2383                         + resolved_plt->output_offset + plt_offset);
2384
2385           switch (r_type)
2386             {
2387             default:
2388 bad_ifunc_reloc:
2389               if (h->root.root.string)
2390                 name = h->root.root.string;
2391               else
2392                 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2393                                          NULL);
2394               _bfd_error_handler
2395                 /* xgettext:c-format */
2396                 (_("%B: relocation %s against STT_GNU_IFUNC "
2397                    "symbol `%s' isn't supported"), input_bfd,
2398                  howto->name, name);
2399               bfd_set_error (bfd_error_bad_value);
2400               return FALSE;
2401
2402             case R_386_32:
2403               /* Generate dynamic relcoation only when there is a
2404                  non-GOT reference in a shared object.  */
2405               if ((bfd_link_pic (info) && h->non_got_ref)
2406                   || h->plt.offset == (bfd_vma) -1)
2407                 {
2408                   Elf_Internal_Rela outrel;
2409                   asection *sreloc;
2410                   bfd_vma offset;
2411
2412 do_ifunc_pointer:
2413                   /* Need a dynamic relocation to get the real function
2414                      adddress.  */
2415                   offset = _bfd_elf_section_offset (output_bfd,
2416                                                     info,
2417                                                     input_section,
2418                                                     rel->r_offset);
2419                   if (offset == (bfd_vma) -1
2420                       || offset == (bfd_vma) -2)
2421                     abort ();
2422
2423                   outrel.r_offset = (input_section->output_section->vma
2424                                      + input_section->output_offset
2425                                      + offset);
2426
2427                   if (h->dynindx == -1
2428                       || h->forced_local
2429                       || bfd_link_executable (info))
2430                     {
2431                       info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
2432                                               h->root.root.string,
2433                                               h->root.u.def.section->owner);
2434
2435                       /* This symbol is resolved locally.  */
2436                       outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2437                       bfd_put_32 (output_bfd,
2438                                   (h->root.u.def.value
2439                                    + h->root.u.def.section->output_section->vma
2440                                    + h->root.u.def.section->output_offset),
2441                                   contents + offset);
2442                     }
2443                   else
2444                     outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2445
2446                   /* Dynamic relocations are stored in
2447                      1. .rel.ifunc section in PIC object.
2448                      2. .rel.got section in dynamic executable.
2449                      3. .rel.iplt section in static executable.  */
2450                   if (bfd_link_pic (info))
2451                     sreloc = htab->elf.irelifunc;
2452                   else if (htab->elf.splt != NULL)
2453                     sreloc = htab->elf.srelgot;
2454                   else
2455                     sreloc = htab->elf.irelplt;
2456                   elf_append_rel (output_bfd, sreloc, &outrel);
2457
2458                   /* If this reloc is against an external symbol, we
2459                      do not want to fiddle with the addend.  Otherwise,
2460                      we need to include the symbol value so that it
2461                      becomes an addend for the dynamic reloc.  For an
2462                      internal symbol, we have updated addend.  */
2463                   continue;
2464                 }
2465               /* FALLTHROUGH */
2466             case R_386_PC32:
2467             case R_386_PLT32:
2468               goto do_relocation;
2469
2470             case R_386_GOTOFF:
2471               relocation -= (gotplt->output_section->vma
2472                              + gotplt->output_offset);
2473               goto do_relocation;
2474             }
2475         }
2476
2477       resolved_to_zero = (eh != NULL
2478                           && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2479
2480       switch (r_type)
2481         {
2482         case R_386_GOT32X:
2483           /* Avoid optimizing _DYNAMIC since ld.so may use its
2484              link-time address.  */
2485           if (h == htab->elf.hdynamic)
2486             goto r_386_got32;
2487
2488           if (bfd_link_pic (info))
2489             {
2490               /* It is OK to convert mov to lea and convert indirect
2491                  branch to direct branch.  It is OK to convert adc,
2492                  add, and, cmp, or, sbb, sub, test, xor only when PIC
2493                  is false.   */
2494               unsigned int opcode, addend;
2495               addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
2496               if (addend != 0)
2497                 goto r_386_got32;
2498               opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2499               if (opcode != 0x8b && opcode != 0xff)
2500                 goto r_386_got32;
2501             }
2502
2503           /* Resolve "mov GOT[(%reg)], %reg",
2504              "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
2505              and "binop foo@GOT[(%reg)], %reg".  */
2506           if (h == NULL
2507               || (h->plt.offset == (bfd_vma) -1
2508                   && h->got.offset == (bfd_vma) -1)
2509               || htab->elf.sgotplt == NULL)
2510             abort ();
2511
2512           offplt = (htab->elf.sgotplt->output_section->vma
2513                     + htab->elf.sgotplt->output_offset);
2514
2515           /* It is relative to .got.plt section.  */
2516           if (h->got.offset != (bfd_vma) -1)
2517             /* Use GOT entry.  Mask off the least significant bit in
2518                GOT offset which may be set by R_386_GOT32 processing
2519                below.  */
2520             relocation = (htab->elf.sgot->output_section->vma
2521                           + htab->elf.sgot->output_offset
2522                           + (h->got.offset & ~1) - offplt);
2523           else
2524             /* Use GOTPLT entry.  */
2525             relocation = (h->plt.offset / plt_entry_size
2526                           - htab->plt.has_plt0 + 3) * 4;
2527
2528           if (!bfd_link_pic (info))
2529             {
2530               /* If not PIC, add the .got.plt section address for
2531                  baseless addressing.  */
2532               unsigned int modrm;
2533               modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2534               if ((modrm & 0xc7) == 0x5)
2535                 relocation += offplt;
2536             }
2537
2538           unresolved_reloc = FALSE;
2539           break;
2540
2541         case R_386_GOT32:
2542 r_386_got32:
2543           /* Relocation is to the entry for this symbol in the global
2544              offset table.  */
2545           if (htab->elf.sgot == NULL)
2546             abort ();
2547
2548           relative_reloc = FALSE;
2549           if (h != NULL)
2550             {
2551               bfd_boolean dyn;
2552
2553               off = h->got.offset;
2554               dyn = htab->elf.dynamic_sections_created;
2555               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2556                                                      bfd_link_pic (info),
2557                                                      h)
2558                   || (bfd_link_pic (info)
2559                       && SYMBOL_REFERENCES_LOCAL_P (info, h))
2560                   || (ELF_ST_VISIBILITY (h->other)
2561                       && h->root.type == bfd_link_hash_undefweak))
2562                 {
2563                   /* This is actually a static link, or it is a
2564                      -Bsymbolic link and the symbol is defined
2565                      locally, or the symbol was forced to be local
2566                      because of a version file.  We must initialize
2567                      this entry in the global offset table.  Since the
2568                      offset must always be a multiple of 4, we use the
2569                      least significant bit to record whether we have
2570                      initialized it already.
2571
2572                      When doing a dynamic link, we create a .rel.got
2573                      relocation entry to initialize the value.  This
2574                      is done in the finish_dynamic_symbol routine.  */
2575                   if ((off & 1) != 0)
2576                     off &= ~1;
2577                   else
2578                     {
2579                       bfd_put_32 (output_bfd, relocation,
2580                                   htab->elf.sgot->contents + off);
2581                       h->got.offset |= 1;
2582
2583                       if (h->dynindx == -1
2584                           && !h->forced_local
2585                           && h->root.type != bfd_link_hash_undefweak
2586                           && bfd_link_pic (info))
2587                         {
2588                           /* PR ld/21402: If this symbol isn't dynamic
2589                              in PIC, generate R_386_RELATIVE here.  */
2590                           eh->no_finish_dynamic_symbol = 1;
2591                           relative_reloc = TRUE;
2592                         }
2593                     }
2594                 }
2595               else
2596                 unresolved_reloc = FALSE;
2597             }
2598           else
2599             {
2600               if (local_got_offsets == NULL)
2601                 abort ();
2602
2603               off = local_got_offsets[r_symndx];
2604
2605               /* The offset must always be a multiple of 4.  We use
2606                  the least significant bit to record whether we have
2607                  already generated the necessary reloc.  */
2608               if ((off & 1) != 0)
2609                 off &= ~1;
2610               else
2611                 {
2612                   bfd_put_32 (output_bfd, relocation,
2613                               htab->elf.sgot->contents + off);
2614                   local_got_offsets[r_symndx] |= 1;
2615
2616                   if (bfd_link_pic (info))
2617                     relative_reloc = TRUE;
2618                 }
2619             }
2620
2621           if (relative_reloc)
2622             {
2623               asection *s;
2624               Elf_Internal_Rela outrel;
2625
2626               s = htab->elf.srelgot;
2627               if (s == NULL)
2628                 abort ();
2629
2630               outrel.r_offset = (htab->elf.sgot->output_section->vma
2631                                  + htab->elf.sgot->output_offset
2632                                  + off);
2633               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2634               elf_append_rel (output_bfd, s, &outrel);
2635             }
2636
2637           if (off >= (bfd_vma) -2)
2638             abort ();
2639
2640           relocation = (htab->elf.sgot->output_section->vma
2641                         + htab->elf.sgot->output_offset + off);
2642           if (rel->r_offset > 1
2643               && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2644               && *(contents + rel->r_offset - 2) != 0x8d)
2645             {
2646               if (bfd_link_pic (info))
2647                 {
2648                   /* For PIC, disallow R_386_GOT32 without a base
2649                      register, except for "lea foo@GOT, %reg", since
2650                      we don't know what the GOT base is.  */
2651                   const char *name;
2652
2653 disallow_got32:
2654                   if (h == NULL || h->root.root.string == NULL)
2655                     name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2656                                              NULL);
2657                   else
2658                     name = h->root.root.string;
2659
2660                   _bfd_error_handler
2661                     /* xgettext:c-format */
2662                     (_("%B: direct GOT relocation %s against `%s'"
2663                        " without base register can not be used"
2664                        " when making a shared object"),
2665                      input_bfd, howto->name, name);
2666                   bfd_set_error (bfd_error_bad_value);
2667                   return FALSE;
2668                 }
2669             }
2670           else
2671             {
2672               /* Subtract the .got.plt section address only with a base
2673                  register.  */
2674               relocation -= (htab->elf.sgotplt->output_section->vma
2675                              + htab->elf.sgotplt->output_offset);
2676             }
2677
2678           break;
2679
2680         case R_386_GOTOFF:
2681           /* Relocation is relative to the start of the global offset
2682              table.  */
2683
2684           /* Check to make sure it isn't a protected function or data
2685              symbol for shared library since it may not be local when
2686              used as function address or with copy relocation.  We also
2687              need to make sure that a symbol is referenced locally.  */
2688           if (!bfd_link_executable (info) && h)
2689             {
2690               if (!h->def_regular)
2691                 {
2692                   const char *v;
2693
2694                   switch (ELF_ST_VISIBILITY (h->other))
2695                     {
2696                     case STV_HIDDEN:
2697                       v = _("hidden symbol");
2698                       break;
2699                     case STV_INTERNAL:
2700                       v = _("internal symbol");
2701                       break;
2702                     case STV_PROTECTED:
2703                       v = _("protected symbol");
2704                       break;
2705                     default:
2706                       v = _("symbol");
2707                       break;
2708                     }
2709
2710                   _bfd_error_handler
2711                     /* xgettext:c-format */
2712                     (_("%B: relocation R_386_GOTOFF against undefined %s"
2713                        " `%s' can not be used when making a shared object"),
2714                      input_bfd, v, h->root.root.string);
2715                   bfd_set_error (bfd_error_bad_value);
2716                   return FALSE;
2717                 }
2718               else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2719                        && (h->type == STT_FUNC
2720                            || h->type == STT_OBJECT)
2721                        && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2722                 {
2723                   _bfd_error_handler
2724                     /* xgettext:c-format */
2725                     (_("%B: relocation R_386_GOTOFF against protected %s"
2726                        " `%s' can not be used when making a shared object"),
2727                      input_bfd,
2728                      h->type == STT_FUNC ? "function" : "data",
2729                      h->root.root.string);
2730                   bfd_set_error (bfd_error_bad_value);
2731                   return FALSE;
2732                 }
2733             }
2734
2735           /* Note that sgot is not involved in this
2736              calculation.  We always want the start of .got.plt.  If we
2737              defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2738              permitted by the ABI, we might have to change this
2739              calculation.  */
2740           relocation -= htab->elf.sgotplt->output_section->vma
2741                         + htab->elf.sgotplt->output_offset;
2742           break;
2743
2744         case R_386_GOTPC:
2745           /* Use global offset table as symbol value.  */
2746           relocation = htab->elf.sgotplt->output_section->vma
2747                        + htab->elf.sgotplt->output_offset;
2748           unresolved_reloc = FALSE;
2749           break;
2750
2751         case R_386_PLT32:
2752           /* Relocation is to the entry for this symbol in the
2753              procedure linkage table.  */
2754
2755           /* Resolve a PLT32 reloc against a local symbol directly,
2756              without using the procedure linkage table.  */
2757           if (h == NULL)
2758             break;
2759
2760           if ((h->plt.offset == (bfd_vma) -1
2761                && eh->plt_got.offset == (bfd_vma) -1)
2762               || htab->elf.splt == NULL)
2763             {
2764               /* We didn't make a PLT entry for this symbol.  This
2765                  happens when statically linking PIC code, or when
2766                  using -Bsymbolic.  */
2767               break;
2768             }
2769
2770           if (h->plt.offset != (bfd_vma) -1)
2771             {
2772               if (htab->plt_second != NULL)
2773                 {
2774                   resolved_plt = htab->plt_second;
2775                   plt_offset = eh->plt_second.offset;
2776                 }
2777               else
2778                 {
2779                   resolved_plt = htab->elf.splt;
2780                   plt_offset = h->plt.offset;
2781                 }
2782             }
2783           else
2784             {
2785               resolved_plt = htab->plt_got;
2786               plt_offset = eh->plt_got.offset;
2787             }
2788
2789           relocation = (resolved_plt->output_section->vma
2790                         + resolved_plt->output_offset
2791                         + plt_offset);
2792           unresolved_reloc = FALSE;
2793           break;
2794
2795         case R_386_SIZE32:
2796           /* Set to symbol size.  */
2797           relocation = st_size;
2798           /* Fall through.  */
2799
2800         case R_386_32:
2801         case R_386_PC32:
2802           if ((input_section->flags & SEC_ALLOC) == 0
2803               || is_vxworks_tls)
2804             break;
2805
2806           /* Copy dynamic function pointer relocations.  Don't generate
2807              dynamic relocations against resolved undefined weak symbols
2808              in PIE, except for R_386_PC32.  */
2809           if ((bfd_link_pic (info)
2810                && (h == NULL
2811                    || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2812                         && (!resolved_to_zero
2813                             || r_type == R_386_PC32))
2814                        || h->root.type != bfd_link_hash_undefweak))
2815                && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
2816                    || !SYMBOL_CALLS_LOCAL (info, h)))
2817               || (ELIMINATE_COPY_RELOCS
2818                   && !bfd_link_pic (info)
2819                   && h != NULL
2820                   && h->dynindx != -1
2821                   && (!h->non_got_ref
2822                       || eh->func_pointer_refcount > 0
2823                       || (h->root.type == bfd_link_hash_undefweak
2824                           && !resolved_to_zero))
2825                   && ((h->def_dynamic && !h->def_regular)
2826                       /* Undefined weak symbol is bound locally when
2827                          PIC is false.  */
2828                       || h->root.type == bfd_link_hash_undefweak)))
2829             {
2830               Elf_Internal_Rela outrel;
2831               bfd_boolean skip, relocate;
2832               asection *sreloc;
2833
2834               /* When generating a shared object, these relocations
2835                  are copied into the output file to be resolved at run
2836                  time.  */
2837
2838               skip = FALSE;
2839               relocate = FALSE;
2840
2841               outrel.r_offset =
2842                 _bfd_elf_section_offset (output_bfd, info, input_section,
2843                                          rel->r_offset);
2844               if (outrel.r_offset == (bfd_vma) -1)
2845                 skip = TRUE;
2846               else if (outrel.r_offset == (bfd_vma) -2)
2847                 skip = TRUE, relocate = TRUE;
2848               outrel.r_offset += (input_section->output_section->vma
2849                                   + input_section->output_offset);
2850
2851               if (skip)
2852                 memset (&outrel, 0, sizeof outrel);
2853               else if (h != NULL
2854                        && h->dynindx != -1
2855                        && (r_type == R_386_PC32
2856                            || !(bfd_link_executable (info)
2857                                 || SYMBOLIC_BIND (info, h))
2858                            || !h->def_regular))
2859                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2860               else
2861                 {
2862                   /* This symbol is local, or marked to become local.  */
2863                   relocate = TRUE;
2864                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2865                 }
2866
2867               sreloc = elf_section_data (input_section)->sreloc;
2868
2869               if (sreloc == NULL || sreloc->contents == NULL)
2870                 {
2871                   r = bfd_reloc_notsupported;
2872                   goto check_relocation_error;
2873                 }
2874
2875               elf_append_rel (output_bfd, sreloc, &outrel);
2876
2877               /* If this reloc is against an external symbol, we do
2878                  not want to fiddle with the addend.  Otherwise, we
2879                  need to include the symbol value so that it becomes
2880                  an addend for the dynamic reloc.  */
2881               if (! relocate)
2882                 continue;
2883             }
2884           break;
2885
2886         case R_386_TLS_IE:
2887           if (!bfd_link_executable (info))
2888             {
2889               Elf_Internal_Rela outrel;
2890               asection *sreloc;
2891
2892               outrel.r_offset = rel->r_offset
2893                                 + input_section->output_section->vma
2894                                 + input_section->output_offset;
2895               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2896               sreloc = elf_section_data (input_section)->sreloc;
2897               if (sreloc == NULL)
2898                 abort ();
2899               elf_append_rel (output_bfd, sreloc, &outrel);
2900             }
2901           /* Fall through */
2902
2903         case R_386_TLS_GD:
2904         case R_386_TLS_GOTDESC:
2905         case R_386_TLS_DESC_CALL:
2906         case R_386_TLS_IE_32:
2907         case R_386_TLS_GOTIE:
2908           tls_type = GOT_UNKNOWN;
2909           if (h == NULL && local_got_offsets)
2910             tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2911           else if (h != NULL)
2912             tls_type = elf_x86_hash_entry(h)->tls_type;
2913           if (tls_type == GOT_TLS_IE)
2914             tls_type = GOT_TLS_IE_NEG;
2915
2916            r_type_tls = r_type;
2917           if (! elf_i386_tls_transition (info, input_bfd,
2918                                          input_section, contents,
2919                                          symtab_hdr, sym_hashes,
2920                                          &r_type_tls, tls_type, rel,
2921                                          relend, h, r_symndx, TRUE))
2922             return FALSE;
2923
2924           if (r_type_tls == R_386_TLS_LE_32)
2925             {
2926               BFD_ASSERT (! unresolved_reloc);
2927               if (r_type == R_386_TLS_GD)
2928                 {
2929                   unsigned int type;
2930                   bfd_vma roff;
2931
2932                   /* GD->LE transition.  */
2933                   type = *(contents + rel->r_offset - 2);
2934                   if (type == 0x04)
2935                     {
2936                       /* Change
2937                                 leal foo@tlsgd(,%ebx,1), %eax
2938                                 call ___tls_get_addr@PLT
2939                          into:
2940                                 movl %gs:0, %eax
2941                                 subl $foo@tpoff, %eax
2942                          (6 byte form of subl).  */
2943                       roff = rel->r_offset + 5;
2944                     }
2945                   else
2946                     {
2947                       /* Change
2948                                 leal foo@tlsgd(%ebx), %eax
2949                                 call ___tls_get_addr@PLT
2950                                 nop
2951                          or
2952                                 leal foo@tlsgd(%reg), %eax
2953                                 call *___tls_get_addr@GOT(%reg)
2954                                 which may be converted to
2955                                 addr32 call ___tls_get_addr
2956                          into:
2957                                 movl %gs:0, %eax; subl $foo@tpoff, %eax
2958                          (6 byte form of subl).  */
2959                       roff = rel->r_offset + 6;
2960                     }
2961                   memcpy (contents + roff - 8,
2962                           "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2963                   bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2964                               contents + roff);
2965                   /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X.  */
2966                   rel++;
2967                   wrel++;
2968                   continue;
2969                 }
2970               else if (r_type == R_386_TLS_GOTDESC)
2971                 {
2972                   /* GDesc -> LE transition.
2973                      It's originally something like:
2974                      leal x@tlsdesc(%ebx), %eax
2975
2976                      leal x@ntpoff, %eax
2977
2978                      Registers other than %eax may be set up here.  */
2979
2980                   unsigned int val;
2981                   bfd_vma roff;
2982
2983                   roff = rel->r_offset;
2984                   val = bfd_get_8 (input_bfd, contents + roff - 1);
2985
2986                   /* Now modify the instruction as appropriate.  */
2987                   /* aoliva FIXME: remove the above and xor the byte
2988                      below with 0x86.  */
2989                   bfd_put_8 (output_bfd, val ^ 0x86,
2990                              contents + roff - 1);
2991                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2992                               contents + roff);
2993                   continue;
2994                 }
2995               else if (r_type == R_386_TLS_DESC_CALL)
2996                 {
2997                   /* GDesc -> LE transition.
2998                      It's originally:
2999                      call *(%eax)
3000                      Turn it into:
3001                      xchg %ax,%ax  */
3002
3003                   bfd_vma roff;
3004
3005                   roff = rel->r_offset;
3006                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3007                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3008                   continue;
3009                 }
3010               else if (r_type == R_386_TLS_IE)
3011                 {
3012                   unsigned int val;
3013
3014                   /* IE->LE transition:
3015                      Originally it can be one of:
3016                      movl foo, %eax
3017                      movl foo, %reg
3018                      addl foo, %reg
3019                      We change it into:
3020                      movl $foo, %eax
3021                      movl $foo, %reg
3022                      addl $foo, %reg.  */
3023                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3024                   if (val == 0xa1)
3025                     {
3026                       /* movl foo, %eax.  */
3027                       bfd_put_8 (output_bfd, 0xb8,
3028                                  contents + rel->r_offset - 1);
3029                     }
3030                   else
3031                     {
3032                       unsigned int type;
3033
3034                       type = bfd_get_8 (input_bfd,
3035                                         contents + rel->r_offset - 2);
3036                       switch (type)
3037                         {
3038                         case 0x8b:
3039                           /* movl */
3040                           bfd_put_8 (output_bfd, 0xc7,
3041                                      contents + rel->r_offset - 2);
3042                           bfd_put_8 (output_bfd,
3043                                      0xc0 | ((val >> 3) & 7),
3044                                      contents + rel->r_offset - 1);
3045                           break;
3046                         case 0x03:
3047                           /* addl */
3048                           bfd_put_8 (output_bfd, 0x81,
3049                                      contents + rel->r_offset - 2);
3050                           bfd_put_8 (output_bfd,
3051                                      0xc0 | ((val >> 3) & 7),
3052                                      contents + rel->r_offset - 1);
3053                           break;
3054                         default:
3055                           BFD_FAIL ();
3056                           break;
3057                         }
3058                     }
3059                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3060                               contents + rel->r_offset);
3061                   continue;
3062                 }
3063               else
3064                 {
3065                   unsigned int val, type;
3066
3067                   /* {IE_32,GOTIE}->LE transition:
3068                      Originally it can be one of:
3069                      subl foo(%reg1), %reg2
3070                      movl foo(%reg1), %reg2
3071                      addl foo(%reg1), %reg2
3072                      We change it into:
3073                      subl $foo, %reg2
3074                      movl $foo, %reg2 (6 byte form)
3075                      addl $foo, %reg2.  */
3076                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3077                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3078                   if (type == 0x8b)
3079                     {
3080                       /* movl */
3081                       bfd_put_8 (output_bfd, 0xc7,
3082                                  contents + rel->r_offset - 2);
3083                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3084                                  contents + rel->r_offset - 1);
3085                     }
3086                   else if (type == 0x2b)
3087                     {
3088                       /* subl */
3089                       bfd_put_8 (output_bfd, 0x81,
3090                                  contents + rel->r_offset - 2);
3091                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3092                                  contents + rel->r_offset - 1);
3093                     }
3094                   else if (type == 0x03)
3095                     {
3096                       /* addl */
3097                       bfd_put_8 (output_bfd, 0x81,
3098                                  contents + rel->r_offset - 2);
3099                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3100                                  contents + rel->r_offset - 1);
3101                     }
3102                   else
3103                     BFD_FAIL ();
3104                   if (r_type == R_386_TLS_GOTIE)
3105                     bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3106                                 contents + rel->r_offset);
3107                   else
3108                     bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3109                                 contents + rel->r_offset);
3110                   continue;
3111                 }
3112             }
3113
3114           if (htab->elf.sgot == NULL)
3115             abort ();
3116
3117           if (h != NULL)
3118             {
3119               off = h->got.offset;
3120               offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3121             }
3122           else
3123             {
3124               if (local_got_offsets == NULL)
3125                 abort ();
3126
3127               off = local_got_offsets[r_symndx];
3128               offplt = local_tlsdesc_gotents[r_symndx];
3129             }
3130
3131           if ((off & 1) != 0)
3132             off &= ~1;
3133           else
3134             {
3135               Elf_Internal_Rela outrel;
3136               int dr_type;
3137               asection *sreloc;
3138
3139               if (htab->elf.srelgot == NULL)
3140                 abort ();
3141
3142               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3143
3144               if (GOT_TLS_GDESC_P (tls_type))
3145                 {
3146                   bfd_byte *loc;
3147                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3148                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3149                               <= htab->elf.sgotplt->size);
3150                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3151                                      + htab->elf.sgotplt->output_offset
3152                                      + offplt
3153                                      + htab->sgotplt_jump_table_size);
3154                   sreloc = htab->elf.srelplt;
3155                   loc = sreloc->contents;
3156                   loc += (htab->next_tls_desc_index++
3157                           * sizeof (Elf32_External_Rel));
3158                   BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3159                               <= sreloc->contents + sreloc->size);
3160                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3161                   if (indx == 0)
3162                     {
3163                       BFD_ASSERT (! unresolved_reloc);
3164                       bfd_put_32 (output_bfd,
3165                                   relocation - _bfd_x86_elf_dtpoff_base (info),
3166                                   htab->elf.sgotplt->contents + offplt
3167                                   + htab->sgotplt_jump_table_size + 4);
3168                     }
3169                   else
3170                     {
3171                       bfd_put_32 (output_bfd, 0,
3172                                   htab->elf.sgotplt->contents + offplt
3173                                   + htab->sgotplt_jump_table_size + 4);
3174                     }
3175                 }
3176
3177               sreloc = htab->elf.srelgot;
3178
3179               outrel.r_offset = (htab->elf.sgot->output_section->vma
3180                                  + htab->elf.sgot->output_offset + off);
3181
3182               if (GOT_TLS_GD_P (tls_type))
3183                 dr_type = R_386_TLS_DTPMOD32;
3184               else if (GOT_TLS_GDESC_P (tls_type))
3185                 goto dr_done;
3186               else if (tls_type == GOT_TLS_IE_POS)
3187                 dr_type = R_386_TLS_TPOFF;
3188               else
3189                 dr_type = R_386_TLS_TPOFF32;
3190
3191               if (dr_type == R_386_TLS_TPOFF && indx == 0)
3192                 bfd_put_32 (output_bfd,
3193                             relocation - _bfd_x86_elf_dtpoff_base (info),
3194                             htab->elf.sgot->contents + off);
3195               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3196                 bfd_put_32 (output_bfd,
3197                             _bfd_x86_elf_dtpoff_base (info) - relocation,
3198                             htab->elf.sgot->contents + off);
3199               else if (dr_type != R_386_TLS_DESC)
3200                 bfd_put_32 (output_bfd, 0,
3201                             htab->elf.sgot->contents + off);
3202               outrel.r_info = ELF32_R_INFO (indx, dr_type);
3203
3204               elf_append_rel (output_bfd, sreloc, &outrel);
3205
3206               if (GOT_TLS_GD_P (tls_type))
3207                 {
3208                   if (indx == 0)
3209                     {
3210                       BFD_ASSERT (! unresolved_reloc);
3211                       bfd_put_32 (output_bfd,
3212                                   relocation - _bfd_x86_elf_dtpoff_base (info),
3213                                   htab->elf.sgot->contents + off + 4);
3214                     }
3215                   else
3216                     {
3217                       bfd_put_32 (output_bfd, 0,
3218                                   htab->elf.sgot->contents + off + 4);
3219                       outrel.r_info = ELF32_R_INFO (indx,
3220                                                     R_386_TLS_DTPOFF32);
3221                       outrel.r_offset += 4;
3222                       elf_append_rel (output_bfd, sreloc, &outrel);
3223                     }
3224                 }
3225               else if (tls_type == GOT_TLS_IE_BOTH)
3226                 {
3227                   bfd_put_32 (output_bfd,
3228                               (indx == 0
3229                                ? relocation - _bfd_x86_elf_dtpoff_base (info)
3230                                : 0),
3231                               htab->elf.sgot->contents + off + 4);
3232                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3233                   outrel.r_offset += 4;
3234                   elf_append_rel (output_bfd, sreloc, &outrel);
3235                 }
3236
3237             dr_done:
3238               if (h != NULL)
3239                 h->got.offset |= 1;
3240               else
3241                 local_got_offsets[r_symndx] |= 1;
3242             }
3243
3244           if (off >= (bfd_vma) -2
3245               && ! GOT_TLS_GDESC_P (tls_type))
3246             abort ();
3247           if (r_type_tls == R_386_TLS_GOTDESC
3248               || r_type_tls == R_386_TLS_DESC_CALL)
3249             {
3250               relocation = htab->sgotplt_jump_table_size + offplt;
3251               unresolved_reloc = FALSE;
3252             }
3253           else if (r_type_tls == r_type)
3254             {
3255               bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3256                               + htab->elf.sgotplt->output_offset;
3257               relocation = htab->elf.sgot->output_section->vma
3258                 + htab->elf.sgot->output_offset + off - g_o_t;
3259               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3260                   && tls_type == GOT_TLS_IE_BOTH)
3261                 relocation += 4;
3262               if (r_type == R_386_TLS_IE)
3263                 relocation += g_o_t;
3264               unresolved_reloc = FALSE;
3265             }
3266           else if (r_type == R_386_TLS_GD)
3267             {
3268               unsigned int val, type;
3269               bfd_vma roff;
3270
3271               /* GD->IE transition.  */
3272               type = *(contents + rel->r_offset - 2);
3273               val = *(contents + rel->r_offset - 1);
3274               if (type == 0x04)
3275                 {
3276                   /* Change
3277                         leal foo@tlsgd(,%ebx,1), %eax
3278                         call ___tls_get_addr@PLT
3279                      into:
3280                         movl %gs:0, %eax
3281                         subl $foo@gottpoff(%ebx), %eax.  */
3282                   val >>= 3;
3283                   roff = rel->r_offset - 3;
3284                 }
3285               else
3286                 {
3287                   /* Change
3288                         leal foo@tlsgd(%ebx), %eax
3289                         call ___tls_get_addr@PLT
3290                         nop
3291                      or
3292                         leal foo@tlsgd(%reg), %eax
3293                         call *___tls_get_addr@GOT(%reg)
3294                         which may be converted to
3295                         addr32 call ___tls_get_addr
3296                      into:
3297                         movl %gs:0, %eax;
3298                         subl $foo@gottpoff(%reg), %eax.  */
3299                   roff = rel->r_offset - 2;
3300                 }
3301               memcpy (contents + roff,
3302                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3303               contents[roff + 7] = 0x80 | (val & 7);
3304               /* If foo is used only with foo@gotntpoff(%reg) and
3305                  foo@indntpoff, but not with foo@gottpoff(%reg), change
3306                  subl $foo@gottpoff(%reg), %eax
3307                  into:
3308                  addl $foo@gotntpoff(%reg), %eax.  */
3309               if (tls_type == GOT_TLS_IE_POS)
3310                 contents[roff + 6] = 0x03;
3311               bfd_put_32 (output_bfd,
3312                           htab->elf.sgot->output_section->vma
3313                           + htab->elf.sgot->output_offset + off
3314                           - htab->elf.sgotplt->output_section->vma
3315                           - htab->elf.sgotplt->output_offset,
3316                           contents + roff + 8);
3317               /* Skip R_386_PLT32 and R_386_GOT32X.  */
3318               rel++;
3319               wrel++;
3320               continue;
3321             }
3322           else if (r_type == R_386_TLS_GOTDESC)
3323             {
3324               /* GDesc -> IE transition.
3325                  It's originally something like:
3326                  leal x@tlsdesc(%ebx), %eax
3327
3328                  Change it to:
3329                  movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3330                  or:
3331                  movl x@gottpoff(%ebx), %eax # before negl %eax
3332
3333                  Registers other than %eax may be set up here.  */
3334
3335               bfd_vma roff;
3336
3337               /* First, make sure it's a leal adding ebx to a 32-bit
3338                  offset into any register, although it's probably
3339                  almost always going to be eax.  */
3340               roff = rel->r_offset;
3341
3342               /* Now modify the instruction as appropriate.  */
3343               /* To turn a leal into a movl in the form we use it, it
3344                  suffices to change the first byte from 0x8d to 0x8b.
3345                  aoliva FIXME: should we decide to keep the leal, all
3346                  we have to do is remove the statement below, and
3347                  adjust the relaxation of R_386_TLS_DESC_CALL.  */
3348               bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3349
3350               if (tls_type == GOT_TLS_IE_BOTH)
3351                 off += 4;
3352
3353               bfd_put_32 (output_bfd,
3354                           htab->elf.sgot->output_section->vma
3355                           + htab->elf.sgot->output_offset + off
3356                           - htab->elf.sgotplt->output_section->vma
3357                           - htab->elf.sgotplt->output_offset,
3358                           contents + roff);
3359               continue;
3360             }
3361           else if (r_type == R_386_TLS_DESC_CALL)
3362             {
3363               /* GDesc -> IE transition.
3364                  It's originally:
3365                  call *(%eax)
3366
3367                  Change it to:
3368                  xchg %ax,%ax
3369                  or
3370                  negl %eax
3371                  depending on how we transformed the TLS_GOTDESC above.
3372               */
3373
3374               bfd_vma roff;
3375
3376               roff = rel->r_offset;
3377
3378               /* Now modify the instruction as appropriate.  */
3379               if (tls_type != GOT_TLS_IE_NEG)
3380                 {
3381                   /* xchg %ax,%ax */
3382                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3383                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3384                 }
3385               else
3386                 {
3387                   /* negl %eax */
3388                   bfd_put_8 (output_bfd, 0xf7, contents + roff);
3389                   bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3390                 }
3391
3392               continue;
3393             }
3394           else
3395             BFD_ASSERT (FALSE);
3396           break;
3397
3398         case R_386_TLS_LDM:
3399           if (! elf_i386_tls_transition (info, input_bfd,
3400                                          input_section, contents,
3401                                          symtab_hdr, sym_hashes,
3402                                          &r_type, GOT_UNKNOWN, rel,
3403                                          relend, h, r_symndx, TRUE))
3404             return FALSE;
3405
3406           if (r_type != R_386_TLS_LDM)
3407             {
3408               /* LD->LE transition.  Change
3409                         leal foo@tlsldm(%ebx) %eax
3410                         call ___tls_get_addr@PLT
3411                  into:
3412                         movl %gs:0, %eax
3413                         nop
3414                         leal 0(%esi,1), %esi
3415                  or change
3416                         leal foo@tlsldm(%reg) %eax
3417                         call *___tls_get_addr@GOT(%reg)
3418                         which may be converted to
3419                         addr32 call ___tls_get_addr
3420                  into:
3421                         movl %gs:0, %eax
3422                         leal 0(%esi), %esi  */
3423               BFD_ASSERT (r_type == R_386_TLS_LE_32);
3424               if (*(contents + rel->r_offset + 4) == 0xff
3425                   || *(contents + rel->r_offset + 4) == 0x67)
3426                 memcpy (contents + rel->r_offset - 2,
3427                         "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3428               else
3429                 memcpy (contents + rel->r_offset - 2,
3430                         "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3431               /* Skip R_386_PC32/R_386_PLT32.  */
3432               rel++;
3433               wrel++;
3434               continue;
3435             }
3436
3437           if (htab->elf.sgot == NULL)
3438             abort ();
3439
3440           off = htab->tls_ld_or_ldm_got.offset;
3441           if (off & 1)
3442             off &= ~1;
3443           else
3444             {
3445               Elf_Internal_Rela outrel;
3446
3447               if (htab->elf.srelgot == NULL)
3448                 abort ();
3449
3450               outrel.r_offset = (htab->elf.sgot->output_section->vma
3451                                  + htab->elf.sgot->output_offset + off);
3452
3453               bfd_put_32 (output_bfd, 0,
3454                           htab->elf.sgot->contents + off);
3455               bfd_put_32 (output_bfd, 0,
3456                           htab->elf.sgot->contents + off + 4);
3457               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3458               elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3459               htab->tls_ld_or_ldm_got.offset |= 1;
3460             }
3461           relocation = htab->elf.sgot->output_section->vma
3462                        + htab->elf.sgot->output_offset + off
3463                        - htab->elf.sgotplt->output_section->vma
3464                        - htab->elf.sgotplt->output_offset;
3465           unresolved_reloc = FALSE;
3466           break;
3467
3468         case R_386_TLS_LDO_32:
3469           if (!bfd_link_executable (info)
3470               || (input_section->flags & SEC_CODE) == 0)
3471             relocation -= _bfd_x86_elf_dtpoff_base (info);
3472           else
3473             /* When converting LDO to LE, we must negate.  */
3474             relocation = -elf_i386_tpoff (info, relocation);
3475           break;
3476
3477         case R_386_TLS_LE_32:
3478         case R_386_TLS_LE:
3479           if (!bfd_link_executable (info))
3480             {
3481               Elf_Internal_Rela outrel;
3482               asection *sreloc;
3483
3484               outrel.r_offset = rel->r_offset
3485                                 + input_section->output_section->vma
3486                                 + input_section->output_offset;
3487               if (h != NULL && h->dynindx != -1)
3488                 indx = h->dynindx;
3489               else
3490                 indx = 0;
3491               if (r_type == R_386_TLS_LE_32)
3492                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3493               else
3494                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3495               sreloc = elf_section_data (input_section)->sreloc;
3496               if (sreloc == NULL)
3497                 abort ();
3498               elf_append_rel (output_bfd, sreloc, &outrel);
3499               if (indx)
3500                 continue;
3501               else if (r_type == R_386_TLS_LE_32)
3502                 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3503               else
3504                 relocation -= _bfd_x86_elf_dtpoff_base (info);
3505             }
3506           else if (r_type == R_386_TLS_LE_32)
3507             relocation = elf_i386_tpoff (info, relocation);
3508           else
3509             relocation = -elf_i386_tpoff (info, relocation);
3510           break;
3511
3512         default:
3513           break;
3514         }
3515
3516       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3517          because such sections are not SEC_ALLOC and thus ld.so will
3518          not process them.  */
3519       if (unresolved_reloc
3520           && !((input_section->flags & SEC_DEBUGGING) != 0
3521                && h->def_dynamic)
3522           && _bfd_elf_section_offset (output_bfd, info, input_section,
3523                                       rel->r_offset) != (bfd_vma) -1)
3524         {
3525           _bfd_error_handler
3526             /* xgettext:c-format */
3527             (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
3528              input_bfd,
3529              input_section,
3530              rel->r_offset,
3531              howto->name,
3532              h->root.root.string);
3533           return FALSE;
3534         }
3535
3536 do_relocation:
3537       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3538                                     contents, rel->r_offset,
3539                                     relocation, 0);
3540
3541 check_relocation_error:
3542       if (r != bfd_reloc_ok)
3543         {
3544           const char *name;
3545
3546           if (h != NULL)
3547             name = h->root.root.string;
3548           else
3549             {
3550               name = bfd_elf_string_from_elf_section (input_bfd,
3551                                                       symtab_hdr->sh_link,
3552                                                       sym->st_name);
3553               if (name == NULL)
3554                 return FALSE;
3555               if (*name == '\0')
3556                 name = bfd_section_name (input_bfd, sec);
3557             }
3558
3559           if (r == bfd_reloc_overflow)
3560             (*info->callbacks->reloc_overflow)
3561               (info, (h ? &h->root : NULL), name, howto->name,
3562                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3563           else
3564             {
3565               _bfd_error_handler
3566                 /* xgettext:c-format */
3567                 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
3568                  input_bfd, input_section,
3569                  rel->r_offset, name, (int) r);
3570               return FALSE;
3571             }
3572         }
3573
3574       if (wrel != rel)
3575         *wrel = *rel;
3576     }
3577
3578   if (wrel != rel)
3579     {
3580       Elf_Internal_Shdr *rel_hdr;
3581       size_t deleted = rel - wrel;
3582
3583       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3584       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3585       if (rel_hdr->sh_size == 0)
3586         {
3587           /* It is too late to remove an empty reloc section.  Leave
3588              one NONE reloc.
3589              ??? What is wrong with an empty section???  */
3590           rel_hdr->sh_size = rel_hdr->sh_entsize;
3591           deleted -= 1;
3592         }
3593       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3594       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3595       input_section->reloc_count -= deleted;
3596     }
3597
3598   return TRUE;
3599 }
3600
3601 /* Finish up dynamic symbol handling.  We set the contents of various
3602    dynamic sections here.  */
3603
3604 static bfd_boolean
3605 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3606                                 struct bfd_link_info *info,
3607                                 struct elf_link_hash_entry *h,
3608                                 Elf_Internal_Sym *sym)
3609 {
3610   struct elf_x86_link_hash_table *htab;
3611   unsigned plt_entry_size;
3612   struct elf_x86_link_hash_entry *eh;
3613   bfd_boolean local_undefweak;
3614   bfd_boolean use_plt_second;
3615
3616   htab = elf_x86_hash_table (info, I386_ELF_DATA);
3617   if (htab == NULL)
3618     return FALSE;
3619
3620   plt_entry_size = htab->plt.plt_entry_size;
3621
3622   /* Use the second PLT section only if there is .plt section.  */
3623   use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3624
3625   eh = (struct elf_x86_link_hash_entry *) h;
3626   if (eh->no_finish_dynamic_symbol)
3627     abort ();
3628
3629   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3630      resolved undefined weak symbols in executable so that their
3631      references have value 0 at run-time.  */
3632   local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3633
3634   if (h->plt.offset != (bfd_vma) -1)
3635     {
3636       bfd_vma plt_index, plt_offset;
3637       bfd_vma got_offset;
3638       Elf_Internal_Rela rel;
3639       bfd_byte *loc;
3640       asection *plt, *resolved_plt, *gotplt, *relplt;
3641
3642       /* When building a static executable, use .iplt, .igot.plt and
3643          .rel.iplt sections for STT_GNU_IFUNC symbols.  */
3644       if (htab->elf.splt != NULL)
3645         {
3646           plt = htab->elf.splt;
3647           gotplt = htab->elf.sgotplt;
3648           relplt = htab->elf.srelplt;
3649         }
3650       else
3651         {
3652           plt = htab->elf.iplt;
3653           gotplt = htab->elf.igotplt;
3654           relplt = htab->elf.irelplt;
3655         }
3656
3657       /* This symbol has an entry in the procedure linkage table.  Set
3658          it up.  */
3659
3660       if ((h->dynindx == -1
3661            && !local_undefweak
3662            && !((h->forced_local || bfd_link_executable (info))
3663                 && h->def_regular
3664                 && h->type == STT_GNU_IFUNC))
3665           || plt == NULL
3666           || gotplt == NULL
3667           || relplt == NULL)
3668         abort ();
3669
3670       /* Get the index in the procedure linkage table which
3671          corresponds to this symbol.  This is the index of this symbol
3672          in all the symbols for which we are making plt entries.  The
3673          first entry in the procedure linkage table is reserved.
3674
3675          Get the offset into the .got table of the entry that
3676          corresponds to this function.  Each .got entry is 4 bytes.
3677          The first three are reserved.
3678
3679          For static executables, we don't reserve anything.  */
3680
3681       if (plt == htab->elf.splt)
3682         {
3683           got_offset = (h->plt.offset / plt_entry_size
3684                         - htab->plt.has_plt0);
3685           got_offset = (got_offset + 3) * 4;
3686         }
3687       else
3688         {
3689           got_offset = h->plt.offset / plt_entry_size;
3690           got_offset = got_offset * 4;
3691         }
3692
3693       /* Fill in the entry in the procedure linkage table and update
3694          the first slot.  */
3695       memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3696               plt_entry_size);
3697
3698       if (use_plt_second)
3699         {
3700           const bfd_byte *plt_entry;
3701           if (bfd_link_pic (info))
3702             plt_entry = htab->non_lazy_plt->pic_plt_entry;
3703           else
3704             plt_entry = htab->non_lazy_plt->plt_entry;
3705           memcpy (htab->plt_second->contents + eh->plt_second.offset,
3706                   plt_entry, htab->non_lazy_plt->plt_entry_size);
3707
3708           resolved_plt = htab->plt_second;
3709           plt_offset = eh->plt_second.offset;
3710         }
3711       else
3712         {
3713           resolved_plt = plt;
3714           plt_offset = h->plt.offset;
3715         }
3716
3717       if (! bfd_link_pic (info))
3718         {
3719           bfd_put_32 (output_bfd,
3720                       (gotplt->output_section->vma
3721                        + gotplt->output_offset
3722                        + got_offset),
3723                       resolved_plt->contents + plt_offset
3724                       + htab->plt.plt_got_offset);
3725
3726           if (htab->is_vxworks)
3727             {
3728               int s, k, reloc_index;
3729
3730               /* Create the R_386_32 relocation referencing the GOT
3731                  for this PLT entry.  */
3732
3733               /* S: Current slot number (zero-based).  */
3734               s = ((h->plt.offset - htab->plt.plt_entry_size)
3735                    / htab->plt.plt_entry_size);
3736               /* K: Number of relocations for PLTResolve. */
3737               if (bfd_link_pic (info))
3738                 k = PLTRESOLVE_RELOCS_SHLIB;
3739               else
3740                 k = PLTRESOLVE_RELOCS;
3741               /* Skip the PLTresolve relocations, and the relocations for
3742                  the other PLT slots. */
3743               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3744               loc = (htab->srelplt2->contents + reloc_index
3745                      * sizeof (Elf32_External_Rel));
3746
3747               rel.r_offset = (plt->output_section->vma
3748                               + plt->output_offset
3749                               + h->plt.offset + 2),
3750               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3751               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3752
3753               /* Create the R_386_32 relocation referencing the beginning of
3754                  the PLT for this GOT entry.  */
3755               rel.r_offset = (htab->elf.sgotplt->output_section->vma
3756                               + htab->elf.sgotplt->output_offset
3757                               + got_offset);
3758               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3759               bfd_elf32_swap_reloc_out (output_bfd, &rel,
3760                                         loc + sizeof (Elf32_External_Rel));
3761             }
3762         }
3763       else
3764         {
3765           bfd_put_32 (output_bfd, got_offset,
3766                       resolved_plt->contents + plt_offset
3767                       + htab->plt.plt_got_offset);
3768         }
3769
3770       /* Fill in the entry in the global offset table.  Leave the entry
3771          as zero for undefined weak symbol in PIE.  No PLT relocation
3772          against undefined weak symbol in PIE.  */
3773       if (!local_undefweak)
3774         {
3775           if (htab->plt.has_plt0)
3776             bfd_put_32 (output_bfd,
3777                         (plt->output_section->vma
3778                          + plt->output_offset
3779                          + h->plt.offset
3780                          + htab->lazy_plt->plt_lazy_offset),
3781                         gotplt->contents + got_offset);
3782
3783           /* Fill in the entry in the .rel.plt section.  */
3784           rel.r_offset = (gotplt->output_section->vma
3785                           + gotplt->output_offset
3786                           + got_offset);
3787           if (h->dynindx == -1
3788               || ((bfd_link_executable (info)
3789                    || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3790                   && h->def_regular
3791                   && h->type == STT_GNU_IFUNC))
3792             {
3793               info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3794                                       h->root.root.string,
3795                                       h->root.u.def.section->owner);
3796
3797               /* If an STT_GNU_IFUNC symbol is locally defined, generate
3798                  R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
3799                  in the .got.plt section.  */
3800               bfd_put_32 (output_bfd,
3801                           (h->root.u.def.value
3802                            + h->root.u.def.section->output_section->vma
3803                            + h->root.u.def.section->output_offset),
3804                           gotplt->contents + got_offset);
3805               rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3806               /* R_386_IRELATIVE comes last.  */
3807               plt_index = htab->next_irelative_index--;
3808             }
3809           else
3810             {
3811               rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3812               plt_index = htab->next_jump_slot_index++;
3813             }
3814
3815           loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3816           bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3817
3818           /* Don't fill the second and third slots in PLT entry for
3819              static executables nor without PLT0.  */
3820           if (plt == htab->elf.splt && htab->plt.has_plt0)
3821             {
3822               bfd_put_32 (output_bfd,
3823                           plt_index * sizeof (Elf32_External_Rel),
3824                           plt->contents + h->plt.offset
3825                           + htab->lazy_plt->plt_reloc_offset);
3826               bfd_put_32 (output_bfd,
3827                           - (h->plt.offset
3828                              + htab->lazy_plt->plt_plt_offset + 4),
3829                           (plt->contents + h->plt.offset
3830                            + htab->lazy_plt->plt_plt_offset));
3831             }
3832         }
3833     }
3834   else if (eh->plt_got.offset != (bfd_vma) -1)
3835     {
3836       bfd_vma got_offset, plt_offset;
3837       asection *plt, *got, *gotplt;
3838       const bfd_byte *got_plt_entry;
3839
3840       /* Set the entry in the GOT procedure linkage table.  */
3841       plt = htab->plt_got;
3842       got = htab->elf.sgot;
3843       gotplt = htab->elf.sgotplt;
3844       got_offset = h->got.offset;
3845
3846       if (got_offset == (bfd_vma) -1
3847           || plt == NULL
3848           || got == NULL
3849           || gotplt == NULL)
3850         abort ();
3851
3852       /* Fill in the entry in the GOT procedure linkage table.  */
3853       if (! bfd_link_pic (info))
3854         {
3855           got_plt_entry = htab->non_lazy_plt->plt_entry;
3856           got_offset += got->output_section->vma + got->output_offset;
3857         }
3858       else
3859         {
3860           got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3861           got_offset += (got->output_section->vma
3862                          + got->output_offset
3863                          - gotplt->output_section->vma
3864                          - gotplt->output_offset);
3865         }
3866
3867       plt_offset = eh->plt_got.offset;
3868       memcpy (plt->contents + plt_offset, got_plt_entry,
3869               htab->non_lazy_plt->plt_entry_size);
3870       bfd_put_32 (output_bfd, got_offset,
3871                   (plt->contents + plt_offset
3872                    + htab->non_lazy_plt->plt_got_offset));
3873     }
3874
3875   if (!local_undefweak
3876       && !h->def_regular
3877       && (h->plt.offset != (bfd_vma) -1
3878           || eh->plt_got.offset != (bfd_vma) -1))
3879     {
3880       /* Mark the symbol as undefined, rather than as defined in
3881          the .plt section.  Leave the value if there were any
3882          relocations where pointer equality matters (this is a clue
3883          for the dynamic linker, to make function pointer
3884          comparisons work between an application and shared
3885          library), otherwise set it to zero.  If a function is only
3886          called from a binary, there is no need to slow down
3887          shared libraries because of that.  */
3888       sym->st_shndx = SHN_UNDEF;
3889       if (!h->pointer_equality_needed)
3890         sym->st_value = 0;
3891     }
3892
3893   /* Don't generate dynamic GOT relocation against undefined weak
3894      symbol in executable.  */
3895   if (h->got.offset != (bfd_vma) -1
3896       && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3897       && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3898       && !local_undefweak)
3899     {
3900       Elf_Internal_Rela rel;
3901       asection *relgot = htab->elf.srelgot;
3902
3903       /* This symbol has an entry in the global offset table.  Set it
3904          up.  */
3905
3906       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3907         abort ();
3908
3909       rel.r_offset = (htab->elf.sgot->output_section->vma
3910                       + htab->elf.sgot->output_offset
3911                       + (h->got.offset & ~(bfd_vma) 1));
3912
3913       /* If this is a static link, or it is a -Bsymbolic link and the
3914          symbol is defined locally or was forced to be local because
3915          of a version file, we just want to emit a RELATIVE reloc.
3916          The entry in the global offset table will already have been
3917          initialized in the relocate_section function.  */
3918       if (h->def_regular
3919           && h->type == STT_GNU_IFUNC)
3920         {
3921           if (h->plt.offset == (bfd_vma) -1)
3922             {
3923               /* STT_GNU_IFUNC is referenced without PLT.  */
3924               if (htab->elf.splt == NULL)
3925                 {
3926                   /* use .rel[a].iplt section to store .got relocations
3927                      in static executable.  */
3928                   relgot = htab->elf.irelplt;
3929                 }
3930               if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3931                 {
3932                   info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3933                                           h->root.root.string,
3934                                           h->root.u.def.section->owner);
3935
3936                   bfd_put_32 (output_bfd,
3937                               (h->root.u.def.value
3938                                + h->root.u.def.section->output_section->vma
3939                                + h->root.u.def.section->output_offset),
3940                               htab->elf.sgot->contents + h->got.offset);
3941                   rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3942                 }
3943               else
3944                 goto do_glob_dat;
3945             }
3946           else if (bfd_link_pic (info))
3947             {
3948               /* Generate R_386_GLOB_DAT.  */
3949               goto do_glob_dat;
3950             }
3951           else
3952             {
3953               asection *plt;
3954               bfd_vma plt_offset;
3955
3956               if (!h->pointer_equality_needed)
3957                 abort ();
3958
3959               /* For non-shared object, we can't use .got.plt, which
3960                  contains the real function addres if we need pointer
3961                  equality.  We load the GOT entry with the PLT entry.  */
3962               if (htab->plt_second != NULL)
3963                 {
3964                   plt = htab->plt_second;
3965                   plt_offset = eh->plt_second.offset;
3966                 }
3967               else
3968                 {
3969                   plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3970                   plt_offset = h->plt.offset;
3971                 }
3972               bfd_put_32 (output_bfd,
3973                           (plt->output_section->vma
3974                            + plt->output_offset + plt_offset),
3975                           htab->elf.sgot->contents + h->got.offset);
3976               return TRUE;
3977             }
3978         }
3979       else if (bfd_link_pic (info)
3980                && SYMBOL_REFERENCES_LOCAL_P (info, h))
3981         {
3982           BFD_ASSERT((h->got.offset & 1) != 0);
3983           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3984         }
3985       else
3986         {
3987           BFD_ASSERT((h->got.offset & 1) == 0);
3988 do_glob_dat:
3989           bfd_put_32 (output_bfd, (bfd_vma) 0,
3990                       htab->elf.sgot->contents + h->got.offset);
3991           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3992         }
3993
3994       elf_append_rel (output_bfd, relgot, &rel);
3995     }
3996
3997   if (h->needs_copy)
3998     {
3999       Elf_Internal_Rela rel;
4000       asection *s;
4001
4002       /* This symbol needs a copy reloc.  Set it up.  */
4003
4004       if (h->dynindx == -1
4005           || (h->root.type != bfd_link_hash_defined
4006               && h->root.type != bfd_link_hash_defweak)
4007           || htab->elf.srelbss == NULL
4008           || htab->elf.sreldynrelro == NULL)
4009         abort ();
4010
4011       rel.r_offset = (h->root.u.def.value
4012                       + h->root.u.def.section->output_section->vma
4013                       + h->root.u.def.section->output_offset);
4014       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4015       if (h->root.u.def.section == htab->elf.sdynrelro)
4016         s = htab->elf.sreldynrelro;
4017       else
4018         s = htab->elf.srelbss;
4019       elf_append_rel (output_bfd, s, &rel);
4020     }
4021
4022   return TRUE;
4023 }
4024
4025 /* Finish up local dynamic symbol handling.  We set the contents of
4026    various dynamic sections here.  */
4027
4028 static bfd_boolean
4029 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4030 {
4031   struct elf_link_hash_entry *h
4032     = (struct elf_link_hash_entry *) *slot;
4033   struct bfd_link_info *info
4034     = (struct bfd_link_info *) inf;
4035
4036   return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4037                                          h, NULL);
4038 }
4039
4040 /* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
4041    here since undefined weak symbol may not be dynamic and may not be
4042    called for elf_i386_finish_dynamic_symbol.  */
4043
4044 static bfd_boolean
4045 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4046                                       void *inf)
4047 {
4048   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4049   struct bfd_link_info *info = (struct bfd_link_info *) inf;
4050
4051   if (h->root.type != bfd_link_hash_undefweak
4052       || h->dynindx != -1)
4053     return TRUE;
4054
4055   return elf_i386_finish_dynamic_symbol (info->output_bfd,
4056                                          info, h, NULL);
4057 }
4058
4059 /* Used to decide how to sort relocs in an optimal manner for the
4060    dynamic linker, before writing them out.  */
4061
4062 static enum elf_reloc_type_class
4063 elf_i386_reloc_type_class (const struct bfd_link_info *info,
4064                            const asection *rel_sec ATTRIBUTE_UNUSED,
4065                            const Elf_Internal_Rela *rela)
4066 {
4067   bfd *abfd = info->output_bfd;
4068   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4069   struct elf_link_hash_table *htab = elf_hash_table (info);
4070
4071   if (htab->dynsym != NULL
4072       && htab->dynsym->contents != NULL)
4073     {
4074       /* Check relocation against STT_GNU_IFUNC symbol if there are
4075          dynamic symbols.  */
4076       unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4077       if (r_symndx != STN_UNDEF)
4078         {
4079           Elf_Internal_Sym sym;
4080           if (!bed->s->swap_symbol_in (abfd,
4081                                        (htab->dynsym->contents
4082                                         + r_symndx * sizeof (Elf32_External_Sym)),
4083                                        0, &sym))
4084             abort ();
4085
4086           if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4087             return reloc_class_ifunc;
4088         }
4089     }
4090
4091   switch (ELF32_R_TYPE (rela->r_info))
4092     {
4093     case R_386_IRELATIVE:
4094       return reloc_class_ifunc;
4095     case R_386_RELATIVE:
4096       return reloc_class_relative;
4097     case R_386_JUMP_SLOT:
4098       return reloc_class_plt;
4099     case R_386_COPY:
4100       return reloc_class_copy;
4101     default:
4102       return reloc_class_normal;
4103     }
4104 }
4105
4106 /* Finish up the dynamic sections.  */
4107
4108 static bfd_boolean
4109 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4110                                   struct bfd_link_info *info)
4111 {
4112   struct elf_x86_link_hash_table *htab;
4113   bfd *dynobj;
4114   asection *sdyn;
4115   const struct elf_i386_backend_data *abed;
4116
4117   htab = elf_x86_hash_table (info, I386_ELF_DATA);
4118   if (htab == NULL)
4119     return FALSE;
4120
4121   dynobj = htab->elf.dynobj;
4122   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4123   abed = get_elf_i386_backend_data (output_bfd);
4124
4125   if (htab->elf.dynamic_sections_created)
4126     {
4127       Elf32_External_Dyn *dyncon, *dynconend;
4128
4129       if (sdyn == NULL || htab->elf.sgot == NULL)
4130         abort ();
4131
4132       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4133       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4134       for (; dyncon < dynconend; dyncon++)
4135         {
4136           Elf_Internal_Dyn dyn;
4137           asection *s;
4138
4139           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4140
4141           switch (dyn.d_tag)
4142             {
4143             default:
4144               if (htab->is_vxworks
4145                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4146                 break;
4147               continue;
4148
4149             case DT_PLTGOT:
4150               s = htab->elf.sgotplt;
4151               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4152               break;
4153
4154             case DT_JMPREL:
4155               s = htab->elf.srelplt;
4156               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4157               break;
4158
4159             case DT_PLTRELSZ:
4160               s = htab->elf.srelplt;
4161               dyn.d_un.d_val = s->size;
4162               break;
4163             }
4164
4165           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4166         }
4167
4168       if (htab->elf.splt && htab->elf.splt->size > 0)
4169         {
4170           /* UnixWare sets the entsize of .plt to 4, although that doesn't
4171              really seem like the right value.  */
4172           elf_section_data (htab->elf.splt->output_section)
4173             ->this_hdr.sh_entsize = 4;
4174
4175           if (htab->plt.has_plt0)
4176             {
4177               /* Fill in the special first entry in the procedure linkage
4178                  table.  */
4179               memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4180                       htab->lazy_plt->plt0_entry_size);
4181               memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4182                       abed->plt0_pad_byte,
4183                       htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4184               if (!bfd_link_pic (info))
4185                 {
4186                   bfd_put_32 (output_bfd,
4187                               (htab->elf.sgotplt->output_section->vma
4188                                + htab->elf.sgotplt->output_offset
4189                                + 4),
4190                               htab->elf.splt->contents
4191                               + htab->lazy_plt->plt0_got1_offset);
4192                   bfd_put_32 (output_bfd,
4193                               (htab->elf.sgotplt->output_section->vma
4194                                + htab->elf.sgotplt->output_offset
4195                                + 8),
4196                               htab->elf.splt->contents
4197                               + htab->lazy_plt->plt0_got2_offset);
4198
4199                   if (htab->is_vxworks)
4200                     {
4201                       Elf_Internal_Rela rel;
4202                       int num_plts = (htab->elf.splt->size
4203                                       / htab->plt.plt_entry_size) - 1;
4204                       unsigned char *p;
4205                       asection *srelplt2 = htab->srelplt2;
4206
4207                       /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4208                          + 4.  On IA32 we use REL relocations so the
4209                          addend goes in the PLT directly.  */
4210                       rel.r_offset = (htab->elf.splt->output_section->vma
4211                                       + htab->elf.splt->output_offset
4212                                       + htab->lazy_plt->plt0_got1_offset);
4213                       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4214                                                  R_386_32);
4215                       bfd_elf32_swap_reloc_out (output_bfd, &rel,
4216                                                 srelplt2->contents);
4217                       /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4218                          + 8.  */
4219                       rel.r_offset = (htab->elf.splt->output_section->vma
4220                                       + htab->elf.splt->output_offset
4221                                       + htab->lazy_plt->plt0_got2_offset);
4222                       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4223                                                  R_386_32);
4224                       bfd_elf32_swap_reloc_out (output_bfd, &rel,
4225                                                 srelplt2->contents +
4226                                                 sizeof (Elf32_External_Rel));
4227                       /* Correct the .rel.plt.unloaded relocations.  */
4228                       p = srelplt2->contents;
4229                       if (bfd_link_pic (info))
4230                         p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4231                       else
4232                         p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4233
4234                       for (; num_plts; num_plts--)
4235                         {
4236                           bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4237                           rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4238                                                      R_386_32);
4239                           bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4240                           p += sizeof (Elf32_External_Rel);
4241
4242                           bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4243                           rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4244                                                      R_386_32);
4245                           bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4246                           p += sizeof (Elf32_External_Rel);
4247                         }
4248                     }
4249                 }
4250             }
4251         }
4252
4253       if (htab->plt_got != NULL && htab->plt_got->size > 0)
4254         elf_section_data (htab->plt_got->output_section)
4255           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4256
4257       if (htab->plt_second != NULL && htab->plt_second->size > 0)
4258         elf_section_data (htab->plt_second->output_section)
4259           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4260     }
4261
4262   /* Fill in the first three entries in the global offset table.  */
4263   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
4264     {
4265       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4266         {
4267           _bfd_error_handler
4268             (_("discarded output section: `%A'"), htab->elf.sgotplt);
4269           return FALSE;
4270         }
4271
4272       bfd_put_32 (output_bfd,
4273                   (sdyn == NULL ? 0
4274                    : sdyn->output_section->vma + sdyn->output_offset),
4275                   htab->elf.sgotplt->contents);
4276       bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4277       bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4278
4279       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4280     }
4281
4282   /* Adjust .eh_frame for .plt section.  */
4283   if (htab->plt_eh_frame != NULL
4284       && htab->plt_eh_frame->contents != NULL)
4285     {
4286       if (htab->elf.splt != NULL
4287           && htab->elf.splt->size != 0
4288           && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4289           && htab->elf.splt->output_section != NULL
4290           && htab->plt_eh_frame->output_section != NULL)
4291         {
4292           bfd_vma plt_start = htab->elf.splt->output_section->vma;
4293           bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4294                                    + htab->plt_eh_frame->output_offset
4295                                    + PLT_FDE_START_OFFSET;
4296           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4297                              htab->plt_eh_frame->contents
4298                              + PLT_FDE_START_OFFSET);
4299         }
4300       if (htab->plt_eh_frame->sec_info_type
4301           == SEC_INFO_TYPE_EH_FRAME)
4302         {
4303           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4304                                                  htab->plt_eh_frame,
4305                                                  htab->plt_eh_frame->contents))
4306             return FALSE;
4307         }
4308     }
4309
4310   /* Adjust .eh_frame for .plt.got section.  */
4311   if (htab->plt_got_eh_frame != NULL
4312       && htab->plt_got_eh_frame->contents != NULL)
4313     {
4314       if (htab->plt_got != NULL
4315           && htab->plt_got->size != 0
4316           && (htab->plt_got->flags & SEC_EXCLUDE) == 0
4317           && htab->plt_got->output_section != NULL
4318           && htab->plt_got_eh_frame->output_section != NULL)
4319         {
4320           bfd_vma plt_start = htab->plt_got->output_section->vma;
4321           bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
4322                                    + htab->plt_got_eh_frame->output_offset
4323                                    + PLT_FDE_START_OFFSET;
4324           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4325                              htab->plt_got_eh_frame->contents
4326                              + PLT_FDE_START_OFFSET);
4327         }
4328       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4329         {
4330           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4331                                                  htab->plt_got_eh_frame,
4332                                                  htab->plt_got_eh_frame->contents))
4333             return FALSE;
4334         }
4335     }
4336
4337   /* Adjust .eh_frame for the second PLT section.  */
4338   if (htab->plt_second_eh_frame != NULL
4339       && htab->plt_second_eh_frame->contents != NULL)
4340     {
4341       if (htab->plt_second != NULL
4342           && htab->plt_second->size != 0
4343           && (htab->plt_second->flags & SEC_EXCLUDE) == 0
4344           && htab->plt_second->output_section != NULL
4345           && htab->plt_second_eh_frame->output_section != NULL)
4346         {
4347           bfd_vma plt_start = htab->plt_second->output_section->vma;
4348           bfd_vma eh_frame_start
4349             = (htab->plt_second_eh_frame->output_section->vma
4350                + htab->plt_second_eh_frame->output_offset
4351                + PLT_FDE_START_OFFSET);
4352           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4353                              htab->plt_second_eh_frame->contents
4354                              + PLT_FDE_START_OFFSET);
4355         }
4356       if (htab->plt_second_eh_frame->sec_info_type
4357           == SEC_INFO_TYPE_EH_FRAME)
4358         {
4359           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4360                                                  htab->plt_second_eh_frame,
4361                                                  htab->plt_second_eh_frame->contents))
4362             return FALSE;
4363         }
4364     }
4365
4366   if (htab->elf.sgot && htab->elf.sgot->size > 0)
4367     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4368
4369   /* Fill PLT entries for undefined weak symbols in PIE.  */
4370   if (bfd_link_pie (info))
4371     bfd_hash_traverse (&info->hash->table,
4372                        elf_i386_pie_finish_undefweak_symbol,
4373                        info);
4374
4375   return TRUE;
4376 }
4377
4378 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4379    STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4380    It has to be done before elf_link_sort_relocs is called so that
4381    dynamic relocations are properly sorted.  */
4382
4383 static bfd_boolean
4384 elf_i386_output_arch_local_syms
4385   (bfd *output_bfd ATTRIBUTE_UNUSED,
4386    struct bfd_link_info *info,
4387    void *flaginfo ATTRIBUTE_UNUSED,
4388    int (*func) (void *, const char *,
4389                 Elf_Internal_Sym *,
4390                 asection *,
4391                 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4392 {
4393   struct elf_x86_link_hash_table *htab
4394     = elf_x86_hash_table (info, I386_ELF_DATA);
4395   if (htab == NULL)
4396     return FALSE;
4397
4398   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4399   htab_traverse (htab->loc_hash_table,
4400                  elf_i386_finish_local_dynamic_symbol,
4401                  info);
4402
4403   return TRUE;
4404 }
4405
4406 /* Forward declaration.  */
4407 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
4408
4409 /* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
4410    dynamic relocations.   */
4411
4412 static long
4413 elf_i386_get_synthetic_symtab (bfd *abfd,
4414                                long symcount ATTRIBUTE_UNUSED,
4415                                asymbol **syms ATTRIBUTE_UNUSED,
4416                                long dynsymcount,
4417                                asymbol **dynsyms,
4418                                asymbol **ret)
4419 {
4420   long count, i, n;
4421   int j;
4422   bfd_byte *plt_contents;
4423   long relsize;
4424   const struct elf_x86_lazy_plt_layout *lazy_plt;
4425   const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4426   const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4427   const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4428   asection *plt;
4429   bfd_vma got_addr;
4430   enum elf_x86_plt_type plt_type;
4431   struct elf_x86_plt plts[] =
4432     {
4433       { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4434       { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4435       { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4436       { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4437     };
4438
4439   *ret = NULL;
4440
4441   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4442     return 0;
4443
4444   if (dynsymcount <= 0)
4445     return 0;
4446
4447   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4448   if (relsize <= 0)
4449     return -1;
4450
4451   non_lazy_plt = NULL;
4452   /* Silence GCC 6.  */
4453   lazy_plt = NULL;
4454   non_lazy_ibt_plt = NULL;
4455   lazy_ibt_plt = NULL;
4456   switch (get_elf_i386_backend_data (abfd)->os)
4457     {
4458     case is_normal:
4459       non_lazy_plt = &elf_i386_non_lazy_plt;
4460       lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4461       non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4462       /* Fall through */
4463     case is_vxworks:
4464       lazy_plt = &elf_i386_lazy_plt;
4465       break;
4466     case is_nacl:
4467       lazy_plt = &elf_i386_nacl_plt;
4468       break;
4469     }
4470
4471   got_addr = 0;
4472
4473   count = 0;
4474   for (j = 0; plts[j].name != NULL; j++)
4475     {
4476       plt = bfd_get_section_by_name (abfd, plts[j].name);
4477       if (plt == NULL || plt->size == 0)
4478         continue;
4479
4480       /* Get the PLT section contents.  */
4481       plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4482       if (plt_contents == NULL)
4483         break;
4484       if (!bfd_get_section_contents (abfd, (asection *) plt,
4485                                      plt_contents, 0, plt->size))
4486         {
4487           free (plt_contents);
4488           break;
4489         }
4490
4491       /* Check what kind of PLT it is.  */
4492       plt_type = plt_unknown;
4493       if (plts[j].type == plt_unknown
4494           && (plt->size >= (lazy_plt->plt0_entry_size
4495                             + lazy_plt->plt_entry_size)))
4496         {
4497           /* Match lazy PLT first.  */
4498           if (memcmp (plt_contents, lazy_plt->plt0_entry,
4499                       lazy_plt->plt0_got1_offset) == 0)
4500             {
4501               /* The fist entry in the lazy IBT PLT is the same as the
4502                  normal lazy PLT.  */
4503               if (lazy_ibt_plt != NULL
4504                   && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4505                               lazy_ibt_plt->plt_entry,
4506                               lazy_ibt_plt->plt_got_offset) == 0))
4507                 plt_type = plt_lazy | plt_second;
4508               else
4509                 plt_type = plt_lazy;
4510             }
4511           else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4512                            lazy_plt->plt0_got1_offset) == 0)
4513             {
4514               /* The fist entry in the PIC lazy IBT PLT is the same as
4515                  the normal PIC lazy PLT.  */
4516               if (lazy_ibt_plt != NULL
4517                   && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4518                               lazy_ibt_plt->pic_plt_entry,
4519                               lazy_ibt_plt->plt_got_offset) == 0))
4520                 plt_type = plt_lazy | plt_pic | plt_second;
4521               else
4522                 plt_type = plt_lazy | plt_pic;
4523             }
4524         }
4525
4526       if (non_lazy_plt != NULL
4527           && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4528           && plt->size >= non_lazy_plt->plt_entry_size)
4529         {
4530           /* Match non-lazy PLT.  */
4531           if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4532                       non_lazy_plt->plt_got_offset) == 0)
4533             plt_type = plt_non_lazy;
4534           else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4535                            non_lazy_plt->plt_got_offset) == 0)
4536             plt_type = plt_pic;
4537         }
4538
4539       if ((non_lazy_ibt_plt != NULL)
4540           && (plt_type == plt_unknown || plt_type == plt_second)
4541           && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4542         {
4543           if (memcmp (plt_contents,
4544                       non_lazy_ibt_plt->plt_entry,
4545                       non_lazy_ibt_plt->plt_got_offset) == 0)
4546             {
4547               /* Match IBT PLT.  */
4548               plt_type = plt_second;
4549               non_lazy_plt = non_lazy_ibt_plt;
4550             }
4551           else if (memcmp (plt_contents,
4552                            non_lazy_ibt_plt->pic_plt_entry,
4553                            non_lazy_ibt_plt->plt_got_offset) == 0)
4554             {
4555               /* Match PIC IBT PLT.  */
4556               plt_type = plt_second | plt_pic;
4557               non_lazy_plt = non_lazy_ibt_plt;
4558             }
4559         }
4560
4561       if (plt_type == plt_unknown)
4562         {
4563           free (plt_contents);
4564           continue;
4565         }
4566
4567       plts[j].sec = plt;
4568       plts[j].type = plt_type;
4569
4570       if ((plt_type & plt_lazy))
4571         {
4572           plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4573           plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4574           /* Skip PLT0 in lazy PLT.  */
4575           i = 1;
4576         }
4577       else
4578         {
4579           plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4580           plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4581           i = 0;
4582         }
4583
4584       /* Skip lazy PLT when the second PLT is used.  */
4585       if ((plt_type & (plt_lazy | plt_second))
4586           == (plt_lazy | plt_second))
4587         plts[j].count = 0;
4588       else
4589         {
4590           n = plt->size / plts[j].plt_entry_size;
4591           plts[j].count = n;
4592           count += n - i;
4593         }
4594
4595       plts[j].contents = plt_contents;
4596
4597       /* The _GLOBAL_OFFSET_TABLE_ address is needed.  */
4598       if ((plt_type & plt_pic))
4599         got_addr = (bfd_vma) -1;
4600     }
4601
4602   return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4603                                             got_addr, plts, dynsyms,
4604                                             ret);
4605 }
4606
4607 /* Set up i386 GNU properties.  Return the first relocatable ELF input
4608    with GNU properties if found.  Otherwise, return NULL.  */
4609
4610 static bfd *
4611 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4612 {
4613   struct elf_x86_init_table init_table;
4614
4615   init_table.normal_target = FALSE;
4616   init_table.is_vxworks = FALSE;
4617   switch (get_elf_i386_backend_data (info->output_bfd)->os)
4618     {
4619     case is_normal:
4620       init_table.lazy_plt = &elf_i386_lazy_plt;
4621       init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4622       init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4623       init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4624       init_table.normal_target = TRUE;
4625       break;
4626     case is_vxworks:
4627       init_table.lazy_plt = &elf_i386_lazy_plt;
4628       init_table.non_lazy_plt = NULL;
4629       init_table.lazy_ibt_plt = NULL;
4630       init_table.non_lazy_ibt_plt = NULL;
4631       init_table.is_vxworks = TRUE;
4632       break;
4633     case is_nacl:
4634       init_table.lazy_plt = &elf_i386_nacl_plt;
4635       init_table.non_lazy_plt = NULL;
4636       init_table.lazy_ibt_plt = NULL;
4637       init_table.non_lazy_ibt_plt = NULL;
4638       break;
4639     }
4640
4641   init_table.r_info = elf32_r_info;
4642   init_table.r_sym = elf32_r_sym;
4643
4644   return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4645 }
4646
4647 #define TARGET_LITTLE_SYM               i386_elf32_vec
4648 #define TARGET_LITTLE_NAME              "elf32-i386"
4649 #define ELF_ARCH                        bfd_arch_i386
4650 #define ELF_TARGET_ID                   I386_ELF_DATA
4651 #define ELF_MACHINE_CODE                EM_386
4652 #define ELF_MAXPAGESIZE                 0x1000
4653
4654 #define elf_backend_can_gc_sections     1
4655 #define elf_backend_can_refcount        1
4656 #define elf_backend_want_got_plt        1
4657 #define elf_backend_plt_readonly        1
4658 #define elf_backend_want_plt_sym        0
4659 #define elf_backend_got_header_size     12
4660 #define elf_backend_plt_alignment       4
4661 #define elf_backend_dtrel_excludes_plt  1
4662 #define elf_backend_extern_protected_data 1
4663 #define elf_backend_caches_rawsize      1
4664 #define elf_backend_want_dynrelro       1
4665
4666 /* Support RELA for objdump of prelink objects.  */
4667 #define elf_info_to_howto                     elf_i386_info_to_howto_rel
4668 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
4669
4670 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4671 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
4672 #define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
4673 #define bfd_elf32_get_synthetic_symtab        elf_i386_get_synthetic_symtab
4674
4675 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
4676 #define elf_backend_check_relocs              elf_i386_check_relocs
4677 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
4678 #define elf_backend_fake_sections             elf_i386_fake_sections
4679 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4680 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4681 #define elf_backend_output_arch_local_syms     elf_i386_output_arch_local_syms
4682 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
4683 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
4684 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
4685 #define elf_backend_relocate_section          elf_i386_relocate_section
4686 #define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
4687
4688 #include "elf32-target.h"
4689
4690 /* FreeBSD support.  */
4691
4692 #undef  TARGET_LITTLE_SYM
4693 #define TARGET_LITTLE_SYM               i386_elf32_fbsd_vec
4694 #undef  TARGET_LITTLE_NAME
4695 #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
4696 #undef  ELF_OSABI
4697 #define ELF_OSABI                       ELFOSABI_FREEBSD
4698
4699 /* The kernel recognizes executables as valid only if they carry a
4700    "FreeBSD" label in the ELF header.  So we put this label on all
4701    executables and (for simplicity) also all other object files.  */
4702
4703 static void
4704 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4705 {
4706   _bfd_elf_post_process_headers (abfd, info);
4707
4708 #ifdef OLD_FREEBSD_ABI_LABEL
4709   {
4710     /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4711     Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4712     memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4713   }
4714 #endif
4715 }
4716
4717 #undef  elf_backend_post_process_headers
4718 #define elf_backend_post_process_headers        elf_i386_fbsd_post_process_headers
4719 #undef  elf32_bed
4720 #define elf32_bed                               elf32_i386_fbsd_bed
4721
4722 #undef elf_backend_add_symbol_hook
4723
4724 #include "elf32-target.h"
4725
4726 /* Solaris 2.  */
4727
4728 #undef  TARGET_LITTLE_SYM
4729 #define TARGET_LITTLE_SYM               i386_elf32_sol2_vec
4730 #undef  TARGET_LITTLE_NAME
4731 #define TARGET_LITTLE_NAME              "elf32-i386-sol2"
4732
4733 #undef elf_backend_post_process_headers
4734
4735 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4736    objects won't be recognized.  */
4737 #undef ELF_OSABI
4738
4739 #undef  elf32_bed
4740 #define elf32_bed                       elf32_i386_sol2_bed
4741
4742 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4743    boundary.  */
4744 #undef  elf_backend_static_tls_alignment
4745 #define elf_backend_static_tls_alignment 8
4746
4747 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4748
4749    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4750    File, p.63.  */
4751 #undef  elf_backend_want_plt_sym
4752 #define elf_backend_want_plt_sym        1
4753
4754 #undef  elf_backend_strtab_flags
4755 #define elf_backend_strtab_flags        SHF_STRINGS
4756
4757 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4758    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised 
4759    FALSE otherwise.  ISECTION is the best guess matching section from the
4760    input bfd IBFD, but it might be NULL.  */
4761
4762 static bfd_boolean
4763 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4764                                                 bfd *obfd ATTRIBUTE_UNUSED,
4765                                                 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4766                                                 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4767 {
4768   /* PR 19938: FIXME: Need to add code for setting the sh_info
4769      and sh_link fields of Solaris specific section types.  */
4770   return FALSE;
4771
4772   /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4773      Object File Format, Table 13-9  ELF sh_link and sh_info Interpretation:
4774
4775 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4776
4777      The following values should be set:
4778      
4779 Type                 Link                           Info
4780 -----------------------------------------------------------------------------
4781 SHT_SUNW_ancillary   The section header index of    0
4782  [0x6fffffee]        the associated string table.
4783         
4784 SHT_SUNW_capinfo     The section header index of    For a dynamic object, the
4785  [0x6ffffff0]        the associated symbol table.   section header index of
4786                                                     the associated
4787                                                     SHT_SUNW_capchain table,
4788                                                     otherwise 0.
4789
4790 SHT_SUNW_symsort     The section header index of    0
4791  [0x6ffffff1]        the associated symbol table.
4792
4793 SHT_SUNW_tlssort     The section header index of    0
4794  [0x6ffffff2]        the associated symbol table.
4795         
4796 SHT_SUNW_LDYNSYM     The section header index of    One greater than the 
4797  [0x6ffffff3]        the associated string table.   symbol table index of the
4798                      This index is the same string  last local symbol, 
4799                      table used by the SHT_DYNSYM   STB_LOCAL. Since
4800                      section.                       SHT_SUNW_LDYNSYM only
4801                                                     contains local symbols,
4802                                                     sh_info is equivalent to
4803                                                     the number of symbols in
4804                                                     the table.
4805
4806 SHT_SUNW_cap         If symbol capabilities exist,  If any capabilities refer
4807  [0x6ffffff5]        the section header index of    to named strings, the
4808                      the associated                 section header index of
4809                      SHT_SUNW_capinfo table,        the associated string 
4810                           otherwise 0.              table, otherwise 0.
4811
4812 SHT_SUNW_move        The section header index of    0
4813  [0x6ffffffa]        the associated symbol table.
4814         
4815 SHT_SUNW_COMDAT      0                              0
4816  [0x6ffffffb]
4817
4818 SHT_SUNW_syminfo     The section header index of    The section header index
4819  [0x6ffffffc]        the associated symbol table.   of the associated
4820                                                     .dynamic section.
4821
4822 SHT_SUNW_verdef      The section header index of    The number of version 
4823  [0x6ffffffd]        the associated string table.   definitions within the
4824                                                     section.
4825
4826 SHT_SUNW_verneed     The section header index of    The number of version
4827  [0x6ffffffe]        the associated string table.   dependencies within the
4828                                                     section.
4829
4830 SHT_SUNW_versym      The section header index of    0
4831  [0x6fffffff]        the associated symbol table.  */
4832 }
4833
4834 #undef  elf_backend_copy_special_section_fields
4835 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4836
4837 #include "elf32-target.h"
4838
4839 /* Intel MCU support.  */
4840
4841 static bfd_boolean
4842 elf32_iamcu_elf_object_p (bfd *abfd)
4843 {
4844   /* Set the right machine number for an IAMCU elf32 file.  */
4845   bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4846   return TRUE;
4847 }
4848
4849 #undef  TARGET_LITTLE_SYM
4850 #define TARGET_LITTLE_SYM               iamcu_elf32_vec
4851 #undef  TARGET_LITTLE_NAME
4852 #define TARGET_LITTLE_NAME              "elf32-iamcu"
4853 #undef  ELF_ARCH
4854 #define ELF_ARCH                        bfd_arch_iamcu
4855
4856 #undef  ELF_MACHINE_CODE
4857 #define ELF_MACHINE_CODE                EM_IAMCU
4858
4859 #undef  ELF_OSABI
4860
4861 #undef  elf32_bed
4862 #define elf32_bed                       elf32_iamcu_bed
4863
4864 #undef  elf_backend_object_p
4865 #define elf_backend_object_p            elf32_iamcu_elf_object_p
4866
4867 #undef  elf_backend_static_tls_alignment
4868
4869 #undef  elf_backend_want_plt_sym
4870 #define elf_backend_want_plt_sym            0
4871
4872 #undef  elf_backend_strtab_flags
4873 #undef  elf_backend_copy_special_section_fields
4874
4875 #include "elf32-target.h"
4876
4877 /* Restore defaults.  */
4878 #undef  ELF_ARCH
4879 #define ELF_ARCH                        bfd_arch_i386
4880 #undef  ELF_MACHINE_CODE
4881 #define ELF_MACHINE_CODE                EM_386
4882
4883 /* Native Client support.  */
4884
4885 #undef  TARGET_LITTLE_SYM
4886 #define TARGET_LITTLE_SYM               i386_elf32_nacl_vec
4887 #undef  TARGET_LITTLE_NAME
4888 #define TARGET_LITTLE_NAME              "elf32-i386-nacl"
4889 #undef  elf32_bed
4890 #define elf32_bed                       elf32_i386_nacl_bed
4891
4892 #undef  ELF_MAXPAGESIZE
4893 #define ELF_MAXPAGESIZE                 0x10000
4894
4895 /* Restore defaults.  */
4896 #undef  ELF_OSABI
4897 #undef  elf_backend_want_plt_sym
4898 #define elf_backend_want_plt_sym        0
4899 #undef  elf_backend_post_process_headers
4900 #undef  elf_backend_static_tls_alignment
4901
4902 /* NaCl uses substantially different PLT entries for the same effects.  */
4903
4904 #undef  elf_backend_plt_alignment
4905 #define elf_backend_plt_alignment       5
4906 #define NACL_PLT_ENTRY_SIZE             64
4907 #define NACLMASK                        0xe0 /* 32-byte alignment mask.  */
4908
4909 static const bfd_byte elf_i386_nacl_plt0_entry[] =
4910   {
4911     0xff, 0x35,                   /* pushl contents of address */
4912     0, 0, 0, 0,                   /* replaced with address of .got + 4.  */
4913     0x8b, 0x0d,                   /* movl contents of address, %ecx */
4914     0, 0, 0, 0,                   /* replaced with address of .got + 8.  */
4915     0x83, 0xe1, NACLMASK,         /* andl $NACLMASK, %ecx */
4916     0xff, 0xe1                    /* jmp *%ecx */
4917   };
4918
4919 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
4920   {
4921     0x8b, 0x0d,                         /* movl contents of address, %ecx */
4922     0, 0, 0, 0,                         /* replaced with GOT slot address.  */
4923     0x83, 0xe1, NACLMASK,               /* andl $NACLMASK, %ecx */
4924     0xff, 0xe1,                         /* jmp *%ecx */
4925
4926     /* Pad to the next 32-byte boundary with nop instructions.  */
4927     0x90,
4928     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4929     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4930
4931     /* Lazy GOT entries point here (32-byte aligned).  */
4932     0x68,                              /* pushl immediate */
4933     0, 0, 0, 0,                        /* replaced with reloc offset.  */
4934     0xe9,                              /* jmp relative */
4935     0, 0, 0, 0,                        /* replaced with offset to .plt.  */
4936
4937     /* Pad to the next 32-byte boundary with nop instructions.  */
4938     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4939     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4940     0x90, 0x90
4941   };
4942
4943 static const bfd_byte
4944 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
4945   {
4946     0xff, 0x73, 0x04,           /* pushl 4(%ebx) */
4947     0x8b, 0x4b, 0x08,           /* mov 0x8(%ebx), %ecx */
4948     0x83, 0xe1, 0xe0,           /* and $NACLMASK, %ecx */
4949     0xff, 0xe1,                 /* jmp *%ecx */
4950
4951     /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
4952        so pad to that size with nop instructions.  */
4953     0x90, 0x90, 0x90, 0x90, 0x90, 0x90
4954   };
4955
4956 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
4957   {
4958     0x8b, 0x8b,          /* movl offset(%ebx), %ecx */
4959     0, 0, 0, 0,          /* replaced with offset of this symbol in .got.  */
4960     0x83, 0xe1, 0xe0,    /* andl $NACLMASK, %ecx */
4961     0xff, 0xe1,          /* jmp *%ecx */
4962
4963     /* Pad to the next 32-byte boundary with nop instructions.  */
4964     0x90,
4965     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4966     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4967
4968     /* Lazy GOT entries point here (32-byte aligned).  */
4969     0x68,                /* pushl immediate */
4970     0, 0, 0, 0,          /* replaced with offset into relocation table.  */
4971     0xe9,                /* jmp relative */
4972     0, 0, 0, 0,          /* replaced with offset to start of .plt.  */
4973
4974     /* Pad to the next 32-byte boundary with nop instructions.  */
4975     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4976     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4977     0x90, 0x90
4978   };
4979
4980 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
4981   {
4982 #if (PLT_CIE_LENGTH != 20                               \
4983      || PLT_FDE_LENGTH != 36                            \
4984      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8  \
4985      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
4986 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
4987 #endif
4988     PLT_CIE_LENGTH, 0, 0, 0,            /* CIE length */
4989     0, 0, 0, 0,                         /* CIE ID */
4990     1,                                  /* CIE version */
4991     'z', 'R', 0,                        /* Augmentation string */
4992     1,                                  /* Code alignment factor */
4993     0x7c,                               /* Data alignment factor: -4 */
4994     8,                                  /* Return address column */
4995     1,                                  /* Augmentation size */
4996     DW_EH_PE_pcrel | DW_EH_PE_sdata4,   /* FDE encoding */
4997     DW_CFA_def_cfa, 4, 4,               /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
4998     DW_CFA_offset + 8, 1,               /* DW_CFA_offset: r8 (eip) at cfa-4 */
4999     DW_CFA_nop, DW_CFA_nop,
5000
5001     PLT_FDE_LENGTH, 0, 0, 0,     /* FDE length */
5002     PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
5003     0, 0, 0, 0,                  /* R_386_PC32 .plt goes here */
5004     0, 0, 0, 0,                  /* .plt size goes here */
5005     0,                           /* Augmentation size */
5006     DW_CFA_def_cfa_offset, 8,    /* DW_CFA_def_cfa_offset: 8 */
5007     DW_CFA_advance_loc + 6,      /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5008     DW_CFA_def_cfa_offset, 12,   /* DW_CFA_def_cfa_offset: 12 */
5009     DW_CFA_advance_loc + 58,     /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5010     DW_CFA_def_cfa_expression,   /* DW_CFA_def_cfa_expression */
5011     13,                          /* Block length */
5012     DW_OP_breg4, 4,              /* DW_OP_breg4 (esp): 4 */
5013     DW_OP_breg8, 0,              /* DW_OP_breg8 (eip): 0 */
5014     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5015     DW_OP_lit2, DW_OP_shl, DW_OP_plus,
5016     DW_CFA_nop, DW_CFA_nop
5017   };
5018
5019 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
5020   {
5021     elf_i386_nacl_plt0_entry,           /* plt0_entry */
5022     sizeof (elf_i386_nacl_plt0_entry),  /* plt0_entry_size */
5023     elf_i386_nacl_plt_entry,            /* plt_entry */
5024     NACL_PLT_ENTRY_SIZE,                /* plt_entry_size */
5025     2,                                  /* plt0_got1_offset */
5026     8,                                  /* plt0_got2_offset */
5027     0,                                  /* plt0_got2_insn_end */
5028     2,                                  /* plt_got_offset */
5029     33,                                 /* plt_reloc_offset */
5030     38,                                 /* plt_plt_offset */
5031     0,                                  /* plt_got_insn_size */
5032     0,                                  /* plt_plt_insn_end */
5033     32,                                 /* plt_lazy_offset */
5034     elf_i386_nacl_pic_plt0_entry,       /* pic_plt0_entry */
5035     elf_i386_nacl_pic_plt_entry,        /* pic_plt_entry */
5036     elf_i386_nacl_eh_frame_plt,         /* eh_frame_plt */
5037     sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
5038   };
5039
5040 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
5041   {
5042     0x90,                               /* plt0_pad_byte: nop insn */
5043     is_nacl                             /* os */
5044   };
5045
5046 static bfd_boolean
5047 elf32_i386_nacl_elf_object_p (bfd *abfd)
5048 {
5049   /* Set the right machine number for a NaCl i386 ELF32 file.  */
5050   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
5051   return TRUE;
5052 }
5053
5054 #undef  elf_backend_arch_data
5055 #define elf_backend_arch_data   &elf_i386_nacl_arch_bed
5056
5057 #undef  elf_backend_object_p
5058 #define elf_backend_object_p                    elf32_i386_nacl_elf_object_p
5059 #undef  elf_backend_modify_segment_map
5060 #define elf_backend_modify_segment_map          nacl_modify_segment_map
5061 #undef  elf_backend_modify_program_headers
5062 #define elf_backend_modify_program_headers      nacl_modify_program_headers
5063 #undef  elf_backend_final_write_processing
5064 #define elf_backend_final_write_processing      nacl_final_write_processing
5065
5066 #include "elf32-target.h"
5067
5068 /* Restore defaults.  */
5069 #undef  elf_backend_object_p
5070 #undef  elf_backend_modify_segment_map
5071 #undef  elf_backend_modify_program_headers
5072 #undef  elf_backend_final_write_processing
5073
5074 /* VxWorks support.  */
5075
5076 #undef  TARGET_LITTLE_SYM
5077 #define TARGET_LITTLE_SYM               i386_elf32_vxworks_vec
5078 #undef  TARGET_LITTLE_NAME
5079 #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
5080 #undef  ELF_OSABI
5081 #undef  ELF_MAXPAGESIZE
5082 #define ELF_MAXPAGESIZE                 0x1000
5083 #undef  elf_backend_plt_alignment
5084 #define elf_backend_plt_alignment       4
5085
5086 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
5087   {
5088     0x90,                               /* plt0_pad_byte */
5089     is_vxworks                          /* os */
5090   };
5091
5092 #undef  elf_backend_arch_data
5093 #define elf_backend_arch_data   &elf_i386_vxworks_arch_bed
5094
5095 #undef elf_backend_relocs_compatible
5096 #undef elf_backend_add_symbol_hook
5097 #define elf_backend_add_symbol_hook \
5098   elf_vxworks_add_symbol_hook
5099 #undef elf_backend_link_output_symbol_hook
5100 #define elf_backend_link_output_symbol_hook \
5101   elf_vxworks_link_output_symbol_hook
5102 #undef elf_backend_emit_relocs
5103 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
5104 #undef elf_backend_final_write_processing
5105 #define elf_backend_final_write_processing \
5106   elf_vxworks_final_write_processing
5107 #undef elf_backend_static_tls_alignment
5108
5109 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5110    define it.  */
5111 #undef elf_backend_want_plt_sym
5112 #define elf_backend_want_plt_sym        1
5113
5114 #undef  elf32_bed
5115 #define elf32_bed                               elf32_i386_vxworks_bed
5116
5117 #include "elf32-target.h"
This page took 0.32375 seconds and 4 git commands to generate.