ELF/BFD,GDB: Handle both variants of the 32-bit Linux core PRPSINFO note
[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->zero_undefweak &= 0x2;
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->zero_undefweak &= 0x2;
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->zero_undefweak &= 0x2;
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->zero_undefweak |= 0x2;
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 (COPY_INPUT_RELOC_P (info, h, r_type))
2782                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2783               else
2784                 {
2785                   /* This symbol is local, or marked to become local.  */
2786                   relocate = TRUE;
2787                   outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2788                 }
2789
2790               sreloc = elf_section_data (input_section)->sreloc;
2791
2792               if (sreloc == NULL || sreloc->contents == NULL)
2793                 {
2794                   r = bfd_reloc_notsupported;
2795                   goto check_relocation_error;
2796                 }
2797
2798               elf_append_rel (output_bfd, sreloc, &outrel);
2799
2800               /* If this reloc is against an external symbol, we do
2801                  not want to fiddle with the addend.  Otherwise, we
2802                  need to include the symbol value so that it becomes
2803                  an addend for the dynamic reloc.  */
2804               if (! relocate)
2805                 continue;
2806             }
2807           break;
2808
2809         case R_386_TLS_IE:
2810           if (!bfd_link_executable (info))
2811             {
2812               Elf_Internal_Rela outrel;
2813               asection *sreloc;
2814
2815               outrel.r_offset = rel->r_offset
2816                                 + input_section->output_section->vma
2817                                 + input_section->output_offset;
2818               outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2819               sreloc = elf_section_data (input_section)->sreloc;
2820               if (sreloc == NULL)
2821                 abort ();
2822               elf_append_rel (output_bfd, sreloc, &outrel);
2823             }
2824           /* Fall through */
2825
2826         case R_386_TLS_GD:
2827         case R_386_TLS_GOTDESC:
2828         case R_386_TLS_DESC_CALL:
2829         case R_386_TLS_IE_32:
2830         case R_386_TLS_GOTIE:
2831           tls_type = GOT_UNKNOWN;
2832           if (h == NULL && local_got_offsets)
2833             tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2834           else if (h != NULL)
2835             tls_type = elf_x86_hash_entry(h)->tls_type;
2836           if (tls_type == GOT_TLS_IE)
2837             tls_type = GOT_TLS_IE_NEG;
2838
2839            r_type_tls = r_type;
2840           if (! elf_i386_tls_transition (info, input_bfd,
2841                                          input_section, contents,
2842                                          symtab_hdr, sym_hashes,
2843                                          &r_type_tls, tls_type, rel,
2844                                          relend, h, r_symndx, TRUE))
2845             return FALSE;
2846
2847           if (r_type_tls == R_386_TLS_LE_32)
2848             {
2849               BFD_ASSERT (! unresolved_reloc);
2850               if (r_type == R_386_TLS_GD)
2851                 {
2852                   unsigned int type;
2853                   bfd_vma roff;
2854
2855                   /* GD->LE transition.  */
2856                   type = *(contents + rel->r_offset - 2);
2857                   if (type == 0x04)
2858                     {
2859                       /* Change
2860                                 leal foo@tlsgd(,%ebx,1), %eax
2861                                 call ___tls_get_addr@PLT
2862                          into:
2863                                 movl %gs:0, %eax
2864                                 subl $foo@tpoff, %eax
2865                          (6 byte form of subl).  */
2866                       roff = rel->r_offset + 5;
2867                     }
2868                   else
2869                     {
2870                       /* Change
2871                                 leal foo@tlsgd(%ebx), %eax
2872                                 call ___tls_get_addr@PLT
2873                                 nop
2874                          or
2875                                 leal foo@tlsgd(%reg), %eax
2876                                 call *___tls_get_addr@GOT(%reg)
2877                                 which may be converted to
2878                                 addr32 call ___tls_get_addr
2879                          into:
2880                                 movl %gs:0, %eax; subl $foo@tpoff, %eax
2881                          (6 byte form of subl).  */
2882                       roff = rel->r_offset + 6;
2883                     }
2884                   memcpy (contents + roff - 8,
2885                           "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2886                   bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2887                               contents + roff);
2888                   /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X.  */
2889                   rel++;
2890                   wrel++;
2891                   continue;
2892                 }
2893               else if (r_type == R_386_TLS_GOTDESC)
2894                 {
2895                   /* GDesc -> LE transition.
2896                      It's originally something like:
2897                      leal x@tlsdesc(%ebx), %eax
2898
2899                      leal x@ntpoff, %eax
2900
2901                      Registers other than %eax may be set up here.  */
2902
2903                   unsigned int val;
2904                   bfd_vma roff;
2905
2906                   roff = rel->r_offset;
2907                   val = bfd_get_8 (input_bfd, contents + roff - 1);
2908
2909                   /* Now modify the instruction as appropriate.  */
2910                   /* aoliva FIXME: remove the above and xor the byte
2911                      below with 0x86.  */
2912                   bfd_put_8 (output_bfd, val ^ 0x86,
2913                              contents + roff - 1);
2914                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2915                               contents + roff);
2916                   continue;
2917                 }
2918               else if (r_type == R_386_TLS_DESC_CALL)
2919                 {
2920                   /* GDesc -> LE transition.
2921                      It's originally:
2922                      call *(%eax)
2923                      Turn it into:
2924                      xchg %ax,%ax  */
2925
2926                   bfd_vma roff;
2927
2928                   roff = rel->r_offset;
2929                   bfd_put_8 (output_bfd, 0x66, contents + roff);
2930                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2931                   continue;
2932                 }
2933               else if (r_type == R_386_TLS_IE)
2934                 {
2935                   unsigned int val;
2936
2937                   /* IE->LE transition:
2938                      Originally it can be one of:
2939                      movl foo, %eax
2940                      movl foo, %reg
2941                      addl foo, %reg
2942                      We change it into:
2943                      movl $foo, %eax
2944                      movl $foo, %reg
2945                      addl $foo, %reg.  */
2946                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2947                   if (val == 0xa1)
2948                     {
2949                       /* movl foo, %eax.  */
2950                       bfd_put_8 (output_bfd, 0xb8,
2951                                  contents + rel->r_offset - 1);
2952                     }
2953                   else
2954                     {
2955                       unsigned int type;
2956
2957                       type = bfd_get_8 (input_bfd,
2958                                         contents + rel->r_offset - 2);
2959                       switch (type)
2960                         {
2961                         case 0x8b:
2962                           /* movl */
2963                           bfd_put_8 (output_bfd, 0xc7,
2964                                      contents + rel->r_offset - 2);
2965                           bfd_put_8 (output_bfd,
2966                                      0xc0 | ((val >> 3) & 7),
2967                                      contents + rel->r_offset - 1);
2968                           break;
2969                         case 0x03:
2970                           /* addl */
2971                           bfd_put_8 (output_bfd, 0x81,
2972                                      contents + rel->r_offset - 2);
2973                           bfd_put_8 (output_bfd,
2974                                      0xc0 | ((val >> 3) & 7),
2975                                      contents + rel->r_offset - 1);
2976                           break;
2977                         default:
2978                           BFD_FAIL ();
2979                           break;
2980                         }
2981                     }
2982                   bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2983                               contents + rel->r_offset);
2984                   continue;
2985                 }
2986               else
2987                 {
2988                   unsigned int val, type;
2989
2990                   /* {IE_32,GOTIE}->LE transition:
2991                      Originally it can be one of:
2992                      subl foo(%reg1), %reg2
2993                      movl foo(%reg1), %reg2
2994                      addl foo(%reg1), %reg2
2995                      We change it into:
2996                      subl $foo, %reg2
2997                      movl $foo, %reg2 (6 byte form)
2998                      addl $foo, %reg2.  */
2999                   type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3000                   val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3001                   if (type == 0x8b)
3002                     {
3003                       /* movl */
3004                       bfd_put_8 (output_bfd, 0xc7,
3005                                  contents + rel->r_offset - 2);
3006                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3007                                  contents + rel->r_offset - 1);
3008                     }
3009                   else if (type == 0x2b)
3010                     {
3011                       /* subl */
3012                       bfd_put_8 (output_bfd, 0x81,
3013                                  contents + rel->r_offset - 2);
3014                       bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3015                                  contents + rel->r_offset - 1);
3016                     }
3017                   else if (type == 0x03)
3018                     {
3019                       /* addl */
3020                       bfd_put_8 (output_bfd, 0x81,
3021                                  contents + rel->r_offset - 2);
3022                       bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3023                                  contents + rel->r_offset - 1);
3024                     }
3025                   else
3026                     BFD_FAIL ();
3027                   if (r_type == R_386_TLS_GOTIE)
3028                     bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3029                                 contents + rel->r_offset);
3030                   else
3031                     bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3032                                 contents + rel->r_offset);
3033                   continue;
3034                 }
3035             }
3036
3037           if (htab->elf.sgot == NULL)
3038             abort ();
3039
3040           if (h != NULL)
3041             {
3042               off = h->got.offset;
3043               offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3044             }
3045           else
3046             {
3047               if (local_got_offsets == NULL)
3048                 abort ();
3049
3050               off = local_got_offsets[r_symndx];
3051               offplt = local_tlsdesc_gotents[r_symndx];
3052             }
3053
3054           if ((off & 1) != 0)
3055             off &= ~1;
3056           else
3057             {
3058               Elf_Internal_Rela outrel;
3059               int dr_type;
3060               asection *sreloc;
3061
3062               if (htab->elf.srelgot == NULL)
3063                 abort ();
3064
3065               indx = h && h->dynindx != -1 ? h->dynindx : 0;
3066
3067               if (GOT_TLS_GDESC_P (tls_type))
3068                 {
3069                   bfd_byte *loc;
3070                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3071                   BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3072                               <= htab->elf.sgotplt->size);
3073                   outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3074                                      + htab->elf.sgotplt->output_offset
3075                                      + offplt
3076                                      + htab->sgotplt_jump_table_size);
3077                   sreloc = htab->elf.srelplt;
3078                   loc = sreloc->contents;
3079                   loc += (htab->next_tls_desc_index++
3080                           * sizeof (Elf32_External_Rel));
3081                   BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3082                               <= sreloc->contents + sreloc->size);
3083                   bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3084                   if (indx == 0)
3085                     {
3086                       BFD_ASSERT (! unresolved_reloc);
3087                       bfd_put_32 (output_bfd,
3088                                   relocation - _bfd_x86_elf_dtpoff_base (info),
3089                                   htab->elf.sgotplt->contents + offplt
3090                                   + htab->sgotplt_jump_table_size + 4);
3091                     }
3092                   else
3093                     {
3094                       bfd_put_32 (output_bfd, 0,
3095                                   htab->elf.sgotplt->contents + offplt
3096                                   + htab->sgotplt_jump_table_size + 4);
3097                     }
3098                 }
3099
3100               sreloc = htab->elf.srelgot;
3101
3102               outrel.r_offset = (htab->elf.sgot->output_section->vma
3103                                  + htab->elf.sgot->output_offset + off);
3104
3105               if (GOT_TLS_GD_P (tls_type))
3106                 dr_type = R_386_TLS_DTPMOD32;
3107               else if (GOT_TLS_GDESC_P (tls_type))
3108                 goto dr_done;
3109               else if (tls_type == GOT_TLS_IE_POS)
3110                 dr_type = R_386_TLS_TPOFF;
3111               else
3112                 dr_type = R_386_TLS_TPOFF32;
3113
3114               if (dr_type == R_386_TLS_TPOFF && indx == 0)
3115                 bfd_put_32 (output_bfd,
3116                             relocation - _bfd_x86_elf_dtpoff_base (info),
3117                             htab->elf.sgot->contents + off);
3118               else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3119                 bfd_put_32 (output_bfd,
3120                             _bfd_x86_elf_dtpoff_base (info) - relocation,
3121                             htab->elf.sgot->contents + off);
3122               else if (dr_type != R_386_TLS_DESC)
3123                 bfd_put_32 (output_bfd, 0,
3124                             htab->elf.sgot->contents + off);
3125               outrel.r_info = ELF32_R_INFO (indx, dr_type);
3126
3127               elf_append_rel (output_bfd, sreloc, &outrel);
3128
3129               if (GOT_TLS_GD_P (tls_type))
3130                 {
3131                   if (indx == 0)
3132                     {
3133                       BFD_ASSERT (! unresolved_reloc);
3134                       bfd_put_32 (output_bfd,
3135                                   relocation - _bfd_x86_elf_dtpoff_base (info),
3136                                   htab->elf.sgot->contents + off + 4);
3137                     }
3138                   else
3139                     {
3140                       bfd_put_32 (output_bfd, 0,
3141                                   htab->elf.sgot->contents + off + 4);
3142                       outrel.r_info = ELF32_R_INFO (indx,
3143                                                     R_386_TLS_DTPOFF32);
3144                       outrel.r_offset += 4;
3145                       elf_append_rel (output_bfd, sreloc, &outrel);
3146                     }
3147                 }
3148               else if (tls_type == GOT_TLS_IE_BOTH)
3149                 {
3150                   bfd_put_32 (output_bfd,
3151                               (indx == 0
3152                                ? relocation - _bfd_x86_elf_dtpoff_base (info)
3153                                : 0),
3154                               htab->elf.sgot->contents + off + 4);
3155                   outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3156                   outrel.r_offset += 4;
3157                   elf_append_rel (output_bfd, sreloc, &outrel);
3158                 }
3159
3160             dr_done:
3161               if (h != NULL)
3162                 h->got.offset |= 1;
3163               else
3164                 local_got_offsets[r_symndx] |= 1;
3165             }
3166
3167           if (off >= (bfd_vma) -2
3168               && ! GOT_TLS_GDESC_P (tls_type))
3169             abort ();
3170           if (r_type_tls == R_386_TLS_GOTDESC
3171               || r_type_tls == R_386_TLS_DESC_CALL)
3172             {
3173               relocation = htab->sgotplt_jump_table_size + offplt;
3174               unresolved_reloc = FALSE;
3175             }
3176           else if (r_type_tls == r_type)
3177             {
3178               bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3179                               + htab->elf.sgotplt->output_offset;
3180               relocation = htab->elf.sgot->output_section->vma
3181                 + htab->elf.sgot->output_offset + off - g_o_t;
3182               if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3183                   && tls_type == GOT_TLS_IE_BOTH)
3184                 relocation += 4;
3185               if (r_type == R_386_TLS_IE)
3186                 relocation += g_o_t;
3187               unresolved_reloc = FALSE;
3188             }
3189           else if (r_type == R_386_TLS_GD)
3190             {
3191               unsigned int val, type;
3192               bfd_vma roff;
3193
3194               /* GD->IE transition.  */
3195               type = *(contents + rel->r_offset - 2);
3196               val = *(contents + rel->r_offset - 1);
3197               if (type == 0x04)
3198                 {
3199                   /* Change
3200                         leal foo@tlsgd(,%ebx,1), %eax
3201                         call ___tls_get_addr@PLT
3202                      into:
3203                         movl %gs:0, %eax
3204                         subl $foo@gottpoff(%ebx), %eax.  */
3205                   val >>= 3;
3206                   roff = rel->r_offset - 3;
3207                 }
3208               else
3209                 {
3210                   /* Change
3211                         leal foo@tlsgd(%ebx), %eax
3212                         call ___tls_get_addr@PLT
3213                         nop
3214                      or
3215                         leal foo@tlsgd(%reg), %eax
3216                         call *___tls_get_addr@GOT(%reg)
3217                         which may be converted to
3218                         addr32 call ___tls_get_addr
3219                      into:
3220                         movl %gs:0, %eax;
3221                         subl $foo@gottpoff(%reg), %eax.  */
3222                   roff = rel->r_offset - 2;
3223                 }
3224               memcpy (contents + roff,
3225                       "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3226               contents[roff + 7] = 0x80 | (val & 7);
3227               /* If foo is used only with foo@gotntpoff(%reg) and
3228                  foo@indntpoff, but not with foo@gottpoff(%reg), change
3229                  subl $foo@gottpoff(%reg), %eax
3230                  into:
3231                  addl $foo@gotntpoff(%reg), %eax.  */
3232               if (tls_type == GOT_TLS_IE_POS)
3233                 contents[roff + 6] = 0x03;
3234               bfd_put_32 (output_bfd,
3235                           htab->elf.sgot->output_section->vma
3236                           + htab->elf.sgot->output_offset + off
3237                           - htab->elf.sgotplt->output_section->vma
3238                           - htab->elf.sgotplt->output_offset,
3239                           contents + roff + 8);
3240               /* Skip R_386_PLT32 and R_386_GOT32X.  */
3241               rel++;
3242               wrel++;
3243               continue;
3244             }
3245           else if (r_type == R_386_TLS_GOTDESC)
3246             {
3247               /* GDesc -> IE transition.
3248                  It's originally something like:
3249                  leal x@tlsdesc(%ebx), %eax
3250
3251                  Change it to:
3252                  movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3253                  or:
3254                  movl x@gottpoff(%ebx), %eax # before negl %eax
3255
3256                  Registers other than %eax may be set up here.  */
3257
3258               bfd_vma roff;
3259
3260               /* First, make sure it's a leal adding ebx to a 32-bit
3261                  offset into any register, although it's probably
3262                  almost always going to be eax.  */
3263               roff = rel->r_offset;
3264
3265               /* Now modify the instruction as appropriate.  */
3266               /* To turn a leal into a movl in the form we use it, it
3267                  suffices to change the first byte from 0x8d to 0x8b.
3268                  aoliva FIXME: should we decide to keep the leal, all
3269                  we have to do is remove the statement below, and
3270                  adjust the relaxation of R_386_TLS_DESC_CALL.  */
3271               bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3272
3273               if (tls_type == GOT_TLS_IE_BOTH)
3274                 off += 4;
3275
3276               bfd_put_32 (output_bfd,
3277                           htab->elf.sgot->output_section->vma
3278                           + htab->elf.sgot->output_offset + off
3279                           - htab->elf.sgotplt->output_section->vma
3280                           - htab->elf.sgotplt->output_offset,
3281                           contents + roff);
3282               continue;
3283             }
3284           else if (r_type == R_386_TLS_DESC_CALL)
3285             {
3286               /* GDesc -> IE transition.
3287                  It's originally:
3288                  call *(%eax)
3289
3290                  Change it to:
3291                  xchg %ax,%ax
3292                  or
3293                  negl %eax
3294                  depending on how we transformed the TLS_GOTDESC above.
3295               */
3296
3297               bfd_vma roff;
3298
3299               roff = rel->r_offset;
3300
3301               /* Now modify the instruction as appropriate.  */
3302               if (tls_type != GOT_TLS_IE_NEG)
3303                 {
3304                   /* xchg %ax,%ax */
3305                   bfd_put_8 (output_bfd, 0x66, contents + roff);
3306                   bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3307                 }
3308               else
3309                 {
3310                   /* negl %eax */
3311                   bfd_put_8 (output_bfd, 0xf7, contents + roff);
3312                   bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3313                 }
3314
3315               continue;
3316             }
3317           else
3318             BFD_ASSERT (FALSE);
3319           break;
3320
3321         case R_386_TLS_LDM:
3322           if (! elf_i386_tls_transition (info, input_bfd,
3323                                          input_section, contents,
3324                                          symtab_hdr, sym_hashes,
3325                                          &r_type, GOT_UNKNOWN, rel,
3326                                          relend, h, r_symndx, TRUE))
3327             return FALSE;
3328
3329           if (r_type != R_386_TLS_LDM)
3330             {
3331               /* LD->LE transition.  Change
3332                         leal foo@tlsldm(%ebx) %eax
3333                         call ___tls_get_addr@PLT
3334                  into:
3335                         movl %gs:0, %eax
3336                         nop
3337                         leal 0(%esi,1), %esi
3338                  or change
3339                         leal foo@tlsldm(%reg) %eax
3340                         call *___tls_get_addr@GOT(%reg)
3341                         which may be converted to
3342                         addr32 call ___tls_get_addr
3343                  into:
3344                         movl %gs:0, %eax
3345                         leal 0(%esi), %esi  */
3346               BFD_ASSERT (r_type == R_386_TLS_LE_32);
3347               if (*(contents + rel->r_offset + 4) == 0xff
3348                   || *(contents + rel->r_offset + 4) == 0x67)
3349                 memcpy (contents + rel->r_offset - 2,
3350                         "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3351               else
3352                 memcpy (contents + rel->r_offset - 2,
3353                         "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3354               /* Skip R_386_PC32/R_386_PLT32.  */
3355               rel++;
3356               wrel++;
3357               continue;
3358             }
3359
3360           if (htab->elf.sgot == NULL)
3361             abort ();
3362
3363           off = htab->tls_ld_or_ldm_got.offset;
3364           if (off & 1)
3365             off &= ~1;
3366           else
3367             {
3368               Elf_Internal_Rela outrel;
3369
3370               if (htab->elf.srelgot == NULL)
3371                 abort ();
3372
3373               outrel.r_offset = (htab->elf.sgot->output_section->vma
3374                                  + htab->elf.sgot->output_offset + off);
3375
3376               bfd_put_32 (output_bfd, 0,
3377                           htab->elf.sgot->contents + off);
3378               bfd_put_32 (output_bfd, 0,
3379                           htab->elf.sgot->contents + off + 4);
3380               outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3381               elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3382               htab->tls_ld_or_ldm_got.offset |= 1;
3383             }
3384           relocation = htab->elf.sgot->output_section->vma
3385                        + htab->elf.sgot->output_offset + off
3386                        - htab->elf.sgotplt->output_section->vma
3387                        - htab->elf.sgotplt->output_offset;
3388           unresolved_reloc = FALSE;
3389           break;
3390
3391         case R_386_TLS_LDO_32:
3392           if (!bfd_link_executable (info)
3393               || (input_section->flags & SEC_CODE) == 0)
3394             relocation -= _bfd_x86_elf_dtpoff_base (info);
3395           else
3396             /* When converting LDO to LE, we must negate.  */
3397             relocation = -elf_i386_tpoff (info, relocation);
3398           break;
3399
3400         case R_386_TLS_LE_32:
3401         case R_386_TLS_LE:
3402           if (!bfd_link_executable (info))
3403             {
3404               Elf_Internal_Rela outrel;
3405               asection *sreloc;
3406
3407               outrel.r_offset = rel->r_offset
3408                                 + input_section->output_section->vma
3409                                 + input_section->output_offset;
3410               if (h != NULL && h->dynindx != -1)
3411                 indx = h->dynindx;
3412               else
3413                 indx = 0;
3414               if (r_type == R_386_TLS_LE_32)
3415                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3416               else
3417                 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3418               sreloc = elf_section_data (input_section)->sreloc;
3419               if (sreloc == NULL)
3420                 abort ();
3421               elf_append_rel (output_bfd, sreloc, &outrel);
3422               if (indx)
3423                 continue;
3424               else if (r_type == R_386_TLS_LE_32)
3425                 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3426               else
3427                 relocation -= _bfd_x86_elf_dtpoff_base (info);
3428             }
3429           else if (r_type == R_386_TLS_LE_32)
3430             relocation = elf_i386_tpoff (info, relocation);
3431           else
3432             relocation = -elf_i386_tpoff (info, relocation);
3433           break;
3434
3435         default:
3436           break;
3437         }
3438
3439       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3440          because such sections are not SEC_ALLOC and thus ld.so will
3441          not process them.  */
3442       if (unresolved_reloc
3443           && !((input_section->flags & SEC_DEBUGGING) != 0
3444                && h->def_dynamic)
3445           && _bfd_elf_section_offset (output_bfd, info, input_section,
3446                                       rel->r_offset) != (bfd_vma) -1)
3447         {
3448           _bfd_error_handler
3449             /* xgettext:c-format */
3450             (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
3451              input_bfd,
3452              input_section,
3453              rel->r_offset,
3454              howto->name,
3455              h->root.root.string);
3456           return FALSE;
3457         }
3458
3459 do_relocation:
3460       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3461                                     contents, rel->r_offset,
3462                                     relocation, 0);
3463
3464 check_relocation_error:
3465       if (r != bfd_reloc_ok)
3466         {
3467           const char *name;
3468
3469           if (h != NULL)
3470             name = h->root.root.string;
3471           else
3472             {
3473               name = bfd_elf_string_from_elf_section (input_bfd,
3474                                                       symtab_hdr->sh_link,
3475                                                       sym->st_name);
3476               if (name == NULL)
3477                 return FALSE;
3478               if (*name == '\0')
3479                 name = bfd_section_name (input_bfd, sec);
3480             }
3481
3482           if (r == bfd_reloc_overflow)
3483             (*info->callbacks->reloc_overflow)
3484               (info, (h ? &h->root : NULL), name, howto->name,
3485                (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3486           else
3487             {
3488               _bfd_error_handler
3489                 /* xgettext:c-format */
3490                 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
3491                  input_bfd, input_section,
3492                  rel->r_offset, name, (int) r);
3493               return FALSE;
3494             }
3495         }
3496
3497       if (wrel != rel)
3498         *wrel = *rel;
3499     }
3500
3501   if (wrel != rel)
3502     {
3503       Elf_Internal_Shdr *rel_hdr;
3504       size_t deleted = rel - wrel;
3505
3506       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3507       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3508       if (rel_hdr->sh_size == 0)
3509         {
3510           /* It is too late to remove an empty reloc section.  Leave
3511              one NONE reloc.
3512              ??? What is wrong with an empty section???  */
3513           rel_hdr->sh_size = rel_hdr->sh_entsize;
3514           deleted -= 1;
3515         }
3516       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3517       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3518       input_section->reloc_count -= deleted;
3519     }
3520
3521   return TRUE;
3522 }
3523
3524 /* Finish up dynamic symbol handling.  We set the contents of various
3525    dynamic sections here.  */
3526
3527 static bfd_boolean
3528 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3529                                 struct bfd_link_info *info,
3530                                 struct elf_link_hash_entry *h,
3531                                 Elf_Internal_Sym *sym)
3532 {
3533   struct elf_x86_link_hash_table *htab;
3534   unsigned plt_entry_size;
3535   struct elf_x86_link_hash_entry *eh;
3536   bfd_boolean local_undefweak;
3537   bfd_boolean use_plt_second;
3538
3539   htab = elf_x86_hash_table (info, I386_ELF_DATA);
3540   if (htab == NULL)
3541     return FALSE;
3542
3543   plt_entry_size = htab->plt.plt_entry_size;
3544
3545   /* Use the second PLT section only if there is .plt section.  */
3546   use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3547
3548   eh = (struct elf_x86_link_hash_entry *) h;
3549   if (eh->no_finish_dynamic_symbol)
3550     abort ();
3551
3552   /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3553      resolved undefined weak symbols in executable so that their
3554      references have value 0 at run-time.  */
3555   local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3556
3557   if (h->plt.offset != (bfd_vma) -1)
3558     {
3559       bfd_vma plt_index, plt_offset;
3560       bfd_vma got_offset;
3561       Elf_Internal_Rela rel;
3562       bfd_byte *loc;
3563       asection *plt, *resolved_plt, *gotplt, *relplt;
3564
3565       /* When building a static executable, use .iplt, .igot.plt and
3566          .rel.iplt sections for STT_GNU_IFUNC symbols.  */
3567       if (htab->elf.splt != NULL)
3568         {
3569           plt = htab->elf.splt;
3570           gotplt = htab->elf.sgotplt;
3571           relplt = htab->elf.srelplt;
3572         }
3573       else
3574         {
3575           plt = htab->elf.iplt;
3576           gotplt = htab->elf.igotplt;
3577           relplt = htab->elf.irelplt;
3578         }
3579
3580       VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3581
3582       /* Get the index in the procedure linkage table which
3583          corresponds to this symbol.  This is the index of this symbol
3584          in all the symbols for which we are making plt entries.  The
3585          first entry in the procedure linkage table is reserved.
3586
3587          Get the offset into the .got table of the entry that
3588          corresponds to this function.  Each .got entry is 4 bytes.
3589          The first three are reserved.
3590
3591          For static executables, we don't reserve anything.  */
3592
3593       if (plt == htab->elf.splt)
3594         {
3595           got_offset = (h->plt.offset / plt_entry_size
3596                         - htab->plt.has_plt0);
3597           got_offset = (got_offset + 3) * 4;
3598         }
3599       else
3600         {
3601           got_offset = h->plt.offset / plt_entry_size;
3602           got_offset = got_offset * 4;
3603         }
3604
3605       /* Fill in the entry in the procedure linkage table and update
3606          the first slot.  */
3607       memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3608               plt_entry_size);
3609
3610       if (use_plt_second)
3611         {
3612           const bfd_byte *plt_entry;
3613           if (bfd_link_pic (info))
3614             plt_entry = htab->non_lazy_plt->pic_plt_entry;
3615           else
3616             plt_entry = htab->non_lazy_plt->plt_entry;
3617           memcpy (htab->plt_second->contents + eh->plt_second.offset,
3618                   plt_entry, htab->non_lazy_plt->plt_entry_size);
3619
3620           resolved_plt = htab->plt_second;
3621           plt_offset = eh->plt_second.offset;
3622         }
3623       else
3624         {
3625           resolved_plt = plt;
3626           plt_offset = h->plt.offset;
3627         }
3628
3629       if (! bfd_link_pic (info))
3630         {
3631           bfd_put_32 (output_bfd,
3632                       (gotplt->output_section->vma
3633                        + gotplt->output_offset
3634                        + got_offset),
3635                       resolved_plt->contents + plt_offset
3636                       + htab->plt.plt_got_offset);
3637
3638           if (htab->is_vxworks)
3639             {
3640               int s, k, reloc_index;
3641
3642               /* Create the R_386_32 relocation referencing the GOT
3643                  for this PLT entry.  */
3644
3645               /* S: Current slot number (zero-based).  */
3646               s = ((h->plt.offset - htab->plt.plt_entry_size)
3647                    / htab->plt.plt_entry_size);
3648               /* K: Number of relocations for PLTResolve. */
3649               if (bfd_link_pic (info))
3650                 k = PLTRESOLVE_RELOCS_SHLIB;
3651               else
3652                 k = PLTRESOLVE_RELOCS;
3653               /* Skip the PLTresolve relocations, and the relocations for
3654                  the other PLT slots. */
3655               reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3656               loc = (htab->srelplt2->contents + reloc_index
3657                      * sizeof (Elf32_External_Rel));
3658
3659               rel.r_offset = (plt->output_section->vma
3660                               + plt->output_offset
3661                               + h->plt.offset + 2),
3662               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3663               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3664
3665               /* Create the R_386_32 relocation referencing the beginning of
3666                  the PLT for this GOT entry.  */
3667               rel.r_offset = (htab->elf.sgotplt->output_section->vma
3668                               + htab->elf.sgotplt->output_offset
3669                               + got_offset);
3670               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3671               bfd_elf32_swap_reloc_out (output_bfd, &rel,
3672                                         loc + sizeof (Elf32_External_Rel));
3673             }
3674         }
3675       else
3676         {
3677           bfd_put_32 (output_bfd, got_offset,
3678                       resolved_plt->contents + plt_offset
3679                       + htab->plt.plt_got_offset);
3680         }
3681
3682       /* Fill in the entry in the global offset table.  Leave the entry
3683          as zero for undefined weak symbol in PIE.  No PLT relocation
3684          against undefined weak symbol in PIE.  */
3685       if (!local_undefweak)
3686         {
3687           if (htab->plt.has_plt0)
3688             bfd_put_32 (output_bfd,
3689                         (plt->output_section->vma
3690                          + plt->output_offset
3691                          + h->plt.offset
3692                          + htab->lazy_plt->plt_lazy_offset),
3693                         gotplt->contents + got_offset);
3694
3695           /* Fill in the entry in the .rel.plt section.  */
3696           rel.r_offset = (gotplt->output_section->vma
3697                           + gotplt->output_offset
3698                           + got_offset);
3699           if (PLT_LOCAL_IFUNC_P (info, h))
3700             {
3701               info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3702                                       h->root.root.string,
3703                                       h->root.u.def.section->owner);
3704
3705               /* If an STT_GNU_IFUNC symbol is locally defined, generate
3706                  R_386_IRELATIVE instead of R_386_JUMP_SLOT.  Store addend
3707                  in the .got.plt section.  */
3708               bfd_put_32 (output_bfd,
3709                           (h->root.u.def.value
3710                            + h->root.u.def.section->output_section->vma
3711                            + h->root.u.def.section->output_offset),
3712                           gotplt->contents + got_offset);
3713               rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3714               /* R_386_IRELATIVE comes last.  */
3715               plt_index = htab->next_irelative_index--;
3716             }
3717           else
3718             {
3719               rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3720               plt_index = htab->next_jump_slot_index++;
3721             }
3722
3723           loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3724           bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3725
3726           /* Don't fill the second and third slots in PLT entry for
3727              static executables nor without PLT0.  */
3728           if (plt == htab->elf.splt && htab->plt.has_plt0)
3729             {
3730               bfd_put_32 (output_bfd,
3731                           plt_index * sizeof (Elf32_External_Rel),
3732                           plt->contents + h->plt.offset
3733                           + htab->lazy_plt->plt_reloc_offset);
3734               bfd_put_32 (output_bfd,
3735                           - (h->plt.offset
3736                              + htab->lazy_plt->plt_plt_offset + 4),
3737                           (plt->contents + h->plt.offset
3738                            + htab->lazy_plt->plt_plt_offset));
3739             }
3740         }
3741     }
3742   else if (eh->plt_got.offset != (bfd_vma) -1)
3743     {
3744       bfd_vma got_offset, plt_offset;
3745       asection *plt, *got, *gotplt;
3746       const bfd_byte *got_plt_entry;
3747
3748       /* Set the entry in the GOT procedure linkage table.  */
3749       plt = htab->plt_got;
3750       got = htab->elf.sgot;
3751       gotplt = htab->elf.sgotplt;
3752       got_offset = h->got.offset;
3753
3754       if (got_offset == (bfd_vma) -1
3755           || plt == NULL
3756           || got == NULL
3757           || gotplt == NULL)
3758         abort ();
3759
3760       /* Fill in the entry in the GOT procedure linkage table.  */
3761       if (! bfd_link_pic (info))
3762         {
3763           got_plt_entry = htab->non_lazy_plt->plt_entry;
3764           got_offset += got->output_section->vma + got->output_offset;
3765         }
3766       else
3767         {
3768           got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3769           got_offset += (got->output_section->vma
3770                          + got->output_offset
3771                          - gotplt->output_section->vma
3772                          - gotplt->output_offset);
3773         }
3774
3775       plt_offset = eh->plt_got.offset;
3776       memcpy (plt->contents + plt_offset, got_plt_entry,
3777               htab->non_lazy_plt->plt_entry_size);
3778       bfd_put_32 (output_bfd, got_offset,
3779                   (plt->contents + plt_offset
3780                    + htab->non_lazy_plt->plt_got_offset));
3781     }
3782
3783   if (!local_undefweak
3784       && !h->def_regular
3785       && (h->plt.offset != (bfd_vma) -1
3786           || eh->plt_got.offset != (bfd_vma) -1))
3787     {
3788       /* Mark the symbol as undefined, rather than as defined in
3789          the .plt section.  Leave the value if there were any
3790          relocations where pointer equality matters (this is a clue
3791          for the dynamic linker, to make function pointer
3792          comparisons work between an application and shared
3793          library), otherwise set it to zero.  If a function is only
3794          called from a binary, there is no need to slow down
3795          shared libraries because of that.  */
3796       sym->st_shndx = SHN_UNDEF;
3797       if (!h->pointer_equality_needed)
3798         sym->st_value = 0;
3799     }
3800
3801   /* Don't generate dynamic GOT relocation against undefined weak
3802      symbol in executable.  */
3803   if (h->got.offset != (bfd_vma) -1
3804       && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3805       && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3806       && !local_undefweak)
3807     {
3808       Elf_Internal_Rela rel;
3809       asection *relgot = htab->elf.srelgot;
3810
3811       /* This symbol has an entry in the global offset table.  Set it
3812          up.  */
3813
3814       if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3815         abort ();
3816
3817       rel.r_offset = (htab->elf.sgot->output_section->vma
3818                       + htab->elf.sgot->output_offset
3819                       + (h->got.offset & ~(bfd_vma) 1));
3820
3821       /* If this is a static link, or it is a -Bsymbolic link and the
3822          symbol is defined locally or was forced to be local because
3823          of a version file, we just want to emit a RELATIVE reloc.
3824          The entry in the global offset table will already have been
3825          initialized in the relocate_section function.  */
3826       if (h->def_regular
3827           && h->type == STT_GNU_IFUNC)
3828         {
3829           if (h->plt.offset == (bfd_vma) -1)
3830             {
3831               /* STT_GNU_IFUNC is referenced without PLT.  */
3832               if (htab->elf.splt == NULL)
3833                 {
3834                   /* use .rel[a].iplt section to store .got relocations
3835                      in static executable.  */
3836                   relgot = htab->elf.irelplt;
3837                 }
3838               if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3839                 {
3840                   info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3841                                           h->root.root.string,
3842                                           h->root.u.def.section->owner);
3843
3844                   bfd_put_32 (output_bfd,
3845                               (h->root.u.def.value
3846                                + h->root.u.def.section->output_section->vma
3847                                + h->root.u.def.section->output_offset),
3848                               htab->elf.sgot->contents + h->got.offset);
3849                   rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3850                 }
3851               else
3852                 goto do_glob_dat;
3853             }
3854           else if (bfd_link_pic (info))
3855             {
3856               /* Generate R_386_GLOB_DAT.  */
3857               goto do_glob_dat;
3858             }
3859           else
3860             {
3861               asection *plt;
3862               bfd_vma plt_offset;
3863
3864               if (!h->pointer_equality_needed)
3865                 abort ();
3866
3867               /* For non-shared object, we can't use .got.plt, which
3868                  contains the real function addres if we need pointer
3869                  equality.  We load the GOT entry with the PLT entry.  */
3870               if (htab->plt_second != NULL)
3871                 {
3872                   plt = htab->plt_second;
3873                   plt_offset = eh->plt_second.offset;
3874                 }
3875               else
3876                 {
3877                   plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3878                   plt_offset = h->plt.offset;
3879                 }
3880               bfd_put_32 (output_bfd,
3881                           (plt->output_section->vma
3882                            + plt->output_offset + plt_offset),
3883                           htab->elf.sgot->contents + h->got.offset);
3884               return TRUE;
3885             }
3886         }
3887       else if (bfd_link_pic (info)
3888                && SYMBOL_REFERENCES_LOCAL_P (info, h))
3889         {
3890           BFD_ASSERT((h->got.offset & 1) != 0);
3891           rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3892         }
3893       else
3894         {
3895           BFD_ASSERT((h->got.offset & 1) == 0);
3896 do_glob_dat:
3897           bfd_put_32 (output_bfd, (bfd_vma) 0,
3898                       htab->elf.sgot->contents + h->got.offset);
3899           rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3900         }
3901
3902       elf_append_rel (output_bfd, relgot, &rel);
3903     }
3904
3905   if (h->needs_copy)
3906     {
3907       Elf_Internal_Rela rel;
3908       asection *s;
3909
3910       /* This symbol needs a copy reloc.  Set it up.  */
3911       VERIFY_COPY_RELOC (h, htab)
3912
3913       rel.r_offset = (h->root.u.def.value
3914                       + h->root.u.def.section->output_section->vma
3915                       + h->root.u.def.section->output_offset);
3916       rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3917       if (h->root.u.def.section == htab->elf.sdynrelro)
3918         s = htab->elf.sreldynrelro;
3919       else
3920         s = htab->elf.srelbss;
3921       elf_append_rel (output_bfd, s, &rel);
3922     }
3923
3924   return TRUE;
3925 }
3926
3927 /* Finish up local dynamic symbol handling.  We set the contents of
3928    various dynamic sections here.  */
3929
3930 static bfd_boolean
3931 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
3932 {
3933   struct elf_link_hash_entry *h
3934     = (struct elf_link_hash_entry *) *slot;
3935   struct bfd_link_info *info
3936     = (struct bfd_link_info *) inf;
3937
3938   return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
3939                                          h, NULL);
3940 }
3941
3942 /* Finish up undefined weak symbol handling in PIE.  Fill its PLT entry
3943    here since undefined weak symbol may not be dynamic and may not be
3944    called for elf_i386_finish_dynamic_symbol.  */
3945
3946 static bfd_boolean
3947 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
3948                                       void *inf)
3949 {
3950   struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
3951   struct bfd_link_info *info = (struct bfd_link_info *) inf;
3952
3953   if (h->root.type != bfd_link_hash_undefweak
3954       || h->dynindx != -1)
3955     return TRUE;
3956
3957   return elf_i386_finish_dynamic_symbol (info->output_bfd,
3958                                          info, h, NULL);
3959 }
3960
3961 /* Used to decide how to sort relocs in an optimal manner for the
3962    dynamic linker, before writing them out.  */
3963
3964 static enum elf_reloc_type_class
3965 elf_i386_reloc_type_class (const struct bfd_link_info *info,
3966                            const asection *rel_sec ATTRIBUTE_UNUSED,
3967                            const Elf_Internal_Rela *rela)
3968 {
3969   bfd *abfd = info->output_bfd;
3970   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3971   struct elf_link_hash_table *htab = elf_hash_table (info);
3972
3973   if (htab->dynsym != NULL
3974       && htab->dynsym->contents != NULL)
3975     {
3976       /* Check relocation against STT_GNU_IFUNC symbol if there are
3977          dynamic symbols.  */
3978       unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
3979       if (r_symndx != STN_UNDEF)
3980         {
3981           Elf_Internal_Sym sym;
3982           if (!bed->s->swap_symbol_in (abfd,
3983                                        (htab->dynsym->contents
3984                                         + r_symndx * sizeof (Elf32_External_Sym)),
3985                                        0, &sym))
3986             abort ();
3987
3988           if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3989             return reloc_class_ifunc;
3990         }
3991     }
3992
3993   switch (ELF32_R_TYPE (rela->r_info))
3994     {
3995     case R_386_IRELATIVE:
3996       return reloc_class_ifunc;
3997     case R_386_RELATIVE:
3998       return reloc_class_relative;
3999     case R_386_JUMP_SLOT:
4000       return reloc_class_plt;
4001     case R_386_COPY:
4002       return reloc_class_copy;
4003     default:
4004       return reloc_class_normal;
4005     }
4006 }
4007
4008 /* Finish up the dynamic sections.  */
4009
4010 static bfd_boolean
4011 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4012                                   struct bfd_link_info *info)
4013 {
4014   struct elf_x86_link_hash_table *htab;
4015   bfd *dynobj;
4016   asection *sdyn;
4017   const struct elf_i386_backend_data *abed;
4018
4019   htab = elf_x86_hash_table (info, I386_ELF_DATA);
4020   if (htab == NULL)
4021     return FALSE;
4022
4023   dynobj = htab->elf.dynobj;
4024   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4025   abed = get_elf_i386_backend_data (output_bfd);
4026
4027   if (htab->elf.dynamic_sections_created)
4028     {
4029       Elf32_External_Dyn *dyncon, *dynconend;
4030
4031       if (sdyn == NULL || htab->elf.sgot == NULL)
4032         abort ();
4033
4034       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4035       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4036       for (; dyncon < dynconend; dyncon++)
4037         {
4038           Elf_Internal_Dyn dyn;
4039           asection *s;
4040
4041           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4042
4043           switch (dyn.d_tag)
4044             {
4045             default:
4046               if (htab->is_vxworks
4047                   && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4048                 break;
4049               continue;
4050
4051             case DT_PLTGOT:
4052               s = htab->elf.sgotplt;
4053               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4054               break;
4055
4056             case DT_JMPREL:
4057               s = htab->elf.srelplt;
4058               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4059               break;
4060
4061             case DT_PLTRELSZ:
4062               s = htab->elf.srelplt;
4063               dyn.d_un.d_val = s->size;
4064               break;
4065             }
4066
4067           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4068         }
4069
4070       if (htab->elf.splt && htab->elf.splt->size > 0)
4071         {
4072           /* UnixWare sets the entsize of .plt to 4, although that doesn't
4073              really seem like the right value.  */
4074           elf_section_data (htab->elf.splt->output_section)
4075             ->this_hdr.sh_entsize = 4;
4076
4077           if (htab->plt.has_plt0)
4078             {
4079               /* Fill in the special first entry in the procedure linkage
4080                  table.  */
4081               memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4082                       htab->lazy_plt->plt0_entry_size);
4083               memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4084                       abed->plt0_pad_byte,
4085                       htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4086               if (!bfd_link_pic (info))
4087                 {
4088                   bfd_put_32 (output_bfd,
4089                               (htab->elf.sgotplt->output_section->vma
4090                                + htab->elf.sgotplt->output_offset
4091                                + 4),
4092                               htab->elf.splt->contents
4093                               + htab->lazy_plt->plt0_got1_offset);
4094                   bfd_put_32 (output_bfd,
4095                               (htab->elf.sgotplt->output_section->vma
4096                                + htab->elf.sgotplt->output_offset
4097                                + 8),
4098                               htab->elf.splt->contents
4099                               + htab->lazy_plt->plt0_got2_offset);
4100
4101                   if (htab->is_vxworks)
4102                     {
4103                       Elf_Internal_Rela rel;
4104                       int num_plts = (htab->elf.splt->size
4105                                       / htab->plt.plt_entry_size) - 1;
4106                       unsigned char *p;
4107                       asection *srelplt2 = htab->srelplt2;
4108
4109                       /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4110                          + 4.  On IA32 we use REL relocations so the
4111                          addend goes in the PLT directly.  */
4112                       rel.r_offset = (htab->elf.splt->output_section->vma
4113                                       + htab->elf.splt->output_offset
4114                                       + htab->lazy_plt->plt0_got1_offset);
4115                       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4116                                                  R_386_32);
4117                       bfd_elf32_swap_reloc_out (output_bfd, &rel,
4118                                                 srelplt2->contents);
4119                       /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4120                          + 8.  */
4121                       rel.r_offset = (htab->elf.splt->output_section->vma
4122                                       + htab->elf.splt->output_offset
4123                                       + htab->lazy_plt->plt0_got2_offset);
4124                       rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4125                                                  R_386_32);
4126                       bfd_elf32_swap_reloc_out (output_bfd, &rel,
4127                                                 srelplt2->contents +
4128                                                 sizeof (Elf32_External_Rel));
4129                       /* Correct the .rel.plt.unloaded relocations.  */
4130                       p = srelplt2->contents;
4131                       if (bfd_link_pic (info))
4132                         p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4133                       else
4134                         p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4135
4136                       for (; num_plts; num_plts--)
4137                         {
4138                           bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4139                           rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4140                                                      R_386_32);
4141                           bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4142                           p += sizeof (Elf32_External_Rel);
4143
4144                           bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4145                           rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4146                                                      R_386_32);
4147                           bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4148                           p += sizeof (Elf32_External_Rel);
4149                         }
4150                     }
4151                 }
4152             }
4153         }
4154
4155       if (htab->plt_got != NULL && htab->plt_got->size > 0)
4156         elf_section_data (htab->plt_got->output_section)
4157           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4158
4159       if (htab->plt_second != NULL && htab->plt_second->size > 0)
4160         elf_section_data (htab->plt_second->output_section)
4161           ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4162     }
4163
4164   /* Fill in the first three entries in the global offset table.  */
4165   if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
4166     {
4167       if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4168         {
4169           _bfd_error_handler
4170             (_("discarded output section: `%A'"), htab->elf.sgotplt);
4171           return FALSE;
4172         }
4173
4174       bfd_put_32 (output_bfd,
4175                   (sdyn == NULL ? 0
4176                    : sdyn->output_section->vma + sdyn->output_offset),
4177                   htab->elf.sgotplt->contents);
4178       bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4179       bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4180
4181       elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4182     }
4183
4184   /* Adjust .eh_frame for .plt section.  */
4185   if (htab->plt_eh_frame != NULL
4186       && htab->plt_eh_frame->contents != NULL)
4187     {
4188       if (htab->elf.splt != NULL
4189           && htab->elf.splt->size != 0
4190           && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4191           && htab->elf.splt->output_section != NULL
4192           && htab->plt_eh_frame->output_section != NULL)
4193         {
4194           bfd_vma plt_start = htab->elf.splt->output_section->vma;
4195           bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4196                                    + htab->plt_eh_frame->output_offset
4197                                    + PLT_FDE_START_OFFSET;
4198           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4199                              htab->plt_eh_frame->contents
4200                              + PLT_FDE_START_OFFSET);
4201         }
4202       if (htab->plt_eh_frame->sec_info_type
4203           == SEC_INFO_TYPE_EH_FRAME)
4204         {
4205           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4206                                                  htab->plt_eh_frame,
4207                                                  htab->plt_eh_frame->contents))
4208             return FALSE;
4209         }
4210     }
4211
4212   /* Adjust .eh_frame for .plt.got section.  */
4213   if (htab->plt_got_eh_frame != NULL
4214       && htab->plt_got_eh_frame->contents != NULL)
4215     {
4216       if (htab->plt_got != NULL
4217           && htab->plt_got->size != 0
4218           && (htab->plt_got->flags & SEC_EXCLUDE) == 0
4219           && htab->plt_got->output_section != NULL
4220           && htab->plt_got_eh_frame->output_section != NULL)
4221         {
4222           bfd_vma plt_start = htab->plt_got->output_section->vma;
4223           bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
4224                                    + htab->plt_got_eh_frame->output_offset
4225                                    + PLT_FDE_START_OFFSET;
4226           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4227                              htab->plt_got_eh_frame->contents
4228                              + PLT_FDE_START_OFFSET);
4229         }
4230       if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4231         {
4232           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4233                                                  htab->plt_got_eh_frame,
4234                                                  htab->plt_got_eh_frame->contents))
4235             return FALSE;
4236         }
4237     }
4238
4239   /* Adjust .eh_frame for the second PLT section.  */
4240   if (htab->plt_second_eh_frame != NULL
4241       && htab->plt_second_eh_frame->contents != NULL)
4242     {
4243       if (htab->plt_second != NULL
4244           && htab->plt_second->size != 0
4245           && (htab->plt_second->flags & SEC_EXCLUDE) == 0
4246           && htab->plt_second->output_section != NULL
4247           && htab->plt_second_eh_frame->output_section != NULL)
4248         {
4249           bfd_vma plt_start = htab->plt_second->output_section->vma;
4250           bfd_vma eh_frame_start
4251             = (htab->plt_second_eh_frame->output_section->vma
4252                + htab->plt_second_eh_frame->output_offset
4253                + PLT_FDE_START_OFFSET);
4254           bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4255                              htab->plt_second_eh_frame->contents
4256                              + PLT_FDE_START_OFFSET);
4257         }
4258       if (htab->plt_second_eh_frame->sec_info_type
4259           == SEC_INFO_TYPE_EH_FRAME)
4260         {
4261           if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4262                                                  htab->plt_second_eh_frame,
4263                                                  htab->plt_second_eh_frame->contents))
4264             return FALSE;
4265         }
4266     }
4267
4268   if (htab->elf.sgot && htab->elf.sgot->size > 0)
4269     elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4270
4271   /* Fill PLT entries for undefined weak symbols in PIE.  */
4272   if (bfd_link_pie (info))
4273     bfd_hash_traverse (&info->hash->table,
4274                        elf_i386_pie_finish_undefweak_symbol,
4275                        info);
4276
4277   return TRUE;
4278 }
4279
4280 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4281    STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4282    It has to be done before elf_link_sort_relocs is called so that
4283    dynamic relocations are properly sorted.  */
4284
4285 static bfd_boolean
4286 elf_i386_output_arch_local_syms
4287   (bfd *output_bfd ATTRIBUTE_UNUSED,
4288    struct bfd_link_info *info,
4289    void *flaginfo ATTRIBUTE_UNUSED,
4290    int (*func) (void *, const char *,
4291                 Elf_Internal_Sym *,
4292                 asection *,
4293                 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4294 {
4295   struct elf_x86_link_hash_table *htab
4296     = elf_x86_hash_table (info, I386_ELF_DATA);
4297   if (htab == NULL)
4298     return FALSE;
4299
4300   /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols.  */
4301   htab_traverse (htab->loc_hash_table,
4302                  elf_i386_finish_local_dynamic_symbol,
4303                  info);
4304
4305   return TRUE;
4306 }
4307
4308 /* Forward declaration.  */
4309 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
4310
4311 /* Similar to _bfd_elf_get_synthetic_symtab.  Support PLTs with all
4312    dynamic relocations.   */
4313
4314 static long
4315 elf_i386_get_synthetic_symtab (bfd *abfd,
4316                                long symcount ATTRIBUTE_UNUSED,
4317                                asymbol **syms ATTRIBUTE_UNUSED,
4318                                long dynsymcount,
4319                                asymbol **dynsyms,
4320                                asymbol **ret)
4321 {
4322   long count, i, n;
4323   int j;
4324   bfd_byte *plt_contents;
4325   long relsize;
4326   const struct elf_x86_lazy_plt_layout *lazy_plt;
4327   const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4328   const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4329   const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4330   asection *plt;
4331   bfd_vma got_addr;
4332   enum elf_x86_plt_type plt_type;
4333   struct elf_x86_plt plts[] =
4334     {
4335       { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4336       { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4337       { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4338       { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4339     };
4340
4341   *ret = NULL;
4342
4343   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4344     return 0;
4345
4346   if (dynsymcount <= 0)
4347     return 0;
4348
4349   relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4350   if (relsize <= 0)
4351     return -1;
4352
4353   non_lazy_plt = NULL;
4354   /* Silence GCC 6.  */
4355   lazy_plt = NULL;
4356   non_lazy_ibt_plt = NULL;
4357   lazy_ibt_plt = NULL;
4358   switch (get_elf_i386_backend_data (abfd)->os)
4359     {
4360     case is_normal:
4361       non_lazy_plt = &elf_i386_non_lazy_plt;
4362       lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4363       non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4364       /* Fall through */
4365     case is_vxworks:
4366       lazy_plt = &elf_i386_lazy_plt;
4367       break;
4368     case is_nacl:
4369       lazy_plt = &elf_i386_nacl_plt;
4370       break;
4371     }
4372
4373   got_addr = 0;
4374
4375   count = 0;
4376   for (j = 0; plts[j].name != NULL; j++)
4377     {
4378       plt = bfd_get_section_by_name (abfd, plts[j].name);
4379       if (plt == NULL || plt->size == 0)
4380         continue;
4381
4382       /* Get the PLT section contents.  */
4383       plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4384       if (plt_contents == NULL)
4385         break;
4386       if (!bfd_get_section_contents (abfd, (asection *) plt,
4387                                      plt_contents, 0, plt->size))
4388         {
4389           free (plt_contents);
4390           break;
4391         }
4392
4393       /* Check what kind of PLT it is.  */
4394       plt_type = plt_unknown;
4395       if (plts[j].type == plt_unknown
4396           && (plt->size >= (lazy_plt->plt0_entry_size
4397                             + lazy_plt->plt_entry_size)))
4398         {
4399           /* Match lazy PLT first.  */
4400           if (memcmp (plt_contents, lazy_plt->plt0_entry,
4401                       lazy_plt->plt0_got1_offset) == 0)
4402             {
4403               /* The fist entry in the lazy IBT PLT is the same as the
4404                  normal lazy PLT.  */
4405               if (lazy_ibt_plt != NULL
4406                   && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4407                               lazy_ibt_plt->plt_entry,
4408                               lazy_ibt_plt->plt_got_offset) == 0))
4409                 plt_type = plt_lazy | plt_second;
4410               else
4411                 plt_type = plt_lazy;
4412             }
4413           else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4414                            lazy_plt->plt0_got1_offset) == 0)
4415             {
4416               /* The fist entry in the PIC lazy IBT PLT is the same as
4417                  the normal PIC lazy PLT.  */
4418               if (lazy_ibt_plt != NULL
4419                   && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4420                               lazy_ibt_plt->pic_plt_entry,
4421                               lazy_ibt_plt->plt_got_offset) == 0))
4422                 plt_type = plt_lazy | plt_pic | plt_second;
4423               else
4424                 plt_type = plt_lazy | plt_pic;
4425             }
4426         }
4427
4428       if (non_lazy_plt != NULL
4429           && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4430           && plt->size >= non_lazy_plt->plt_entry_size)
4431         {
4432           /* Match non-lazy PLT.  */
4433           if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4434                       non_lazy_plt->plt_got_offset) == 0)
4435             plt_type = plt_non_lazy;
4436           else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4437                            non_lazy_plt->plt_got_offset) == 0)
4438             plt_type = plt_pic;
4439         }
4440
4441       if ((non_lazy_ibt_plt != NULL)
4442           && (plt_type == plt_unknown || plt_type == plt_second)
4443           && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4444         {
4445           if (memcmp (plt_contents,
4446                       non_lazy_ibt_plt->plt_entry,
4447                       non_lazy_ibt_plt->plt_got_offset) == 0)
4448             {
4449               /* Match IBT PLT.  */
4450               plt_type = plt_second;
4451               non_lazy_plt = non_lazy_ibt_plt;
4452             }
4453           else if (memcmp (plt_contents,
4454                            non_lazy_ibt_plt->pic_plt_entry,
4455                            non_lazy_ibt_plt->plt_got_offset) == 0)
4456             {
4457               /* Match PIC IBT PLT.  */
4458               plt_type = plt_second | plt_pic;
4459               non_lazy_plt = non_lazy_ibt_plt;
4460             }
4461         }
4462
4463       if (plt_type == plt_unknown)
4464         {
4465           free (plt_contents);
4466           continue;
4467         }
4468
4469       plts[j].sec = plt;
4470       plts[j].type = plt_type;
4471
4472       if ((plt_type & plt_lazy))
4473         {
4474           plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4475           plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4476           /* Skip PLT0 in lazy PLT.  */
4477           i = 1;
4478         }
4479       else
4480         {
4481           plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4482           plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4483           i = 0;
4484         }
4485
4486       /* Skip lazy PLT when the second PLT is used.  */
4487       if ((plt_type & (plt_lazy | plt_second))
4488           == (plt_lazy | plt_second))
4489         plts[j].count = 0;
4490       else
4491         {
4492           n = plt->size / plts[j].plt_entry_size;
4493           plts[j].count = n;
4494           count += n - i;
4495         }
4496
4497       plts[j].contents = plt_contents;
4498
4499       /* The _GLOBAL_OFFSET_TABLE_ address is needed.  */
4500       if ((plt_type & plt_pic))
4501         got_addr = (bfd_vma) -1;
4502     }
4503
4504   return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4505                                             got_addr, plts, dynsyms,
4506                                             ret);
4507 }
4508
4509 /* Set up i386 GNU properties.  Return the first relocatable ELF input
4510    with GNU properties if found.  Otherwise, return NULL.  */
4511
4512 static bfd *
4513 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4514 {
4515   struct elf_x86_init_table init_table;
4516
4517   init_table.normal_target = FALSE;
4518   init_table.is_vxworks = FALSE;
4519   switch (get_elf_i386_backend_data (info->output_bfd)->os)
4520     {
4521     case is_normal:
4522       init_table.lazy_plt = &elf_i386_lazy_plt;
4523       init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4524       init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4525       init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4526       init_table.normal_target = TRUE;
4527       break;
4528     case is_vxworks:
4529       init_table.lazy_plt = &elf_i386_lazy_plt;
4530       init_table.non_lazy_plt = NULL;
4531       init_table.lazy_ibt_plt = NULL;
4532       init_table.non_lazy_ibt_plt = NULL;
4533       init_table.is_vxworks = TRUE;
4534       break;
4535     case is_nacl:
4536       init_table.lazy_plt = &elf_i386_nacl_plt;
4537       init_table.non_lazy_plt = NULL;
4538       init_table.lazy_ibt_plt = NULL;
4539       init_table.non_lazy_ibt_plt = NULL;
4540       break;
4541     }
4542
4543   init_table.r_info = elf32_r_info;
4544   init_table.r_sym = elf32_r_sym;
4545
4546   return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4547 }
4548
4549 #define TARGET_LITTLE_SYM               i386_elf32_vec
4550 #define TARGET_LITTLE_NAME              "elf32-i386"
4551 #define ELF_ARCH                        bfd_arch_i386
4552 #define ELF_TARGET_ID                   I386_ELF_DATA
4553 #define ELF_MACHINE_CODE                EM_386
4554 #define ELF_MAXPAGESIZE                 0x1000
4555
4556 #define elf_backend_can_gc_sections     1
4557 #define elf_backend_can_refcount        1
4558 #define elf_backend_want_got_plt        1
4559 #define elf_backend_plt_readonly        1
4560 #define elf_backend_want_plt_sym        0
4561 #define elf_backend_got_header_size     12
4562 #define elf_backend_plt_alignment       4
4563 #define elf_backend_dtrel_excludes_plt  1
4564 #define elf_backend_extern_protected_data 1
4565 #define elf_backend_caches_rawsize      1
4566 #define elf_backend_want_dynrelro       1
4567
4568 /* Support RELA for objdump of prelink objects.  */
4569 #define elf_info_to_howto                     elf_i386_info_to_howto_rel
4570 #define elf_info_to_howto_rel                 elf_i386_info_to_howto_rel
4571
4572 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
4573 #define bfd_elf32_bfd_reloc_type_lookup       elf_i386_reloc_type_lookup
4574 #define bfd_elf32_bfd_reloc_name_lookup       elf_i386_reloc_name_lookup
4575 #define bfd_elf32_get_synthetic_symtab        elf_i386_get_synthetic_symtab
4576
4577 #define elf_backend_relocs_compatible         _bfd_elf_relocs_compatible
4578 #define elf_backend_check_relocs              elf_i386_check_relocs
4579 #define elf_backend_create_dynamic_sections   _bfd_elf_create_dynamic_sections
4580 #define elf_backend_fake_sections             elf_i386_fake_sections
4581 #define elf_backend_finish_dynamic_sections   elf_i386_finish_dynamic_sections
4582 #define elf_backend_finish_dynamic_symbol     elf_i386_finish_dynamic_symbol
4583 #define elf_backend_output_arch_local_syms     elf_i386_output_arch_local_syms
4584 #define elf_backend_grok_prstatus             elf_i386_grok_prstatus
4585 #define elf_backend_grok_psinfo               elf_i386_grok_psinfo
4586 #define elf_backend_reloc_type_class          elf_i386_reloc_type_class
4587 #define elf_backend_relocate_section          elf_i386_relocate_section
4588 #define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
4589
4590 #define elf_backend_linux_prpsinfo32_ugid16     TRUE
4591
4592 #include "elf32-target.h"
4593
4594 /* FreeBSD support.  */
4595
4596 #undef  TARGET_LITTLE_SYM
4597 #define TARGET_LITTLE_SYM               i386_elf32_fbsd_vec
4598 #undef  TARGET_LITTLE_NAME
4599 #define TARGET_LITTLE_NAME              "elf32-i386-freebsd"
4600 #undef  ELF_OSABI
4601 #define ELF_OSABI                       ELFOSABI_FREEBSD
4602
4603 /* The kernel recognizes executables as valid only if they carry a
4604    "FreeBSD" label in the ELF header.  So we put this label on all
4605    executables and (for simplicity) also all other object files.  */
4606
4607 static void
4608 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4609 {
4610   _bfd_elf_post_process_headers (abfd, info);
4611
4612 #ifdef OLD_FREEBSD_ABI_LABEL
4613   {
4614     /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard.  */
4615     Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4616     memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4617   }
4618 #endif
4619 }
4620
4621 #undef  elf_backend_post_process_headers
4622 #define elf_backend_post_process_headers        elf_i386_fbsd_post_process_headers
4623 #undef  elf32_bed
4624 #define elf32_bed                               elf32_i386_fbsd_bed
4625
4626 #undef elf_backend_add_symbol_hook
4627
4628 #include "elf32-target.h"
4629
4630 /* Solaris 2.  */
4631
4632 #undef  TARGET_LITTLE_SYM
4633 #define TARGET_LITTLE_SYM               i386_elf32_sol2_vec
4634 #undef  TARGET_LITTLE_NAME
4635 #define TARGET_LITTLE_NAME              "elf32-i386-sol2"
4636
4637 #undef elf_backend_post_process_headers
4638
4639 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4640    objects won't be recognized.  */
4641 #undef ELF_OSABI
4642
4643 #undef  elf32_bed
4644 #define elf32_bed                       elf32_i386_sol2_bed
4645
4646 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4647    boundary.  */
4648 #undef  elf_backend_static_tls_alignment
4649 #define elf_backend_static_tls_alignment 8
4650
4651 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4652
4653    Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4654    File, p.63.  */
4655 #undef  elf_backend_want_plt_sym
4656 #define elf_backend_want_plt_sym        1
4657
4658 #undef  elf_backend_strtab_flags
4659 #define elf_backend_strtab_flags        SHF_STRINGS
4660
4661 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4662    has a type >= SHT_LOOS.  Returns TRUE if these fields were initialised 
4663    FALSE otherwise.  ISECTION is the best guess matching section from the
4664    input bfd IBFD, but it might be NULL.  */
4665
4666 static bfd_boolean
4667 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4668                                                 bfd *obfd ATTRIBUTE_UNUSED,
4669                                                 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4670                                                 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4671 {
4672   /* PR 19938: FIXME: Need to add code for setting the sh_info
4673      and sh_link fields of Solaris specific section types.  */
4674   return FALSE;
4675
4676   /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4677      Object File Format, Table 13-9  ELF sh_link and sh_info Interpretation:
4678
4679 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4680
4681      The following values should be set:
4682      
4683 Type                 Link                           Info
4684 -----------------------------------------------------------------------------
4685 SHT_SUNW_ancillary   The section header index of    0
4686  [0x6fffffee]        the associated string table.
4687         
4688 SHT_SUNW_capinfo     The section header index of    For a dynamic object, the
4689  [0x6ffffff0]        the associated symbol table.   section header index of
4690                                                     the associated
4691                                                     SHT_SUNW_capchain table,
4692                                                     otherwise 0.
4693
4694 SHT_SUNW_symsort     The section header index of    0
4695  [0x6ffffff1]        the associated symbol table.
4696
4697 SHT_SUNW_tlssort     The section header index of    0
4698  [0x6ffffff2]        the associated symbol table.
4699         
4700 SHT_SUNW_LDYNSYM     The section header index of    One greater than the 
4701  [0x6ffffff3]        the associated string table.   symbol table index of the
4702                      This index is the same string  last local symbol, 
4703                      table used by the SHT_DYNSYM   STB_LOCAL. Since
4704                      section.                       SHT_SUNW_LDYNSYM only
4705                                                     contains local symbols,
4706                                                     sh_info is equivalent to
4707                                                     the number of symbols in
4708                                                     the table.
4709
4710 SHT_SUNW_cap         If symbol capabilities exist,  If any capabilities refer
4711  [0x6ffffff5]        the section header index of    to named strings, the
4712                      the associated                 section header index of
4713                      SHT_SUNW_capinfo table,        the associated string 
4714                           otherwise 0.              table, otherwise 0.
4715
4716 SHT_SUNW_move        The section header index of    0
4717  [0x6ffffffa]        the associated symbol table.
4718         
4719 SHT_SUNW_COMDAT      0                              0
4720  [0x6ffffffb]
4721
4722 SHT_SUNW_syminfo     The section header index of    The section header index
4723  [0x6ffffffc]        the associated symbol table.   of the associated
4724                                                     .dynamic section.
4725
4726 SHT_SUNW_verdef      The section header index of    The number of version 
4727  [0x6ffffffd]        the associated string table.   definitions within the
4728                                                     section.
4729
4730 SHT_SUNW_verneed     The section header index of    The number of version
4731  [0x6ffffffe]        the associated string table.   dependencies within the
4732                                                     section.
4733
4734 SHT_SUNW_versym      The section header index of    0
4735  [0x6fffffff]        the associated symbol table.  */
4736 }
4737
4738 #undef  elf_backend_copy_special_section_fields
4739 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4740
4741 #include "elf32-target.h"
4742
4743 /* Intel MCU support.  */
4744
4745 static bfd_boolean
4746 elf32_iamcu_elf_object_p (bfd *abfd)
4747 {
4748   /* Set the right machine number for an IAMCU elf32 file.  */
4749   bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4750   return TRUE;
4751 }
4752
4753 #undef  TARGET_LITTLE_SYM
4754 #define TARGET_LITTLE_SYM               iamcu_elf32_vec
4755 #undef  TARGET_LITTLE_NAME
4756 #define TARGET_LITTLE_NAME              "elf32-iamcu"
4757 #undef  ELF_ARCH
4758 #define ELF_ARCH                        bfd_arch_iamcu
4759
4760 #undef  ELF_MACHINE_CODE
4761 #define ELF_MACHINE_CODE                EM_IAMCU
4762
4763 #undef  ELF_OSABI
4764
4765 #undef  elf32_bed
4766 #define elf32_bed                       elf32_iamcu_bed
4767
4768 #undef  elf_backend_object_p
4769 #define elf_backend_object_p            elf32_iamcu_elf_object_p
4770
4771 #undef  elf_backend_static_tls_alignment
4772
4773 #undef  elf_backend_want_plt_sym
4774 #define elf_backend_want_plt_sym            0
4775
4776 #undef  elf_backend_strtab_flags
4777 #undef  elf_backend_copy_special_section_fields
4778
4779 #include "elf32-target.h"
4780
4781 /* Restore defaults.  */
4782 #undef  ELF_ARCH
4783 #define ELF_ARCH                        bfd_arch_i386
4784 #undef  ELF_MACHINE_CODE
4785 #define ELF_MACHINE_CODE                EM_386
4786
4787 /* Native Client support.  */
4788
4789 #undef  TARGET_LITTLE_SYM
4790 #define TARGET_LITTLE_SYM               i386_elf32_nacl_vec
4791 #undef  TARGET_LITTLE_NAME
4792 #define TARGET_LITTLE_NAME              "elf32-i386-nacl"
4793 #undef  elf32_bed
4794 #define elf32_bed                       elf32_i386_nacl_bed
4795
4796 #undef  ELF_MAXPAGESIZE
4797 #define ELF_MAXPAGESIZE                 0x10000
4798
4799 /* Restore defaults.  */
4800 #undef  ELF_OSABI
4801 #undef  elf_backend_want_plt_sym
4802 #define elf_backend_want_plt_sym        0
4803 #undef  elf_backend_post_process_headers
4804 #undef  elf_backend_static_tls_alignment
4805
4806 /* NaCl uses substantially different PLT entries for the same effects.  */
4807
4808 #undef  elf_backend_plt_alignment
4809 #define elf_backend_plt_alignment       5
4810 #define NACL_PLT_ENTRY_SIZE             64
4811 #define NACLMASK                        0xe0 /* 32-byte alignment mask.  */
4812
4813 static const bfd_byte elf_i386_nacl_plt0_entry[] =
4814   {
4815     0xff, 0x35,                   /* pushl contents of address */
4816     0, 0, 0, 0,                   /* replaced with address of .got + 4.  */
4817     0x8b, 0x0d,                   /* movl contents of address, %ecx */
4818     0, 0, 0, 0,                   /* replaced with address of .got + 8.  */
4819     0x83, 0xe1, NACLMASK,         /* andl $NACLMASK, %ecx */
4820     0xff, 0xe1                    /* jmp *%ecx */
4821   };
4822
4823 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
4824   {
4825     0x8b, 0x0d,                         /* movl contents of address, %ecx */
4826     0, 0, 0, 0,                         /* replaced with GOT slot address.  */
4827     0x83, 0xe1, NACLMASK,               /* andl $NACLMASK, %ecx */
4828     0xff, 0xe1,                         /* jmp *%ecx */
4829
4830     /* Pad to the next 32-byte boundary with nop instructions.  */
4831     0x90,
4832     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4833     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4834
4835     /* Lazy GOT entries point here (32-byte aligned).  */
4836     0x68,                              /* pushl immediate */
4837     0, 0, 0, 0,                        /* replaced with reloc offset.  */
4838     0xe9,                              /* jmp relative */
4839     0, 0, 0, 0,                        /* replaced with offset to .plt.  */
4840
4841     /* Pad to the next 32-byte boundary with nop instructions.  */
4842     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4843     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4844     0x90, 0x90
4845   };
4846
4847 static const bfd_byte
4848 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
4849   {
4850     0xff, 0x73, 0x04,           /* pushl 4(%ebx) */
4851     0x8b, 0x4b, 0x08,           /* mov 0x8(%ebx), %ecx */
4852     0x83, 0xe1, 0xe0,           /* and $NACLMASK, %ecx */
4853     0xff, 0xe1,                 /* jmp *%ecx */
4854
4855     /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
4856        so pad to that size with nop instructions.  */
4857     0x90, 0x90, 0x90, 0x90, 0x90, 0x90
4858   };
4859
4860 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
4861   {
4862     0x8b, 0x8b,          /* movl offset(%ebx), %ecx */
4863     0, 0, 0, 0,          /* replaced with offset of this symbol in .got.  */
4864     0x83, 0xe1, 0xe0,    /* andl $NACLMASK, %ecx */
4865     0xff, 0xe1,          /* jmp *%ecx */
4866
4867     /* Pad to the next 32-byte boundary with nop instructions.  */
4868     0x90,
4869     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4870     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4871
4872     /* Lazy GOT entries point here (32-byte aligned).  */
4873     0x68,                /* pushl immediate */
4874     0, 0, 0, 0,          /* replaced with offset into relocation table.  */
4875     0xe9,                /* jmp relative */
4876     0, 0, 0, 0,          /* replaced with offset to start of .plt.  */
4877
4878     /* Pad to the next 32-byte boundary with nop instructions.  */
4879     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4880     0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4881     0x90, 0x90
4882   };
4883
4884 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
4885   {
4886 #if (PLT_CIE_LENGTH != 20                               \
4887      || PLT_FDE_LENGTH != 36                            \
4888      || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8  \
4889      || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
4890 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
4891 #endif
4892     PLT_CIE_LENGTH, 0, 0, 0,            /* CIE length */
4893     0, 0, 0, 0,                         /* CIE ID */
4894     1,                                  /* CIE version */
4895     'z', 'R', 0,                        /* Augmentation string */
4896     1,                                  /* Code alignment factor */
4897     0x7c,                               /* Data alignment factor: -4 */
4898     8,                                  /* Return address column */
4899     1,                                  /* Augmentation size */
4900     DW_EH_PE_pcrel | DW_EH_PE_sdata4,   /* FDE encoding */
4901     DW_CFA_def_cfa, 4, 4,               /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
4902     DW_CFA_offset + 8, 1,               /* DW_CFA_offset: r8 (eip) at cfa-4 */
4903     DW_CFA_nop, DW_CFA_nop,
4904
4905     PLT_FDE_LENGTH, 0, 0, 0,     /* FDE length */
4906     PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
4907     0, 0, 0, 0,                  /* R_386_PC32 .plt goes here */
4908     0, 0, 0, 0,                  /* .plt size goes here */
4909     0,                           /* Augmentation size */
4910     DW_CFA_def_cfa_offset, 8,    /* DW_CFA_def_cfa_offset: 8 */
4911     DW_CFA_advance_loc + 6,      /* DW_CFA_advance_loc: 6 to __PLT__+6 */
4912     DW_CFA_def_cfa_offset, 12,   /* DW_CFA_def_cfa_offset: 12 */
4913     DW_CFA_advance_loc + 58,     /* DW_CFA_advance_loc: 58 to __PLT__+64 */
4914     DW_CFA_def_cfa_expression,   /* DW_CFA_def_cfa_expression */
4915     13,                          /* Block length */
4916     DW_OP_breg4, 4,              /* DW_OP_breg4 (esp): 4 */
4917     DW_OP_breg8, 0,              /* DW_OP_breg8 (eip): 0 */
4918     DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
4919     DW_OP_lit2, DW_OP_shl, DW_OP_plus,
4920     DW_CFA_nop, DW_CFA_nop
4921   };
4922
4923 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
4924   {
4925     elf_i386_nacl_plt0_entry,           /* plt0_entry */
4926     sizeof (elf_i386_nacl_plt0_entry),  /* plt0_entry_size */
4927     elf_i386_nacl_plt_entry,            /* plt_entry */
4928     NACL_PLT_ENTRY_SIZE,                /* plt_entry_size */
4929     2,                                  /* plt0_got1_offset */
4930     8,                                  /* plt0_got2_offset */
4931     0,                                  /* plt0_got2_insn_end */
4932     2,                                  /* plt_got_offset */
4933     33,                                 /* plt_reloc_offset */
4934     38,                                 /* plt_plt_offset */
4935     0,                                  /* plt_got_insn_size */
4936     0,                                  /* plt_plt_insn_end */
4937     32,                                 /* plt_lazy_offset */
4938     elf_i386_nacl_pic_plt0_entry,       /* pic_plt0_entry */
4939     elf_i386_nacl_pic_plt_entry,        /* pic_plt_entry */
4940     elf_i386_nacl_eh_frame_plt,         /* eh_frame_plt */
4941     sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
4942   };
4943
4944 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
4945   {
4946     0x90,                               /* plt0_pad_byte: nop insn */
4947     is_nacl                             /* os */
4948   };
4949
4950 static bfd_boolean
4951 elf32_i386_nacl_elf_object_p (bfd *abfd)
4952 {
4953   /* Set the right machine number for a NaCl i386 ELF32 file.  */
4954   bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
4955   return TRUE;
4956 }
4957
4958 #undef  elf_backend_arch_data
4959 #define elf_backend_arch_data   &elf_i386_nacl_arch_bed
4960
4961 #undef  elf_backend_object_p
4962 #define elf_backend_object_p                    elf32_i386_nacl_elf_object_p
4963 #undef  elf_backend_modify_segment_map
4964 #define elf_backend_modify_segment_map          nacl_modify_segment_map
4965 #undef  elf_backend_modify_program_headers
4966 #define elf_backend_modify_program_headers      nacl_modify_program_headers
4967 #undef  elf_backend_final_write_processing
4968 #define elf_backend_final_write_processing      nacl_final_write_processing
4969
4970 #include "elf32-target.h"
4971
4972 /* Restore defaults.  */
4973 #undef  elf_backend_object_p
4974 #undef  elf_backend_modify_segment_map
4975 #undef  elf_backend_modify_program_headers
4976 #undef  elf_backend_final_write_processing
4977
4978 /* VxWorks support.  */
4979
4980 #undef  TARGET_LITTLE_SYM
4981 #define TARGET_LITTLE_SYM               i386_elf32_vxworks_vec
4982 #undef  TARGET_LITTLE_NAME
4983 #define TARGET_LITTLE_NAME              "elf32-i386-vxworks"
4984 #undef  ELF_OSABI
4985 #undef  ELF_MAXPAGESIZE
4986 #define ELF_MAXPAGESIZE                 0x1000
4987 #undef  elf_backend_plt_alignment
4988 #define elf_backend_plt_alignment       4
4989
4990 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
4991   {
4992     0x90,                               /* plt0_pad_byte */
4993     is_vxworks                          /* os */
4994   };
4995
4996 #undef  elf_backend_arch_data
4997 #define elf_backend_arch_data   &elf_i386_vxworks_arch_bed
4998
4999 #undef elf_backend_relocs_compatible
5000 #undef elf_backend_add_symbol_hook
5001 #define elf_backend_add_symbol_hook \
5002   elf_vxworks_add_symbol_hook
5003 #undef elf_backend_link_output_symbol_hook
5004 #define elf_backend_link_output_symbol_hook \
5005   elf_vxworks_link_output_symbol_hook
5006 #undef elf_backend_emit_relocs
5007 #define elf_backend_emit_relocs                 elf_vxworks_emit_relocs
5008 #undef elf_backend_final_write_processing
5009 #define elf_backend_final_write_processing \
5010   elf_vxworks_final_write_processing
5011 #undef elf_backend_static_tls_alignment
5012
5013 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5014    define it.  */
5015 #undef elf_backend_want_plt_sym
5016 #define elf_backend_want_plt_sym        1
5017
5018 #undef  elf32_bed
5019 #define elf32_bed                               elf32_i386_vxworks_bed
5020
5021 #include "elf32-target.h"