x86: Update UNDEFINED_WEAK_RESOLVED_TO_ZERO
[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 bfd_boolean local_ref;
1233 struct elf_x86_link_hash_entry *eh;
1234
1235 if (roff < 2)
1236 return TRUE;
1237
1238 /* Addend for R_386_GOT32X relocations must be 0. */
1239 addend = bfd_get_32 (abfd, contents + roff);
1240 if (addend != 0)
1241 return TRUE;
1242
1243 htab = elf_x86_hash_table (link_info, I386_ELF_DATA);
1244 is_pic = bfd_link_pic (link_info);
1245
1246 r_type = *r_type_p;
1247 r_symndx = ELF32_R_SYM (irel->r_info);
1248
1249 modrm = bfd_get_8 (abfd, contents + roff - 1);
1250 baseless = (modrm & 0xc7) == 0x5;
1251
1252 if (baseless && is_pic)
1253 {
1254 /* For PIC, disallow R_386_GOT32X without a base register
1255 since we don't know what the GOT base is. */
1256 const char *name;
1257
1258 if (h == NULL)
1259 {
1260 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd,
1261 r_symndx);
1262 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1263 }
1264 else
1265 name = h->root.root.string;
1266
1267 _bfd_error_handler
1268 /* xgettext:c-format */
1269 (_("%B: direct GOT relocation R_386_GOT32X against `%s' without base"
1270 " register can not be used when making a shared object"),
1271 abfd, name);
1272 return FALSE;
1273 }
1274
1275 opcode = bfd_get_8 (abfd, contents + roff - 2);
1276
1277 /* Convert to R_386_32 if PIC is false or there is no base
1278 register. */
1279 to_reloc_32 = !is_pic || baseless;
1280
1281 eh = elf_x86_hash_entry (h);
1282
1283 /* Try to convert R_386_GOT32X. Get the symbol referred to by the
1284 reloc. */
1285 if (h == NULL)
1286 {
1287 if (opcode == 0x0ff)
1288 /* Convert "call/jmp *foo@GOT[(%reg)]". */
1289 goto convert_branch;
1290 else
1291 /* Convert "mov foo@GOT[(%reg1)], %reg2",
1292 "test %reg1, foo@GOT(%reg2)" and
1293 "binop foo@GOT[(%reg1)], %reg2". */
1294 goto convert_load;
1295 }
1296
1297 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1298 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1299
1300 /* Undefined weak symbol is only bound locally in executable
1301 and its reference is resolved as 0. */
1302 if (h->root.type == bfd_link_hash_undefweak
1303 && !eh->linker_def
1304 && local_ref)
1305 {
1306 if (opcode == 0xff)
1307 {
1308 /* No direct branch to 0 for PIC. */
1309 if (is_pic)
1310 return TRUE;
1311 else
1312 goto convert_branch;
1313 }
1314 else
1315 {
1316 /* We can convert load of address 0 to R_386_32. */
1317 to_reloc_32 = TRUE;
1318 goto convert_load;
1319 }
1320 }
1321
1322 if (opcode == 0xff)
1323 {
1324 /* We have "call/jmp *foo@GOT[(%reg)]". */
1325 if ((h->root.type == bfd_link_hash_defined
1326 || h->root.type == bfd_link_hash_defweak)
1327 && local_ref)
1328 {
1329 /* The function is locally defined. */
1330 convert_branch:
1331 /* Convert R_386_GOT32X to R_386_PC32. */
1332 if (modrm == 0x15 || (modrm & 0xf8) == 0x90)
1333 {
1334 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1335 is a nop prefix. */
1336 modrm = 0xe8;
1337 /* To support TLS optimization, always use addr32 prefix
1338 for "call *___tls_get_addr@GOT(%reg)". */
1339 if (eh && eh->tls_get_addr)
1340 {
1341 nop = 0x67;
1342 nop_offset = irel->r_offset - 2;
1343 }
1344 else
1345 {
1346 nop = link_info->call_nop_byte;
1347 if (link_info->call_nop_as_suffix)
1348 {
1349 nop_offset = roff + 3;
1350 irel->r_offset -= 1;
1351 }
1352 else
1353 nop_offset = roff - 2;
1354 }
1355 }
1356 else
1357 {
1358 /* Convert to "jmp foo nop". */
1359 modrm = 0xe9;
1360 nop = NOP_OPCODE;
1361 nop_offset = roff + 3;
1362 irel->r_offset -= 1;
1363 }
1364
1365 bfd_put_8 (abfd, nop, contents + nop_offset);
1366 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1367 /* When converting to PC-relative relocation, we
1368 need to adjust addend by -4. */
1369 bfd_put_32 (abfd, -4, contents + irel->r_offset);
1370 irel->r_info = ELF32_R_INFO (r_symndx, R_386_PC32);
1371 *r_type_p = R_386_PC32;
1372 }
1373 }
1374 else
1375 {
1376 /* We have "mov foo@GOT[(%re1g)], %reg2",
1377 "test %reg1, foo@GOT(%reg2)" and
1378 "binop foo@GOT[(%reg1)], %reg2".
1379
1380 Avoid optimizing _DYNAMIC since ld.so may use its
1381 link-time address. */
1382 if (h == htab->elf.hdynamic)
1383 return TRUE;
1384
1385 /* def_regular is set by an assignment in a linker script in
1386 bfd_elf_record_link_assignment. start_stop is set on
1387 __start_SECNAME/__stop_SECNAME which mark section SECNAME. */
1388 if (h->start_stop
1389 || eh->linker_def
1390 || ((h->def_regular
1391 || h->root.type == bfd_link_hash_defined
1392 || h->root.type == bfd_link_hash_defweak)
1393 && local_ref))
1394 {
1395 convert_load:
1396 if (opcode == 0x8b)
1397 {
1398 if (to_reloc_32)
1399 {
1400 /* Convert "mov foo@GOT[(%reg1)], %reg2" to
1401 "mov $foo, %reg2" with R_386_32. */
1402 r_type = R_386_32;
1403 modrm = 0xc0 | (modrm & 0x38) >> 3;
1404 bfd_put_8 (abfd, modrm, contents + roff - 1);
1405 opcode = 0xc7;
1406 }
1407 else
1408 {
1409 /* Convert "mov foo@GOT(%reg1), %reg2" to
1410 "lea foo@GOTOFF(%reg1), %reg2". */
1411 r_type = R_386_GOTOFF;
1412 opcode = 0x8d;
1413 }
1414 }
1415 else
1416 {
1417 /* Only R_386_32 is supported. */
1418 if (!to_reloc_32)
1419 return TRUE;
1420
1421 if (opcode == 0x85)
1422 {
1423 /* Convert "test %reg1, foo@GOT(%reg2)" to
1424 "test $foo, %reg1". */
1425 modrm = 0xc0 | (modrm & 0x38) >> 3;
1426 opcode = 0xf7;
1427 }
1428 else
1429 {
1430 /* Convert "binop foo@GOT(%reg1), %reg2" to
1431 "binop $foo, %reg2". */
1432 modrm = (0xc0
1433 | (modrm & 0x38) >> 3
1434 | (opcode & 0x3c));
1435 opcode = 0x81;
1436 }
1437 bfd_put_8 (abfd, modrm, contents + roff - 1);
1438 r_type = R_386_32;
1439 }
1440
1441 bfd_put_8 (abfd, opcode, contents + roff - 2);
1442 irel->r_info = ELF32_R_INFO (r_symndx, r_type);
1443 *r_type_p = r_type;
1444 }
1445 }
1446
1447 return TRUE;
1448 }
1449
1450 /* Rename some of the generic section flags to better document how they
1451 are used here. */
1452 #define check_relocs_failed sec_flg0
1453
1454 /* Look through the relocs for a section during the first phase, and
1455 calculate needed space in the global offset table, procedure linkage
1456 table, and dynamic reloc sections. */
1457
1458 static bfd_boolean
1459 elf_i386_check_relocs (bfd *abfd,
1460 struct bfd_link_info *info,
1461 asection *sec,
1462 const Elf_Internal_Rela *relocs)
1463 {
1464 struct elf_x86_link_hash_table *htab;
1465 Elf_Internal_Shdr *symtab_hdr;
1466 struct elf_link_hash_entry **sym_hashes;
1467 const Elf_Internal_Rela *rel;
1468 const Elf_Internal_Rela *rel_end;
1469 asection *sreloc;
1470 bfd_byte *contents;
1471
1472 if (bfd_link_relocatable (info))
1473 return TRUE;
1474
1475 /* Don't do anything special with non-loaded, non-alloced sections.
1476 In particular, any relocs in such sections should not affect GOT
1477 and PLT reference counting (ie. we don't allow them to create GOT
1478 or PLT entries), there's no possibility or desire to optimize TLS
1479 relocs, and there's not much point in propagating relocs to shared
1480 libs that the dynamic linker won't relocate. */
1481 if ((sec->flags & SEC_ALLOC) == 0)
1482 return TRUE;
1483
1484 htab = elf_x86_hash_table (info, I386_ELF_DATA);
1485 if (htab == NULL)
1486 {
1487 sec->check_relocs_failed = 1;
1488 return FALSE;
1489 }
1490
1491 BFD_ASSERT (is_x86_elf (abfd, htab));
1492
1493 /* Get the section contents. */
1494 if (elf_section_data (sec)->this_hdr.contents != NULL)
1495 contents = elf_section_data (sec)->this_hdr.contents;
1496 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1497 {
1498 sec->check_relocs_failed = 1;
1499 return FALSE;
1500 }
1501
1502 symtab_hdr = &elf_symtab_hdr (abfd);
1503 sym_hashes = elf_sym_hashes (abfd);
1504
1505 sreloc = NULL;
1506
1507 rel_end = relocs + sec->reloc_count;
1508 for (rel = relocs; rel < rel_end; rel++)
1509 {
1510 unsigned int r_type;
1511 unsigned int r_symndx;
1512 struct elf_link_hash_entry *h;
1513 struct elf_x86_link_hash_entry *eh;
1514 Elf_Internal_Sym *isym;
1515 const char *name;
1516 bfd_boolean size_reloc;
1517
1518 r_symndx = ELF32_R_SYM (rel->r_info);
1519 r_type = ELF32_R_TYPE (rel->r_info);
1520
1521 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1522 {
1523 /* xgettext:c-format */
1524 _bfd_error_handler (_("%B: bad symbol index: %d"),
1525 abfd, r_symndx);
1526 goto error_return;
1527 }
1528
1529 if (r_symndx < symtab_hdr->sh_info)
1530 {
1531 /* A local symbol. */
1532 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1533 abfd, r_symndx);
1534 if (isym == NULL)
1535 goto error_return;
1536
1537 /* Check relocation against local STT_GNU_IFUNC symbol. */
1538 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1539 {
1540 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel, TRUE);
1541 if (h == NULL)
1542 goto error_return;
1543
1544 /* Fake a STT_GNU_IFUNC symbol. */
1545 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1546 isym, NULL);
1547 h->type = STT_GNU_IFUNC;
1548 h->def_regular = 1;
1549 h->ref_regular = 1;
1550 h->forced_local = 1;
1551 h->root.type = bfd_link_hash_defined;
1552 }
1553 else
1554 h = NULL;
1555 }
1556 else
1557 {
1558 isym = NULL;
1559 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1560 while (h->root.type == bfd_link_hash_indirect
1561 || h->root.type == bfd_link_hash_warning)
1562 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1563 }
1564
1565 eh = (struct elf_x86_link_hash_entry *) h;
1566 if (h != NULL)
1567 {
1568 if (r_type == R_386_GOTOFF)
1569 eh->gotoff_ref = 1;
1570
1571 /* It is referenced by a non-shared object. */
1572 h->ref_regular = 1;
1573 h->root.non_ir_ref_regular = 1;
1574
1575 if (h->type == STT_GNU_IFUNC)
1576 elf_tdata (info->output_bfd)->has_gnu_symbols
1577 |= elf_gnu_symbol_ifunc;
1578 }
1579
1580 if (r_type == R_386_GOT32X
1581 && (h == NULL || h->type != STT_GNU_IFUNC))
1582 {
1583 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1584 if (!elf_i386_convert_load_reloc (abfd, symtab_hdr, contents,
1585 &r_type, irel, h, info))
1586 goto error_return;
1587 }
1588
1589 if (! elf_i386_tls_transition (info, abfd, sec, contents,
1590 symtab_hdr, sym_hashes,
1591 &r_type, GOT_UNKNOWN,
1592 rel, rel_end, h, r_symndx, FALSE))
1593 goto error_return;
1594
1595 switch (r_type)
1596 {
1597 case R_386_TLS_LDM:
1598 htab->tls_ld_or_ldm_got.refcount += 1;
1599 goto create_got;
1600
1601 case R_386_PLT32:
1602 /* This symbol requires a procedure linkage table entry. We
1603 actually build the entry in adjust_dynamic_symbol,
1604 because this might be a case of linking PIC code which is
1605 never referenced by a dynamic object, in which case we
1606 don't need to generate a procedure linkage table entry
1607 after all. */
1608
1609 /* If this is a local symbol, we resolve it directly without
1610 creating a procedure linkage table entry. */
1611 if (h == NULL)
1612 continue;
1613
1614 eh->has_got_reloc = 1;
1615 h->needs_plt = 1;
1616 h->plt.refcount += 1;
1617 break;
1618
1619 case R_386_SIZE32:
1620 size_reloc = TRUE;
1621 goto do_size;
1622
1623 case R_386_TLS_IE_32:
1624 case R_386_TLS_IE:
1625 case R_386_TLS_GOTIE:
1626 if (!bfd_link_executable (info))
1627 info->flags |= DF_STATIC_TLS;
1628 /* Fall through */
1629
1630 case R_386_GOT32:
1631 case R_386_GOT32X:
1632 case R_386_TLS_GD:
1633 case R_386_TLS_GOTDESC:
1634 case R_386_TLS_DESC_CALL:
1635 /* This symbol requires a global offset table entry. */
1636 {
1637 int tls_type, old_tls_type;
1638
1639 switch (r_type)
1640 {
1641 default:
1642 case R_386_GOT32:
1643 case R_386_GOT32X:
1644 tls_type = GOT_NORMAL;
1645 break;
1646 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1647 case R_386_TLS_GOTDESC:
1648 case R_386_TLS_DESC_CALL:
1649 tls_type = GOT_TLS_GDESC; break;
1650 case R_386_TLS_IE_32:
1651 if (ELF32_R_TYPE (rel->r_info) == r_type)
1652 tls_type = GOT_TLS_IE_NEG;
1653 else
1654 /* If this is a GD->IE transition, we may use either of
1655 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1656 tls_type = GOT_TLS_IE;
1657 break;
1658 case R_386_TLS_IE:
1659 case R_386_TLS_GOTIE:
1660 tls_type = GOT_TLS_IE_POS; break;
1661 }
1662
1663 if (h != NULL)
1664 {
1665 h->got.refcount += 1;
1666 old_tls_type = elf_x86_hash_entry (h)->tls_type;
1667 }
1668 else
1669 {
1670 bfd_signed_vma *local_got_refcounts;
1671
1672 /* This is a global offset table entry for a local symbol. */
1673 local_got_refcounts = elf_local_got_refcounts (abfd);
1674 if (local_got_refcounts == NULL)
1675 {
1676 bfd_size_type size;
1677
1678 size = symtab_hdr->sh_info;
1679 size *= (sizeof (bfd_signed_vma)
1680 + sizeof (bfd_vma) + sizeof(char));
1681 local_got_refcounts = (bfd_signed_vma *)
1682 bfd_zalloc (abfd, size);
1683 if (local_got_refcounts == NULL)
1684 goto error_return;
1685 elf_local_got_refcounts (abfd) = local_got_refcounts;
1686 elf_x86_local_tlsdesc_gotent (abfd)
1687 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1688 elf_x86_local_got_tls_type (abfd)
1689 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1690 }
1691 local_got_refcounts[r_symndx] += 1;
1692 old_tls_type = elf_x86_local_got_tls_type (abfd) [r_symndx];
1693 }
1694
1695 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1696 tls_type |= old_tls_type;
1697 /* If a TLS symbol is accessed using IE at least once,
1698 there is no point to use dynamic model for it. */
1699 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1700 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1701 || (tls_type & GOT_TLS_IE) == 0))
1702 {
1703 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1704 tls_type = old_tls_type;
1705 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1706 && GOT_TLS_GD_ANY_P (tls_type))
1707 tls_type |= old_tls_type;
1708 else
1709 {
1710 if (h)
1711 name = h->root.root.string;
1712 else
1713 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1714 NULL);
1715 _bfd_error_handler
1716 /* xgettext:c-format */
1717 (_("%B: `%s' accessed both as normal and "
1718 "thread local symbol"),
1719 abfd, name);
1720 bfd_set_error (bfd_error_bad_value);
1721 goto error_return;
1722 }
1723 }
1724
1725 if (old_tls_type != tls_type)
1726 {
1727 if (h != NULL)
1728 elf_x86_hash_entry (h)->tls_type = tls_type;
1729 else
1730 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
1731 }
1732 }
1733 /* Fall through */
1734
1735 case R_386_GOTOFF:
1736 case R_386_GOTPC:
1737 create_got:
1738 if (r_type != R_386_TLS_IE)
1739 {
1740 if (eh != NULL)
1741 eh->has_got_reloc = 1;
1742 break;
1743 }
1744 /* Fall through */
1745
1746 case R_386_TLS_LE_32:
1747 case R_386_TLS_LE:
1748 if (eh != NULL)
1749 eh->has_got_reloc = 1;
1750 if (bfd_link_executable (info))
1751 break;
1752 info->flags |= DF_STATIC_TLS;
1753 goto do_relocation;
1754
1755 case R_386_32:
1756 case R_386_PC32:
1757 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
1758 eh->has_non_got_reloc = 1;
1759 do_relocation:
1760 /* We are called after all symbols have been resolved. Only
1761 relocation against STT_GNU_IFUNC symbol must go through
1762 PLT. */
1763 if (h != NULL
1764 && (bfd_link_executable (info)
1765 || h->type == STT_GNU_IFUNC))
1766 {
1767 /* If this reloc is in a read-only section, we might
1768 need a copy reloc. We can't check reliably at this
1769 stage whether the section is read-only, as input
1770 sections have not yet been mapped to output sections.
1771 Tentatively set the flag for now, and correct in
1772 adjust_dynamic_symbol. */
1773 h->non_got_ref = 1;
1774
1775 /* We may need a .plt entry if the symbol is a function
1776 defined in a shared lib or is a STT_GNU_IFUNC function
1777 referenced from the code or read-only section. */
1778 if (!h->def_regular
1779 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
1780 h->plt.refcount += 1;
1781
1782 if (r_type == R_386_PC32)
1783 {
1784 /* Since something like ".long foo - ." may be used
1785 as pointer, make sure that PLT is used if foo is
1786 a function defined in a shared library. */
1787 if ((sec->flags & SEC_CODE) == 0)
1788 h->pointer_equality_needed = 1;
1789 else if (h->type == STT_GNU_IFUNC
1790 && bfd_link_pic (info))
1791 {
1792 _bfd_error_handler
1793 /* xgettext:c-format */
1794 (_("%B: unsupported non-PIC call to IFUNC `%s'"),
1795 abfd, h->root.root.string);
1796 bfd_set_error (bfd_error_bad_value);
1797 goto error_return;
1798 }
1799 }
1800 else
1801 {
1802 h->pointer_equality_needed = 1;
1803 /* R_386_32 can be resolved at run-time. */
1804 if (r_type == R_386_32
1805 && (sec->flags & SEC_READONLY) == 0)
1806 eh->func_pointer_refcount += 1;
1807 }
1808 }
1809
1810 size_reloc = FALSE;
1811 do_size:
1812 /* If we are creating a shared library, and this is a reloc
1813 against a global symbol, or a non PC relative reloc
1814 against a local symbol, then we need to copy the reloc
1815 into the shared library. However, if we are linking with
1816 -Bsymbolic, we do not need to copy a reloc against a
1817 global symbol which is defined in an object we are
1818 including in the link (i.e., DEF_REGULAR is set). At
1819 this point we have not seen all the input files, so it is
1820 possible that DEF_REGULAR is not set now but will be set
1821 later (it is never cleared). In case of a weak definition,
1822 DEF_REGULAR may be cleared later by a strong definition in
1823 a shared library. We account for that possibility below by
1824 storing information in the relocs_copied field of the hash
1825 table entry. A similar situation occurs when creating
1826 shared libraries and symbol visibility changes render the
1827 symbol local.
1828
1829 If on the other hand, we are creating an executable, we
1830 may need to keep relocations for symbols satisfied by a
1831 dynamic library if we manage to avoid copy relocs for the
1832 symbol.
1833
1834 Generate dynamic pointer relocation against STT_GNU_IFUNC
1835 symbol in the non-code section. */
1836 if ((bfd_link_pic (info)
1837 && (r_type != R_386_PC32
1838 || (h != NULL
1839 && (! (bfd_link_pie (info)
1840 || SYMBOLIC_BIND (info, h))
1841 || h->root.type == bfd_link_hash_defweak
1842 || !h->def_regular))))
1843 || (h != NULL
1844 && h->type == STT_GNU_IFUNC
1845 && r_type == R_386_32
1846 && (sec->flags & SEC_CODE) == 0)
1847 || (ELIMINATE_COPY_RELOCS
1848 && !bfd_link_pic (info)
1849 && h != NULL
1850 && (h->root.type == bfd_link_hash_defweak
1851 || !h->def_regular)))
1852 {
1853 struct elf_dyn_relocs *p;
1854 struct elf_dyn_relocs **head;
1855
1856 /* We must copy these reloc types into the output file.
1857 Create a reloc section in dynobj and make room for
1858 this reloc. */
1859 if (sreloc == NULL)
1860 {
1861 sreloc = _bfd_elf_make_dynamic_reloc_section
1862 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1863
1864 if (sreloc == NULL)
1865 goto error_return;
1866 }
1867
1868 /* If this is a global symbol, we count the number of
1869 relocations we need for this symbol. */
1870 if (h != NULL)
1871 {
1872 head = &eh->dyn_relocs;
1873 }
1874 else
1875 {
1876 /* Track dynamic relocs needed for local syms too.
1877 We really need local syms available to do this
1878 easily. Oh well. */
1879 void **vpp;
1880 asection *s;
1881
1882 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1883 abfd, r_symndx);
1884 if (isym == NULL)
1885 goto error_return;
1886
1887 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1888 if (s == NULL)
1889 s = sec;
1890
1891 vpp = &elf_section_data (s)->local_dynrel;
1892 head = (struct elf_dyn_relocs **)vpp;
1893 }
1894
1895 p = *head;
1896 if (p == NULL || p->sec != sec)
1897 {
1898 bfd_size_type amt = sizeof *p;
1899 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj,
1900 amt);
1901 if (p == NULL)
1902 goto error_return;
1903 p->next = *head;
1904 *head = p;
1905 p->sec = sec;
1906 p->count = 0;
1907 p->pc_count = 0;
1908 }
1909
1910 p->count += 1;
1911 /* Count size relocation as PC-relative relocation. */
1912 if (r_type == R_386_PC32 || size_reloc)
1913 p->pc_count += 1;
1914 }
1915 break;
1916
1917 /* This relocation describes the C++ object vtable hierarchy.
1918 Reconstruct it for later use during GC. */
1919 case R_386_GNU_VTINHERIT:
1920 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1921 goto error_return;
1922 break;
1923
1924 /* This relocation describes which C++ vtable entries are actually
1925 used. Record for later use during GC. */
1926 case R_386_GNU_VTENTRY:
1927 BFD_ASSERT (h != NULL);
1928 if (h != NULL
1929 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1930 goto error_return;
1931 break;
1932
1933 default:
1934 break;
1935 }
1936 }
1937
1938 if (elf_section_data (sec)->this_hdr.contents != contents)
1939 {
1940 if (!info->keep_memory)
1941 free (contents);
1942 else
1943 {
1944 /* Cache the section contents for elf_link_input_bfd. */
1945 elf_section_data (sec)->this_hdr.contents = contents;
1946 }
1947 }
1948
1949 return TRUE;
1950
1951 error_return:
1952 if (elf_section_data (sec)->this_hdr.contents != contents)
1953 free (contents);
1954 sec->check_relocs_failed = 1;
1955 return FALSE;
1956 }
1957
1958 /* Set the correct type for an x86 ELF section. We do this by the
1959 section name, which is a hack, but ought to work. */
1960
1961 static bfd_boolean
1962 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1963 Elf_Internal_Shdr *hdr,
1964 asection *sec)
1965 {
1966 const char *name;
1967
1968 name = bfd_get_section_name (abfd, sec);
1969
1970 /* This is an ugly, but unfortunately necessary hack that is
1971 needed when producing EFI binaries on x86. It tells
1972 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1973 containing ELF relocation info. We need this hack in order to
1974 be able to generate ELF binaries that can be translated into
1975 EFI applications (which are essentially COFF objects). Those
1976 files contain a COFF ".reloc" section inside an ELFNN object,
1977 which would normally cause BFD to segfault because it would
1978 attempt to interpret this section as containing relocation
1979 entries for section "oc". With this hack enabled, ".reloc"
1980 will be treated as a normal data section, which will avoid the
1981 segfault. However, you won't be able to create an ELFNN binary
1982 with a section named "oc" that needs relocations, but that's
1983 the kind of ugly side-effects you get when detecting section
1984 types based on their names... In practice, this limitation is
1985 unlikely to bite. */
1986 if (strcmp (name, ".reloc") == 0)
1987 hdr->sh_type = SHT_PROGBITS;
1988
1989 return TRUE;
1990 }
1991
1992 /* Return the relocation value for @tpoff relocation
1993 if STT_TLS virtual address is ADDRESS. */
1994
1995 static bfd_vma
1996 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
1997 {
1998 struct elf_link_hash_table *htab = elf_hash_table (info);
1999 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2000 bfd_vma static_tls_size;
2001
2002 /* If tls_sec is NULL, we should have signalled an error already. */
2003 if (htab->tls_sec == NULL)
2004 return 0;
2005
2006 /* Consider special static TLS alignment requirements. */
2007 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2008 return static_tls_size + htab->tls_sec->vma - address;
2009 }
2010
2011 /* Relocate an i386 ELF section. */
2012
2013 static bfd_boolean
2014 elf_i386_relocate_section (bfd *output_bfd,
2015 struct bfd_link_info *info,
2016 bfd *input_bfd,
2017 asection *input_section,
2018 bfd_byte *contents,
2019 Elf_Internal_Rela *relocs,
2020 Elf_Internal_Sym *local_syms,
2021 asection **local_sections)
2022 {
2023 struct elf_x86_link_hash_table *htab;
2024 Elf_Internal_Shdr *symtab_hdr;
2025 struct elf_link_hash_entry **sym_hashes;
2026 bfd_vma *local_got_offsets;
2027 bfd_vma *local_tlsdesc_gotents;
2028 Elf_Internal_Rela *rel;
2029 Elf_Internal_Rela *wrel;
2030 Elf_Internal_Rela *relend;
2031 bfd_boolean is_vxworks_tls;
2032 unsigned plt_entry_size;
2033
2034 /* Skip if check_relocs failed. */
2035 if (input_section->check_relocs_failed)
2036 return FALSE;
2037
2038 htab = elf_x86_hash_table (info, I386_ELF_DATA);
2039 if (htab == NULL)
2040 return FALSE;
2041
2042 BFD_ASSERT (is_x86_elf (input_bfd, htab));
2043
2044 symtab_hdr = &elf_symtab_hdr (input_bfd);
2045 sym_hashes = elf_sym_hashes (input_bfd);
2046 local_got_offsets = elf_local_got_offsets (input_bfd);
2047 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2048 /* We have to handle relocations in vxworks .tls_vars sections
2049 specially, because the dynamic loader is 'weird'. */
2050 is_vxworks_tls = (htab->is_vxworks
2051 && bfd_link_pic (info)
2052 && !strcmp (input_section->output_section->name,
2053 ".tls_vars"));
2054
2055 _bfd_x86_elf_set_tls_module_base (info);
2056
2057 plt_entry_size = htab->plt.plt_entry_size;
2058
2059 rel = wrel = relocs;
2060 relend = relocs + input_section->reloc_count;
2061 for (; rel < relend; wrel++, rel++)
2062 {
2063 unsigned int r_type, r_type_tls;
2064 reloc_howto_type *howto;
2065 unsigned long r_symndx;
2066 struct elf_link_hash_entry *h;
2067 struct elf_x86_link_hash_entry *eh;
2068 Elf_Internal_Sym *sym;
2069 asection *sec;
2070 bfd_vma off, offplt, plt_offset;
2071 bfd_vma relocation;
2072 bfd_boolean unresolved_reloc;
2073 bfd_reloc_status_type r;
2074 unsigned int indx;
2075 int tls_type;
2076 bfd_vma st_size;
2077 asection *resolved_plt;
2078 bfd_boolean resolved_to_zero;
2079 bfd_boolean relative_reloc;
2080
2081 r_type = ELF32_R_TYPE (rel->r_info);
2082 if (r_type == R_386_GNU_VTINHERIT
2083 || r_type == R_386_GNU_VTENTRY)
2084 {
2085 if (wrel != rel)
2086 *wrel = *rel;
2087 continue;
2088 }
2089
2090 if ((indx = r_type) >= R_386_standard
2091 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2092 >= R_386_ext - R_386_standard)
2093 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2094 >= R_386_ext2 - R_386_ext))
2095 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2096
2097 howto = elf_howto_table + indx;
2098
2099 r_symndx = ELF32_R_SYM (rel->r_info);
2100 h = NULL;
2101 sym = NULL;
2102 sec = NULL;
2103 unresolved_reloc = FALSE;
2104 if (r_symndx < symtab_hdr->sh_info)
2105 {
2106 sym = local_syms + r_symndx;
2107 sec = local_sections[r_symndx];
2108 relocation = (sec->output_section->vma
2109 + sec->output_offset
2110 + sym->st_value);
2111 st_size = sym->st_size;
2112
2113 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2114 && ((sec->flags & SEC_MERGE) != 0
2115 || (bfd_link_relocatable (info)
2116 && sec->output_offset != 0)))
2117 {
2118 bfd_vma addend;
2119 bfd_byte *where = contents + rel->r_offset;
2120
2121 switch (howto->size)
2122 {
2123 case 0:
2124 addend = bfd_get_8 (input_bfd, where);
2125 if (howto->pc_relative)
2126 {
2127 addend = (addend ^ 0x80) - 0x80;
2128 addend += 1;
2129 }
2130 break;
2131 case 1:
2132 addend = bfd_get_16 (input_bfd, where);
2133 if (howto->pc_relative)
2134 {
2135 addend = (addend ^ 0x8000) - 0x8000;
2136 addend += 2;
2137 }
2138 break;
2139 case 2:
2140 addend = bfd_get_32 (input_bfd, where);
2141 if (howto->pc_relative)
2142 {
2143 addend = (addend ^ 0x80000000) - 0x80000000;
2144 addend += 4;
2145 }
2146 break;
2147 default:
2148 abort ();
2149 }
2150
2151 if (bfd_link_relocatable (info))
2152 addend += sec->output_offset;
2153 else
2154 {
2155 asection *msec = sec;
2156 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2157 addend);
2158 addend -= relocation;
2159 addend += msec->output_section->vma + msec->output_offset;
2160 }
2161
2162 switch (howto->size)
2163 {
2164 case 0:
2165 /* FIXME: overflow checks. */
2166 if (howto->pc_relative)
2167 addend -= 1;
2168 bfd_put_8 (input_bfd, addend, where);
2169 break;
2170 case 1:
2171 if (howto->pc_relative)
2172 addend -= 2;
2173 bfd_put_16 (input_bfd, addend, where);
2174 break;
2175 case 2:
2176 if (howto->pc_relative)
2177 addend -= 4;
2178 bfd_put_32 (input_bfd, addend, where);
2179 break;
2180 }
2181 }
2182 else if (!bfd_link_relocatable (info)
2183 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2184 {
2185 /* Relocate against local STT_GNU_IFUNC symbol. */
2186 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd, rel,
2187 FALSE);
2188 if (h == NULL)
2189 abort ();
2190
2191 /* Set STT_GNU_IFUNC symbol value. */
2192 h->root.u.def.value = sym->st_value;
2193 h->root.u.def.section = sec;
2194 }
2195 }
2196 else
2197 {
2198 bfd_boolean warned ATTRIBUTE_UNUSED;
2199 bfd_boolean ignored ATTRIBUTE_UNUSED;
2200
2201 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2202 r_symndx, symtab_hdr, sym_hashes,
2203 h, sec, relocation,
2204 unresolved_reloc, warned, ignored);
2205 st_size = h->size;
2206 }
2207
2208 if (sec != NULL && discarded_section (sec))
2209 {
2210 _bfd_clear_contents (howto, input_bfd, input_section,
2211 contents + rel->r_offset);
2212 wrel->r_offset = rel->r_offset;
2213 wrel->r_info = 0;
2214 wrel->r_addend = 0;
2215
2216 /* For ld -r, remove relocations in debug sections against
2217 sections defined in discarded sections. Not done for
2218 eh_frame editing code expects to be present. */
2219 if (bfd_link_relocatable (info)
2220 && (input_section->flags & SEC_DEBUGGING))
2221 wrel--;
2222
2223 continue;
2224 }
2225
2226 if (bfd_link_relocatable (info))
2227 {
2228 if (wrel != rel)
2229 *wrel = *rel;
2230 continue;
2231 }
2232
2233 eh = (struct elf_x86_link_hash_entry *) h;
2234
2235 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2236 it here if it is defined in a non-shared object. */
2237 if (h != NULL
2238 && h->type == STT_GNU_IFUNC
2239 && h->def_regular)
2240 {
2241 asection *gotplt, *base_got;
2242 bfd_vma plt_index;
2243 const char *name;
2244
2245 if ((input_section->flags & SEC_ALLOC) == 0)
2246 {
2247 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2248 sections because such sections are not SEC_ALLOC and
2249 thus ld.so will not process them. */
2250 if ((input_section->flags & SEC_DEBUGGING) != 0)
2251 continue;
2252 abort ();
2253 }
2254
2255 /* STT_GNU_IFUNC symbol must go through PLT. */
2256 if (htab->elf.splt != NULL)
2257 {
2258 if (htab->plt_second != NULL)
2259 {
2260 resolved_plt = htab->plt_second;
2261 plt_offset = eh->plt_second.offset;
2262 }
2263 else
2264 {
2265 resolved_plt = htab->elf.splt;
2266 plt_offset = h->plt.offset;
2267 }
2268 gotplt = htab->elf.sgotplt;
2269 }
2270 else
2271 {
2272 resolved_plt = htab->elf.iplt;
2273 plt_offset = h->plt.offset;
2274 gotplt = htab->elf.igotplt;
2275 }
2276
2277 switch (r_type)
2278 {
2279 default:
2280 break;
2281
2282 case R_386_GOT32:
2283 case R_386_GOT32X:
2284 base_got = htab->elf.sgot;
2285 off = h->got.offset;
2286
2287 if (base_got == NULL)
2288 abort ();
2289
2290 if (off == (bfd_vma) -1)
2291 {
2292 /* We can't use h->got.offset here to save state, or
2293 even just remember the offset, as finish_dynamic_symbol
2294 would use that as offset into .got. */
2295
2296 if (h->plt.offset == (bfd_vma) -1)
2297 abort ();
2298
2299 if (htab->elf.splt != NULL)
2300 {
2301 plt_index = (h->plt.offset / plt_entry_size
2302 - htab->plt.has_plt0);
2303 off = (plt_index + 3) * 4;
2304 base_got = htab->elf.sgotplt;
2305 }
2306 else
2307 {
2308 plt_index = h->plt.offset / plt_entry_size;
2309 off = plt_index * 4;
2310 base_got = htab->elf.igotplt;
2311 }
2312
2313 if (h->dynindx == -1
2314 || h->forced_local
2315 || info->symbolic)
2316 {
2317 /* This references the local defitionion. We must
2318 initialize this entry in the global offset table.
2319 Since the offset must always be a multiple of 8,
2320 we use the least significant bit to record
2321 whether we have initialized it already.
2322
2323 When doing a dynamic link, we create a .rela.got
2324 relocation entry to initialize the value. This
2325 is done in the finish_dynamic_symbol routine. */
2326 if ((off & 1) != 0)
2327 off &= ~1;
2328 else
2329 {
2330 bfd_put_32 (output_bfd, relocation,
2331 base_got->contents + off);
2332 h->got.offset |= 1;
2333 }
2334 }
2335
2336 relocation = off;
2337 }
2338 else
2339 relocation = (base_got->output_section->vma
2340 + base_got->output_offset + off
2341 - gotplt->output_section->vma
2342 - gotplt->output_offset);
2343
2344 if (rel->r_offset > 1
2345 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2346 && *(contents + rel->r_offset - 2) != 0x8d)
2347 {
2348 if (bfd_link_pic (info))
2349 goto disallow_got32;
2350
2351 /* Add the GOT base if there is no base register. */
2352 relocation += (gotplt->output_section->vma
2353 + gotplt->output_offset);
2354 }
2355 else if (htab->elf.splt == NULL)
2356 {
2357 /* Adjust for static executables. */
2358 relocation += gotplt->output_offset;
2359 }
2360
2361 goto do_relocation;
2362 }
2363
2364 if (h->plt.offset == (bfd_vma) -1)
2365 {
2366 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2367 if (r_type == R_386_32
2368 && (input_section->flags & SEC_CODE) == 0)
2369 goto do_ifunc_pointer;
2370 goto bad_ifunc_reloc;
2371 }
2372
2373 relocation = (resolved_plt->output_section->vma
2374 + resolved_plt->output_offset + plt_offset);
2375
2376 switch (r_type)
2377 {
2378 default:
2379 bad_ifunc_reloc:
2380 if (h->root.root.string)
2381 name = h->root.root.string;
2382 else
2383 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2384 NULL);
2385 _bfd_error_handler
2386 /* xgettext:c-format */
2387 (_("%B: relocation %s against STT_GNU_IFUNC "
2388 "symbol `%s' isn't supported"), input_bfd,
2389 howto->name, name);
2390 bfd_set_error (bfd_error_bad_value);
2391 return FALSE;
2392
2393 case R_386_32:
2394 /* Generate dynamic relcoation only when there is a
2395 non-GOT reference in a shared object. */
2396 if ((bfd_link_pic (info) && h->non_got_ref)
2397 || h->plt.offset == (bfd_vma) -1)
2398 {
2399 Elf_Internal_Rela outrel;
2400 asection *sreloc;
2401 bfd_vma offset;
2402
2403 do_ifunc_pointer:
2404 /* Need a dynamic relocation to get the real function
2405 adddress. */
2406 offset = _bfd_elf_section_offset (output_bfd,
2407 info,
2408 input_section,
2409 rel->r_offset);
2410 if (offset == (bfd_vma) -1
2411 || offset == (bfd_vma) -2)
2412 abort ();
2413
2414 outrel.r_offset = (input_section->output_section->vma
2415 + input_section->output_offset
2416 + offset);
2417
2418 if (h->dynindx == -1
2419 || h->forced_local
2420 || bfd_link_executable (info))
2421 {
2422 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
2423 h->root.root.string,
2424 h->root.u.def.section->owner);
2425
2426 /* This symbol is resolved locally. */
2427 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
2428 bfd_put_32 (output_bfd,
2429 (h->root.u.def.value
2430 + h->root.u.def.section->output_section->vma
2431 + h->root.u.def.section->output_offset),
2432 contents + offset);
2433 }
2434 else
2435 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2436
2437 /* Dynamic relocations are stored in
2438 1. .rel.ifunc section in PIC object.
2439 2. .rel.got section in dynamic executable.
2440 3. .rel.iplt section in static executable. */
2441 if (bfd_link_pic (info))
2442 sreloc = htab->elf.irelifunc;
2443 else if (htab->elf.splt != NULL)
2444 sreloc = htab->elf.srelgot;
2445 else
2446 sreloc = htab->elf.irelplt;
2447 elf_append_rel (output_bfd, sreloc, &outrel);
2448
2449 /* If this reloc is against an external symbol, we
2450 do not want to fiddle with the addend. Otherwise,
2451 we need to include the symbol value so that it
2452 becomes an addend for the dynamic reloc. For an
2453 internal symbol, we have updated addend. */
2454 continue;
2455 }
2456 /* FALLTHROUGH */
2457 case R_386_PC32:
2458 case R_386_PLT32:
2459 goto do_relocation;
2460
2461 case R_386_GOTOFF:
2462 relocation -= (gotplt->output_section->vma
2463 + gotplt->output_offset);
2464 goto do_relocation;
2465 }
2466 }
2467
2468 resolved_to_zero = (eh != NULL
2469 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2470
2471 switch (r_type)
2472 {
2473 case R_386_GOT32X:
2474 /* Avoid optimizing _DYNAMIC since ld.so may use its
2475 link-time address. */
2476 if (h == htab->elf.hdynamic)
2477 goto r_386_got32;
2478
2479 if (bfd_link_pic (info))
2480 {
2481 /* It is OK to convert mov to lea and convert indirect
2482 branch to direct branch. It is OK to convert adc,
2483 add, and, cmp, or, sbb, sub, test, xor only when PIC
2484 is false. */
2485 unsigned int opcode, addend;
2486 addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
2487 if (addend != 0)
2488 goto r_386_got32;
2489 opcode = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2490 if (opcode != 0x8b && opcode != 0xff)
2491 goto r_386_got32;
2492 }
2493
2494 /* Resolve "mov GOT[(%reg)], %reg",
2495 "call/jmp *GOT[(%reg)]", "test %reg, foo@GOT[(%reg)]"
2496 and "binop foo@GOT[(%reg)], %reg". */
2497 if (h == NULL
2498 || (h->plt.offset == (bfd_vma) -1
2499 && h->got.offset == (bfd_vma) -1)
2500 || htab->elf.sgotplt == NULL)
2501 abort ();
2502
2503 offplt = (htab->elf.sgotplt->output_section->vma
2504 + htab->elf.sgotplt->output_offset);
2505
2506 /* It is relative to .got.plt section. */
2507 if (h->got.offset != (bfd_vma) -1)
2508 /* Use GOT entry. Mask off the least significant bit in
2509 GOT offset which may be set by R_386_GOT32 processing
2510 below. */
2511 relocation = (htab->elf.sgot->output_section->vma
2512 + htab->elf.sgot->output_offset
2513 + (h->got.offset & ~1) - offplt);
2514 else
2515 /* Use GOTPLT entry. */
2516 relocation = (h->plt.offset / plt_entry_size
2517 - htab->plt.has_plt0 + 3) * 4;
2518
2519 if (!bfd_link_pic (info))
2520 {
2521 /* If not PIC, add the .got.plt section address for
2522 baseless addressing. */
2523 unsigned int modrm;
2524 modrm = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2525 if ((modrm & 0xc7) == 0x5)
2526 relocation += offplt;
2527 }
2528
2529 unresolved_reloc = FALSE;
2530 break;
2531
2532 case R_386_GOT32:
2533 r_386_got32:
2534 /* Relocation is to the entry for this symbol in the global
2535 offset table. */
2536 if (htab->elf.sgot == NULL)
2537 abort ();
2538
2539 relative_reloc = FALSE;
2540 if (h != NULL)
2541 {
2542 bfd_boolean dyn;
2543
2544 off = h->got.offset;
2545 dyn = htab->elf.dynamic_sections_created;
2546 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
2547 bfd_link_pic (info),
2548 h)
2549 || (bfd_link_pic (info)
2550 && SYMBOL_REFERENCES_LOCAL_P (info, h))
2551 || (ELF_ST_VISIBILITY (h->other)
2552 && h->root.type == bfd_link_hash_undefweak))
2553 {
2554 /* This is actually a static link, or it is a
2555 -Bsymbolic link and the symbol is defined
2556 locally, or the symbol was forced to be local
2557 because of a version file. We must initialize
2558 this entry in the global offset table. Since the
2559 offset must always be a multiple of 4, we use the
2560 least significant bit to record whether we have
2561 initialized it already.
2562
2563 When doing a dynamic link, we create a .rel.got
2564 relocation entry to initialize the value. This
2565 is done in the finish_dynamic_symbol routine. */
2566 if ((off & 1) != 0)
2567 off &= ~1;
2568 else
2569 {
2570 bfd_put_32 (output_bfd, relocation,
2571 htab->elf.sgot->contents + off);
2572 h->got.offset |= 1;
2573
2574 if (h->dynindx == -1
2575 && !h->forced_local
2576 && h->root.type != bfd_link_hash_undefweak
2577 && bfd_link_pic (info))
2578 {
2579 /* PR ld/21402: If this symbol isn't dynamic
2580 in PIC, generate R_386_RELATIVE here. */
2581 eh->no_finish_dynamic_symbol = 1;
2582 relative_reloc = TRUE;
2583 }
2584 }
2585 }
2586 else
2587 unresolved_reloc = FALSE;
2588 }
2589 else
2590 {
2591 if (local_got_offsets == NULL)
2592 abort ();
2593
2594 off = local_got_offsets[r_symndx];
2595
2596 /* The offset must always be a multiple of 4. We use
2597 the least significant bit to record whether we have
2598 already generated the necessary reloc. */
2599 if ((off & 1) != 0)
2600 off &= ~1;
2601 else
2602 {
2603 bfd_put_32 (output_bfd, relocation,
2604 htab->elf.sgot->contents + off);
2605 local_got_offsets[r_symndx] |= 1;
2606
2607 if (bfd_link_pic (info))
2608 relative_reloc = TRUE;
2609 }
2610 }
2611
2612 if (relative_reloc)
2613 {
2614 asection *s;
2615 Elf_Internal_Rela outrel;
2616
2617 s = htab->elf.srelgot;
2618 if (s == NULL)
2619 abort ();
2620
2621 outrel.r_offset = (htab->elf.sgot->output_section->vma
2622 + htab->elf.sgot->output_offset
2623 + off);
2624 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2625 elf_append_rel (output_bfd, s, &outrel);
2626 }
2627
2628 if (off >= (bfd_vma) -2)
2629 abort ();
2630
2631 relocation = (htab->elf.sgot->output_section->vma
2632 + htab->elf.sgot->output_offset + off);
2633 if (rel->r_offset > 1
2634 && (*(contents + rel->r_offset - 1) & 0xc7) == 0x5
2635 && *(contents + rel->r_offset - 2) != 0x8d)
2636 {
2637 if (bfd_link_pic (info))
2638 {
2639 /* For PIC, disallow R_386_GOT32 without a base
2640 register, except for "lea foo@GOT, %reg", since
2641 we don't know what the GOT base is. */
2642 const char *name;
2643
2644 disallow_got32:
2645 if (h == NULL || h->root.root.string == NULL)
2646 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2647 NULL);
2648 else
2649 name = h->root.root.string;
2650
2651 _bfd_error_handler
2652 /* xgettext:c-format */
2653 (_("%B: direct GOT relocation %s against `%s'"
2654 " without base register can not be used"
2655 " when making a shared object"),
2656 input_bfd, howto->name, name);
2657 bfd_set_error (bfd_error_bad_value);
2658 return FALSE;
2659 }
2660 }
2661 else
2662 {
2663 /* Subtract the .got.plt section address only with a base
2664 register. */
2665 relocation -= (htab->elf.sgotplt->output_section->vma
2666 + htab->elf.sgotplt->output_offset);
2667 }
2668
2669 break;
2670
2671 case R_386_GOTOFF:
2672 /* Relocation is relative to the start of the global offset
2673 table. */
2674
2675 /* Check to make sure it isn't a protected function or data
2676 symbol for shared library since it may not be local when
2677 used as function address or with copy relocation. We also
2678 need to make sure that a symbol is referenced locally. */
2679 if (!bfd_link_executable (info) && h)
2680 {
2681 if (!h->def_regular)
2682 {
2683 const char *v;
2684
2685 switch (ELF_ST_VISIBILITY (h->other))
2686 {
2687 case STV_HIDDEN:
2688 v = _("hidden symbol");
2689 break;
2690 case STV_INTERNAL:
2691 v = _("internal symbol");
2692 break;
2693 case STV_PROTECTED:
2694 v = _("protected symbol");
2695 break;
2696 default:
2697 v = _("symbol");
2698 break;
2699 }
2700
2701 _bfd_error_handler
2702 /* xgettext:c-format */
2703 (_("%B: relocation R_386_GOTOFF against undefined %s"
2704 " `%s' can not be used when making a shared object"),
2705 input_bfd, v, h->root.root.string);
2706 bfd_set_error (bfd_error_bad_value);
2707 return FALSE;
2708 }
2709 else if (!SYMBOL_REFERENCES_LOCAL_P (info, h)
2710 && (h->type == STT_FUNC
2711 || h->type == STT_OBJECT)
2712 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2713 {
2714 _bfd_error_handler
2715 /* xgettext:c-format */
2716 (_("%B: relocation R_386_GOTOFF against protected %s"
2717 " `%s' can not be used when making a shared object"),
2718 input_bfd,
2719 h->type == STT_FUNC ? "function" : "data",
2720 h->root.root.string);
2721 bfd_set_error (bfd_error_bad_value);
2722 return FALSE;
2723 }
2724 }
2725
2726 /* Note that sgot is not involved in this
2727 calculation. We always want the start of .got.plt. If we
2728 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2729 permitted by the ABI, we might have to change this
2730 calculation. */
2731 relocation -= htab->elf.sgotplt->output_section->vma
2732 + htab->elf.sgotplt->output_offset;
2733 break;
2734
2735 case R_386_GOTPC:
2736 /* Use global offset table as symbol value. */
2737 relocation = htab->elf.sgotplt->output_section->vma
2738 + htab->elf.sgotplt->output_offset;
2739 unresolved_reloc = FALSE;
2740 break;
2741
2742 case R_386_PLT32:
2743 /* Relocation is to the entry for this symbol in the
2744 procedure linkage table. */
2745
2746 /* Resolve a PLT32 reloc against a local symbol directly,
2747 without using the procedure linkage table. */
2748 if (h == NULL)
2749 break;
2750
2751 if ((h->plt.offset == (bfd_vma) -1
2752 && eh->plt_got.offset == (bfd_vma) -1)
2753 || htab->elf.splt == NULL)
2754 {
2755 /* We didn't make a PLT entry for this symbol. This
2756 happens when statically linking PIC code, or when
2757 using -Bsymbolic. */
2758 break;
2759 }
2760
2761 if (h->plt.offset != (bfd_vma) -1)
2762 {
2763 if (htab->plt_second != NULL)
2764 {
2765 resolved_plt = htab->plt_second;
2766 plt_offset = eh->plt_second.offset;
2767 }
2768 else
2769 {
2770 resolved_plt = htab->elf.splt;
2771 plt_offset = h->plt.offset;
2772 }
2773 }
2774 else
2775 {
2776 resolved_plt = htab->plt_got;
2777 plt_offset = eh->plt_got.offset;
2778 }
2779
2780 relocation = (resolved_plt->output_section->vma
2781 + resolved_plt->output_offset
2782 + plt_offset);
2783 unresolved_reloc = FALSE;
2784 break;
2785
2786 case R_386_SIZE32:
2787 /* Set to symbol size. */
2788 relocation = st_size;
2789 /* Fall through. */
2790
2791 case R_386_32:
2792 case R_386_PC32:
2793 if ((input_section->flags & SEC_ALLOC) == 0
2794 || is_vxworks_tls)
2795 break;
2796
2797 /* Copy dynamic function pointer relocations. Don't generate
2798 dynamic relocations against resolved undefined weak symbols
2799 in PIE, except for R_386_PC32. */
2800 if ((bfd_link_pic (info)
2801 && (h == NULL
2802 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2803 && (!resolved_to_zero
2804 || r_type == R_386_PC32))
2805 || h->root.type != bfd_link_hash_undefweak))
2806 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32)
2807 || !SYMBOL_CALLS_LOCAL (info, h)))
2808 || (ELIMINATE_COPY_RELOCS
2809 && !bfd_link_pic (info)
2810 && h != NULL
2811 && h->dynindx != -1
2812 && (!h->non_got_ref
2813 || eh->func_pointer_refcount > 0
2814 || (h->root.type == bfd_link_hash_undefweak
2815 && !resolved_to_zero))
2816 && ((h->def_dynamic && !h->def_regular)
2817 /* Undefined weak symbol is bound locally when
2818 PIC is false. */
2819 || h->root.type == bfd_link_hash_undefweak)))
2820 {
2821 Elf_Internal_Rela outrel;
2822 bfd_boolean skip, relocate;
2823 asection *sreloc;
2824
2825 /* When generating a shared object, these relocations
2826 are copied into the output file to be resolved at run
2827 time. */
2828
2829 skip = FALSE;
2830 relocate = FALSE;
2831
2832 outrel.r_offset =
2833 _bfd_elf_section_offset (output_bfd, info, input_section,
2834 rel->r_offset);
2835 if (outrel.r_offset == (bfd_vma) -1)
2836 skip = TRUE;
2837 else if (outrel.r_offset == (bfd_vma) -2)
2838 skip = TRUE, relocate = TRUE;
2839 outrel.r_offset += (input_section->output_section->vma
2840 + input_section->output_offset);
2841
2842 if (skip)
2843 memset (&outrel, 0, sizeof outrel);
2844 else if (h != NULL
2845 && h->dynindx != -1
2846 && (r_type == R_386_PC32
2847 || !(bfd_link_executable (info)
2848 || SYMBOLIC_BIND (info, h))
2849 || !h->def_regular))
2850 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2851 else
2852 {
2853 /* This symbol is local, or marked to become local. */
2854 relocate = TRUE;
2855 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2856 }
2857
2858 sreloc = elf_section_data (input_section)->sreloc;
2859
2860 if (sreloc == NULL || sreloc->contents == NULL)
2861 {
2862 r = bfd_reloc_notsupported;
2863 goto check_relocation_error;
2864 }
2865
2866 elf_append_rel (output_bfd, sreloc, &outrel);
2867
2868 /* If this reloc is against an external symbol, we do
2869 not want to fiddle with the addend. Otherwise, we
2870 need to include the symbol value so that it becomes
2871 an addend for the dynamic reloc. */
2872 if (! relocate)
2873 continue;
2874 }
2875 break;
2876
2877 case R_386_TLS_IE:
2878 if (!bfd_link_executable (info))
2879 {
2880 Elf_Internal_Rela outrel;
2881 asection *sreloc;
2882
2883 outrel.r_offset = rel->r_offset
2884 + input_section->output_section->vma
2885 + input_section->output_offset;
2886 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2887 sreloc = elf_section_data (input_section)->sreloc;
2888 if (sreloc == NULL)
2889 abort ();
2890 elf_append_rel (output_bfd, sreloc, &outrel);
2891 }
2892 /* Fall through */
2893
2894 case R_386_TLS_GD:
2895 case R_386_TLS_GOTDESC:
2896 case R_386_TLS_DESC_CALL:
2897 case R_386_TLS_IE_32:
2898 case R_386_TLS_GOTIE:
2899 tls_type = GOT_UNKNOWN;
2900 if (h == NULL && local_got_offsets)
2901 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
2902 else if (h != NULL)
2903 tls_type = elf_x86_hash_entry(h)->tls_type;
2904 if (tls_type == GOT_TLS_IE)
2905 tls_type = GOT_TLS_IE_NEG;
2906
2907 r_type_tls = r_type;
2908 if (! elf_i386_tls_transition (info, input_bfd,
2909 input_section, contents,
2910 symtab_hdr, sym_hashes,
2911 &r_type_tls, tls_type, rel,
2912 relend, h, r_symndx, TRUE))
2913 return FALSE;
2914
2915 if (r_type_tls == R_386_TLS_LE_32)
2916 {
2917 BFD_ASSERT (! unresolved_reloc);
2918 if (r_type == R_386_TLS_GD)
2919 {
2920 unsigned int type;
2921 bfd_vma roff;
2922
2923 /* GD->LE transition. */
2924 type = *(contents + rel->r_offset - 2);
2925 if (type == 0x04)
2926 {
2927 /* Change
2928 leal foo@tlsgd(,%ebx,1), %eax
2929 call ___tls_get_addr@PLT
2930 into:
2931 movl %gs:0, %eax
2932 subl $foo@tpoff, %eax
2933 (6 byte form of subl). */
2934 roff = rel->r_offset + 5;
2935 }
2936 else
2937 {
2938 /* Change
2939 leal foo@tlsgd(%ebx), %eax
2940 call ___tls_get_addr@PLT
2941 nop
2942 or
2943 leal foo@tlsgd(%reg), %eax
2944 call *___tls_get_addr@GOT(%reg)
2945 which may be converted to
2946 addr32 call ___tls_get_addr
2947 into:
2948 movl %gs:0, %eax; subl $foo@tpoff, %eax
2949 (6 byte form of subl). */
2950 roff = rel->r_offset + 6;
2951 }
2952 memcpy (contents + roff - 8,
2953 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2954 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
2955 contents + roff);
2956 /* Skip R_386_PC32, R_386_PLT32 and R_386_GOT32X. */
2957 rel++;
2958 wrel++;
2959 continue;
2960 }
2961 else if (r_type == R_386_TLS_GOTDESC)
2962 {
2963 /* GDesc -> LE transition.
2964 It's originally something like:
2965 leal x@tlsdesc(%ebx), %eax
2966
2967 leal x@ntpoff, %eax
2968
2969 Registers other than %eax may be set up here. */
2970
2971 unsigned int val;
2972 bfd_vma roff;
2973
2974 roff = rel->r_offset;
2975 val = bfd_get_8 (input_bfd, contents + roff - 1);
2976
2977 /* Now modify the instruction as appropriate. */
2978 /* aoliva FIXME: remove the above and xor the byte
2979 below with 0x86. */
2980 bfd_put_8 (output_bfd, val ^ 0x86,
2981 contents + roff - 1);
2982 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
2983 contents + roff);
2984 continue;
2985 }
2986 else if (r_type == R_386_TLS_DESC_CALL)
2987 {
2988 /* GDesc -> LE transition.
2989 It's originally:
2990 call *(%eax)
2991 Turn it into:
2992 xchg %ax,%ax */
2993
2994 bfd_vma roff;
2995
2996 roff = rel->r_offset;
2997 bfd_put_8 (output_bfd, 0x66, contents + roff);
2998 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2999 continue;
3000 }
3001 else if (r_type == R_386_TLS_IE)
3002 {
3003 unsigned int val;
3004
3005 /* IE->LE transition:
3006 Originally it can be one of:
3007 movl foo, %eax
3008 movl foo, %reg
3009 addl foo, %reg
3010 We change it into:
3011 movl $foo, %eax
3012 movl $foo, %reg
3013 addl $foo, %reg. */
3014 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3015 if (val == 0xa1)
3016 {
3017 /* movl foo, %eax. */
3018 bfd_put_8 (output_bfd, 0xb8,
3019 contents + rel->r_offset - 1);
3020 }
3021 else
3022 {
3023 unsigned int type;
3024
3025 type = bfd_get_8 (input_bfd,
3026 contents + rel->r_offset - 2);
3027 switch (type)
3028 {
3029 case 0x8b:
3030 /* movl */
3031 bfd_put_8 (output_bfd, 0xc7,
3032 contents + rel->r_offset - 2);
3033 bfd_put_8 (output_bfd,
3034 0xc0 | ((val >> 3) & 7),
3035 contents + rel->r_offset - 1);
3036 break;
3037 case 0x03:
3038 /* addl */
3039 bfd_put_8 (output_bfd, 0x81,
3040 contents + rel->r_offset - 2);
3041 bfd_put_8 (output_bfd,
3042 0xc0 | ((val >> 3) & 7),
3043 contents + rel->r_offset - 1);
3044 break;
3045 default:
3046 BFD_FAIL ();
3047 break;
3048 }
3049 }
3050 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3051 contents + rel->r_offset);
3052 continue;
3053 }
3054 else
3055 {
3056 unsigned int val, type;
3057
3058 /* {IE_32,GOTIE}->LE transition:
3059 Originally it can be one of:
3060 subl foo(%reg1), %reg2
3061 movl foo(%reg1), %reg2
3062 addl foo(%reg1), %reg2
3063 We change it into:
3064 subl $foo, %reg2
3065 movl $foo, %reg2 (6 byte form)
3066 addl $foo, %reg2. */
3067 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3068 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3069 if (type == 0x8b)
3070 {
3071 /* movl */
3072 bfd_put_8 (output_bfd, 0xc7,
3073 contents + rel->r_offset - 2);
3074 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3075 contents + rel->r_offset - 1);
3076 }
3077 else if (type == 0x2b)
3078 {
3079 /* subl */
3080 bfd_put_8 (output_bfd, 0x81,
3081 contents + rel->r_offset - 2);
3082 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3083 contents + rel->r_offset - 1);
3084 }
3085 else if (type == 0x03)
3086 {
3087 /* addl */
3088 bfd_put_8 (output_bfd, 0x81,
3089 contents + rel->r_offset - 2);
3090 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3091 contents + rel->r_offset - 1);
3092 }
3093 else
3094 BFD_FAIL ();
3095 if (r_type == R_386_TLS_GOTIE)
3096 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3097 contents + rel->r_offset);
3098 else
3099 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3100 contents + rel->r_offset);
3101 continue;
3102 }
3103 }
3104
3105 if (htab->elf.sgot == NULL)
3106 abort ();
3107
3108 if (h != NULL)
3109 {
3110 off = h->got.offset;
3111 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3112 }
3113 else
3114 {
3115 if (local_got_offsets == NULL)
3116 abort ();
3117
3118 off = local_got_offsets[r_symndx];
3119 offplt = local_tlsdesc_gotents[r_symndx];
3120 }
3121
3122 if ((off & 1) != 0)
3123 off &= ~1;
3124 else
3125 {
3126 Elf_Internal_Rela outrel;
3127 int dr_type;
3128 asection *sreloc;
3129
3130 if (htab->elf.srelgot == NULL)
3131 abort ();
3132
3133 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3134
3135 if (GOT_TLS_GDESC_P (tls_type))
3136 {
3137 bfd_byte *loc;
3138 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3139 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3140 <= htab->elf.sgotplt->size);
3141 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3142 + htab->elf.sgotplt->output_offset
3143 + offplt
3144 + htab->sgotplt_jump_table_size);
3145 sreloc = htab->elf.srelplt;
3146 loc = sreloc->contents;
3147 loc += (htab->next_tls_desc_index++
3148 * sizeof (Elf32_External_Rel));
3149 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3150 <= sreloc->contents + sreloc->size);
3151 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3152 if (indx == 0)
3153 {
3154 BFD_ASSERT (! unresolved_reloc);
3155 bfd_put_32 (output_bfd,
3156 relocation - _bfd_x86_elf_dtpoff_base (info),
3157 htab->elf.sgotplt->contents + offplt
3158 + htab->sgotplt_jump_table_size + 4);
3159 }
3160 else
3161 {
3162 bfd_put_32 (output_bfd, 0,
3163 htab->elf.sgotplt->contents + offplt
3164 + htab->sgotplt_jump_table_size + 4);
3165 }
3166 }
3167
3168 sreloc = htab->elf.srelgot;
3169
3170 outrel.r_offset = (htab->elf.sgot->output_section->vma
3171 + htab->elf.sgot->output_offset + off);
3172
3173 if (GOT_TLS_GD_P (tls_type))
3174 dr_type = R_386_TLS_DTPMOD32;
3175 else if (GOT_TLS_GDESC_P (tls_type))
3176 goto dr_done;
3177 else if (tls_type == GOT_TLS_IE_POS)
3178 dr_type = R_386_TLS_TPOFF;
3179 else
3180 dr_type = R_386_TLS_TPOFF32;
3181
3182 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3183 bfd_put_32 (output_bfd,
3184 relocation - _bfd_x86_elf_dtpoff_base (info),
3185 htab->elf.sgot->contents + off);
3186 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3187 bfd_put_32 (output_bfd,
3188 _bfd_x86_elf_dtpoff_base (info) - relocation,
3189 htab->elf.sgot->contents + off);
3190 else if (dr_type != R_386_TLS_DESC)
3191 bfd_put_32 (output_bfd, 0,
3192 htab->elf.sgot->contents + off);
3193 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3194
3195 elf_append_rel (output_bfd, sreloc, &outrel);
3196
3197 if (GOT_TLS_GD_P (tls_type))
3198 {
3199 if (indx == 0)
3200 {
3201 BFD_ASSERT (! unresolved_reloc);
3202 bfd_put_32 (output_bfd,
3203 relocation - _bfd_x86_elf_dtpoff_base (info),
3204 htab->elf.sgot->contents + off + 4);
3205 }
3206 else
3207 {
3208 bfd_put_32 (output_bfd, 0,
3209 htab->elf.sgot->contents + off + 4);
3210 outrel.r_info = ELF32_R_INFO (indx,
3211 R_386_TLS_DTPOFF32);
3212 outrel.r_offset += 4;
3213 elf_append_rel (output_bfd, sreloc, &outrel);
3214 }
3215 }
3216 else if (tls_type == GOT_TLS_IE_BOTH)
3217 {
3218 bfd_put_32 (output_bfd,
3219 (indx == 0
3220 ? relocation - _bfd_x86_elf_dtpoff_base (info)
3221 : 0),
3222 htab->elf.sgot->contents + off + 4);
3223 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3224 outrel.r_offset += 4;
3225 elf_append_rel (output_bfd, sreloc, &outrel);
3226 }
3227
3228 dr_done:
3229 if (h != NULL)
3230 h->got.offset |= 1;
3231 else
3232 local_got_offsets[r_symndx] |= 1;
3233 }
3234
3235 if (off >= (bfd_vma) -2
3236 && ! GOT_TLS_GDESC_P (tls_type))
3237 abort ();
3238 if (r_type_tls == R_386_TLS_GOTDESC
3239 || r_type_tls == R_386_TLS_DESC_CALL)
3240 {
3241 relocation = htab->sgotplt_jump_table_size + offplt;
3242 unresolved_reloc = FALSE;
3243 }
3244 else if (r_type_tls == r_type)
3245 {
3246 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma
3247 + htab->elf.sgotplt->output_offset;
3248 relocation = htab->elf.sgot->output_section->vma
3249 + htab->elf.sgot->output_offset + off - g_o_t;
3250 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3251 && tls_type == GOT_TLS_IE_BOTH)
3252 relocation += 4;
3253 if (r_type == R_386_TLS_IE)
3254 relocation += g_o_t;
3255 unresolved_reloc = FALSE;
3256 }
3257 else if (r_type == R_386_TLS_GD)
3258 {
3259 unsigned int val, type;
3260 bfd_vma roff;
3261
3262 /* GD->IE transition. */
3263 type = *(contents + rel->r_offset - 2);
3264 val = *(contents + rel->r_offset - 1);
3265 if (type == 0x04)
3266 {
3267 /* Change
3268 leal foo@tlsgd(,%ebx,1), %eax
3269 call ___tls_get_addr@PLT
3270 into:
3271 movl %gs:0, %eax
3272 subl $foo@gottpoff(%ebx), %eax. */
3273 val >>= 3;
3274 roff = rel->r_offset - 3;
3275 }
3276 else
3277 {
3278 /* Change
3279 leal foo@tlsgd(%ebx), %eax
3280 call ___tls_get_addr@PLT
3281 nop
3282 or
3283 leal foo@tlsgd(%reg), %eax
3284 call *___tls_get_addr@GOT(%reg)
3285 which may be converted to
3286 addr32 call ___tls_get_addr
3287 into:
3288 movl %gs:0, %eax;
3289 subl $foo@gottpoff(%reg), %eax. */
3290 roff = rel->r_offset - 2;
3291 }
3292 memcpy (contents + roff,
3293 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3294 contents[roff + 7] = 0x80 | (val & 7);
3295 /* If foo is used only with foo@gotntpoff(%reg) and
3296 foo@indntpoff, but not with foo@gottpoff(%reg), change
3297 subl $foo@gottpoff(%reg), %eax
3298 into:
3299 addl $foo@gotntpoff(%reg), %eax. */
3300 if (tls_type == GOT_TLS_IE_POS)
3301 contents[roff + 6] = 0x03;
3302 bfd_put_32 (output_bfd,
3303 htab->elf.sgot->output_section->vma
3304 + htab->elf.sgot->output_offset + off
3305 - htab->elf.sgotplt->output_section->vma
3306 - htab->elf.sgotplt->output_offset,
3307 contents + roff + 8);
3308 /* Skip R_386_PLT32 and R_386_GOT32X. */
3309 rel++;
3310 wrel++;
3311 continue;
3312 }
3313 else if (r_type == R_386_TLS_GOTDESC)
3314 {
3315 /* GDesc -> IE transition.
3316 It's originally something like:
3317 leal x@tlsdesc(%ebx), %eax
3318
3319 Change it to:
3320 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3321 or:
3322 movl x@gottpoff(%ebx), %eax # before negl %eax
3323
3324 Registers other than %eax may be set up here. */
3325
3326 bfd_vma roff;
3327
3328 /* First, make sure it's a leal adding ebx to a 32-bit
3329 offset into any register, although it's probably
3330 almost always going to be eax. */
3331 roff = rel->r_offset;
3332
3333 /* Now modify the instruction as appropriate. */
3334 /* To turn a leal into a movl in the form we use it, it
3335 suffices to change the first byte from 0x8d to 0x8b.
3336 aoliva FIXME: should we decide to keep the leal, all
3337 we have to do is remove the statement below, and
3338 adjust the relaxation of R_386_TLS_DESC_CALL. */
3339 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3340
3341 if (tls_type == GOT_TLS_IE_BOTH)
3342 off += 4;
3343
3344 bfd_put_32 (output_bfd,
3345 htab->elf.sgot->output_section->vma
3346 + htab->elf.sgot->output_offset + off
3347 - htab->elf.sgotplt->output_section->vma
3348 - htab->elf.sgotplt->output_offset,
3349 contents + roff);
3350 continue;
3351 }
3352 else if (r_type == R_386_TLS_DESC_CALL)
3353 {
3354 /* GDesc -> IE transition.
3355 It's originally:
3356 call *(%eax)
3357
3358 Change it to:
3359 xchg %ax,%ax
3360 or
3361 negl %eax
3362 depending on how we transformed the TLS_GOTDESC above.
3363 */
3364
3365 bfd_vma roff;
3366
3367 roff = rel->r_offset;
3368
3369 /* Now modify the instruction as appropriate. */
3370 if (tls_type != GOT_TLS_IE_NEG)
3371 {
3372 /* xchg %ax,%ax */
3373 bfd_put_8 (output_bfd, 0x66, contents + roff);
3374 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3375 }
3376 else
3377 {
3378 /* negl %eax */
3379 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3380 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3381 }
3382
3383 continue;
3384 }
3385 else
3386 BFD_ASSERT (FALSE);
3387 break;
3388
3389 case R_386_TLS_LDM:
3390 if (! elf_i386_tls_transition (info, input_bfd,
3391 input_section, contents,
3392 symtab_hdr, sym_hashes,
3393 &r_type, GOT_UNKNOWN, rel,
3394 relend, h, r_symndx, TRUE))
3395 return FALSE;
3396
3397 if (r_type != R_386_TLS_LDM)
3398 {
3399 /* LD->LE transition. Change
3400 leal foo@tlsldm(%ebx) %eax
3401 call ___tls_get_addr@PLT
3402 into:
3403 movl %gs:0, %eax
3404 nop
3405 leal 0(%esi,1), %esi
3406 or change
3407 leal foo@tlsldm(%reg) %eax
3408 call *___tls_get_addr@GOT(%reg)
3409 which may be converted to
3410 addr32 call ___tls_get_addr
3411 into:
3412 movl %gs:0, %eax
3413 leal 0(%esi), %esi */
3414 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3415 if (*(contents + rel->r_offset + 4) == 0xff
3416 || *(contents + rel->r_offset + 4) == 0x67)
3417 memcpy (contents + rel->r_offset - 2,
3418 "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0", 12);
3419 else
3420 memcpy (contents + rel->r_offset - 2,
3421 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3422 /* Skip R_386_PC32/R_386_PLT32. */
3423 rel++;
3424 wrel++;
3425 continue;
3426 }
3427
3428 if (htab->elf.sgot == NULL)
3429 abort ();
3430
3431 off = htab->tls_ld_or_ldm_got.offset;
3432 if (off & 1)
3433 off &= ~1;
3434 else
3435 {
3436 Elf_Internal_Rela outrel;
3437
3438 if (htab->elf.srelgot == NULL)
3439 abort ();
3440
3441 outrel.r_offset = (htab->elf.sgot->output_section->vma
3442 + htab->elf.sgot->output_offset + off);
3443
3444 bfd_put_32 (output_bfd, 0,
3445 htab->elf.sgot->contents + off);
3446 bfd_put_32 (output_bfd, 0,
3447 htab->elf.sgot->contents + off + 4);
3448 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3449 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel);
3450 htab->tls_ld_or_ldm_got.offset |= 1;
3451 }
3452 relocation = htab->elf.sgot->output_section->vma
3453 + htab->elf.sgot->output_offset + off
3454 - htab->elf.sgotplt->output_section->vma
3455 - htab->elf.sgotplt->output_offset;
3456 unresolved_reloc = FALSE;
3457 break;
3458
3459 case R_386_TLS_LDO_32:
3460 if (!bfd_link_executable (info)
3461 || (input_section->flags & SEC_CODE) == 0)
3462 relocation -= _bfd_x86_elf_dtpoff_base (info);
3463 else
3464 /* When converting LDO to LE, we must negate. */
3465 relocation = -elf_i386_tpoff (info, relocation);
3466 break;
3467
3468 case R_386_TLS_LE_32:
3469 case R_386_TLS_LE:
3470 if (!bfd_link_executable (info))
3471 {
3472 Elf_Internal_Rela outrel;
3473 asection *sreloc;
3474
3475 outrel.r_offset = rel->r_offset
3476 + input_section->output_section->vma
3477 + input_section->output_offset;
3478 if (h != NULL && h->dynindx != -1)
3479 indx = h->dynindx;
3480 else
3481 indx = 0;
3482 if (r_type == R_386_TLS_LE_32)
3483 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3484 else
3485 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3486 sreloc = elf_section_data (input_section)->sreloc;
3487 if (sreloc == NULL)
3488 abort ();
3489 elf_append_rel (output_bfd, sreloc, &outrel);
3490 if (indx)
3491 continue;
3492 else if (r_type == R_386_TLS_LE_32)
3493 relocation = _bfd_x86_elf_dtpoff_base (info) - relocation;
3494 else
3495 relocation -= _bfd_x86_elf_dtpoff_base (info);
3496 }
3497 else if (r_type == R_386_TLS_LE_32)
3498 relocation = elf_i386_tpoff (info, relocation);
3499 else
3500 relocation = -elf_i386_tpoff (info, relocation);
3501 break;
3502
3503 default:
3504 break;
3505 }
3506
3507 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3508 because such sections are not SEC_ALLOC and thus ld.so will
3509 not process them. */
3510 if (unresolved_reloc
3511 && !((input_section->flags & SEC_DEBUGGING) != 0
3512 && h->def_dynamic)
3513 && _bfd_elf_section_offset (output_bfd, info, input_section,
3514 rel->r_offset) != (bfd_vma) -1)
3515 {
3516 _bfd_error_handler
3517 /* xgettext:c-format */
3518 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
3519 input_bfd,
3520 input_section,
3521 rel->r_offset,
3522 howto->name,
3523 h->root.root.string);
3524 return FALSE;
3525 }
3526
3527 do_relocation:
3528 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3529 contents, rel->r_offset,
3530 relocation, 0);
3531
3532 check_relocation_error:
3533 if (r != bfd_reloc_ok)
3534 {
3535 const char *name;
3536
3537 if (h != NULL)
3538 name = h->root.root.string;
3539 else
3540 {
3541 name = bfd_elf_string_from_elf_section (input_bfd,
3542 symtab_hdr->sh_link,
3543 sym->st_name);
3544 if (name == NULL)
3545 return FALSE;
3546 if (*name == '\0')
3547 name = bfd_section_name (input_bfd, sec);
3548 }
3549
3550 if (r == bfd_reloc_overflow)
3551 (*info->callbacks->reloc_overflow)
3552 (info, (h ? &h->root : NULL), name, howto->name,
3553 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3554 else
3555 {
3556 _bfd_error_handler
3557 /* xgettext:c-format */
3558 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
3559 input_bfd, input_section,
3560 rel->r_offset, name, (int) r);
3561 return FALSE;
3562 }
3563 }
3564
3565 if (wrel != rel)
3566 *wrel = *rel;
3567 }
3568
3569 if (wrel != rel)
3570 {
3571 Elf_Internal_Shdr *rel_hdr;
3572 size_t deleted = rel - wrel;
3573
3574 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
3575 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3576 if (rel_hdr->sh_size == 0)
3577 {
3578 /* It is too late to remove an empty reloc section. Leave
3579 one NONE reloc.
3580 ??? What is wrong with an empty section??? */
3581 rel_hdr->sh_size = rel_hdr->sh_entsize;
3582 deleted -= 1;
3583 }
3584 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
3585 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
3586 input_section->reloc_count -= deleted;
3587 }
3588
3589 return TRUE;
3590 }
3591
3592 /* Finish up dynamic symbol handling. We set the contents of various
3593 dynamic sections here. */
3594
3595 static bfd_boolean
3596 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3597 struct bfd_link_info *info,
3598 struct elf_link_hash_entry *h,
3599 Elf_Internal_Sym *sym)
3600 {
3601 struct elf_x86_link_hash_table *htab;
3602 unsigned plt_entry_size;
3603 struct elf_x86_link_hash_entry *eh;
3604 bfd_boolean local_undefweak;
3605 bfd_boolean use_plt_second;
3606
3607 htab = elf_x86_hash_table (info, I386_ELF_DATA);
3608 if (htab == NULL)
3609 return FALSE;
3610
3611 plt_entry_size = htab->plt.plt_entry_size;
3612
3613 /* Use the second PLT section only if there is .plt section. */
3614 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
3615
3616 eh = (struct elf_x86_link_hash_entry *) h;
3617 if (eh->no_finish_dynamic_symbol)
3618 abort ();
3619
3620 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
3621 resolved undefined weak symbols in executable so that their
3622 references have value 0 at run-time. */
3623 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
3624
3625 if (h->plt.offset != (bfd_vma) -1)
3626 {
3627 bfd_vma plt_index, plt_offset;
3628 bfd_vma got_offset;
3629 Elf_Internal_Rela rel;
3630 bfd_byte *loc;
3631 asection *plt, *resolved_plt, *gotplt, *relplt;
3632
3633 /* When building a static executable, use .iplt, .igot.plt and
3634 .rel.iplt sections for STT_GNU_IFUNC symbols. */
3635 if (htab->elf.splt != NULL)
3636 {
3637 plt = htab->elf.splt;
3638 gotplt = htab->elf.sgotplt;
3639 relplt = htab->elf.srelplt;
3640 }
3641 else
3642 {
3643 plt = htab->elf.iplt;
3644 gotplt = htab->elf.igotplt;
3645 relplt = htab->elf.irelplt;
3646 }
3647
3648 /* This symbol has an entry in the procedure linkage table. Set
3649 it up. */
3650
3651 if ((h->dynindx == -1
3652 && !local_undefweak
3653 && !((h->forced_local || bfd_link_executable (info))
3654 && h->def_regular
3655 && h->type == STT_GNU_IFUNC))
3656 || plt == NULL
3657 || gotplt == NULL
3658 || relplt == NULL)
3659 abort ();
3660
3661 /* Get the index in the procedure linkage table which
3662 corresponds to this symbol. This is the index of this symbol
3663 in all the symbols for which we are making plt entries. The
3664 first entry in the procedure linkage table is reserved.
3665
3666 Get the offset into the .got table of the entry that
3667 corresponds to this function. Each .got entry is 4 bytes.
3668 The first three are reserved.
3669
3670 For static executables, we don't reserve anything. */
3671
3672 if (plt == htab->elf.splt)
3673 {
3674 got_offset = (h->plt.offset / plt_entry_size
3675 - htab->plt.has_plt0);
3676 got_offset = (got_offset + 3) * 4;
3677 }
3678 else
3679 {
3680 got_offset = h->plt.offset / plt_entry_size;
3681 got_offset = got_offset * 4;
3682 }
3683
3684 /* Fill in the entry in the procedure linkage table and update
3685 the first slot. */
3686 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
3687 plt_entry_size);
3688
3689 if (use_plt_second)
3690 {
3691 const bfd_byte *plt_entry;
3692 if (bfd_link_pic (info))
3693 plt_entry = htab->non_lazy_plt->pic_plt_entry;
3694 else
3695 plt_entry = htab->non_lazy_plt->plt_entry;
3696 memcpy (htab->plt_second->contents + eh->plt_second.offset,
3697 plt_entry, htab->non_lazy_plt->plt_entry_size);
3698
3699 resolved_plt = htab->plt_second;
3700 plt_offset = eh->plt_second.offset;
3701 }
3702 else
3703 {
3704 resolved_plt = plt;
3705 plt_offset = h->plt.offset;
3706 }
3707
3708 if (! bfd_link_pic (info))
3709 {
3710 bfd_put_32 (output_bfd,
3711 (gotplt->output_section->vma
3712 + gotplt->output_offset
3713 + got_offset),
3714 resolved_plt->contents + plt_offset
3715 + htab->plt.plt_got_offset);
3716
3717 if (htab->is_vxworks)
3718 {
3719 int s, k, reloc_index;
3720
3721 /* Create the R_386_32 relocation referencing the GOT
3722 for this PLT entry. */
3723
3724 /* S: Current slot number (zero-based). */
3725 s = ((h->plt.offset - htab->plt.plt_entry_size)
3726 / htab->plt.plt_entry_size);
3727 /* K: Number of relocations for PLTResolve. */
3728 if (bfd_link_pic (info))
3729 k = PLTRESOLVE_RELOCS_SHLIB;
3730 else
3731 k = PLTRESOLVE_RELOCS;
3732 /* Skip the PLTresolve relocations, and the relocations for
3733 the other PLT slots. */
3734 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
3735 loc = (htab->srelplt2->contents + reloc_index
3736 * sizeof (Elf32_External_Rel));
3737
3738 rel.r_offset = (plt->output_section->vma
3739 + plt->output_offset
3740 + h->plt.offset + 2),
3741 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
3742 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3743
3744 /* Create the R_386_32 relocation referencing the beginning of
3745 the PLT for this GOT entry. */
3746 rel.r_offset = (htab->elf.sgotplt->output_section->vma
3747 + htab->elf.sgotplt->output_offset
3748 + got_offset);
3749 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
3750 bfd_elf32_swap_reloc_out (output_bfd, &rel,
3751 loc + sizeof (Elf32_External_Rel));
3752 }
3753 }
3754 else
3755 {
3756 bfd_put_32 (output_bfd, got_offset,
3757 resolved_plt->contents + plt_offset
3758 + htab->plt.plt_got_offset);
3759 }
3760
3761 /* Fill in the entry in the global offset table. Leave the entry
3762 as zero for undefined weak symbol in PIE. No PLT relocation
3763 against undefined weak symbol in PIE. */
3764 if (!local_undefweak)
3765 {
3766 if (htab->plt.has_plt0)
3767 bfd_put_32 (output_bfd,
3768 (plt->output_section->vma
3769 + plt->output_offset
3770 + h->plt.offset
3771 + htab->lazy_plt->plt_lazy_offset),
3772 gotplt->contents + got_offset);
3773
3774 /* Fill in the entry in the .rel.plt section. */
3775 rel.r_offset = (gotplt->output_section->vma
3776 + gotplt->output_offset
3777 + got_offset);
3778 if (h->dynindx == -1
3779 || ((bfd_link_executable (info)
3780 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3781 && h->def_regular
3782 && h->type == STT_GNU_IFUNC))
3783 {
3784 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3785 h->root.root.string,
3786 h->root.u.def.section->owner);
3787
3788 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3789 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
3790 in the .got.plt section. */
3791 bfd_put_32 (output_bfd,
3792 (h->root.u.def.value
3793 + h->root.u.def.section->output_section->vma
3794 + h->root.u.def.section->output_offset),
3795 gotplt->contents + got_offset);
3796 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3797 /* R_386_IRELATIVE comes last. */
3798 plt_index = htab->next_irelative_index--;
3799 }
3800 else
3801 {
3802 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3803 plt_index = htab->next_jump_slot_index++;
3804 }
3805
3806 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
3807 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3808
3809 /* Don't fill the second and third slots in PLT entry for
3810 static executables nor without PLT0. */
3811 if (plt == htab->elf.splt && htab->plt.has_plt0)
3812 {
3813 bfd_put_32 (output_bfd,
3814 plt_index * sizeof (Elf32_External_Rel),
3815 plt->contents + h->plt.offset
3816 + htab->lazy_plt->plt_reloc_offset);
3817 bfd_put_32 (output_bfd,
3818 - (h->plt.offset
3819 + htab->lazy_plt->plt_plt_offset + 4),
3820 (plt->contents + h->plt.offset
3821 + htab->lazy_plt->plt_plt_offset));
3822 }
3823 }
3824 }
3825 else if (eh->plt_got.offset != (bfd_vma) -1)
3826 {
3827 bfd_vma got_offset, plt_offset;
3828 asection *plt, *got, *gotplt;
3829 const bfd_byte *got_plt_entry;
3830
3831 /* Set the entry in the GOT procedure linkage table. */
3832 plt = htab->plt_got;
3833 got = htab->elf.sgot;
3834 gotplt = htab->elf.sgotplt;
3835 got_offset = h->got.offset;
3836
3837 if (got_offset == (bfd_vma) -1
3838 || plt == NULL
3839 || got == NULL
3840 || gotplt == NULL)
3841 abort ();
3842
3843 /* Fill in the entry in the GOT procedure linkage table. */
3844 if (! bfd_link_pic (info))
3845 {
3846 got_plt_entry = htab->non_lazy_plt->plt_entry;
3847 got_offset += got->output_section->vma + got->output_offset;
3848 }
3849 else
3850 {
3851 got_plt_entry = htab->non_lazy_plt->pic_plt_entry;
3852 got_offset += (got->output_section->vma
3853 + got->output_offset
3854 - gotplt->output_section->vma
3855 - gotplt->output_offset);
3856 }
3857
3858 plt_offset = eh->plt_got.offset;
3859 memcpy (plt->contents + plt_offset, got_plt_entry,
3860 htab->non_lazy_plt->plt_entry_size);
3861 bfd_put_32 (output_bfd, got_offset,
3862 (plt->contents + plt_offset
3863 + htab->non_lazy_plt->plt_got_offset));
3864 }
3865
3866 if (!local_undefweak
3867 && !h->def_regular
3868 && (h->plt.offset != (bfd_vma) -1
3869 || eh->plt_got.offset != (bfd_vma) -1))
3870 {
3871 /* Mark the symbol as undefined, rather than as defined in
3872 the .plt section. Leave the value if there were any
3873 relocations where pointer equality matters (this is a clue
3874 for the dynamic linker, to make function pointer
3875 comparisons work between an application and shared
3876 library), otherwise set it to zero. If a function is only
3877 called from a binary, there is no need to slow down
3878 shared libraries because of that. */
3879 sym->st_shndx = SHN_UNDEF;
3880 if (!h->pointer_equality_needed)
3881 sym->st_value = 0;
3882 }
3883
3884 /* Don't generate dynamic GOT relocation against undefined weak
3885 symbol in executable. */
3886 if (h->got.offset != (bfd_vma) -1
3887 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry(h)->tls_type)
3888 && (elf_x86_hash_entry(h)->tls_type & GOT_TLS_IE) == 0
3889 && !local_undefweak)
3890 {
3891 Elf_Internal_Rela rel;
3892 asection *relgot = htab->elf.srelgot;
3893
3894 /* This symbol has an entry in the global offset table. Set it
3895 up. */
3896
3897 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3898 abort ();
3899
3900 rel.r_offset = (htab->elf.sgot->output_section->vma
3901 + htab->elf.sgot->output_offset
3902 + (h->got.offset & ~(bfd_vma) 1));
3903
3904 /* If this is a static link, or it is a -Bsymbolic link and the
3905 symbol is defined locally or was forced to be local because
3906 of a version file, we just want to emit a RELATIVE reloc.
3907 The entry in the global offset table will already have been
3908 initialized in the relocate_section function. */
3909 if (h->def_regular
3910 && h->type == STT_GNU_IFUNC)
3911 {
3912 if (h->plt.offset == (bfd_vma) -1)
3913 {
3914 /* STT_GNU_IFUNC is referenced without PLT. */
3915 if (htab->elf.splt == NULL)
3916 {
3917 /* use .rel[a].iplt section to store .got relocations
3918 in static executable. */
3919 relgot = htab->elf.irelplt;
3920 }
3921 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3922 {
3923 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
3924 h->root.root.string,
3925 h->root.u.def.section->owner);
3926
3927 bfd_put_32 (output_bfd,
3928 (h->root.u.def.value
3929 + h->root.u.def.section->output_section->vma
3930 + h->root.u.def.section->output_offset),
3931 htab->elf.sgot->contents + h->got.offset);
3932 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
3933 }
3934 else
3935 goto do_glob_dat;
3936 }
3937 else if (bfd_link_pic (info))
3938 {
3939 /* Generate R_386_GLOB_DAT. */
3940 goto do_glob_dat;
3941 }
3942 else
3943 {
3944 asection *plt;
3945 bfd_vma plt_offset;
3946
3947 if (!h->pointer_equality_needed)
3948 abort ();
3949
3950 /* For non-shared object, we can't use .got.plt, which
3951 contains the real function addres if we need pointer
3952 equality. We load the GOT entry with the PLT entry. */
3953 if (htab->plt_second != NULL)
3954 {
3955 plt = htab->plt_second;
3956 plt_offset = eh->plt_second.offset;
3957 }
3958 else
3959 {
3960 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3961 plt_offset = h->plt.offset;
3962 }
3963 bfd_put_32 (output_bfd,
3964 (plt->output_section->vma
3965 + plt->output_offset + plt_offset),
3966 htab->elf.sgot->contents + h->got.offset);
3967 return TRUE;
3968 }
3969 }
3970 else if (bfd_link_pic (info)
3971 && SYMBOL_REFERENCES_LOCAL_P (info, h))
3972 {
3973 BFD_ASSERT((h->got.offset & 1) != 0);
3974 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3975 }
3976 else
3977 {
3978 BFD_ASSERT((h->got.offset & 1) == 0);
3979 do_glob_dat:
3980 bfd_put_32 (output_bfd, (bfd_vma) 0,
3981 htab->elf.sgot->contents + h->got.offset);
3982 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3983 }
3984
3985 elf_append_rel (output_bfd, relgot, &rel);
3986 }
3987
3988 if (h->needs_copy)
3989 {
3990 Elf_Internal_Rela rel;
3991 asection *s;
3992
3993 /* This symbol needs a copy reloc. Set it up. */
3994
3995 if (h->dynindx == -1
3996 || (h->root.type != bfd_link_hash_defined
3997 && h->root.type != bfd_link_hash_defweak)
3998 || htab->elf.srelbss == NULL
3999 || htab->elf.sreldynrelro == NULL)
4000 abort ();
4001
4002 rel.r_offset = (h->root.u.def.value
4003 + h->root.u.def.section->output_section->vma
4004 + h->root.u.def.section->output_offset);
4005 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4006 if (h->root.u.def.section == htab->elf.sdynrelro)
4007 s = htab->elf.sreldynrelro;
4008 else
4009 s = htab->elf.srelbss;
4010 elf_append_rel (output_bfd, s, &rel);
4011 }
4012
4013 return TRUE;
4014 }
4015
4016 /* Finish up local dynamic symbol handling. We set the contents of
4017 various dynamic sections here. */
4018
4019 static bfd_boolean
4020 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf)
4021 {
4022 struct elf_link_hash_entry *h
4023 = (struct elf_link_hash_entry *) *slot;
4024 struct bfd_link_info *info
4025 = (struct bfd_link_info *) inf;
4026
4027 return elf_i386_finish_dynamic_symbol (info->output_bfd, info,
4028 h, NULL);
4029 }
4030
4031 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4032 here since undefined weak symbol may not be dynamic and may not be
4033 called for elf_i386_finish_dynamic_symbol. */
4034
4035 static bfd_boolean
4036 elf_i386_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4037 void *inf)
4038 {
4039 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4040 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4041
4042 if (h->root.type != bfd_link_hash_undefweak
4043 || h->dynindx != -1)
4044 return TRUE;
4045
4046 return elf_i386_finish_dynamic_symbol (info->output_bfd,
4047 info, h, NULL);
4048 }
4049
4050 /* Used to decide how to sort relocs in an optimal manner for the
4051 dynamic linker, before writing them out. */
4052
4053 static enum elf_reloc_type_class
4054 elf_i386_reloc_type_class (const struct bfd_link_info *info,
4055 const asection *rel_sec ATTRIBUTE_UNUSED,
4056 const Elf_Internal_Rela *rela)
4057 {
4058 bfd *abfd = info->output_bfd;
4059 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4060 struct elf_link_hash_table *htab = elf_hash_table (info);
4061
4062 if (htab->dynsym != NULL
4063 && htab->dynsym->contents != NULL)
4064 {
4065 /* Check relocation against STT_GNU_IFUNC symbol if there are
4066 dynamic symbols. */
4067 unsigned long r_symndx = ELF32_R_SYM (rela->r_info);
4068 if (r_symndx != STN_UNDEF)
4069 {
4070 Elf_Internal_Sym sym;
4071 if (!bed->s->swap_symbol_in (abfd,
4072 (htab->dynsym->contents
4073 + r_symndx * sizeof (Elf32_External_Sym)),
4074 0, &sym))
4075 abort ();
4076
4077 if (ELF32_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4078 return reloc_class_ifunc;
4079 }
4080 }
4081
4082 switch (ELF32_R_TYPE (rela->r_info))
4083 {
4084 case R_386_IRELATIVE:
4085 return reloc_class_ifunc;
4086 case R_386_RELATIVE:
4087 return reloc_class_relative;
4088 case R_386_JUMP_SLOT:
4089 return reloc_class_plt;
4090 case R_386_COPY:
4091 return reloc_class_copy;
4092 default:
4093 return reloc_class_normal;
4094 }
4095 }
4096
4097 /* Finish up the dynamic sections. */
4098
4099 static bfd_boolean
4100 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4101 struct bfd_link_info *info)
4102 {
4103 struct elf_x86_link_hash_table *htab;
4104 bfd *dynobj;
4105 asection *sdyn;
4106 const struct elf_i386_backend_data *abed;
4107
4108 htab = elf_x86_hash_table (info, I386_ELF_DATA);
4109 if (htab == NULL)
4110 return FALSE;
4111
4112 dynobj = htab->elf.dynobj;
4113 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4114 abed = get_elf_i386_backend_data (output_bfd);
4115
4116 if (htab->elf.dynamic_sections_created)
4117 {
4118 Elf32_External_Dyn *dyncon, *dynconend;
4119
4120 if (sdyn == NULL || htab->elf.sgot == NULL)
4121 abort ();
4122
4123 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4124 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4125 for (; dyncon < dynconend; dyncon++)
4126 {
4127 Elf_Internal_Dyn dyn;
4128 asection *s;
4129
4130 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4131
4132 switch (dyn.d_tag)
4133 {
4134 default:
4135 if (htab->is_vxworks
4136 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4137 break;
4138 continue;
4139
4140 case DT_PLTGOT:
4141 s = htab->elf.sgotplt;
4142 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4143 break;
4144
4145 case DT_JMPREL:
4146 s = htab->elf.srelplt;
4147 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4148 break;
4149
4150 case DT_PLTRELSZ:
4151 s = htab->elf.srelplt;
4152 dyn.d_un.d_val = s->size;
4153 break;
4154 }
4155
4156 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4157 }
4158
4159 if (htab->elf.splt && htab->elf.splt->size > 0)
4160 {
4161 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4162 really seem like the right value. */
4163 elf_section_data (htab->elf.splt->output_section)
4164 ->this_hdr.sh_entsize = 4;
4165
4166 if (htab->plt.has_plt0)
4167 {
4168 /* Fill in the special first entry in the procedure linkage
4169 table. */
4170 memcpy (htab->elf.splt->contents, htab->plt.plt0_entry,
4171 htab->lazy_plt->plt0_entry_size);
4172 memset (htab->elf.splt->contents + htab->lazy_plt->plt0_entry_size,
4173 abed->plt0_pad_byte,
4174 htab->plt.plt_entry_size - htab->lazy_plt->plt0_entry_size);
4175 if (!bfd_link_pic (info))
4176 {
4177 bfd_put_32 (output_bfd,
4178 (htab->elf.sgotplt->output_section->vma
4179 + htab->elf.sgotplt->output_offset
4180 + 4),
4181 htab->elf.splt->contents
4182 + htab->lazy_plt->plt0_got1_offset);
4183 bfd_put_32 (output_bfd,
4184 (htab->elf.sgotplt->output_section->vma
4185 + htab->elf.sgotplt->output_offset
4186 + 8),
4187 htab->elf.splt->contents
4188 + htab->lazy_plt->plt0_got2_offset);
4189
4190 if (htab->is_vxworks)
4191 {
4192 Elf_Internal_Rela rel;
4193 int num_plts = (htab->elf.splt->size
4194 / htab->plt.plt_entry_size) - 1;
4195 unsigned char *p;
4196 asection *srelplt2 = htab->srelplt2;
4197
4198 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4199 + 4. On IA32 we use REL relocations so the
4200 addend goes in the PLT directly. */
4201 rel.r_offset = (htab->elf.splt->output_section->vma
4202 + htab->elf.splt->output_offset
4203 + htab->lazy_plt->plt0_got1_offset);
4204 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4205 R_386_32);
4206 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4207 srelplt2->contents);
4208 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_
4209 + 8. */
4210 rel.r_offset = (htab->elf.splt->output_section->vma
4211 + htab->elf.splt->output_offset
4212 + htab->lazy_plt->plt0_got2_offset);
4213 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4214 R_386_32);
4215 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4216 srelplt2->contents +
4217 sizeof (Elf32_External_Rel));
4218 /* Correct the .rel.plt.unloaded relocations. */
4219 p = srelplt2->contents;
4220 if (bfd_link_pic (info))
4221 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4222 else
4223 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4224
4225 for (; num_plts; num_plts--)
4226 {
4227 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4228 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
4229 R_386_32);
4230 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4231 p += sizeof (Elf32_External_Rel);
4232
4233 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4234 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
4235 R_386_32);
4236 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4237 p += sizeof (Elf32_External_Rel);
4238 }
4239 }
4240 }
4241 }
4242 }
4243
4244 if (htab->plt_got != NULL && htab->plt_got->size > 0)
4245 elf_section_data (htab->plt_got->output_section)
4246 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4247
4248 if (htab->plt_second != NULL && htab->plt_second->size > 0)
4249 elf_section_data (htab->plt_second->output_section)
4250 ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
4251 }
4252
4253 /* Fill in the first three entries in the global offset table. */
4254 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
4255 {
4256 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4257 {
4258 _bfd_error_handler
4259 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4260 return FALSE;
4261 }
4262
4263 bfd_put_32 (output_bfd,
4264 (sdyn == NULL ? 0
4265 : sdyn->output_section->vma + sdyn->output_offset),
4266 htab->elf.sgotplt->contents);
4267 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4);
4268 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8);
4269
4270 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4;
4271 }
4272
4273 /* Adjust .eh_frame for .plt section. */
4274 if (htab->plt_eh_frame != NULL
4275 && htab->plt_eh_frame->contents != NULL)
4276 {
4277 if (htab->elf.splt != NULL
4278 && htab->elf.splt->size != 0
4279 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4280 && htab->elf.splt->output_section != NULL
4281 && htab->plt_eh_frame->output_section != NULL)
4282 {
4283 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4284 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4285 + htab->plt_eh_frame->output_offset
4286 + PLT_FDE_START_OFFSET;
4287 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4288 htab->plt_eh_frame->contents
4289 + PLT_FDE_START_OFFSET);
4290 }
4291 if (htab->plt_eh_frame->sec_info_type
4292 == SEC_INFO_TYPE_EH_FRAME)
4293 {
4294 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4295 htab->plt_eh_frame,
4296 htab->plt_eh_frame->contents))
4297 return FALSE;
4298 }
4299 }
4300
4301 /* Adjust .eh_frame for .plt.got section. */
4302 if (htab->plt_got_eh_frame != NULL
4303 && htab->plt_got_eh_frame->contents != NULL)
4304 {
4305 if (htab->plt_got != NULL
4306 && htab->plt_got->size != 0
4307 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
4308 && htab->plt_got->output_section != NULL
4309 && htab->plt_got_eh_frame->output_section != NULL)
4310 {
4311 bfd_vma plt_start = htab->plt_got->output_section->vma;
4312 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
4313 + htab->plt_got_eh_frame->output_offset
4314 + PLT_FDE_START_OFFSET;
4315 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4316 htab->plt_got_eh_frame->contents
4317 + PLT_FDE_START_OFFSET);
4318 }
4319 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4320 {
4321 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4322 htab->plt_got_eh_frame,
4323 htab->plt_got_eh_frame->contents))
4324 return FALSE;
4325 }
4326 }
4327
4328 /* Adjust .eh_frame for the second PLT section. */
4329 if (htab->plt_second_eh_frame != NULL
4330 && htab->plt_second_eh_frame->contents != NULL)
4331 {
4332 if (htab->plt_second != NULL
4333 && htab->plt_second->size != 0
4334 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
4335 && htab->plt_second->output_section != NULL
4336 && htab->plt_second_eh_frame->output_section != NULL)
4337 {
4338 bfd_vma plt_start = htab->plt_second->output_section->vma;
4339 bfd_vma eh_frame_start
4340 = (htab->plt_second_eh_frame->output_section->vma
4341 + htab->plt_second_eh_frame->output_offset
4342 + PLT_FDE_START_OFFSET);
4343 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
4344 htab->plt_second_eh_frame->contents
4345 + PLT_FDE_START_OFFSET);
4346 }
4347 if (htab->plt_second_eh_frame->sec_info_type
4348 == SEC_INFO_TYPE_EH_FRAME)
4349 {
4350 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
4351 htab->plt_second_eh_frame,
4352 htab->plt_second_eh_frame->contents))
4353 return FALSE;
4354 }
4355 }
4356
4357 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4358 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
4359
4360 /* Fill PLT entries for undefined weak symbols in PIE. */
4361 if (bfd_link_pie (info))
4362 bfd_hash_traverse (&info->hash->table,
4363 elf_i386_pie_finish_undefweak_symbol,
4364 info);
4365
4366 return TRUE;
4367 }
4368
4369 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4370 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4371 It has to be done before elf_link_sort_relocs is called so that
4372 dynamic relocations are properly sorted. */
4373
4374 static bfd_boolean
4375 elf_i386_output_arch_local_syms
4376 (bfd *output_bfd ATTRIBUTE_UNUSED,
4377 struct bfd_link_info *info,
4378 void *flaginfo ATTRIBUTE_UNUSED,
4379 int (*func) (void *, const char *,
4380 Elf_Internal_Sym *,
4381 asection *,
4382 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4383 {
4384 struct elf_x86_link_hash_table *htab
4385 = elf_x86_hash_table (info, I386_ELF_DATA);
4386 if (htab == NULL)
4387 return FALSE;
4388
4389 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4390 htab_traverse (htab->loc_hash_table,
4391 elf_i386_finish_local_dynamic_symbol,
4392 info);
4393
4394 return TRUE;
4395 }
4396
4397 /* Forward declaration. */
4398 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt;
4399
4400 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4401 dynamic relocations. */
4402
4403 static long
4404 elf_i386_get_synthetic_symtab (bfd *abfd,
4405 long symcount ATTRIBUTE_UNUSED,
4406 asymbol **syms ATTRIBUTE_UNUSED,
4407 long dynsymcount,
4408 asymbol **dynsyms,
4409 asymbol **ret)
4410 {
4411 long count, i, n;
4412 int j;
4413 bfd_byte *plt_contents;
4414 long relsize;
4415 const struct elf_x86_lazy_plt_layout *lazy_plt;
4416 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4417 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4418 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4419 asection *plt;
4420 bfd_vma got_addr;
4421 enum elf_x86_plt_type plt_type;
4422 struct elf_x86_plt plts[] =
4423 {
4424 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4425 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4426 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4427 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4428 };
4429
4430 *ret = NULL;
4431
4432 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4433 return 0;
4434
4435 if (dynsymcount <= 0)
4436 return 0;
4437
4438 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4439 if (relsize <= 0)
4440 return -1;
4441
4442 non_lazy_plt = NULL;
4443 /* Silence GCC 6. */
4444 lazy_plt = NULL;
4445 non_lazy_ibt_plt = NULL;
4446 lazy_ibt_plt = NULL;
4447 switch (get_elf_i386_backend_data (abfd)->os)
4448 {
4449 case is_normal:
4450 non_lazy_plt = &elf_i386_non_lazy_plt;
4451 lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4452 non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4453 /* Fall through */
4454 case is_vxworks:
4455 lazy_plt = &elf_i386_lazy_plt;
4456 break;
4457 case is_nacl:
4458 lazy_plt = &elf_i386_nacl_plt;
4459 break;
4460 }
4461
4462 got_addr = 0;
4463
4464 count = 0;
4465 for (j = 0; plts[j].name != NULL; j++)
4466 {
4467 plt = bfd_get_section_by_name (abfd, plts[j].name);
4468 if (plt == NULL || plt->size == 0)
4469 continue;
4470
4471 /* Get the PLT section contents. */
4472 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
4473 if (plt_contents == NULL)
4474 break;
4475 if (!bfd_get_section_contents (abfd, (asection *) plt,
4476 plt_contents, 0, plt->size))
4477 {
4478 free (plt_contents);
4479 break;
4480 }
4481
4482 /* Check what kind of PLT it is. */
4483 plt_type = plt_unknown;
4484 if (plts[j].type == plt_unknown
4485 && (plt->size >= (lazy_plt->plt0_entry_size
4486 + lazy_plt->plt_entry_size)))
4487 {
4488 /* Match lazy PLT first. */
4489 if (memcmp (plt_contents, lazy_plt->plt0_entry,
4490 lazy_plt->plt0_got1_offset) == 0)
4491 {
4492 /* The fist entry in the lazy IBT PLT is the same as the
4493 normal lazy PLT. */
4494 if (lazy_ibt_plt != NULL
4495 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4496 lazy_ibt_plt->plt_entry,
4497 lazy_ibt_plt->plt_got_offset) == 0))
4498 plt_type = plt_lazy | plt_second;
4499 else
4500 plt_type = plt_lazy;
4501 }
4502 else if (memcmp (plt_contents, lazy_plt->pic_plt0_entry,
4503 lazy_plt->plt0_got1_offset) == 0)
4504 {
4505 /* The fist entry in the PIC lazy IBT PLT is the same as
4506 the normal PIC lazy PLT. */
4507 if (lazy_ibt_plt != NULL
4508 && (memcmp (plt_contents + lazy_ibt_plt->plt0_entry_size,
4509 lazy_ibt_plt->pic_plt_entry,
4510 lazy_ibt_plt->plt_got_offset) == 0))
4511 plt_type = plt_lazy | plt_pic | plt_second;
4512 else
4513 plt_type = plt_lazy | plt_pic;
4514 }
4515 }
4516
4517 if (non_lazy_plt != NULL
4518 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4519 && plt->size >= non_lazy_plt->plt_entry_size)
4520 {
4521 /* Match non-lazy PLT. */
4522 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4523 non_lazy_plt->plt_got_offset) == 0)
4524 plt_type = plt_non_lazy;
4525 else if (memcmp (plt_contents, non_lazy_plt->pic_plt_entry,
4526 non_lazy_plt->plt_got_offset) == 0)
4527 plt_type = plt_pic;
4528 }
4529
4530 if ((non_lazy_ibt_plt != NULL)
4531 && (plt_type == plt_unknown || plt_type == plt_second)
4532 && plt->size >= non_lazy_ibt_plt->plt_entry_size)
4533 {
4534 if (memcmp (plt_contents,
4535 non_lazy_ibt_plt->plt_entry,
4536 non_lazy_ibt_plt->plt_got_offset) == 0)
4537 {
4538 /* Match IBT PLT. */
4539 plt_type = plt_second;
4540 non_lazy_plt = non_lazy_ibt_plt;
4541 }
4542 else if (memcmp (plt_contents,
4543 non_lazy_ibt_plt->pic_plt_entry,
4544 non_lazy_ibt_plt->plt_got_offset) == 0)
4545 {
4546 /* Match PIC IBT PLT. */
4547 plt_type = plt_second | plt_pic;
4548 non_lazy_plt = non_lazy_ibt_plt;
4549 }
4550 }
4551
4552 if (plt_type == plt_unknown)
4553 {
4554 free (plt_contents);
4555 continue;
4556 }
4557
4558 plts[j].sec = plt;
4559 plts[j].type = plt_type;
4560
4561 if ((plt_type & plt_lazy))
4562 {
4563 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4564 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4565 /* Skip PLT0 in lazy PLT. */
4566 i = 1;
4567 }
4568 else
4569 {
4570 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4571 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4572 i = 0;
4573 }
4574
4575 /* Skip lazy PLT when the second PLT is used. */
4576 if ((plt_type & (plt_lazy | plt_second))
4577 == (plt_lazy | plt_second))
4578 plts[j].count = 0;
4579 else
4580 {
4581 n = plt->size / plts[j].plt_entry_size;
4582 plts[j].count = n;
4583 count += n - i;
4584 }
4585
4586 plts[j].contents = plt_contents;
4587
4588 /* The _GLOBAL_OFFSET_TABLE_ address is needed. */
4589 if ((plt_type & plt_pic))
4590 got_addr = (bfd_vma) -1;
4591 }
4592
4593 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4594 got_addr, plts, dynsyms,
4595 ret);
4596 }
4597
4598 /* Set up i386 GNU properties. Return the first relocatable ELF input
4599 with GNU properties if found. Otherwise, return NULL. */
4600
4601 static bfd *
4602 elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
4603 {
4604 struct elf_x86_init_table init_table;
4605
4606 init_table.normal_target = FALSE;
4607 init_table.is_vxworks = FALSE;
4608 switch (get_elf_i386_backend_data (info->output_bfd)->os)
4609 {
4610 case is_normal:
4611 init_table.lazy_plt = &elf_i386_lazy_plt;
4612 init_table.non_lazy_plt = &elf_i386_non_lazy_plt;
4613 init_table.lazy_ibt_plt = &elf_i386_lazy_ibt_plt;
4614 init_table.non_lazy_ibt_plt = &elf_i386_non_lazy_ibt_plt;
4615 init_table.normal_target = TRUE;
4616 break;
4617 case is_vxworks:
4618 init_table.lazy_plt = &elf_i386_lazy_plt;
4619 init_table.non_lazy_plt = NULL;
4620 init_table.lazy_ibt_plt = NULL;
4621 init_table.non_lazy_ibt_plt = NULL;
4622 init_table.is_vxworks = TRUE;
4623 break;
4624 case is_nacl:
4625 init_table.lazy_plt = &elf_i386_nacl_plt;
4626 init_table.non_lazy_plt = NULL;
4627 init_table.lazy_ibt_plt = NULL;
4628 init_table.non_lazy_ibt_plt = NULL;
4629 break;
4630 }
4631
4632 init_table.r_info = elf32_r_info;
4633 init_table.r_sym = elf32_r_sym;
4634
4635 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
4636 }
4637
4638 #define TARGET_LITTLE_SYM i386_elf32_vec
4639 #define TARGET_LITTLE_NAME "elf32-i386"
4640 #define ELF_ARCH bfd_arch_i386
4641 #define ELF_TARGET_ID I386_ELF_DATA
4642 #define ELF_MACHINE_CODE EM_386
4643 #define ELF_MAXPAGESIZE 0x1000
4644
4645 #define elf_backend_can_gc_sections 1
4646 #define elf_backend_can_refcount 1
4647 #define elf_backend_want_got_plt 1
4648 #define elf_backend_plt_readonly 1
4649 #define elf_backend_want_plt_sym 0
4650 #define elf_backend_got_header_size 12
4651 #define elf_backend_plt_alignment 4
4652 #define elf_backend_dtrel_excludes_plt 1
4653 #define elf_backend_extern_protected_data 1
4654 #define elf_backend_caches_rawsize 1
4655 #define elf_backend_want_dynrelro 1
4656
4657 /* Support RELA for objdump of prelink objects. */
4658 #define elf_info_to_howto elf_i386_info_to_howto_rel
4659 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4660
4661 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4662 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4663 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4664 #define bfd_elf32_get_synthetic_symtab elf_i386_get_synthetic_symtab
4665
4666 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4667 #define elf_backend_check_relocs elf_i386_check_relocs
4668 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
4669 #define elf_backend_fake_sections elf_i386_fake_sections
4670 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4671 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4672 #define elf_backend_output_arch_local_syms elf_i386_output_arch_local_syms
4673 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4674 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4675 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4676 #define elf_backend_relocate_section elf_i386_relocate_section
4677 #define elf_backend_setup_gnu_properties elf_i386_link_setup_gnu_properties
4678
4679 #include "elf32-target.h"
4680
4681 /* FreeBSD support. */
4682
4683 #undef TARGET_LITTLE_SYM
4684 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec
4685 #undef TARGET_LITTLE_NAME
4686 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4687 #undef ELF_OSABI
4688 #define ELF_OSABI ELFOSABI_FREEBSD
4689
4690 /* The kernel recognizes executables as valid only if they carry a
4691 "FreeBSD" label in the ELF header. So we put this label on all
4692 executables and (for simplicity) also all other object files. */
4693
4694 static void
4695 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4696 {
4697 _bfd_elf_post_process_headers (abfd, info);
4698
4699 #ifdef OLD_FREEBSD_ABI_LABEL
4700 {
4701 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4702 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
4703 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4704 }
4705 #endif
4706 }
4707
4708 #undef elf_backend_post_process_headers
4709 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
4710 #undef elf32_bed
4711 #define elf32_bed elf32_i386_fbsd_bed
4712
4713 #undef elf_backend_add_symbol_hook
4714
4715 #include "elf32-target.h"
4716
4717 /* Solaris 2. */
4718
4719 #undef TARGET_LITTLE_SYM
4720 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec
4721 #undef TARGET_LITTLE_NAME
4722 #define TARGET_LITTLE_NAME "elf32-i386-sol2"
4723
4724 #undef elf_backend_post_process_headers
4725
4726 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4727 objects won't be recognized. */
4728 #undef ELF_OSABI
4729
4730 #undef elf32_bed
4731 #define elf32_bed elf32_i386_sol2_bed
4732
4733 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
4734 boundary. */
4735 #undef elf_backend_static_tls_alignment
4736 #define elf_backend_static_tls_alignment 8
4737
4738 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4739
4740 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4741 File, p.63. */
4742 #undef elf_backend_want_plt_sym
4743 #define elf_backend_want_plt_sym 1
4744
4745 #undef elf_backend_strtab_flags
4746 #define elf_backend_strtab_flags SHF_STRINGS
4747
4748 /* Called to set the sh_flags, sh_link and sh_info fields of OSECTION which
4749 has a type >= SHT_LOOS. Returns TRUE if these fields were initialised
4750 FALSE otherwise. ISECTION is the best guess matching section from the
4751 input bfd IBFD, but it might be NULL. */
4752
4753 static bfd_boolean
4754 elf32_i386_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
4755 bfd *obfd ATTRIBUTE_UNUSED,
4756 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
4757 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
4758 {
4759 /* PR 19938: FIXME: Need to add code for setting the sh_info
4760 and sh_link fields of Solaris specific section types. */
4761 return FALSE;
4762
4763 /* Based upon Oracle Solaris 11.3 Linkers and Libraries Guide, Ch. 13,
4764 Object File Format, Table 13-9 ELF sh_link and sh_info Interpretation:
4765
4766 http://docs.oracle.com/cd/E53394_01/html/E54813/chapter6-94076.html#scrolltoc
4767
4768 The following values should be set:
4769
4770 Type Link Info
4771 -----------------------------------------------------------------------------
4772 SHT_SUNW_ancillary The section header index of 0
4773 [0x6fffffee] the associated string table.
4774
4775 SHT_SUNW_capinfo The section header index of For a dynamic object, the
4776 [0x6ffffff0] the associated symbol table. section header index of
4777 the associated
4778 SHT_SUNW_capchain table,
4779 otherwise 0.
4780
4781 SHT_SUNW_symsort The section header index of 0
4782 [0x6ffffff1] the associated symbol table.
4783
4784 SHT_SUNW_tlssort The section header index of 0
4785 [0x6ffffff2] the associated symbol table.
4786
4787 SHT_SUNW_LDYNSYM The section header index of One greater than the
4788 [0x6ffffff3] the associated string table. symbol table index of the
4789 This index is the same string last local symbol,
4790 table used by the SHT_DYNSYM STB_LOCAL. Since
4791 section. SHT_SUNW_LDYNSYM only
4792 contains local symbols,
4793 sh_info is equivalent to
4794 the number of symbols in
4795 the table.
4796
4797 SHT_SUNW_cap If symbol capabilities exist, If any capabilities refer
4798 [0x6ffffff5] the section header index of to named strings, the
4799 the associated section header index of
4800 SHT_SUNW_capinfo table, the associated string
4801 otherwise 0. table, otherwise 0.
4802
4803 SHT_SUNW_move The section header index of 0
4804 [0x6ffffffa] the associated symbol table.
4805
4806 SHT_SUNW_COMDAT 0 0
4807 [0x6ffffffb]
4808
4809 SHT_SUNW_syminfo The section header index of The section header index
4810 [0x6ffffffc] the associated symbol table. of the associated
4811 .dynamic section.
4812
4813 SHT_SUNW_verdef The section header index of The number of version
4814 [0x6ffffffd] the associated string table. definitions within the
4815 section.
4816
4817 SHT_SUNW_verneed The section header index of The number of version
4818 [0x6ffffffe] the associated string table. dependencies within the
4819 section.
4820
4821 SHT_SUNW_versym The section header index of 0
4822 [0x6fffffff] the associated symbol table. */
4823 }
4824
4825 #undef elf_backend_copy_special_section_fields
4826 #define elf_backend_copy_special_section_fields elf32_i386_copy_solaris_special_section_fields
4827
4828 #include "elf32-target.h"
4829
4830 /* Intel MCU support. */
4831
4832 static bfd_boolean
4833 elf32_iamcu_elf_object_p (bfd *abfd)
4834 {
4835 /* Set the right machine number for an IAMCU elf32 file. */
4836 bfd_default_set_arch_mach (abfd, bfd_arch_iamcu, bfd_mach_i386_iamcu);
4837 return TRUE;
4838 }
4839
4840 #undef TARGET_LITTLE_SYM
4841 #define TARGET_LITTLE_SYM iamcu_elf32_vec
4842 #undef TARGET_LITTLE_NAME
4843 #define TARGET_LITTLE_NAME "elf32-iamcu"
4844 #undef ELF_ARCH
4845 #define ELF_ARCH bfd_arch_iamcu
4846
4847 #undef ELF_MACHINE_CODE
4848 #define ELF_MACHINE_CODE EM_IAMCU
4849
4850 #undef ELF_OSABI
4851
4852 #undef elf32_bed
4853 #define elf32_bed elf32_iamcu_bed
4854
4855 #undef elf_backend_object_p
4856 #define elf_backend_object_p elf32_iamcu_elf_object_p
4857
4858 #undef elf_backend_static_tls_alignment
4859
4860 #undef elf_backend_want_plt_sym
4861 #define elf_backend_want_plt_sym 0
4862
4863 #undef elf_backend_strtab_flags
4864 #undef elf_backend_copy_special_section_fields
4865
4866 #include "elf32-target.h"
4867
4868 /* Restore defaults. */
4869 #undef ELF_ARCH
4870 #define ELF_ARCH bfd_arch_i386
4871 #undef ELF_MACHINE_CODE
4872 #define ELF_MACHINE_CODE EM_386
4873
4874 /* Native Client support. */
4875
4876 #undef TARGET_LITTLE_SYM
4877 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec
4878 #undef TARGET_LITTLE_NAME
4879 #define TARGET_LITTLE_NAME "elf32-i386-nacl"
4880 #undef elf32_bed
4881 #define elf32_bed elf32_i386_nacl_bed
4882
4883 #undef ELF_MAXPAGESIZE
4884 #define ELF_MAXPAGESIZE 0x10000
4885
4886 /* Restore defaults. */
4887 #undef ELF_OSABI
4888 #undef elf_backend_want_plt_sym
4889 #define elf_backend_want_plt_sym 0
4890 #undef elf_backend_post_process_headers
4891 #undef elf_backend_static_tls_alignment
4892
4893 /* NaCl uses substantially different PLT entries for the same effects. */
4894
4895 #undef elf_backend_plt_alignment
4896 #define elf_backend_plt_alignment 5
4897 #define NACL_PLT_ENTRY_SIZE 64
4898 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
4899
4900 static const bfd_byte elf_i386_nacl_plt0_entry[] =
4901 {
4902 0xff, 0x35, /* pushl contents of address */
4903 0, 0, 0, 0, /* replaced with address of .got + 4. */
4904 0x8b, 0x0d, /* movl contents of address, %ecx */
4905 0, 0, 0, 0, /* replaced with address of .got + 8. */
4906 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
4907 0xff, 0xe1 /* jmp *%ecx */
4908 };
4909
4910 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
4911 {
4912 0x8b, 0x0d, /* movl contents of address, %ecx */
4913 0, 0, 0, 0, /* replaced with GOT slot address. */
4914 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */
4915 0xff, 0xe1, /* jmp *%ecx */
4916
4917 /* Pad to the next 32-byte boundary with nop instructions. */
4918 0x90,
4919 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4920 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4921
4922 /* Lazy GOT entries point here (32-byte aligned). */
4923 0x68, /* pushl immediate */
4924 0, 0, 0, 0, /* replaced with reloc offset. */
4925 0xe9, /* jmp relative */
4926 0, 0, 0, 0, /* replaced with offset to .plt. */
4927
4928 /* Pad to the next 32-byte boundary with nop instructions. */
4929 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4930 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4931 0x90, 0x90
4932 };
4933
4934 static const bfd_byte
4935 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] =
4936 {
4937 0xff, 0x73, 0x04, /* pushl 4(%ebx) */
4938 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */
4939 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */
4940 0xff, 0xe1, /* jmp *%ecx */
4941
4942 /* This is expected to be the same size as elf_i386_nacl_plt0_entry,
4943 so pad to that size with nop instructions. */
4944 0x90, 0x90, 0x90, 0x90, 0x90, 0x90
4945 };
4946
4947 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] =
4948 {
4949 0x8b, 0x8b, /* movl offset(%ebx), %ecx */
4950 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
4951 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */
4952 0xff, 0xe1, /* jmp *%ecx */
4953
4954 /* Pad to the next 32-byte boundary with nop instructions. */
4955 0x90,
4956 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4957 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4958
4959 /* Lazy GOT entries point here (32-byte aligned). */
4960 0x68, /* pushl immediate */
4961 0, 0, 0, 0, /* replaced with offset into relocation table. */
4962 0xe9, /* jmp relative */
4963 0, 0, 0, 0, /* replaced with offset to start of .plt. */
4964
4965 /* Pad to the next 32-byte boundary with nop instructions. */
4966 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4967 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
4968 0x90, 0x90
4969 };
4970
4971 static const bfd_byte elf_i386_nacl_eh_frame_plt[] =
4972 {
4973 #if (PLT_CIE_LENGTH != 20 \
4974 || PLT_FDE_LENGTH != 36 \
4975 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
4976 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
4977 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!"
4978 #endif
4979 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
4980 0, 0, 0, 0, /* CIE ID */
4981 1, /* CIE version */
4982 'z', 'R', 0, /* Augmentation string */
4983 1, /* Code alignment factor */
4984 0x7c, /* Data alignment factor: -4 */
4985 8, /* Return address column */
4986 1, /* Augmentation size */
4987 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
4988 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */
4989 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */
4990 DW_CFA_nop, DW_CFA_nop,
4991
4992 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
4993 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
4994 0, 0, 0, 0, /* R_386_PC32 .plt goes here */
4995 0, 0, 0, 0, /* .plt size goes here */
4996 0, /* Augmentation size */
4997 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */
4998 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
4999 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */
5000 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5001 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5002 13, /* Block length */
5003 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */
5004 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */
5005 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5006 DW_OP_lit2, DW_OP_shl, DW_OP_plus,
5007 DW_CFA_nop, DW_CFA_nop
5008 };
5009
5010 static const struct elf_x86_lazy_plt_layout elf_i386_nacl_plt =
5011 {
5012 elf_i386_nacl_plt0_entry, /* plt0_entry */
5013 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */
5014 elf_i386_nacl_plt_entry, /* plt_entry */
5015 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5016 2, /* plt0_got1_offset */
5017 8, /* plt0_got2_offset */
5018 0, /* plt0_got2_insn_end */
5019 2, /* plt_got_offset */
5020 33, /* plt_reloc_offset */
5021 38, /* plt_plt_offset */
5022 0, /* plt_got_insn_size */
5023 0, /* plt_plt_insn_end */
5024 32, /* plt_lazy_offset */
5025 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */
5026 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */
5027 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */
5028 sizeof (elf_i386_nacl_eh_frame_plt) /* eh_frame_plt_size */
5029 };
5030
5031 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed =
5032 {
5033 0x90, /* plt0_pad_byte: nop insn */
5034 is_nacl /* os */
5035 };
5036
5037 static bfd_boolean
5038 elf32_i386_nacl_elf_object_p (bfd *abfd)
5039 {
5040 /* Set the right machine number for a NaCl i386 ELF32 file. */
5041 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl);
5042 return TRUE;
5043 }
5044
5045 #undef elf_backend_arch_data
5046 #define elf_backend_arch_data &elf_i386_nacl_arch_bed
5047
5048 #undef elf_backend_object_p
5049 #define elf_backend_object_p elf32_i386_nacl_elf_object_p
5050 #undef elf_backend_modify_segment_map
5051 #define elf_backend_modify_segment_map nacl_modify_segment_map
5052 #undef elf_backend_modify_program_headers
5053 #define elf_backend_modify_program_headers nacl_modify_program_headers
5054 #undef elf_backend_final_write_processing
5055 #define elf_backend_final_write_processing nacl_final_write_processing
5056
5057 #include "elf32-target.h"
5058
5059 /* Restore defaults. */
5060 #undef elf_backend_object_p
5061 #undef elf_backend_modify_segment_map
5062 #undef elf_backend_modify_program_headers
5063 #undef elf_backend_final_write_processing
5064
5065 /* VxWorks support. */
5066
5067 #undef TARGET_LITTLE_SYM
5068 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec
5069 #undef TARGET_LITTLE_NAME
5070 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
5071 #undef ELF_OSABI
5072 #undef ELF_MAXPAGESIZE
5073 #define ELF_MAXPAGESIZE 0x1000
5074 #undef elf_backend_plt_alignment
5075 #define elf_backend_plt_alignment 4
5076
5077 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed =
5078 {
5079 0x90, /* plt0_pad_byte */
5080 is_vxworks /* os */
5081 };
5082
5083 #undef elf_backend_arch_data
5084 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed
5085
5086 #undef elf_backend_relocs_compatible
5087 #undef elf_backend_add_symbol_hook
5088 #define elf_backend_add_symbol_hook \
5089 elf_vxworks_add_symbol_hook
5090 #undef elf_backend_link_output_symbol_hook
5091 #define elf_backend_link_output_symbol_hook \
5092 elf_vxworks_link_output_symbol_hook
5093 #undef elf_backend_emit_relocs
5094 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
5095 #undef elf_backend_final_write_processing
5096 #define elf_backend_final_write_processing \
5097 elf_vxworks_final_write_processing
5098 #undef elf_backend_static_tls_alignment
5099
5100 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
5101 define it. */
5102 #undef elf_backend_want_plt_sym
5103 #define elf_backend_want_plt_sym 1
5104
5105 #undef elf32_bed
5106 #define elf32_bed elf32_i386_vxworks_bed
5107
5108 #include "elf32-target.h"
This page took 0.138658 seconds and 5 git commands to generate.