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