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