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