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