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