Support x86 Intel MSR_IMM
[binutils-gdb.git] / bfd / elf32-i386.c
blob2e8d59518aca123c17cec8c792f008728ee3a3cd
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, %eax
979 movl foo@indntpoff, %reg
980 addl foo@indntpoff, %reg
983 if (offset < 1 || (offset + 4) > sec->size)
984 return elf_x86_tls_error_yes;
986 /* Check "movl foo@indntpoff, %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@indntpoff, %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 /* It has been checked in elf_i386_tls_transition. */
1043 return elf_x86_tls_error_none;
1045 default:
1046 abort ();
1050 /* Return TRUE if the TLS access transition is OK or no transition
1051 will be performed. Update R_TYPE if there is a transition. */
1053 static bool
1054 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1055 asection *sec, bfd_byte *contents,
1056 Elf_Internal_Shdr *symtab_hdr,
1057 struct elf_link_hash_entry **sym_hashes,
1058 unsigned int *r_type, int tls_type,
1059 const Elf_Internal_Rela *rel,
1060 const Elf_Internal_Rela *relend,
1061 struct elf_link_hash_entry *h,
1062 Elf_Internal_Sym *sym,
1063 bool from_relocate_section)
1065 unsigned int from_type = *r_type;
1066 unsigned int to_type = from_type;
1067 bool check = true;
1068 unsigned int to_le_type, to_ie_type;
1069 bfd_vma offset;
1070 bfd_byte *call;
1072 /* Skip TLS transition for functions. */
1073 if (h != NULL
1074 && (h->type == STT_FUNC
1075 || h->type == STT_GNU_IFUNC))
1076 return true;
1078 if (get_elf_backend_data (abfd)->target_os == is_solaris)
1080 /* NB: Solaris only supports R_386_TLS_LE and R_386_TLS_IE. */
1081 to_le_type = R_386_TLS_LE;
1082 to_ie_type = R_386_TLS_IE;
1084 else
1086 to_le_type = R_386_TLS_LE_32;
1087 to_ie_type = R_386_TLS_IE_32;
1090 switch (from_type)
1092 case R_386_TLS_DESC_CALL:
1093 /* Check valid GDesc call:
1094 call *x@tlscall(%eax)
1096 offset = rel->r_offset;
1097 call = NULL;
1098 if (offset + 2 <= sec->size)
1100 /* Make sure that it's a call *x@tlscall(%eax). */
1101 call = contents + offset;
1102 if (call[0] != 0xff || call[1] != 0x10)
1103 call = NULL;
1106 if (call == NULL)
1108 _bfd_x86_elf_link_report_tls_transition_error
1109 (info, abfd, sec, symtab_hdr, h, sym, rel,
1110 "R_386_TLS_DESC_CALL", NULL,
1111 elf_x86_tls_error_indirect_call);
1113 return false;
1116 /* Fall through. */
1118 case R_386_TLS_GD:
1119 case R_386_TLS_GOTDESC:
1120 case R_386_TLS_IE_32:
1121 case R_386_TLS_IE:
1122 case R_386_TLS_GOTIE:
1123 if (bfd_link_executable (info))
1125 if (h == NULL)
1126 to_type = to_le_type;
1127 else if (from_type != R_386_TLS_IE
1128 && from_type != R_386_TLS_GOTIE)
1129 to_type = to_ie_type;
1132 /* When we are called from elf_i386_relocate_section, there may
1133 be additional transitions based on TLS_TYPE. */
1134 if (from_relocate_section)
1136 unsigned int new_to_type = to_type;
1138 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1139 new_to_type = to_le_type;
1141 if (to_type == R_386_TLS_GD
1142 || to_type == R_386_TLS_GOTDESC
1143 || to_type == R_386_TLS_DESC_CALL)
1145 if (tls_type == GOT_TLS_IE_POS)
1146 new_to_type = R_386_TLS_GOTIE;
1147 else if (tls_type & GOT_TLS_IE)
1148 new_to_type = to_ie_type;
1151 /* We checked the transition before when we were called from
1152 elf_i386_scan_relocs. We only want to check the new
1153 transition which hasn't been checked before. */
1154 check = new_to_type != to_type && from_type == to_type;
1155 to_type = new_to_type;
1158 break;
1160 case R_386_TLS_LDM:
1161 if (bfd_link_executable (info))
1162 to_type = to_le_type;
1163 break;
1165 default:
1166 return true;
1169 /* Return TRUE if there is no transition. */
1170 if (from_type == to_type)
1171 return true;
1173 /* Check if the transition can be performed. */
1174 enum elf_x86_tls_error_type tls_error;
1175 if (check
1176 && ((tls_error = elf_i386_check_tls_transition (sec, contents,
1177 symtab_hdr,
1178 sym_hashes,
1179 from_type, rel,
1180 relend))
1181 != elf_x86_tls_error_none))
1183 reloc_howto_type *from, *to;
1185 from = elf_i386_rtype_to_howto (from_type);
1186 to = elf_i386_rtype_to_howto (to_type);
1188 _bfd_x86_elf_link_report_tls_transition_error
1189 (info, abfd, sec, symtab_hdr, h, sym, rel, from->name,
1190 to->name, tls_error);
1192 return false;
1195 *r_type = to_type;
1196 return true;
1199 /* With the local symbol, foo, we convert
1200 mov foo@GOT[(%reg1)], %reg2
1202 lea foo[@GOTOFF(%reg1)], %reg2
1203 and convert
1204 call/jmp *foo@GOT[(%reg)]
1206 nop call foo/jmp foo nop
1207 When PIC is false, convert
1208 test %reg1, foo@GOT[(%reg2)]
1210 test $foo, %reg1
1211 and convert
1212 binop foo@GOT[(%reg1)], %reg2
1214 binop $foo, %reg2
1215 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1216 instructions. */
1218 static
1219 bool
1220 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1221 bfd_byte *contents,
1222 unsigned int *r_type_p,
1223 Elf_Internal_Rela *irel,
1224 struct elf_link_hash_entry *h,
1225 bool *converted,
1226 struct bfd_link_info *link_info)
1228 struct elf_x86_link_hash_table *htab;
1229 unsigned int opcode;
1230 unsigned int modrm;
1231 bool baseless;
1232 Elf_Internal_Sym *isym;
1233 unsigned int addend;
1234 unsigned int nop;
1235 bfd_vma nop_offset;
1236 bool is_pic;
1237 bool to_reloc_32;
1238 bool abs_symbol;
1239 unsigned int r_type;
1240 unsigned int r_symndx;
1241 bfd_vma roff = irel->r_offset;
1242 bool local_ref;
1243 struct elf_x86_link_hash_entry *eh;
1245 if (roff < 2)
1246 return true;
1248 /* Addend for R_386_GOT32X relocations must be 0. */
1249 addend = bfd_get_32 (abfd, contents + roff);
1250 if (addend != 0)
1251 return true;
1253 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1254 if (htab == NULL || ! is_x86_elf (abfd, htab))
1256 bfd_set_error (bfd_error_wrong_format);
1257 return false;
1260 is_pic = bfd_link_pic (link_info);
1262 r_type = *r_type_p;
1263 r_symndx = ELF32_R_SYM (irel->r_info);
1265 modrm = bfd_get_8 (abfd, contents + roff - 1);
1266 baseless = (modrm & 0xc7) == 0x5;
1268 if (h)
1270 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1271 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1272 isym = NULL;
1273 abs_symbol = ABS_SYMBOL_P (h);
1275 else
1277 local_ref = true;
1278 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
1279 r_symndx);
1280 abs_symbol = isym->st_shndx == SHN_ABS;
1283 if (baseless && is_pic)
1285 /* For PIC, disallow R_386_GOT32X without a base register
1286 since we don't know what the GOT base is. */
1287 const char *name;
1289 if (h == NULL)
1290 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1291 else
1292 name = h->root.root.string;
1294 _bfd_error_handler
1295 /* xgettext:c-format */
1296 (_("%pB: direct GOT relocation R_386_GOT32X against `%s' without base"
1297 " register can not be used when making a shared object"),
1298 abfd, name);
1299 return false;
1302 opcode = bfd_get_8 (abfd, contents + roff - 2);
1304 /* Convert to R_386_32 if PIC is false or there is no base
1305 register. */
1306 to_reloc_32 = !is_pic || baseless;
1308 eh = elf_x86_hash_entry (h);
1310 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1311 reloc. */
1312 if (h == NULL)
1314 if (opcode == 0x0ff)
1315 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1316 goto convert_branch;
1317 else
1318 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1319 "test %reg1, foo@GOT(%reg2)" and
1320 "binop foo@GOT[(%reg1)], %reg2". */
1321 goto convert_load;
1324 /* Undefined weak symbol is only bound locally in executable
1325 and its reference is resolved as 0. */
1326 if (h->root.type == bfd_link_hash_undefweak
1327 && !eh->linker_def
1328 && local_ref)
1330 if (opcode == 0xff)
1332 /* No direct branch to 0 for PIC. */
1333 if (is_pic)
1334 return true;
1335 else
1336 goto convert_branch;
1338 else
1340 /* We can convert load of address 0 to R_386_32. */
1341 to_reloc_32 = true;
1342 goto convert_load;
1346 if (opcode == 0xff)
1348 /* We have "call/jmp *foo@GOT[(%reg)]". */
1349 if ((h->root.type == bfd_link_hash_defined
1350 || h->root.type == bfd_link_hash_defweak)
1351 && local_ref)
1353 /* The function is locally defined. */
1354 convert_branch:
1355 /* Convert R_386_GOT32X to R_386_PC32. */
1356 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1358 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1359 is a nop prefix. */
1360 modrm = 0xe8;
1361 /* To support TLS optimization, always use addr32 prefix
1362 for "call *___tls_get_addr@GOT(%reg)". */
1363 if (eh && eh->tls_get_addr)
1365 nop = 0x67;
1366 nop_offset = irel->r_offset - 2;
1368 else
1370 nop = htab->params->call_nop_byte;
1371 if (htab->params->call_nop_as_suffix)
1373 nop_offset = roff + 3;
1374 irel->r_offset -= 1;
1376 else
1377 nop_offset = roff - 2;
1380 else
1382 /* Convert to "jmp foo nop". */
1383 modrm = 0xe9;
1384 nop = NOP_OPCODE;
1385 nop_offset = roff + 3;
1386 irel->r_offset -= 1;
1389 bfd_put_8 (abfd, nop, contents + nop_offset);
1390 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1391 /* When converting to PC-relative relocation, we
1392 need to adjust addend by -4. */
1393 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1394 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1395 *r_type_p = R_386_PC32;
1396 *converted = true;
1399 else
1401 /* We have "mov foo@GOT[(%re1g)], %reg2",
1402 "test %reg1, foo@GOT(%reg2)" and
1403 "binop foo@GOT[(%reg1)], %reg2".
1405 Avoid optimizing _DYNAMIC since ld.so may use its
1406 link-time address. */
1407 if (h == htab->elf.hdynamic)
1408 return true;
1410 /* def_regular is set by an assignment in a linker script in
1411 bfd_elf_record_link_assignment. start_stop is set on
1412 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1413 if (h->start_stop
1414 || eh->linker_def
1415 || ((h->def_regular
1416 || h->root.type == bfd_link_hash_defined
1417 || h->root.type == bfd_link_hash_defweak)
1418 && local_ref))
1420 convert_load:
1421 if (opcode == 0x8b)
1423 if (abs_symbol && local_ref)
1424 to_reloc_32 = true;
1426 if (to_reloc_32)
1428 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1429 "mov $foo, %reg2" with R_386_32. */
1430 r_type = R_386_32;
1431 modrm = 0xc0 | (modrm & 0x38) >> 3;
1432 bfd_put_8 (abfd, modrm, contents + roff - 1);
1433 opcode = 0xc7;
1435 else
1437 /* Convert "mov foo@GOT(%reg1), %reg2" to
1438 "lea foo@GOTOFF(%reg1), %reg2". */
1439 r_type = R_386_GOTOFF;
1440 opcode = 0x8d;
1443 else
1445 /* Only R_386_32 is supported. */
1446 if (!to_reloc_32)
1447 return true;
1449 if (opcode == 0x85)
1451 /* Convert "test %reg1, foo@GOT(%reg2)" to
1452 "test $foo, %reg1". */
1453 modrm = 0xc0 | (modrm & 0x38) >> 3;
1454 opcode = 0xf7;
1456 else
1458 /* Convert "binop foo@GOT(%reg1), %reg2" to
1459 "binop $foo, %reg2". */
1460 modrm = (0xc0
1461 | (modrm & 0x38) >> 3
1462 | (opcode & 0x3c));
1463 opcode = 0x81;
1465 bfd_put_8 (abfd, modrm, contents + roff - 1);
1466 r_type = R_386_32;
1469 bfd_put_8 (abfd, opcode, contents + roff - 2);
1470 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1471 *r_type_p = r_type;
1472 *converted = true;
1476 return true;
1479 /* Look through the relocs for a section during the first phase, and
1480 calculate needed space in the global offset table, and procedure
1481 linkage table. */
1483 static bool
1484 elf_i386_scan_relocs (bfd *abfd,
1485 struct bfd_link_info *info,
1486 asection *sec,
1487 const Elf_Internal_Rela *relocs)
1489 struct elf_x86_link_hash_table *htab;
1490 Elf_Internal_Shdr *symtab_hdr;
1491 struct elf_link_hash_entry **sym_hashes;
1492 const Elf_Internal_Rela *rel;
1493 const Elf_Internal_Rela *rel_end;
1494 bfd_byte *contents;
1495 bool converted;
1497 if (bfd_link_relocatable (info))
1498 return true;
1500 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1501 if (htab == NULL)
1503 sec->check_relocs_failed = 1;
1504 return false;
1507 BFD_ASSERT (is_x86_elf (abfd, htab));
1509 /* Get the section contents. */
1510 if (elf_section_data (sec)->this_hdr.contents != NULL)
1511 contents = elf_section_data (sec)->this_hdr.contents;
1512 else if (!_bfd_elf_mmap_section_contents (abfd, sec, &contents))
1514 sec->check_relocs_failed = 1;
1515 return false;
1518 symtab_hdr = &elf_symtab_hdr (abfd);
1519 sym_hashes = elf_sym_hashes (abfd);
1521 converted = false;
1523 rel_end = relocs + sec->reloc_count;
1524 for (rel = relocs; rel < rel_end; rel++)
1526 unsigned int r_type;
1527 unsigned int r_symndx;
1528 struct elf_link_hash_entry *h;
1529 struct elf_x86_link_hash_entry *eh;
1530 Elf_Internal_Sym *isym;
1531 const char *name;
1532 bool size_reloc;
1533 bool no_dynreloc;
1535 r_symndx = ELF32_R_SYM (rel->r_info);
1536 r_type = ELF32_R_TYPE (rel->r_info);
1538 /* Don't check R_386_NONE. */
1539 if (r_type == R_386_NONE)
1540 continue;
1542 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1544 /* xgettext:c-format */
1545 _bfd_error_handler (_("%pB: bad symbol index: %d"),
1546 abfd, r_symndx);
1547 goto error_return;
1550 if (r_symndx < symtab_hdr->sh_info)
1552 /* A local symbol. */
1553 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1554 abfd, r_symndx);
1555 if (isym == NULL)
1556 goto error_return;
1558 /* Check relocation against local STT_GNU_IFUNC symbol. */
1559 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1561 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, true);
1562 if (h == NULL)
1563 goto error_return;
1565 /* Fake a STT_GNU_IFUNC symbol. */
1566 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1567 isym, NULL);
1568 h->type = STT_GNU_IFUNC;
1569 h->def_regular = 1;
1570 h->ref_regular = 1;
1571 h->forced_local = 1;
1572 h->root.type = bfd_link_hash_defined;
1574 else
1575 h = NULL;
1577 else
1579 isym = NULL;
1580 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1581 while (h->root.type == bfd_link_hash_indirect
1582 || h->root.type == bfd_link_hash_warning)
1583 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1586 eh = (struct elf_x86_link_hash_entry *) h;
1587 if (h != NULL)
1589 if (r_type == R_386_GOTOFF)
1590 eh->gotoff_ref = 1;
1592 /* It is referenced by a non-shared object. */
1593 h->ref_regular = 1;
1596 if (r_type == R_386_GOT32X
1597 && (h == NULL || h->type != STT_GNU_IFUNC))
1599 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1600 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1601 &r_type, irel, h,
1602 &converted, info))
1603 goto error_return;
1606 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
1607 symtab_hdr, &no_dynreloc))
1608 return false;
1610 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1611 symtab_hdr, sym_hashes,
1612 &r_type, GOT_UNKNOWN,
1613 rel, rel_end, h, isym, false))
1614 goto error_return;
1616 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
1617 if (h == htab->elf.hgot)
1618 htab->got_referenced = true;
1620 switch (r_type)
1622 case R_386_TLS_LDM:
1623 htab->tls_ld_or_ldm_got.refcount = 1;
1624 goto create_got;
1626 case R_386_PLT32:
1627 /* This symbol requires a procedure linkage table entry. We
1628 actually build the entry in adjust_dynamic_symbol,
1629 because this might be a case of linking PIC code which is
1630 never referenced by a dynamic object, in which case we
1631 don't need to generate a procedure linkage table entry
1632 after all. */
1634 /* If this is a local symbol, we resolve it directly without
1635 creating a procedure linkage table entry. */
1636 if (h == NULL)
1637 continue;
1639 eh->zero_undefweak &= 0x2;
1640 h->needs_plt = 1;
1641 h->plt.refcount = 1;
1642 break;
1644 case R_386_SIZE32:
1645 size_reloc = true;
1646 goto do_size;
1648 case R_386_TLS_IE_32:
1649 case R_386_TLS_IE:
1650 case R_386_TLS_GOTIE:
1651 if (!bfd_link_executable (info))
1652 info->flags |= DF_STATIC_TLS;
1653 /* Fall through */
1655 case R_386_GOT32:
1656 case R_386_GOT32X:
1657 case R_386_TLS_GD:
1658 case R_386_TLS_GOTDESC:
1659 case R_386_TLS_DESC_CALL:
1660 /* This symbol requires a global offset table entry. */
1662 int tls_type, old_tls_type;
1664 switch (r_type)
1666 default:
1667 case R_386_GOT32:
1668 case R_386_GOT32X:
1669 tls_type = GOT_NORMAL;
1670 break;
1671 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1672 case R_386_TLS_GOTDESC:
1673 case R_386_TLS_DESC_CALL:
1674 tls_type = GOT_TLS_GDESC; break;
1675 case R_386_TLS_IE_32:
1676 if (ELF32_R_TYPE (rel->r_info) == r_type)
1677 tls_type = GOT_TLS_IE_NEG;
1678 else
1679 /* If this is a GD->IE transition, we may use either of
1680 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1681 tls_type = GOT_TLS_IE;
1682 break;
1683 case R_386_TLS_IE:
1684 case R_386_TLS_GOTIE:
1685 tls_type = GOT_TLS_IE_POS; break;
1688 if (h != NULL)
1690 h->got.refcount = 1;
1691 old_tls_type = elf_x86_hash_entry (h)->tls_type;
1693 else
1695 bfd_signed_vma *local_got_refcounts;
1697 if (!elf_x86_allocate_local_got_info (abfd,
1698 symtab_hdr->sh_info))
1699 goto error_return;
1701 /* This is a global offset table entry for a local symbol. */
1702 local_got_refcounts = elf_local_got_refcounts (abfd);
1703 local_got_refcounts[r_symndx] = 1;
1704 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1707 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1708 tls_type |= old_tls_type;
1709 /* If a TLS symbol is accessed using IE at least once,
1710 there is no point to use dynamic model for it. */
1711 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1712 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1713 || (tls_type & GOT_TLS_IE) == 0))
1715 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1716 tls_type = old_tls_type;
1717 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1718 && GOT_TLS_GD_ANY_P (tls_type))
1719 tls_type |= old_tls_type;
1720 else
1722 if (h)
1723 name = h->root.root.string;
1724 else
1725 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1726 NULL);
1727 _bfd_error_handler
1728 /* xgettext:c-format */
1729 (_("%pB: `%s' accessed both as normal and "
1730 "thread local symbol"),
1731 abfd, name);
1732 bfd_set_error (bfd_error_bad_value);
1733 goto error_return;
1737 if (old_tls_type != tls_type)
1739 if (h != NULL)
1740 elf_x86_hash_entry (h)->tls_type = tls_type;
1741 else
1742 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1745 /* Fall through */
1747 case R_386_GOTOFF:
1748 case R_386_GOTPC:
1749 create_got:
1750 if (r_type != R_386_TLS_IE)
1752 if (eh != NULL)
1754 eh->zero_undefweak &= 0x2;
1756 /* Need GOT to resolve undefined weak symbol to 0. */
1757 if (r_type == R_386_GOTOFF
1758 && h->root.type == bfd_link_hash_undefweak
1759 && bfd_link_executable (info))
1760 htab->got_referenced = true;
1762 break;
1764 /* Fall through */
1766 case R_386_TLS_LE_32:
1767 case R_386_TLS_LE:
1768 if (eh != NULL)
1769 eh->zero_undefweak &= 0x2;
1770 if (bfd_link_executable (info))
1771 break;
1772 info->flags |= DF_STATIC_TLS;
1773 goto do_relocation;
1775 case R_386_32:
1776 case R_386_PC32:
1777 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1778 eh->zero_undefweak |= 0x2;
1779 do_relocation:
1780 /* We are called after all symbols have been resolved. Only
1781 relocation against STT_GNU_IFUNC symbol must go through
1782 PLT. */
1783 if (h != NULL
1784 && (bfd_link_executable (info)
1785 || h->type == STT_GNU_IFUNC))
1787 bool func_pointer_ref = false;
1789 if (r_type == R_386_PC32)
1791 /* Since something like ".long foo - ." may be used
1792 as pointer, make sure that PLT is used if foo is
1793 a function defined in a shared library. */
1794 if ((sec->flags & SEC_CODE) == 0)
1795 h->pointer_equality_needed = 1;
1796 else if (h->type == STT_GNU_IFUNC
1797 && bfd_link_pic (info))
1799 _bfd_error_handler
1800 /* xgettext:c-format */
1801 (_("%pB: unsupported non-PIC call to IFUNC `%s'"),
1802 abfd, h->root.root.string);
1803 bfd_set_error (bfd_error_bad_value);
1804 goto error_return;
1807 else
1809 /* R_386_32 can be resolved at run-time. Function
1810 pointer reference doesn't need PLT for pointer
1811 equality. */
1812 if (r_type == R_386_32
1813 && (sec->flags & SEC_READONLY) == 0)
1814 func_pointer_ref = true;
1816 /* IFUNC symbol needs pointer equality in PDE so that
1817 function pointer reference will be resolved to its
1818 PLT entry directly. */
1819 if (!func_pointer_ref
1820 || (bfd_link_pde (info)
1821 && h->type == STT_GNU_IFUNC))
1822 h->pointer_equality_needed = 1;
1825 if (!func_pointer_ref)
1827 /* If this reloc is in a read-only section, we might
1828 need a copy reloc. We can't check reliably at this
1829 stage whether the section is read-only, as input
1830 sections have not yet been mapped to output sections.
1831 Tentatively set the flag for now, and correct in
1832 adjust_dynamic_symbol. */
1833 h->non_got_ref = 1;
1835 if (!elf_has_indirect_extern_access (sec->owner))
1836 eh->non_got_ref_without_indirect_extern_access = 1;
1838 /* We may need a .plt entry if the symbol is a function
1839 defined in a shared lib or is a function referenced
1840 from the code or read-only section. */
1841 if (!h->def_regular
1842 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1843 h->plt.refcount = 1;
1845 if (htab->elf.target_os != is_solaris
1846 && h->pointer_equality_needed
1847 && h->type == STT_FUNC
1848 && eh->def_protected
1849 && !SYMBOL_DEFINED_NON_SHARED_P (h)
1850 && h->def_dynamic)
1852 /* Disallow non-canonical reference to canonical
1853 protected function. */
1854 _bfd_error_handler
1855 /* xgettext:c-format */
1856 (_("%pB: non-canonical reference to canonical "
1857 "protected function `%s' in %pB"),
1858 abfd, h->root.root.string,
1859 h->root.u.def.section->owner);
1860 bfd_set_error (bfd_error_bad_value);
1861 goto error_return;
1866 size_reloc = false;
1867 do_size:
1868 if (!no_dynreloc
1869 && NEED_DYNAMIC_RELOCATION_P (false, info, false, h, sec,
1870 r_type, R_386_32))
1872 struct elf_dyn_relocs *p;
1873 struct elf_dyn_relocs **head;
1875 /* If this is a global symbol, we count the number of
1876 relocations we need for this symbol. */
1877 if (h != NULL)
1879 head = &h->dyn_relocs;
1881 else
1883 /* Track dynamic relocs needed for local syms too.
1884 We really need local syms available to do this
1885 easily. Oh well. */
1886 void **vpp;
1887 asection *s;
1889 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1890 abfd, r_symndx);
1891 if (isym == NULL)
1892 goto error_return;
1894 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1895 if (s == NULL)
1896 s = sec;
1898 vpp = &elf_section_data (s)->local_dynrel;
1899 head = (struct elf_dyn_relocs **)vpp;
1902 p = *head;
1903 if (p == NULL || p->sec != sec)
1905 size_t amt = sizeof *p;
1906 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1907 amt);
1908 if (p == NULL)
1909 goto error_return;
1910 p->next = *head;
1911 *head = p;
1912 p->sec = sec;
1913 p->count = 0;
1914 p->pc_count = 0;
1917 p->count += 1;
1918 /* Count size relocation as PC-relative relocation. */
1919 if (r_type == R_386_PC32 || size_reloc)
1920 p->pc_count += 1;
1922 break;
1924 /* This relocation describes the C++ object vtable hierarchy.
1925 Reconstruct it for later use during GC. */
1926 case R_386_GNU_VTINHERIT:
1927 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1928 goto error_return;
1929 break;
1931 /* This relocation describes which C++ vtable entries are actually
1932 used. Record for later use during GC. */
1933 case R_386_GNU_VTENTRY:
1934 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1935 goto error_return;
1936 break;
1938 default:
1939 break;
1943 if (elf_section_data (sec)->this_hdr.contents != contents)
1945 if (!converted)
1946 _bfd_elf_munmap_section_contents (sec, contents);
1947 else
1949 /* Cache the section contents for elf_link_input_bfd if any
1950 load is converted or --no-keep-memory isn't used. */
1951 elf_section_data (sec)->this_hdr.contents = contents;
1952 info->cache_size += sec->size;
1956 /* Cache relocations if any load is converted. */
1957 if (elf_section_data (sec)->relocs != relocs && converted)
1958 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1960 return true;
1962 error_return:
1963 if (elf_section_data (sec)->this_hdr.contents != contents)
1964 _bfd_elf_munmap_section_contents (sec, contents);
1965 sec->check_relocs_failed = 1;
1966 return false;
1969 static bool
1970 elf_i386_early_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1972 bfd *abfd;
1974 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
1975 for (abfd = info->input_bfds;
1976 abfd != (bfd *) NULL;
1977 abfd = abfd->link.next)
1978 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1979 && !_bfd_elf_link_iterate_on_relocs (abfd, info,
1980 elf_i386_scan_relocs))
1981 return false;
1983 return _bfd_x86_elf_early_size_sections (output_bfd, info);
1986 /* Set the correct type for an x86 ELF section. We do this by the
1987 section name, which is a hack, but ought to work. */
1989 static bool
1990 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1991 Elf_Internal_Shdr *hdr,
1992 asection *sec)
1994 const char *name;
1996 name = bfd_section_name (sec);
1998 /* This is an ugly, but unfortunately necessary hack that is
1999 needed when producing EFI binaries on x86. It tells
2000 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2001 containing ELF relocation info. We need this hack in order to
2002 be able to generate ELF binaries that can be translated into
2003 EFI applications (which are essentially COFF objects). Those
2004 files contain a COFF ".reloc" section inside an ELFNN object,
2005 which would normally cause BFD to segfault because it would
2006 attempt to interpret this section as containing relocation
2007 entries for section "oc". With this hack enabled, ".reloc"
2008 will be treated as a normal data section, which will avoid the
2009 segfault. However, you won't be able to create an ELFNN binary
2010 with a section named "oc" that needs relocations, but that's
2011 the kind of ugly side-effects you get when detecting section
2012 types based on their names... In practice, this limitation is
2013 unlikely to bite. */
2014 if (strcmp (name, ".reloc") == 0)
2015 hdr->sh_type = SHT_PROGBITS;
2017 return true;
2020 /* Return the relocation value for @tpoff relocation
2021 if STT_TLS virtual address is ADDRESS. */
2023 static bfd_vma
2024 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2026 struct elf_link_hash_table *htab = elf_hash_table (info);
2027 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2028 bfd_vma static_tls_size;
2030 /* If tls_sec is NULL, we should have signalled an error already. */
2031 if (htab->tls_sec == NULL)
2032 return 0;
2034 /* Consider special static TLS alignment requirements. */
2035 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2036 return static_tls_size + htab->tls_sec->vma - address;
2039 /* Relocate an i386 ELF section. */
2041 static int
2042 elf_i386_relocate_section (bfd *output_bfd,
2043 struct bfd_link_info *info,
2044 bfd *input_bfd,
2045 asection *input_section,
2046 bfd_byte *contents,
2047 Elf_Internal_Rela *relocs,
2048 Elf_Internal_Sym *local_syms,
2049 asection **local_sections)
2051 struct elf_x86_link_hash_table *htab;
2052 Elf_Internal_Shdr *symtab_hdr;
2053 struct elf_link_hash_entry **sym_hashes;
2054 bfd_vma *local_got_offsets;
2055 bfd_vma *local_tlsdesc_gotents;
2056 Elf_Internal_Rela *rel;
2057 Elf_Internal_Rela *wrel;
2058 Elf_Internal_Rela *relend;
2059 bool is_vxworks_tls;
2060 unsigned expected_tls_le;
2061 unsigned plt_entry_size;
2063 /* Skip if check_relocs or scan_relocs failed. */
2064 if (input_section->check_relocs_failed)
2065 return false;
2067 htab = elf_x86_hash_table (info, I386_ELF_DATA);
2068 if (htab == NULL)
2069 return false;
2071 if (!is_x86_elf (input_bfd, htab))
2073 bfd_set_error (bfd_error_wrong_format);
2074 return false;
2077 symtab_hdr = &elf_symtab_hdr (input_bfd);
2078 sym_hashes = elf_sym_hashes (input_bfd);
2079 local_got_offsets = elf_local_got_offsets (input_bfd);
2080 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2081 /* We have to handle relocations in vxworks .tls_vars sections
2082 specially, because the dynamic loader is 'weird'. */
2083 is_vxworks_tls = (htab->elf.target_os == is_vxworks
2084 && bfd_link_pic (info)
2085 && !strcmp (input_section->output_section->name,
2086 ".tls_vars"));
2088 _bfd_x86_elf_set_tls_module_base (info);
2090 plt_entry_size = htab->plt.plt_entry_size;
2092 rel = wrel = relocs;
2093 relend = relocs + input_section->reloc_count;
2094 for (; rel < relend; wrel++, rel++)
2096 unsigned int r_type, r_type_tls;
2097 reloc_howto_type *howto;
2098 unsigned long r_symndx;
2099 struct elf_link_hash_entry *h;
2100 struct elf_x86_link_hash_entry *eh;
2101 Elf_Internal_Sym *sym;
2102 asection *sec;
2103 bfd_vma off, offplt, plt_offset;
2104 bfd_vma relocation;
2105 bool unresolved_reloc;
2106 bfd_reloc_status_type r;
2107 unsigned int indx;
2108 int tls_type;
2109 bfd_vma st_size;
2110 asection *resolved_plt;
2111 bool resolved_to_zero;
2112 bool relative_reloc;
2114 r_type = ELF32_R_TYPE (rel->r_info);
2115 if (r_type == R_386_GNU_VTINHERIT
2116 || r_type == R_386_GNU_VTENTRY)
2118 if (wrel != rel)
2119 *wrel = *rel;
2120 continue;
2123 howto = elf_i386_rtype_to_howto (r_type);
2124 if (howto == NULL)
2125 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2127 r_symndx = ELF32_R_SYM (rel->r_info);
2128 h = NULL;
2129 sym = NULL;
2130 sec = NULL;
2131 unresolved_reloc = false;
2132 if (r_symndx < symtab_hdr->sh_info)
2134 sym = local_syms + r_symndx;
2135 sec = local_sections[r_symndx];
2136 relocation = (sec->output_section->vma
2137 + sec->output_offset
2138 + sym->st_value);
2139 st_size = sym->st_size;
2141 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2142 && ((sec->flags & SEC_MERGE) != 0
2143 || (bfd_link_relocatable (info)
2144 && sec->output_offset != 0)))
2146 bfd_vma addend;
2147 bfd_byte *where = contents + rel->r_offset;
2149 switch (bfd_get_reloc_size (howto))
2151 case 1:
2152 addend = bfd_get_8 (input_bfd, where);
2153 if (howto->pc_relative)
2155 addend = (addend ^ 0x80) - 0x80;
2156 addend += 1;
2158 break;
2159 case 2:
2160 addend = bfd_get_16 (input_bfd, where);
2161 if (howto->pc_relative)
2163 addend = (addend ^ 0x8000) - 0x8000;
2164 addend += 2;
2166 break;
2167 case 4:
2168 addend = bfd_get_32 (input_bfd, where);
2169 if (howto->pc_relative)
2171 addend = (addend ^ 0x80000000) - 0x80000000;
2172 addend += 4;
2174 break;
2175 default:
2176 abort ();
2179 if (bfd_link_relocatable (info))
2180 addend += sec->output_offset;
2181 else
2183 asection *msec = sec;
2184 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2185 addend);
2186 addend -= relocation;
2187 addend += msec->output_section->vma + msec->output_offset;
2190 switch (bfd_get_reloc_size (howto))
2192 case 1:
2193 /* FIXME: overflow checks. */
2194 if (howto->pc_relative)
2195 addend -= 1;
2196 bfd_put_8 (input_bfd, addend, where);
2197 break;
2198 case 2:
2199 if (howto->pc_relative)
2200 addend -= 2;
2201 bfd_put_16 (input_bfd, addend, where);
2202 break;
2203 case 4:
2204 if (howto->pc_relative)
2205 addend -= 4;
2206 bfd_put_32 (input_bfd, addend, where);
2207 break;
2210 else if (!bfd_link_relocatable (info)
2211 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2213 /* Relocate against local STT_GNU_IFUNC symbol. */
2214 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2215 false);
2216 if (h == NULL)
2217 abort ();
2219 /* Set STT_GNU_IFUNC symbol value. */
2220 h->root.u.def.value = sym->st_value;
2221 h->root.u.def.section = sec;
2224 else
2226 bool warned ATTRIBUTE_UNUSED;
2227 bool ignored ATTRIBUTE_UNUSED;
2229 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2230 r_symndx, symtab_hdr, sym_hashes,
2231 h, sec, relocation,
2232 unresolved_reloc, warned, ignored);
2233 st_size = h->size;
2236 if (sec != NULL && discarded_section (sec))
2238 _bfd_clear_contents (howto, input_bfd, input_section,
2239 contents, rel->r_offset);
2240 wrel->r_offset = rel->r_offset;
2241 wrel->r_info = 0;
2242 wrel->r_addend = 0;
2244 /* For ld -r, remove relocations in debug sections against
2245 sections defined in discarded sections. Not done for
2246 eh_frame editing code expects to be present. */
2247 if (bfd_link_relocatable (info)
2248 && (input_section->flags & SEC_DEBUGGING))
2249 wrel--;
2251 continue;
2254 if (bfd_link_relocatable (info))
2256 if (wrel != rel)
2257 *wrel = *rel;
2258 continue;
2261 eh = (struct elf_x86_link_hash_entry *) h;
2263 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2264 it here if it is defined in a non-shared object. */
2265 if (h != NULL
2266 && h->type == STT_GNU_IFUNC
2267 && h->def_regular)
2269 asection *gotplt, *base_got;
2270 bfd_vma plt_index;
2271 const char *name;
2273 if ((input_section->flags & SEC_ALLOC) == 0)
2275 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2276 STT_GNU_IFUNC symbol as STT_FUNC. */
2277 if (elf_section_type (input_section) == SHT_NOTE)
2278 goto skip_ifunc;
2279 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2280 sections because such sections are not SEC_ALLOC and
2281 thus ld.so will not process them. */
2282 if ((input_section->flags & SEC_DEBUGGING) != 0)
2283 continue;
2284 abort ();
2287 /* STT_GNU_IFUNC symbol must go through PLT. */
2288 if (htab->elf.splt != NULL)
2290 if (htab->plt_second != NULL)
2292 resolved_plt = htab->plt_second;
2293 plt_offset = eh->plt_second.offset;
2295 else
2297 resolved_plt = htab->elf.splt;
2298 plt_offset = h->plt.offset;
2300 gotplt = htab->elf.sgotplt;
2302 else
2304 resolved_plt = htab->elf.iplt;
2305 plt_offset = h->plt.offset;
2306 gotplt = htab->elf.igotplt;
2309 switch (r_type)
2311 default:
2312 break;
2314 case R_386_GOT32:
2315 case R_386_GOT32X:
2316 base_got = htab->elf.sgot;
2317 off = h->got.offset;
2319 if (base_got == NULL)
2320 abort ();
2322 if (off == (bfd_vma) -1)
2324 /* We can't use h->got.offset here to save state, or
2325 even just remember the offset, as finish_dynamic_symbol
2326 would use that as offset into .got. */
2328 if (h->plt.offset == (bfd_vma) -1)
2329 abort ();
2331 if (htab->elf.splt != NULL)
2333 plt_index = (h->plt.offset / plt_entry_size
2334 - htab->plt.has_plt0);
2335 off = (plt_index + 3) * 4;
2336 base_got = htab->elf.sgotplt;
2338 else
2340 plt_index = h->plt.offset / plt_entry_size;
2341 off = plt_index * 4;
2342 base_got = htab->elf.igotplt;
2345 if (h->dynindx == -1
2346 || h->forced_local
2347 || info->symbolic)
2349 /* This references the local defitionion. We must
2350 initialize this entry in the global offset table.
2351 Since the offset must always be a multiple of 4,
2352 we use the least significant bit to record
2353 whether we have initialized it already.
2355 When doing a dynamic link, we create a .rela.got
2356 relocation entry to initialize the value. This
2357 is done in the finish_dynamic_symbol routine. */
2358 if ((off & 1) != 0)
2359 off &= ~1;
2360 else
2362 bfd_put_32 (output_bfd, relocation,
2363 base_got->contents + off);
2364 h->got.offset |= 1;
2368 relocation = off;
2370 else
2371 relocation = (base_got->output_section->vma
2372 + base_got->output_offset + off
2373 - gotplt->output_section->vma
2374 - gotplt->output_offset);
2376 if (rel->r_offset > 1
2377 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2378 && *(contents + rel->r_offset - 2) != 0x8d)
2380 if (bfd_link_pic (info))
2381 goto disallow_got32;
2383 /* Add the GOT base if there is no base register. */
2384 relocation += (gotplt->output_section->vma
2385 + gotplt->output_offset);
2387 else if (htab->elf.splt == NULL)
2389 /* Adjust for static executables. */
2390 relocation += gotplt->output_offset;
2393 goto do_relocation;
2396 if (h->plt.offset == (bfd_vma) -1)
2398 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2399 if (r_type == R_386_32
2400 && (input_section->flags & SEC_CODE) == 0)
2401 goto do_ifunc_pointer;
2402 goto bad_ifunc_reloc;
2405 relocation = (resolved_plt->output_section->vma
2406 + resolved_plt->output_offset + plt_offset);
2408 switch (r_type)
2410 default:
2411 bad_ifunc_reloc:
2412 if (h->root.root.string)
2413 name = h->root.root.string;
2414 else
2415 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2416 NULL);
2417 _bfd_error_handler
2418 /* xgettext:c-format */
2419 (_("%pB: relocation %s against STT_GNU_IFUNC "
2420 "symbol `%s' isn't supported"), input_bfd,
2421 howto->name, name);
2422 bfd_set_error (bfd_error_bad_value);
2423 return false;
2425 case R_386_32:
2426 /* Generate dynamic relcoation only when there is a
2427 non-GOT reference in a shared object. */
2428 if ((bfd_link_pic (info) && h->non_got_ref)
2429 || h->plt.offset == (bfd_vma) -1)
2431 Elf_Internal_Rela outrel;
2432 asection *sreloc;
2433 bfd_vma offset;
2435 do_ifunc_pointer:
2436 /* Need a dynamic relocation to get the real function
2437 adddress. */
2438 offset = _bfd_elf_section_offset (output_bfd,
2439 info,
2440 input_section,
2441 rel->r_offset);
2442 if (offset == (bfd_vma) -1
2443 || offset == (bfd_vma) -2)
2444 abort ();
2446 outrel.r_offset = (input_section->output_section->vma
2447 + input_section->output_offset
2448 + offset);
2450 if (POINTER_LOCAL_IFUNC_P (info, h))
2452 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2453 h->root.root.string,
2454 h->root.u.def.section->owner);
2456 /* This symbol is resolved locally. */
2457 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2459 if (htab->params->report_relative_reloc)
2460 _bfd_x86_elf_link_report_relative_reloc
2461 (info, input_section, h, sym,
2462 "R_386_IRELATIVE", &outrel);
2464 bfd_put_32 (output_bfd,
2465 (h->root.u.def.value
2466 + h->root.u.def.section->output_section->vma
2467 + h->root.u.def.section->output_offset),
2468 contents + offset);
2470 else
2471 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2473 /* Dynamic relocations are stored in
2474 1. .rel.ifunc section in PIC object.
2475 2. .rel.got section in dynamic executable.
2476 3. .rel.iplt section in static executable. */
2477 if (bfd_link_pic (info))
2478 sreloc = htab->elf.irelifunc;
2479 else if (htab->elf.splt != NULL)
2480 sreloc = htab->elf.srelgot;
2481 else
2482 sreloc = htab->elf.irelplt;
2483 elf_append_rel (output_bfd, sreloc, &outrel);
2485 /* If this reloc is against an external symbol, we
2486 do not want to fiddle with the addend. Otherwise,
2487 we need to include the symbol value so that it
2488 becomes an addend for the dynamic reloc. For an
2489 internal symbol, we have updated addend. */
2490 continue;
2492 /* FALLTHROUGH */
2493 case R_386_PC32:
2494 case R_386_PLT32:
2495 goto do_relocation;
2497 case R_386_GOTOFF:
2498 /* NB: We can't use the PLT entry as the function address
2499 for PIC since the PIC register may not be set up
2500 properly for indirect call. */
2501 if (bfd_link_pic (info))
2502 goto bad_ifunc_reloc;
2503 relocation -= (gotplt->output_section->vma
2504 + gotplt->output_offset);
2505 goto do_relocation;
2509 skip_ifunc:
2510 resolved_to_zero = (eh != NULL
2511 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2513 switch (r_type)
2515 case R_386_GOT32X:
2516 case R_386_GOT32:
2517 /* Relocation is to the entry for this symbol in the global
2518 offset table. */
2519 if (htab->elf.sgot == NULL)
2520 abort ();
2522 relative_reloc = false;
2523 if (h != NULL)
2525 off = h->got.offset;
2526 if (RESOLVED_LOCALLY_P (info, h, htab))
2528 /* We must initialize this entry in the global offset
2529 table. Since the offset must always be a multiple
2530 of 4, we use the least significant bit to record
2531 whether we have initialized it already.
2533 When doing a dynamic link, we create a .rel.got
2534 relocation entry to initialize the value. This
2535 is done in the finish_dynamic_symbol routine. */
2536 if ((off & 1) != 0)
2537 off &= ~1;
2538 else
2540 bfd_put_32 (output_bfd, relocation,
2541 htab->elf.sgot->contents + off);
2542 h->got.offset |= 1;
2543 /* NB: Don't generate relative relocation here if
2544 it has been generated by DT_RELR. */
2545 if (!info->enable_dt_relr
2546 && GENERATE_RELATIVE_RELOC_P (info, h))
2548 /* PR ld/21402: If this symbol isn't dynamic
2549 in PIC, generate R_386_RELATIVE here. */
2550 eh->no_finish_dynamic_symbol = 1;
2551 relative_reloc = true;
2555 else
2556 unresolved_reloc = false;
2558 else
2560 if (local_got_offsets == NULL)
2561 abort ();
2563 off = local_got_offsets[r_symndx];
2565 /* The offset must always be a multiple of 4. We use
2566 the least significant bit to record whether we have
2567 already generated the necessary reloc. */
2568 if ((off & 1) != 0)
2569 off &= ~1;
2570 else
2572 bfd_put_32 (output_bfd, relocation,
2573 htab->elf.sgot->contents + off);
2574 local_got_offsets[r_symndx] |= 1;
2576 /* NB: Don't generate relative relocation here if it
2577 has been generated by DT_RELR. */
2578 if (!info->enable_dt_relr && bfd_link_pic (info))
2579 relative_reloc = true;
2583 if (relative_reloc)
2585 asection *s;
2586 Elf_Internal_Rela outrel;
2588 s = htab->elf.srelgot;
2589 if (s == NULL)
2590 abort ();
2592 outrel.r_offset = (htab->elf.sgot->output_section->vma
2593 + htab->elf.sgot->output_offset
2594 + off);
2595 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2597 if (htab->params->report_relative_reloc)
2598 _bfd_x86_elf_link_report_relative_reloc
2599 (info, input_section, h, sym, "R_386_RELATIVE",
2600 &outrel);
2602 elf_append_rel (output_bfd, s, &outrel);
2605 if (off >= (bfd_vma) -2)
2606 abort ();
2608 relocation = (htab->elf.sgot->output_section->vma
2609 + htab->elf.sgot->output_offset + off);
2610 if (rel->r_offset > 1
2611 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2612 && *(contents + rel->r_offset - 2) != 0x8d)
2614 if (bfd_link_pic (info))
2616 /* For PIC, disallow R_386_GOT32 without a base
2617 register, except for "lea foo@GOT, %reg", since
2618 we don't know what the GOT base is. */
2619 const char *name;
2621 disallow_got32:
2622 if (h == NULL || h->root.root.string == NULL)
2623 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2624 NULL);
2625 else
2626 name = h->root.root.string;
2628 _bfd_error_handler
2629 /* xgettext:c-format */
2630 (_("%pB: direct GOT relocation %s against `%s'"
2631 " without base register can not be used"
2632 " when making a shared object"),
2633 input_bfd, howto->name, name);
2634 bfd_set_error (bfd_error_bad_value);
2635 return false;
2638 else
2640 /* Subtract the .got.plt section address only with a base
2641 register. */
2642 relocation -= (htab->elf.sgotplt->output_section->vma
2643 + htab->elf.sgotplt->output_offset);
2646 break;
2648 case R_386_GOTOFF:
2649 /* Relocation is relative to the start of the global offset
2650 table. */
2652 /* Check to make sure it isn't a protected function or data
2653 symbol for shared library since it may not be local when
2654 used as function address or with copy relocation. We also
2655 need to make sure that a symbol is referenced locally. */
2656 if (!bfd_link_executable (info) && h)
2658 if (!h->def_regular)
2660 const char *v;
2662 switch (ELF_ST_VISIBILITY (h->other))
2664 case STV_HIDDEN:
2665 v = _("hidden symbol");
2666 break;
2667 case STV_INTERNAL:
2668 v = _("internal symbol");
2669 break;
2670 case STV_PROTECTED:
2671 v = _("protected symbol");
2672 break;
2673 default:
2674 v = _("symbol");
2675 break;
2678 _bfd_error_handler
2679 /* xgettext:c-format */
2680 (_("%pB: relocation R_386_GOTOFF against undefined %s"
2681 " `%s' can not be used when making a shared object"),
2682 input_bfd, v, h->root.root.string);
2683 bfd_set_error (bfd_error_bad_value);
2684 return false;
2686 else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2687 && (h->type == STT_FUNC
2688 || h->type == STT_OBJECT)
2689 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2691 _bfd_error_handler
2692 /* xgettext:c-format */
2693 (_("%pB: relocation R_386_GOTOFF against protected %s"
2694 " `%s' can not be used when making a shared object"),
2695 input_bfd,
2696 h->type == STT_FUNC ? "function" : "data",
2697 h->root.root.string);
2698 bfd_set_error (bfd_error_bad_value);
2699 return false;
2703 /* Note that sgot is not involved in this
2704 calculation. We always want the start of .got.plt. If we
2705 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2706 permitted by the ABI, we might have to change this
2707 calculation. */
2708 relocation -= htab->elf.sgotplt->output_section->vma
2709 + htab->elf.sgotplt->output_offset;
2710 break;
2712 case R_386_GOTPC:
2713 /* Use global offset table as symbol value. */
2714 relocation = htab->elf.sgotplt->output_section->vma
2715 + htab->elf.sgotplt->output_offset;
2716 unresolved_reloc = false;
2717 break;
2719 case R_386_PLT32:
2720 /* Relocation is to the entry for this symbol in the
2721 procedure linkage table. */
2723 /* Resolve a PLT32 reloc against a local symbol directly,
2724 without using the procedure linkage table. */
2725 if (h == NULL)
2726 break;
2728 if ((h->plt.offset == (bfd_vma) -1
2729 && eh->plt_got.offset == (bfd_vma) -1)
2730 || htab->elf.splt == NULL)
2732 /* We didn't make a PLT entry for this symbol. This
2733 happens when statically linking PIC code, or when
2734 using -Bsymbolic. */
2735 break;
2738 if (h->plt.offset != (bfd_vma) -1)
2740 if (htab->plt_second != NULL)
2742 resolved_plt = htab->plt_second;
2743 plt_offset = eh->plt_second.offset;
2745 else
2747 resolved_plt = htab->elf.splt;
2748 plt_offset = h->plt.offset;
2751 else
2753 resolved_plt = htab->plt_got;
2754 plt_offset = eh->plt_got.offset;
2757 relocation = (resolved_plt->output_section->vma
2758 + resolved_plt->output_offset
2759 + plt_offset);
2760 unresolved_reloc = false;
2761 break;
2763 case R_386_SIZE32:
2764 /* Set to symbol size. */
2765 relocation = st_size;
2766 /* Fall through. */
2768 case R_386_32:
2769 case R_386_PC32:
2770 if ((input_section->flags & SEC_ALLOC) == 0
2771 || is_vxworks_tls)
2772 break;
2774 if (GENERATE_DYNAMIC_RELOCATION_P (false, info, eh, r_type,
2775 sec, false,
2776 resolved_to_zero,
2777 (r_type == R_386_PC32)))
2779 Elf_Internal_Rela outrel;
2780 bool skip, relocate;
2781 bool generate_dynamic_reloc = true;
2782 asection *sreloc;
2784 /* When generating a shared object, these relocations
2785 are copied into the output file to be resolved at run
2786 time. */
2788 skip = false;
2789 relocate = false;
2791 outrel.r_offset =
2792 _bfd_elf_section_offset (output_bfd, info, input_section,
2793 rel->r_offset);
2794 if (outrel.r_offset == (bfd_vma) -1)
2795 skip = true;
2796 else if (outrel.r_offset == (bfd_vma) -2)
2797 skip = true, relocate = true;
2798 outrel.r_offset += (input_section->output_section->vma
2799 + input_section->output_offset);
2801 if (skip)
2802 memset (&outrel, 0, sizeof outrel);
2803 else if (COPY_INPUT_RELOC_P (false, info, h, r_type))
2804 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2805 else
2807 /* This symbol is local, or marked to become local. */
2808 relocate = true;
2809 /* NB: Don't generate relative relocation here if it
2810 has been generated by DT_RELR. */
2811 if (info->enable_dt_relr)
2812 generate_dynamic_reloc = false;
2813 else
2815 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2817 if (htab->params->report_relative_reloc)
2818 _bfd_x86_elf_link_report_relative_reloc
2819 (info, input_section, h, sym, "R_386_RELATIVE",
2820 &outrel);
2824 if (generate_dynamic_reloc)
2826 sreloc = elf_section_data (input_section)->sreloc;
2828 if (sreloc == NULL || sreloc->contents == NULL)
2830 r = bfd_reloc_notsupported;
2831 goto check_relocation_error;
2834 elf_append_rel (output_bfd, sreloc, &outrel);
2837 /* If this reloc is against an external symbol, we do
2838 not want to fiddle with the addend. Otherwise, we
2839 need to include the symbol value so that it becomes
2840 an addend for the dynamic reloc. */
2841 if (! relocate)
2842 continue;
2844 break;
2846 case R_386_TLS_IE:
2847 if (!bfd_link_executable (info))
2849 Elf_Internal_Rela outrel;
2850 asection *sreloc;
2852 outrel.r_offset = rel->r_offset
2853 + input_section->output_section->vma
2854 + input_section->output_offset;
2855 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2857 if (htab->params->report_relative_reloc)
2858 _bfd_x86_elf_link_report_relative_reloc
2859 (info, input_section, h, sym, "R_386_RELATIVE",
2860 &outrel);
2862 sreloc = elf_section_data (input_section)->sreloc;
2863 if (sreloc == NULL)
2864 abort ();
2865 elf_append_rel (output_bfd, sreloc, &outrel);
2867 /* Fall through */
2869 case R_386_TLS_GD:
2870 case R_386_TLS_GOTDESC:
2871 case R_386_TLS_DESC_CALL:
2872 case R_386_TLS_IE_32:
2873 case R_386_TLS_GOTIE:
2874 tls_type = GOT_UNKNOWN;
2875 if (h == NULL && local_got_offsets)
2876 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2877 else if (h != NULL)
2878 tls_type = elf_x86_hash_entry(h)->tls_type;
2879 if (tls_type == GOT_TLS_IE)
2880 tls_type = GOT_TLS_IE_NEG;
2882 r_type_tls = r_type;
2883 if (! elf_i386_tls_transition (info, input_bfd,
2884 input_section, contents,
2885 symtab_hdr, sym_hashes,
2886 &r_type_tls, tls_type, rel,
2887 relend, h, sym, true))
2888 return false;
2890 expected_tls_le = htab->elf.target_os == is_solaris
2891 ? R_386_TLS_LE : R_386_TLS_LE_32;
2892 if (r_type_tls == expected_tls_le)
2894 /* NB: Solaris only supports R_386_TLS_GD->R_386_TLS_LE. */
2895 BFD_ASSERT (! unresolved_reloc
2896 && (htab->elf.target_os != is_solaris
2897 || (htab->elf.target_os == is_solaris
2898 && (r_type == R_386_TLS_GD
2899 || r_type == R_386_TLS_IE
2900 || r_type == R_386_TLS_GOTIE))));
2901 if (r_type == R_386_TLS_GD)
2903 unsigned int type;
2904 bfd_vma roff;
2906 /* GD->LE transition. */
2907 type = *(contents + rel->r_offset - 2);
2908 if (type == 0x04)
2910 /* Change
2911 leal foo@tlsgd(,%ebx,1), %eax
2912 call ___tls_get_addr@PLT
2913 into:
2914 movl %gs:0, %eax
2915 subl $foo@tpoff, %eax
2916 (6 byte form of subl). */
2917 roff = rel->r_offset + 5;
2919 else
2921 /* Change
2922 leal foo@tlsgd(%ebx), %eax
2923 call ___tls_get_addr@PLT
2926 leal foo@tlsgd(%reg), %eax
2927 call *___tls_get_addr@GOT(%reg)
2928 which may be converted to
2929 addr32 call ___tls_get_addr
2930 into:
2931 movl %gs:0, %eax; subl $foo@tpoff, %eax
2932 (6 byte form of subl). */
2933 roff = rel->r_offset + 6;
2935 memcpy (contents + roff - 8,
2936 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2937 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2938 contents + roff);
2939 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
2940 rel++;
2941 wrel++;
2942 continue;
2944 else if (r_type == R_386_TLS_GOTDESC)
2946 /* GDesc -> LE transition.
2947 It's originally something like:
2948 leal x@tlsdesc(%ebx), %eax
2950 leal x@ntpoff, %eax
2952 Registers other than %eax may be set up here. */
2954 unsigned int val;
2955 bfd_vma roff;
2957 roff = rel->r_offset;
2958 val = bfd_get_8 (input_bfd, contents + roff - 1);
2960 /* Now modify the instruction as appropriate. */
2961 /* aoliva FIXME: remove the above and xor the byte
2962 below with 0x86. */
2963 bfd_put_8 (output_bfd, val ^ 0x86,
2964 contents + roff - 1);
2965 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2966 contents + roff);
2967 continue;
2969 else if (r_type == R_386_TLS_DESC_CALL)
2971 /* GDesc -> LE transition.
2972 It's originally:
2973 call *(%eax)
2974 Turn it into:
2975 xchg %ax,%ax */
2977 bfd_vma roff;
2979 roff = rel->r_offset;
2980 bfd_put_8 (output_bfd, 0x66, contents + roff);
2981 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2982 continue;
2984 else if (r_type == R_386_TLS_IE)
2986 unsigned int val;
2988 /* IE->LE transition:
2989 Originally it can be one of:
2990 movl foo, %eax
2991 movl foo, %reg
2992 addl foo, %reg
2993 We change it into:
2994 movl $foo, %eax
2995 movl $foo, %reg
2996 addl $foo, %reg. */
2997 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2998 if (val == 0xa1)
3000 /* movl foo, %eax. */
3001 bfd_put_8 (output_bfd, 0xb8,
3002 contents + rel->r_offset - 1);
3004 else
3006 unsigned int type;
3008 type = bfd_get_8 (input_bfd,
3009 contents + rel->r_offset - 2);
3010 switch (type)
3012 case 0x8b:
3013 /* movl */
3014 bfd_put_8 (output_bfd, 0xc7,
3015 contents + rel->r_offset - 2);
3016 bfd_put_8 (output_bfd,
3017 0xc0 | ((val >> 3) & 7),
3018 contents + rel->r_offset - 1);
3019 break;
3020 case 0x03:
3021 /* addl */
3022 bfd_put_8 (output_bfd, 0x81,
3023 contents + rel->r_offset - 2);
3024 bfd_put_8 (output_bfd,
3025 0xc0 | ((val >> 3) & 7),
3026 contents + rel->r_offset - 1);
3027 break;
3028 default:
3029 BFD_FAIL ();
3030 break;
3033 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3034 contents + rel->r_offset);
3035 continue;
3037 else
3039 unsigned int val, type;
3041 /* {IE_32,GOTIE}->LE transition:
3042 Originally it can be one of:
3043 subl foo(%reg1), %reg2
3044 movl foo(%reg1), %reg2
3045 addl foo(%reg1), %reg2
3046 We change it into:
3047 subl $foo, %reg2
3048 movl $foo, %reg2 (6 byte form)
3049 addl $foo, %reg2. */
3050 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3051 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3052 if (type == 0x8b)
3054 /* movl */
3055 bfd_put_8 (output_bfd, 0xc7,
3056 contents + rel->r_offset - 2);
3057 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3058 contents + rel->r_offset - 1);
3060 else if (type == 0x2b)
3062 /* subl */
3063 bfd_put_8 (output_bfd, 0x81,
3064 contents + rel->r_offset - 2);
3065 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3066 contents + rel->r_offset - 1);
3068 else if (type == 0x03)
3070 /* addl */
3071 bfd_put_8 (output_bfd, 0x81,
3072 contents + rel->r_offset - 2);
3073 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3074 contents + rel->r_offset - 1);
3076 else
3077 BFD_FAIL ();
3078 if (r_type == R_386_TLS_GOTIE)
3079 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3080 contents + rel->r_offset);
3081 else
3082 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3083 contents + rel->r_offset);
3084 continue;
3088 if (htab->elf.sgot == NULL)
3089 abort ();
3091 if (h != NULL)
3093 off = h->got.offset;
3094 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3096 else
3098 if (local_got_offsets == NULL)
3099 abort ();
3101 off = local_got_offsets[r_symndx];
3102 offplt = local_tlsdesc_gotents[r_symndx];
3105 if ((off & 1) != 0)
3106 off &= ~1;
3107 else
3109 Elf_Internal_Rela outrel;
3110 int dr_type;
3111 asection *sreloc;
3113 if (htab->elf.srelgot == NULL)
3114 abort ();
3116 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3118 if (GOT_TLS_GDESC_P (tls_type))
3120 bfd_byte *loc;
3121 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3122 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3123 <= htab->elf.sgotplt->size);
3124 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3125 + htab->elf.sgotplt->output_offset
3126 + offplt
3127 + htab->sgotplt_jump_table_size);
3128 sreloc = htab->elf.srelplt;
3129 loc = sreloc->contents;
3130 loc += (htab->next_tls_desc_index++
3131 * sizeof (Elf32_External_Rel));
3132 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3133 <= sreloc->contents + sreloc->size);
3134 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3135 if (indx == 0)
3137 BFD_ASSERT (! unresolved_reloc);
3138 bfd_put_32 (output_bfd,
3139 relocation - _bfd_x86_elf_dtpoff_base (info),
3140 htab->elf.sgotplt->contents + offplt
3141 + htab->sgotplt_jump_table_size + 4);
3143 else
3145 bfd_put_32 (output_bfd, 0,
3146 htab->elf.sgotplt->contents + offplt
3147 + htab->sgotplt_jump_table_size + 4);
3151 sreloc = htab->elf.srelgot;
3153 outrel.r_offset = (htab->elf.sgot->output_section->vma
3154 + htab->elf.sgot->output_offset + off);
3156 if (GOT_TLS_GD_P (tls_type))
3157 dr_type = R_386_TLS_DTPMOD32;
3158 else if (GOT_TLS_GDESC_P (tls_type))
3159 goto dr_done;
3160 else if (tls_type == GOT_TLS_IE_POS)
3161 dr_type = R_386_TLS_TPOFF;
3162 else
3163 dr_type = R_386_TLS_TPOFF32;
3165 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3166 bfd_put_32 (output_bfd,
3167 relocation - _bfd_x86_elf_dtpoff_base (info),
3168 htab->elf.sgot->contents + off);
3169 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3170 bfd_put_32 (output_bfd,
3171 _bfd_x86_elf_dtpoff_base (info) - relocation,
3172 htab->elf.sgot->contents + off);
3173 else if (dr_type != R_386_TLS_DESC)
3174 bfd_put_32 (output_bfd, 0,
3175 htab->elf.sgot->contents + off);
3176 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3178 elf_append_rel (output_bfd, sreloc, &outrel);
3180 if (GOT_TLS_GD_P (tls_type))
3182 if (indx == 0)
3184 BFD_ASSERT (! unresolved_reloc);
3185 bfd_put_32 (output_bfd,
3186 relocation - _bfd_x86_elf_dtpoff_base (info),
3187 htab->elf.sgot->contents + off + 4);
3189 else
3191 bfd_put_32 (output_bfd, 0,
3192 htab->elf.sgot->contents + off + 4);
3193 outrel.r_info = ELF32_R_INFO (indx,
3194 R_386_TLS_DTPOFF32);
3195 outrel.r_offset += 4;
3196 elf_append_rel (output_bfd, sreloc, &outrel);
3199 else if (tls_type == GOT_TLS_IE_BOTH)
3201 bfd_put_32 (output_bfd,
3202 (indx == 0
3203 ? relocation - _bfd_x86_elf_dtpoff_base (info)
3204 : 0),
3205 htab->elf.sgot->contents + off + 4);
3206 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3207 outrel.r_offset += 4;
3208 elf_append_rel (output_bfd, sreloc, &outrel);
3211 dr_done:
3212 if (h != NULL)
3213 h->got.offset |= 1;
3214 else
3215 local_got_offsets[r_symndx] |= 1;
3218 if (off >= (bfd_vma) -2
3219 && ! GOT_TLS_GDESC_P (tls_type))
3220 abort ();
3221 if (r_type_tls == R_386_TLS_GOTDESC
3222 || r_type_tls == R_386_TLS_DESC_CALL)
3224 relocation = htab->sgotplt_jump_table_size + offplt;
3225 unresolved_reloc = false;
3227 else if (r_type_tls == r_type)
3229 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3230 + htab->elf.sgotplt->output_offset;
3231 relocation = htab->elf.sgot->output_section->vma
3232 + htab->elf.sgot->output_offset + off - g_o_t;
3233 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3234 && tls_type == GOT_TLS_IE_BOTH)
3235 relocation += 4;
3236 if (r_type == R_386_TLS_IE)
3237 relocation += g_o_t;
3238 unresolved_reloc = false;
3240 else if (r_type == R_386_TLS_GD)
3242 unsigned int val, type;
3243 bfd_vma roff;
3245 /* GD->IE transition. */
3246 type = *(contents + rel->r_offset - 2);
3247 val = *(contents + rel->r_offset - 1);
3248 if (type == 0x04)
3250 /* Change
3251 leal foo@tlsgd(,%ebx,1), %eax
3252 call ___tls_get_addr@PLT
3253 into:
3254 movl %gs:0, %eax
3255 subl $foo@gottpoff(%ebx), %eax. */
3256 val >>= 3;
3257 roff = rel->r_offset - 3;
3259 else
3261 /* Change
3262 leal foo@tlsgd(%ebx), %eax
3263 call ___tls_get_addr@PLT
3266 leal foo@tlsgd(%reg), %eax
3267 call *___tls_get_addr@GOT(%reg)
3268 which may be converted to
3269 addr32 call ___tls_get_addr
3270 into:
3271 movl %gs:0, %eax;
3272 subl $foo@gottpoff(%reg), %eax. */
3273 roff = rel->r_offset - 2;
3275 memcpy (contents + roff,
3276 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3277 contents[roff + 7] = 0x80 | (val & 7);
3278 /* If foo is used only with foo@gotntpoff(%reg) and
3279 foo@indntpoff, but not with foo@gottpoff(%reg), change
3280 subl $foo@gottpoff(%reg), %eax
3281 into:
3282 addl $foo@gotntpoff(%reg), %eax. */
3283 if (tls_type == GOT_TLS_IE_POS)
3284 contents[roff + 6] = 0x03;
3285 bfd_put_32 (output_bfd,
3286 htab->elf.sgot->output_section->vma
3287 + htab->elf.sgot->output_offset + off
3288 - htab->elf.sgotplt->output_section->vma
3289 - htab->elf.sgotplt->output_offset,
3290 contents + roff + 8);
3291 /* Skip R_386_PLT32 and R_386_GOT32X. */
3292 rel++;
3293 wrel++;
3294 continue;
3296 else if (r_type == R_386_TLS_GOTDESC)
3298 /* GDesc -> IE transition.
3299 It's originally something like:
3300 leal x@tlsdesc(%ebx), %eax
3302 Change it to:
3303 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3305 movl x@gottpoff(%ebx), %eax # before negl %eax
3307 Registers other than %eax may be set up here. */
3309 bfd_vma roff;
3311 /* First, make sure it's a leal adding ebx to a 32-bit
3312 offset into any register, although it's probably
3313 almost always going to be eax. */
3314 roff = rel->r_offset;
3316 /* Now modify the instruction as appropriate. */
3317 /* To turn a leal into a movl in the form we use it, it
3318 suffices to change the first byte from 0x8d to 0x8b.
3319 aoliva FIXME: should we decide to keep the leal, all
3320 we have to do is remove the statement below, and
3321 adjust the relaxation of R_386_TLS_DESC_CALL. */
3322 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3324 if (tls_type == GOT_TLS_IE_BOTH)
3325 off += 4;
3327 bfd_put_32 (output_bfd,
3328 htab->elf.sgot->output_section->vma
3329 + htab->elf.sgot->output_offset + off
3330 - htab->elf.sgotplt->output_section->vma
3331 - htab->elf.sgotplt->output_offset,
3332 contents + roff);
3333 continue;
3335 else if (r_type == R_386_TLS_DESC_CALL)
3337 /* GDesc -> IE transition.
3338 It's originally:
3339 call *(%eax)
3341 Change it to:
3342 xchg %ax,%ax
3344 negl %eax
3345 depending on how we transformed the TLS_GOTDESC above.
3348 bfd_vma roff;
3350 roff = rel->r_offset;
3352 /* Now modify the instruction as appropriate. */
3353 if (tls_type != GOT_TLS_IE_NEG)
3355 /* xchg %ax,%ax */
3356 bfd_put_8 (output_bfd, 0x66, contents + roff);
3357 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3359 else
3361 /* negl %eax */
3362 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3363 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3366 continue;
3368 else
3369 BFD_ASSERT (false);
3370 break;
3372 case R_386_TLS_LDM:
3373 if (! elf_i386_tls_transition (info, input_bfd,
3374 input_section, contents,
3375 symtab_hdr, sym_hashes,
3376 &r_type, GOT_UNKNOWN, rel,
3377 relend, h, sym, true))
3378 return false;
3380 if (r_type != R_386_TLS_LDM)
3382 /* LD->LE transition. Change
3383 leal foo@tlsldm(%ebx) %eax
3384 call ___tls_get_addr@PLT
3385 into:
3386 movl %gs:0, %eax
3388 leal 0(%esi,1), %esi
3389 or change
3390 leal foo@tlsldm(%reg) %eax
3391 call *___tls_get_addr@GOT(%reg)
3392 which may be converted to
3393 addr32 call ___tls_get_addr
3394 into:
3395 movl %gs:0, %eax
3396 leal 0(%esi), %esi */
3397 expected_tls_le = htab->elf.target_os == is_solaris
3398 ? R_386_TLS_LE : R_386_TLS_LE_32;
3399 BFD_ASSERT (r_type == expected_tls_le);
3400 if (*(contents + rel->r_offset + 4) == 0xff
3401 || *(contents + rel->r_offset + 4) == 0x67)
3402 memcpy (contents + rel->r_offset - 2,
3403 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3404 else
3405 memcpy (contents + rel->r_offset - 2,
3406 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3407 /* Skip R_386_PC32/R_386_PLT32. */
3408 rel++;
3409 wrel++;
3410 continue;
3413 if (htab->elf.sgot == NULL)
3414 abort ();
3416 off = htab->tls_ld_or_ldm_got.offset;
3417 if (off & 1)
3418 off &= ~1;
3419 else
3421 Elf_Internal_Rela outrel;
3423 if (htab->elf.srelgot == NULL)
3424 abort ();
3426 outrel.r_offset = (htab->elf.sgot->output_section->vma
3427 + htab->elf.sgot->output_offset + off);
3429 bfd_put_32 (output_bfd, 0,
3430 htab->elf.sgot->contents + off);
3431 bfd_put_32 (output_bfd, 0,
3432 htab->elf.sgot->contents + off + 4);
3433 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3434 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3435 htab->tls_ld_or_ldm_got.offset |= 1;
3437 relocation = htab->elf.sgot->output_section->vma
3438 + htab->elf.sgot->output_offset + off
3439 - htab->elf.sgotplt->output_section->vma
3440 - htab->elf.sgotplt->output_offset;
3441 unresolved_reloc = false;
3442 break;
3444 case R_386_TLS_LDO_32:
3445 if (!bfd_link_executable (info)
3446 || (input_section->flags & SEC_CODE) == 0)
3447 relocation -= _bfd_x86_elf_dtpoff_base (info);
3448 else
3449 /* When converting LDO to LE, we must negate. */
3450 relocation = -elf_i386_tpoff (info, relocation);
3451 break;
3453 case R_386_TLS_LE_32:
3454 case R_386_TLS_LE:
3455 if (!bfd_link_executable (info))
3457 Elf_Internal_Rela outrel;
3458 asection *sreloc;
3460 outrel.r_offset = rel->r_offset
3461 + input_section->output_section->vma
3462 + input_section->output_offset;
3463 if (h != NULL && h->dynindx != -1)
3464 indx = h->dynindx;
3465 else
3466 indx = 0;
3467 if (r_type == R_386_TLS_LE_32)
3468 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3469 else
3470 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3471 sreloc = elf_section_data (input_section)->sreloc;
3472 if (sreloc == NULL)
3473 abort ();
3474 elf_append_rel (output_bfd, sreloc, &outrel);
3475 if (indx)
3476 continue;
3477 else if (r_type == R_386_TLS_LE_32)
3478 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3479 else
3480 relocation -= _bfd_x86_elf_dtpoff_base (info);
3482 else if (r_type == R_386_TLS_LE_32)
3483 relocation = elf_i386_tpoff (info, relocation);
3484 else
3485 relocation = -elf_i386_tpoff (info, relocation);
3486 break;
3488 default:
3489 break;
3492 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3493 because such sections are not SEC_ALLOC and thus ld.so will
3494 not process them. */
3495 if (unresolved_reloc
3496 && !((input_section->flags & SEC_DEBUGGING) != 0
3497 && h->def_dynamic)
3498 && _bfd_elf_section_offset (output_bfd, info, input_section,
3499 rel->r_offset) != (bfd_vma) -1)
3501 _bfd_error_handler
3502 /* xgettext:c-format */
3503 (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
3504 input_bfd,
3505 input_section,
3506 (uint64_t) rel->r_offset,
3507 howto->name,
3508 h->root.root.string);
3509 return false;
3512 do_relocation:
3513 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3514 contents, rel->r_offset,
3515 relocation, 0);
3517 check_relocation_error:
3518 if (r != bfd_reloc_ok)
3520 const char *name;
3522 if (h != NULL)
3523 name = h->root.root.string;
3524 else
3526 name = bfd_elf_string_from_elf_section (input_bfd,
3527 symtab_hdr->sh_link,
3528 sym->st_name);
3529 if (name == NULL)
3530 return false;
3531 if (*name == '\0')
3532 name = bfd_section_name (sec);
3535 if (r == bfd_reloc_overflow)
3536 (*info->callbacks->reloc_overflow)
3537 (info, (h ? &h->root : NULL), name, howto->name,
3538 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3539 else
3541 _bfd_error_handler
3542 /* xgettext:c-format */
3543 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3544 input_bfd, input_section,
3545 (uint64_t) rel->r_offset, name, (int) r);
3546 return false;
3550 if (wrel != rel)
3551 *wrel = *rel;
3554 if (wrel != rel)
3556 Elf_Internal_Shdr *rel_hdr;
3557 size_t deleted = rel - wrel;
3559 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3560 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3561 if (rel_hdr->sh_size == 0)
3563 /* It is too late to remove an empty reloc section. Leave
3564 one NONE reloc.
3565 ??? What is wrong with an empty section??? */
3566 rel_hdr->sh_size = rel_hdr->sh_entsize;
3567 deleted -= 1;
3569 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3570 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3571 input_section->reloc_count -= deleted;
3574 return true;
3577 /* Finish up dynamic symbol handling. We set the contents of various
3578 dynamic sections here. */
3580 static bool
3581 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3582 struct bfd_link_info *info,
3583 struct elf_link_hash_entry *h,
3584 Elf_Internal_Sym *sym)
3586 struct elf_x86_link_hash_table *htab;
3587 unsigned plt_entry_size;
3588 struct elf_x86_link_hash_entry *eh;
3589 bool local_undefweak;
3590 bool use_plt_second;
3592 htab = elf_x86_hash_table (info, I386_ELF_DATA);
3594 plt_entry_size = htab->plt.plt_entry_size;
3596 /* Use the second PLT section only if there is .plt section. */
3597 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3599 eh = (struct elf_x86_link_hash_entry *) h;
3600 if (eh->no_finish_dynamic_symbol)
3601 abort ();
3603 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3604 resolved undefined weak symbols in executable so that their
3605 references have value 0 at run-time. */
3606 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3608 if (h->plt.offset != (bfd_vma) -1)
3610 bfd_vma plt_index, plt_offset;
3611 bfd_vma got_offset;
3612 Elf_Internal_Rela rel;
3613 bfd_byte *loc;
3614 asection *plt, *resolved_plt, *gotplt, *relplt;
3616 /* When building a static executable, use .iplt, .igot.plt and
3617 .rel.iplt sections for STT_GNU_IFUNC symbols. */
3618 if (htab->elf.splt != NULL)
3620 plt = htab->elf.splt;
3621 gotplt = htab->elf.sgotplt;
3622 relplt = htab->elf.srelplt;
3624 else
3626 plt = htab->elf.iplt;
3627 gotplt = htab->elf.igotplt;
3628 relplt = htab->elf.irelplt;
3631 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3633 /* Get the index in the procedure linkage table which
3634 corresponds to this symbol. This is the index of this symbol
3635 in all the symbols for which we are making plt entries. The
3636 first entry in the procedure linkage table is reserved.
3638 Get the offset into the .got table of the entry that
3639 corresponds to this function. Each .got entry is 4 bytes.
3640 The first three are reserved.
3642 For static executables, we don't reserve anything. */
3644 if (plt == htab->elf.splt)
3646 got_offset = (h->plt.offset / plt_entry_size
3647 - htab->plt.has_plt0);
3648 got_offset = (got_offset + 3) * 4;
3650 else
3652 got_offset = h->plt.offset / plt_entry_size;
3653 got_offset = got_offset * 4;
3656 /* Fill in the entry in the procedure linkage table and update
3657 the first slot. */
3658 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3659 plt_entry_size);
3661 if (use_plt_second)
3663 const bfd_byte *plt_entry;
3664 if (bfd_link_pic (info))
3665 plt_entry = htab->non_lazy_plt->pic_plt_entry;
3666 else
3667 plt_entry = htab->non_lazy_plt->plt_entry;
3668 memcpy (htab->plt_second->contents + eh->plt_second.offset,
3669 plt_entry, htab->non_lazy_plt->plt_entry_size);
3671 resolved_plt = htab->plt_second;
3672 plt_offset = eh->plt_second.offset;
3674 else
3676 resolved_plt = plt;
3677 plt_offset = h->plt.offset;
3680 if (! bfd_link_pic (info))
3682 bfd_put_32 (output_bfd,
3683 (gotplt->output_section->vma
3684 + gotplt->output_offset
3685 + got_offset),
3686 resolved_plt->contents + plt_offset
3687 + htab->plt.plt_got_offset);
3689 if (htab->elf.target_os == is_vxworks)
3691 int s, k, reloc_index;
3693 /* Create the R_386_32 relocation referencing the GOT
3694 for this PLT entry. */
3696 /* S: Current slot number (zero-based). */
3697 s = ((h->plt.offset - htab->plt.plt_entry_size)
3698 / htab->plt.plt_entry_size);
3699 /* K: Number of relocations for PLTResolve. */
3700 if (bfd_link_pic (info))
3701 k = PLTRESOLVE_RELOCS_SHLIB;
3702 else
3703 k = PLTRESOLVE_RELOCS;
3704 /* Skip the PLTresolve relocations, and the relocations for
3705 the other PLT slots. */
3706 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3707 loc = (htab->srelplt2->contents + reloc_index
3708 * sizeof (Elf32_External_Rel));
3710 rel.r_offset = (plt->output_section->vma
3711 + plt->output_offset
3712 + h->plt.offset + 2),
3713 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3714 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3716 /* Create the R_386_32 relocation referencing the beginning of
3717 the PLT for this GOT entry. */
3718 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3719 + htab->elf.sgotplt->output_offset
3720 + got_offset);
3721 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3722 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3723 loc + sizeof (Elf32_External_Rel));
3726 else
3728 bfd_put_32 (output_bfd, got_offset,
3729 resolved_plt->contents + plt_offset
3730 + htab->plt.plt_got_offset);
3733 /* Fill in the entry in the global offset table. Leave the entry
3734 as zero for undefined weak symbol in PIE. No PLT relocation
3735 against undefined weak symbol in PIE. */
3736 if (!local_undefweak)
3738 if (htab->plt.has_plt0)
3739 bfd_put_32 (output_bfd,
3740 (plt->output_section->vma
3741 + plt->output_offset
3742 + h->plt.offset
3743 + htab->lazy_plt->plt_lazy_offset),
3744 gotplt->contents + got_offset);
3746 /* Fill in the entry in the .rel.plt section. */
3747 rel.r_offset = (gotplt->output_section->vma
3748 + gotplt->output_offset
3749 + got_offset);
3750 if (PLT_LOCAL_IFUNC_P (info, h))
3752 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3753 h->root.root.string,
3754 h->root.u.def.section->owner);
3756 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3757 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3758 in the .got.plt section. */
3759 bfd_put_32 (output_bfd,
3760 (h->root.u.def.value
3761 + h->root.u.def.section->output_section->vma
3762 + h->root.u.def.section->output_offset),
3763 gotplt->contents + got_offset);
3764 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3766 if (htab->params->report_relative_reloc)
3767 _bfd_x86_elf_link_report_relative_reloc
3768 (info, relplt, h, sym, "R_386_IRELATIVE", &rel);
3770 /* R_386_IRELATIVE comes last. */
3771 plt_index = htab->next_irelative_index--;
3773 else
3775 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3776 plt_index = htab->next_jump_slot_index++;
3779 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3780 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3782 /* Don't fill the second and third slots in PLT entry for
3783 static executables nor without PLT0. */
3784 if (plt == htab->elf.splt && htab->plt.has_plt0)
3786 bfd_put_32 (output_bfd,
3787 plt_index * sizeof (Elf32_External_Rel),
3788 plt->contents + h->plt.offset
3789 + htab->lazy_plt->plt_reloc_offset);
3790 bfd_put_32 (output_bfd,
3791 - (h->plt.offset
3792 + htab->lazy_plt->plt_plt_offset + 4),
3793 (plt->contents + h->plt.offset
3794 + htab->lazy_plt->plt_plt_offset));
3798 else if (eh->plt_got.offset != (bfd_vma) -1)
3800 bfd_vma got_offset, plt_offset;
3801 asection *plt, *got, *gotplt;
3802 const bfd_byte *got_plt_entry;
3804 /* Set the entry in the GOT procedure linkage table. */
3805 plt = htab->plt_got;
3806 got = htab->elf.sgot;
3807 gotplt = htab->elf.sgotplt;
3808 got_offset = h->got.offset;
3810 if (got_offset == (bfd_vma) -1
3811 || plt == NULL
3812 || got == NULL
3813 || gotplt == NULL)
3814 abort ();
3816 /* Fill in the entry in the GOT procedure linkage table. */
3817 if (! bfd_link_pic (info))
3819 got_plt_entry = htab->non_lazy_plt->plt_entry;
3820 got_offset += got->output_section->vma + got->output_offset;
3822 else
3824 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3825 got_offset += (got->output_section->vma
3826 + got->output_offset
3827 - gotplt->output_section->vma
3828 - gotplt->output_offset);
3831 plt_offset = eh->plt_got.offset;
3832 memcpy (plt->contents + plt_offset, got_plt_entry,
3833 htab->non_lazy_plt->plt_entry_size);
3834 bfd_put_32 (output_bfd, got_offset,
3835 (plt->contents + plt_offset
3836 + htab->non_lazy_plt->plt_got_offset));
3839 if (!local_undefweak
3840 && !h->def_regular
3841 && (h->plt.offset != (bfd_vma) -1
3842 || eh->plt_got.offset != (bfd_vma) -1))
3844 /* Mark the symbol as undefined, rather than as defined in
3845 the .plt section. Leave the value if there were any
3846 relocations where pointer equality matters (this is a clue
3847 for the dynamic linker, to make function pointer
3848 comparisons work between an application and shared
3849 library), otherwise set it to zero. If a function is only
3850 called from a binary, there is no need to slow down
3851 shared libraries because of that. */
3852 sym->st_shndx = SHN_UNDEF;
3853 if (!h->pointer_equality_needed)
3854 sym->st_value = 0;
3857 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3859 /* Don't generate dynamic GOT relocation against undefined weak
3860 symbol in executable. */
3861 if (h->got.offset != (bfd_vma) -1
3862 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3863 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3864 && !local_undefweak)
3866 Elf_Internal_Rela rel;
3867 asection *relgot = htab->elf.srelgot;
3868 const char *relative_reloc_name = NULL;
3869 bool generate_dynamic_reloc = true;
3871 /* This symbol has an entry in the global offset table. Set it
3872 up. */
3874 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3875 abort ();
3877 rel.r_offset = (htab->elf.sgot->output_section->vma
3878 + htab->elf.sgot->output_offset
3879 + (h->got.offset & ~(bfd_vma) 1));
3881 /* If this is a static link, or it is a -Bsymbolic link and the
3882 symbol is defined locally or was forced to be local because
3883 of a version file, we just want to emit a RELATIVE reloc.
3884 The entry in the global offset table will already have been
3885 initialized in the relocate_section function. */
3886 if (h->def_regular
3887 && h->type == STT_GNU_IFUNC)
3889 if (h->plt.offset == (bfd_vma) -1)
3891 /* STT_GNU_IFUNC is referenced without PLT. */
3892 if (htab->elf.splt == NULL)
3894 /* use .rel[a].iplt section to store .got relocations
3895 in static executable. */
3896 relgot = htab->elf.irelplt;
3898 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3900 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3901 h->root.root.string,
3902 h->root.u.def.section->owner);
3904 bfd_put_32 (output_bfd,
3905 (h->root.u.def.value
3906 + h->root.u.def.section->output_section->vma
3907 + h->root.u.def.section->output_offset),
3908 htab->elf.sgot->contents + h->got.offset);
3909 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3910 relative_reloc_name = "R_386_IRELATIVE";
3912 else
3913 goto do_glob_dat;
3915 else if (bfd_link_pic (info))
3917 /* Generate R_386_GLOB_DAT. */
3918 goto do_glob_dat;
3920 else
3922 asection *plt;
3923 bfd_vma plt_offset;
3925 if (!h->pointer_equality_needed)
3926 abort ();
3928 /* For non-shared object, we can't use .got.plt, which
3929 contains the real function addres if we need pointer
3930 equality. We load the GOT entry with the PLT entry. */
3931 if (htab->plt_second != NULL)
3933 plt = htab->plt_second;
3934 plt_offset = eh->plt_second.offset;
3936 else
3938 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3939 plt_offset = h->plt.offset;
3941 bfd_put_32 (output_bfd,
3942 (plt->output_section->vma
3943 + plt->output_offset + plt_offset),
3944 htab->elf.sgot->contents + h->got.offset);
3945 return true;
3948 else if (bfd_link_pic (info)
3949 && SYMBOL_REFERENCES_LOCAL_P (info, h))
3951 BFD_ASSERT((h->got.offset & 1) != 0);
3952 if (info->enable_dt_relr)
3953 generate_dynamic_reloc = false;
3954 else
3956 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3957 relative_reloc_name = "R_386_RELATIVE";
3960 else
3962 BFD_ASSERT((h->got.offset & 1) == 0);
3963 do_glob_dat:
3964 bfd_put_32 (output_bfd, (bfd_vma) 0,
3965 htab->elf.sgot->contents + h->got.offset);
3966 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3969 if (generate_dynamic_reloc)
3971 if (relative_reloc_name != NULL
3972 && htab->params->report_relative_reloc)
3973 _bfd_x86_elf_link_report_relative_reloc
3974 (info, relgot, h, sym, relative_reloc_name, &rel);
3976 elf_append_rel (output_bfd, relgot, &rel);
3980 if (h->needs_copy)
3982 Elf_Internal_Rela rel;
3983 asection *s;
3985 /* This symbol needs a copy reloc. Set it up. */
3986 VERIFY_COPY_RELOC (h, htab)
3988 rel.r_offset = (h->root.u.def.value
3989 + h->root.u.def.section->output_section->vma
3990 + h->root.u.def.section->output_offset);
3991 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3992 if (h->root.u.def.section == htab->elf.sdynrelro)
3993 s = htab->elf.sreldynrelro;
3994 else
3995 s = htab->elf.srelbss;
3996 elf_append_rel (output_bfd, s, &rel);
3999 return true;
4002 /* Finish up local dynamic symbol handling. We set the contents of
4003 various dynamic sections here. */
4005 static int
4006 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4008 struct elf_link_hash_entry *h
4009 = (struct elf_link_hash_entry *) *slot;
4010 struct bfd_link_info *info
4011 = (struct bfd_link_info *) inf;
4013 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4014 h, NULL);
4017 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4018 here since undefined weak symbol may not be dynamic and may not be
4019 called for elf_i386_finish_dynamic_symbol. */
4021 static bool
4022 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4023 void *inf)
4025 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4026 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4028 if (h->root.type != bfd_link_hash_undefweak
4029 || h->dynindx != -1)
4030 return true;
4032 return elf_i386_finish_dynamic_symbol (info->output_bfd,
4033 info, h, NULL);
4036 /* Used to decide how to sort relocs in an optimal manner for the
4037 dynamic linker, before writing them out. */
4039 static enum elf_reloc_type_class
4040 elf_i386_reloc_type_class (const struct bfd_link_info *info,
4041 const asection *rel_sec ATTRIBUTE_UNUSED,
4042 const Elf_Internal_Rela *rela)
4044 bfd *abfd = info->output_bfd;
4045 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4046 struct elf_link_hash_table *htab = elf_hash_table (info);
4048 if (htab->dynsym != NULL
4049 && htab->dynsym->contents != NULL)
4051 /* Check relocation against STT_GNU_IFUNC symbol if there are
4052 dynamic symbols. */
4053 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4054 if (r_symndx != STN_UNDEF)
4056 Elf_Internal_Sym sym;
4057 if (!bed->s->swap_symbol_in (abfd,
4058 (htab->dynsym->contents
4059 + r_symndx * sizeof (Elf32_External_Sym)),
4060 0, &sym))
4061 abort ();
4063 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4064 return reloc_class_ifunc;
4068 switch (ELF32_R_TYPE (rela->r_info))
4070 case R_386_IRELATIVE:
4071 return reloc_class_ifunc;
4072 case R_386_RELATIVE:
4073 return reloc_class_relative;
4074 case R_386_JUMP_SLOT:
4075 return reloc_class_plt;
4076 case R_386_COPY:
4077 return reloc_class_copy;
4078 default:
4079 return reloc_class_normal;
4083 /* Finish up the dynamic sections. */
4085 static bool
4086 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4087 struct bfd_link_info *info)
4089 struct elf_x86_link_hash_table *htab;
4091 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4092 if (htab == NULL)
4093 return false;
4095 if (!htab->elf.dynamic_sections_created)
4096 return true;
4098 if (htab->elf.splt && htab->elf.splt->size > 0)
4100 if (bfd_is_abs_section (htab->elf.splt->output_section))
4102 info->callbacks->einfo
4103 (_("%F%P: discarded output section: `%pA'\n"),
4104 htab->elf.splt);
4105 return false;
4108 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4109 really seem like the right value. */
4110 elf_section_data (htab->elf.splt->output_section)
4111 ->this_hdr.sh_entsize = 4;
4113 if (htab->plt.has_plt0)
4115 /* Fill in the special first entry in the procedure linkage
4116 table. */
4117 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4118 htab->lazy_plt->plt0_entry_size);
4119 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4120 htab->plt0_pad_byte,
4121 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4122 if (!bfd_link_pic (info))
4124 bfd_put_32 (output_bfd,
4125 (htab->elf.sgotplt->output_section->vma
4126 + htab->elf.sgotplt->output_offset
4127 + 4),
4128 htab->elf.splt->contents
4129 + htab->lazy_plt->plt0_got1_offset);
4130 bfd_put_32 (output_bfd,
4131 (htab->elf.sgotplt->output_section->vma
4132 + htab->elf.sgotplt->output_offset
4133 + 8),
4134 htab->elf.splt->contents
4135 + htab->lazy_plt->plt0_got2_offset);
4137 if (htab->elf.target_os == is_vxworks)
4139 Elf_Internal_Rela rel;
4140 int num_plts = (htab->elf.splt->size
4141 / htab->plt.plt_entry_size) - 1;
4142 unsigned char *p;
4143 asection *srelplt2 = htab->srelplt2;
4145 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4146 + 4. On IA32 we use REL relocations so the
4147 addend goes in the PLT directly. */
4148 rel.r_offset = (htab->elf.splt->output_section->vma
4149 + htab->elf.splt->output_offset
4150 + htab->lazy_plt->plt0_got1_offset);
4151 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4152 R_386_32);
4153 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4154 srelplt2->contents);
4155 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4156 + 8. */
4157 rel.r_offset = (htab->elf.splt->output_section->vma
4158 + htab->elf.splt->output_offset
4159 + htab->lazy_plt->plt0_got2_offset);
4160 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4161 R_386_32);
4162 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4163 srelplt2->contents +
4164 sizeof (Elf32_External_Rel));
4165 /* Correct the .rel.plt.unloaded relocations. */
4166 p = srelplt2->contents;
4167 if (bfd_link_pic (info))
4168 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4169 else
4170 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4172 for (; num_plts; num_plts--)
4174 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4175 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4176 R_386_32);
4177 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4178 p += sizeof (Elf32_External_Rel);
4180 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4181 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4182 R_386_32);
4183 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4184 p += sizeof (Elf32_External_Rel);
4191 /* Fill PLT entries for undefined weak symbols in PIE. */
4192 if (bfd_link_pie (info))
4193 bfd_hash_traverse (&info->hash->table,
4194 elf_i386_pie_finish_undefweak_symbol,
4195 info);
4197 return true;
4200 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4201 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4202 It has to be done before elf_link_sort_relocs is called so that
4203 dynamic relocations are properly sorted. */
4205 static bool
4206 elf_i386_output_arch_local_syms
4207 (bfd *output_bfd ATTRIBUTE_UNUSED,
4208 struct bfd_link_info *info,
4209 void *flaginfo ATTRIBUTE_UNUSED,
4210 int (*func) (void *, const char *,
4211 Elf_Internal_Sym *,
4212 asection *,
4213 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4215 struct elf_x86_link_hash_table *htab
4216 = elf_x86_hash_table (info, I386_ELF_DATA);
4217 if (htab == NULL)
4218 return false;
4220 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4221 htab_traverse (htab->loc_hash_table,
4222 elf_i386_finish_local_dynamic_symbol,
4223 info);
4225 return true;
4228 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4229 dynamic relocations. */
4231 static long
4232 elf_i386_get_synthetic_symtab (bfd *abfd,
4233 long symcount ATTRIBUTE_UNUSED,
4234 asymbol **syms ATTRIBUTE_UNUSED,
4235 long dynsymcount,
4236 asymbol **dynsyms,
4237 asymbol **ret)
4239 long count, i, n;
4240 int j;
4241 bfd_byte *plt_contents;
4242 long relsize;
4243 const struct elf_x86_lazy_plt_layout *lazy_plt;
4244 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4245 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4246 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4247 asection *plt;
4248 bfd_vma got_addr;
4249 enum elf_x86_plt_type plt_type;
4250 struct elf_x86_plt plts[] =
4252 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4253 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4254 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4255 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4258 *ret = NULL;
4260 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4261 return 0;
4263 if (dynsymcount <= 0)
4264 return 0;
4266 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4267 if (relsize <= 0)
4268 return -1;
4270 non_lazy_plt = NULL;
4271 /* Silence GCC 6. */
4272 lazy_plt = NULL;
4273 non_lazy_ibt_plt = NULL;
4274 lazy_ibt_plt = NULL;
4275 switch (get_elf_backend_data (abfd)->target_os)
4277 case is_normal:
4278 case is_solaris:
4279 non_lazy_plt = &elf_i386_non_lazy_plt;
4280 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4281 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4282 /* Fall through */
4283 case is_vxworks:
4284 lazy_plt = &elf_i386_lazy_plt;
4285 break;
4286 default:
4287 abort ();
4290 got_addr = 0;
4292 count = 0;
4293 for (j = 0; plts[j].name != NULL; j++)
4295 plt = bfd_get_section_by_name (abfd, plts[j].name);
4296 if (plt == NULL || plt->size == 0)
4297 continue;
4299 /* Get the PLT section contents. */
4300 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4301 if (plt_contents == NULL)
4302 break;
4303 if (!bfd_get_section_contents (abfd, (asection *) plt,
4304 plt_contents, 0, plt->size))
4306 free (plt_contents);
4307 break;
4310 /* Check what kind of PLT it is. */
4311 plt_type = plt_unknown;
4312 if (plts[j].type == plt_unknown
4313 && (plt->size >= (lazy_plt->plt0_entry_size
4314 + lazy_plt->plt_entry_size)))
4316 /* Match lazy PLT first. */
4317 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4318 lazy_plt->plt0_got1_offset) == 0)
4320 /* The fist entry in the lazy IBT PLT is the same as the
4321 normal lazy PLT. */
4322 if (lazy_ibt_plt != NULL
4323 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4324 lazy_ibt_plt->plt_entry,
4325 lazy_ibt_plt->plt_got_offset) == 0))
4326 plt_type = plt_lazy | plt_second;
4327 else
4328 plt_type = plt_lazy;
4330 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4331 lazy_plt->plt0_got1_offset) == 0)
4333 /* The fist entry in the PIC lazy IBT PLT is the same as
4334 the normal PIC lazy PLT. */
4335 if (lazy_ibt_plt != NULL
4336 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4337 lazy_ibt_plt->pic_plt_entry,
4338 lazy_ibt_plt->plt_got_offset) == 0))
4339 plt_type = plt_lazy | plt_pic | plt_second;
4340 else
4341 plt_type = plt_lazy | plt_pic;
4345 if (non_lazy_plt != NULL
4346 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4347 && plt->size >= non_lazy_plt->plt_entry_size)
4349 /* Match non-lazy PLT. */
4350 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4351 non_lazy_plt->plt_got_offset) == 0)
4352 plt_type = plt_non_lazy;
4353 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4354 non_lazy_plt->plt_got_offset) == 0)
4355 plt_type = plt_pic;
4358 if ((non_lazy_ibt_plt != NULL)
4359 && (plt_type == plt_unknown || plt_type == plt_second)
4360 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4362 if (memcmp (plt_contents,
4363 non_lazy_ibt_plt->plt_entry,
4364 non_lazy_ibt_plt->plt_got_offset) == 0)
4366 /* Match IBT PLT. */
4367 plt_type = plt_second;
4368 non_lazy_plt = non_lazy_ibt_plt;
4370 else if (memcmp (plt_contents,
4371 non_lazy_ibt_plt->pic_plt_entry,
4372 non_lazy_ibt_plt->plt_got_offset) == 0)
4374 /* Match PIC IBT PLT. */
4375 plt_type = plt_second | plt_pic;
4376 non_lazy_plt = non_lazy_ibt_plt;
4380 if (plt_type == plt_unknown)
4382 free (plt_contents);
4383 continue;
4386 plts[j].sec = plt;
4387 plts[j].type = plt_type;
4389 if ((plt_type & plt_lazy))
4391 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4392 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4393 /* Skip PLT0 in lazy PLT. */
4394 i = 1;
4396 else
4398 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4399 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4400 i = 0;
4403 /* Skip lazy PLT when the second PLT is used. */
4404 if ((plt_type & (plt_lazy | plt_second))
4405 == (plt_lazy | plt_second))
4406 plts[j].count = 0;
4407 else
4409 n = plt->size / plts[j].plt_entry_size;
4410 plts[j].count = n;
4411 count += n - i;
4414 plts[j].contents = plt_contents;
4416 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4417 if ((plt_type & plt_pic))
4418 got_addr = (bfd_vma) -1;
4421 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4422 got_addr, plts, dynsyms,
4423 ret);
4426 /* Set up i386 GNU properties. Return the first relocatable ELF input
4427 with GNU properties if found. Otherwise, return NULL. */
4429 static bfd *
4430 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4432 struct elf_x86_init_table init_table;
4434 switch (get_elf_backend_data (info->output_bfd)->target_os)
4436 case is_normal:
4437 case is_solaris:
4438 init_table.plt0_pad_byte = 0x0;
4439 init_table.lazy_plt = &elf_i386_lazy_plt;
4440 init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4441 init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4442 init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4443 break;
4444 case is_vxworks:
4445 init_table.plt0_pad_byte = 0x90;
4446 init_table.lazy_plt = &elf_i386_lazy_plt;
4447 init_table.non_lazy_plt = NULL;
4448 init_table.lazy_ibt_plt = NULL;
4449 init_table.non_lazy_ibt_plt = NULL;
4450 break;
4451 default:
4452 abort ();
4455 init_table.r_info = elf32_r_info;
4456 init_table.r_sym = elf32_r_sym;
4458 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4461 #define TARGET_LITTLE_SYM i386_elf32_vec
4462 #define TARGET_LITTLE_NAME "elf32-i386"
4463 #define ELF_ARCH bfd_arch_i386
4464 #define ELF_TARGET_ID I386_ELF_DATA
4465 #define ELF_MACHINE_CODE EM_386
4466 #define ELF_MAXPAGESIZE 0x1000
4468 #define elf_backend_can_gc_sections 1
4469 #define elf_backend_can_refcount 1
4470 #define elf_backend_want_got_plt 1
4471 #define elf_backend_plt_readonly 1
4472 #define elf_backend_want_plt_sym 0
4473 #define elf_backend_got_header_size 12
4474 #define elf_backend_plt_alignment 4
4475 #define elf_backend_dtrel_excludes_plt 1
4476 #define elf_backend_caches_rawsize 1
4477 #define elf_backend_want_dynrelro 1
4479 /* Support RELA for objdump of prelink objects. */
4480 #define elf_info_to_howto elf_i386_info_to_howto_rel
4481 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4483 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4484 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4485 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4486 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
4488 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4489 #define elf_backend_early_size_sections elf_i386_early_size_sections
4490 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
4491 #define elf_backend_fake_sections elf_i386_fake_sections
4492 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4493 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4494 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
4495 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4496 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4497 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4498 #define elf_backend_relocate_section elf_i386_relocate_section
4499 #define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
4500 #define elf_backend_hide_symbol _bfd_x86_elf_hide_symbol
4502 #define elf_backend_linux_prpsinfo32_ugid16 true
4504 #define elf32_bed elf32_i386_bed
4506 #include "elf32-target.h"
4508 /* FreeBSD support. */
4510 #undef TARGET_LITTLE_SYM
4511 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
4512 #undef TARGET_LITTLE_NAME
4513 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4514 #undef ELF_OSABI
4515 #define ELF_OSABI ELFOSABI_FREEBSD
4517 /* The kernel recognizes executables as valid only if they carry a
4518 "FreeBSD" label in the ELF header. So we put this label on all
4519 executables and (for simplicity) also all other object files. */
4521 static bool
4522 elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
4524 if (!_bfd_elf_init_file_header (abfd, info))
4525 return false;
4527 #ifdef OLD_FREEBSD_ABI_LABEL
4529 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4530 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4531 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4533 #endif
4534 return true;
4537 #undef elf_backend_init_file_header
4538 #define elf_backend_init_file_header elf_i386_fbsd_init_file_header
4539 #undef elf32_bed
4540 #define elf32_bed elf32_i386_fbsd_bed
4542 #undef elf_backend_add_symbol_hook
4544 #include "elf32-target.h"
4546 #undef elf_backend_init_file_header
4548 /* Solaris 2. */
4550 #undef TARGET_LITTLE_SYM
4551 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
4552 #undef TARGET_LITTLE_NAME
4553 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
4555 #undef ELF_TARGET_OS
4556 #define ELF_TARGET_OS is_solaris
4558 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4559 objects won't be recognized. */
4560 #undef ELF_OSABI
4562 #undef elf32_bed
4563 #define elf32_bed elf32_i386_sol2_bed
4565 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4566 boundary. */
4567 #undef elf_backend_static_tls_alignment
4568 #define elf_backend_static_tls_alignment 8
4570 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4572 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4573 File, p.63. */
4574 #undef elf_backend_want_plt_sym
4575 #define elf_backend_want_plt_sym 1
4577 #undef elf_backend_strtab_flags
4578 #define elf_backend_strtab_flags SHF_STRINGS
4580 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4581 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
4582 FALSE otherwise. ISECTION is the best guess matching section from the
4583 input bfd IBFD, but it might be NULL. */
4585 static bool
4586 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4587 bfd *obfd ATTRIBUTE_UNUSED,
4588 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4589 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4591 /* PR 19938: FIXME: Need to add code for setting the sh_info
4592 and sh_link fields of Solaris specific section types. */
4593 return false;
4595 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4596 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4598 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4600 The following values should be set:
4602 Type Link Info
4603 -----------------------------------------------------------------------------
4604 SHT_SUNW_ancillary The section header index of 0
4605 [0x6fffffee] the associated string table.
4607 SHT_SUNW_capinfo The section header index of For a dynamic object, the
4608 [0x6ffffff0] the associated symbol table. section header index of
4609 the associated
4610 SHT_SUNW_capchain table,
4611 otherwise 0.
4613 SHT_SUNW_symsort The section header index of 0
4614 [0x6ffffff1] the associated symbol table.
4616 SHT_SUNW_tlssort The section header index of 0
4617 [0x6ffffff2] the associated symbol table.
4619 SHT_SUNW_LDYNSYM The section header index of One greater than the
4620 [0x6ffffff3] the associated string table. symbol table index of the
4621 This index is the same string last local symbol,
4622 table used by the SHT_DYNSYM STB_LOCAL. Since
4623 section. SHT_SUNW_LDYNSYM only
4624 contains local symbols,
4625 sh_info is equivalent to
4626 the number of symbols in
4627 the table.
4629 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4630 [0x6ffffff5] the section header index of to named strings, the
4631 the associated section header index of
4632 SHT_SUNW_capinfo table, the associated string
4633 otherwise 0. table, otherwise 0.
4635 SHT_SUNW_move The section header index of 0
4636 [0x6ffffffa] the associated symbol table.
4638 SHT_SUNW_COMDAT 0 0
4639 [0x6ffffffb]
4641 SHT_SUNW_syminfo The section header index of The section header index
4642 [0x6ffffffc] the associated symbol table. of the associated
4643 .dynamic section.
4645 SHT_SUNW_verdef The section header index of The number of version
4646 [0x6ffffffd] the associated string table. definitions within the
4647 section.
4649 SHT_SUNW_verneed The section header index of The number of version
4650 [0x6ffffffe] the associated string table. dependencies within the
4651 section.
4653 SHT_SUNW_versym The section header index of 0
4654 [0x6fffffff] the associated symbol table. */
4657 #undef elf_backend_copy_special_section_fields
4658 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4660 #include "elf32-target.h"
4662 /* Intel MCU support. */
4664 static bool
4665 elf32_iamcu_elf_object_p (bfd *abfd)
4667 /* Set the right machine number for an IAMCU elf32 file. */
4668 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4669 return true;
4672 #undef TARGET_LITTLE_SYM
4673 #define TARGET_LITTLE_SYM iamcu_elf32_vec
4674 #undef TARGET_LITTLE_NAME
4675 #define TARGET_LITTLE_NAME "elf32-iamcu"
4676 #undef ELF_ARCH
4677 #define ELF_ARCH bfd_arch_iamcu
4679 #undef ELF_MACHINE_CODE
4680 #define ELF_MACHINE_CODE EM_IAMCU
4682 #undef ELF_TARGET_OS
4683 #undef ELF_OSABI
4685 #undef elf32_bed
4686 #define elf32_bed elf32_iamcu_bed
4688 #undef elf_backend_object_p
4689 #define elf_backend_object_p elf32_iamcu_elf_object_p
4691 #undef elf_backend_static_tls_alignment
4693 #undef elf_backend_want_plt_sym
4694 #define elf_backend_want_plt_sym 0
4696 #undef elf_backend_strtab_flags
4697 #undef elf_backend_copy_special_section_fields
4699 #include "elf32-target.h"
4701 /* Restore defaults. */
4702 #undef ELF_ARCH
4703 #define ELF_ARCH bfd_arch_i386
4704 #undef ELF_MACHINE_CODE
4705 #define ELF_MACHINE_CODE EM_386
4706 #undef elf_backend_object_p
4708 /* VxWorks support. */
4710 #undef TARGET_LITTLE_SYM
4711 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
4712 #undef TARGET_LITTLE_NAME
4713 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4714 #undef ELF_OSABI
4715 #undef ELF_MAXPAGESIZE
4716 #define ELF_MAXPAGESIZE 0x1000
4717 #undef elf_backend_plt_alignment
4718 #define elf_backend_plt_alignment 4
4720 #undef ELF_TARGET_OS
4721 #define ELF_TARGET_OS is_vxworks
4723 #undef elf_backend_relocs_compatible
4724 #undef elf_backend_add_symbol_hook
4725 #define elf_backend_add_symbol_hook \
4726 elf_vxworks_add_symbol_hook
4727 #undef elf_backend_link_output_symbol_hook
4728 #define elf_backend_link_output_symbol_hook \
4729 elf_vxworks_link_output_symbol_hook
4730 #undef elf_backend_emit_relocs
4731 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4732 #undef elf_backend_final_write_processing
4733 #define elf_backend_final_write_processing \
4734 elf_vxworks_final_write_processing
4735 #undef elf_backend_static_tls_alignment
4737 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4738 define it. */
4739 #undef elf_backend_want_plt_sym
4740 #define elf_backend_want_plt_sym 1
4742 #undef elf32_bed
4743 #define elf32_bed elf32_i386_vxworks_bed
4745 #include "elf32-target.h"