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