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