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