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