Automatic date update in version.in
[binutils-gdb.git] / bfd / elf32-i386.c
blob7d573e7e2c7e29e3a0fd847722fe22cfcba0af23
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright (C) 1993-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
21 #include "elfxx-x86.h"
22 #include "elf-vxworks.h"
23 #include "dwarf2.h"
24 #include "opcode/i386.h"
26 /* 386 uses REL relocations instead of RELA. */
27 #define USE_REL 1
29 static reloc_howto_type elf_howto_table[]=
31 HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
32 bfd_elf_generic_reloc, "R_386_NONE",
33 true, 0x00000000, 0x00000000, false),
34 HOWTO(R_386_32, 0, 4, 32, false, 0, complain_overflow_dont,
35 bfd_elf_generic_reloc, "R_386_32",
36 true, 0xffffffff, 0xffffffff, false),
37 HOWTO(R_386_PC32, 0, 4, 32, true, 0, complain_overflow_dont,
38 bfd_elf_generic_reloc, "R_386_PC32",
39 true, 0xffffffff, 0xffffffff, true),
40 HOWTO(R_386_GOT32, 0, 4, 32, false, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_386_GOT32",
42 true, 0xffffffff, 0xffffffff, false),
43 HOWTO(R_386_PLT32, 0, 4, 32, true, 0, complain_overflow_dont,
44 bfd_elf_generic_reloc, "R_386_PLT32",
45 true, 0xffffffff, 0xffffffff, true),
46 HOWTO(R_386_COPY, 0, 4, 32, false, 0, complain_overflow_dont,
47 bfd_elf_generic_reloc, "R_386_COPY",
48 true, 0xffffffff, 0xffffffff, false),
49 HOWTO(R_386_GLOB_DAT, 0, 4, 32, false, 0, complain_overflow_dont,
50 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
51 true, 0xffffffff, 0xffffffff, false),
52 HOWTO(R_386_JUMP_SLOT, 0, 4, 32, false, 0, complain_overflow_dont,
53 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
54 true, 0xffffffff, 0xffffffff, false),
55 HOWTO(R_386_RELATIVE, 0, 4, 32, false, 0, complain_overflow_dont,
56 bfd_elf_generic_reloc, "R_386_RELATIVE",
57 true, 0xffffffff, 0xffffffff, false),
58 HOWTO(R_386_GOTOFF, 0, 4, 32, false, 0, complain_overflow_dont,
59 bfd_elf_generic_reloc, "R_386_GOTOFF",
60 true, 0xffffffff, 0xffffffff, false),
61 HOWTO(R_386_GOTPC, 0, 4, 32, true, 0, complain_overflow_dont,
62 bfd_elf_generic_reloc, "R_386_GOTPC",
63 true, 0xffffffff, 0xffffffff, true),
65 /* We have a gap in the reloc numbers here.
66 R_386_standard counts the number up to this point, and
67 R_386_ext_offset is the value to subtract from a reloc type of
68 R_386_16 thru R_386_PC8 to form an index into this table. */
69 #define R_386_standard (R_386_GOTPC + 1)
70 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
72 /* These relocs are a GNU extension. */
73 HOWTO(R_386_TLS_TPOFF, 0, 4, 32, false, 0, complain_overflow_dont,
74 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
75 true, 0xffffffff, 0xffffffff, false),
76 HOWTO(R_386_TLS_IE, 0, 4, 32, false, 0, complain_overflow_dont,
77 bfd_elf_generic_reloc, "R_386_TLS_IE",
78 true, 0xffffffff, 0xffffffff, false),
79 HOWTO(R_386_TLS_GOTIE, 0, 4, 32, false, 0, complain_overflow_dont,
80 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
81 true, 0xffffffff, 0xffffffff, false),
82 HOWTO(R_386_TLS_LE, 0, 4, 32, false, 0, complain_overflow_dont,
83 bfd_elf_generic_reloc, "R_386_TLS_LE",
84 true, 0xffffffff, 0xffffffff, false),
85 HOWTO(R_386_TLS_GD, 0, 4, 32, false, 0, complain_overflow_dont,
86 bfd_elf_generic_reloc, "R_386_TLS_GD",
87 true, 0xffffffff, 0xffffffff, false),
88 HOWTO(R_386_TLS_LDM, 0, 4, 32, false, 0, complain_overflow_dont,
89 bfd_elf_generic_reloc, "R_386_TLS_LDM",
90 true, 0xffffffff, 0xffffffff, false),
91 HOWTO(R_386_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
92 bfd_elf_generic_reloc, "R_386_16",
93 true, 0xffff, 0xffff, false),
94 HOWTO(R_386_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_386_PC16",
96 true, 0xffff, 0xffff, true),
97 HOWTO(R_386_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_386_8",
99 true, 0xff, 0xff, false),
100 HOWTO(R_386_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_386_PC8",
102 true, 0xff, 0xff, true),
104 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
105 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
106 /* These are common with Solaris TLS implementation. */
107 HOWTO(R_386_TLS_LDO_32, 0, 4, 32, false, 0, complain_overflow_dont,
108 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
109 true, 0xffffffff, 0xffffffff, false),
110 HOWTO(R_386_TLS_IE_32, 0, 4, 32, false, 0, complain_overflow_dont,
111 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
112 true, 0xffffffff, 0xffffffff, false),
113 HOWTO(R_386_TLS_LE_32, 0, 4, 32, false, 0, complain_overflow_dont,
114 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
115 true, 0xffffffff, 0xffffffff, false),
116 HOWTO(R_386_TLS_DTPMOD32, 0, 4, 32, false, 0, complain_overflow_dont,
117 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
118 true, 0xffffffff, 0xffffffff, false),
119 HOWTO(R_386_TLS_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_dont,
120 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
121 true, 0xffffffff, 0xffffffff, false),
122 HOWTO(R_386_TLS_TPOFF32, 0, 4, 32, false, 0, complain_overflow_dont,
123 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
124 true, 0xffffffff, 0xffffffff, false),
125 HOWTO(R_386_SIZE32, 0, 4, 32, false, 0, complain_overflow_dont,
126 bfd_elf_generic_reloc, "R_386_SIZE32",
127 true, 0xffffffff, 0xffffffff, false),
128 HOWTO(R_386_TLS_GOTDESC, 0, 4, 32, false, 0, complain_overflow_dont,
129 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
130 true, 0xffffffff, 0xffffffff, false),
131 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, false, 0, complain_overflow_dont,
132 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
133 false, 0, 0, false),
134 HOWTO(R_386_TLS_DESC, 0, 4, 32, false, 0, complain_overflow_dont,
135 bfd_elf_generic_reloc, "R_386_TLS_DESC",
136 true, 0xffffffff, 0xffffffff, false),
137 HOWTO(R_386_IRELATIVE, 0, 4, 32, false, 0, complain_overflow_dont,
138 bfd_elf_generic_reloc, "R_386_IRELATIVE",
139 true, 0xffffffff, 0xffffffff, false),
140 HOWTO(R_386_GOT32X, 0, 4, 32, false, 0, complain_overflow_dont,
141 bfd_elf_generic_reloc, "R_386_GOT32X",
142 true, 0xffffffff, 0xffffffff, false),
144 /* Another gap. */
145 #define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
146 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
148 /* GNU extension to record C++ vtable hierarchy. */
149 HOWTO (R_386_GNU_VTINHERIT, /* type */
150 0, /* rightshift */
151 4, /* size */
152 0, /* bitsize */
153 false, /* pc_relative */
154 0, /* bitpos */
155 complain_overflow_dont, /* complain_on_overflow */
156 NULL, /* special_function */
157 "R_386_GNU_VTINHERIT", /* name */
158 false, /* partial_inplace */
159 0, /* src_mask */
160 0, /* dst_mask */
161 false), /* pcrel_offset */
163 /* GNU extension to record C++ vtable member usage. */
164 HOWTO (R_386_GNU_VTENTRY, /* type */
165 0, /* rightshift */
166 4, /* size */
167 0, /* bitsize */
168 false, /* pc_relative */
169 0, /* bitpos */
170 complain_overflow_dont, /* complain_on_overflow */
171 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
172 "R_386_GNU_VTENTRY", /* name */
173 false, /* partial_inplace */
174 0, /* src_mask */
175 0, /* dst_mask */
176 false) /* pcrel_offset */
178 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
182 #ifdef DEBUG_GEN_RELOC
183 #define TRACE(str) \
184 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
185 #else
186 #define TRACE(str)
187 #endif
189 static reloc_howto_type *
190 elf_i386_reloc_type_lookup (bfd *abfd,
191 bfd_reloc_code_real_type code)
193 switch (code)
195 case BFD_RELOC_NONE:
196 TRACE ("BFD_RELOC_NONE");
197 return &elf_howto_table[R_386_NONE];
199 case BFD_RELOC_32:
200 TRACE ("BFD_RELOC_32");
201 return &elf_howto_table[R_386_32];
203 case BFD_RELOC_CTOR:
204 TRACE ("BFD_RELOC_CTOR");
205 return &elf_howto_table[R_386_32];
207 case BFD_RELOC_32_PCREL:
208 TRACE ("BFD_RELOC_PC32");
209 return &elf_howto_table[R_386_PC32];
211 case BFD_RELOC_386_GOT32:
212 TRACE ("BFD_RELOC_386_GOT32");
213 return &elf_howto_table[R_386_GOT32];
215 case BFD_RELOC_386_PLT32:
216 TRACE ("BFD_RELOC_386_PLT32");
217 return &elf_howto_table[R_386_PLT32];
219 case BFD_RELOC_386_COPY:
220 TRACE ("BFD_RELOC_386_COPY");
221 return &elf_howto_table[R_386_COPY];
223 case BFD_RELOC_386_GLOB_DAT:
224 TRACE ("BFD_RELOC_386_GLOB_DAT");
225 return &elf_howto_table[R_386_GLOB_DAT];
227 case BFD_RELOC_386_JUMP_SLOT:
228 TRACE ("BFD_RELOC_386_JUMP_SLOT");
229 return &elf_howto_table[R_386_JUMP_SLOT];
231 case BFD_RELOC_386_RELATIVE:
232 TRACE ("BFD_RELOC_386_RELATIVE");
233 return &elf_howto_table[R_386_RELATIVE];
235 case BFD_RELOC_386_GOTOFF:
236 TRACE ("BFD_RELOC_386_GOTOFF");
237 return &elf_howto_table[R_386_GOTOFF];
239 case BFD_RELOC_386_GOTPC:
240 TRACE ("BFD_RELOC_386_GOTPC");
241 return &elf_howto_table[R_386_GOTPC];
243 /* These relocs are a GNU extension. */
244 case BFD_RELOC_386_TLS_TPOFF:
245 TRACE ("BFD_RELOC_386_TLS_TPOFF");
246 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
248 case BFD_RELOC_386_TLS_IE:
249 TRACE ("BFD_RELOC_386_TLS_IE");
250 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
252 case BFD_RELOC_386_TLS_GOTIE:
253 TRACE ("BFD_RELOC_386_TLS_GOTIE");
254 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
256 case BFD_RELOC_386_TLS_LE:
257 TRACE ("BFD_RELOC_386_TLS_LE");
258 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
260 case BFD_RELOC_386_TLS_GD:
261 TRACE ("BFD_RELOC_386_TLS_GD");
262 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
264 case BFD_RELOC_386_TLS_LDM:
265 TRACE ("BFD_RELOC_386_TLS_LDM");
266 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
268 case BFD_RELOC_16:
269 TRACE ("BFD_RELOC_16");
270 return &elf_howto_table[R_386_16 - R_386_ext_offset];
272 case BFD_RELOC_16_PCREL:
273 TRACE ("BFD_RELOC_16_PCREL");
274 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
276 case BFD_RELOC_8:
277 TRACE ("BFD_RELOC_8");
278 return &elf_howto_table[R_386_8 - R_386_ext_offset];
280 case BFD_RELOC_8_PCREL:
281 TRACE ("BFD_RELOC_8_PCREL");
282 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
284 /* Common with Sun TLS implementation. */
285 case BFD_RELOC_386_TLS_LDO_32:
286 TRACE ("BFD_RELOC_386_TLS_LDO_32");
287 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
289 case BFD_RELOC_386_TLS_IE_32:
290 TRACE ("BFD_RELOC_386_TLS_IE_32");
291 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
293 case BFD_RELOC_386_TLS_LE_32:
294 TRACE ("BFD_RELOC_386_TLS_LE_32");
295 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
297 case BFD_RELOC_386_TLS_DTPMOD32:
298 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
299 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
301 case BFD_RELOC_386_TLS_DTPOFF32:
302 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
303 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
305 case BFD_RELOC_386_TLS_TPOFF32:
306 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
307 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
309 case BFD_RELOC_SIZE32:
310 TRACE ("BFD_RELOC_SIZE32");
311 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
313 case BFD_RELOC_386_TLS_GOTDESC:
314 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
315 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
317 case BFD_RELOC_386_TLS_DESC_CALL:
318 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
319 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
321 case BFD_RELOC_386_TLS_DESC:
322 TRACE ("BFD_RELOC_386_TLS_DESC");
323 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
325 case BFD_RELOC_386_IRELATIVE:
326 TRACE ("BFD_RELOC_386_IRELATIVE");
327 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
329 case BFD_RELOC_386_GOT32X:
330 TRACE ("BFD_RELOC_386_GOT32X");
331 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
333 case BFD_RELOC_VTABLE_INHERIT:
334 TRACE ("BFD_RELOC_VTABLE_INHERIT");
335 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
337 case BFD_RELOC_VTABLE_ENTRY:
338 TRACE ("BFD_RELOC_VTABLE_ENTRY");
339 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
341 default:
342 TRACE ("Unknown");
343 /* xgettext:c-format */
344 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
345 abfd, (int) code);
346 bfd_set_error (bfd_error_bad_value);
347 return NULL;
351 static reloc_howto_type *
352 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
353 const char *r_name)
355 unsigned int i;
357 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
358 if (elf_howto_table[i].name != NULL
359 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
360 return &elf_howto_table[i];
362 return NULL;
365 static reloc_howto_type *
366 elf_i386_rtype_to_howto (unsigned r_type)
368 unsigned int indx;
370 if ((indx = r_type) >= R_386_standard
371 && ((indx = r_type - R_386_ext_offset) - R_386_standard
372 >= R_386_ext - R_386_standard)
373 && ((indx = r_type - R_386_tls_offset) - R_386_ext
374 >= R_386_ext2 - R_386_ext)
375 && ((indx = r_type - R_386_vt_offset) - R_386_ext2
376 >= R_386_vt - R_386_ext2))
377 return NULL;
378 /* PR 17512: file: 0f67f69d. */
379 if (elf_howto_table [indx].type != r_type)
380 return NULL;
381 return &elf_howto_table[indx];
384 static bool
385 elf_i386_info_to_howto_rel (bfd *abfd,
386 arelent *cache_ptr,
387 Elf_Internal_Rela *dst)
389 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
391 if ((cache_ptr->howto = elf_i386_rtype_to_howto (r_type)) == NULL)
393 /* xgettext:c-format */
394 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
395 abfd, r_type);
396 bfd_set_error (bfd_error_bad_value);
397 return false;
400 return true;
403 /* Return whether a symbol name implies a local label. The UnixWare
404 2.1 cc generates temporary symbols that start with .X, so we
405 recognize them here. FIXME: do other SVR4 compilers also use .X?.
406 If so, we should move the .X recognition into
407 _bfd_elf_is_local_label_name. */
409 static bool
410 elf_i386_is_local_label_name (bfd *abfd, const char *name)
412 if (name[0] == '.' && name[1] == 'X')
413 return true;
415 return _bfd_elf_is_local_label_name (abfd, name);
418 /* Support for core dump NOTE sections. */
420 static bool
421 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
423 int offset;
424 size_t size;
426 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
428 int pr_version = bfd_get_32 (abfd, note->descdata);
430 if (pr_version != 1)
431 return false;
433 /* pr_cursig */
434 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
436 /* pr_pid */
437 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
439 /* pr_reg */
440 offset = 28;
441 size = bfd_get_32 (abfd, note->descdata + 8);
443 else
445 switch (note->descsz)
447 default:
448 return false;
450 case 144: /* Linux/i386 */
451 /* pr_cursig */
452 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
454 /* pr_pid */
455 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
457 /* pr_reg */
458 offset = 72;
459 size = 68;
461 break;
465 /* Make a ".reg/999" section. */
466 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
467 size, note->descpos + offset);
470 static bool
471 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
473 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
475 int pr_version = bfd_get_32 (abfd, note->descdata);
477 if (pr_version != 1)
478 return false;
480 elf_tdata (abfd)->core->program
481 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
482 elf_tdata (abfd)->core->command
483 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
485 else
487 switch (note->descsz)
489 default:
490 return false;
492 case 124: /* Linux/i386 elf_prpsinfo. */
493 elf_tdata (abfd)->core->pid
494 = bfd_get_32 (abfd, note->descdata + 12);
495 elf_tdata (abfd)->core->program
496 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
497 elf_tdata (abfd)->core->command
498 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
502 /* Note that for some reason, a spurious space is tacked
503 onto the end of the args in some (at least one anyway)
504 implementations, so strip it off if it exists. */
506 char *command = elf_tdata (abfd)->core->command;
507 int n = strlen (command);
509 if (0 < n && command[n - 1] == ' ')
510 command[n - 1] = '\0';
513 return true;
516 /* Functions for the i386 ELF linker.
518 In order to gain some understanding of code in this file without
519 knowing all the intricate details of the linker, note the
520 following:
522 Functions named elf_i386_* are called by external routines, other
523 functions are only called locally. elf_i386_* functions appear
524 in this file more or less in the order in which they are called
525 from external routines. eg. elf_i386_scan_relocs is called
526 early in the link process, elf_i386_finish_dynamic_sections is
527 one of the last functions. */
529 /* The size in bytes of an entry in the lazy procedure linkage table. */
531 #define LAZY_PLT_ENTRY_SIZE 16
533 /* The size in bytes of an entry in the non-lazy procedure linkage
534 table. */
536 #define NON_LAZY_PLT_ENTRY_SIZE 8
538 /* The first entry in an absolute lazy procedure linkage table looks
539 like this. See the SVR4 ABI i386 supplement to see how this works.
540 Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte. */
542 static const bfd_byte elf_i386_lazy_plt0_entry[12] =
544 0xff, 0x35, /* pushl contents of address */
545 0, 0, 0, 0, /* replaced with address of .got + 4. */
546 0xff, 0x25, /* jmp indirect */
547 0, 0, 0, 0 /* replaced with address of .got + 8. */
550 /* Subsequent entries in an absolute lazy procedure linkage table look
551 like this. */
553 static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
555 0xff, 0x25, /* jmp indirect */
556 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
557 0x68, /* pushl immediate */
558 0, 0, 0, 0, /* replaced with offset into relocation table. */
559 0xe9, /* jmp relative */
560 0, 0, 0, 0 /* replaced with offset to start of .plt. */
563 /* The first entry in a PIC lazy procedure linkage table look like
564 this. Will be padded to LAZY_PLT_ENTRY_SIZE with
565 lazy_plt->plt0_pad_byte. */
567 static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
569 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
570 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
573 /* Subsequent entries in a PIC lazy procedure linkage table look like
574 this. */
576 static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
578 0xff, 0xa3, /* jmp *offset(%ebx) */
579 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
580 0x68, /* pushl immediate */
581 0, 0, 0, 0, /* replaced with offset into relocation table. */
582 0xe9, /* jmp relative */
583 0, 0, 0, 0 /* replaced with offset to start of .plt. */
586 /* Entries in the non-lazy procedure linkage table look like this. */
588 static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
590 0xff, 0x25, /* jmp indirect */
591 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
592 0x66, 0x90 /* xchg %ax,%ax */
595 /* Entries in the PIC non-lazy procedure linkage table look like
596 this. */
598 static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
600 0xff, 0xa3, /* jmp *offset(%ebx) */
601 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
602 0x66, 0x90 /* xchg %ax,%ax */
605 /* The first entry in an absolute IBT-enabled lazy procedure linkage
606 table looks like this. */
608 static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
610 0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1] */
611 0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2] */
612 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
615 /* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
616 table look like this. Subsequent entries for a PIC IBT-enabled lazy
617 procedure linkage table are the same. */
619 static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
621 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
622 0x68, 0, 0, 0, 0, /* pushl immediate */
623 0xe9, 0, 0, 0, 0, /* jmp relative */
624 0x66, 0x90 /* xchg %ax,%ax */
627 /* The first entry in a PIC IBT-enabled lazy procedure linkage table
628 look like. */
630 static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
632 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
633 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
634 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
637 /* Entries for branches with IBT-enabled in the absolute non-lazey
638 procedure linkage table look like this. They have the same size
639 as the lazy PLT entry. */
641 static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
643 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
644 0xff, 0x25, 0, 0, 0, 0, /* jmp *name@GOT */
645 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
648 /* Entries for branches with IBT-enabled in the PIC non-lazey procedure
649 linkage table look like this. They have the same size as the lazy
650 PLT entry. */
652 static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
654 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
655 0xff, 0xa3, 0, 0, 0, 0, /* jmp *name@GOT(%ebx) */
656 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
659 /* .eh_frame covering the lazy .plt section. */
661 static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
663 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
664 0, 0, 0, 0, /* CIE ID */
665 1, /* CIE version */
666 'z', 'R', 0, /* Augmentation string */
667 1, /* Code alignment factor */
668 0x7c, /* Data alignment factor */
669 8, /* Return address column */
670 1, /* Augmentation size */
671 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
672 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
673 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
674 DW_CFA_nop, DW_CFA_nop,
676 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
677 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
678 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
679 0, 0, 0, 0, /* .plt size goes here */
680 0, /* Augmentation size */
681 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
682 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
683 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
684 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
685 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
686 11, /* Block length */
687 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
688 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
689 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
690 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
691 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
694 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
696 static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
698 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
699 0, 0, 0, 0, /* CIE ID */
700 1, /* CIE version */
701 'z', 'R', 0, /* Augmentation string */
702 1, /* Code alignment factor */
703 0x7c, /* Data alignment factor */
704 8, /* Return address column */
705 1, /* Augmentation size */
706 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
707 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
708 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
709 DW_CFA_nop, DW_CFA_nop,
711 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
712 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
713 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
714 0, 0, 0, 0, /* .plt size goes here */
715 0, /* Augmentation size */
716 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
717 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
718 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
719 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
720 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
721 11, /* Block length */
722 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
723 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
724 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
725 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
726 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
729 /* .eh_frame covering the non-lazy .plt section. */
731 static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
733 #define PLT_GOT_FDE_LENGTH 16
734 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
735 0, 0, 0, 0, /* CIE ID */
736 1, /* CIE version */
737 'z', 'R', 0, /* Augmentation string */
738 1, /* Code alignment factor */
739 0x7c, /* Data alignment factor */
740 8, /* Return address column */
741 1, /* Augmentation size */
742 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
743 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
744 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
745 DW_CFA_nop, DW_CFA_nop,
747 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
748 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
749 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
750 0, 0, 0, 0, /* non-lazy .plt size goes here */
751 0, /* Augmentation size */
752 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
755 /* These are the standard parameters. */
756 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
758 elf_i386_lazy_plt0_entry, /* plt0_entry */
759 sizeof (elf_i386_lazy_plt0_entry), /* plt0_entry_size */
760 elf_i386_lazy_plt_entry, /* plt_entry */
761 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
762 NULL, /* plt_tlsdesc_entry */
763 0, /* plt_tlsdesc_entry_size*/
764 0, /* plt_tlsdesc_got1_offset */
765 0, /* plt_tlsdesc_got2_offset */
766 0, /* plt_tlsdesc_got1_insn_end */
767 0, /* plt_tlsdesc_got2_insn_end */
768 2, /* plt0_got1_offset */
769 8, /* plt0_got2_offset */
770 0, /* plt0_got2_insn_end */
771 2, /* plt_got_offset */
772 7, /* plt_reloc_offset */
773 12, /* plt_plt_offset */
774 0, /* plt_got_insn_size */
775 0, /* plt_plt_insn_end */
776 6, /* plt_lazy_offset */
777 elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */
778 elf_i386_pic_lazy_plt_entry, /* pic_plt_entry */
779 elf_i386_eh_frame_lazy_plt, /* eh_frame_plt */
780 sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
783 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
785 elf_i386_non_lazy_plt_entry, /* plt_entry */
786 elf_i386_pic_non_lazy_plt_entry, /* pic_plt_entry */
787 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
788 2, /* plt_got_offset */
789 0, /* plt_got_insn_size */
790 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
791 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
794 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
796 elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */
797 sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
798 elf_i386_lazy_ibt_plt_entry, /* plt_entry */
799 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
800 NULL, /* plt_tlsdesc_entry */
801 0, /* plt_tlsdesc_entry_size*/
802 0, /* plt_tlsdesc_got1_offset */
803 0, /* plt_tlsdesc_got2_offset */
804 0, /* plt_tlsdesc_got1_insn_end */
805 0, /* plt_tlsdesc_got2_insn_end */
806 2, /* plt0_got1_offset */
807 8, /* plt0_got2_offset */
808 0, /* plt0_got2_insn_end */
809 4+2, /* plt_got_offset */
810 4+1, /* plt_reloc_offset */
811 4+6, /* plt_plt_offset */
812 0, /* plt_got_insn_size */
813 0, /* plt_plt_insn_end */
814 0, /* plt_lazy_offset */
815 elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */
816 elf_i386_lazy_ibt_plt_entry, /* pic_plt_entry */
817 elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
818 sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
821 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
823 elf_i386_non_lazy_ibt_plt_entry, /* plt_entry */
824 elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
825 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
826 4+2, /* plt_got_offset */
827 0, /* plt_got_insn_size */
828 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
829 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
833 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
834 for the PLTResolve stub and then for each PLT entry. */
835 #define PLTRESOLVE_RELOCS_SHLIB 0
836 #define PLTRESOLVE_RELOCS 2
837 #define PLT_NON_JUMP_SLOT_RELOCS 2
839 /* Return TRUE if the TLS access code sequence support transition
840 from R_TYPE. */
842 static enum elf_x86_tls_error_type
843 elf_i386_check_tls_transition (asection *sec,
844 bfd_byte *contents,
845 Elf_Internal_Shdr *symtab_hdr,
846 struct elf_link_hash_entry **sym_hashes,
847 unsigned int r_type,
848 const Elf_Internal_Rela *rel,
849 const Elf_Internal_Rela *relend)
851 unsigned int val, type, reg;
852 unsigned long r_symndx;
853 struct elf_link_hash_entry *h;
854 bfd_vma offset;
855 bfd_byte *call;
856 bool indirect_call;
858 offset = rel->r_offset;
859 switch (r_type)
861 case R_386_TLS_GD:
862 case R_386_TLS_LDM:
863 if (offset < 2 || (rel + 1) >= relend)
864 return elf_x86_tls_error_yes;
866 indirect_call = false;
867 call = contents + offset + 4;
868 val = *(call - 5);
869 type = *(call - 6);
870 if (r_type == R_386_TLS_GD)
872 /* Check transition from GD access model. Only
873 leal foo@tlsgd(,%ebx,1), %eax
874 call ___tls_get_addr@PLT
876 leal foo@tlsgd(%ebx) %eax
877 call ___tls_get_addr@PLT
880 leal foo@tlsgd(%reg), %eax
881 call *___tls_get_addr@GOT(%reg)
882 which may be converted to
883 addr32 call ___tls_get_addr
884 can transit to different access model. */
885 if ((offset + 10) > sec->size
886 || (type != 0x8d && type != 0x04))
887 return elf_x86_tls_error_yes;
889 if (type == 0x04)
891 /* leal foo@tlsgd(,%ebx,1), %eax
892 call ___tls_get_addr@PLT */
893 if (offset < 3)
894 return elf_x86_tls_error_yes;
896 if (*(call - 7) != 0x8d
897 || val != 0x1d
898 || call[0] != 0xe8)
899 return elf_x86_tls_error_yes;
901 else
903 /* This must be
904 leal foo@tlsgd(%ebx), %eax
905 call ___tls_get_addr@PLT
908 leal foo@tlsgd(%reg), %eax
909 call *___tls_get_addr@GOT(%reg)
910 which may be converted to
911 addr32 call ___tls_get_addr
913 %eax can't be used as the GOT base register since it
914 is used to pass parameter to ___tls_get_addr. */
915 reg = val & 7;
916 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
917 return elf_x86_tls_error_yes;
919 indirect_call = call[0] == 0xff;
920 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
921 && !(call[0] == 0x67 && call[1] == 0xe8)
922 && !(indirect_call
923 && (call[1] & 0xf8) == 0x90
924 && (call[1] & 0x7) == reg))
925 return elf_x86_tls_error_yes;
928 else
930 /* Check transition from LD access model. Only
931 leal foo@tlsldm(%ebx), %eax
932 call ___tls_get_addr@PLT
934 leal foo@tlsldm(%reg), %eax
935 call *___tls_get_addr@GOT(%reg)
936 which may be converted to
937 addr32 call ___tls_get_addr
938 can transit to different access model. */
939 if (type != 0x8d || (offset + 9) > sec->size)
940 return elf_x86_tls_error_yes;
942 /* %eax can't be used as the GOT base register since it is
943 used to pass parameter to ___tls_get_addr. */
944 reg = val & 7;
945 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
946 return elf_x86_tls_error_yes;
948 indirect_call = call[0] == 0xff;
949 if (!(reg == 3 && call[0] == 0xe8)
950 && !(call[0] == 0x67 && call[1] == 0xe8)
951 && !(indirect_call
952 && (call[1] & 0xf8) == 0x90
953 && (call[1] & 0x7) == reg))
954 return elf_x86_tls_error_yes;
957 r_symndx = ELF32_R_SYM (rel[1].r_info);
958 if (r_symndx < symtab_hdr->sh_info)
959 return elf_x86_tls_error_yes;
961 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
962 if (h == NULL
963 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
964 return elf_x86_tls_error_yes;
965 else if (indirect_call)
966 return ((ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X
967 || ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32)
968 ? elf_x86_tls_error_none
969 : elf_x86_tls_error_yes);
970 else
971 return ((ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
972 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32)
973 ? elf_x86_tls_error_none
974 : elf_x86_tls_error_yes);
976 case R_386_TLS_IE:
977 /* Check transition from IE access model:
978 movl foo@indntpoff(%rip), %eax
979 movl foo@indntpoff(%rip), %reg
980 addl foo@indntpoff(%rip), %reg
983 if (offset < 1 || (offset + 4) > sec->size)
984 return elf_x86_tls_error_yes;
986 /* Check "movl foo@tpoff(%rip), %eax" first. */
987 val = bfd_get_8 (abfd, contents + offset - 1);
988 if (val == 0xa1)
989 return elf_x86_tls_error_none;
991 if (offset < 2)
992 return elf_x86_tls_error_yes;
994 /* Check movl|addl foo@tpoff(%rip), %reg. */
995 type = bfd_get_8 (abfd, contents + offset - 2);
996 if (type != 0x8b && type != 0x03)
997 return elf_x86_tls_error_add_mov;
998 return ((val & 0xc7) == 0x05
999 ? elf_x86_tls_error_none
1000 : elf_x86_tls_error_yes);
1002 case R_386_TLS_GOTIE:
1003 case R_386_TLS_IE_32:
1004 /* Check transition from {IE_32,GOTIE} access model:
1005 subl foo@{tpoff,gontoff}(%reg1), %reg2
1006 movl foo@{tpoff,gontoff}(%reg1), %reg2
1007 addl foo@{tpoff,gontoff}(%reg1), %reg2
1010 if (offset < 2 || (offset + 4) > sec->size)
1011 return elf_x86_tls_error_yes;
1013 val = bfd_get_8 (abfd, contents + offset - 1);
1014 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1015 return elf_x86_tls_error_yes;
1017 type = bfd_get_8 (abfd, contents + offset - 2);
1018 return (type == 0x8b || type == 0x2b || type == 0x03
1019 ? elf_x86_tls_error_none
1020 : elf_x86_tls_error_add_sub_mov);
1022 case R_386_TLS_GOTDESC:
1023 /* Check transition from GDesc access model:
1024 leal x@tlsdesc(%ebx), %eax
1026 Make sure it's a leal adding ebx to a 32-bit offset
1027 into any register, although it's probably almost always
1028 going to be eax. */
1030 if (offset < 2 || (offset + 4) > sec->size)
1031 return elf_x86_tls_error_yes;
1033 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1034 return elf_x86_tls_error_lea;
1036 val = bfd_get_8 (abfd, contents + offset - 1);
1037 return ((val & 0xc7) == 0x83
1038 ? elf_x86_tls_error_none
1039 : elf_x86_tls_error_yes);
1041 case R_386_TLS_DESC_CALL:
1042 /* Check transition from GDesc access model:
1043 call *x@tlsdesc(%eax)
1045 if (offset + 2 <= sec->size)
1047 /* Make sure that it's a call *x@tlsdesc(%eax). */
1048 call = contents + offset;
1049 return (call[0] == 0xff && call[1] == 0x10
1050 ? elf_x86_tls_error_none
1051 : elf_x86_tls_error_indirect_call);
1054 return elf_x86_tls_error_yes;
1056 default:
1057 abort ();
1061 /* Return TRUE if the TLS access transition is OK or no transition
1062 will be performed. Update R_TYPE if there is a transition. */
1064 static bool
1065 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1066 asection *sec, bfd_byte *contents,
1067 Elf_Internal_Shdr *symtab_hdr,
1068 struct elf_link_hash_entry **sym_hashes,
1069 unsigned int *r_type, int tls_type,
1070 const Elf_Internal_Rela *rel,
1071 const Elf_Internal_Rela *relend,
1072 struct elf_link_hash_entry *h,
1073 Elf_Internal_Sym *sym,
1074 bool from_relocate_section)
1076 unsigned int from_type = *r_type;
1077 unsigned int to_type = from_type;
1078 bool check = true;
1079 unsigned int to_le_type, to_ie_type;
1081 /* Skip TLS transition for functions. */
1082 if (h != NULL
1083 && (h->type == STT_FUNC
1084 || h->type == STT_GNU_IFUNC))
1085 return true;
1087 if (get_elf_backend_data (abfd)->target_os == is_solaris)
1089 /* NB: Solaris only supports R_386_TLS_LE and R_386_TLS_IE. */
1090 to_le_type = R_386_TLS_LE;
1091 to_ie_type = R_386_TLS_IE;
1093 else
1095 to_le_type = R_386_TLS_LE_32;
1096 to_ie_type = R_386_TLS_IE_32;
1099 switch (from_type)
1101 case R_386_TLS_GD:
1102 case R_386_TLS_GOTDESC:
1103 case R_386_TLS_DESC_CALL:
1104 case R_386_TLS_IE_32:
1105 case R_386_TLS_IE:
1106 case R_386_TLS_GOTIE:
1107 if (bfd_link_executable (info))
1109 if (h == NULL)
1110 to_type = to_le_type;
1111 else if (from_type != R_386_TLS_IE
1112 && from_type != R_386_TLS_GOTIE)
1113 to_type = to_ie_type;
1116 /* When we are called from elf_i386_relocate_section, there may
1117 be additional transitions based on TLS_TYPE. */
1118 if (from_relocate_section)
1120 unsigned int new_to_type = to_type;
1122 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1123 new_to_type = to_le_type;
1125 if (to_type == R_386_TLS_GD
1126 || to_type == R_386_TLS_GOTDESC
1127 || to_type == R_386_TLS_DESC_CALL)
1129 if (tls_type == GOT_TLS_IE_POS)
1130 new_to_type = R_386_TLS_GOTIE;
1131 else if (tls_type & GOT_TLS_IE)
1132 new_to_type = to_ie_type;
1135 /* We checked the transition before when we were called from
1136 elf_i386_scan_relocs. We only want to check the new
1137 transition which hasn't been checked before. */
1138 check = new_to_type != to_type && from_type == to_type;
1139 to_type = new_to_type;
1142 break;
1144 case R_386_TLS_LDM:
1145 if (bfd_link_executable (info))
1146 to_type = to_le_type;
1147 break;
1149 default:
1150 return true;
1153 /* Return TRUE if there is no transition. */
1154 if (from_type == to_type)
1155 return true;
1157 /* Check if the transition can be performed. */
1158 enum elf_x86_tls_error_type tls_error;
1159 if (check
1160 && ((tls_error = elf_i386_check_tls_transition (sec, contents,
1161 symtab_hdr,
1162 sym_hashes,
1163 from_type, rel,
1164 relend))
1165 != elf_x86_tls_error_none))
1167 reloc_howto_type *from, *to;
1169 from = elf_i386_rtype_to_howto (from_type);
1170 to = elf_i386_rtype_to_howto (to_type);
1172 _bfd_x86_elf_link_report_tls_transition_error
1173 (info, abfd, sec, symtab_hdr, h, sym, rel, from->name,
1174 to->name, tls_error);
1176 return false;
1179 *r_type = to_type;
1180 return true;
1183 /* With the local symbol, foo, we convert
1184 mov foo@GOT[(%reg1)], %reg2
1186 lea foo[@GOTOFF(%reg1)], %reg2
1187 and convert
1188 call/jmp *foo@GOT[(%reg)]
1190 nop call foo/jmp foo nop
1191 When PIC is false, convert
1192 test %reg1, foo@GOT[(%reg2)]
1194 test $foo, %reg1
1195 and convert
1196 binop foo@GOT[(%reg1)], %reg2
1198 binop $foo, %reg2
1199 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1200 instructions. */
1202 static
1203 bool
1204 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1205 bfd_byte *contents,
1206 unsigned int *r_type_p,
1207 Elf_Internal_Rela *irel,
1208 struct elf_link_hash_entry *h,
1209 bool *converted,
1210 struct bfd_link_info *link_info)
1212 struct elf_x86_link_hash_table *htab;
1213 unsigned int opcode;
1214 unsigned int modrm;
1215 bool baseless;
1216 Elf_Internal_Sym *isym;
1217 unsigned int addend;
1218 unsigned int nop;
1219 bfd_vma nop_offset;
1220 bool is_pic;
1221 bool to_reloc_32;
1222 bool abs_symbol;
1223 unsigned int r_type;
1224 unsigned int r_symndx;
1225 bfd_vma roff = irel->r_offset;
1226 bool local_ref;
1227 struct elf_x86_link_hash_entry *eh;
1229 if (roff < 2)
1230 return true;
1232 /* Addend for R_386_GOT32X relocations must be 0. */
1233 addend = bfd_get_32 (abfd, contents + roff);
1234 if (addend != 0)
1235 return true;
1237 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1238 if (htab == NULL || ! is_x86_elf (abfd, htab))
1240 bfd_set_error (bfd_error_wrong_format);
1241 return false;
1244 is_pic = bfd_link_pic (link_info);
1246 r_type = *r_type_p;
1247 r_symndx = ELF32_R_SYM (irel->r_info);
1249 modrm = bfd_get_8 (abfd, contents + roff - 1);
1250 baseless = (modrm & 0xc7) == 0x5;
1252 if (h)
1254 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1255 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1256 isym = NULL;
1257 abs_symbol = ABS_SYMBOL_P (h);
1259 else
1261 local_ref = true;
1262 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
1263 r_symndx);
1264 abs_symbol = isym->st_shndx == SHN_ABS;
1267 if (baseless && is_pic)
1269 /* For PIC, disallow R_386_GOT32X without a base register
1270 since we don't know what the GOT base is. */
1271 const char *name;
1273 if (h == NULL)
1274 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1275 else
1276 name = h->root.root.string;
1278 _bfd_error_handler
1279 /* xgettext:c-format */
1280 (_("%pB: direct GOT relocation R_386_GOT32X against `%s' without base"
1281 " register can not be used when making a shared object"),
1282 abfd, name);
1283 return false;
1286 opcode = bfd_get_8 (abfd, contents + roff - 2);
1288 /* Convert to R_386_32 if PIC is false or there is no base
1289 register. */
1290 to_reloc_32 = !is_pic || baseless;
1292 eh = elf_x86_hash_entry (h);
1294 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1295 reloc. */
1296 if (h == NULL)
1298 if (opcode == 0x0ff)
1299 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1300 goto convert_branch;
1301 else
1302 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1303 "test %reg1, foo@GOT(%reg2)" and
1304 "binop foo@GOT[(%reg1)], %reg2". */
1305 goto convert_load;
1308 /* Undefined weak symbol is only bound locally in executable
1309 and its reference is resolved as 0. */
1310 if (h->root.type == bfd_link_hash_undefweak
1311 && !eh->linker_def
1312 && local_ref)
1314 if (opcode == 0xff)
1316 /* No direct branch to 0 for PIC. */
1317 if (is_pic)
1318 return true;
1319 else
1320 goto convert_branch;
1322 else
1324 /* We can convert load of address 0 to R_386_32. */
1325 to_reloc_32 = true;
1326 goto convert_load;
1330 if (opcode == 0xff)
1332 /* We have "call/jmp *foo@GOT[(%reg)]". */
1333 if ((h->root.type == bfd_link_hash_defined
1334 || h->root.type == bfd_link_hash_defweak)
1335 && local_ref)
1337 /* The function is locally defined. */
1338 convert_branch:
1339 /* Convert R_386_GOT32X to R_386_PC32. */
1340 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1342 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1343 is a nop prefix. */
1344 modrm = 0xe8;
1345 /* To support TLS optimization, always use addr32 prefix
1346 for "call *___tls_get_addr@GOT(%reg)". */
1347 if (eh && eh->tls_get_addr)
1349 nop = 0x67;
1350 nop_offset = irel->r_offset - 2;
1352 else
1354 nop = htab->params->call_nop_byte;
1355 if (htab->params->call_nop_as_suffix)
1357 nop_offset = roff + 3;
1358 irel->r_offset -= 1;
1360 else
1361 nop_offset = roff - 2;
1364 else
1366 /* Convert to "jmp foo nop". */
1367 modrm = 0xe9;
1368 nop = NOP_OPCODE;
1369 nop_offset = roff + 3;
1370 irel->r_offset -= 1;
1373 bfd_put_8 (abfd, nop, contents + nop_offset);
1374 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1375 /* When converting to PC-relative relocation, we
1376 need to adjust addend by -4. */
1377 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1378 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1379 *r_type_p = R_386_PC32;
1380 *converted = true;
1383 else
1385 /* We have "mov foo@GOT[(%re1g)], %reg2",
1386 "test %reg1, foo@GOT(%reg2)" and
1387 "binop foo@GOT[(%reg1)], %reg2".
1389 Avoid optimizing _DYNAMIC since ld.so may use its
1390 link-time address. */
1391 if (h == htab->elf.hdynamic)
1392 return true;
1394 /* def_regular is set by an assignment in a linker script in
1395 bfd_elf_record_link_assignment. start_stop is set on
1396 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1397 if (h->start_stop
1398 || eh->linker_def
1399 || ((h->def_regular
1400 || h->root.type == bfd_link_hash_defined
1401 || h->root.type == bfd_link_hash_defweak)
1402 && local_ref))
1404 convert_load:
1405 if (opcode == 0x8b)
1407 if (abs_symbol && local_ref)
1408 to_reloc_32 = true;
1410 if (to_reloc_32)
1412 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1413 "mov $foo, %reg2" with R_386_32. */
1414 r_type = R_386_32;
1415 modrm = 0xc0 | (modrm & 0x38) >> 3;
1416 bfd_put_8 (abfd, modrm, contents + roff - 1);
1417 opcode = 0xc7;
1419 else
1421 /* Convert "mov foo@GOT(%reg1), %reg2" to
1422 "lea foo@GOTOFF(%reg1), %reg2". */
1423 r_type = R_386_GOTOFF;
1424 opcode = 0x8d;
1427 else
1429 /* Only R_386_32 is supported. */
1430 if (!to_reloc_32)
1431 return true;
1433 if (opcode == 0x85)
1435 /* Convert "test %reg1, foo@GOT(%reg2)" to
1436 "test $foo, %reg1". */
1437 modrm = 0xc0 | (modrm & 0x38) >> 3;
1438 opcode = 0xf7;
1440 else
1442 /* Convert "binop foo@GOT(%reg1), %reg2" to
1443 "binop $foo, %reg2". */
1444 modrm = (0xc0
1445 | (modrm & 0x38) >> 3
1446 | (opcode & 0x3c));
1447 opcode = 0x81;
1449 bfd_put_8 (abfd, modrm, contents + roff - 1);
1450 r_type = R_386_32;
1453 bfd_put_8 (abfd, opcode, contents + roff - 2);
1454 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1455 *r_type_p = r_type;
1456 *converted = true;
1460 return true;
1463 /* Look through the relocs for a section during the first phase, and
1464 calculate needed space in the global offset table, and procedure
1465 linkage table. */
1467 static bool
1468 elf_i386_scan_relocs (bfd *abfd,
1469 struct bfd_link_info *info,
1470 asection *sec,
1471 const Elf_Internal_Rela *relocs)
1473 struct elf_x86_link_hash_table *htab;
1474 Elf_Internal_Shdr *symtab_hdr;
1475 struct elf_link_hash_entry **sym_hashes;
1476 const Elf_Internal_Rela *rel;
1477 const Elf_Internal_Rela *rel_end;
1478 bfd_byte *contents;
1479 bool converted;
1481 if (bfd_link_relocatable (info))
1482 return true;
1484 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1485 if (htab == NULL)
1487 sec->check_relocs_failed = 1;
1488 return false;
1491 BFD_ASSERT (is_x86_elf (abfd, htab));
1493 /* Get the section contents. */
1494 if (elf_section_data (sec)->this_hdr.contents != NULL)
1495 contents = elf_section_data (sec)->this_hdr.contents;
1496 else if (!_bfd_elf_mmap_section_contents (abfd, sec, &contents))
1498 sec->check_relocs_failed = 1;
1499 return false;
1502 symtab_hdr = &elf_symtab_hdr (abfd);
1503 sym_hashes = elf_sym_hashes (abfd);
1505 converted = false;
1507 rel_end = relocs + sec->reloc_count;
1508 for (rel = relocs; rel < rel_end; rel++)
1510 unsigned int r_type;
1511 unsigned int r_symndx;
1512 struct elf_link_hash_entry *h;
1513 struct elf_x86_link_hash_entry *eh;
1514 Elf_Internal_Sym *isym;
1515 const char *name;
1516 bool size_reloc;
1517 bool no_dynreloc;
1519 r_symndx = ELF32_R_SYM (rel->r_info);
1520 r_type = ELF32_R_TYPE (rel->r_info);
1522 /* Don't check R_386_NONE. */
1523 if (r_type == R_386_NONE)
1524 continue;
1526 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1528 /* xgettext:c-format */
1529 _bfd_error_handler (_("%pB: bad symbol index: %d"),
1530 abfd, r_symndx);
1531 goto error_return;
1534 if (r_symndx < symtab_hdr->sh_info)
1536 /* A local symbol. */
1537 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1538 abfd, r_symndx);
1539 if (isym == NULL)
1540 goto error_return;
1542 /* Check relocation against local STT_GNU_IFUNC symbol. */
1543 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1545 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, true);
1546 if (h == NULL)
1547 goto error_return;
1549 /* Fake a STT_GNU_IFUNC symbol. */
1550 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1551 isym, NULL);
1552 h->type = STT_GNU_IFUNC;
1553 h->def_regular = 1;
1554 h->ref_regular = 1;
1555 h->forced_local = 1;
1556 h->root.type = bfd_link_hash_defined;
1558 else
1559 h = NULL;
1561 else
1563 isym = NULL;
1564 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1565 while (h->root.type == bfd_link_hash_indirect
1566 || h->root.type == bfd_link_hash_warning)
1567 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1570 eh = (struct elf_x86_link_hash_entry *) h;
1571 if (h != NULL)
1573 if (r_type == R_386_GOTOFF)
1574 eh->gotoff_ref = 1;
1576 /* It is referenced by a non-shared object. */
1577 h->ref_regular = 1;
1580 if (r_type == R_386_GOT32X
1581 && (h == NULL || h->type != STT_GNU_IFUNC))
1583 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1584 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1585 &r_type, irel, h,
1586 &converted, info))
1587 goto error_return;
1590 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
1591 symtab_hdr, &no_dynreloc))
1592 return false;
1594 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1595 symtab_hdr, sym_hashes,
1596 &r_type, GOT_UNKNOWN,
1597 rel, rel_end, h, isym, false))
1598 goto error_return;
1600 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
1601 if (h == htab->elf.hgot)
1602 htab->got_referenced = true;
1604 switch (r_type)
1606 case R_386_TLS_LDM:
1607 htab->tls_ld_or_ldm_got.refcount = 1;
1608 goto create_got;
1610 case R_386_PLT32:
1611 /* This symbol requires a procedure linkage table entry. We
1612 actually build the entry in adjust_dynamic_symbol,
1613 because this might be a case of linking PIC code which is
1614 never referenced by a dynamic object, in which case we
1615 don't need to generate a procedure linkage table entry
1616 after all. */
1618 /* If this is a local symbol, we resolve it directly without
1619 creating a procedure linkage table entry. */
1620 if (h == NULL)
1621 continue;
1623 eh->zero_undefweak &= 0x2;
1624 h->needs_plt = 1;
1625 h->plt.refcount = 1;
1626 break;
1628 case R_386_SIZE32:
1629 size_reloc = true;
1630 goto do_size;
1632 case R_386_TLS_IE_32:
1633 case R_386_TLS_IE:
1634 case R_386_TLS_GOTIE:
1635 if (!bfd_link_executable (info))
1636 info->flags |= DF_STATIC_TLS;
1637 /* Fall through */
1639 case R_386_GOT32:
1640 case R_386_GOT32X:
1641 case R_386_TLS_GD:
1642 case R_386_TLS_GOTDESC:
1643 case R_386_TLS_DESC_CALL:
1644 /* This symbol requires a global offset table entry. */
1646 int tls_type, old_tls_type;
1648 switch (r_type)
1650 default:
1651 case R_386_GOT32:
1652 case R_386_GOT32X:
1653 tls_type = GOT_NORMAL;
1654 break;
1655 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1656 case R_386_TLS_GOTDESC:
1657 case R_386_TLS_DESC_CALL:
1658 tls_type = GOT_TLS_GDESC; break;
1659 case R_386_TLS_IE_32:
1660 if (ELF32_R_TYPE (rel->r_info) == r_type)
1661 tls_type = GOT_TLS_IE_NEG;
1662 else
1663 /* If this is a GD->IE transition, we may use either of
1664 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1665 tls_type = GOT_TLS_IE;
1666 break;
1667 case R_386_TLS_IE:
1668 case R_386_TLS_GOTIE:
1669 tls_type = GOT_TLS_IE_POS; break;
1672 if (h != NULL)
1674 h->got.refcount = 1;
1675 old_tls_type = elf_x86_hash_entry (h)->tls_type;
1677 else
1679 bfd_signed_vma *local_got_refcounts;
1681 if (!elf_x86_allocate_local_got_info (abfd,
1682 symtab_hdr->sh_info))
1683 goto error_return;
1685 /* This is a global offset table entry for a local symbol. */
1686 local_got_refcounts = elf_local_got_refcounts (abfd);
1687 local_got_refcounts[r_symndx] = 1;
1688 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1691 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1692 tls_type |= old_tls_type;
1693 /* If a TLS symbol is accessed using IE at least once,
1694 there is no point to use dynamic model for it. */
1695 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1696 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1697 || (tls_type & GOT_TLS_IE) == 0))
1699 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1700 tls_type = old_tls_type;
1701 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1702 && GOT_TLS_GD_ANY_P (tls_type))
1703 tls_type |= old_tls_type;
1704 else
1706 if (h)
1707 name = h->root.root.string;
1708 else
1709 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1710 NULL);
1711 _bfd_error_handler
1712 /* xgettext:c-format */
1713 (_("%pB: `%s' accessed both as normal and "
1714 "thread local symbol"),
1715 abfd, name);
1716 bfd_set_error (bfd_error_bad_value);
1717 goto error_return;
1721 if (old_tls_type != tls_type)
1723 if (h != NULL)
1724 elf_x86_hash_entry (h)->tls_type = tls_type;
1725 else
1726 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1729 /* Fall through */
1731 case R_386_GOTOFF:
1732 case R_386_GOTPC:
1733 create_got:
1734 if (r_type != R_386_TLS_IE)
1736 if (eh != NULL)
1738 eh->zero_undefweak &= 0x2;
1740 /* Need GOT to resolve undefined weak symbol to 0. */
1741 if (r_type == R_386_GOTOFF
1742 && h->root.type == bfd_link_hash_undefweak
1743 && bfd_link_executable (info))
1744 htab->got_referenced = true;
1746 break;
1748 /* Fall through */
1750 case R_386_TLS_LE_32:
1751 case R_386_TLS_LE:
1752 if (eh != NULL)
1753 eh->zero_undefweak &= 0x2;
1754 if (bfd_link_executable (info))
1755 break;
1756 info->flags |= DF_STATIC_TLS;
1757 goto do_relocation;
1759 case R_386_32:
1760 case R_386_PC32:
1761 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1762 eh->zero_undefweak |= 0x2;
1763 do_relocation:
1764 /* We are called after all symbols have been resolved. Only
1765 relocation against STT_GNU_IFUNC symbol must go through
1766 PLT. */
1767 if (h != NULL
1768 && (bfd_link_executable (info)
1769 || h->type == STT_GNU_IFUNC))
1771 bool func_pointer_ref = false;
1773 if (r_type == R_386_PC32)
1775 /* Since something like ".long foo - ." may be used
1776 as pointer, make sure that PLT is used if foo is
1777 a function defined in a shared library. */
1778 if ((sec->flags & SEC_CODE) == 0)
1779 h->pointer_equality_needed = 1;
1780 else if (h->type == STT_GNU_IFUNC
1781 && bfd_link_pic (info))
1783 _bfd_error_handler
1784 /* xgettext:c-format */
1785 (_("%pB: unsupported non-PIC call to IFUNC `%s'"),
1786 abfd, h->root.root.string);
1787 bfd_set_error (bfd_error_bad_value);
1788 goto error_return;
1791 else
1793 /* R_386_32 can be resolved at run-time. Function
1794 pointer reference doesn't need PLT for pointer
1795 equality. */
1796 if (r_type == R_386_32
1797 && (sec->flags & SEC_READONLY) == 0)
1798 func_pointer_ref = true;
1800 /* IFUNC symbol needs pointer equality in PDE so that
1801 function pointer reference will be resolved to its
1802 PLT entry directly. */
1803 if (!func_pointer_ref
1804 || (bfd_link_pde (info)
1805 && h->type == STT_GNU_IFUNC))
1806 h->pointer_equality_needed = 1;
1809 if (!func_pointer_ref)
1811 /* If this reloc is in a read-only section, we might
1812 need a copy reloc. We can't check reliably at this
1813 stage whether the section is read-only, as input
1814 sections have not yet been mapped to output sections.
1815 Tentatively set the flag for now, and correct in
1816 adjust_dynamic_symbol. */
1817 h->non_got_ref = 1;
1819 if (!elf_has_indirect_extern_access (sec->owner))
1820 eh->non_got_ref_without_indirect_extern_access = 1;
1822 /* We may need a .plt entry if the symbol is a function
1823 defined in a shared lib or is a function referenced
1824 from the code or read-only section. */
1825 if (!h->def_regular
1826 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1827 h->plt.refcount = 1;
1829 if (htab->elf.target_os != is_solaris
1830 && h->pointer_equality_needed
1831 && h->type == STT_FUNC
1832 && eh->def_protected
1833 && !SYMBOL_DEFINED_NON_SHARED_P (h)
1834 && h->def_dynamic)
1836 /* Disallow non-canonical reference to canonical
1837 protected function. */
1838 _bfd_error_handler
1839 /* xgettext:c-format */
1840 (_("%pB: non-canonical reference to canonical "
1841 "protected function `%s' in %pB"),
1842 abfd, h->root.root.string,
1843 h->root.u.def.section->owner);
1844 bfd_set_error (bfd_error_bad_value);
1845 goto error_return;
1850 size_reloc = false;
1851 do_size:
1852 if (!no_dynreloc
1853 && NEED_DYNAMIC_RELOCATION_P (false, info, false, h, sec,
1854 r_type, R_386_32))
1856 struct elf_dyn_relocs *p;
1857 struct elf_dyn_relocs **head;
1859 /* If this is a global symbol, we count the number of
1860 relocations we need for this symbol. */
1861 if (h != NULL)
1863 head = &h->dyn_relocs;
1865 else
1867 /* Track dynamic relocs needed for local syms too.
1868 We really need local syms available to do this
1869 easily. Oh well. */
1870 void **vpp;
1871 asection *s;
1873 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1874 abfd, r_symndx);
1875 if (isym == NULL)
1876 goto error_return;
1878 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1879 if (s == NULL)
1880 s = sec;
1882 vpp = &elf_section_data (s)->local_dynrel;
1883 head = (struct elf_dyn_relocs **)vpp;
1886 p = *head;
1887 if (p == NULL || p->sec != sec)
1889 size_t amt = sizeof *p;
1890 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1891 amt);
1892 if (p == NULL)
1893 goto error_return;
1894 p->next = *head;
1895 *head = p;
1896 p->sec = sec;
1897 p->count = 0;
1898 p->pc_count = 0;
1901 p->count += 1;
1902 /* Count size relocation as PC-relative relocation. */
1903 if (r_type == R_386_PC32 || size_reloc)
1904 p->pc_count += 1;
1906 break;
1908 /* This relocation describes the C++ object vtable hierarchy.
1909 Reconstruct it for later use during GC. */
1910 case R_386_GNU_VTINHERIT:
1911 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1912 goto error_return;
1913 break;
1915 /* This relocation describes which C++ vtable entries are actually
1916 used. Record for later use during GC. */
1917 case R_386_GNU_VTENTRY:
1918 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1919 goto error_return;
1920 break;
1922 default:
1923 break;
1927 if (elf_section_data (sec)->this_hdr.contents != contents)
1929 if (!converted)
1930 _bfd_elf_munmap_section_contents (sec, contents);
1931 else
1933 /* Cache the section contents for elf_link_input_bfd if any
1934 load is converted or --no-keep-memory isn't used. */
1935 elf_section_data (sec)->this_hdr.contents = contents;
1936 info->cache_size += sec->size;
1940 /* Cache relocations if any load is converted. */
1941 if (elf_section_data (sec)->relocs != relocs && converted)
1942 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1944 return true;
1946 error_return:
1947 if (elf_section_data (sec)->this_hdr.contents != contents)
1948 _bfd_elf_munmap_section_contents (sec, contents);
1949 sec->check_relocs_failed = 1;
1950 return false;
1953 static bool
1954 elf_i386_early_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1956 bfd *abfd;
1958 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
1959 for (abfd = info->input_bfds;
1960 abfd != (bfd *) NULL;
1961 abfd = abfd->link.next)
1962 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1963 && !_bfd_elf_link_iterate_on_relocs (abfd, info,
1964 elf_i386_scan_relocs))
1965 return false;
1967 return _bfd_x86_elf_early_size_sections (output_bfd, info);
1970 /* Set the correct type for an x86 ELF section. We do this by the
1971 section name, which is a hack, but ought to work. */
1973 static bool
1974 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1975 Elf_Internal_Shdr *hdr,
1976 asection *sec)
1978 const char *name;
1980 name = bfd_section_name (sec);
1982 /* This is an ugly, but unfortunately necessary hack that is
1983 needed when producing EFI binaries on x86. It tells
1984 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1985 containing ELF relocation info. We need this hack in order to
1986 be able to generate ELF binaries that can be translated into
1987 EFI applications (which are essentially COFF objects). Those
1988 files contain a COFF ".reloc" section inside an ELFNN object,
1989 which would normally cause BFD to segfault because it would
1990 attempt to interpret this section as containing relocation
1991 entries for section "oc". With this hack enabled, ".reloc"
1992 will be treated as a normal data section, which will avoid the
1993 segfault. However, you won't be able to create an ELFNN binary
1994 with a section named "oc" that needs relocations, but that's
1995 the kind of ugly side-effects you get when detecting section
1996 types based on their names... In practice, this limitation is
1997 unlikely to bite. */
1998 if (strcmp (name, ".reloc") == 0)
1999 hdr->sh_type = SHT_PROGBITS;
2001 return true;
2004 /* Return the relocation value for @tpoff relocation
2005 if STT_TLS virtual address is ADDRESS. */
2007 static bfd_vma
2008 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2010 struct elf_link_hash_table *htab = elf_hash_table (info);
2011 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2012 bfd_vma static_tls_size;
2014 /* If tls_sec is NULL, we should have signalled an error already. */
2015 if (htab->tls_sec == NULL)
2016 return 0;
2018 /* Consider special static TLS alignment requirements. */
2019 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2020 return static_tls_size + htab->tls_sec->vma - address;
2023 /* Relocate an i386 ELF section. */
2025 static int
2026 elf_i386_relocate_section (bfd *output_bfd,
2027 struct bfd_link_info *info,
2028 bfd *input_bfd,
2029 asection *input_section,
2030 bfd_byte *contents,
2031 Elf_Internal_Rela *relocs,
2032 Elf_Internal_Sym *local_syms,
2033 asection **local_sections)
2035 struct elf_x86_link_hash_table *htab;
2036 Elf_Internal_Shdr *symtab_hdr;
2037 struct elf_link_hash_entry **sym_hashes;
2038 bfd_vma *local_got_offsets;
2039 bfd_vma *local_tlsdesc_gotents;
2040 Elf_Internal_Rela *rel;
2041 Elf_Internal_Rela *wrel;
2042 Elf_Internal_Rela *relend;
2043 bool is_vxworks_tls;
2044 unsigned expected_tls_le;
2045 unsigned plt_entry_size;
2047 /* Skip if check_relocs or scan_relocs failed. */
2048 if (input_section->check_relocs_failed)
2049 return false;
2051 htab = elf_x86_hash_table (info, I386_ELF_DATA);
2052 if (htab == NULL)
2053 return false;
2055 if (!is_x86_elf (input_bfd, htab))
2057 bfd_set_error (bfd_error_wrong_format);
2058 return false;
2061 symtab_hdr = &elf_symtab_hdr (input_bfd);
2062 sym_hashes = elf_sym_hashes (input_bfd);
2063 local_got_offsets = elf_local_got_offsets (input_bfd);
2064 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2065 /* We have to handle relocations in vxworks .tls_vars sections
2066 specially, because the dynamic loader is 'weird'. */
2067 is_vxworks_tls = (htab->elf.target_os == is_vxworks
2068 && bfd_link_pic (info)
2069 && !strcmp (input_section->output_section->name,
2070 ".tls_vars"));
2072 _bfd_x86_elf_set_tls_module_base (info);
2074 plt_entry_size = htab->plt.plt_entry_size;
2076 rel = wrel = relocs;
2077 relend = relocs + input_section->reloc_count;
2078 for (; rel < relend; wrel++, rel++)
2080 unsigned int r_type, r_type_tls;
2081 reloc_howto_type *howto;
2082 unsigned long r_symndx;
2083 struct elf_link_hash_entry *h;
2084 struct elf_x86_link_hash_entry *eh;
2085 Elf_Internal_Sym *sym;
2086 asection *sec;
2087 bfd_vma off, offplt, plt_offset;
2088 bfd_vma relocation;
2089 bool unresolved_reloc;
2090 bfd_reloc_status_type r;
2091 unsigned int indx;
2092 int tls_type;
2093 bfd_vma st_size;
2094 asection *resolved_plt;
2095 bool resolved_to_zero;
2096 bool relative_reloc;
2098 r_type = ELF32_R_TYPE (rel->r_info);
2099 if (r_type == R_386_GNU_VTINHERIT
2100 || r_type == R_386_GNU_VTENTRY)
2102 if (wrel != rel)
2103 *wrel = *rel;
2104 continue;
2107 howto = elf_i386_rtype_to_howto (r_type);
2108 if (howto == NULL)
2109 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2111 r_symndx = ELF32_R_SYM (rel->r_info);
2112 h = NULL;
2113 sym = NULL;
2114 sec = NULL;
2115 unresolved_reloc = false;
2116 if (r_symndx < symtab_hdr->sh_info)
2118 sym = local_syms + r_symndx;
2119 sec = local_sections[r_symndx];
2120 relocation = (sec->output_section->vma
2121 + sec->output_offset
2122 + sym->st_value);
2123 st_size = sym->st_size;
2125 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2126 && ((sec->flags & SEC_MERGE) != 0
2127 || (bfd_link_relocatable (info)
2128 && sec->output_offset != 0)))
2130 bfd_vma addend;
2131 bfd_byte *where = contents + rel->r_offset;
2133 switch (bfd_get_reloc_size (howto))
2135 case 1:
2136 addend = bfd_get_8 (input_bfd, where);
2137 if (howto->pc_relative)
2139 addend = (addend ^ 0x80) - 0x80;
2140 addend += 1;
2142 break;
2143 case 2:
2144 addend = bfd_get_16 (input_bfd, where);
2145 if (howto->pc_relative)
2147 addend = (addend ^ 0x8000) - 0x8000;
2148 addend += 2;
2150 break;
2151 case 4:
2152 addend = bfd_get_32 (input_bfd, where);
2153 if (howto->pc_relative)
2155 addend = (addend ^ 0x80000000) - 0x80000000;
2156 addend += 4;
2158 break;
2159 default:
2160 abort ();
2163 if (bfd_link_relocatable (info))
2164 addend += sec->output_offset;
2165 else
2167 asection *msec = sec;
2168 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2169 addend);
2170 addend -= relocation;
2171 addend += msec->output_section->vma + msec->output_offset;
2174 switch (bfd_get_reloc_size (howto))
2176 case 1:
2177 /* FIXME: overflow checks. */
2178 if (howto->pc_relative)
2179 addend -= 1;
2180 bfd_put_8 (input_bfd, addend, where);
2181 break;
2182 case 2:
2183 if (howto->pc_relative)
2184 addend -= 2;
2185 bfd_put_16 (input_bfd, addend, where);
2186 break;
2187 case 4:
2188 if (howto->pc_relative)
2189 addend -= 4;
2190 bfd_put_32 (input_bfd, addend, where);
2191 break;
2194 else if (!bfd_link_relocatable (info)
2195 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2197 /* Relocate against local STT_GNU_IFUNC symbol. */
2198 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2199 false);
2200 if (h == NULL)
2201 abort ();
2203 /* Set STT_GNU_IFUNC symbol value. */
2204 h->root.u.def.value = sym->st_value;
2205 h->root.u.def.section = sec;
2208 else
2210 bool warned ATTRIBUTE_UNUSED;
2211 bool ignored ATTRIBUTE_UNUSED;
2213 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2214 r_symndx, symtab_hdr, sym_hashes,
2215 h, sec, relocation,
2216 unresolved_reloc, warned, ignored);
2217 st_size = h->size;
2220 if (sec != NULL && discarded_section (sec))
2222 _bfd_clear_contents (howto, input_bfd, input_section,
2223 contents, rel->r_offset);
2224 wrel->r_offset = rel->r_offset;
2225 wrel->r_info = 0;
2226 wrel->r_addend = 0;
2228 /* For ld -r, remove relocations in debug sections against
2229 sections defined in discarded sections. Not done for
2230 eh_frame editing code expects to be present. */
2231 if (bfd_link_relocatable (info)
2232 && (input_section->flags & SEC_DEBUGGING))
2233 wrel--;
2235 continue;
2238 if (bfd_link_relocatable (info))
2240 if (wrel != rel)
2241 *wrel = *rel;
2242 continue;
2245 eh = (struct elf_x86_link_hash_entry *) h;
2247 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2248 it here if it is defined in a non-shared object. */
2249 if (h != NULL
2250 && h->type == STT_GNU_IFUNC
2251 && h->def_regular)
2253 asection *gotplt, *base_got;
2254 bfd_vma plt_index;
2255 const char *name;
2257 if ((input_section->flags & SEC_ALLOC) == 0)
2259 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2260 STT_GNU_IFUNC symbol as STT_FUNC. */
2261 if (elf_section_type (input_section) == SHT_NOTE)
2262 goto skip_ifunc;
2263 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2264 sections because such sections are not SEC_ALLOC and
2265 thus ld.so will not process them. */
2266 if ((input_section->flags & SEC_DEBUGGING) != 0)
2267 continue;
2268 abort ();
2271 /* STT_GNU_IFUNC symbol must go through PLT. */
2272 if (htab->elf.splt != NULL)
2274 if (htab->plt_second != NULL)
2276 resolved_plt = htab->plt_second;
2277 plt_offset = eh->plt_second.offset;
2279 else
2281 resolved_plt = htab->elf.splt;
2282 plt_offset = h->plt.offset;
2284 gotplt = htab->elf.sgotplt;
2286 else
2288 resolved_plt = htab->elf.iplt;
2289 plt_offset = h->plt.offset;
2290 gotplt = htab->elf.igotplt;
2293 switch (r_type)
2295 default:
2296 break;
2298 case R_386_GOT32:
2299 case R_386_GOT32X:
2300 base_got = htab->elf.sgot;
2301 off = h->got.offset;
2303 if (base_got == NULL)
2304 abort ();
2306 if (off == (bfd_vma) -1)
2308 /* We can't use h->got.offset here to save state, or
2309 even just remember the offset, as finish_dynamic_symbol
2310 would use that as offset into .got. */
2312 if (h->plt.offset == (bfd_vma) -1)
2313 abort ();
2315 if (htab->elf.splt != NULL)
2317 plt_index = (h->plt.offset / plt_entry_size
2318 - htab->plt.has_plt0);
2319 off = (plt_index + 3) * 4;
2320 base_got = htab->elf.sgotplt;
2322 else
2324 plt_index = h->plt.offset / plt_entry_size;
2325 off = plt_index * 4;
2326 base_got = htab->elf.igotplt;
2329 if (h->dynindx == -1
2330 || h->forced_local
2331 || info->symbolic)
2333 /* This references the local defitionion. We must
2334 initialize this entry in the global offset table.
2335 Since the offset must always be a multiple of 4,
2336 we use the least significant bit to record
2337 whether we have initialized it already.
2339 When doing a dynamic link, we create a .rela.got
2340 relocation entry to initialize the value. This
2341 is done in the finish_dynamic_symbol routine. */
2342 if ((off & 1) != 0)
2343 off &= ~1;
2344 else
2346 bfd_put_32 (output_bfd, relocation,
2347 base_got->contents + off);
2348 h->got.offset |= 1;
2352 relocation = off;
2354 else
2355 relocation = (base_got->output_section->vma
2356 + base_got->output_offset + off
2357 - gotplt->output_section->vma
2358 - gotplt->output_offset);
2360 if (rel->r_offset > 1
2361 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2362 && *(contents + rel->r_offset - 2) != 0x8d)
2364 if (bfd_link_pic (info))
2365 goto disallow_got32;
2367 /* Add the GOT base if there is no base register. */
2368 relocation += (gotplt->output_section->vma
2369 + gotplt->output_offset);
2371 else if (htab->elf.splt == NULL)
2373 /* Adjust for static executables. */
2374 relocation += gotplt->output_offset;
2377 goto do_relocation;
2380 if (h->plt.offset == (bfd_vma) -1)
2382 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2383 if (r_type == R_386_32
2384 && (input_section->flags & SEC_CODE) == 0)
2385 goto do_ifunc_pointer;
2386 goto bad_ifunc_reloc;
2389 relocation = (resolved_plt->output_section->vma
2390 + resolved_plt->output_offset + plt_offset);
2392 switch (r_type)
2394 default:
2395 bad_ifunc_reloc:
2396 if (h->root.root.string)
2397 name = h->root.root.string;
2398 else
2399 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2400 NULL);
2401 _bfd_error_handler
2402 /* xgettext:c-format */
2403 (_("%pB: relocation %s against STT_GNU_IFUNC "
2404 "symbol `%s' isn't supported"), input_bfd,
2405 howto->name, name);
2406 bfd_set_error (bfd_error_bad_value);
2407 return false;
2409 case R_386_32:
2410 /* Generate dynamic relcoation only when there is a
2411 non-GOT reference in a shared object. */
2412 if ((bfd_link_pic (info) && h->non_got_ref)
2413 || h->plt.offset == (bfd_vma) -1)
2415 Elf_Internal_Rela outrel;
2416 asection *sreloc;
2417 bfd_vma offset;
2419 do_ifunc_pointer:
2420 /* Need a dynamic relocation to get the real function
2421 adddress. */
2422 offset = _bfd_elf_section_offset (output_bfd,
2423 info,
2424 input_section,
2425 rel->r_offset);
2426 if (offset == (bfd_vma) -1
2427 || offset == (bfd_vma) -2)
2428 abort ();
2430 outrel.r_offset = (input_section->output_section->vma
2431 + input_section->output_offset
2432 + offset);
2434 if (POINTER_LOCAL_IFUNC_P (info, h))
2436 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2437 h->root.root.string,
2438 h->root.u.def.section->owner);
2440 /* This symbol is resolved locally. */
2441 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2443 if (htab->params->report_relative_reloc)
2444 _bfd_x86_elf_link_report_relative_reloc
2445 (info, input_section, h, sym,
2446 "R_386_IRELATIVE", &outrel);
2448 bfd_put_32 (output_bfd,
2449 (h->root.u.def.value
2450 + h->root.u.def.section->output_section->vma
2451 + h->root.u.def.section->output_offset),
2452 contents + offset);
2454 else
2455 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2457 /* Dynamic relocations are stored in
2458 1. .rel.ifunc section in PIC object.
2459 2. .rel.got section in dynamic executable.
2460 3. .rel.iplt section in static executable. */
2461 if (bfd_link_pic (info))
2462 sreloc = htab->elf.irelifunc;
2463 else if (htab->elf.splt != NULL)
2464 sreloc = htab->elf.srelgot;
2465 else
2466 sreloc = htab->elf.irelplt;
2467 elf_append_rel (output_bfd, sreloc, &outrel);
2469 /* If this reloc is against an external symbol, we
2470 do not want to fiddle with the addend. Otherwise,
2471 we need to include the symbol value so that it
2472 becomes an addend for the dynamic reloc. For an
2473 internal symbol, we have updated addend. */
2474 continue;
2476 /* FALLTHROUGH */
2477 case R_386_PC32:
2478 case R_386_PLT32:
2479 goto do_relocation;
2481 case R_386_GOTOFF:
2482 /* NB: We can't use the PLT entry as the function address
2483 for PIC since the PIC register may not be set up
2484 properly for indirect call. */
2485 if (bfd_link_pic (info))
2486 goto bad_ifunc_reloc;
2487 relocation -= (gotplt->output_section->vma
2488 + gotplt->output_offset);
2489 goto do_relocation;
2493 skip_ifunc:
2494 resolved_to_zero = (eh != NULL
2495 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2497 switch (r_type)
2499 case R_386_GOT32X:
2500 case R_386_GOT32:
2501 /* Relocation is to the entry for this symbol in the global
2502 offset table. */
2503 if (htab->elf.sgot == NULL)
2504 abort ();
2506 relative_reloc = false;
2507 if (h != NULL)
2509 off = h->got.offset;
2510 if (RESOLVED_LOCALLY_P (info, h, htab))
2512 /* We must initialize this entry in the global offset
2513 table. Since the offset must always be a multiple
2514 of 4, we use the least significant bit to record
2515 whether we have initialized it already.
2517 When doing a dynamic link, we create a .rel.got
2518 relocation entry to initialize the value. This
2519 is done in the finish_dynamic_symbol routine. */
2520 if ((off & 1) != 0)
2521 off &= ~1;
2522 else
2524 bfd_put_32 (output_bfd, relocation,
2525 htab->elf.sgot->contents + off);
2526 h->got.offset |= 1;
2527 /* NB: Don't generate relative relocation here if
2528 it has been generated by DT_RELR. */
2529 if (!info->enable_dt_relr
2530 && GENERATE_RELATIVE_RELOC_P (info, h))
2532 /* PR ld/21402: If this symbol isn't dynamic
2533 in PIC, generate R_386_RELATIVE here. */
2534 eh->no_finish_dynamic_symbol = 1;
2535 relative_reloc = true;
2539 else
2540 unresolved_reloc = false;
2542 else
2544 if (local_got_offsets == NULL)
2545 abort ();
2547 off = local_got_offsets[r_symndx];
2549 /* The offset must always be a multiple of 4. We use
2550 the least significant bit to record whether we have
2551 already generated the necessary reloc. */
2552 if ((off & 1) != 0)
2553 off &= ~1;
2554 else
2556 bfd_put_32 (output_bfd, relocation,
2557 htab->elf.sgot->contents + off);
2558 local_got_offsets[r_symndx] |= 1;
2560 /* NB: Don't generate relative relocation here if it
2561 has been generated by DT_RELR. */
2562 if (!info->enable_dt_relr && bfd_link_pic (info))
2563 relative_reloc = true;
2567 if (relative_reloc)
2569 asection *s;
2570 Elf_Internal_Rela outrel;
2572 s = htab->elf.srelgot;
2573 if (s == NULL)
2574 abort ();
2576 outrel.r_offset = (htab->elf.sgot->output_section->vma
2577 + htab->elf.sgot->output_offset
2578 + off);
2579 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2581 if (htab->params->report_relative_reloc)
2582 _bfd_x86_elf_link_report_relative_reloc
2583 (info, input_section, h, sym, "R_386_RELATIVE",
2584 &outrel);
2586 elf_append_rel (output_bfd, s, &outrel);
2589 if (off >= (bfd_vma) -2)
2590 abort ();
2592 relocation = (htab->elf.sgot->output_section->vma
2593 + htab->elf.sgot->output_offset + off);
2594 if (rel->r_offset > 1
2595 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2596 && *(contents + rel->r_offset - 2) != 0x8d)
2598 if (bfd_link_pic (info))
2600 /* For PIC, disallow R_386_GOT32 without a base
2601 register, except for "lea foo@GOT, %reg", since
2602 we don't know what the GOT base is. */
2603 const char *name;
2605 disallow_got32:
2606 if (h == NULL || h->root.root.string == NULL)
2607 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2608 NULL);
2609 else
2610 name = h->root.root.string;
2612 _bfd_error_handler
2613 /* xgettext:c-format */
2614 (_("%pB: direct GOT relocation %s against `%s'"
2615 " without base register can not be used"
2616 " when making a shared object"),
2617 input_bfd, howto->name, name);
2618 bfd_set_error (bfd_error_bad_value);
2619 return false;
2622 else
2624 /* Subtract the .got.plt section address only with a base
2625 register. */
2626 relocation -= (htab->elf.sgotplt->output_section->vma
2627 + htab->elf.sgotplt->output_offset);
2630 break;
2632 case R_386_GOTOFF:
2633 /* Relocation is relative to the start of the global offset
2634 table. */
2636 /* Check to make sure it isn't a protected function or data
2637 symbol for shared library since it may not be local when
2638 used as function address or with copy relocation. We also
2639 need to make sure that a symbol is referenced locally. */
2640 if (!bfd_link_executable (info) && h)
2642 if (!h->def_regular)
2644 const char *v;
2646 switch (ELF_ST_VISIBILITY (h->other))
2648 case STV_HIDDEN:
2649 v = _("hidden symbol");
2650 break;
2651 case STV_INTERNAL:
2652 v = _("internal symbol");
2653 break;
2654 case STV_PROTECTED:
2655 v = _("protected symbol");
2656 break;
2657 default:
2658 v = _("symbol");
2659 break;
2662 _bfd_error_handler
2663 /* xgettext:c-format */
2664 (_("%pB: relocation R_386_GOTOFF against undefined %s"
2665 " `%s' can not be used when making a shared object"),
2666 input_bfd, v, h->root.root.string);
2667 bfd_set_error (bfd_error_bad_value);
2668 return false;
2670 else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2671 && (h->type == STT_FUNC
2672 || h->type == STT_OBJECT)
2673 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2675 _bfd_error_handler
2676 /* xgettext:c-format */
2677 (_("%pB: relocation R_386_GOTOFF against protected %s"
2678 " `%s' can not be used when making a shared object"),
2679 input_bfd,
2680 h->type == STT_FUNC ? "function" : "data",
2681 h->root.root.string);
2682 bfd_set_error (bfd_error_bad_value);
2683 return false;
2687 /* Note that sgot is not involved in this
2688 calculation. We always want the start of .got.plt. If we
2689 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2690 permitted by the ABI, we might have to change this
2691 calculation. */
2692 relocation -= htab->elf.sgotplt->output_section->vma
2693 + htab->elf.sgotplt->output_offset;
2694 break;
2696 case R_386_GOTPC:
2697 /* Use global offset table as symbol value. */
2698 relocation = htab->elf.sgotplt->output_section->vma
2699 + htab->elf.sgotplt->output_offset;
2700 unresolved_reloc = false;
2701 break;
2703 case R_386_PLT32:
2704 /* Relocation is to the entry for this symbol in the
2705 procedure linkage table. */
2707 /* Resolve a PLT32 reloc against a local symbol directly,
2708 without using the procedure linkage table. */
2709 if (h == NULL)
2710 break;
2712 if ((h->plt.offset == (bfd_vma) -1
2713 && eh->plt_got.offset == (bfd_vma) -1)
2714 || htab->elf.splt == NULL)
2716 /* We didn't make a PLT entry for this symbol. This
2717 happens when statically linking PIC code, or when
2718 using -Bsymbolic. */
2719 break;
2722 if (h->plt.offset != (bfd_vma) -1)
2724 if (htab->plt_second != NULL)
2726 resolved_plt = htab->plt_second;
2727 plt_offset = eh->plt_second.offset;
2729 else
2731 resolved_plt = htab->elf.splt;
2732 plt_offset = h->plt.offset;
2735 else
2737 resolved_plt = htab->plt_got;
2738 plt_offset = eh->plt_got.offset;
2741 relocation = (resolved_plt->output_section->vma
2742 + resolved_plt->output_offset
2743 + plt_offset);
2744 unresolved_reloc = false;
2745 break;
2747 case R_386_SIZE32:
2748 /* Set to symbol size. */
2749 relocation = st_size;
2750 /* Fall through. */
2752 case R_386_32:
2753 case R_386_PC32:
2754 if ((input_section->flags & SEC_ALLOC) == 0
2755 || is_vxworks_tls)
2756 break;
2758 if (GENERATE_DYNAMIC_RELOCATION_P (false, info, eh, r_type,
2759 sec, false,
2760 resolved_to_zero,
2761 (r_type == R_386_PC32)))
2763 Elf_Internal_Rela outrel;
2764 bool skip, relocate;
2765 bool generate_dynamic_reloc = true;
2766 asection *sreloc;
2768 /* When generating a shared object, these relocations
2769 are copied into the output file to be resolved at run
2770 time. */
2772 skip = false;
2773 relocate = false;
2775 outrel.r_offset =
2776 _bfd_elf_section_offset (output_bfd, info, input_section,
2777 rel->r_offset);
2778 if (outrel.r_offset == (bfd_vma) -1)
2779 skip = true;
2780 else if (outrel.r_offset == (bfd_vma) -2)
2781 skip = true, relocate = true;
2782 outrel.r_offset += (input_section->output_section->vma
2783 + input_section->output_offset);
2785 if (skip)
2786 memset (&outrel, 0, sizeof outrel);
2787 else if (COPY_INPUT_RELOC_P (false, info, h, r_type))
2788 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2789 else
2791 /* This symbol is local, or marked to become local. */
2792 relocate = true;
2793 /* NB: Don't generate relative relocation here if it
2794 has been generated by DT_RELR. */
2795 if (info->enable_dt_relr)
2796 generate_dynamic_reloc = false;
2797 else
2799 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2801 if (htab->params->report_relative_reloc)
2802 _bfd_x86_elf_link_report_relative_reloc
2803 (info, input_section, h, sym, "R_386_RELATIVE",
2804 &outrel);
2808 if (generate_dynamic_reloc)
2810 sreloc = elf_section_data (input_section)->sreloc;
2812 if (sreloc == NULL || sreloc->contents == NULL)
2814 r = bfd_reloc_notsupported;
2815 goto check_relocation_error;
2818 elf_append_rel (output_bfd, sreloc, &outrel);
2821 /* If this reloc is against an external symbol, we do
2822 not want to fiddle with the addend. Otherwise, we
2823 need to include the symbol value so that it becomes
2824 an addend for the dynamic reloc. */
2825 if (! relocate)
2826 continue;
2828 break;
2830 case R_386_TLS_IE:
2831 if (!bfd_link_executable (info))
2833 Elf_Internal_Rela outrel;
2834 asection *sreloc;
2836 outrel.r_offset = rel->r_offset
2837 + input_section->output_section->vma
2838 + input_section->output_offset;
2839 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2841 if (htab->params->report_relative_reloc)
2842 _bfd_x86_elf_link_report_relative_reloc
2843 (info, input_section, h, sym, "R_386_RELATIVE",
2844 &outrel);
2846 sreloc = elf_section_data (input_section)->sreloc;
2847 if (sreloc == NULL)
2848 abort ();
2849 elf_append_rel (output_bfd, sreloc, &outrel);
2851 /* Fall through */
2853 case R_386_TLS_GD:
2854 case R_386_TLS_GOTDESC:
2855 case R_386_TLS_DESC_CALL:
2856 case R_386_TLS_IE_32:
2857 case R_386_TLS_GOTIE:
2858 tls_type = GOT_UNKNOWN;
2859 if (h == NULL && local_got_offsets)
2860 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2861 else if (h != NULL)
2862 tls_type = elf_x86_hash_entry(h)->tls_type;
2863 if (tls_type == GOT_TLS_IE)
2864 tls_type = GOT_TLS_IE_NEG;
2866 r_type_tls = r_type;
2867 if (! elf_i386_tls_transition (info, input_bfd,
2868 input_section, contents,
2869 symtab_hdr, sym_hashes,
2870 &r_type_tls, tls_type, rel,
2871 relend, h, sym, true))
2872 return false;
2874 expected_tls_le = htab->elf.target_os == is_solaris
2875 ? R_386_TLS_LE : R_386_TLS_LE_32;
2876 if (r_type_tls == expected_tls_le)
2878 /* NB: Solaris only supports R_386_TLS_GD->R_386_TLS_LE. */
2879 BFD_ASSERT (! unresolved_reloc
2880 && (htab->elf.target_os != is_solaris
2881 || (htab->elf.target_os == is_solaris
2882 && (r_type == R_386_TLS_GD
2883 || r_type == R_386_TLS_IE
2884 || r_type == R_386_TLS_GOTIE))));
2885 if (r_type == R_386_TLS_GD)
2887 unsigned int type;
2888 bfd_vma roff;
2890 /* GD->LE transition. */
2891 type = *(contents + rel->r_offset - 2);
2892 if (type == 0x04)
2894 /* Change
2895 leal foo@tlsgd(,%ebx,1), %eax
2896 call ___tls_get_addr@PLT
2897 into:
2898 movl %gs:0, %eax
2899 subl $foo@tpoff, %eax
2900 (6 byte form of subl). */
2901 roff = rel->r_offset + 5;
2903 else
2905 /* Change
2906 leal foo@tlsgd(%ebx), %eax
2907 call ___tls_get_addr@PLT
2910 leal foo@tlsgd(%reg), %eax
2911 call *___tls_get_addr@GOT(%reg)
2912 which may be converted to
2913 addr32 call ___tls_get_addr
2914 into:
2915 movl %gs:0, %eax; subl $foo@tpoff, %eax
2916 (6 byte form of subl). */
2917 roff = rel->r_offset + 6;
2919 memcpy (contents + roff - 8,
2920 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2921 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2922 contents + roff);
2923 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
2924 rel++;
2925 wrel++;
2926 continue;
2928 else if (r_type == R_386_TLS_GOTDESC)
2930 /* GDesc -> LE transition.
2931 It's originally something like:
2932 leal x@tlsdesc(%ebx), %eax
2934 leal x@ntpoff, %eax
2936 Registers other than %eax may be set up here. */
2938 unsigned int val;
2939 bfd_vma roff;
2941 roff = rel->r_offset;
2942 val = bfd_get_8 (input_bfd, contents + roff - 1);
2944 /* Now modify the instruction as appropriate. */
2945 /* aoliva FIXME: remove the above and xor the byte
2946 below with 0x86. */
2947 bfd_put_8 (output_bfd, val ^ 0x86,
2948 contents + roff - 1);
2949 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2950 contents + roff);
2951 continue;
2953 else if (r_type == R_386_TLS_DESC_CALL)
2955 /* GDesc -> LE transition.
2956 It's originally:
2957 call *(%eax)
2958 Turn it into:
2959 xchg %ax,%ax */
2961 bfd_vma roff;
2963 roff = rel->r_offset;
2964 bfd_put_8 (output_bfd, 0x66, contents + roff);
2965 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2966 continue;
2968 else if (r_type == R_386_TLS_IE)
2970 unsigned int val;
2972 /* IE->LE transition:
2973 Originally it can be one of:
2974 movl foo, %eax
2975 movl foo, %reg
2976 addl foo, %reg
2977 We change it into:
2978 movl $foo, %eax
2979 movl $foo, %reg
2980 addl $foo, %reg. */
2981 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2982 if (val == 0xa1)
2984 /* movl foo, %eax. */
2985 bfd_put_8 (output_bfd, 0xb8,
2986 contents + rel->r_offset - 1);
2988 else
2990 unsigned int type;
2992 type = bfd_get_8 (input_bfd,
2993 contents + rel->r_offset - 2);
2994 switch (type)
2996 case 0x8b:
2997 /* movl */
2998 bfd_put_8 (output_bfd, 0xc7,
2999 contents + rel->r_offset - 2);
3000 bfd_put_8 (output_bfd,
3001 0xc0 | ((val >> 3) & 7),
3002 contents + rel->r_offset - 1);
3003 break;
3004 case 0x03:
3005 /* addl */
3006 bfd_put_8 (output_bfd, 0x81,
3007 contents + rel->r_offset - 2);
3008 bfd_put_8 (output_bfd,
3009 0xc0 | ((val >> 3) & 7),
3010 contents + rel->r_offset - 1);
3011 break;
3012 default:
3013 BFD_FAIL ();
3014 break;
3017 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3018 contents + rel->r_offset);
3019 continue;
3021 else
3023 unsigned int val, type;
3025 /* {IE_32,GOTIE}->LE transition:
3026 Originally it can be one of:
3027 subl foo(%reg1), %reg2
3028 movl foo(%reg1), %reg2
3029 addl foo(%reg1), %reg2
3030 We change it into:
3031 subl $foo, %reg2
3032 movl $foo, %reg2 (6 byte form)
3033 addl $foo, %reg2. */
3034 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3035 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3036 if (type == 0x8b)
3038 /* movl */
3039 bfd_put_8 (output_bfd, 0xc7,
3040 contents + rel->r_offset - 2);
3041 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3042 contents + rel->r_offset - 1);
3044 else if (type == 0x2b)
3046 /* subl */
3047 bfd_put_8 (output_bfd, 0x81,
3048 contents + rel->r_offset - 2);
3049 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3050 contents + rel->r_offset - 1);
3052 else if (type == 0x03)
3054 /* addl */
3055 bfd_put_8 (output_bfd, 0x81,
3056 contents + rel->r_offset - 2);
3057 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3058 contents + rel->r_offset - 1);
3060 else
3061 BFD_FAIL ();
3062 if (r_type == R_386_TLS_GOTIE)
3063 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3064 contents + rel->r_offset);
3065 else
3066 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3067 contents + rel->r_offset);
3068 continue;
3072 if (htab->elf.sgot == NULL)
3073 abort ();
3075 if (h != NULL)
3077 off = h->got.offset;
3078 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3080 else
3082 if (local_got_offsets == NULL)
3083 abort ();
3085 off = local_got_offsets[r_symndx];
3086 offplt = local_tlsdesc_gotents[r_symndx];
3089 if ((off & 1) != 0)
3090 off &= ~1;
3091 else
3093 Elf_Internal_Rela outrel;
3094 int dr_type;
3095 asection *sreloc;
3097 if (htab->elf.srelgot == NULL)
3098 abort ();
3100 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3102 if (GOT_TLS_GDESC_P (tls_type))
3104 bfd_byte *loc;
3105 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3106 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3107 <= htab->elf.sgotplt->size);
3108 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3109 + htab->elf.sgotplt->output_offset
3110 + offplt
3111 + htab->sgotplt_jump_table_size);
3112 sreloc = htab->elf.srelplt;
3113 loc = sreloc->contents;
3114 loc += (htab->next_tls_desc_index++
3115 * sizeof (Elf32_External_Rel));
3116 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3117 <= sreloc->contents + sreloc->size);
3118 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3119 if (indx == 0)
3121 BFD_ASSERT (! unresolved_reloc);
3122 bfd_put_32 (output_bfd,
3123 relocation - _bfd_x86_elf_dtpoff_base (info),
3124 htab->elf.sgotplt->contents + offplt
3125 + htab->sgotplt_jump_table_size + 4);
3127 else
3129 bfd_put_32 (output_bfd, 0,
3130 htab->elf.sgotplt->contents + offplt
3131 + htab->sgotplt_jump_table_size + 4);
3135 sreloc = htab->elf.srelgot;
3137 outrel.r_offset = (htab->elf.sgot->output_section->vma
3138 + htab->elf.sgot->output_offset + off);
3140 if (GOT_TLS_GD_P (tls_type))
3141 dr_type = R_386_TLS_DTPMOD32;
3142 else if (GOT_TLS_GDESC_P (tls_type))
3143 goto dr_done;
3144 else if (tls_type == GOT_TLS_IE_POS)
3145 dr_type = R_386_TLS_TPOFF;
3146 else
3147 dr_type = R_386_TLS_TPOFF32;
3149 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3150 bfd_put_32 (output_bfd,
3151 relocation - _bfd_x86_elf_dtpoff_base (info),
3152 htab->elf.sgot->contents + off);
3153 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3154 bfd_put_32 (output_bfd,
3155 _bfd_x86_elf_dtpoff_base (info) - relocation,
3156 htab->elf.sgot->contents + off);
3157 else if (dr_type != R_386_TLS_DESC)
3158 bfd_put_32 (output_bfd, 0,
3159 htab->elf.sgot->contents + off);
3160 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3162 elf_append_rel (output_bfd, sreloc, &outrel);
3164 if (GOT_TLS_GD_P (tls_type))
3166 if (indx == 0)
3168 BFD_ASSERT (! unresolved_reloc);
3169 bfd_put_32 (output_bfd,
3170 relocation - _bfd_x86_elf_dtpoff_base (info),
3171 htab->elf.sgot->contents + off + 4);
3173 else
3175 bfd_put_32 (output_bfd, 0,
3176 htab->elf.sgot->contents + off + 4);
3177 outrel.r_info = ELF32_R_INFO (indx,
3178 R_386_TLS_DTPOFF32);
3179 outrel.r_offset += 4;
3180 elf_append_rel (output_bfd, sreloc, &outrel);
3183 else if (tls_type == GOT_TLS_IE_BOTH)
3185 bfd_put_32 (output_bfd,
3186 (indx == 0
3187 ? relocation - _bfd_x86_elf_dtpoff_base (info)
3188 : 0),
3189 htab->elf.sgot->contents + off + 4);
3190 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3191 outrel.r_offset += 4;
3192 elf_append_rel (output_bfd, sreloc, &outrel);
3195 dr_done:
3196 if (h != NULL)
3197 h->got.offset |= 1;
3198 else
3199 local_got_offsets[r_symndx] |= 1;
3202 if (off >= (bfd_vma) -2
3203 && ! GOT_TLS_GDESC_P (tls_type))
3204 abort ();
3205 if (r_type_tls == R_386_TLS_GOTDESC
3206 || r_type_tls == R_386_TLS_DESC_CALL)
3208 relocation = htab->sgotplt_jump_table_size + offplt;
3209 unresolved_reloc = false;
3211 else if (r_type_tls == r_type)
3213 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3214 + htab->elf.sgotplt->output_offset;
3215 relocation = htab->elf.sgot->output_section->vma
3216 + htab->elf.sgot->output_offset + off - g_o_t;
3217 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3218 && tls_type == GOT_TLS_IE_BOTH)
3219 relocation += 4;
3220 if (r_type == R_386_TLS_IE)
3221 relocation += g_o_t;
3222 unresolved_reloc = false;
3224 else if (r_type == R_386_TLS_GD)
3226 unsigned int val, type;
3227 bfd_vma roff;
3229 /* GD->IE transition. */
3230 type = *(contents + rel->r_offset - 2);
3231 val = *(contents + rel->r_offset - 1);
3232 if (type == 0x04)
3234 /* Change
3235 leal foo@tlsgd(,%ebx,1), %eax
3236 call ___tls_get_addr@PLT
3237 into:
3238 movl %gs:0, %eax
3239 subl $foo@gottpoff(%ebx), %eax. */
3240 val >>= 3;
3241 roff = rel->r_offset - 3;
3243 else
3245 /* Change
3246 leal foo@tlsgd(%ebx), %eax
3247 call ___tls_get_addr@PLT
3250 leal foo@tlsgd(%reg), %eax
3251 call *___tls_get_addr@GOT(%reg)
3252 which may be converted to
3253 addr32 call ___tls_get_addr
3254 into:
3255 movl %gs:0, %eax;
3256 subl $foo@gottpoff(%reg), %eax. */
3257 roff = rel->r_offset - 2;
3259 memcpy (contents + roff,
3260 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3261 contents[roff + 7] = 0x80 | (val & 7);
3262 /* If foo is used only with foo@gotntpoff(%reg) and
3263 foo@indntpoff, but not with foo@gottpoff(%reg), change
3264 subl $foo@gottpoff(%reg), %eax
3265 into:
3266 addl $foo@gotntpoff(%reg), %eax. */
3267 if (tls_type == GOT_TLS_IE_POS)
3268 contents[roff + 6] = 0x03;
3269 bfd_put_32 (output_bfd,
3270 htab->elf.sgot->output_section->vma
3271 + htab->elf.sgot->output_offset + off
3272 - htab->elf.sgotplt->output_section->vma
3273 - htab->elf.sgotplt->output_offset,
3274 contents + roff + 8);
3275 /* Skip R_386_PLT32 and R_386_GOT32X. */
3276 rel++;
3277 wrel++;
3278 continue;
3280 else if (r_type == R_386_TLS_GOTDESC)
3282 /* GDesc -> IE transition.
3283 It's originally something like:
3284 leal x@tlsdesc(%ebx), %eax
3286 Change it to:
3287 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3289 movl x@gottpoff(%ebx), %eax # before negl %eax
3291 Registers other than %eax may be set up here. */
3293 bfd_vma roff;
3295 /* First, make sure it's a leal adding ebx to a 32-bit
3296 offset into any register, although it's probably
3297 almost always going to be eax. */
3298 roff = rel->r_offset;
3300 /* Now modify the instruction as appropriate. */
3301 /* To turn a leal into a movl in the form we use it, it
3302 suffices to change the first byte from 0x8d to 0x8b.
3303 aoliva FIXME: should we decide to keep the leal, all
3304 we have to do is remove the statement below, and
3305 adjust the relaxation of R_386_TLS_DESC_CALL. */
3306 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3308 if (tls_type == GOT_TLS_IE_BOTH)
3309 off += 4;
3311 bfd_put_32 (output_bfd,
3312 htab->elf.sgot->output_section->vma
3313 + htab->elf.sgot->output_offset + off
3314 - htab->elf.sgotplt->output_section->vma
3315 - htab->elf.sgotplt->output_offset,
3316 contents + roff);
3317 continue;
3319 else if (r_type == R_386_TLS_DESC_CALL)
3321 /* GDesc -> IE transition.
3322 It's originally:
3323 call *(%eax)
3325 Change it to:
3326 xchg %ax,%ax
3328 negl %eax
3329 depending on how we transformed the TLS_GOTDESC above.
3332 bfd_vma roff;
3334 roff = rel->r_offset;
3336 /* Now modify the instruction as appropriate. */
3337 if (tls_type != GOT_TLS_IE_NEG)
3339 /* xchg %ax,%ax */
3340 bfd_put_8 (output_bfd, 0x66, contents + roff);
3341 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3343 else
3345 /* negl %eax */
3346 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3347 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3350 continue;
3352 else
3353 BFD_ASSERT (false);
3354 break;
3356 case R_386_TLS_LDM:
3357 if (! elf_i386_tls_transition (info, input_bfd,
3358 input_section, contents,
3359 symtab_hdr, sym_hashes,
3360 &r_type, GOT_UNKNOWN, rel,
3361 relend, h, sym, true))
3362 return false;
3364 if (r_type != R_386_TLS_LDM)
3366 /* LD->LE transition. Change
3367 leal foo@tlsldm(%ebx) %eax
3368 call ___tls_get_addr@PLT
3369 into:
3370 movl %gs:0, %eax
3372 leal 0(%esi,1), %esi
3373 or change
3374 leal foo@tlsldm(%reg) %eax
3375 call *___tls_get_addr@GOT(%reg)
3376 which may be converted to
3377 addr32 call ___tls_get_addr
3378 into:
3379 movl %gs:0, %eax
3380 leal 0(%esi), %esi */
3381 expected_tls_le = htab->elf.target_os == is_solaris
3382 ? R_386_TLS_LE : R_386_TLS_LE_32;
3383 BFD_ASSERT (r_type == expected_tls_le);
3384 if (*(contents + rel->r_offset + 4) == 0xff
3385 || *(contents + rel->r_offset + 4) == 0x67)
3386 memcpy (contents + rel->r_offset - 2,
3387 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3388 else
3389 memcpy (contents + rel->r_offset - 2,
3390 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3391 /* Skip R_386_PC32/R_386_PLT32. */
3392 rel++;
3393 wrel++;
3394 continue;
3397 if (htab->elf.sgot == NULL)
3398 abort ();
3400 off = htab->tls_ld_or_ldm_got.offset;
3401 if (off & 1)
3402 off &= ~1;
3403 else
3405 Elf_Internal_Rela outrel;
3407 if (htab->elf.srelgot == NULL)
3408 abort ();
3410 outrel.r_offset = (htab->elf.sgot->output_section->vma
3411 + htab->elf.sgot->output_offset + off);
3413 bfd_put_32 (output_bfd, 0,
3414 htab->elf.sgot->contents + off);
3415 bfd_put_32 (output_bfd, 0,
3416 htab->elf.sgot->contents + off + 4);
3417 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3418 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3419 htab->tls_ld_or_ldm_got.offset |= 1;
3421 relocation = htab->elf.sgot->output_section->vma
3422 + htab->elf.sgot->output_offset + off
3423 - htab->elf.sgotplt->output_section->vma
3424 - htab->elf.sgotplt->output_offset;
3425 unresolved_reloc = false;
3426 break;
3428 case R_386_TLS_LDO_32:
3429 if (!bfd_link_executable (info)
3430 || (input_section->flags & SEC_CODE) == 0)
3431 relocation -= _bfd_x86_elf_dtpoff_base (info);
3432 else
3433 /* When converting LDO to LE, we must negate. */
3434 relocation = -elf_i386_tpoff (info, relocation);
3435 break;
3437 case R_386_TLS_LE_32:
3438 case R_386_TLS_LE:
3439 if (!bfd_link_executable (info))
3441 Elf_Internal_Rela outrel;
3442 asection *sreloc;
3444 outrel.r_offset = rel->r_offset
3445 + input_section->output_section->vma
3446 + input_section->output_offset;
3447 if (h != NULL && h->dynindx != -1)
3448 indx = h->dynindx;
3449 else
3450 indx = 0;
3451 if (r_type == R_386_TLS_LE_32)
3452 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3453 else
3454 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3455 sreloc = elf_section_data (input_section)->sreloc;
3456 if (sreloc == NULL)
3457 abort ();
3458 elf_append_rel (output_bfd, sreloc, &outrel);
3459 if (indx)
3460 continue;
3461 else if (r_type == R_386_TLS_LE_32)
3462 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3463 else
3464 relocation -= _bfd_x86_elf_dtpoff_base (info);
3466 else if (r_type == R_386_TLS_LE_32)
3467 relocation = elf_i386_tpoff (info, relocation);
3468 else
3469 relocation = -elf_i386_tpoff (info, relocation);
3470 break;
3472 default:
3473 break;
3476 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3477 because such sections are not SEC_ALLOC and thus ld.so will
3478 not process them. */
3479 if (unresolved_reloc
3480 && !((input_section->flags & SEC_DEBUGGING) != 0
3481 && h->def_dynamic)
3482 && _bfd_elf_section_offset (output_bfd, info, input_section,
3483 rel->r_offset) != (bfd_vma) -1)
3485 _bfd_error_handler
3486 /* xgettext:c-format */
3487 (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
3488 input_bfd,
3489 input_section,
3490 (uint64_t) rel->r_offset,
3491 howto->name,
3492 h->root.root.string);
3493 return false;
3496 do_relocation:
3497 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3498 contents, rel->r_offset,
3499 relocation, 0);
3501 check_relocation_error:
3502 if (r != bfd_reloc_ok)
3504 const char *name;
3506 if (h != NULL)
3507 name = h->root.root.string;
3508 else
3510 name = bfd_elf_string_from_elf_section (input_bfd,
3511 symtab_hdr->sh_link,
3512 sym->st_name);
3513 if (name == NULL)
3514 return false;
3515 if (*name == '\0')
3516 name = bfd_section_name (sec);
3519 if (r == bfd_reloc_overflow)
3520 (*info->callbacks->reloc_overflow)
3521 (info, (h ? &h->root : NULL), name, howto->name,
3522 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3523 else
3525 _bfd_error_handler
3526 /* xgettext:c-format */
3527 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3528 input_bfd, input_section,
3529 (uint64_t) rel->r_offset, name, (int) r);
3530 return false;
3534 if (wrel != rel)
3535 *wrel = *rel;
3538 if (wrel != rel)
3540 Elf_Internal_Shdr *rel_hdr;
3541 size_t deleted = rel - wrel;
3543 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3544 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3545 if (rel_hdr->sh_size == 0)
3547 /* It is too late to remove an empty reloc section. Leave
3548 one NONE reloc.
3549 ??? What is wrong with an empty section??? */
3550 rel_hdr->sh_size = rel_hdr->sh_entsize;
3551 deleted -= 1;
3553 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3554 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3555 input_section->reloc_count -= deleted;
3558 return true;
3561 /* Finish up dynamic symbol handling. We set the contents of various
3562 dynamic sections here. */
3564 static bool
3565 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3566 struct bfd_link_info *info,
3567 struct elf_link_hash_entry *h,
3568 Elf_Internal_Sym *sym)
3570 struct elf_x86_link_hash_table *htab;
3571 unsigned plt_entry_size;
3572 struct elf_x86_link_hash_entry *eh;
3573 bool local_undefweak;
3574 bool use_plt_second;
3576 htab = elf_x86_hash_table (info, I386_ELF_DATA);
3578 plt_entry_size = htab->plt.plt_entry_size;
3580 /* Use the second PLT section only if there is .plt section. */
3581 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3583 eh = (struct elf_x86_link_hash_entry *) h;
3584 if (eh->no_finish_dynamic_symbol)
3585 abort ();
3587 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3588 resolved undefined weak symbols in executable so that their
3589 references have value 0 at run-time. */
3590 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3592 if (h->plt.offset != (bfd_vma) -1)
3594 bfd_vma plt_index, plt_offset;
3595 bfd_vma got_offset;
3596 Elf_Internal_Rela rel;
3597 bfd_byte *loc;
3598 asection *plt, *resolved_plt, *gotplt, *relplt;
3600 /* When building a static executable, use .iplt, .igot.plt and
3601 .rel.iplt sections for STT_GNU_IFUNC symbols. */
3602 if (htab->elf.splt != NULL)
3604 plt = htab->elf.splt;
3605 gotplt = htab->elf.sgotplt;
3606 relplt = htab->elf.srelplt;
3608 else
3610 plt = htab->elf.iplt;
3611 gotplt = htab->elf.igotplt;
3612 relplt = htab->elf.irelplt;
3615 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3617 /* Get the index in the procedure linkage table which
3618 corresponds to this symbol. This is the index of this symbol
3619 in all the symbols for which we are making plt entries. The
3620 first entry in the procedure linkage table is reserved.
3622 Get the offset into the .got table of the entry that
3623 corresponds to this function. Each .got entry is 4 bytes.
3624 The first three are reserved.
3626 For static executables, we don't reserve anything. */
3628 if (plt == htab->elf.splt)
3630 got_offset = (h->plt.offset / plt_entry_size
3631 - htab->plt.has_plt0);
3632 got_offset = (got_offset + 3) * 4;
3634 else
3636 got_offset = h->plt.offset / plt_entry_size;
3637 got_offset = got_offset * 4;
3640 /* Fill in the entry in the procedure linkage table and update
3641 the first slot. */
3642 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3643 plt_entry_size);
3645 if (use_plt_second)
3647 const bfd_byte *plt_entry;
3648 if (bfd_link_pic (info))
3649 plt_entry = htab->non_lazy_plt->pic_plt_entry;
3650 else
3651 plt_entry = htab->non_lazy_plt->plt_entry;
3652 memcpy (htab->plt_second->contents + eh->plt_second.offset,
3653 plt_entry, htab->non_lazy_plt->plt_entry_size);
3655 resolved_plt = htab->plt_second;
3656 plt_offset = eh->plt_second.offset;
3658 else
3660 resolved_plt = plt;
3661 plt_offset = h->plt.offset;
3664 if (! bfd_link_pic (info))
3666 bfd_put_32 (output_bfd,
3667 (gotplt->output_section->vma
3668 + gotplt->output_offset
3669 + got_offset),
3670 resolved_plt->contents + plt_offset
3671 + htab->plt.plt_got_offset);
3673 if (htab->elf.target_os == is_vxworks)
3675 int s, k, reloc_index;
3677 /* Create the R_386_32 relocation referencing the GOT
3678 for this PLT entry. */
3680 /* S: Current slot number (zero-based). */
3681 s = ((h->plt.offset - htab->plt.plt_entry_size)
3682 / htab->plt.plt_entry_size);
3683 /* K: Number of relocations for PLTResolve. */
3684 if (bfd_link_pic (info))
3685 k = PLTRESOLVE_RELOCS_SHLIB;
3686 else
3687 k = PLTRESOLVE_RELOCS;
3688 /* Skip the PLTresolve relocations, and the relocations for
3689 the other PLT slots. */
3690 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3691 loc = (htab->srelplt2->contents + reloc_index
3692 * sizeof (Elf32_External_Rel));
3694 rel.r_offset = (plt->output_section->vma
3695 + plt->output_offset
3696 + h->plt.offset + 2),
3697 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3698 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3700 /* Create the R_386_32 relocation referencing the beginning of
3701 the PLT for this GOT entry. */
3702 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3703 + htab->elf.sgotplt->output_offset
3704 + got_offset);
3705 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3706 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3707 loc + sizeof (Elf32_External_Rel));
3710 else
3712 bfd_put_32 (output_bfd, got_offset,
3713 resolved_plt->contents + plt_offset
3714 + htab->plt.plt_got_offset);
3717 /* Fill in the entry in the global offset table. Leave the entry
3718 as zero for undefined weak symbol in PIE. No PLT relocation
3719 against undefined weak symbol in PIE. */
3720 if (!local_undefweak)
3722 if (htab->plt.has_plt0)
3723 bfd_put_32 (output_bfd,
3724 (plt->output_section->vma
3725 + plt->output_offset
3726 + h->plt.offset
3727 + htab->lazy_plt->plt_lazy_offset),
3728 gotplt->contents + got_offset);
3730 /* Fill in the entry in the .rel.plt section. */
3731 rel.r_offset = (gotplt->output_section->vma
3732 + gotplt->output_offset
3733 + got_offset);
3734 if (PLT_LOCAL_IFUNC_P (info, h))
3736 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3737 h->root.root.string,
3738 h->root.u.def.section->owner);
3740 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3741 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3742 in the .got.plt section. */
3743 bfd_put_32 (output_bfd,
3744 (h->root.u.def.value
3745 + h->root.u.def.section->output_section->vma
3746 + h->root.u.def.section->output_offset),
3747 gotplt->contents + got_offset);
3748 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3750 if (htab->params->report_relative_reloc)
3751 _bfd_x86_elf_link_report_relative_reloc
3752 (info, relplt, h, sym, "R_386_IRELATIVE", &rel);
3754 /* R_386_IRELATIVE comes last. */
3755 plt_index = htab->next_irelative_index--;
3757 else
3759 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3760 plt_index = htab->next_jump_slot_index++;
3763 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3764 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3766 /* Don't fill the second and third slots in PLT entry for
3767 static executables nor without PLT0. */
3768 if (plt == htab->elf.splt && htab->plt.has_plt0)
3770 bfd_put_32 (output_bfd,
3771 plt_index * sizeof (Elf32_External_Rel),
3772 plt->contents + h->plt.offset
3773 + htab->lazy_plt->plt_reloc_offset);
3774 bfd_put_32 (output_bfd,
3775 - (h->plt.offset
3776 + htab->lazy_plt->plt_plt_offset + 4),
3777 (plt->contents + h->plt.offset
3778 + htab->lazy_plt->plt_plt_offset));
3782 else if (eh->plt_got.offset != (bfd_vma) -1)
3784 bfd_vma got_offset, plt_offset;
3785 asection *plt, *got, *gotplt;
3786 const bfd_byte *got_plt_entry;
3788 /* Set the entry in the GOT procedure linkage table. */
3789 plt = htab->plt_got;
3790 got = htab->elf.sgot;
3791 gotplt = htab->elf.sgotplt;
3792 got_offset = h->got.offset;
3794 if (got_offset == (bfd_vma) -1
3795 || plt == NULL
3796 || got == NULL
3797 || gotplt == NULL)
3798 abort ();
3800 /* Fill in the entry in the GOT procedure linkage table. */
3801 if (! bfd_link_pic (info))
3803 got_plt_entry = htab->non_lazy_plt->plt_entry;
3804 got_offset += got->output_section->vma + got->output_offset;
3806 else
3808 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3809 got_offset += (got->output_section->vma
3810 + got->output_offset
3811 - gotplt->output_section->vma
3812 - gotplt->output_offset);
3815 plt_offset = eh->plt_got.offset;
3816 memcpy (plt->contents + plt_offset, got_plt_entry,
3817 htab->non_lazy_plt->plt_entry_size);
3818 bfd_put_32 (output_bfd, got_offset,
3819 (plt->contents + plt_offset
3820 + htab->non_lazy_plt->plt_got_offset));
3823 if (!local_undefweak
3824 && !h->def_regular
3825 && (h->plt.offset != (bfd_vma) -1
3826 || eh->plt_got.offset != (bfd_vma) -1))
3828 /* Mark the symbol as undefined, rather than as defined in
3829 the .plt section. Leave the value if there were any
3830 relocations where pointer equality matters (this is a clue
3831 for the dynamic linker, to make function pointer
3832 comparisons work between an application and shared
3833 library), otherwise set it to zero. If a function is only
3834 called from a binary, there is no need to slow down
3835 shared libraries because of that. */
3836 sym->st_shndx = SHN_UNDEF;
3837 if (!h->pointer_equality_needed)
3838 sym->st_value = 0;
3841 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3843 /* Don't generate dynamic GOT relocation against undefined weak
3844 symbol in executable. */
3845 if (h->got.offset != (bfd_vma) -1
3846 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3847 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3848 && !local_undefweak)
3850 Elf_Internal_Rela rel;
3851 asection *relgot = htab->elf.srelgot;
3852 const char *relative_reloc_name = NULL;
3853 bool generate_dynamic_reloc = true;
3855 /* This symbol has an entry in the global offset table. Set it
3856 up. */
3858 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3859 abort ();
3861 rel.r_offset = (htab->elf.sgot->output_section->vma
3862 + htab->elf.sgot->output_offset
3863 + (h->got.offset & ~(bfd_vma) 1));
3865 /* If this is a static link, or it is a -Bsymbolic link and the
3866 symbol is defined locally or was forced to be local because
3867 of a version file, we just want to emit a RELATIVE reloc.
3868 The entry in the global offset table will already have been
3869 initialized in the relocate_section function. */
3870 if (h->def_regular
3871 && h->type == STT_GNU_IFUNC)
3873 if (h->plt.offset == (bfd_vma) -1)
3875 /* STT_GNU_IFUNC is referenced without PLT. */
3876 if (htab->elf.splt == NULL)
3878 /* use .rel[a].iplt section to store .got relocations
3879 in static executable. */
3880 relgot = htab->elf.irelplt;
3882 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3884 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3885 h->root.root.string,
3886 h->root.u.def.section->owner);
3888 bfd_put_32 (output_bfd,
3889 (h->root.u.def.value
3890 + h->root.u.def.section->output_section->vma
3891 + h->root.u.def.section->output_offset),
3892 htab->elf.sgot->contents + h->got.offset);
3893 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3894 relative_reloc_name = "R_386_IRELATIVE";
3896 else
3897 goto do_glob_dat;
3899 else if (bfd_link_pic (info))
3901 /* Generate R_386_GLOB_DAT. */
3902 goto do_glob_dat;
3904 else
3906 asection *plt;
3907 bfd_vma plt_offset;
3909 if (!h->pointer_equality_needed)
3910 abort ();
3912 /* For non-shared object, we can't use .got.plt, which
3913 contains the real function addres if we need pointer
3914 equality. We load the GOT entry with the PLT entry. */
3915 if (htab->plt_second != NULL)
3917 plt = htab->plt_second;
3918 plt_offset = eh->plt_second.offset;
3920 else
3922 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3923 plt_offset = h->plt.offset;
3925 bfd_put_32 (output_bfd,
3926 (plt->output_section->vma
3927 + plt->output_offset + plt_offset),
3928 htab->elf.sgot->contents + h->got.offset);
3929 return true;
3932 else if (bfd_link_pic (info)
3933 && SYMBOL_REFERENCES_LOCAL_P (info, h))
3935 BFD_ASSERT((h->got.offset & 1) != 0);
3936 if (info->enable_dt_relr)
3937 generate_dynamic_reloc = false;
3938 else
3940 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3941 relative_reloc_name = "R_386_RELATIVE";
3944 else
3946 BFD_ASSERT((h->got.offset & 1) == 0);
3947 do_glob_dat:
3948 bfd_put_32 (output_bfd, (bfd_vma) 0,
3949 htab->elf.sgot->contents + h->got.offset);
3950 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3953 if (generate_dynamic_reloc)
3955 if (relative_reloc_name != NULL
3956 && htab->params->report_relative_reloc)
3957 _bfd_x86_elf_link_report_relative_reloc
3958 (info, relgot, h, sym, relative_reloc_name, &rel);
3960 elf_append_rel (output_bfd, relgot, &rel);
3964 if (h->needs_copy)
3966 Elf_Internal_Rela rel;
3967 asection *s;
3969 /* This symbol needs a copy reloc. Set it up. */
3970 VERIFY_COPY_RELOC (h, htab)
3972 rel.r_offset = (h->root.u.def.value
3973 + h->root.u.def.section->output_section->vma
3974 + h->root.u.def.section->output_offset);
3975 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3976 if (h->root.u.def.section == htab->elf.sdynrelro)
3977 s = htab->elf.sreldynrelro;
3978 else
3979 s = htab->elf.srelbss;
3980 elf_append_rel (output_bfd, s, &rel);
3983 return true;
3986 /* Finish up local dynamic symbol handling. We set the contents of
3987 various dynamic sections here. */
3989 static int
3990 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
3992 struct elf_link_hash_entry *h
3993 = (struct elf_link_hash_entry *) *slot;
3994 struct bfd_link_info *info
3995 = (struct bfd_link_info *) inf;
3997 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
3998 h, NULL);
4001 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4002 here since undefined weak symbol may not be dynamic and may not be
4003 called for elf_i386_finish_dynamic_symbol. */
4005 static bool
4006 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4007 void *inf)
4009 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4010 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4012 if (h->root.type != bfd_link_hash_undefweak
4013 || h->dynindx != -1)
4014 return true;
4016 return elf_i386_finish_dynamic_symbol (info->output_bfd,
4017 info, h, NULL);
4020 /* Used to decide how to sort relocs in an optimal manner for the
4021 dynamic linker, before writing them out. */
4023 static enum elf_reloc_type_class
4024 elf_i386_reloc_type_class (const struct bfd_link_info *info,
4025 const asection *rel_sec ATTRIBUTE_UNUSED,
4026 const Elf_Internal_Rela *rela)
4028 bfd *abfd = info->output_bfd;
4029 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4030 struct elf_link_hash_table *htab = elf_hash_table (info);
4032 if (htab->dynsym != NULL
4033 && htab->dynsym->contents != NULL)
4035 /* Check relocation against STT_GNU_IFUNC symbol if there are
4036 dynamic symbols. */
4037 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4038 if (r_symndx != STN_UNDEF)
4040 Elf_Internal_Sym sym;
4041 if (!bed->s->swap_symbol_in (abfd,
4042 (htab->dynsym->contents
4043 + r_symndx * sizeof (Elf32_External_Sym)),
4044 0, &sym))
4045 abort ();
4047 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4048 return reloc_class_ifunc;
4052 switch (ELF32_R_TYPE (rela->r_info))
4054 case R_386_IRELATIVE:
4055 return reloc_class_ifunc;
4056 case R_386_RELATIVE:
4057 return reloc_class_relative;
4058 case R_386_JUMP_SLOT:
4059 return reloc_class_plt;
4060 case R_386_COPY:
4061 return reloc_class_copy;
4062 default:
4063 return reloc_class_normal;
4067 /* Finish up the dynamic sections. */
4069 static bool
4070 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4071 struct bfd_link_info *info)
4073 struct elf_x86_link_hash_table *htab;
4075 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4076 if (htab == NULL)
4077 return false;
4079 if (!htab->elf.dynamic_sections_created)
4080 return true;
4082 if (htab->elf.splt && htab->elf.splt->size > 0)
4084 if (bfd_is_abs_section (htab->elf.splt->output_section))
4086 info->callbacks->einfo
4087 (_("%F%P: discarded output section: `%pA'\n"),
4088 htab->elf.splt);
4089 return false;
4092 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4093 really seem like the right value. */
4094 elf_section_data (htab->elf.splt->output_section)
4095 ->this_hdr.sh_entsize = 4;
4097 if (htab->plt.has_plt0)
4099 /* Fill in the special first entry in the procedure linkage
4100 table. */
4101 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4102 htab->lazy_plt->plt0_entry_size);
4103 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4104 htab->plt0_pad_byte,
4105 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4106 if (!bfd_link_pic (info))
4108 bfd_put_32 (output_bfd,
4109 (htab->elf.sgotplt->output_section->vma
4110 + htab->elf.sgotplt->output_offset
4111 + 4),
4112 htab->elf.splt->contents
4113 + htab->lazy_plt->plt0_got1_offset);
4114 bfd_put_32 (output_bfd,
4115 (htab->elf.sgotplt->output_section->vma
4116 + htab->elf.sgotplt->output_offset
4117 + 8),
4118 htab->elf.splt->contents
4119 + htab->lazy_plt->plt0_got2_offset);
4121 if (htab->elf.target_os == is_vxworks)
4123 Elf_Internal_Rela rel;
4124 int num_plts = (htab->elf.splt->size
4125 / htab->plt.plt_entry_size) - 1;
4126 unsigned char *p;
4127 asection *srelplt2 = htab->srelplt2;
4129 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4130 + 4. On IA32 we use REL relocations so the
4131 addend goes in the PLT directly. */
4132 rel.r_offset = (htab->elf.splt->output_section->vma
4133 + htab->elf.splt->output_offset
4134 + htab->lazy_plt->plt0_got1_offset);
4135 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4136 R_386_32);
4137 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4138 srelplt2->contents);
4139 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4140 + 8. */
4141 rel.r_offset = (htab->elf.splt->output_section->vma
4142 + htab->elf.splt->output_offset
4143 + htab->lazy_plt->plt0_got2_offset);
4144 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4145 R_386_32);
4146 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4147 srelplt2->contents +
4148 sizeof (Elf32_External_Rel));
4149 /* Correct the .rel.plt.unloaded relocations. */
4150 p = srelplt2->contents;
4151 if (bfd_link_pic (info))
4152 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4153 else
4154 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4156 for (; num_plts; num_plts--)
4158 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4159 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4160 R_386_32);
4161 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4162 p += sizeof (Elf32_External_Rel);
4164 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4165 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4166 R_386_32);
4167 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4168 p += sizeof (Elf32_External_Rel);
4175 /* Fill PLT entries for undefined weak symbols in PIE. */
4176 if (bfd_link_pie (info))
4177 bfd_hash_traverse (&info->hash->table,
4178 elf_i386_pie_finish_undefweak_symbol,
4179 info);
4181 return true;
4184 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4185 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4186 It has to be done before elf_link_sort_relocs is called so that
4187 dynamic relocations are properly sorted. */
4189 static bool
4190 elf_i386_output_arch_local_syms
4191 (bfd *output_bfd ATTRIBUTE_UNUSED,
4192 struct bfd_link_info *info,
4193 void *flaginfo ATTRIBUTE_UNUSED,
4194 int (*func) (void *, const char *,
4195 Elf_Internal_Sym *,
4196 asection *,
4197 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4199 struct elf_x86_link_hash_table *htab
4200 = elf_x86_hash_table (info, I386_ELF_DATA);
4201 if (htab == NULL)
4202 return false;
4204 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4205 htab_traverse (htab->loc_hash_table,
4206 elf_i386_finish_local_dynamic_symbol,
4207 info);
4209 return true;
4212 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4213 dynamic relocations. */
4215 static long
4216 elf_i386_get_synthetic_symtab (bfd *abfd,
4217 long symcount ATTRIBUTE_UNUSED,
4218 asymbol **syms ATTRIBUTE_UNUSED,
4219 long dynsymcount,
4220 asymbol **dynsyms,
4221 asymbol **ret)
4223 long count, i, n;
4224 int j;
4225 bfd_byte *plt_contents;
4226 long relsize;
4227 const struct elf_x86_lazy_plt_layout *lazy_plt;
4228 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4229 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4230 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4231 asection *plt;
4232 bfd_vma got_addr;
4233 enum elf_x86_plt_type plt_type;
4234 struct elf_x86_plt plts[] =
4236 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4237 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4238 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4239 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4242 *ret = NULL;
4244 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4245 return 0;
4247 if (dynsymcount <= 0)
4248 return 0;
4250 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4251 if (relsize <= 0)
4252 return -1;
4254 non_lazy_plt = NULL;
4255 /* Silence GCC 6. */
4256 lazy_plt = NULL;
4257 non_lazy_ibt_plt = NULL;
4258 lazy_ibt_plt = NULL;
4259 switch (get_elf_backend_data (abfd)->target_os)
4261 case is_normal:
4262 case is_solaris:
4263 non_lazy_plt = &elf_i386_non_lazy_plt;
4264 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4265 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4266 /* Fall through */
4267 case is_vxworks:
4268 lazy_plt = &elf_i386_lazy_plt;
4269 break;
4270 default:
4271 abort ();
4274 got_addr = 0;
4276 count = 0;
4277 for (j = 0; plts[j].name != NULL; j++)
4279 plt = bfd_get_section_by_name (abfd, plts[j].name);
4280 if (plt == NULL || plt->size == 0)
4281 continue;
4283 /* Get the PLT section contents. */
4284 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4285 if (plt_contents == NULL)
4286 break;
4287 if (!bfd_get_section_contents (abfd, (asection *) plt,
4288 plt_contents, 0, plt->size))
4290 free (plt_contents);
4291 break;
4294 /* Check what kind of PLT it is. */
4295 plt_type = plt_unknown;
4296 if (plts[j].type == plt_unknown
4297 && (plt->size >= (lazy_plt->plt0_entry_size
4298 + lazy_plt->plt_entry_size)))
4300 /* Match lazy PLT first. */
4301 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4302 lazy_plt->plt0_got1_offset) == 0)
4304 /* The fist entry in the lazy IBT PLT is the same as the
4305 normal lazy PLT. */
4306 if (lazy_ibt_plt != NULL
4307 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4308 lazy_ibt_plt->plt_entry,
4309 lazy_ibt_plt->plt_got_offset) == 0))
4310 plt_type = plt_lazy | plt_second;
4311 else
4312 plt_type = plt_lazy;
4314 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4315 lazy_plt->plt0_got1_offset) == 0)
4317 /* The fist entry in the PIC lazy IBT PLT is the same as
4318 the normal PIC lazy PLT. */
4319 if (lazy_ibt_plt != NULL
4320 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4321 lazy_ibt_plt->pic_plt_entry,
4322 lazy_ibt_plt->plt_got_offset) == 0))
4323 plt_type = plt_lazy | plt_pic | plt_second;
4324 else
4325 plt_type = plt_lazy | plt_pic;
4329 if (non_lazy_plt != NULL
4330 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4331 && plt->size >= non_lazy_plt->plt_entry_size)
4333 /* Match non-lazy PLT. */
4334 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4335 non_lazy_plt->plt_got_offset) == 0)
4336 plt_type = plt_non_lazy;
4337 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4338 non_lazy_plt->plt_got_offset) == 0)
4339 plt_type = plt_pic;
4342 if ((non_lazy_ibt_plt != NULL)
4343 && (plt_type == plt_unknown || plt_type == plt_second)
4344 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4346 if (memcmp (plt_contents,
4347 non_lazy_ibt_plt->plt_entry,
4348 non_lazy_ibt_plt->plt_got_offset) == 0)
4350 /* Match IBT PLT. */
4351 plt_type = plt_second;
4352 non_lazy_plt = non_lazy_ibt_plt;
4354 else if (memcmp (plt_contents,
4355 non_lazy_ibt_plt->pic_plt_entry,
4356 non_lazy_ibt_plt->plt_got_offset) == 0)
4358 /* Match PIC IBT PLT. */
4359 plt_type = plt_second | plt_pic;
4360 non_lazy_plt = non_lazy_ibt_plt;
4364 if (plt_type == plt_unknown)
4366 free (plt_contents);
4367 continue;
4370 plts[j].sec = plt;
4371 plts[j].type = plt_type;
4373 if ((plt_type & plt_lazy))
4375 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4376 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4377 /* Skip PLT0 in lazy PLT. */
4378 i = 1;
4380 else
4382 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4383 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4384 i = 0;
4387 /* Skip lazy PLT when the second PLT is used. */
4388 if ((plt_type & (plt_lazy | plt_second))
4389 == (plt_lazy | plt_second))
4390 plts[j].count = 0;
4391 else
4393 n = plt->size / plts[j].plt_entry_size;
4394 plts[j].count = n;
4395 count += n - i;
4398 plts[j].contents = plt_contents;
4400 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4401 if ((plt_type & plt_pic))
4402 got_addr = (bfd_vma) -1;
4405 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4406 got_addr, plts, dynsyms,
4407 ret);
4410 /* Set up i386 GNU properties. Return the first relocatable ELF input
4411 with GNU properties if found. Otherwise, return NULL. */
4413 static bfd *
4414 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4416 struct elf_x86_init_table init_table;
4418 switch (get_elf_backend_data (info->output_bfd)->target_os)
4420 case is_normal:
4421 case is_solaris:
4422 init_table.plt0_pad_byte = 0x0;
4423 init_table.lazy_plt = &elf_i386_lazy_plt;
4424 init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4425 init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4426 init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4427 break;
4428 case is_vxworks:
4429 init_table.plt0_pad_byte = 0x90;
4430 init_table.lazy_plt = &elf_i386_lazy_plt;
4431 init_table.non_lazy_plt = NULL;
4432 init_table.lazy_ibt_plt = NULL;
4433 init_table.non_lazy_ibt_plt = NULL;
4434 break;
4435 default:
4436 abort ();
4439 init_table.r_info = elf32_r_info;
4440 init_table.r_sym = elf32_r_sym;
4442 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4445 #define TARGET_LITTLE_SYM i386_elf32_vec
4446 #define TARGET_LITTLE_NAME "elf32-i386"
4447 #define ELF_ARCH bfd_arch_i386
4448 #define ELF_TARGET_ID I386_ELF_DATA
4449 #define ELF_MACHINE_CODE EM_386
4450 #define ELF_MAXPAGESIZE 0x1000
4452 #define elf_backend_can_gc_sections 1
4453 #define elf_backend_can_refcount 1
4454 #define elf_backend_want_got_plt 1
4455 #define elf_backend_plt_readonly 1
4456 #define elf_backend_want_plt_sym 0
4457 #define elf_backend_got_header_size 12
4458 #define elf_backend_plt_alignment 4
4459 #define elf_backend_dtrel_excludes_plt 1
4460 #define elf_backend_caches_rawsize 1
4461 #define elf_backend_want_dynrelro 1
4463 /* Support RELA for objdump of prelink objects. */
4464 #define elf_info_to_howto elf_i386_info_to_howto_rel
4465 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4467 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4468 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4469 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4470 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
4472 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4473 #define elf_backend_early_size_sections elf_i386_early_size_sections
4474 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
4475 #define elf_backend_fake_sections elf_i386_fake_sections
4476 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4477 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4478 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
4479 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4480 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4481 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4482 #define elf_backend_relocate_section elf_i386_relocate_section
4483 #define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
4484 #define elf_backend_hide_symbol _bfd_x86_elf_hide_symbol
4486 #define elf_backend_linux_prpsinfo32_ugid16 true
4488 #define elf32_bed elf32_i386_bed
4490 #include "elf32-target.h"
4492 /* FreeBSD support. */
4494 #undef TARGET_LITTLE_SYM
4495 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
4496 #undef TARGET_LITTLE_NAME
4497 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4498 #undef ELF_OSABI
4499 #define ELF_OSABI ELFOSABI_FREEBSD
4501 /* The kernel recognizes executables as valid only if they carry a
4502 "FreeBSD" label in the ELF header. So we put this label on all
4503 executables and (for simplicity) also all other object files. */
4505 static bool
4506 elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
4508 if (!_bfd_elf_init_file_header (abfd, info))
4509 return false;
4511 #ifdef OLD_FREEBSD_ABI_LABEL
4513 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4514 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4515 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4517 #endif
4518 return true;
4521 #undef elf_backend_init_file_header
4522 #define elf_backend_init_file_header elf_i386_fbsd_init_file_header
4523 #undef elf32_bed
4524 #define elf32_bed elf32_i386_fbsd_bed
4526 #undef elf_backend_add_symbol_hook
4528 #include "elf32-target.h"
4530 #undef elf_backend_init_file_header
4532 /* Solaris 2. */
4534 #undef TARGET_LITTLE_SYM
4535 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
4536 #undef TARGET_LITTLE_NAME
4537 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
4539 #undef ELF_TARGET_OS
4540 #define ELF_TARGET_OS is_solaris
4542 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4543 objects won't be recognized. */
4544 #undef ELF_OSABI
4546 #undef elf32_bed
4547 #define elf32_bed elf32_i386_sol2_bed
4549 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4550 boundary. */
4551 #undef elf_backend_static_tls_alignment
4552 #define elf_backend_static_tls_alignment 8
4554 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4556 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4557 File, p.63. */
4558 #undef elf_backend_want_plt_sym
4559 #define elf_backend_want_plt_sym 1
4561 #undef elf_backend_strtab_flags
4562 #define elf_backend_strtab_flags SHF_STRINGS
4564 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4565 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
4566 FALSE otherwise. ISECTION is the best guess matching section from the
4567 input bfd IBFD, but it might be NULL. */
4569 static bool
4570 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4571 bfd *obfd ATTRIBUTE_UNUSED,
4572 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4573 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4575 /* PR 19938: FIXME: Need to add code for setting the sh_info
4576 and sh_link fields of Solaris specific section types. */
4577 return false;
4579 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4580 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4582 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4584 The following values should be set:
4586 Type Link Info
4587 -----------------------------------------------------------------------------
4588 SHT_SUNW_ancillary The section header index of 0
4589 [0x6fffffee] the associated string table.
4591 SHT_SUNW_capinfo The section header index of For a dynamic object, the
4592 [0x6ffffff0] the associated symbol table. section header index of
4593 the associated
4594 SHT_SUNW_capchain table,
4595 otherwise 0.
4597 SHT_SUNW_symsort The section header index of 0
4598 [0x6ffffff1] the associated symbol table.
4600 SHT_SUNW_tlssort The section header index of 0
4601 [0x6ffffff2] the associated symbol table.
4603 SHT_SUNW_LDYNSYM The section header index of One greater than the
4604 [0x6ffffff3] the associated string table. symbol table index of the
4605 This index is the same string last local symbol,
4606 table used by the SHT_DYNSYM STB_LOCAL. Since
4607 section. SHT_SUNW_LDYNSYM only
4608 contains local symbols,
4609 sh_info is equivalent to
4610 the number of symbols in
4611 the table.
4613 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4614 [0x6ffffff5] the section header index of to named strings, the
4615 the associated section header index of
4616 SHT_SUNW_capinfo table, the associated string
4617 otherwise 0. table, otherwise 0.
4619 SHT_SUNW_move The section header index of 0
4620 [0x6ffffffa] the associated symbol table.
4622 SHT_SUNW_COMDAT 0 0
4623 [0x6ffffffb]
4625 SHT_SUNW_syminfo The section header index of The section header index
4626 [0x6ffffffc] the associated symbol table. of the associated
4627 .dynamic section.
4629 SHT_SUNW_verdef The section header index of The number of version
4630 [0x6ffffffd] the associated string table. definitions within the
4631 section.
4633 SHT_SUNW_verneed The section header index of The number of version
4634 [0x6ffffffe] the associated string table. dependencies within the
4635 section.
4637 SHT_SUNW_versym The section header index of 0
4638 [0x6fffffff] the associated symbol table. */
4641 #undef elf_backend_copy_special_section_fields
4642 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4644 #include "elf32-target.h"
4646 /* Intel MCU support. */
4648 static bool
4649 elf32_iamcu_elf_object_p (bfd *abfd)
4651 /* Set the right machine number for an IAMCU elf32 file. */
4652 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4653 return true;
4656 #undef TARGET_LITTLE_SYM
4657 #define TARGET_LITTLE_SYM iamcu_elf32_vec
4658 #undef TARGET_LITTLE_NAME
4659 #define TARGET_LITTLE_NAME "elf32-iamcu"
4660 #undef ELF_ARCH
4661 #define ELF_ARCH bfd_arch_iamcu
4663 #undef ELF_MACHINE_CODE
4664 #define ELF_MACHINE_CODE EM_IAMCU
4666 #undef ELF_TARGET_OS
4667 #undef ELF_OSABI
4669 #undef elf32_bed
4670 #define elf32_bed elf32_iamcu_bed
4672 #undef elf_backend_object_p
4673 #define elf_backend_object_p elf32_iamcu_elf_object_p
4675 #undef elf_backend_static_tls_alignment
4677 #undef elf_backend_want_plt_sym
4678 #define elf_backend_want_plt_sym 0
4680 #undef elf_backend_strtab_flags
4681 #undef elf_backend_copy_special_section_fields
4683 #include "elf32-target.h"
4685 /* Restore defaults. */
4686 #undef ELF_ARCH
4687 #define ELF_ARCH bfd_arch_i386
4688 #undef ELF_MACHINE_CODE
4689 #define ELF_MACHINE_CODE EM_386
4690 #undef elf_backend_object_p
4692 /* VxWorks support. */
4694 #undef TARGET_LITTLE_SYM
4695 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
4696 #undef TARGET_LITTLE_NAME
4697 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4698 #undef ELF_OSABI
4699 #undef ELF_MAXPAGESIZE
4700 #define ELF_MAXPAGESIZE 0x1000
4701 #undef elf_backend_plt_alignment
4702 #define elf_backend_plt_alignment 4
4704 #undef ELF_TARGET_OS
4705 #define ELF_TARGET_OS is_vxworks
4707 #undef elf_backend_relocs_compatible
4708 #undef elf_backend_add_symbol_hook
4709 #define elf_backend_add_symbol_hook \
4710 elf_vxworks_add_symbol_hook
4711 #undef elf_backend_link_output_symbol_hook
4712 #define elf_backend_link_output_symbol_hook \
4713 elf_vxworks_link_output_symbol_hook
4714 #undef elf_backend_emit_relocs
4715 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4716 #undef elf_backend_final_write_processing
4717 #define elf_backend_final_write_processing \
4718 elf_vxworks_final_write_processing
4719 #undef elf_backend_static_tls_alignment
4721 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4722 define it. */
4723 #undef elf_backend_want_plt_sym
4724 #define elf_backend_want_plt_sym 1
4726 #undef elf32_bed
4727 #define elf32_bed elf32_i386_vxworks_bed
4729 #include "elf32-target.h"