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