x86: Propery check PC16 reloc overflow in 16-bit mode instructions
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright (C) 1993-2021 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
20
21 #include "elfxx-x86.h"
22 #include "elf-vxworks.h"
23 #include "dwarf2.h"
24 #include "opcode/i386.h"
25 #include "libiberty.h"
26
27 /* 386 uses REL relocations instead of RELA. */
28 #define USE_REL 1
29
30 #include "elf/i386.h"
31
32 static reloc_howto_type elf_howto_table[]=
33 {
34 HOWTO(R_386_NONE, 0, 3, 0, false, 0, complain_overflow_dont,
35 bfd_elf_generic_reloc, "R_386_NONE",
36 true, 0x00000000, 0x00000000, false),
37 HOWTO(R_386_32, 0, 2, 32, false, 0, complain_overflow_dont,
38 bfd_elf_generic_reloc, "R_386_32",
39 true, 0xffffffff, 0xffffffff, false),
40 HOWTO(R_386_PC32, 0, 2, 32, true, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_386_PC32",
42 true, 0xffffffff, 0xffffffff, true),
43 HOWTO(R_386_GOT32, 0, 2, 32, false, 0, complain_overflow_dont,
44 bfd_elf_generic_reloc, "R_386_GOT32",
45 true, 0xffffffff, 0xffffffff, false),
46 HOWTO(R_386_PLT32, 0, 2, 32, true, 0, complain_overflow_dont,
47 bfd_elf_generic_reloc, "R_386_PLT32",
48 true, 0xffffffff, 0xffffffff, true),
49 HOWTO(R_386_COPY, 0, 2, 32, false, 0, complain_overflow_dont,
50 bfd_elf_generic_reloc, "R_386_COPY",
51 true, 0xffffffff, 0xffffffff, false),
52 HOWTO(R_386_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_dont,
53 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
54 true, 0xffffffff, 0xffffffff, false),
55 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, false, 0, complain_overflow_dont,
56 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
57 true, 0xffffffff, 0xffffffff, false),
58 HOWTO(R_386_RELATIVE, 0, 2, 32, false, 0, complain_overflow_dont,
59 bfd_elf_generic_reloc, "R_386_RELATIVE",
60 true, 0xffffffff, 0xffffffff, false),
61 HOWTO(R_386_GOTOFF, 0, 2, 32, false, 0, complain_overflow_dont,
62 bfd_elf_generic_reloc, "R_386_GOTOFF",
63 true, 0xffffffff, 0xffffffff, false),
64 HOWTO(R_386_GOTPC, 0, 2, 32, true, 0, complain_overflow_dont,
65 bfd_elf_generic_reloc, "R_386_GOTPC",
66 true, 0xffffffff, 0xffffffff, true),
67
68 /* We have a gap in the reloc numbers here.
69 R_386_standard counts the number up to this point, and
70 R_386_ext_offset is the value to subtract from a reloc type of
71 R_386_16 thru R_386_PC8 to form an index into this table. */
72 #define R_386_standard (R_386_GOTPC + 1)
73 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
74
75 /* These relocs are a GNU extension. */
76 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, false, 0, complain_overflow_dont,
77 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
78 true, 0xffffffff, 0xffffffff, false),
79 HOWTO(R_386_TLS_IE, 0, 2, 32, false, 0, complain_overflow_dont,
80 bfd_elf_generic_reloc, "R_386_TLS_IE",
81 true, 0xffffffff, 0xffffffff, false),
82 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, false, 0, complain_overflow_dont,
83 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
84 true, 0xffffffff, 0xffffffff, false),
85 HOWTO(R_386_TLS_LE, 0, 2, 32, false, 0, complain_overflow_dont,
86 bfd_elf_generic_reloc, "R_386_TLS_LE",
87 true, 0xffffffff, 0xffffffff, false),
88 HOWTO(R_386_TLS_GD, 0, 2, 32, false, 0, complain_overflow_dont,
89 bfd_elf_generic_reloc, "R_386_TLS_GD",
90 true, 0xffffffff, 0xffffffff, false),
91 HOWTO(R_386_TLS_LDM, 0, 2, 32, false, 0, complain_overflow_dont,
92 bfd_elf_generic_reloc, "R_386_TLS_LDM",
93 true, 0xffffffff, 0xffffffff, false),
94 HOWTO(R_386_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_386_16",
96 true, 0xffff, 0xffff, false),
97 HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_signed,
98 bfd_elf_generic_reloc, "R_386_PC16",
99 true, 0xffff, 0xffff, true),
100 HOWTO(R_386_8, 0, 0, 8, false, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_386_8",
102 true, 0xff, 0xff, false),
103 HOWTO(R_386_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_386_PC8",
105 true, 0xff, 0xff, true),
106
107 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
108 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
109 /* These are common with Solaris TLS implementation. */
110 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, false, 0, complain_overflow_dont,
111 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
112 true, 0xffffffff, 0xffffffff, false),
113 HOWTO(R_386_TLS_IE_32, 0, 2, 32, false, 0, complain_overflow_dont,
114 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
115 true, 0xffffffff, 0xffffffff, false),
116 HOWTO(R_386_TLS_LE_32, 0, 2, 32, false, 0, complain_overflow_dont,
117 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
118 true, 0xffffffff, 0xffffffff, false),
119 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, false, 0, complain_overflow_dont,
120 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
121 true, 0xffffffff, 0xffffffff, false),
122 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, false, 0, complain_overflow_dont,
123 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
124 true, 0xffffffff, 0xffffffff, false),
125 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, false, 0, complain_overflow_dont,
126 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
127 true, 0xffffffff, 0xffffffff, false),
128 HOWTO(R_386_SIZE32, 0, 2, 32, false, 0, complain_overflow_dont,
129 bfd_elf_generic_reloc, "R_386_SIZE32",
130 true, 0xffffffff, 0xffffffff, false),
131 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, false, 0, complain_overflow_dont,
132 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC",
133 true, 0xffffffff, 0xffffffff, false),
134 HOWTO(R_386_TLS_DESC_CALL, 0, 3, 0, false, 0, complain_overflow_dont,
135 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL",
136 false, 0, 0, false),
137 HOWTO(R_386_TLS_DESC, 0, 2, 32, false, 0, complain_overflow_dont,
138 bfd_elf_generic_reloc, "R_386_TLS_DESC",
139 true, 0xffffffff, 0xffffffff, false),
140 HOWTO(R_386_IRELATIVE, 0, 2, 32, false, 0, complain_overflow_dont,
141 bfd_elf_generic_reloc, "R_386_IRELATIVE",
142 true, 0xffffffff, 0xffffffff, false),
143 HOWTO(R_386_GOT32X, 0, 2, 32, false, 0, complain_overflow_dont,
144 bfd_elf_generic_reloc, "R_386_GOT32X",
145 true, 0xffffffff, 0xffffffff, false),
146
147 /* Another gap. */
148 #define R_386_ext2 (R_386_GOT32X + 1 - R_386_tls_offset)
149 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_ext2)
150
151 /* GNU extension to record C++ vtable hierarchy. */
152 HOWTO (R_386_GNU_VTINHERIT, /* type */
153 0, /* rightshift */
154 2, /* size (0 = byte, 1 = short, 2 = long) */
155 0, /* bitsize */
156 false, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_dont, /* complain_on_overflow */
159 NULL, /* special_function */
160 "R_386_GNU_VTINHERIT", /* name */
161 false, /* partial_inplace */
162 0, /* src_mask */
163 0, /* dst_mask */
164 false), /* pcrel_offset */
165
166 /* GNU extension to record C++ vtable member usage. */
167 HOWTO (R_386_GNU_VTENTRY, /* type */
168 0, /* rightshift */
169 2, /* size (0 = byte, 1 = short, 2 = long) */
170 0, /* bitsize */
171 false, /* pc_relative */
172 0, /* bitpos */
173 complain_overflow_dont, /* complain_on_overflow */
174 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
175 "R_386_GNU_VTENTRY", /* name */
176 false, /* partial_inplace */
177 0, /* src_mask */
178 0, /* dst_mask */
179 false), /* pcrel_offset */
180
181 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
182
183 /* Use complain_overflow_bitfield on R_386_PC16 for code16. */
184 HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield,
185 bfd_elf_generic_reloc, "R_386_PC16",
186 true, 0xffff, 0xffff, true)
187 };
188
189 #define X86_PCREL_TYPE_P(TYPE) ((TYPE) == R_386_PC32)
190
191 #define X86_SIZE_TYPE_P(TYPE) ((TYPE) == R_386_SIZE32)
192
193 #ifdef DEBUG_GEN_RELOC
194 #define TRACE(str) \
195 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
196 #else
197 #define TRACE(str)
198 #endif
199
200 static reloc_howto_type *
201 elf_i386_reloc_type_lookup (bfd *abfd,
202 bfd_reloc_code_real_type code)
203 {
204 switch (code)
205 {
206 case BFD_RELOC_NONE:
207 TRACE ("BFD_RELOC_NONE");
208 return &elf_howto_table[R_386_NONE];
209
210 case BFD_RELOC_32:
211 TRACE ("BFD_RELOC_32");
212 return &elf_howto_table[R_386_32];
213
214 case BFD_RELOC_CTOR:
215 TRACE ("BFD_RELOC_CTOR");
216 return &elf_howto_table[R_386_32];
217
218 case BFD_RELOC_32_PCREL:
219 TRACE ("BFD_RELOC_PC32");
220 return &elf_howto_table[R_386_PC32];
221
222 case BFD_RELOC_386_GOT32:
223 TRACE ("BFD_RELOC_386_GOT32");
224 return &elf_howto_table[R_386_GOT32];
225
226 case BFD_RELOC_386_PLT32:
227 TRACE ("BFD_RELOC_386_PLT32");
228 return &elf_howto_table[R_386_PLT32];
229
230 case BFD_RELOC_386_COPY:
231 TRACE ("BFD_RELOC_386_COPY");
232 return &elf_howto_table[R_386_COPY];
233
234 case BFD_RELOC_386_GLOB_DAT:
235 TRACE ("BFD_RELOC_386_GLOB_DAT");
236 return &elf_howto_table[R_386_GLOB_DAT];
237
238 case BFD_RELOC_386_JUMP_SLOT:
239 TRACE ("BFD_RELOC_386_JUMP_SLOT");
240 return &elf_howto_table[R_386_JUMP_SLOT];
241
242 case BFD_RELOC_386_RELATIVE:
243 TRACE ("BFD_RELOC_386_RELATIVE");
244 return &elf_howto_table[R_386_RELATIVE];
245
246 case BFD_RELOC_386_GOTOFF:
247 TRACE ("BFD_RELOC_386_GOTOFF");
248 return &elf_howto_table[R_386_GOTOFF];
249
250 case BFD_RELOC_386_GOTPC:
251 TRACE ("BFD_RELOC_386_GOTPC");
252 return &elf_howto_table[R_386_GOTPC];
253
254 /* These relocs are a GNU extension. */
255 case BFD_RELOC_386_TLS_TPOFF:
256 TRACE ("BFD_RELOC_386_TLS_TPOFF");
257 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
258
259 case BFD_RELOC_386_TLS_IE:
260 TRACE ("BFD_RELOC_386_TLS_IE");
261 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
262
263 case BFD_RELOC_386_TLS_GOTIE:
264 TRACE ("BFD_RELOC_386_TLS_GOTIE");
265 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
266
267 case BFD_RELOC_386_TLS_LE:
268 TRACE ("BFD_RELOC_386_TLS_LE");
269 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
270
271 case BFD_RELOC_386_TLS_GD:
272 TRACE ("BFD_RELOC_386_TLS_GD");
273 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
274
275 case BFD_RELOC_386_TLS_LDM:
276 TRACE ("BFD_RELOC_386_TLS_LDM");
277 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
278
279 case BFD_RELOC_16:
280 TRACE ("BFD_RELOC_16");
281 return &elf_howto_table[R_386_16 - R_386_ext_offset];
282
283 case BFD_RELOC_16_PCREL:
284 TRACE ("BFD_RELOC_16_PCREL");
285 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
286
287 case BFD_RELOC_8:
288 TRACE ("BFD_RELOC_8");
289 return &elf_howto_table[R_386_8 - R_386_ext_offset];
290
291 case BFD_RELOC_8_PCREL:
292 TRACE ("BFD_RELOC_8_PCREL");
293 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
294
295 /* Common with Sun TLS implementation. */
296 case BFD_RELOC_386_TLS_LDO_32:
297 TRACE ("BFD_RELOC_386_TLS_LDO_32");
298 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
299
300 case BFD_RELOC_386_TLS_IE_32:
301 TRACE ("BFD_RELOC_386_TLS_IE_32");
302 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
303
304 case BFD_RELOC_386_TLS_LE_32:
305 TRACE ("BFD_RELOC_386_TLS_LE_32");
306 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
307
308 case BFD_RELOC_386_TLS_DTPMOD32:
309 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
310 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
311
312 case BFD_RELOC_386_TLS_DTPOFF32:
313 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
314 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
315
316 case BFD_RELOC_386_TLS_TPOFF32:
317 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
318 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
319
320 case BFD_RELOC_SIZE32:
321 TRACE ("BFD_RELOC_SIZE32");
322 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset];
323
324 case BFD_RELOC_386_TLS_GOTDESC:
325 TRACE ("BFD_RELOC_386_TLS_GOTDESC");
326 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset];
327
328 case BFD_RELOC_386_TLS_DESC_CALL:
329 TRACE ("BFD_RELOC_386_TLS_DESC_CALL");
330 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset];
331
332 case BFD_RELOC_386_TLS_DESC:
333 TRACE ("BFD_RELOC_386_TLS_DESC");
334 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset];
335
336 case BFD_RELOC_386_IRELATIVE:
337 TRACE ("BFD_RELOC_386_IRELATIVE");
338 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset];
339
340 case BFD_RELOC_386_GOT32X:
341 TRACE ("BFD_RELOC_386_GOT32X");
342 return &elf_howto_table[R_386_GOT32X - R_386_tls_offset];
343
344 case BFD_RELOC_VTABLE_INHERIT:
345 TRACE ("BFD_RELOC_VTABLE_INHERIT");
346 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
347
348 case BFD_RELOC_VTABLE_ENTRY:
349 TRACE ("BFD_RELOC_VTABLE_ENTRY");
350 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
351
352 default:
353 TRACE ("Unknown");
354 /* xgettext:c-format */
355 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
356 abfd, (int) code);
357 bfd_set_error (bfd_error_bad_value);
358 return NULL;
359 }
360 }
361
362 static reloc_howto_type *
363 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
364 const char *r_name)
365 {
366 unsigned int i;
367
368 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++)
369 if (elf_howto_table[i].name != NULL
370 && strcasecmp (elf_howto_table[i].name, r_name) == 0)
371 return &elf_howto_table[i];
372
373 return NULL;
374 }
375
376 static reloc_howto_type *
377 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type)
378 {
379 unsigned int indx;
380
381 if ((indx = r_type) >= R_386_standard
382 && ((indx = r_type - R_386_ext_offset) - R_386_standard
383 >= R_386_ext - R_386_standard)
384 && ((indx = r_type - R_386_tls_offset) - R_386_ext
385 >= R_386_ext2 - R_386_ext)
386 && ((indx = r_type - R_386_vt_offset) - R_386_ext2
387 >= R_386_vt - R_386_ext2))
388 return NULL;
389 /* PR 17512: file: 0f67f69d. */
390 if (elf_howto_table [indx].type != r_type)
391 return NULL;
392
393 /* Use complain_overflow_bitfield on R_386_PC16 for code16. */
394 if (r_type == (unsigned int) R_386_PC16 && elf_x86_has_code16 (abfd))
395 indx = ARRAY_SIZE (elf_howto_table) - 1;
396
397 return &elf_howto_table[indx];
398 }
399
400 static bool
401 elf_i386_info_to_howto_rel (bfd *abfd,
402 arelent *cache_ptr,
403 Elf_Internal_Rela *dst)
404 {
405 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
406
407 if ((cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type))
408 == NULL)
409 {
410 /* xgettext:c-format */
411 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
412 abfd, r_type);
413 bfd_set_error (bfd_error_bad_value);
414 return false;
415 }
416
417 return true;
418 }
419
420 /* Return whether a symbol name implies a local label. The UnixWare
421 2.1 cc generates temporary symbols that start with .X, so we
422 recognize them here. FIXME: do other SVR4 compilers also use .X?.
423 If so, we should move the .X recognition into
424 _bfd_elf_is_local_label_name. */
425
426 static bool
427 elf_i386_is_local_label_name (bfd *abfd, const char *name)
428 {
429 if (name[0] == '.' && name[1] == 'X')
430 return true;
431
432 return _bfd_elf_is_local_label_name (abfd, name);
433 }
434 \f
435 /* Support for core dump NOTE sections. */
436
437 static bool
438 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
439 {
440 int offset;
441 size_t size;
442
443 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
444 {
445 int pr_version = bfd_get_32 (abfd, note->descdata);
446
447 if (pr_version != 1)
448 return false;
449
450 /* pr_cursig */
451 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20);
452
453 /* pr_pid */
454 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
455
456 /* pr_reg */
457 offset = 28;
458 size = bfd_get_32 (abfd, note->descdata + 8);
459 }
460 else
461 {
462 switch (note->descsz)
463 {
464 default:
465 return false;
466
467 case 144: /* Linux/i386 */
468 /* pr_cursig */
469 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
470
471 /* pr_pid */
472 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
473
474 /* pr_reg */
475 offset = 72;
476 size = 68;
477
478 break;
479 }
480 }
481
482 /* Make a ".reg/999" section. */
483 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
484 size, note->descpos + offset);
485 }
486
487 static bool
488 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
489 {
490 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
491 {
492 int pr_version = bfd_get_32 (abfd, note->descdata);
493
494 if (pr_version != 1)
495 return false;
496
497 elf_tdata (abfd)->core->program
498 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
499 elf_tdata (abfd)->core->command
500 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
501 }
502 else
503 {
504 switch (note->descsz)
505 {
506 default:
507 return false;
508
509 case 124: /* Linux/i386 elf_prpsinfo. */
510 elf_tdata (abfd)->core->pid
511 = bfd_get_32 (abfd, note->descdata + 12);
512 elf_tdata (abfd)->core->program
513 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
514 elf_tdata (abfd)->core->command
515 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
516 }
517 }
518
519 /* Note that for some reason, a spurious space is tacked
520 onto the end of the args in some (at least one anyway)
521 implementations, so strip it off if it exists. */
522 {
523 char *command = elf_tdata (abfd)->core->command;
524 int n = strlen (command);
525
526 if (0 < n && command[n - 1] == ' ')
527 command[n - 1] = '\0';
528 }
529
530 return true;
531 }
532 \f
533 /* Functions for the i386 ELF linker.
534
535 In order to gain some understanding of code in this file without
536 knowing all the intricate details of the linker, note the
537 following:
538
539 Functions named elf_i386_* are called by external routines, other
540 functions are only called locally. elf_i386_* functions appear
541 in this file more or less in the order in which they are called
542 from external routines. eg. elf_i386_check_relocs is called
543 early in the link process, elf_i386_finish_dynamic_sections is
544 one of the last functions. */
545
546 /* The size in bytes of an entry in the lazy procedure linkage table. */
547
548 #define LAZY_PLT_ENTRY_SIZE 16
549
550 /* The size in bytes of an entry in the non-lazy procedure linkage
551 table. */
552
553 #define NON_LAZY_PLT_ENTRY_SIZE 8
554
555 /* The first entry in an absolute lazy procedure linkage table looks
556 like this. See the SVR4 ABI i386 supplement to see how this works.
557 Will be padded to LAZY_PLT_ENTRY_SIZE with lazy_plt->plt0_pad_byte. */
558
559 static const bfd_byte elf_i386_lazy_plt0_entry[12] =
560 {
561 0xff, 0x35, /* pushl contents of address */
562 0, 0, 0, 0, /* replaced with address of .got + 4. */
563 0xff, 0x25, /* jmp indirect */
564 0, 0, 0, 0 /* replaced with address of .got + 8. */
565 };
566
567 /* Subsequent entries in an absolute lazy procedure linkage table look
568 like this. */
569
570 static const bfd_byte elf_i386_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
571 {
572 0xff, 0x25, /* jmp indirect */
573 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
574 0x68, /* pushl immediate */
575 0, 0, 0, 0, /* replaced with offset into relocation table. */
576 0xe9, /* jmp relative */
577 0, 0, 0, 0 /* replaced with offset to start of .plt. */
578 };
579
580 /* The first entry in a PIC lazy procedure linkage table look like
581 this. Will be padded to LAZY_PLT_ENTRY_SIZE with
582 lazy_plt->plt0_pad_byte. */
583
584 static const bfd_byte elf_i386_pic_lazy_plt0_entry[12] =
585 {
586 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
587 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */
588 };
589
590 /* Subsequent entries in a PIC lazy procedure linkage table look like
591 this. */
592
593 static const bfd_byte elf_i386_pic_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
594 {
595 0xff, 0xa3, /* jmp *offset(%ebx) */
596 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
597 0x68, /* pushl immediate */
598 0, 0, 0, 0, /* replaced with offset into relocation table. */
599 0xe9, /* jmp relative */
600 0, 0, 0, 0 /* replaced with offset to start of .plt. */
601 };
602
603 /* Entries in the non-lazy procedure linkage table look like this. */
604
605 static const bfd_byte elf_i386_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
606 {
607 0xff, 0x25, /* jmp indirect */
608 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
609 0x66, 0x90 /* xchg %ax,%ax */
610 };
611
612 /* Entries in the PIC non-lazy procedure linkage table look like
613 this. */
614
615 static const bfd_byte elf_i386_pic_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
616 {
617 0xff, 0xa3, /* jmp *offset(%ebx) */
618 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
619 0x66, 0x90 /* xchg %ax,%ax */
620 };
621
622 /* The first entry in an absolute IBT-enabled lazy procedure linkage
623 table looks like this. */
624
625 static const bfd_byte elf_i386_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
626 {
627 0xff, 0x35, 0, 0, 0, 0, /* pushl GOT[1] */
628 0xff, 0x25, 0, 0, 0, 0, /* jmp *GOT[2] */
629 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
630 };
631
632 /* Subsequent entries for an absolute IBT-enabled lazy procedure linkage
633 table look like this. Subsequent entries for a PIC IBT-enabled lazy
634 procedure linkage table are the same. */
635
636 static const bfd_byte elf_i386_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
637 {
638 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
639 0x68, 0, 0, 0, 0, /* pushl immediate */
640 0xe9, 0, 0, 0, 0, /* jmp relative */
641 0x66, 0x90 /* xchg %ax,%ax */
642 };
643
644 /* The first entry in a PIC IBT-enabled lazy procedure linkage table
645 look like. */
646
647 static const bfd_byte elf_i386_pic_lazy_ibt_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
648 {
649 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
650 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
651 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
652 };
653
654 /* Entries for branches with IBT-enabled in the absolute non-lazey
655 procedure linkage table look like this. They have the same size
656 as the lazy PLT entry. */
657
658 static const bfd_byte elf_i386_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
659 {
660 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
661 0xff, 0x25, 0, 0, 0, 0, /* jmp *name@GOT */
662 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
663 };
664
665 /* Entries for branches with IBT-enabled in the PIC non-lazey procedure
666 linkage table look like this. They have the same size as the lazy
667 PLT entry. */
668
669 static const bfd_byte elf_i386_pic_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
670 {
671 0xf3, 0x0f, 0x1e, 0xfb, /* endbr32 */
672 0xff, 0xa3, 0, 0, 0, 0, /* jmp *name@GOT(%ebx) */
673 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
674 };
675
676 /* .eh_frame covering the lazy .plt section. */
677
678 static const bfd_byte elf_i386_eh_frame_lazy_plt[] =
679 {
680 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
681 0, 0, 0, 0, /* CIE ID */
682 1, /* CIE version */
683 'z', 'R', 0, /* Augmentation string */
684 1, /* Code alignment factor */
685 0x7c, /* Data alignment factor */
686 8, /* Return address column */
687 1, /* Augmentation size */
688 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
689 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
690 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
691 DW_CFA_nop, DW_CFA_nop,
692
693 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
694 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
695 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
696 0, 0, 0, 0, /* .plt size goes here */
697 0, /* Augmentation size */
698 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
699 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
700 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
701 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
702 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
703 11, /* Block length */
704 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
705 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
706 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
707 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
708 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
709 };
710
711 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
712
713 static const bfd_byte elf_i386_eh_frame_lazy_ibt_plt[] =
714 {
715 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
716 0, 0, 0, 0, /* CIE ID */
717 1, /* CIE version */
718 'z', 'R', 0, /* Augmentation string */
719 1, /* Code alignment factor */
720 0x7c, /* Data alignment factor */
721 8, /* Return address column */
722 1, /* Augmentation size */
723 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
724 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
725 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
726 DW_CFA_nop, DW_CFA_nop,
727
728 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
729 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
730 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
731 0, 0, 0, 0, /* .plt size goes here */
732 0, /* Augmentation size */
733 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
734 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
735 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
736 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
737 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
738 11, /* Block length */
739 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
740 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
741 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
742 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
743 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
744 };
745
746 /* .eh_frame covering the non-lazy .plt section. */
747
748 static const bfd_byte elf_i386_eh_frame_non_lazy_plt[] =
749 {
750 #define PLT_GOT_FDE_LENGTH 16
751 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
752 0, 0, 0, 0, /* CIE ID */
753 1, /* CIE version */
754 'z', 'R', 0, /* Augmentation string */
755 1, /* Code alignment factor */
756 0x7c, /* Data alignment factor */
757 8, /* Return address column */
758 1, /* Augmentation size */
759 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
760 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
761 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
762 DW_CFA_nop, DW_CFA_nop,
763
764 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
765 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
766 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
767 0, 0, 0, 0, /* non-lazy .plt size goes here */
768 0, /* Augmentation size */
769 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
770 };
771
772 /* These are the standard parameters. */
773 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_plt =
774 {
775 elf_i386_lazy_plt0_entry, /* plt0_entry */
776 sizeof (elf_i386_lazy_plt0_entry), /* plt0_entry_size */
777 elf_i386_lazy_plt_entry, /* plt_entry */
778 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
779 NULL, /* plt_tlsdesc_entry */
780 0, /* plt_tlsdesc_entry_size*/
781 0, /* plt_tlsdesc_got1_offset */
782 0, /* plt_tlsdesc_got2_offset */
783 0, /* plt_tlsdesc_got1_insn_end */
784 0, /* plt_tlsdesc_got2_insn_end */
785 2, /* plt0_got1_offset */
786 8, /* plt0_got2_offset */
787 0, /* plt0_got2_insn_end */
788 2, /* plt_got_offset */
789 7, /* plt_reloc_offset */
790 12, /* plt_plt_offset */
791 0, /* plt_got_insn_size */
792 0, /* plt_plt_insn_end */
793 6, /* plt_lazy_offset */
794 elf_i386_pic_lazy_plt0_entry, /* pic_plt0_entry */
795 elf_i386_pic_lazy_plt_entry, /* pic_plt_entry */
796 elf_i386_eh_frame_lazy_plt, /* eh_frame_plt */
797 sizeof (elf_i386_eh_frame_lazy_plt) /* eh_frame_plt_size */
798 };
799
800 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_plt =
801 {
802 elf_i386_non_lazy_plt_entry, /* plt_entry */
803 elf_i386_pic_non_lazy_plt_entry, /* pic_plt_entry */
804 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
805 2, /* plt_got_offset */
806 0, /* plt_got_insn_size */
807 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
808 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
809 };
810
811 static const struct elf_x86_lazy_plt_layout elf_i386_lazy_ibt_plt =
812 {
813 elf_i386_lazy_ibt_plt0_entry, /* plt0_entry */
814 sizeof (elf_i386_lazy_ibt_plt0_entry), /* plt0_entry_size */
815 elf_i386_lazy_ibt_plt_entry, /* plt_entry */
816 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
817 NULL, /* plt_tlsdesc_entry */
818 0, /* plt_tlsdesc_entry_size*/
819 0, /* plt_tlsdesc_got1_offset */
820 0, /* plt_tlsdesc_got2_offset */
821 0, /* plt_tlsdesc_got1_insn_end */
822 0, /* plt_tlsdesc_got2_insn_end */
823 2, /* plt0_got1_offset */
824 8, /* plt0_got2_offset */
825 0, /* plt0_got2_insn_end */
826 4+2, /* plt_got_offset */
827 4+1, /* plt_reloc_offset */
828 4+6, /* plt_plt_offset */
829 0, /* plt_got_insn_size */
830 0, /* plt_plt_insn_end */
831 0, /* plt_lazy_offset */
832 elf_i386_pic_lazy_ibt_plt0_entry, /* pic_plt0_entry */
833 elf_i386_lazy_ibt_plt_entry, /* pic_plt_entry */
834 elf_i386_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
835 sizeof (elf_i386_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
836 };
837
838 static const struct elf_x86_non_lazy_plt_layout elf_i386_non_lazy_ibt_plt =
839 {
840 elf_i386_non_lazy_ibt_plt_entry, /* plt_entry */
841 elf_i386_pic_non_lazy_ibt_plt_entry,/* pic_plt_entry */
842 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
843 4+2, /* plt_got_offset */
844 0, /* plt_got_insn_size */
845 elf_i386_eh_frame_non_lazy_plt, /* eh_frame_plt */
846 sizeof (elf_i386_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
847 };
848 \f
849
850 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations
851 for the PLTResolve stub and then for each PLT entry. */
852 #define PLTRESOLVE_RELOCS_SHLIB 0
853 #define PLTRESOLVE_RELOCS 2
854 #define PLT_NON_JUMP_SLOT_RELOCS 2
855
856 /* Return TRUE if the TLS access code sequence support transition
857 from R_TYPE. */
858
859 static bool
860 elf_i386_check_tls_transition (asection *sec,
861 bfd_byte *contents,
862 Elf_Internal_Shdr *symtab_hdr,
863 struct elf_link_hash_entry **sym_hashes,
864 unsigned int r_type,
865 const Elf_Internal_Rela *rel,
866 const Elf_Internal_Rela *relend)
867 {
868 unsigned int val, type, reg;
869 unsigned long r_symndx;
870 struct elf_link_hash_entry *h;
871 bfd_vma offset;
872 bfd_byte *call;
873 bool indirect_call;
874
875 offset = rel->r_offset;
876 switch (r_type)
877 {
878 case R_386_TLS_GD:
879 case R_386_TLS_LDM:
880 if (offset < 2 || (rel + 1) >= relend)
881 return false;
882
883 indirect_call = false;
884 call = contents + offset + 4;
885 val = *(call - 5);
886 type = *(call - 6);
887 if (r_type == R_386_TLS_GD)
888 {
889 /* Check transition from GD access model. Only
890 leal foo@tlsgd(,%ebx,1), %eax
891 call ___tls_get_addr@PLT
892 or
893 leal foo@tlsgd(%ebx) %eax
894 call ___tls_get_addr@PLT
895 nop
896 or
897 leal foo@tlsgd(%reg), %eax
898 call *___tls_get_addr@GOT(%reg)
899 which may be converted to
900 addr32 call ___tls_get_addr
901 can transit to different access model. */
902 if ((offset + 10) > sec->size
903 || (type != 0x8d && type != 0x04))
904 return false;
905
906 if (type == 0x04)
907 {
908 /* leal foo@tlsgd(,%ebx,1), %eax
909 call ___tls_get_addr@PLT */
910 if (offset < 3)
911 return false;
912
913 if (*(call - 7) != 0x8d
914 || val != 0x1d
915 || call[0] != 0xe8)
916 return false;
917 }
918 else
919 {
920 /* This must be
921 leal foo@tlsgd(%ebx), %eax
922 call ___tls_get_addr@PLT
923 nop
924 or
925 leal foo@tlsgd(%reg), %eax
926 call *___tls_get_addr@GOT(%reg)
927 which may be converted to
928 addr32 call ___tls_get_addr
929
930 %eax can't be used as the GOT base register since it
931 is used to pass parameter to ___tls_get_addr. */
932 reg = val & 7;
933 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
934 return false;
935
936 indirect_call = call[0] == 0xff;
937 if (!(reg == 3 && call[0] == 0xe8 && call[5] == 0x90)
938 && !(call[0] == 0x67 && call[1] == 0xe8)
939 && !(indirect_call
940 && (call[1] & 0xf8) == 0x90
941 && (call[1] & 0x7) == reg))
942 return false;
943 }
944 }
945 else
946 {
947 /* Check transition from LD access model. Only
948 leal foo@tlsldm(%ebx), %eax
949 call ___tls_get_addr@PLT
950 or
951 leal foo@tlsldm(%reg), %eax
952 call *___tls_get_addr@GOT(%reg)
953 which may be converted to
954 addr32 call ___tls_get_addr
955 can transit to different access model. */
956 if (type != 0x8d || (offset + 9) > sec->size)
957 return false;
958
959 /* %eax can't be used as the GOT base register since it is
960 used to pass parameter to ___tls_get_addr. */
961 reg = val & 7;
962 if ((val & 0xf8) != 0x80 || reg == 4 || reg == 0)
963 return false;
964
965 indirect_call = call[0] == 0xff;
966 if (!(reg == 3 && call[0] == 0xe8)
967 && !(call[0] == 0x67 && call[1] == 0xe8)
968 && !(indirect_call
969 && (call[1] & 0xf8) == 0x90
970 && (call[1] & 0x7) == reg))
971 return false;
972 }
973
974 r_symndx = ELF32_R_SYM (rel[1].r_info);
975 if (r_symndx < symtab_hdr->sh_info)
976 return false;
977
978 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
979 if (h == NULL
980 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
981 return false;
982 else if (indirect_call)
983 return (ELF32_R_TYPE (rel[1].r_info) == R_386_GOT32X);
984 else
985 return (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32
986 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
987
988 case R_386_TLS_IE:
989 /* Check transition from IE access model:
990 movl foo@indntpoff(%rip), %eax
991 movl foo@indntpoff(%rip), %reg
992 addl foo@indntpoff(%rip), %reg
993 */
994
995 if (offset < 1 || (offset + 4) > sec->size)
996 return false;
997
998 /* Check "movl foo@tpoff(%rip), %eax" first. */
999 val = bfd_get_8 (abfd, contents + offset - 1);
1000 if (val == 0xa1)
1001 return true;
1002
1003 if (offset < 2)
1004 return false;
1005
1006 /* Check movl|addl foo@tpoff(%rip), %reg. */
1007 type = bfd_get_8 (abfd, contents + offset - 2);
1008 return ((type == 0x8b || type == 0x03)
1009 && (val & 0xc7) == 0x05);
1010
1011 case R_386_TLS_GOTIE:
1012 case R_386_TLS_IE_32:
1013 /* Check transition from {IE_32,GOTIE} access model:
1014 subl foo@{tpoff,gontoff}(%reg1), %reg2
1015 movl foo@{tpoff,gontoff}(%reg1), %reg2
1016 addl foo@{tpoff,gontoff}(%reg1), %reg2
1017 */
1018
1019 if (offset < 2 || (offset + 4) > sec->size)
1020 return false;
1021
1022 val = bfd_get_8 (abfd, contents + offset - 1);
1023 if ((val & 0xc0) != 0x80 || (val & 7) == 4)
1024 return false;
1025
1026 type = bfd_get_8 (abfd, contents + offset - 2);
1027 return type == 0x8b || type == 0x2b || type == 0x03;
1028
1029 case R_386_TLS_GOTDESC:
1030 /* Check transition from GDesc access model:
1031 leal x@tlsdesc(%ebx), %eax
1032
1033 Make sure it's a leal adding ebx to a 32-bit offset
1034 into any register, although it's probably almost always
1035 going to be eax. */
1036
1037 if (offset < 2 || (offset + 4) > sec->size)
1038 return false;
1039
1040 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1041 return false;
1042
1043 val = bfd_get_8 (abfd, contents + offset - 1);
1044 return (val & 0xc7) == 0x83;
1045
1046 case R_386_TLS_DESC_CALL:
1047 /* Check transition from GDesc access model:
1048 call *x@tlsdesc(%eax)
1049 */
1050 if (offset + 2 <= sec->size)
1051 {
1052 /* Make sure that it's a call *x@tlsdesc(%eax). */
1053 call = contents + offset;
1054 return call[0] == 0xff && call[1] == 0x10;
1055 }
1056
1057 return false;
1058
1059 default:
1060 abort ();
1061 }
1062 }
1063
1064 /* Return TRUE if the TLS access transition is OK or no transition
1065 will be performed. Update R_TYPE if there is a transition. */
1066
1067 static bool
1068 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd,
1069 asection *sec, bfd_byte *contents,
1070 Elf_Internal_Shdr *symtab_hdr,
1071 struct elf_link_hash_entry **sym_hashes,
1072 unsigned int *r_type, int tls_type,
1073 const Elf_Internal_Rela *rel,
1074 const Elf_Internal_Rela *relend,
1075 struct elf_link_hash_entry *h,
1076 unsigned long r_symndx,
1077 bool from_relocate_section)
1078 {
1079 unsigned int from_type = *r_type;
1080 unsigned int to_type = from_type;
1081 bool check = true;
1082
1083 /* Skip TLS transition for functions. */
1084 if (h != NULL
1085 && (h->type == STT_FUNC
1086 || h->type == STT_GNU_IFUNC))
1087 return true;
1088
1089 switch (from_type)
1090 {
1091 case R_386_TLS_GD:
1092 case R_386_TLS_GOTDESC:
1093 case R_386_TLS_DESC_CALL:
1094 case R_386_TLS_IE_32:
1095 case R_386_TLS_IE:
1096 case R_386_TLS_GOTIE:
1097 if (bfd_link_executable (info))
1098 {
1099 if (h == NULL)
1100 to_type = R_386_TLS_LE_32;
1101 else if (from_type != R_386_TLS_IE
1102 && from_type != R_386_TLS_GOTIE)
1103 to_type = R_386_TLS_IE_32;
1104 }
1105
1106 /* When we are called from elf_i386_relocate_section, there may
1107 be additional transitions based on TLS_TYPE. */
1108 if (from_relocate_section)
1109 {
1110 unsigned int new_to_type = to_type;
1111
1112 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1113 new_to_type = R_386_TLS_LE_32;
1114
1115 if (to_type == R_386_TLS_GD
1116 || to_type == R_386_TLS_GOTDESC
1117 || to_type == R_386_TLS_DESC_CALL)
1118 {
1119 if (tls_type == GOT_TLS_IE_POS)
1120 new_to_type = R_386_TLS_GOTIE;
1121 else if (tls_type & GOT_TLS_IE)
1122 new_to_type = R_386_TLS_IE_32;
1123 }
1124
1125 /* We checked the transition before when we were called from
1126 elf_i386_check_relocs. We only want to check the new
1127 transition which hasn't been checked before. */
1128 check = new_to_type != to_type && from_type == to_type;
1129 to_type = new_to_type;
1130 }
1131
1132 break;
1133
1134 case R_386_TLS_LDM:
1135 if (bfd_link_executable (info))
1136 to_type = R_386_TLS_LE_32;
1137 break;
1138
1139 default:
1140 return true;
1141 }
1142
1143 /* Return TRUE if there is no transition. */
1144 if (from_type == to_type)
1145 return true;
1146
1147 /* Check if the transition can be performed. */
1148 if (check
1149 && ! elf_i386_check_tls_transition (sec, contents,
1150 symtab_hdr, sym_hashes,
1151 from_type, rel, relend))
1152 {
1153 reloc_howto_type *from, *to;
1154 const char *name;
1155
1156 from = elf_i386_rtype_to_howto (abfd, from_type);
1157 to = elf_i386_rtype_to_howto (abfd, to_type);
1158
1159 if (h)
1160 name = h->root.root.string;
1161 else
1162 {
1163 struct elf_x86_link_hash_table *htab;
1164
1165 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1166 if (htab == NULL)
1167 name = "*unknown*";
1168 else
1169 {
1170 Elf_Internal_Sym *isym;
1171
1172 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1173 abfd, r_symndx);
1174 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1175 }
1176 }
1177
1178 _bfd_error_handler
1179 /* xgettext:c-format */
1180 (_("%pB: TLS transition from %s to %s against `%s'"
1181 " at %#" PRIx64 " in section `%pA' failed"),
1182 abfd, from->name, to->name, name,
1183 (uint64_t) rel->r_offset, sec);
1184 bfd_set_error (bfd_error_bad_value);
1185 return false;
1186 }
1187
1188 *r_type = to_type;
1189 return true;
1190 }
1191
1192 /* With the local symbol, foo, we convert
1193 mov foo@GOT[(%reg1)], %reg2
1194 to
1195 lea foo[@GOTOFF(%reg1)], %reg2
1196 and convert
1197 call/jmp *foo@GOT[(%reg)]
1198 to
1199 nop call foo/jmp foo nop
1200 When PIC is false, convert
1201 test %reg1, foo@GOT[(%reg2)]
1202 to
1203 test $foo, %reg1
1204 and convert
1205 binop foo@GOT[(%reg1)], %reg2
1206 to
1207 binop $foo, %reg2
1208 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1209 instructions. */
1210
1211 static
1212 bool
1213 elf_i386_convert_load_reloc (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
1214 bfd_byte *contents,
1215 unsigned int *r_type_p,
1216 Elf_Internal_Rela *irel,
1217 struct elf_link_hash_entry *h,
1218 bool *converted,
1219 struct bfd_link_info *link_info)
1220 {
1221 struct elf_x86_link_hash_table *htab;
1222 unsigned int opcode;
1223 unsigned int modrm;
1224 bool baseless;
1225 Elf_Internal_Sym *isym;
1226 unsigned int addend;
1227 unsigned int nop;
1228 bfd_vma nop_offset;
1229 bool is_pic;
1230 bool to_reloc_32;
1231 bool abs_symbol;
1232 unsigned int r_type;
1233 unsigned int r_symndx;
1234 bfd_vma roff = irel->r_offset;
1235 bool local_ref;
1236 struct elf_x86_link_hash_entry *eh;
1237
1238 if (roff < 2)
1239 return true;
1240
1241 /* Addend for R_386_GOT32X relocations must be 0. */
1242 addend = bfd_get_32 (abfd, contents + roff);
1243 if (addend != 0)
1244 return true;
1245
1246 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1247 is_pic = bfd_link_pic (link_info);
1248
1249 r_type = *r_type_p;
1250 r_symndx = ELF32_R_SYM (irel->r_info);
1251
1252 modrm = bfd_get_8 (abfd, contents + roff - 1);
1253 baseless = (modrm & 0xc7) == 0x5;
1254
1255 if (h)
1256 {
1257 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1258 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1259 isym = NULL;
1260 abs_symbol = ABS_SYMBOL_P (h);
1261 }
1262 else
1263 {
1264 local_ref = true;
1265 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd,
1266 r_symndx);
1267 abs_symbol = isym->st_shndx == SHN_ABS;
1268 }
1269
1270 if (baseless && is_pic)
1271 {
1272 /* For PIC, disallow R_386_GOT32X without a base register
1273 since we don't know what the GOT base is. */
1274 const char *name;
1275
1276 if (h == NULL)
1277 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1278 else
1279 name = h->root.root.string;
1280
1281 _bfd_error_handler
1282 /* xgettext:c-format */
1283 (_("%pB: direct GOT relocation R_386_GOT32X against `%s' without base"
1284 " register can not be used when making a shared object"),
1285 abfd, name);
1286 return false;
1287 }
1288
1289 opcode = bfd_get_8 (abfd, contents + roff - 2);
1290
1291 /* Convert to R_386_32 if PIC is false or there is no base
1292 register. */
1293 to_reloc_32 = !is_pic || baseless;
1294
1295 eh = elf_x86_hash_entry (h);
1296
1297 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1298 reloc. */
1299 if (h == NULL)
1300 {
1301 if (opcode == 0x0ff)
1302 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1303 goto convert_branch;
1304 else
1305 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1306 "test %reg1, foo@GOT(%reg2)" and
1307 "binop foo@GOT[(%reg1)], %reg2". */
1308 goto convert_load;
1309 }
1310
1311 /* Undefined weak symbol is only bound locally in executable
1312 and its reference is resolved as 0. */
1313 if (h->root.type == bfd_link_hash_undefweak
1314 && !eh->linker_def
1315 && local_ref)
1316 {
1317 if (opcode == 0xff)
1318 {
1319 /* No direct branch to 0 for PIC. */
1320 if (is_pic)
1321 return true;
1322 else
1323 goto convert_branch;
1324 }
1325 else
1326 {
1327 /* We can convert load of address 0 to R_386_32. */
1328 to_reloc_32 = true;
1329 goto convert_load;
1330 }
1331 }
1332
1333 if (opcode == 0xff)
1334 {
1335 /* We have "call/jmp *foo@GOT[(%reg)]". */
1336 if ((h->root.type == bfd_link_hash_defined
1337 || h->root.type == bfd_link_hash_defweak)
1338 && local_ref)
1339 {
1340 /* The function is locally defined. */
1341 convert_branch:
1342 /* Convert R_386_GOT32X to R_386_PC32. */
1343 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1344 {
1345 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1346 is a nop prefix. */
1347 modrm = 0xe8;
1348 /* To support TLS optimization, always use addr32 prefix
1349 for "call *___tls_get_addr@GOT(%reg)". */
1350 if (eh && eh->tls_get_addr)
1351 {
1352 nop = 0x67;
1353 nop_offset = irel->r_offset - 2;
1354 }
1355 else
1356 {
1357 nop = htab->params->call_nop_byte;
1358 if (htab->params->call_nop_as_suffix)
1359 {
1360 nop_offset = roff + 3;
1361 irel->r_offset -= 1;
1362 }
1363 else
1364 nop_offset = roff - 2;
1365 }
1366 }
1367 else
1368 {
1369 /* Convert to "jmp foo nop". */
1370 modrm = 0xe9;
1371 nop = NOP_OPCODE;
1372 nop_offset = roff + 3;
1373 irel->r_offset -= 1;
1374 }
1375
1376 bfd_put_8 (abfd, nop, contents + nop_offset);
1377 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1378 /* When converting to PC-relative relocation, we
1379 need to adjust addend by -4. */
1380 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1381 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1382 *r_type_p = R_386_PC32;
1383 *converted = true;
1384 }
1385 }
1386 else
1387 {
1388 /* We have "mov foo@GOT[(%re1g)], %reg2",
1389 "test %reg1, foo@GOT(%reg2)" and
1390 "binop foo@GOT[(%reg1)], %reg2".
1391
1392 Avoid optimizing _DYNAMIC since ld.so may use its
1393 link-time address. */
1394 if (h == htab->elf.hdynamic)
1395 return true;
1396
1397 /* def_regular is set by an assignment in a linker script in
1398 bfd_elf_record_link_assignment. start_stop is set on
1399 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1400 if (h->start_stop
1401 || eh->linker_def
1402 || ((h->def_regular
1403 || h->root.type == bfd_link_hash_defined
1404 || h->root.type == bfd_link_hash_defweak)
1405 && local_ref))
1406 {
1407 convert_load:
1408 if (opcode == 0x8b)
1409 {
1410 if (abs_symbol && local_ref)
1411 to_reloc_32 = true;
1412
1413 if (to_reloc_32)
1414 {
1415 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1416 "mov $foo, %reg2" with R_386_32. */
1417 r_type = R_386_32;
1418 modrm = 0xc0 | (modrm & 0x38) >> 3;
1419 bfd_put_8 (abfd, modrm, contents + roff - 1);
1420 opcode = 0xc7;
1421 }
1422 else
1423 {
1424 /* Convert "mov foo@GOT(%reg1), %reg2" to
1425 "lea foo@GOTOFF(%reg1), %reg2". */
1426 r_type = R_386_GOTOFF;
1427 opcode = 0x8d;
1428 }
1429 }
1430 else
1431 {
1432 /* Only R_386_32 is supported. */
1433 if (!to_reloc_32)
1434 return true;
1435
1436 if (opcode == 0x85)
1437 {
1438 /* Convert "test %reg1, foo@GOT(%reg2)" to
1439 "test $foo, %reg1". */
1440 modrm = 0xc0 | (modrm & 0x38) >> 3;
1441 opcode = 0xf7;
1442 }
1443 else
1444 {
1445 /* Convert "binop foo@GOT(%reg1), %reg2" to
1446 "binop $foo, %reg2". */
1447 modrm = (0xc0
1448 | (modrm & 0x38) >> 3
1449 | (opcode & 0x3c));
1450 opcode = 0x81;
1451 }
1452 bfd_put_8 (abfd, modrm, contents + roff - 1);
1453 r_type = R_386_32;
1454 }
1455
1456 bfd_put_8 (abfd, opcode, contents + roff - 2);
1457 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1458 *r_type_p = r_type;
1459 *converted = true;
1460 }
1461 }
1462
1463 return true;
1464 }
1465
1466 /* Rename some of the generic section flags to better document how they
1467 are used here. */
1468 #define check_relocs_failed sec_flg0
1469
1470 /* Look through the relocs for a section during the first phase, and
1471 calculate needed space in the global offset table, procedure linkage
1472 table, and dynamic reloc sections. */
1473
1474 static bool
1475 elf_i386_check_relocs (bfd *abfd,
1476 struct bfd_link_info *info,
1477 asection *sec,
1478 const Elf_Internal_Rela *relocs)
1479 {
1480 struct elf_x86_link_hash_table *htab;
1481 Elf_Internal_Shdr *symtab_hdr;
1482 struct elf_link_hash_entry **sym_hashes;
1483 const Elf_Internal_Rela *rel;
1484 const Elf_Internal_Rela *rel_end;
1485 asection *sreloc;
1486 bfd_byte *contents;
1487 bool converted;
1488
1489 if (bfd_link_relocatable (info))
1490 return true;
1491
1492 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1493 if (htab == NULL)
1494 {
1495 sec->check_relocs_failed = 1;
1496 return false;
1497 }
1498
1499 BFD_ASSERT (is_x86_elf (abfd, htab));
1500
1501 /* Get the section contents. */
1502 if (elf_section_data (sec)->this_hdr.contents != NULL)
1503 contents = elf_section_data (sec)->this_hdr.contents;
1504 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1505 {
1506 sec->check_relocs_failed = 1;
1507 return false;
1508 }
1509
1510 symtab_hdr = &elf_symtab_hdr (abfd);
1511 sym_hashes = elf_sym_hashes (abfd);
1512
1513 converted = false;
1514
1515 sreloc = NULL;
1516
1517 rel_end = relocs + sec->reloc_count;
1518 for (rel = relocs; rel < rel_end; rel++)
1519 {
1520 unsigned int r_type;
1521 unsigned int r_symndx;
1522 struct elf_link_hash_entry *h;
1523 struct elf_x86_link_hash_entry *eh;
1524 Elf_Internal_Sym *isym;
1525 const char *name;
1526 bool size_reloc;
1527 bool no_dynreloc;
1528
1529 r_symndx = ELF32_R_SYM (rel->r_info);
1530 r_type = ELF32_R_TYPE (rel->r_info);
1531
1532 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1533 {
1534 /* xgettext:c-format */
1535 _bfd_error_handler (_("%pB: bad symbol index: %d"),
1536 abfd, r_symndx);
1537 goto error_return;
1538 }
1539
1540 if (r_symndx < symtab_hdr->sh_info)
1541 {
1542 /* A local symbol. */
1543 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1544 abfd, r_symndx);
1545 if (isym == NULL)
1546 goto error_return;
1547
1548 /* Check relocation against local STT_GNU_IFUNC symbol. */
1549 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1550 {
1551 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, true);
1552 if (h == NULL)
1553 goto error_return;
1554
1555 /* Fake a STT_GNU_IFUNC symbol. */
1556 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1557 isym, NULL);
1558 h->type = STT_GNU_IFUNC;
1559 h->def_regular = 1;
1560 h->ref_regular = 1;
1561 h->forced_local = 1;
1562 h->root.type = bfd_link_hash_defined;
1563 }
1564 else
1565 h = NULL;
1566 }
1567 else
1568 {
1569 isym = NULL;
1570 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1571 while (h->root.type == bfd_link_hash_indirect
1572 || h->root.type == bfd_link_hash_warning)
1573 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1574 }
1575
1576 eh = (struct elf_x86_link_hash_entry *) h;
1577 if (h != NULL)
1578 {
1579 if (r_type == R_386_GOTOFF)
1580 eh->gotoff_ref = 1;
1581
1582 /* It is referenced by a non-shared object. */
1583 h->ref_regular = 1;
1584 }
1585
1586 if (r_type == R_386_GOT32X
1587 && (h == NULL || h->type != STT_GNU_IFUNC))
1588 {
1589 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1590 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1591 &r_type, irel, h,
1592 &converted, info))
1593 goto error_return;
1594 }
1595
1596 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
1597 symtab_hdr, &no_dynreloc))
1598 return false;
1599
1600 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1601 symtab_hdr, sym_hashes,
1602 &r_type, GOT_UNKNOWN,
1603 rel, rel_end, h, r_symndx, false))
1604 goto error_return;
1605
1606 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
1607 if (h == htab->elf.hgot)
1608 htab->got_referenced = true;
1609
1610 switch (r_type)
1611 {
1612 case R_386_TLS_LDM:
1613 htab->tls_ld_or_ldm_got.refcount = 1;
1614 goto create_got;
1615
1616 case R_386_PLT32:
1617 /* This symbol requires a procedure linkage table entry. We
1618 actually build the entry in adjust_dynamic_symbol,
1619 because this might be a case of linking PIC code which is
1620 never referenced by a dynamic object, in which case we
1621 don't need to generate a procedure linkage table entry
1622 after all. */
1623
1624 /* If this is a local symbol, we resolve it directly without
1625 creating a procedure linkage table entry. */
1626 if (h == NULL)
1627 continue;
1628
1629 eh->zero_undefweak &= 0x2;
1630 h->needs_plt = 1;
1631 h->plt.refcount = 1;
1632 break;
1633
1634 case R_386_SIZE32:
1635 size_reloc = true;
1636 goto do_size;
1637
1638 case R_386_TLS_IE_32:
1639 case R_386_TLS_IE:
1640 case R_386_TLS_GOTIE:
1641 if (!bfd_link_executable (info))
1642 info->flags |= DF_STATIC_TLS;
1643 /* Fall through */
1644
1645 case R_386_GOT32:
1646 case R_386_GOT32X:
1647 case R_386_TLS_GD:
1648 case R_386_TLS_GOTDESC:
1649 case R_386_TLS_DESC_CALL:
1650 /* This symbol requires a global offset table entry. */
1651 {
1652 int tls_type, old_tls_type;
1653
1654 switch (r_type)
1655 {
1656 default:
1657 case R_386_GOT32:
1658 case R_386_GOT32X:
1659 tls_type = GOT_NORMAL;
1660 break;
1661 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1662 case R_386_TLS_GOTDESC:
1663 case R_386_TLS_DESC_CALL:
1664 tls_type = GOT_TLS_GDESC; break;
1665 case R_386_TLS_IE_32:
1666 if (ELF32_R_TYPE (rel->r_info) == r_type)
1667 tls_type = GOT_TLS_IE_NEG;
1668 else
1669 /* If this is a GD->IE transition, we may use either of
1670 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1671 tls_type = GOT_TLS_IE;
1672 break;
1673 case R_386_TLS_IE:
1674 case R_386_TLS_GOTIE:
1675 tls_type = GOT_TLS_IE_POS; break;
1676 }
1677
1678 if (h != NULL)
1679 {
1680 h->got.refcount = 1;
1681 old_tls_type = elf_x86_hash_entry (h)->tls_type;
1682 }
1683 else
1684 {
1685 bfd_signed_vma *local_got_refcounts;
1686
1687 /* This is a global offset table entry for a local symbol. */
1688 local_got_refcounts = elf_local_got_refcounts (abfd);
1689 if (local_got_refcounts == NULL)
1690 {
1691 bfd_size_type size;
1692
1693 size = symtab_hdr->sh_info;
1694 size *= (sizeof (bfd_signed_vma)
1695 + sizeof (bfd_vma) + sizeof(char));
1696 local_got_refcounts = (bfd_signed_vma *)
1697 bfd_zalloc (abfd, size);
1698 if (local_got_refcounts == NULL)
1699 goto error_return;
1700 elf_local_got_refcounts (abfd) = local_got_refcounts;
1701 elf_x86_local_tlsdesc_gotent (abfd)
1702 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1703 elf_x86_local_got_tls_type (abfd)
1704 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1705 }
1706 local_got_refcounts[r_symndx] = 1;
1707 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1708 }
1709
1710 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1711 tls_type |= old_tls_type;
1712 /* If a TLS symbol is accessed using IE at least once,
1713 there is no point to use dynamic model for it. */
1714 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1715 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1716 || (tls_type & GOT_TLS_IE) == 0))
1717 {
1718 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1719 tls_type = old_tls_type;
1720 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1721 && GOT_TLS_GD_ANY_P (tls_type))
1722 tls_type |= old_tls_type;
1723 else
1724 {
1725 if (h)
1726 name = h->root.root.string;
1727 else
1728 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1729 NULL);
1730 _bfd_error_handler
1731 /* xgettext:c-format */
1732 (_("%pB: `%s' accessed both as normal and "
1733 "thread local symbol"),
1734 abfd, name);
1735 bfd_set_error (bfd_error_bad_value);
1736 goto error_return;
1737 }
1738 }
1739
1740 if (old_tls_type != tls_type)
1741 {
1742 if (h != NULL)
1743 elf_x86_hash_entry (h)->tls_type = tls_type;
1744 else
1745 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1746 }
1747 }
1748 /* Fall through */
1749
1750 case R_386_GOTOFF:
1751 case R_386_GOTPC:
1752 create_got:
1753 if (r_type != R_386_TLS_IE)
1754 {
1755 if (eh != NULL)
1756 {
1757 eh->zero_undefweak &= 0x2;
1758
1759 /* Need GOT to resolve undefined weak symbol to 0. */
1760 if (r_type == R_386_GOTOFF
1761 && h->root.type == bfd_link_hash_undefweak
1762 && bfd_link_executable (info))
1763 htab->got_referenced = true;
1764 }
1765 break;
1766 }
1767 /* Fall through */
1768
1769 case R_386_TLS_LE_32:
1770 case R_386_TLS_LE:
1771 if (eh != NULL)
1772 eh->zero_undefweak &= 0x2;
1773 if (bfd_link_executable (info))
1774 break;
1775 info->flags |= DF_STATIC_TLS;
1776 goto do_relocation;
1777
1778 case R_386_32:
1779 case R_386_PC32:
1780 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1781 eh->zero_undefweak |= 0x2;
1782 do_relocation:
1783 /* We are called after all symbols have been resolved. Only
1784 relocation against STT_GNU_IFUNC symbol must go through
1785 PLT. */
1786 if (h != NULL
1787 && (bfd_link_executable (info)
1788 || h->type == STT_GNU_IFUNC))
1789 {
1790 bool func_pointer_ref = false;
1791
1792 if (r_type == R_386_PC32)
1793 {
1794 /* Since something like ".long foo - ." may be used
1795 as pointer, make sure that PLT is used if foo is
1796 a function defined in a shared library. */
1797 if ((sec->flags & SEC_CODE) == 0)
1798 h->pointer_equality_needed = 1;
1799 else if (h->type == STT_GNU_IFUNC
1800 && bfd_link_pic (info))
1801 {
1802 _bfd_error_handler
1803 /* xgettext:c-format */
1804 (_("%pB: unsupported non-PIC call to IFUNC `%s'"),
1805 abfd, h->root.root.string);
1806 bfd_set_error (bfd_error_bad_value);
1807 goto error_return;
1808 }
1809 }
1810 else
1811 {
1812 h->pointer_equality_needed = 1;
1813 /* R_386_32 can be resolved at run-time. */
1814 if (r_type == R_386_32
1815 && (sec->flags & SEC_READONLY) == 0)
1816 func_pointer_ref = true;
1817 }
1818
1819 if (!func_pointer_ref)
1820 {
1821 /* If this reloc is in a read-only section, we might
1822 need a copy reloc. We can't check reliably at this
1823 stage whether the section is read-only, as input
1824 sections have not yet been mapped to output sections.
1825 Tentatively set the flag for now, and correct in
1826 adjust_dynamic_symbol. */
1827 h->non_got_ref = 1;
1828
1829 /* We may need a .plt entry if the symbol is a function
1830 defined in a shared lib or is a function referenced
1831 from the code or read-only section. */
1832 if (!h->def_regular
1833 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1834 h->plt.refcount = 1;
1835 }
1836 }
1837
1838 size_reloc = false;
1839 do_size:
1840 if (!no_dynreloc
1841 && NEED_DYNAMIC_RELOCATION_P (info, false, h, sec, r_type,
1842 R_386_32))
1843 {
1844 struct elf_dyn_relocs *p;
1845 struct elf_dyn_relocs **head;
1846
1847 /* We must copy these reloc types into the output file.
1848 Create a reloc section in dynobj and make room for
1849 this reloc. */
1850 if (sreloc == NULL)
1851 {
1852 sreloc = _bfd_elf_make_dynamic_reloc_section
1853 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ false);
1854
1855 if (sreloc == NULL)
1856 goto error_return;
1857 }
1858
1859 /* If this is a global symbol, we count the number of
1860 relocations we need for this symbol. */
1861 if (h != NULL)
1862 {
1863 head = &h->dyn_relocs;
1864 }
1865 else
1866 {
1867 /* Track dynamic relocs needed for local syms too.
1868 We really need local syms available to do this
1869 easily. Oh well. */
1870 void **vpp;
1871 asection *s;
1872
1873 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1874 abfd, r_symndx);
1875 if (isym == NULL)
1876 goto error_return;
1877
1878 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1879 if (s == NULL)
1880 s = sec;
1881
1882 vpp = &elf_section_data (s)->local_dynrel;
1883 head = (struct elf_dyn_relocs **)vpp;
1884 }
1885
1886 p = *head;
1887 if (p == NULL || p->sec != sec)
1888 {
1889 size_t amt = sizeof *p;
1890 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1891 amt);
1892 if (p == NULL)
1893 goto error_return;
1894 p->next = *head;
1895 *head = p;
1896 p->sec = sec;
1897 p->count = 0;
1898 p->pc_count = 0;
1899 }
1900
1901 p->count += 1;
1902 /* Count size relocation as PC-relative relocation. */
1903 if (r_type == R_386_PC32 || size_reloc)
1904 p->pc_count += 1;
1905 }
1906 break;
1907
1908 /* This relocation describes the C++ object vtable hierarchy.
1909 Reconstruct it for later use during GC. */
1910 case R_386_GNU_VTINHERIT:
1911 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1912 goto error_return;
1913 break;
1914
1915 /* This relocation describes which C++ vtable entries are actually
1916 used. Record for later use during GC. */
1917 case R_386_GNU_VTENTRY:
1918 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1919 goto error_return;
1920 break;
1921
1922 default:
1923 break;
1924 }
1925 }
1926
1927 if (elf_section_data (sec)->this_hdr.contents != contents)
1928 {
1929 if (!converted && !info->keep_memory)
1930 free (contents);
1931 else
1932 {
1933 /* Cache the section contents for elf_link_input_bfd if any
1934 load is converted or --no-keep-memory isn't used. */
1935 elf_section_data (sec)->this_hdr.contents = contents;
1936 }
1937 }
1938
1939 /* Cache relocations if any load is converted. */
1940 if (elf_section_data (sec)->relocs != relocs && converted)
1941 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
1942
1943 return true;
1944
1945 error_return:
1946 if (elf_section_data (sec)->this_hdr.contents != contents)
1947 free (contents);
1948 sec->check_relocs_failed = 1;
1949 return false;
1950 }
1951
1952 /* Set the correct type for an x86 ELF section. We do this by the
1953 section name, which is a hack, but ought to work. */
1954
1955 static bool
1956 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1957 Elf_Internal_Shdr *hdr,
1958 asection *sec)
1959 {
1960 const char *name;
1961
1962 name = bfd_section_name (sec);
1963
1964 /* This is an ugly, but unfortunately necessary hack that is
1965 needed when producing EFI binaries on x86. It tells
1966 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1967 containing ELF relocation info. We need this hack in order to
1968 be able to generate ELF binaries that can be translated into
1969 EFI applications (which are essentially COFF objects). Those
1970 files contain a COFF ".reloc" section inside an ELFNN object,
1971 which would normally cause BFD to segfault because it would
1972 attempt to interpret this section as containing relocation
1973 entries for section "oc". With this hack enabled, ".reloc"
1974 will be treated as a normal data section, which will avoid the
1975 segfault. However, you won't be able to create an ELFNN binary
1976 with a section named "oc" that needs relocations, but that's
1977 the kind of ugly side-effects you get when detecting section
1978 types based on their names... In practice, this limitation is
1979 unlikely to bite. */
1980 if (strcmp (name, ".reloc") == 0)
1981 hdr->sh_type = SHT_PROGBITS;
1982
1983 return true;
1984 }
1985
1986 /* Return the relocation value for @tpoff relocation
1987 if STT_TLS virtual address is ADDRESS. */
1988
1989 static bfd_vma
1990 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
1991 {
1992 struct elf_link_hash_table *htab = elf_hash_table (info);
1993 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
1994 bfd_vma static_tls_size;
1995
1996 /* If tls_sec is NULL, we should have signalled an error already. */
1997 if (htab->tls_sec == NULL)
1998 return 0;
1999
2000 /* Consider special static TLS alignment requirements. */
2001 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2002 return static_tls_size + htab->tls_sec->vma - address;
2003 }
2004
2005 /* Relocate an i386 ELF section. */
2006
2007 static int
2008 elf_i386_relocate_section (bfd *output_bfd,
2009 struct bfd_link_info *info,
2010 bfd *input_bfd,
2011 asection *input_section,
2012 bfd_byte *contents,
2013 Elf_Internal_Rela *relocs,
2014 Elf_Internal_Sym *local_syms,
2015 asection **local_sections)
2016 {
2017 struct elf_x86_link_hash_table *htab;
2018 Elf_Internal_Shdr *symtab_hdr;
2019 struct elf_link_hash_entry **sym_hashes;
2020 bfd_vma *local_got_offsets;
2021 bfd_vma *local_tlsdesc_gotents;
2022 Elf_Internal_Rela *rel;
2023 Elf_Internal_Rela *wrel;
2024 Elf_Internal_Rela *relend;
2025 bool is_vxworks_tls;
2026 unsigned plt_entry_size;
2027
2028 /* Skip if check_relocs failed. */
2029 if (input_section->check_relocs_failed)
2030 return false;
2031
2032 htab = elf_x86_hash_table (info, I386_ELF_DATA);
2033 if (htab == NULL)
2034 return false;
2035
2036 if (!is_x86_elf (input_bfd, htab))
2037 {
2038 bfd_set_error (bfd_error_wrong_format);
2039 return false;
2040 }
2041
2042 symtab_hdr = &elf_symtab_hdr (input_bfd);
2043 sym_hashes = elf_sym_hashes (input_bfd);
2044 local_got_offsets = elf_local_got_offsets (input_bfd);
2045 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2046 /* We have to handle relocations in vxworks .tls_vars sections
2047 specially, because the dynamic loader is 'weird'. */
2048 is_vxworks_tls = (htab->elf.target_os == is_vxworks
2049 && bfd_link_pic (info)
2050 && !strcmp (input_section->output_section->name,
2051 ".tls_vars"));
2052
2053 _bfd_x86_elf_set_tls_module_base (info);
2054
2055 plt_entry_size = htab->plt.plt_entry_size;
2056
2057 rel = wrel = relocs;
2058 relend = relocs + input_section->reloc_count;
2059 for (; rel < relend; wrel++, rel++)
2060 {
2061 unsigned int r_type, r_type_tls;
2062 reloc_howto_type *howto;
2063 unsigned long r_symndx;
2064 struct elf_link_hash_entry *h;
2065 struct elf_x86_link_hash_entry *eh;
2066 Elf_Internal_Sym *sym;
2067 asection *sec;
2068 bfd_vma off, offplt, plt_offset;
2069 bfd_vma relocation;
2070 bool unresolved_reloc;
2071 bfd_reloc_status_type r;
2072 unsigned int indx;
2073 int tls_type;
2074 bfd_vma st_size;
2075 asection *resolved_plt;
2076 bool resolved_to_zero;
2077 bool relative_reloc;
2078
2079 r_type = ELF32_R_TYPE (rel->r_info);
2080 if (r_type == R_386_GNU_VTINHERIT
2081 || r_type == R_386_GNU_VTENTRY)
2082 {
2083 if (wrel != rel)
2084 *wrel = *rel;
2085 continue;
2086 }
2087
2088 howto = elf_i386_rtype_to_howto (input_bfd, r_type);
2089 if (howto == NULL)
2090 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2091
2092 r_symndx = ELF32_R_SYM (rel->r_info);
2093 h = NULL;
2094 sym = NULL;
2095 sec = NULL;
2096 unresolved_reloc = false;
2097 if (r_symndx < symtab_hdr->sh_info)
2098 {
2099 sym = local_syms + r_symndx;
2100 sec = local_sections[r_symndx];
2101 relocation = (sec->output_section->vma
2102 + sec->output_offset
2103 + sym->st_value);
2104 st_size = sym->st_size;
2105
2106 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2107 && ((sec->flags & SEC_MERGE) != 0
2108 || (bfd_link_relocatable (info)
2109 && sec->output_offset != 0)))
2110 {
2111 bfd_vma addend;
2112 bfd_byte *where = contents + rel->r_offset;
2113
2114 switch (howto->size)
2115 {
2116 case 0:
2117 addend = bfd_get_8 (input_bfd, where);
2118 if (howto->pc_relative)
2119 {
2120 addend = (addend ^ 0x80) - 0x80;
2121 addend += 1;
2122 }
2123 break;
2124 case 1:
2125 addend = bfd_get_16 (input_bfd, where);
2126 if (howto->pc_relative)
2127 {
2128 addend = (addend ^ 0x8000) - 0x8000;
2129 addend += 2;
2130 }
2131 break;
2132 case 2:
2133 addend = bfd_get_32 (input_bfd, where);
2134 if (howto->pc_relative)
2135 {
2136 addend = (addend ^ 0x80000000) - 0x80000000;
2137 addend += 4;
2138 }
2139 break;
2140 default:
2141 abort ();
2142 }
2143
2144 if (bfd_link_relocatable (info))
2145 addend += sec->output_offset;
2146 else
2147 {
2148 asection *msec = sec;
2149 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2150 addend);
2151 addend -= relocation;
2152 addend += msec->output_section->vma + msec->output_offset;
2153 }
2154
2155 switch (howto->size)
2156 {
2157 case 0:
2158 /* FIXME: overflow checks. */
2159 if (howto->pc_relative)
2160 addend -= 1;
2161 bfd_put_8 (input_bfd, addend, where);
2162 break;
2163 case 1:
2164 if (howto->pc_relative)
2165 addend -= 2;
2166 bfd_put_16 (input_bfd, addend, where);
2167 break;
2168 case 2:
2169 if (howto->pc_relative)
2170 addend -= 4;
2171 bfd_put_32 (input_bfd, addend, where);
2172 break;
2173 }
2174 }
2175 else if (!bfd_link_relocatable (info)
2176 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2177 {
2178 /* Relocate against local STT_GNU_IFUNC symbol. */
2179 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2180 false);
2181 if (h == NULL)
2182 abort ();
2183
2184 /* Set STT_GNU_IFUNC symbol value. */
2185 h->root.u.def.value = sym->st_value;
2186 h->root.u.def.section = sec;
2187 }
2188 }
2189 else
2190 {
2191 bool warned ATTRIBUTE_UNUSED;
2192 bool ignored ATTRIBUTE_UNUSED;
2193
2194 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2195 r_symndx, symtab_hdr, sym_hashes,
2196 h, sec, relocation,
2197 unresolved_reloc, warned, ignored);
2198 st_size = h->size;
2199 }
2200
2201 if (sec != NULL && discarded_section (sec))
2202 {
2203 _bfd_clear_contents (howto, input_bfd, input_section,
2204 contents, rel->r_offset);
2205 wrel->r_offset = rel->r_offset;
2206 wrel->r_info = 0;
2207 wrel->r_addend = 0;
2208
2209 /* For ld -r, remove relocations in debug sections against
2210 sections defined in discarded sections. Not done for
2211 eh_frame editing code expects to be present. */
2212 if (bfd_link_relocatable (info)
2213 && (input_section->flags & SEC_DEBUGGING))
2214 wrel--;
2215
2216 continue;
2217 }
2218
2219 if (bfd_link_relocatable (info))
2220 {
2221 if (wrel != rel)
2222 *wrel = *rel;
2223 continue;
2224 }
2225
2226 eh = (struct elf_x86_link_hash_entry *) h;
2227
2228 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2229 it here if it is defined in a non-shared object. */
2230 if (h != NULL
2231 && h->type == STT_GNU_IFUNC
2232 && h->def_regular)
2233 {
2234 asection *gotplt, *base_got;
2235 bfd_vma plt_index;
2236 const char *name;
2237
2238 if ((input_section->flags & SEC_ALLOC) == 0)
2239 {
2240 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2241 STT_GNU_IFUNC symbol as STT_FUNC. */
2242 if (elf_section_type (input_section) == SHT_NOTE)
2243 goto skip_ifunc;
2244 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2245 sections because such sections are not SEC_ALLOC and
2246 thus ld.so will not process them. */
2247 if ((input_section->flags & SEC_DEBUGGING) != 0)
2248 continue;
2249 abort ();
2250 }
2251
2252 /* STT_GNU_IFUNC symbol must go through PLT. */
2253 if (htab->elf.splt != NULL)
2254 {
2255 if (htab->plt_second != NULL)
2256 {
2257 resolved_plt = htab->plt_second;
2258 plt_offset = eh->plt_second.offset;
2259 }
2260 else
2261 {
2262 resolved_plt = htab->elf.splt;
2263 plt_offset = h->plt.offset;
2264 }
2265 gotplt = htab->elf.sgotplt;
2266 }
2267 else
2268 {
2269 resolved_plt = htab->elf.iplt;
2270 plt_offset = h->plt.offset;
2271 gotplt = htab->elf.igotplt;
2272 }
2273
2274 switch (r_type)
2275 {
2276 default:
2277 break;
2278
2279 case R_386_GOT32:
2280 case R_386_GOT32X:
2281 base_got = htab->elf.sgot;
2282 off = h->got.offset;
2283
2284 if (base_got == NULL)
2285 abort ();
2286
2287 if (off == (bfd_vma) -1)
2288 {
2289 /* We can't use h->got.offset here to save state, or
2290 even just remember the offset, as finish_dynamic_symbol
2291 would use that as offset into .got. */
2292
2293 if (h->plt.offset == (bfd_vma) -1)
2294 abort ();
2295
2296 if (htab->elf.splt != NULL)
2297 {
2298 plt_index = (h->plt.offset / plt_entry_size
2299 - htab->plt.has_plt0);
2300 off = (plt_index + 3) * 4;
2301 base_got = htab->elf.sgotplt;
2302 }
2303 else
2304 {
2305 plt_index = h->plt.offset / plt_entry_size;
2306 off = plt_index * 4;
2307 base_got = htab->elf.igotplt;
2308 }
2309
2310 if (h->dynindx == -1
2311 || h->forced_local
2312 || info->symbolic)
2313 {
2314 /* This references the local defitionion. We must
2315 initialize this entry in the global offset table.
2316 Since the offset must always be a multiple of 8,
2317 we use the least significant bit to record
2318 whether we have initialized it already.
2319
2320 When doing a dynamic link, we create a .rela.got
2321 relocation entry to initialize the value. This
2322 is done in the finish_dynamic_symbol routine. */
2323 if ((off & 1) != 0)
2324 off &= ~1;
2325 else
2326 {
2327 bfd_put_32 (output_bfd, relocation,
2328 base_got->contents + off);
2329 h->got.offset |= 1;
2330 }
2331 }
2332
2333 relocation = off;
2334 }
2335 else
2336 relocation = (base_got->output_section->vma
2337 + base_got->output_offset + off
2338 - gotplt->output_section->vma
2339 - gotplt->output_offset);
2340
2341 if (rel->r_offset > 1
2342 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2343 && *(contents + rel->r_offset - 2) != 0x8d)
2344 {
2345 if (bfd_link_pic (info))
2346 goto disallow_got32;
2347
2348 /* Add the GOT base if there is no base register. */
2349 relocation += (gotplt->output_section->vma
2350 + gotplt->output_offset);
2351 }
2352 else if (htab->elf.splt == NULL)
2353 {
2354 /* Adjust for static executables. */
2355 relocation += gotplt->output_offset;
2356 }
2357
2358 goto do_relocation;
2359 }
2360
2361 if (h->plt.offset == (bfd_vma) -1)
2362 {
2363 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2364 if (r_type == R_386_32
2365 && (input_section->flags & SEC_CODE) == 0)
2366 goto do_ifunc_pointer;
2367 goto bad_ifunc_reloc;
2368 }
2369
2370 relocation = (resolved_plt->output_section->vma
2371 + resolved_plt->output_offset + plt_offset);
2372
2373 switch (r_type)
2374 {
2375 default:
2376 bad_ifunc_reloc:
2377 if (h->root.root.string)
2378 name = h->root.root.string;
2379 else
2380 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2381 NULL);
2382 _bfd_error_handler
2383 /* xgettext:c-format */
2384 (_("%pB: relocation %s against STT_GNU_IFUNC "
2385 "symbol `%s' isn't supported"), input_bfd,
2386 howto->name, name);
2387 bfd_set_error (bfd_error_bad_value);
2388 return false;
2389
2390 case R_386_32:
2391 /* Generate dynamic relcoation only when there is a
2392 non-GOT reference in a shared object. */
2393 if ((bfd_link_pic (info) && h->non_got_ref)
2394 || h->plt.offset == (bfd_vma) -1)
2395 {
2396 Elf_Internal_Rela outrel;
2397 asection *sreloc;
2398 bfd_vma offset;
2399
2400 do_ifunc_pointer:
2401 /* Need a dynamic relocation to get the real function
2402 adddress. */
2403 offset = _bfd_elf_section_offset (output_bfd,
2404 info,
2405 input_section,
2406 rel->r_offset);
2407 if (offset == (bfd_vma) -1
2408 || offset == (bfd_vma) -2)
2409 abort ();
2410
2411 outrel.r_offset = (input_section->output_section->vma
2412 + input_section->output_offset
2413 + offset);
2414
2415 if (POINTER_LOCAL_IFUNC_P (info, h))
2416 {
2417 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2418 h->root.root.string,
2419 h->root.u.def.section->owner);
2420
2421 /* This symbol is resolved locally. */
2422 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2423
2424 if (htab->params->report_relative_reloc)
2425 _bfd_x86_elf_link_report_relative_reloc
2426 (info, input_section, h, sym,
2427 "R_386_IRELATIVE", &outrel);
2428
2429 bfd_put_32 (output_bfd,
2430 (h->root.u.def.value
2431 + h->root.u.def.section->output_section->vma
2432 + h->root.u.def.section->output_offset),
2433 contents + offset);
2434 }
2435 else
2436 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2437
2438 /* Dynamic relocations are stored in
2439 1. .rel.ifunc section in PIC object.
2440 2. .rel.got section in dynamic executable.
2441 3. .rel.iplt section in static executable. */
2442 if (bfd_link_pic (info))
2443 sreloc = htab->elf.irelifunc;
2444 else if (htab->elf.splt != NULL)
2445 sreloc = htab->elf.srelgot;
2446 else
2447 sreloc = htab->elf.irelplt;
2448 elf_append_rel (output_bfd, sreloc, &outrel);
2449
2450 /* If this reloc is against an external symbol, we
2451 do not want to fiddle with the addend. Otherwise,
2452 we need to include the symbol value so that it
2453 becomes an addend for the dynamic reloc. For an
2454 internal symbol, we have updated addend. */
2455 continue;
2456 }
2457 /* FALLTHROUGH */
2458 case R_386_PC32:
2459 case R_386_PLT32:
2460 goto do_relocation;
2461
2462 case R_386_GOTOFF:
2463 relocation -= (gotplt->output_section->vma
2464 + gotplt->output_offset);
2465 goto do_relocation;
2466 }
2467 }
2468
2469 skip_ifunc:
2470 resolved_to_zero = (eh != NULL
2471 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2472
2473 switch (r_type)
2474 {
2475 case R_386_GOT32X:
2476 case R_386_GOT32:
2477 /* Relocation is to the entry for this symbol in the global
2478 offset table. */
2479 if (htab->elf.sgot == NULL)
2480 abort ();
2481
2482 relative_reloc = false;
2483 if (h != NULL)
2484 {
2485 off = h->got.offset;
2486 if (RESOLVED_LOCALLY_P (info, h, htab))
2487 {
2488 /* We must initialize this entry in the global offset
2489 table. Since the offset must always be a multiple
2490 of 4, we use the least significant bit to record
2491 whether we have initialized it already.
2492
2493 When doing a dynamic link, we create a .rel.got
2494 relocation entry to initialize the value. This
2495 is done in the finish_dynamic_symbol routine. */
2496 if ((off & 1) != 0)
2497 off &= ~1;
2498 else
2499 {
2500 bfd_put_32 (output_bfd, relocation,
2501 htab->elf.sgot->contents + off);
2502 h->got.offset |= 1;
2503
2504 if (GENERATE_RELATIVE_RELOC_P (info, h))
2505 {
2506 /* PR ld/21402: If this symbol isn't dynamic
2507 in PIC, generate R_386_RELATIVE here. */
2508 eh->no_finish_dynamic_symbol = 1;
2509 relative_reloc = true;
2510 }
2511 }
2512 }
2513 else
2514 unresolved_reloc = false;
2515 }
2516 else
2517 {
2518 if (local_got_offsets == NULL)
2519 abort ();
2520
2521 off = local_got_offsets[r_symndx];
2522
2523 /* The offset must always be a multiple of 4. We use
2524 the least significant bit to record whether we have
2525 already generated the necessary reloc. */
2526 if ((off & 1) != 0)
2527 off &= ~1;
2528 else
2529 {
2530 bfd_put_32 (output_bfd, relocation,
2531 htab->elf.sgot->contents + off);
2532 local_got_offsets[r_symndx] |= 1;
2533
2534 if (bfd_link_pic (info))
2535 relative_reloc = true;
2536 }
2537 }
2538
2539 if (relative_reloc)
2540 {
2541 asection *s;
2542 Elf_Internal_Rela outrel;
2543
2544 s = htab->elf.srelgot;
2545 if (s == NULL)
2546 abort ();
2547
2548 outrel.r_offset = (htab->elf.sgot->output_section->vma
2549 + htab->elf.sgot->output_offset
2550 + off);
2551 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2552
2553 if (htab->params->report_relative_reloc)
2554 _bfd_x86_elf_link_report_relative_reloc
2555 (info, input_section, h, sym, "R_386_RELATIVE",
2556 &outrel);
2557
2558 elf_append_rel (output_bfd, s, &outrel);
2559 }
2560
2561 if (off >= (bfd_vma) -2)
2562 abort ();
2563
2564 relocation = (htab->elf.sgot->output_section->vma
2565 + htab->elf.sgot->output_offset + off);
2566 if (rel->r_offset > 1
2567 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2568 && *(contents + rel->r_offset - 2) != 0x8d)
2569 {
2570 if (bfd_link_pic (info))
2571 {
2572 /* For PIC, disallow R_386_GOT32 without a base
2573 register, except for "lea foo@GOT, %reg", since
2574 we don't know what the GOT base is. */
2575 const char *name;
2576
2577 disallow_got32:
2578 if (h == NULL || h->root.root.string == NULL)
2579 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2580 NULL);
2581 else
2582 name = h->root.root.string;
2583
2584 _bfd_error_handler
2585 /* xgettext:c-format */
2586 (_("%pB: direct GOT relocation %s against `%s'"
2587 " without base register can not be used"
2588 " when making a shared object"),
2589 input_bfd, howto->name, name);
2590 bfd_set_error (bfd_error_bad_value);
2591 return false;
2592 }
2593 }
2594 else
2595 {
2596 /* Subtract the .got.plt section address only with a base
2597 register. */
2598 relocation -= (htab->elf.sgotplt->output_section->vma
2599 + htab->elf.sgotplt->output_offset);
2600 }
2601
2602 break;
2603
2604 case R_386_GOTOFF:
2605 /* Relocation is relative to the start of the global offset
2606 table. */
2607
2608 /* Check to make sure it isn't a protected function or data
2609 symbol for shared library since it may not be local when
2610 used as function address or with copy relocation. We also
2611 need to make sure that a symbol is referenced locally. */
2612 if (!bfd_link_executable (info) && h)
2613 {
2614 if (!h->def_regular)
2615 {
2616 const char *v;
2617
2618 switch (ELF_ST_VISIBILITY (h->other))
2619 {
2620 case STV_HIDDEN:
2621 v = _("hidden symbol");
2622 break;
2623 case STV_INTERNAL:
2624 v = _("internal symbol");
2625 break;
2626 case STV_PROTECTED:
2627 v = _("protected symbol");
2628 break;
2629 default:
2630 v = _("symbol");
2631 break;
2632 }
2633
2634 _bfd_error_handler
2635 /* xgettext:c-format */
2636 (_("%pB: relocation R_386_GOTOFF against undefined %s"
2637 " `%s' can not be used when making a shared object"),
2638 input_bfd, v, h->root.root.string);
2639 bfd_set_error (bfd_error_bad_value);
2640 return false;
2641 }
2642 else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2643 && (h->type == STT_FUNC
2644 || h->type == STT_OBJECT)
2645 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2646 {
2647 _bfd_error_handler
2648 /* xgettext:c-format */
2649 (_("%pB: relocation R_386_GOTOFF against protected %s"
2650 " `%s' can not be used when making a shared object"),
2651 input_bfd,
2652 h->type == STT_FUNC ? "function" : "data",
2653 h->root.root.string);
2654 bfd_set_error (bfd_error_bad_value);
2655 return false;
2656 }
2657 }
2658
2659 /* Note that sgot is not involved in this
2660 calculation. We always want the start of .got.plt. If we
2661 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2662 permitted by the ABI, we might have to change this
2663 calculation. */
2664 relocation -= htab->elf.sgotplt->output_section->vma
2665 + htab->elf.sgotplt->output_offset;
2666 break;
2667
2668 case R_386_GOTPC:
2669 /* Use global offset table as symbol value. */
2670 relocation = htab->elf.sgotplt->output_section->vma
2671 + htab->elf.sgotplt->output_offset;
2672 unresolved_reloc = false;
2673 break;
2674
2675 case R_386_PLT32:
2676 /* Relocation is to the entry for this symbol in the
2677 procedure linkage table. */
2678
2679 /* Resolve a PLT32 reloc against a local symbol directly,
2680 without using the procedure linkage table. */
2681 if (h == NULL)
2682 break;
2683
2684 if ((h->plt.offset == (bfd_vma) -1
2685 && eh->plt_got.offset == (bfd_vma) -1)
2686 || htab->elf.splt == NULL)
2687 {
2688 /* We didn't make a PLT entry for this symbol. This
2689 happens when statically linking PIC code, or when
2690 using -Bsymbolic. */
2691 break;
2692 }
2693
2694 if (h->plt.offset != (bfd_vma) -1)
2695 {
2696 if (htab->plt_second != NULL)
2697 {
2698 resolved_plt = htab->plt_second;
2699 plt_offset = eh->plt_second.offset;
2700 }
2701 else
2702 {
2703 resolved_plt = htab->elf.splt;
2704 plt_offset = h->plt.offset;
2705 }
2706 }
2707 else
2708 {
2709 resolved_plt = htab->plt_got;
2710 plt_offset = eh->plt_got.offset;
2711 }
2712
2713 relocation = (resolved_plt->output_section->vma
2714 + resolved_plt->output_offset
2715 + plt_offset);
2716 unresolved_reloc = false;
2717 break;
2718
2719 case R_386_SIZE32:
2720 /* Set to symbol size. */
2721 relocation = st_size;
2722 /* Fall through. */
2723
2724 case R_386_32:
2725 case R_386_PC32:
2726 if ((input_section->flags & SEC_ALLOC) == 0
2727 || is_vxworks_tls)
2728 break;
2729
2730 if (GENERATE_DYNAMIC_RELOCATION_P (info, eh, r_type, sec,
2731 false, resolved_to_zero,
2732 (r_type == R_386_PC32)))
2733 {
2734 Elf_Internal_Rela outrel;
2735 bool skip, relocate;
2736 asection *sreloc;
2737
2738 /* When generating a shared object, these relocations
2739 are copied into the output file to be resolved at run
2740 time. */
2741
2742 skip = false;
2743 relocate = false;
2744
2745 outrel.r_offset =
2746 _bfd_elf_section_offset (output_bfd, info, input_section,
2747 rel->r_offset);
2748 if (outrel.r_offset == (bfd_vma) -1)
2749 skip = true;
2750 else if (outrel.r_offset == (bfd_vma) -2)
2751 skip = true, relocate = true;
2752 outrel.r_offset += (input_section->output_section->vma
2753 + input_section->output_offset);
2754
2755 if (skip)
2756 memset (&outrel, 0, sizeof outrel);
2757 else if (COPY_INPUT_RELOC_P (info, h, r_type))
2758 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2759 else
2760 {
2761 /* This symbol is local, or marked to become local. */
2762 relocate = true;
2763 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2764
2765 if (htab->params->report_relative_reloc)
2766 _bfd_x86_elf_link_report_relative_reloc
2767 (info, input_section, h, sym, "R_386_RELATIVE",
2768 &outrel);
2769 }
2770
2771 sreloc = elf_section_data (input_section)->sreloc;
2772
2773 if (sreloc == NULL || sreloc->contents == NULL)
2774 {
2775 r = bfd_reloc_notsupported;
2776 goto check_relocation_error;
2777 }
2778
2779 elf_append_rel (output_bfd, sreloc, &outrel);
2780
2781 /* If this reloc is against an external symbol, we do
2782 not want to fiddle with the addend. Otherwise, we
2783 need to include the symbol value so that it becomes
2784 an addend for the dynamic reloc. */
2785 if (! relocate)
2786 continue;
2787 }
2788 break;
2789
2790 case R_386_TLS_IE:
2791 if (!bfd_link_executable (info))
2792 {
2793 Elf_Internal_Rela outrel;
2794 asection *sreloc;
2795
2796 outrel.r_offset = rel->r_offset
2797 + input_section->output_section->vma
2798 + input_section->output_offset;
2799 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2800
2801 if (htab->params->report_relative_reloc)
2802 _bfd_x86_elf_link_report_relative_reloc
2803 (info, input_section, h, sym, "R_386_RELATIVE",
2804 &outrel);
2805
2806 sreloc = elf_section_data (input_section)->sreloc;
2807 if (sreloc == NULL)
2808 abort ();
2809 elf_append_rel (output_bfd, sreloc, &outrel);
2810 }
2811 /* Fall through */
2812
2813 case R_386_TLS_GD:
2814 case R_386_TLS_GOTDESC:
2815 case R_386_TLS_DESC_CALL:
2816 case R_386_TLS_IE_32:
2817 case R_386_TLS_GOTIE:
2818 tls_type = GOT_UNKNOWN;
2819 if (h == NULL && local_got_offsets)
2820 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2821 else if (h != NULL)
2822 tls_type = elf_x86_hash_entry(h)->tls_type;
2823 if (tls_type == GOT_TLS_IE)
2824 tls_type = GOT_TLS_IE_NEG;
2825
2826 r_type_tls = r_type;
2827 if (! elf_i386_tls_transition (info, input_bfd,
2828 input_section, contents,
2829 symtab_hdr, sym_hashes,
2830 &r_type_tls, tls_type, rel,
2831 relend, h, r_symndx, true))
2832 return false;
2833
2834 if (r_type_tls == R_386_TLS_LE_32)
2835 {
2836 BFD_ASSERT (! unresolved_reloc);
2837 if (r_type == R_386_TLS_GD)
2838 {
2839 unsigned int type;
2840 bfd_vma roff;
2841
2842 /* GD->LE transition. */
2843 type = *(contents + rel->r_offset - 2);
2844 if (type == 0x04)
2845 {
2846 /* Change
2847 leal foo@tlsgd(,%ebx,1), %eax
2848 call ___tls_get_addr@PLT
2849 into:
2850 movl %gs:0, %eax
2851 subl $foo@tpoff, %eax
2852 (6 byte form of subl). */
2853 roff = rel->r_offset + 5;
2854 }
2855 else
2856 {
2857 /* Change
2858 leal foo@tlsgd(%ebx), %eax
2859 call ___tls_get_addr@PLT
2860 nop
2861 or
2862 leal foo@tlsgd(%reg), %eax
2863 call *___tls_get_addr@GOT(%reg)
2864 which may be converted to
2865 addr32 call ___tls_get_addr
2866 into:
2867 movl %gs:0, %eax; subl $foo@tpoff, %eax
2868 (6 byte form of subl). */
2869 roff = rel->r_offset + 6;
2870 }
2871 memcpy (contents + roff - 8,
2872 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2873 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2874 contents + roff);
2875 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
2876 rel++;
2877 wrel++;
2878 continue;
2879 }
2880 else if (r_type == R_386_TLS_GOTDESC)
2881 {
2882 /* GDesc -> LE transition.
2883 It's originally something like:
2884 leal x@tlsdesc(%ebx), %eax
2885
2886 leal x@ntpoff, %eax
2887
2888 Registers other than %eax may be set up here. */
2889
2890 unsigned int val;
2891 bfd_vma roff;
2892
2893 roff = rel->r_offset;
2894 val = bfd_get_8 (input_bfd, contents + roff - 1);
2895
2896 /* Now modify the instruction as appropriate. */
2897 /* aoliva FIXME: remove the above and xor the byte
2898 below with 0x86. */
2899 bfd_put_8 (output_bfd, val ^ 0x86,
2900 contents + roff - 1);
2901 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2902 contents + roff);
2903 continue;
2904 }
2905 else if (r_type == R_386_TLS_DESC_CALL)
2906 {
2907 /* GDesc -> LE transition.
2908 It's originally:
2909 call *(%eax)
2910 Turn it into:
2911 xchg %ax,%ax */
2912
2913 bfd_vma roff;
2914
2915 roff = rel->r_offset;
2916 bfd_put_8 (output_bfd, 0x66, contents + roff);
2917 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2918 continue;
2919 }
2920 else if (r_type == R_386_TLS_IE)
2921 {
2922 unsigned int val;
2923
2924 /* IE->LE transition:
2925 Originally it can be one of:
2926 movl foo, %eax
2927 movl foo, %reg
2928 addl foo, %reg
2929 We change it into:
2930 movl $foo, %eax
2931 movl $foo, %reg
2932 addl $foo, %reg. */
2933 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2934 if (val == 0xa1)
2935 {
2936 /* movl foo, %eax. */
2937 bfd_put_8 (output_bfd, 0xb8,
2938 contents + rel->r_offset - 1);
2939 }
2940 else
2941 {
2942 unsigned int type;
2943
2944 type = bfd_get_8 (input_bfd,
2945 contents + rel->r_offset - 2);
2946 switch (type)
2947 {
2948 case 0x8b:
2949 /* movl */
2950 bfd_put_8 (output_bfd, 0xc7,
2951 contents + rel->r_offset - 2);
2952 bfd_put_8 (output_bfd,
2953 0xc0 | ((val >> 3) & 7),
2954 contents + rel->r_offset - 1);
2955 break;
2956 case 0x03:
2957 /* addl */
2958 bfd_put_8 (output_bfd, 0x81,
2959 contents + rel->r_offset - 2);
2960 bfd_put_8 (output_bfd,
2961 0xc0 | ((val >> 3) & 7),
2962 contents + rel->r_offset - 1);
2963 break;
2964 default:
2965 BFD_FAIL ();
2966 break;
2967 }
2968 }
2969 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2970 contents + rel->r_offset);
2971 continue;
2972 }
2973 else
2974 {
2975 unsigned int val, type;
2976
2977 /* {IE_32,GOTIE}->LE transition:
2978 Originally it can be one of:
2979 subl foo(%reg1), %reg2
2980 movl foo(%reg1), %reg2
2981 addl foo(%reg1), %reg2
2982 We change it into:
2983 subl $foo, %reg2
2984 movl $foo, %reg2 (6 byte form)
2985 addl $foo, %reg2. */
2986 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2987 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2988 if (type == 0x8b)
2989 {
2990 /* movl */
2991 bfd_put_8 (output_bfd, 0xc7,
2992 contents + rel->r_offset - 2);
2993 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2994 contents + rel->r_offset - 1);
2995 }
2996 else if (type == 0x2b)
2997 {
2998 /* subl */
2999 bfd_put_8 (output_bfd, 0x81,
3000 contents + rel->r_offset - 2);
3001 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3002 contents + rel->r_offset - 1);
3003 }
3004 else if (type == 0x03)
3005 {
3006 /* addl */
3007 bfd_put_8 (output_bfd, 0x81,
3008 contents + rel->r_offset - 2);
3009 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3010 contents + rel->r_offset - 1);
3011 }
3012 else
3013 BFD_FAIL ();
3014 if (r_type == R_386_TLS_GOTIE)
3015 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3016 contents + rel->r_offset);
3017 else
3018 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3019 contents + rel->r_offset);
3020 continue;
3021 }
3022 }
3023
3024 if (htab->elf.sgot == NULL)
3025 abort ();
3026
3027 if (h != NULL)
3028 {
3029 off = h->got.offset;
3030 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3031 }
3032 else
3033 {
3034 if (local_got_offsets == NULL)
3035 abort ();
3036
3037 off = local_got_offsets[r_symndx];
3038 offplt = local_tlsdesc_gotents[r_symndx];
3039 }
3040
3041 if ((off & 1) != 0)
3042 off &= ~1;
3043 else
3044 {
3045 Elf_Internal_Rela outrel;
3046 int dr_type;
3047 asection *sreloc;
3048
3049 if (htab->elf.srelgot == NULL)
3050 abort ();
3051
3052 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3053
3054 if (GOT_TLS_GDESC_P (tls_type))
3055 {
3056 bfd_byte *loc;
3057 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3058 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3059 <= htab->elf.sgotplt->size);
3060 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3061 + htab->elf.sgotplt->output_offset
3062 + offplt
3063 + htab->sgotplt_jump_table_size);
3064 sreloc = htab->elf.srelplt;
3065 loc = sreloc->contents;
3066 loc += (htab->next_tls_desc_index++
3067 * sizeof (Elf32_External_Rel));
3068 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3069 <= sreloc->contents + sreloc->size);
3070 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3071 if (indx == 0)
3072 {
3073 BFD_ASSERT (! unresolved_reloc);
3074 bfd_put_32 (output_bfd,
3075 relocation - _bfd_x86_elf_dtpoff_base (info),
3076 htab->elf.sgotplt->contents + offplt
3077 + htab->sgotplt_jump_table_size + 4);
3078 }
3079 else
3080 {
3081 bfd_put_32 (output_bfd, 0,
3082 htab->elf.sgotplt->contents + offplt
3083 + htab->sgotplt_jump_table_size + 4);
3084 }
3085 }
3086
3087 sreloc = htab->elf.srelgot;
3088
3089 outrel.r_offset = (htab->elf.sgot->output_section->vma
3090 + htab->elf.sgot->output_offset + off);
3091
3092 if (GOT_TLS_GD_P (tls_type))
3093 dr_type = R_386_TLS_DTPMOD32;
3094 else if (GOT_TLS_GDESC_P (tls_type))
3095 goto dr_done;
3096 else if (tls_type == GOT_TLS_IE_POS)
3097 dr_type = R_386_TLS_TPOFF;
3098 else
3099 dr_type = R_386_TLS_TPOFF32;
3100
3101 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3102 bfd_put_32 (output_bfd,
3103 relocation - _bfd_x86_elf_dtpoff_base (info),
3104 htab->elf.sgot->contents + off);
3105 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3106 bfd_put_32 (output_bfd,
3107 _bfd_x86_elf_dtpoff_base (info) - relocation,
3108 htab->elf.sgot->contents + off);
3109 else if (dr_type != R_386_TLS_DESC)
3110 bfd_put_32 (output_bfd, 0,
3111 htab->elf.sgot->contents + off);
3112 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3113
3114 elf_append_rel (output_bfd, sreloc, &outrel);
3115
3116 if (GOT_TLS_GD_P (tls_type))
3117 {
3118 if (indx == 0)
3119 {
3120 BFD_ASSERT (! unresolved_reloc);
3121 bfd_put_32 (output_bfd,
3122 relocation - _bfd_x86_elf_dtpoff_base (info),
3123 htab->elf.sgot->contents + off + 4);
3124 }
3125 else
3126 {
3127 bfd_put_32 (output_bfd, 0,
3128 htab->elf.sgot->contents + off + 4);
3129 outrel.r_info = ELF32_R_INFO (indx,
3130 R_386_TLS_DTPOFF32);
3131 outrel.r_offset += 4;
3132 elf_append_rel (output_bfd, sreloc, &outrel);
3133 }
3134 }
3135 else if (tls_type == GOT_TLS_IE_BOTH)
3136 {
3137 bfd_put_32 (output_bfd,
3138 (indx == 0
3139 ? relocation - _bfd_x86_elf_dtpoff_base (info)
3140 : 0),
3141 htab->elf.sgot->contents + off + 4);
3142 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3143 outrel.r_offset += 4;
3144 elf_append_rel (output_bfd, sreloc, &outrel);
3145 }
3146
3147 dr_done:
3148 if (h != NULL)
3149 h->got.offset |= 1;
3150 else
3151 local_got_offsets[r_symndx] |= 1;
3152 }
3153
3154 if (off >= (bfd_vma) -2
3155 && ! GOT_TLS_GDESC_P (tls_type))
3156 abort ();
3157 if (r_type_tls == R_386_TLS_GOTDESC
3158 || r_type_tls == R_386_TLS_DESC_CALL)
3159 {
3160 relocation = htab->sgotplt_jump_table_size + offplt;
3161 unresolved_reloc = false;
3162 }
3163 else if (r_type_tls == r_type)
3164 {
3165 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3166 + htab->elf.sgotplt->output_offset;
3167 relocation = htab->elf.sgot->output_section->vma
3168 + htab->elf.sgot->output_offset + off - g_o_t;
3169 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3170 && tls_type == GOT_TLS_IE_BOTH)
3171 relocation += 4;
3172 if (r_type == R_386_TLS_IE)
3173 relocation += g_o_t;
3174 unresolved_reloc = false;
3175 }
3176 else if (r_type == R_386_TLS_GD)
3177 {
3178 unsigned int val, type;
3179 bfd_vma roff;
3180
3181 /* GD->IE transition. */
3182 type = *(contents + rel->r_offset - 2);
3183 val = *(contents + rel->r_offset - 1);
3184 if (type == 0x04)
3185 {
3186 /* Change
3187 leal foo@tlsgd(,%ebx,1), %eax
3188 call ___tls_get_addr@PLT
3189 into:
3190 movl %gs:0, %eax
3191 subl $foo@gottpoff(%ebx), %eax. */
3192 val >>= 3;
3193 roff = rel->r_offset - 3;
3194 }
3195 else
3196 {
3197 /* Change
3198 leal foo@tlsgd(%ebx), %eax
3199 call ___tls_get_addr@PLT
3200 nop
3201 or
3202 leal foo@tlsgd(%reg), %eax
3203 call *___tls_get_addr@GOT(%reg)
3204 which may be converted to
3205 addr32 call ___tls_get_addr
3206 into:
3207 movl %gs:0, %eax;
3208 subl $foo@gottpoff(%reg), %eax. */
3209 roff = rel->r_offset - 2;
3210 }
3211 memcpy (contents + roff,
3212 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3213 contents[roff + 7] = 0x80 | (val & 7);
3214 /* If foo is used only with foo@gotntpoff(%reg) and
3215 foo@indntpoff, but not with foo@gottpoff(%reg), change
3216 subl $foo@gottpoff(%reg), %eax
3217 into:
3218 addl $foo@gotntpoff(%reg), %eax. */
3219 if (tls_type == GOT_TLS_IE_POS)
3220 contents[roff + 6] = 0x03;
3221 bfd_put_32 (output_bfd,
3222 htab->elf.sgot->output_section->vma
3223 + htab->elf.sgot->output_offset + off
3224 - htab->elf.sgotplt->output_section->vma
3225 - htab->elf.sgotplt->output_offset,
3226 contents + roff + 8);
3227 /* Skip R_386_PLT32 and R_386_GOT32X. */
3228 rel++;
3229 wrel++;
3230 continue;
3231 }
3232 else if (r_type == R_386_TLS_GOTDESC)
3233 {
3234 /* GDesc -> IE transition.
3235 It's originally something like:
3236 leal x@tlsdesc(%ebx), %eax
3237
3238 Change it to:
3239 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3240 or:
3241 movl x@gottpoff(%ebx), %eax # before negl %eax
3242
3243 Registers other than %eax may be set up here. */
3244
3245 bfd_vma roff;
3246
3247 /* First, make sure it's a leal adding ebx to a 32-bit
3248 offset into any register, although it's probably
3249 almost always going to be eax. */
3250 roff = rel->r_offset;
3251
3252 /* Now modify the instruction as appropriate. */
3253 /* To turn a leal into a movl in the form we use it, it
3254 suffices to change the first byte from 0x8d to 0x8b.
3255 aoliva FIXME: should we decide to keep the leal, all
3256 we have to do is remove the statement below, and
3257 adjust the relaxation of R_386_TLS_DESC_CALL. */
3258 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3259
3260 if (tls_type == GOT_TLS_IE_BOTH)
3261 off += 4;
3262
3263 bfd_put_32 (output_bfd,
3264 htab->elf.sgot->output_section->vma
3265 + htab->elf.sgot->output_offset + off
3266 - htab->elf.sgotplt->output_section->vma
3267 - htab->elf.sgotplt->output_offset,
3268 contents + roff);
3269 continue;
3270 }
3271 else if (r_type == R_386_TLS_DESC_CALL)
3272 {
3273 /* GDesc -> IE transition.
3274 It's originally:
3275 call *(%eax)
3276
3277 Change it to:
3278 xchg %ax,%ax
3279 or
3280 negl %eax
3281 depending on how we transformed the TLS_GOTDESC above.
3282 */
3283
3284 bfd_vma roff;
3285
3286 roff = rel->r_offset;
3287
3288 /* Now modify the instruction as appropriate. */
3289 if (tls_type != GOT_TLS_IE_NEG)
3290 {
3291 /* xchg %ax,%ax */
3292 bfd_put_8 (output_bfd, 0x66, contents + roff);
3293 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3294 }
3295 else
3296 {
3297 /* negl %eax */
3298 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3299 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3300 }
3301
3302 continue;
3303 }
3304 else
3305 BFD_ASSERT (false);
3306 break;
3307
3308 case R_386_TLS_LDM:
3309 if (! elf_i386_tls_transition (info, input_bfd,
3310 input_section, contents,
3311 symtab_hdr, sym_hashes,
3312 &r_type, GOT_UNKNOWN, rel,
3313 relend, h, r_symndx, true))
3314 return false;
3315
3316 if (r_type != R_386_TLS_LDM)
3317 {
3318 /* LD->LE transition. Change
3319 leal foo@tlsldm(%ebx) %eax
3320 call ___tls_get_addr@PLT
3321 into:
3322 movl %gs:0, %eax
3323 nop
3324 leal 0(%esi,1), %esi
3325 or change
3326 leal foo@tlsldm(%reg) %eax
3327 call *___tls_get_addr@GOT(%reg)
3328 which may be converted to
3329 addr32 call ___tls_get_addr
3330 into:
3331 movl %gs:0, %eax
3332 leal 0(%esi), %esi */
3333 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3334 if (*(contents + rel->r_offset + 4) == 0xff
3335 || *(contents + rel->r_offset + 4) == 0x67)
3336 memcpy (contents + rel->r_offset - 2,
3337 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3338 else
3339 memcpy (contents + rel->r_offset - 2,
3340 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3341 /* Skip R_386_PC32/R_386_PLT32. */
3342 rel++;
3343 wrel++;
3344 continue;
3345 }
3346
3347 if (htab->elf.sgot == NULL)
3348 abort ();
3349
3350 off = htab->tls_ld_or_ldm_got.offset;
3351 if (off & 1)
3352 off &= ~1;
3353 else
3354 {
3355 Elf_Internal_Rela outrel;
3356
3357 if (htab->elf.srelgot == NULL)
3358 abort ();
3359
3360 outrel.r_offset = (htab->elf.sgot->output_section->vma
3361 + htab->elf.sgot->output_offset + off);
3362
3363 bfd_put_32 (output_bfd, 0,
3364 htab->elf.sgot->contents + off);
3365 bfd_put_32 (output_bfd, 0,
3366 htab->elf.sgot->contents + off + 4);
3367 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3368 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3369 htab->tls_ld_or_ldm_got.offset |= 1;
3370 }
3371 relocation = htab->elf.sgot->output_section->vma
3372 + htab->elf.sgot->output_offset + off
3373 - htab->elf.sgotplt->output_section->vma
3374 - htab->elf.sgotplt->output_offset;
3375 unresolved_reloc = false;
3376 break;
3377
3378 case R_386_TLS_LDO_32:
3379 if (!bfd_link_executable (info)
3380 || (input_section->flags & SEC_CODE) == 0)
3381 relocation -= _bfd_x86_elf_dtpoff_base (info);
3382 else
3383 /* When converting LDO to LE, we must negate. */
3384 relocation = -elf_i386_tpoff (info, relocation);
3385 break;
3386
3387 case R_386_TLS_LE_32:
3388 case R_386_TLS_LE:
3389 if (!bfd_link_executable (info))
3390 {
3391 Elf_Internal_Rela outrel;
3392 asection *sreloc;
3393
3394 outrel.r_offset = rel->r_offset
3395 + input_section->output_section->vma
3396 + input_section->output_offset;
3397 if (h != NULL && h->dynindx != -1)
3398 indx = h->dynindx;
3399 else
3400 indx = 0;
3401 if (r_type == R_386_TLS_LE_32)
3402 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3403 else
3404 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3405 sreloc = elf_section_data (input_section)->sreloc;
3406 if (sreloc == NULL)
3407 abort ();
3408 elf_append_rel (output_bfd, sreloc, &outrel);
3409 if (indx)
3410 continue;
3411 else if (r_type == R_386_TLS_LE_32)
3412 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3413 else
3414 relocation -= _bfd_x86_elf_dtpoff_base (info);
3415 }
3416 else if (r_type == R_386_TLS_LE_32)
3417 relocation = elf_i386_tpoff (info, relocation);
3418 else
3419 relocation = -elf_i386_tpoff (info, relocation);
3420 break;
3421
3422 default:
3423 break;
3424 }
3425
3426 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3427 because such sections are not SEC_ALLOC and thus ld.so will
3428 not process them. */
3429 if (unresolved_reloc
3430 && !((input_section->flags & SEC_DEBUGGING) != 0
3431 && h->def_dynamic)
3432 && _bfd_elf_section_offset (output_bfd, info, input_section,
3433 rel->r_offset) != (bfd_vma) -1)
3434 {
3435 _bfd_error_handler
3436 /* xgettext:c-format */
3437 (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation against symbol `%s'"),
3438 input_bfd,
3439 input_section,
3440 (uint64_t) rel->r_offset,
3441 howto->name,
3442 h->root.root.string);
3443 return false;
3444 }
3445
3446 do_relocation:
3447 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3448 contents, rel->r_offset,
3449 relocation, 0);
3450
3451 check_relocation_error:
3452 if (r != bfd_reloc_ok)
3453 {
3454 const char *name;
3455
3456 if (h != NULL)
3457 name = h->root.root.string;
3458 else
3459 {
3460 name = bfd_elf_string_from_elf_section (input_bfd,
3461 symtab_hdr->sh_link,
3462 sym->st_name);
3463 if (name == NULL)
3464 return false;
3465 if (*name == '\0')
3466 name = bfd_section_name (sec);
3467 }
3468
3469 if (r == bfd_reloc_overflow)
3470 (*info->callbacks->reloc_overflow)
3471 (info, (h ? &h->root : NULL), name, howto->name,
3472 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3473 else
3474 {
3475 _bfd_error_handler
3476 /* xgettext:c-format */
3477 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
3478 input_bfd, input_section,
3479 (uint64_t) rel->r_offset, name, (int) r);
3480 return false;
3481 }
3482 }
3483
3484 if (wrel != rel)
3485 *wrel = *rel;
3486 }
3487
3488 if (wrel != rel)
3489 {
3490 Elf_Internal_Shdr *rel_hdr;
3491 size_t deleted = rel - wrel;
3492
3493 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3494 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3495 if (rel_hdr->sh_size == 0)
3496 {
3497 /* It is too late to remove an empty reloc section. Leave
3498 one NONE reloc.
3499 ??? What is wrong with an empty section??? */
3500 rel_hdr->sh_size = rel_hdr->sh_entsize;
3501 deleted -= 1;
3502 }
3503 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3504 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3505 input_section->reloc_count -= deleted;
3506 }
3507
3508 return true;
3509 }
3510
3511 /* Finish up dynamic symbol handling. We set the contents of various
3512 dynamic sections here. */
3513
3514 static bool
3515 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3516 struct bfd_link_info *info,
3517 struct elf_link_hash_entry *h,
3518 Elf_Internal_Sym *sym)
3519 {
3520 struct elf_x86_link_hash_table *htab;
3521 unsigned plt_entry_size;
3522 struct elf_x86_link_hash_entry *eh;
3523 bool local_undefweak;
3524 bool use_plt_second;
3525
3526 htab = elf_x86_hash_table (info, I386_ELF_DATA);
3527 if (htab == NULL)
3528 return false;
3529
3530 plt_entry_size = htab->plt.plt_entry_size;
3531
3532 /* Use the second PLT section only if there is .plt section. */
3533 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3534
3535 eh = (struct elf_x86_link_hash_entry *) h;
3536 if (eh->no_finish_dynamic_symbol)
3537 abort ();
3538
3539 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3540 resolved undefined weak symbols in executable so that their
3541 references have value 0 at run-time. */
3542 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3543
3544 if (h->plt.offset != (bfd_vma) -1)
3545 {
3546 bfd_vma plt_index, plt_offset;
3547 bfd_vma got_offset;
3548 Elf_Internal_Rela rel;
3549 bfd_byte *loc;
3550 asection *plt, *resolved_plt, *gotplt, *relplt;
3551
3552 /* When building a static executable, use .iplt, .igot.plt and
3553 .rel.iplt sections for STT_GNU_IFUNC symbols. */
3554 if (htab->elf.splt != NULL)
3555 {
3556 plt = htab->elf.splt;
3557 gotplt = htab->elf.sgotplt;
3558 relplt = htab->elf.srelplt;
3559 }
3560 else
3561 {
3562 plt = htab->elf.iplt;
3563 gotplt = htab->elf.igotplt;
3564 relplt = htab->elf.irelplt;
3565 }
3566
3567 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
3568
3569 /* Get the index in the procedure linkage table which
3570 corresponds to this symbol. This is the index of this symbol
3571 in all the symbols for which we are making plt entries. The
3572 first entry in the procedure linkage table is reserved.
3573
3574 Get the offset into the .got table of the entry that
3575 corresponds to this function. Each .got entry is 4 bytes.
3576 The first three are reserved.
3577
3578 For static executables, we don't reserve anything. */
3579
3580 if (plt == htab->elf.splt)
3581 {
3582 got_offset = (h->plt.offset / plt_entry_size
3583 - htab->plt.has_plt0);
3584 got_offset = (got_offset + 3) * 4;
3585 }
3586 else
3587 {
3588 got_offset = h->plt.offset / plt_entry_size;
3589 got_offset = got_offset * 4;
3590 }
3591
3592 /* Fill in the entry in the procedure linkage table and update
3593 the first slot. */
3594 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3595 plt_entry_size);
3596
3597 if (use_plt_second)
3598 {
3599 const bfd_byte *plt_entry;
3600 if (bfd_link_pic (info))
3601 plt_entry = htab->non_lazy_plt->pic_plt_entry;
3602 else
3603 plt_entry = htab->non_lazy_plt->plt_entry;
3604 memcpy (htab->plt_second->contents + eh->plt_second.offset,
3605 plt_entry, htab->non_lazy_plt->plt_entry_size);
3606
3607 resolved_plt = htab->plt_second;
3608 plt_offset = eh->plt_second.offset;
3609 }
3610 else
3611 {
3612 resolved_plt = plt;
3613 plt_offset = h->plt.offset;
3614 }
3615
3616 if (! bfd_link_pic (info))
3617 {
3618 bfd_put_32 (output_bfd,
3619 (gotplt->output_section->vma
3620 + gotplt->output_offset
3621 + got_offset),
3622 resolved_plt->contents + plt_offset
3623 + htab->plt.plt_got_offset);
3624
3625 if (htab->elf.target_os == is_vxworks)
3626 {
3627 int s, k, reloc_index;
3628
3629 /* Create the R_386_32 relocation referencing the GOT
3630 for this PLT entry. */
3631
3632 /* S: Current slot number (zero-based). */
3633 s = ((h->plt.offset - htab->plt.plt_entry_size)
3634 / htab->plt.plt_entry_size);
3635 /* K: Number of relocations for PLTResolve. */
3636 if (bfd_link_pic (info))
3637 k = PLTRESOLVE_RELOCS_SHLIB;
3638 else
3639 k = PLTRESOLVE_RELOCS;
3640 /* Skip the PLTresolve relocations, and the relocations for
3641 the other PLT slots. */
3642 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3643 loc = (htab->srelplt2->contents + reloc_index
3644 * sizeof (Elf32_External_Rel));
3645
3646 rel.r_offset = (plt->output_section->vma
3647 + plt->output_offset
3648 + h->plt.offset + 2),
3649 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3650 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3651
3652 /* Create the R_386_32 relocation referencing the beginning of
3653 the PLT for this GOT entry. */
3654 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3655 + htab->elf.sgotplt->output_offset
3656 + got_offset);
3657 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3658 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3659 loc + sizeof (Elf32_External_Rel));
3660 }
3661 }
3662 else
3663 {
3664 bfd_put_32 (output_bfd, got_offset,
3665 resolved_plt->contents + plt_offset
3666 + htab->plt.plt_got_offset);
3667 }
3668
3669 /* Fill in the entry in the global offset table. Leave the entry
3670 as zero for undefined weak symbol in PIE. No PLT relocation
3671 against undefined weak symbol in PIE. */
3672 if (!local_undefweak)
3673 {
3674 if (htab->plt.has_plt0)
3675 bfd_put_32 (output_bfd,
3676 (plt->output_section->vma
3677 + plt->output_offset
3678 + h->plt.offset
3679 + htab->lazy_plt->plt_lazy_offset),
3680 gotplt->contents + got_offset);
3681
3682 /* Fill in the entry in the .rel.plt section. */
3683 rel.r_offset = (gotplt->output_section->vma
3684 + gotplt->output_offset
3685 + got_offset);
3686 if (PLT_LOCAL_IFUNC_P (info, h))
3687 {
3688 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3689 h->root.root.string,
3690 h->root.u.def.section->owner);
3691
3692 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3693 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3694 in the .got.plt section. */
3695 bfd_put_32 (output_bfd,
3696 (h->root.u.def.value
3697 + h->root.u.def.section->output_section->vma
3698 + h->root.u.def.section->output_offset),
3699 gotplt->contents + got_offset);
3700 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3701
3702 if (htab->params->report_relative_reloc)
3703 _bfd_x86_elf_link_report_relative_reloc
3704 (info, relplt, h, sym, "R_386_IRELATIVE", &rel);
3705
3706 /* R_386_IRELATIVE comes last. */
3707 plt_index = htab->next_irelative_index--;
3708 }
3709 else
3710 {
3711 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3712 plt_index = htab->next_jump_slot_index++;
3713 }
3714
3715 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3716 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3717
3718 /* Don't fill the second and third slots in PLT entry for
3719 static executables nor without PLT0. */
3720 if (plt == htab->elf.splt && htab->plt.has_plt0)
3721 {
3722 bfd_put_32 (output_bfd,
3723 plt_index * sizeof (Elf32_External_Rel),
3724 plt->contents + h->plt.offset
3725 + htab->lazy_plt->plt_reloc_offset);
3726 bfd_put_32 (output_bfd,
3727 - (h->plt.offset
3728 + htab->lazy_plt->plt_plt_offset + 4),
3729 (plt->contents + h->plt.offset
3730 + htab->lazy_plt->plt_plt_offset));
3731 }
3732 }
3733 }
3734 else if (eh->plt_got.offset != (bfd_vma) -1)
3735 {
3736 bfd_vma got_offset, plt_offset;
3737 asection *plt, *got, *gotplt;
3738 const bfd_byte *got_plt_entry;
3739
3740 /* Set the entry in the GOT procedure linkage table. */
3741 plt = htab->plt_got;
3742 got = htab->elf.sgot;
3743 gotplt = htab->elf.sgotplt;
3744 got_offset = h->got.offset;
3745
3746 if (got_offset == (bfd_vma) -1
3747 || plt == NULL
3748 || got == NULL
3749 || gotplt == NULL)
3750 abort ();
3751
3752 /* Fill in the entry in the GOT procedure linkage table. */
3753 if (! bfd_link_pic (info))
3754 {
3755 got_plt_entry = htab->non_lazy_plt->plt_entry;
3756 got_offset += got->output_section->vma + got->output_offset;
3757 }
3758 else
3759 {
3760 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3761 got_offset += (got->output_section->vma
3762 + got->output_offset
3763 - gotplt->output_section->vma
3764 - gotplt->output_offset);
3765 }
3766
3767 plt_offset = eh->plt_got.offset;
3768 memcpy (plt->contents + plt_offset, got_plt_entry,
3769 htab->non_lazy_plt->plt_entry_size);
3770 bfd_put_32 (output_bfd, got_offset,
3771 (plt->contents + plt_offset
3772 + htab->non_lazy_plt->plt_got_offset));
3773 }
3774
3775 if (!local_undefweak
3776 && !h->def_regular
3777 && (h->plt.offset != (bfd_vma) -1
3778 || eh->plt_got.offset != (bfd_vma) -1))
3779 {
3780 /* Mark the symbol as undefined, rather than as defined in
3781 the .plt section. Leave the value if there were any
3782 relocations where pointer equality matters (this is a clue
3783 for the dynamic linker, to make function pointer
3784 comparisons work between an application and shared
3785 library), otherwise set it to zero. If a function is only
3786 called from a binary, there is no need to slow down
3787 shared libraries because of that. */
3788 sym->st_shndx = SHN_UNDEF;
3789 if (!h->pointer_equality_needed)
3790 sym->st_value = 0;
3791 }
3792
3793 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
3794
3795 /* Don't generate dynamic GOT relocation against undefined weak
3796 symbol in executable. */
3797 if (h->got.offset != (bfd_vma) -1
3798 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3799 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3800 && !local_undefweak)
3801 {
3802 Elf_Internal_Rela rel;
3803 asection *relgot = htab->elf.srelgot;
3804 const char *relative_reloc_name = NULL;
3805
3806 /* This symbol has an entry in the global offset table. Set it
3807 up. */
3808
3809 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3810 abort ();
3811
3812 rel.r_offset = (htab->elf.sgot->output_section->vma
3813 + htab->elf.sgot->output_offset
3814 + (h->got.offset & ~(bfd_vma) 1));
3815
3816 /* If this is a static link, or it is a -Bsymbolic link and the
3817 symbol is defined locally or was forced to be local because
3818 of a version file, we just want to emit a RELATIVE reloc.
3819 The entry in the global offset table will already have been
3820 initialized in the relocate_section function. */
3821 if (h->def_regular
3822 && h->type == STT_GNU_IFUNC)
3823 {
3824 if (h->plt.offset == (bfd_vma) -1)
3825 {
3826 /* STT_GNU_IFUNC is referenced without PLT. */
3827 if (htab->elf.splt == NULL)
3828 {
3829 /* use .rel[a].iplt section to store .got relocations
3830 in static executable. */
3831 relgot = htab->elf.irelplt;
3832 }
3833 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3834 {
3835 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3836 h->root.root.string,
3837 h->root.u.def.section->owner);
3838
3839 bfd_put_32 (output_bfd,
3840 (h->root.u.def.value
3841 + h->root.u.def.section->output_section->vma
3842 + h->root.u.def.section->output_offset),
3843 htab->elf.sgot->contents + h->got.offset);
3844 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3845 relative_reloc_name = "R_386_IRELATIVE";
3846 }
3847 else
3848 goto do_glob_dat;
3849 }
3850 else if (bfd_link_pic (info))
3851 {
3852 /* Generate R_386_GLOB_DAT. */
3853 goto do_glob_dat;
3854 }
3855 else
3856 {
3857 asection *plt;
3858 bfd_vma plt_offset;
3859
3860 if (!h->pointer_equality_needed)
3861 abort ();
3862
3863 /* For non-shared object, we can't use .got.plt, which
3864 contains the real function addres if we need pointer
3865 equality. We load the GOT entry with the PLT entry. */
3866 if (htab->plt_second != NULL)
3867 {
3868 plt = htab->plt_second;
3869 plt_offset = eh->plt_second.offset;
3870 }
3871 else
3872 {
3873 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3874 plt_offset = h->plt.offset;
3875 }
3876 bfd_put_32 (output_bfd,
3877 (plt->output_section->vma
3878 + plt->output_offset + plt_offset),
3879 htab->elf.sgot->contents + h->got.offset);
3880 return true;
3881 }
3882 }
3883 else if (bfd_link_pic (info)
3884 && SYMBOL_REFERENCES_LOCAL_P (info, h))
3885 {
3886 BFD_ASSERT((h->got.offset & 1) != 0);
3887 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3888 relative_reloc_name = "R_386_RELATIVE";
3889 }
3890 else
3891 {
3892 BFD_ASSERT((h->got.offset & 1) == 0);
3893 do_glob_dat:
3894 bfd_put_32 (output_bfd, (bfd_vma) 0,
3895 htab->elf.sgot->contents + h->got.offset);
3896 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3897 }
3898
3899 if (relative_reloc_name != NULL
3900 && htab->params->report_relative_reloc)
3901 _bfd_x86_elf_link_report_relative_reloc
3902 (info, relgot, h, sym, relative_reloc_name, &rel);
3903
3904 elf_append_rel (output_bfd, relgot, &rel);
3905 }
3906
3907 if (h->needs_copy)
3908 {
3909 Elf_Internal_Rela rel;
3910 asection *s;
3911
3912 /* This symbol needs a copy reloc. Set it up. */
3913 VERIFY_COPY_RELOC (h, htab)
3914
3915 rel.r_offset = (h->root.u.def.value
3916 + h->root.u.def.section->output_section->vma
3917 + h->root.u.def.section->output_offset);
3918 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3919 if (h->root.u.def.section == htab->elf.sdynrelro)
3920 s = htab->elf.sreldynrelro;
3921 else
3922 s = htab->elf.srelbss;
3923 elf_append_rel (output_bfd, s, &rel);
3924 }
3925
3926 return true;
3927 }
3928
3929 /* Finish up local dynamic symbol handling. We set the contents of
3930 various dynamic sections here. */
3931
3932 static int
3933 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
3934 {
3935 struct elf_link_hash_entry *h
3936 = (struct elf_link_hash_entry *) *slot;
3937 struct bfd_link_info *info
3938 = (struct bfd_link_info *) inf;
3939
3940 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
3941 h, NULL);
3942 }
3943
3944 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
3945 here since undefined weak symbol may not be dynamic and may not be
3946 called for elf_i386_finish_dynamic_symbol. */
3947
3948 static bool
3949 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
3950 void *inf)
3951 {
3952 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
3953 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3954
3955 if (h->root.type != bfd_link_hash_undefweak
3956 || h->dynindx != -1)
3957 return true;
3958
3959 return elf_i386_finish_dynamic_symbol (info->output_bfd,
3960 info, h, NULL);
3961 }
3962
3963 /* Used to decide how to sort relocs in an optimal manner for the
3964 dynamic linker, before writing them out. */
3965
3966 static enum elf_reloc_type_class
3967 elf_i386_reloc_type_class (const struct bfd_link_info *info,
3968 const asection *rel_sec ATTRIBUTE_UNUSED,
3969 const Elf_Internal_Rela *rela)
3970 {
3971 bfd *abfd = info->output_bfd;
3972 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3973 struct elf_link_hash_table *htab = elf_hash_table (info);
3974
3975 if (htab->dynsym != NULL
3976 && htab->dynsym->contents != NULL)
3977 {
3978 /* Check relocation against STT_GNU_IFUNC symbol if there are
3979 dynamic symbols. */
3980 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
3981 if (r_symndx != STN_UNDEF)
3982 {
3983 Elf_Internal_Sym sym;
3984 if (!bed->s->swap_symbol_in (abfd,
3985 (htab->dynsym->contents
3986 + r_symndx * sizeof (Elf32_External_Sym)),
3987 0, &sym))
3988 abort ();
3989
3990 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
3991 return reloc_class_ifunc;
3992 }
3993 }
3994
3995 switch (ELF32_R_TYPE (rela->r_info))
3996 {
3997 case R_386_IRELATIVE:
3998 return reloc_class_ifunc;
3999 case R_386_RELATIVE:
4000 return reloc_class_relative;
4001 case R_386_JUMP_SLOT:
4002 return reloc_class_plt;
4003 case R_386_COPY:
4004 return reloc_class_copy;
4005 default:
4006 return reloc_class_normal;
4007 }
4008 }
4009
4010 /* Finish up the dynamic sections. */
4011
4012 static bool
4013 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4014 struct bfd_link_info *info)
4015 {
4016 struct elf_x86_link_hash_table *htab;
4017
4018 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4019 if (htab == NULL)
4020 return false;
4021
4022 if (!htab->elf.dynamic_sections_created)
4023 return true;
4024
4025 if (htab->elf.splt && htab->elf.splt->size > 0)
4026 {
4027 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4028 really seem like the right value. */
4029 elf_section_data (htab->elf.splt->output_section)
4030 ->this_hdr.sh_entsize = 4;
4031
4032 if (htab->plt.has_plt0)
4033 {
4034 /* Fill in the special first entry in the procedure linkage
4035 table. */
4036 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4037 htab->lazy_plt->plt0_entry_size);
4038 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4039 htab->plt0_pad_byte,
4040 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4041 if (!bfd_link_pic (info))
4042 {
4043 bfd_put_32 (output_bfd,
4044 (htab->elf.sgotplt->output_section->vma
4045 + htab->elf.sgotplt->output_offset
4046 + 4),
4047 htab->elf.splt->contents
4048 + htab->lazy_plt->plt0_got1_offset);
4049 bfd_put_32 (output_bfd,
4050 (htab->elf.sgotplt->output_section->vma
4051 + htab->elf.sgotplt->output_offset
4052 + 8),
4053 htab->elf.splt->contents
4054 + htab->lazy_plt->plt0_got2_offset);
4055
4056 if (htab->elf.target_os == is_vxworks)
4057 {
4058 Elf_Internal_Rela rel;
4059 int num_plts = (htab->elf.splt->size
4060 / htab->plt.plt_entry_size) - 1;
4061 unsigned char *p;
4062 asection *srelplt2 = htab->srelplt2;
4063
4064 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4065 + 4. On IA32 we use REL relocations so the
4066 addend goes in the PLT directly. */
4067 rel.r_offset = (htab->elf.splt->output_section->vma
4068 + htab->elf.splt->output_offset
4069 + htab->lazy_plt->plt0_got1_offset);
4070 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4071 R_386_32);
4072 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4073 srelplt2->contents);
4074 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4075 + 8. */
4076 rel.r_offset = (htab->elf.splt->output_section->vma
4077 + htab->elf.splt->output_offset
4078 + htab->lazy_plt->plt0_got2_offset);
4079 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4080 R_386_32);
4081 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4082 srelplt2->contents +
4083 sizeof (Elf32_External_Rel));
4084 /* Correct the .rel.plt.unloaded relocations. */
4085 p = srelplt2->contents;
4086 if (bfd_link_pic (info))
4087 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4088 else
4089 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4090
4091 for (; num_plts; num_plts--)
4092 {
4093 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4094 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4095 R_386_32);
4096 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4097 p += sizeof (Elf32_External_Rel);
4098
4099 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4100 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4101 R_386_32);
4102 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4103 p += sizeof (Elf32_External_Rel);
4104 }
4105 }
4106 }
4107 }
4108 }
4109
4110 /* Fill PLT entries for undefined weak symbols in PIE. */
4111 if (bfd_link_pie (info))
4112 bfd_hash_traverse (&info->hash->table,
4113 elf_i386_pie_finish_undefweak_symbol,
4114 info);
4115
4116 return true;
4117 }
4118
4119 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4120 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4121 It has to be done before elf_link_sort_relocs is called so that
4122 dynamic relocations are properly sorted. */
4123
4124 static bool
4125 elf_i386_output_arch_local_syms
4126 (bfd *output_bfd ATTRIBUTE_UNUSED,
4127 struct bfd_link_info *info,
4128 void *flaginfo ATTRIBUTE_UNUSED,
4129 int (*func) (void *, const char *,
4130 Elf_Internal_Sym *,
4131 asection *,
4132 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4133 {
4134 struct elf_x86_link_hash_table *htab
4135 = elf_x86_hash_table (info, I386_ELF_DATA);
4136 if (htab == NULL)
4137 return false;
4138
4139 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4140 htab_traverse (htab->loc_hash_table,
4141 elf_i386_finish_local_dynamic_symbol,
4142 info);
4143
4144 return true;
4145 }
4146
4147 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4148 dynamic relocations. */
4149
4150 static long
4151 elf_i386_get_synthetic_symtab (bfd *abfd,
4152 long symcount ATTRIBUTE_UNUSED,
4153 asymbol **syms ATTRIBUTE_UNUSED,
4154 long dynsymcount,
4155 asymbol **dynsyms,
4156 asymbol **ret)
4157 {
4158 long count, i, n;
4159 int j;
4160 bfd_byte *plt_contents;
4161 long relsize;
4162 const struct elf_x86_lazy_plt_layout *lazy_plt;
4163 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4164 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4165 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4166 asection *plt;
4167 bfd_vma got_addr;
4168 enum elf_x86_plt_type plt_type;
4169 struct elf_x86_plt plts[] =
4170 {
4171 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4172 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4173 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4174 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4175 };
4176
4177 *ret = NULL;
4178
4179 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4180 return 0;
4181
4182 if (dynsymcount <= 0)
4183 return 0;
4184
4185 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4186 if (relsize <= 0)
4187 return -1;
4188
4189 non_lazy_plt = NULL;
4190 /* Silence GCC 6. */
4191 lazy_plt = NULL;
4192 non_lazy_ibt_plt = NULL;
4193 lazy_ibt_plt = NULL;
4194 switch (get_elf_backend_data (abfd)->target_os)
4195 {
4196 case is_normal:
4197 case is_solaris:
4198 non_lazy_plt = &elf_i386_non_lazy_plt;
4199 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4200 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4201 /* Fall through */
4202 case is_vxworks:
4203 lazy_plt = &elf_i386_lazy_plt;
4204 break;
4205 default:
4206 abort ();
4207 }
4208
4209 got_addr = 0;
4210
4211 count = 0;
4212 for (j = 0; plts[j].name != NULL; j++)
4213 {
4214 plt = bfd_get_section_by_name (abfd, plts[j].name);
4215 if (plt == NULL || plt->size == 0)
4216 continue;
4217
4218 /* Get the PLT section contents. */
4219 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4220 if (plt_contents == NULL)
4221 break;
4222 if (!bfd_get_section_contents (abfd, (asection *) plt,
4223 plt_contents, 0, plt->size))
4224 {
4225 free (plt_contents);
4226 break;
4227 }
4228
4229 /* Check what kind of PLT it is. */
4230 plt_type = plt_unknown;
4231 if (plts[j].type == plt_unknown
4232 && (plt->size >= (lazy_plt->plt0_entry_size
4233 + lazy_plt->plt_entry_size)))
4234 {
4235 /* Match lazy PLT first. */
4236 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4237 lazy_plt->plt0_got1_offset) == 0)
4238 {
4239 /* The fist entry in the lazy IBT PLT is the same as the
4240 normal lazy PLT. */
4241 if (lazy_ibt_plt != NULL
4242 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4243 lazy_ibt_plt->plt_entry,
4244 lazy_ibt_plt->plt_got_offset) == 0))
4245 plt_type = plt_lazy | plt_second;
4246 else
4247 plt_type = plt_lazy;
4248 }
4249 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4250 lazy_plt->plt0_got1_offset) == 0)
4251 {
4252 /* The fist entry in the PIC lazy IBT PLT is the same as
4253 the normal PIC lazy PLT. */
4254 if (lazy_ibt_plt != NULL
4255 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4256 lazy_ibt_plt->pic_plt_entry,
4257 lazy_ibt_plt->plt_got_offset) == 0))
4258 plt_type = plt_lazy | plt_pic | plt_second;
4259 else
4260 plt_type = plt_lazy | plt_pic;
4261 }
4262 }
4263
4264 if (non_lazy_plt != NULL
4265 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4266 && plt->size >= non_lazy_plt->plt_entry_size)
4267 {
4268 /* Match non-lazy PLT. */
4269 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4270 non_lazy_plt->plt_got_offset) == 0)
4271 plt_type = plt_non_lazy;
4272 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4273 non_lazy_plt->plt_got_offset) == 0)
4274 plt_type = plt_pic;
4275 }
4276
4277 if ((non_lazy_ibt_plt != NULL)
4278 && (plt_type == plt_unknown || plt_type == plt_second)
4279 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4280 {
4281 if (memcmp (plt_contents,
4282 non_lazy_ibt_plt->plt_entry,
4283 non_lazy_ibt_plt->plt_got_offset) == 0)
4284 {
4285 /* Match IBT PLT. */
4286 plt_type = plt_second;
4287 non_lazy_plt = non_lazy_ibt_plt;
4288 }
4289 else if (memcmp (plt_contents,
4290 non_lazy_ibt_plt->pic_plt_entry,
4291 non_lazy_ibt_plt->plt_got_offset) == 0)
4292 {
4293 /* Match PIC IBT PLT. */
4294 plt_type = plt_second | plt_pic;
4295 non_lazy_plt = non_lazy_ibt_plt;
4296 }
4297 }
4298
4299 if (plt_type == plt_unknown)
4300 {
4301 free (plt_contents);
4302 continue;
4303 }
4304
4305 plts[j].sec = plt;
4306 plts[j].type = plt_type;
4307
4308 if ((plt_type & plt_lazy))
4309 {
4310 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4311 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4312 /* Skip PLT0 in lazy PLT. */
4313 i = 1;
4314 }
4315 else
4316 {
4317 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4318 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4319 i = 0;
4320 }
4321
4322 /* Skip lazy PLT when the second PLT is used. */
4323 if ((plt_type & (plt_lazy | plt_second))
4324 == (plt_lazy | plt_second))
4325 plts[j].count = 0;
4326 else
4327 {
4328 n = plt->size / plts[j].plt_entry_size;
4329 plts[j].count = n;
4330 count += n - i;
4331 }
4332
4333 plts[j].contents = plt_contents;
4334
4335 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4336 if ((plt_type & plt_pic))
4337 got_addr = (bfd_vma) -1;
4338 }
4339
4340 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4341 got_addr, plts, dynsyms,
4342 ret);
4343 }
4344
4345 /* Set up i386 GNU properties. Return the first relocatable ELF input
4346 with GNU properties if found. Otherwise, return NULL. */
4347
4348 static bfd *
4349 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4350 {
4351 struct elf_x86_init_table init_table;
4352
4353 switch (get_elf_backend_data (info->output_bfd)->target_os)
4354 {
4355 case is_normal:
4356 case is_solaris:
4357 init_table.plt0_pad_byte = 0x0;
4358 init_table.lazy_plt = &elf_i386_lazy_plt;
4359 init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4360 init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4361 init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4362 break;
4363 case is_vxworks:
4364 init_table.plt0_pad_byte = 0x90;
4365 init_table.lazy_plt = &elf_i386_lazy_plt;
4366 init_table.non_lazy_plt = NULL;
4367 init_table.lazy_ibt_plt = NULL;
4368 init_table.non_lazy_ibt_plt = NULL;
4369 break;
4370 default:
4371 abort ();
4372 }
4373
4374 init_table.r_info = elf32_r_info;
4375 init_table.r_sym = elf32_r_sym;
4376
4377 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4378 }
4379
4380 #define TARGET_LITTLE_SYM i386_elf32_vec
4381 #define TARGET_LITTLE_NAME "elf32-i386"
4382 #define ELF_ARCH bfd_arch_i386
4383 #define ELF_TARGET_ID I386_ELF_DATA
4384 #define ELF_MACHINE_CODE EM_386
4385 #define ELF_MAXPAGESIZE 0x1000
4386
4387 #define elf_backend_can_gc_sections 1
4388 #define elf_backend_can_refcount 1
4389 #define elf_backend_want_got_plt 1
4390 #define elf_backend_plt_readonly 1
4391 #define elf_backend_want_plt_sym 0
4392 #define elf_backend_got_header_size 12
4393 #define elf_backend_plt_alignment 4
4394 #define elf_backend_dtrel_excludes_plt 1
4395 #define elf_backend_extern_protected_data 1
4396 #define elf_backend_caches_rawsize 1
4397 #define elf_backend_want_dynrelro 1
4398
4399 /* Support RELA for objdump of prelink objects. */
4400 #define elf_info_to_howto elf_i386_info_to_howto_rel
4401 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4402
4403 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4404 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4405 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4406 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
4407
4408 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4409 #define elf_backend_check_relocs elf_i386_check_relocs
4410 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
4411 #define elf_backend_fake_sections elf_i386_fake_sections
4412 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4413 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4414 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
4415 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4416 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4417 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4418 #define elf_backend_relocate_section elf_i386_relocate_section
4419 #define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
4420 #define elf_backend_hide_symbol _bfd_x86_elf_hide_symbol
4421
4422 #define elf_backend_linux_prpsinfo32_ugid16 true
4423
4424 #define elf32_bed elf32_i386_bed
4425
4426 #include "elf32-target.h"
4427
4428 /* FreeBSD support. */
4429
4430 #undef TARGET_LITTLE_SYM
4431 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
4432 #undef TARGET_LITTLE_NAME
4433 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4434 #undef ELF_OSABI
4435 #define ELF_OSABI ELFOSABI_FREEBSD
4436
4437 /* The kernel recognizes executables as valid only if they carry a
4438 "FreeBSD" label in the ELF header. So we put this label on all
4439 executables and (for simplicity) also all other object files. */
4440
4441 static bool
4442 elf_i386_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
4443 {
4444 if (!_bfd_elf_init_file_header (abfd, info))
4445 return false;
4446
4447 #ifdef OLD_FREEBSD_ABI_LABEL
4448 {
4449 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4450 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4451 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4452 }
4453 #endif
4454 return true;
4455 }
4456
4457 #undef elf_backend_init_file_header
4458 #define elf_backend_init_file_header elf_i386_fbsd_init_file_header
4459 #undef elf32_bed
4460 #define elf32_bed elf32_i386_fbsd_bed
4461
4462 #undef elf_backend_add_symbol_hook
4463
4464 #include "elf32-target.h"
4465
4466 #undef elf_backend_init_file_header
4467
4468 /* Solaris 2. */
4469
4470 #undef TARGET_LITTLE_SYM
4471 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
4472 #undef TARGET_LITTLE_NAME
4473 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
4474
4475 #undef ELF_TARGET_OS
4476 #define ELF_TARGET_OS is_solaris
4477
4478 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4479 objects won't be recognized. */
4480 #undef ELF_OSABI
4481
4482 #undef elf32_bed
4483 #define elf32_bed elf32_i386_sol2_bed
4484
4485 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4486 boundary. */
4487 #undef elf_backend_static_tls_alignment
4488 #define elf_backend_static_tls_alignment 8
4489
4490 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4491
4492 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4493 File, p.63. */
4494 #undef elf_backend_want_plt_sym
4495 #define elf_backend_want_plt_sym 1
4496
4497 #undef elf_backend_strtab_flags
4498 #define elf_backend_strtab_flags SHF_STRINGS
4499
4500 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4501 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
4502 FALSE otherwise. ISECTION is the best guess matching section from the
4503 input bfd IBFD, but it might be NULL. */
4504
4505 static bool
4506 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4507 bfd *obfd ATTRIBUTE_UNUSED,
4508 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4509 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4510 {
4511 /* PR 19938: FIXME: Need to add code for setting the sh_info
4512 and sh_link fields of Solaris specific section types. */
4513 return false;
4514
4515 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4516 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4517
4518 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4519
4520 The following values should be set:
4521
4522 Type Link Info
4523 -----------------------------------------------------------------------------
4524 SHT_SUNW_ancillary The section header index of 0
4525 [0x6fffffee] the associated string table.
4526
4527 SHT_SUNW_capinfo The section header index of For a dynamic object, the
4528 [0x6ffffff0] the associated symbol table. section header index of
4529 the associated
4530 SHT_SUNW_capchain table,
4531 otherwise 0.
4532
4533 SHT_SUNW_symsort The section header index of 0
4534 [0x6ffffff1] the associated symbol table.
4535
4536 SHT_SUNW_tlssort The section header index of 0
4537 [0x6ffffff2] the associated symbol table.
4538
4539 SHT_SUNW_LDYNSYM The section header index of One greater than the
4540 [0x6ffffff3] the associated string table. symbol table index of the
4541 This index is the same string last local symbol,
4542 table used by the SHT_DYNSYM STB_LOCAL. Since
4543 section. SHT_SUNW_LDYNSYM only
4544 contains local symbols,
4545 sh_info is equivalent to
4546 the number of symbols in
4547 the table.
4548
4549 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4550 [0x6ffffff5] the section header index of to named strings, the
4551 the associated section header index of
4552 SHT_SUNW_capinfo table, the associated string
4553 otherwise 0. table, otherwise 0.
4554
4555 SHT_SUNW_move The section header index of 0
4556 [0x6ffffffa] the associated symbol table.
4557
4558 SHT_SUNW_COMDAT 0 0
4559 [0x6ffffffb]
4560
4561 SHT_SUNW_syminfo The section header index of The section header index
4562 [0x6ffffffc] the associated symbol table. of the associated
4563 .dynamic section.
4564
4565 SHT_SUNW_verdef The section header index of The number of version
4566 [0x6ffffffd] the associated string table. definitions within the
4567 section.
4568
4569 SHT_SUNW_verneed The section header index of The number of version
4570 [0x6ffffffe] the associated string table. dependencies within the
4571 section.
4572
4573 SHT_SUNW_versym The section header index of 0
4574 [0x6fffffff] the associated symbol table. */
4575 }
4576
4577 #undef elf_backend_copy_special_section_fields
4578 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4579
4580 #include "elf32-target.h"
4581
4582 /* Intel MCU support. */
4583
4584 static bool
4585 elf32_iamcu_elf_object_p (bfd *abfd)
4586 {
4587 /* Set the right machine number for an IAMCU elf32 file. */
4588 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4589 return true;
4590 }
4591
4592 #undef TARGET_LITTLE_SYM
4593 #define TARGET_LITTLE_SYM iamcu_elf32_vec
4594 #undef TARGET_LITTLE_NAME
4595 #define TARGET_LITTLE_NAME "elf32-iamcu"
4596 #undef ELF_ARCH
4597 #define ELF_ARCH bfd_arch_iamcu
4598
4599 #undef ELF_MACHINE_CODE
4600 #define ELF_MACHINE_CODE EM_IAMCU
4601
4602 #undef ELF_TARGET_OS
4603 #undef ELF_OSABI
4604
4605 #undef elf32_bed
4606 #define elf32_bed elf32_iamcu_bed
4607
4608 #undef elf_backend_object_p
4609 #define elf_backend_object_p elf32_iamcu_elf_object_p
4610
4611 #undef elf_backend_static_tls_alignment
4612
4613 #undef elf_backend_want_plt_sym
4614 #define elf_backend_want_plt_sym 0
4615
4616 #undef elf_backend_strtab_flags
4617 #undef elf_backend_copy_special_section_fields
4618
4619 #include "elf32-target.h"
4620
4621 /* Restore defaults. */
4622 #undef ELF_ARCH
4623 #define ELF_ARCH bfd_arch_i386
4624 #undef ELF_MACHINE_CODE
4625 #define ELF_MACHINE_CODE EM_386
4626 #undef elf_backend_object_p
4627
4628 /* VxWorks support. */
4629
4630 #undef TARGET_LITTLE_SYM
4631 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
4632 #undef TARGET_LITTLE_NAME
4633 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4634 #undef ELF_OSABI
4635 #undef ELF_MAXPAGESIZE
4636 #define ELF_MAXPAGESIZE 0x1000
4637 #undef elf_backend_plt_alignment
4638 #define elf_backend_plt_alignment 4
4639
4640 #undef ELF_TARGET_OS
4641 #define ELF_TARGET_OS is_vxworks
4642
4643 #undef elf_backend_relocs_compatible
4644 #undef elf_backend_add_symbol_hook
4645 #define elf_backend_add_symbol_hook \
4646 elf_vxworks_add_symbol_hook
4647 #undef elf_backend_link_output_symbol_hook
4648 #define elf_backend_link_output_symbol_hook \
4649 elf_vxworks_link_output_symbol_hook
4650 #undef elf_backend_emit_relocs
4651 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4652 #undef elf_backend_final_write_processing
4653 #define elf_backend_final_write_processing \
4654 elf_vxworks_final_write_processing
4655 #undef elf_backend_static_tls_alignment
4656
4657 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4658 define it. */
4659 #undef elf_backend_want_plt_sym
4660 #define elf_backend_want_plt_sym 1
4661
4662 #undef elf32_bed
4663 #define elf32_bed elf32_i386_vxworks_bed
4664
4665 #include "elf32-target.h"
This page took 0.132994 seconds and 5 git commands to generate.