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