2009-06-03 H.J. Lu <hongjiu.lu@intel.com>
[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 h->got.refcount += 1;
1340 if (htab->sgot == NULL
1341 && !elf_i386_create_got_section (htab->elf.dynobj,
1342 info))
1343 return FALSE;
1344 break;
1345 }
1346
1347 continue;
1348 }
1349 }
1350
1351 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1352 symtab_hdr, sym_hashes,
1353 &r_type, GOT_UNKNOWN,
1354 rel, rel_end, h))
1355 return FALSE;
1356
1357 switch (r_type)
1358 {
1359 case R_386_TLS_LDM:
1360 htab->tls_ldm_got.refcount += 1;
1361 goto create_got;
1362
1363 case R_386_PLT32:
1364 /* This symbol requires a procedure linkage table entry. We
1365 actually build the entry in adjust_dynamic_symbol,
1366 because this might be a case of linking PIC code which is
1367 never referenced by a dynamic object, in which case we
1368 don't need to generate a procedure linkage table entry
1369 after all. */
1370
1371 /* If this is a local symbol, we resolve it directly without
1372 creating a procedure linkage table entry. */
1373 if (h == NULL)
1374 continue;
1375
1376 h->needs_plt = 1;
1377 h->plt.refcount += 1;
1378 break;
1379
1380 case R_386_TLS_IE_32:
1381 case R_386_TLS_IE:
1382 case R_386_TLS_GOTIE:
1383 if (info->shared)
1384 info->flags |= DF_STATIC_TLS;
1385 /* Fall through */
1386
1387 case R_386_GOT32:
1388 case R_386_TLS_GD:
1389 case R_386_TLS_GOTDESC:
1390 case R_386_TLS_DESC_CALL:
1391 /* This symbol requires a global offset table entry. */
1392 {
1393 int tls_type, old_tls_type;
1394
1395 switch (r_type)
1396 {
1397 default:
1398 case R_386_GOT32: tls_type = GOT_NORMAL; break;
1399 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
1400 case R_386_TLS_GOTDESC:
1401 case R_386_TLS_DESC_CALL:
1402 tls_type = GOT_TLS_GDESC; break;
1403 case R_386_TLS_IE_32:
1404 if (ELF32_R_TYPE (rel->r_info) == r_type)
1405 tls_type = GOT_TLS_IE_NEG;
1406 else
1407 /* If this is a GD->IE transition, we may use either of
1408 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
1409 tls_type = GOT_TLS_IE;
1410 break;
1411 case R_386_TLS_IE:
1412 case R_386_TLS_GOTIE:
1413 tls_type = GOT_TLS_IE_POS; break;
1414 }
1415
1416 if (h != NULL)
1417 {
1418 h->got.refcount += 1;
1419 old_tls_type = elf_i386_hash_entry(h)->tls_type;
1420 }
1421 else
1422 {
1423 bfd_signed_vma *local_got_refcounts;
1424
1425 /* This is a global offset table entry for a local symbol. */
1426 local_got_refcounts = elf_local_got_refcounts (abfd);
1427 if (local_got_refcounts == NULL)
1428 {
1429 bfd_size_type size;
1430
1431 size = symtab_hdr->sh_info;
1432 size *= (sizeof (bfd_signed_vma)
1433 + sizeof (bfd_vma) + sizeof(char));
1434 local_got_refcounts = bfd_zalloc (abfd, size);
1435 if (local_got_refcounts == NULL)
1436 return FALSE;
1437 elf_local_got_refcounts (abfd) = local_got_refcounts;
1438 elf_i386_local_tlsdesc_gotent (abfd)
1439 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1440 elf_i386_local_got_tls_type (abfd)
1441 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1442 }
1443 local_got_refcounts[r_symndx] += 1;
1444 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
1445 }
1446
1447 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
1448 tls_type |= old_tls_type;
1449 /* If a TLS symbol is accessed using IE at least once,
1450 there is no point to use dynamic model for it. */
1451 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1452 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1453 || (tls_type & GOT_TLS_IE) == 0))
1454 {
1455 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type))
1456 tls_type = old_tls_type;
1457 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1458 && GOT_TLS_GD_ANY_P (tls_type))
1459 tls_type |= old_tls_type;
1460 else
1461 {
1462 (*_bfd_error_handler)
1463 (_("%B: `%s' accessed both as normal and "
1464 "thread local symbol"),
1465 abfd,
1466 h ? h->root.root.string : "<local>");
1467 return FALSE;
1468 }
1469 }
1470
1471 if (old_tls_type != tls_type)
1472 {
1473 if (h != NULL)
1474 elf_i386_hash_entry (h)->tls_type = tls_type;
1475 else
1476 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
1477 }
1478 }
1479 /* Fall through */
1480
1481 case R_386_GOTOFF:
1482 case R_386_GOTPC:
1483 create_got:
1484 if (htab->sgot == NULL)
1485 {
1486 if (htab->elf.dynobj == NULL)
1487 htab->elf.dynobj = abfd;
1488 if (!elf_i386_create_got_section (htab->elf.dynobj, info))
1489 return FALSE;
1490 }
1491 if (r_type != R_386_TLS_IE)
1492 break;
1493 /* Fall through */
1494
1495 case R_386_TLS_LE_32:
1496 case R_386_TLS_LE:
1497 if (!info->shared)
1498 break;
1499 info->flags |= DF_STATIC_TLS;
1500 /* Fall through */
1501
1502 case R_386_32:
1503 case R_386_PC32:
1504 if (h != NULL && !info->shared)
1505 {
1506 /* If this reloc is in a read-only section, we might
1507 need a copy reloc. We can't check reliably at this
1508 stage whether the section is read-only, as input
1509 sections have not yet been mapped to output sections.
1510 Tentatively set the flag for now, and correct in
1511 adjust_dynamic_symbol. */
1512 h->non_got_ref = 1;
1513
1514 /* We may need a .plt entry if the function this reloc
1515 refers to is in a shared lib. */
1516 h->plt.refcount += 1;
1517 if (r_type != R_386_PC32)
1518 h->pointer_equality_needed = 1;
1519 }
1520
1521 /* If we are creating a shared library, and this is a reloc
1522 against a global symbol, or a non PC relative reloc
1523 against a local symbol, then we need to copy the reloc
1524 into the shared library. However, if we are linking with
1525 -Bsymbolic, we do not need to copy a reloc against a
1526 global symbol which is defined in an object we are
1527 including in the link (i.e., DEF_REGULAR is set). At
1528 this point we have not seen all the input files, so it is
1529 possible that DEF_REGULAR is not set now but will be set
1530 later (it is never cleared). In case of a weak definition,
1531 DEF_REGULAR may be cleared later by a strong definition in
1532 a shared library. We account for that possibility below by
1533 storing information in the relocs_copied field of the hash
1534 table entry. A similar situation occurs when creating
1535 shared libraries and symbol visibility changes render the
1536 symbol local.
1537
1538 If on the other hand, we are creating an executable, we
1539 may need to keep relocations for symbols satisfied by a
1540 dynamic library if we manage to avoid copy relocs for the
1541 symbol. */
1542 if ((info->shared
1543 && (sec->flags & SEC_ALLOC) != 0
1544 && (r_type != R_386_PC32
1545 || (h != NULL
1546 && (! SYMBOLIC_BIND (info, h)
1547 || h->root.type == bfd_link_hash_defweak
1548 || !h->def_regular))))
1549 || (ELIMINATE_COPY_RELOCS
1550 && !info->shared
1551 && (sec->flags & SEC_ALLOC) != 0
1552 && h != NULL
1553 && (h->root.type == bfd_link_hash_defweak
1554 || !h->def_regular)))
1555 {
1556 struct elf_i386_dyn_relocs *p;
1557 struct elf_i386_dyn_relocs **head;
1558
1559 /* We must copy these reloc types into the output file.
1560 Create a reloc section in dynobj and make room for
1561 this reloc. */
1562 if (sreloc == NULL)
1563 {
1564 if (htab->elf.dynobj == NULL)
1565 htab->elf.dynobj = abfd;
1566
1567 sreloc = _bfd_elf_make_dynamic_reloc_section
1568 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE);
1569
1570 if (sreloc == NULL)
1571 return FALSE;
1572 }
1573
1574 /* If this is a global symbol, we count the number of
1575 relocations we need for this symbol. */
1576 if (h != NULL)
1577 {
1578 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1579 }
1580 else
1581 {
1582 void **vpp;
1583 /* Track dynamic relocs needed for local syms too.
1584 We really need local syms available to do this
1585 easily. Oh well. */
1586
1587 asection *s;
1588 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1589 sec, r_symndx);
1590 if (s == NULL)
1591 return FALSE;
1592
1593 vpp = &elf_section_data (s)->local_dynrel;
1594 head = (struct elf_i386_dyn_relocs **)vpp;
1595 }
1596
1597 p = *head;
1598 if (p == NULL || p->sec != sec)
1599 {
1600 bfd_size_type amt = sizeof *p;
1601 p = bfd_alloc (htab->elf.dynobj, amt);
1602 if (p == NULL)
1603 return FALSE;
1604 p->next = *head;
1605 *head = p;
1606 p->sec = sec;
1607 p->count = 0;
1608 p->pc_count = 0;
1609 }
1610
1611 p->count += 1;
1612 if (r_type == R_386_PC32)
1613 p->pc_count += 1;
1614 }
1615 break;
1616
1617 /* This relocation describes the C++ object vtable hierarchy.
1618 Reconstruct it for later use during GC. */
1619 case R_386_GNU_VTINHERIT:
1620 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1621 return FALSE;
1622 break;
1623
1624 /* This relocation describes which C++ vtable entries are actually
1625 used. Record for later use during GC. */
1626 case R_386_GNU_VTENTRY:
1627 BFD_ASSERT (h != NULL);
1628 if (h != NULL
1629 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1630 return FALSE;
1631 break;
1632
1633 default:
1634 break;
1635 }
1636 }
1637
1638 return TRUE;
1639 }
1640
1641 /* Return the section that should be marked against GC for a given
1642 relocation. */
1643
1644 static asection *
1645 elf_i386_gc_mark_hook (asection *sec,
1646 struct bfd_link_info *info,
1647 Elf_Internal_Rela *rel,
1648 struct elf_link_hash_entry *h,
1649 Elf_Internal_Sym *sym)
1650 {
1651 if (h != NULL)
1652 switch (ELF32_R_TYPE (rel->r_info))
1653 {
1654 case R_386_GNU_VTINHERIT:
1655 case R_386_GNU_VTENTRY:
1656 return NULL;
1657 }
1658
1659 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1660 }
1661
1662 /* Update the got entry reference counts for the section being removed. */
1663
1664 static bfd_boolean
1665 elf_i386_gc_sweep_hook (bfd *abfd,
1666 struct bfd_link_info *info,
1667 asection *sec,
1668 const Elf_Internal_Rela *relocs)
1669 {
1670 Elf_Internal_Shdr *symtab_hdr;
1671 struct elf_link_hash_entry **sym_hashes;
1672 bfd_signed_vma *local_got_refcounts;
1673 const Elf_Internal_Rela *rel, *relend;
1674
1675 if (info->relocatable)
1676 return TRUE;
1677
1678 elf_section_data (sec)->local_dynrel = NULL;
1679
1680 symtab_hdr = &elf_symtab_hdr (abfd);
1681 sym_hashes = elf_sym_hashes (abfd);
1682 local_got_refcounts = elf_local_got_refcounts (abfd);
1683
1684 relend = relocs + sec->reloc_count;
1685 for (rel = relocs; rel < relend; rel++)
1686 {
1687 unsigned long r_symndx;
1688 unsigned int r_type;
1689 struct elf_link_hash_entry *h = NULL;
1690
1691 r_symndx = ELF32_R_SYM (rel->r_info);
1692 if (r_symndx >= symtab_hdr->sh_info)
1693 {
1694 struct elf_i386_link_hash_entry *eh;
1695 struct elf_i386_dyn_relocs **pp;
1696 struct elf_i386_dyn_relocs *p;
1697
1698 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1699 while (h->root.type == bfd_link_hash_indirect
1700 || h->root.type == bfd_link_hash_warning)
1701 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1702 eh = (struct elf_i386_link_hash_entry *) h;
1703
1704 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1705 if (p->sec == sec)
1706 {
1707 /* Everything must go for SEC. */
1708 *pp = p->next;
1709 break;
1710 }
1711 }
1712
1713 r_type = ELF32_R_TYPE (rel->r_info);
1714 if (! elf_i386_tls_transition (info, abfd, sec, NULL,
1715 symtab_hdr, sym_hashes,
1716 &r_type, GOT_UNKNOWN,
1717 rel, relend, h))
1718 return FALSE;
1719
1720 switch (r_type)
1721 {
1722 case R_386_TLS_LDM:
1723 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1724 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1725 break;
1726
1727 case R_386_TLS_GD:
1728 case R_386_TLS_GOTDESC:
1729 case R_386_TLS_DESC_CALL:
1730 case R_386_TLS_IE_32:
1731 case R_386_TLS_IE:
1732 case R_386_TLS_GOTIE:
1733 case R_386_GOT32:
1734 if (h != NULL)
1735 {
1736 if (h->got.refcount > 0)
1737 h->got.refcount -= 1;
1738 }
1739 else if (local_got_refcounts != NULL)
1740 {
1741 if (local_got_refcounts[r_symndx] > 0)
1742 local_got_refcounts[r_symndx] -= 1;
1743 }
1744 break;
1745
1746 case R_386_32:
1747 case R_386_PC32:
1748 if (info->shared)
1749 break;
1750 /* Fall through */
1751
1752 case R_386_PLT32:
1753 if (h != NULL)
1754 {
1755 if (h->plt.refcount > 0)
1756 h->plt.refcount -= 1;
1757 }
1758 break;
1759
1760 default:
1761 break;
1762 }
1763 }
1764
1765 return TRUE;
1766 }
1767
1768 /* Adjust a symbol defined by a dynamic object and referenced by a
1769 regular object. The current definition is in some section of the
1770 dynamic object, but we're not including those sections. We have to
1771 change the definition to something the rest of the link can
1772 understand. */
1773
1774 static bfd_boolean
1775 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1776 struct elf_link_hash_entry *h)
1777 {
1778 struct elf_i386_link_hash_table *htab;
1779 asection *s;
1780
1781 /* STT_GNU_IFUNC symbol must go through PLT. */
1782 if (h->type == STT_GNU_IFUNC)
1783 {
1784 if (h->plt.refcount <= 0)
1785 {
1786 h->plt.offset = (bfd_vma) -1;
1787 h->needs_plt = 0;
1788 }
1789 return TRUE;
1790 }
1791
1792 /* If this is a function, put it in the procedure linkage table. We
1793 will fill in the contents of the procedure linkage table later,
1794 when we know the address of the .got section. */
1795 if (h->type == STT_FUNC
1796 || h->needs_plt)
1797 {
1798 if (h->plt.refcount <= 0
1799 || SYMBOL_CALLS_LOCAL (info, h)
1800 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1801 && h->root.type == bfd_link_hash_undefweak))
1802 {
1803 /* This case can occur if we saw a PLT32 reloc in an input
1804 file, but the symbol was never referred to by a dynamic
1805 object, or if all references were garbage collected. In
1806 such a case, we don't actually need to build a procedure
1807 linkage table, and we can just do a PC32 reloc instead. */
1808 h->plt.offset = (bfd_vma) -1;
1809 h->needs_plt = 0;
1810 }
1811
1812 return TRUE;
1813 }
1814 else
1815 /* It's possible that we incorrectly decided a .plt reloc was
1816 needed for an R_386_PC32 reloc to a non-function sym in
1817 check_relocs. We can't decide accurately between function and
1818 non-function syms in check-relocs; Objects loaded later in
1819 the link may change h->type. So fix it now. */
1820 h->plt.offset = (bfd_vma) -1;
1821
1822 /* If this is a weak symbol, and there is a real definition, the
1823 processor independent code will have arranged for us to see the
1824 real definition first, and we can just use the same value. */
1825 if (h->u.weakdef != NULL)
1826 {
1827 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1828 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1829 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1830 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1831 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1832 h->non_got_ref = h->u.weakdef->non_got_ref;
1833 return TRUE;
1834 }
1835
1836 /* This is a reference to a symbol defined by a dynamic object which
1837 is not a function. */
1838
1839 /* If we are creating a shared library, we must presume that the
1840 only references to the symbol are via the global offset table.
1841 For such cases we need not do anything here; the relocations will
1842 be handled correctly by relocate_section. */
1843 if (info->shared)
1844 return TRUE;
1845
1846 /* If there are no references to this symbol that do not use the
1847 GOT, we don't need to generate a copy reloc. */
1848 if (!h->non_got_ref)
1849 return TRUE;
1850
1851 /* If -z nocopyreloc was given, we won't generate them either. */
1852 if (info->nocopyreloc)
1853 {
1854 h->non_got_ref = 0;
1855 return TRUE;
1856 }
1857
1858 htab = elf_i386_hash_table (info);
1859
1860 /* If there aren't any dynamic relocs in read-only sections, then
1861 we can keep the dynamic relocs and avoid the copy reloc. This
1862 doesn't work on VxWorks, where we can not have dynamic relocations
1863 (other than copy and jump slot relocations) in an executable. */
1864 if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
1865 {
1866 struct elf_i386_link_hash_entry * eh;
1867 struct elf_i386_dyn_relocs *p;
1868
1869 eh = (struct elf_i386_link_hash_entry *) h;
1870 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1871 {
1872 s = p->sec->output_section;
1873 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1874 break;
1875 }
1876
1877 if (p == NULL)
1878 {
1879 h->non_got_ref = 0;
1880 return TRUE;
1881 }
1882 }
1883
1884 if (h->size == 0)
1885 {
1886 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1887 h->root.root.string);
1888 return TRUE;
1889 }
1890
1891 /* We must allocate the symbol in our .dynbss section, which will
1892 become part of the .bss section of the executable. There will be
1893 an entry for this symbol in the .dynsym section. The dynamic
1894 object will contain position independent code, so all references
1895 from the dynamic object to this symbol will go through the global
1896 offset table. The dynamic linker will use the .dynsym entry to
1897 determine the address it must put in the global offset table, so
1898 both the dynamic object and the regular object will refer to the
1899 same memory location for the variable. */
1900
1901 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1902 copy the initial value out of the dynamic object and into the
1903 runtime process image. */
1904 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1905 {
1906 htab->srelbss->size += sizeof (Elf32_External_Rel);
1907 h->needs_copy = 1;
1908 }
1909
1910 s = htab->sdynbss;
1911
1912 return _bfd_elf_adjust_dynamic_copy (h, s);
1913 }
1914
1915 /* Allocate space in .plt, .got and associated reloc sections for
1916 dynamic relocs. */
1917
1918 static bfd_boolean
1919 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1920 {
1921 struct bfd_link_info *info;
1922 struct elf_i386_link_hash_table *htab;
1923 struct elf_i386_link_hash_entry *eh;
1924 struct elf_i386_dyn_relocs *p;
1925
1926 if (h->root.type == bfd_link_hash_indirect)
1927 return TRUE;
1928
1929 if (h->root.type == bfd_link_hash_warning)
1930 /* When warning symbols are created, they **replace** the "real"
1931 entry in the hash table, thus we never get to see the real
1932 symbol in a hash traversal. So look at it now. */
1933 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1934 eh = (struct elf_i386_link_hash_entry *) h;
1935
1936 info = (struct bfd_link_info *) inf;
1937 htab = elf_i386_hash_table (info);
1938
1939 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1940 here if it is defined and referenced in a non-shared object. */
1941 if (h->type == STT_GNU_IFUNC
1942 && h->def_regular)
1943 {
1944 asection *plt, *gotplt, *relplt;
1945
1946 /* Return and discard space for dynamic relocations against it if
1947 it is never referenced in a non-shared object. */
1948 if (!h->ref_regular)
1949 {
1950 if (h->plt.refcount > 0
1951 || h->got.refcount > 0)
1952 abort ();
1953 h->got.offset = (bfd_vma) -1;
1954 eh->dyn_relocs = NULL;
1955 return TRUE;
1956 }
1957
1958 /* When building a static executable, use .iplt, .igot.plt and
1959 .rel.iplt sections for STT_GNU_IFUNC symbols. */
1960 if (htab->splt != 0)
1961 {
1962 plt = htab->splt;
1963 gotplt = htab->sgotplt;
1964 relplt = htab->srelplt;
1965
1966 /* If this is the first .plt entry, make room for the special
1967 first entry. */
1968 if (plt->size == 0)
1969 plt->size += PLT_ENTRY_SIZE;
1970 }
1971 else
1972 {
1973 plt = htab->iplt;
1974 gotplt = htab->igotplt;
1975 relplt = htab->irelplt;
1976 }
1977
1978 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
1979 the original value for R_386_IRELATIVE. */
1980 h->plt.offset = plt->size;
1981
1982 /* Make room for this entry in the .plt/.iplt section. */
1983 plt->size += PLT_ENTRY_SIZE;
1984
1985 /* We also need to make an entry in the .got.plt/.got.iplt
1986 section, which will be placed in the .got section by the
1987 linker script. */
1988 gotplt->size += 4;
1989
1990 /* We also need to make an entry in the .rela.plt/.rela.iplt
1991 section. */
1992 relplt->size += sizeof (Elf32_External_Rel);
1993 relplt->reloc_count++;
1994
1995 /* No need for dynamic relocation for local STT_GNU_IFUNC symbol.
1996 Discard space for relocations against it. */
1997 if (h->dynindx == -1 || h->forced_local)
1998 eh->dyn_relocs = NULL;
1999
2000 /* STT_GNU_IFUNC symbol uses .got.plt, not .got. But for
2001 shared library, we must go through GOT and we can't
2002 use R_386_IRELATIVE unless it is forced local. */
2003 if (info->executable
2004 || info->symbolic
2005 || h->forced_local)
2006 {
2007 if (h->pointer_equality_needed
2008 && htab->sgot != NULL)
2009 {
2010 /* We can't use .got.plt, which contains the real
2011 function addres, since we need pointer equality.
2012 We will load the GOT entry with the PLT entry
2013 in elf_i386_finish_dynamic_symbol and don't
2014 need GOT relocation. */
2015 h->got.offset = htab->sgot->size;
2016 htab->sgot->size += 4;
2017 eh->tlsdesc_got = (bfd_vma) -1;
2018 goto skip_relgot;
2019 }
2020 else
2021 h->got.refcount = 0;
2022 }
2023 }
2024 else if (htab->elf.dynamic_sections_created
2025 && h->plt.refcount > 0)
2026 {
2027 /* Make sure this symbol is output as a dynamic symbol.
2028 Undefined weak syms won't yet be marked as dynamic. */
2029 if (h->dynindx == -1
2030 && !h->forced_local)
2031 {
2032 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2033 return FALSE;
2034 }
2035
2036 if (info->shared
2037 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2038 {
2039 asection *s = htab->splt;
2040
2041 /* If this is the first .plt entry, make room for the special
2042 first entry. */
2043 if (s->size == 0)
2044 s->size += PLT_ENTRY_SIZE;
2045
2046 h->plt.offset = s->size;
2047
2048 /* If this symbol is not defined in a regular file, and we are
2049 not generating a shared library, then set the symbol to this
2050 location in the .plt. This is required to make function
2051 pointers compare as equal between the normal executable and
2052 the shared library. */
2053 if (! info->shared
2054 && !h->def_regular)
2055 {
2056 h->root.u.def.section = s;
2057 h->root.u.def.value = h->plt.offset;
2058 }
2059
2060 /* Make room for this entry. */
2061 s->size += PLT_ENTRY_SIZE;
2062
2063 /* We also need to make an entry in the .got.plt section, which
2064 will be placed in the .got section by the linker script. */
2065 htab->sgotplt->size += 4;
2066
2067 /* We also need to make an entry in the .rel.plt section. */
2068 htab->srelplt->size += sizeof (Elf32_External_Rel);
2069 htab->next_tls_desc_index++;
2070
2071 if (htab->is_vxworks && !info->shared)
2072 {
2073 /* VxWorks has a second set of relocations for each PLT entry
2074 in executables. They go in a separate relocation section,
2075 which is processed by the kernel loader. */
2076
2077 /* There are two relocations for the initial PLT entry: an
2078 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
2079 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
2080
2081 if (h->plt.offset == PLT_ENTRY_SIZE)
2082 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2083
2084 /* There are two extra relocations for each subsequent PLT entry:
2085 an R_386_32 relocation for the GOT entry, and an R_386_32
2086 relocation for the PLT entry. */
2087
2088 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
2089 }
2090 }
2091 else
2092 {
2093 h->plt.offset = (bfd_vma) -1;
2094 h->needs_plt = 0;
2095 }
2096 }
2097 else
2098 {
2099 h->plt.offset = (bfd_vma) -1;
2100 h->needs_plt = 0;
2101 }
2102
2103 eh->tlsdesc_got = (bfd_vma) -1;
2104
2105 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
2106 make it a R_386_TLS_LE_32 requiring no TLS entry. */
2107 if (h->got.refcount > 0
2108 && !info->shared
2109 && h->dynindx == -1
2110 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
2111 h->got.offset = (bfd_vma) -1;
2112 else if (h->got.refcount > 0)
2113 {
2114 asection *s;
2115 bfd_boolean dyn;
2116 int tls_type = elf_i386_hash_entry(h)->tls_type;
2117
2118 /* Make sure this symbol is output as a dynamic symbol.
2119 Undefined weak syms won't yet be marked as dynamic. */
2120 if (h->dynindx == -1
2121 && !h->forced_local)
2122 {
2123 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2124 return FALSE;
2125 }
2126
2127 s = htab->sgot;
2128 if (GOT_TLS_GDESC_P (tls_type))
2129 {
2130 eh->tlsdesc_got = htab->sgotplt->size
2131 - elf_i386_compute_jump_table_size (htab);
2132 htab->sgotplt->size += 8;
2133 h->got.offset = (bfd_vma) -2;
2134 }
2135 if (! GOT_TLS_GDESC_P (tls_type)
2136 || GOT_TLS_GD_P (tls_type))
2137 {
2138 h->got.offset = s->size;
2139 s->size += 4;
2140 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
2141 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
2142 s->size += 4;
2143 }
2144 dyn = htab->elf.dynamic_sections_created;
2145 /* R_386_TLS_IE_32 needs one dynamic relocation,
2146 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
2147 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
2148 need two), R_386_TLS_GD needs one if local symbol and two if
2149 global. */
2150 if (tls_type == GOT_TLS_IE_BOTH)
2151 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
2152 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2153 || (tls_type & GOT_TLS_IE))
2154 htab->srelgot->size += sizeof (Elf32_External_Rel);
2155 else if (GOT_TLS_GD_P (tls_type))
2156 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
2157 else if (! GOT_TLS_GDESC_P (tls_type)
2158 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2159 || h->root.type != bfd_link_hash_undefweak)
2160 && (info->shared
2161 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2162 htab->srelgot->size += sizeof (Elf32_External_Rel);
2163 if (GOT_TLS_GDESC_P (tls_type))
2164 htab->srelplt->size += sizeof (Elf32_External_Rel);
2165 }
2166 else
2167 h->got.offset = (bfd_vma) -1;
2168
2169 skip_relgot:
2170 if (eh->dyn_relocs == NULL)
2171 return TRUE;
2172
2173 /* In the shared -Bsymbolic case, discard space allocated for
2174 dynamic pc-relative relocs against symbols which turn out to be
2175 defined in regular objects. For the normal shared case, discard
2176 space for pc-relative relocs that have become local due to symbol
2177 visibility changes. */
2178
2179 if (info->shared)
2180 {
2181 /* The only reloc that uses pc_count is R_386_PC32, which will
2182 appear on a call or on something like ".long foo - .". We
2183 want calls to protected symbols to resolve directly to the
2184 function rather than going via the plt. If people want
2185 function pointer comparisons to work as expected then they
2186 should avoid writing assembly like ".long foo - .". */
2187 if (SYMBOL_CALLS_LOCAL (info, h))
2188 {
2189 struct elf_i386_dyn_relocs **pp;
2190
2191 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2192 {
2193 p->count -= p->pc_count;
2194 p->pc_count = 0;
2195 if (p->count == 0)
2196 *pp = p->next;
2197 else
2198 pp = &p->next;
2199 }
2200 }
2201
2202 if (htab->is_vxworks)
2203 {
2204 struct elf_i386_dyn_relocs **pp;
2205 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2206 {
2207 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2208 *pp = p->next;
2209 else
2210 pp = &p->next;
2211 }
2212 }
2213
2214 /* Also discard relocs on undefined weak syms with non-default
2215 visibility. */
2216 if (eh->dyn_relocs != NULL
2217 && h->root.type == bfd_link_hash_undefweak)
2218 {
2219 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2220 eh->dyn_relocs = NULL;
2221
2222 /* Make sure undefined weak symbols are output as a dynamic
2223 symbol in PIEs. */
2224 else if (h->dynindx == -1
2225 && !h->forced_local)
2226 {
2227 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2228 return FALSE;
2229 }
2230 }
2231 }
2232 else if (ELIMINATE_COPY_RELOCS)
2233 {
2234 /* For the non-shared case, discard space for relocs against
2235 symbols which turn out to need copy relocs or are not
2236 dynamic. */
2237
2238 if (!h->non_got_ref
2239 && ((h->def_dynamic
2240 && !h->def_regular)
2241 || (htab->elf.dynamic_sections_created
2242 && (h->root.type == bfd_link_hash_undefweak
2243 || h->root.type == bfd_link_hash_undefined))))
2244 {
2245 /* Make sure this symbol is output as a dynamic symbol.
2246 Undefined weak syms won't yet be marked as dynamic. */
2247 if (h->dynindx == -1
2248 && !h->forced_local)
2249 {
2250 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2251 return FALSE;
2252 }
2253
2254 /* If that succeeded, we know we'll be keeping all the
2255 relocs. */
2256 if (h->dynindx != -1)
2257 goto keep;
2258 }
2259
2260 eh->dyn_relocs = NULL;
2261
2262 keep: ;
2263 }
2264
2265 /* Finally, allocate space. */
2266 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2267 {
2268 asection *sreloc;
2269
2270 sreloc = elf_section_data (p->sec)->sreloc;
2271
2272 BFD_ASSERT (sreloc != NULL);
2273 sreloc->size += p->count * sizeof (Elf32_External_Rel);
2274 }
2275
2276 return TRUE;
2277 }
2278
2279 /* Find any dynamic relocs that apply to read-only sections. */
2280
2281 static bfd_boolean
2282 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2283 {
2284 struct elf_i386_link_hash_entry *eh;
2285 struct elf_i386_dyn_relocs *p;
2286
2287 if (h->root.type == bfd_link_hash_warning)
2288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2289
2290 eh = (struct elf_i386_link_hash_entry *) h;
2291 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2292 {
2293 asection *s = p->sec->output_section;
2294
2295 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2296 {
2297 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2298
2299 info->flags |= DF_TEXTREL;
2300
2301 /* Not an error, just cut short the traversal. */
2302 return FALSE;
2303 }
2304 }
2305 return TRUE;
2306 }
2307
2308 /* Set the sizes of the dynamic sections. */
2309
2310 static bfd_boolean
2311 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2312 struct bfd_link_info *info)
2313 {
2314 struct elf_i386_link_hash_table *htab;
2315 bfd *dynobj;
2316 asection *s;
2317 bfd_boolean relocs;
2318 bfd *ibfd;
2319
2320 htab = elf_i386_hash_table (info);
2321 dynobj = htab->elf.dynobj;
2322 if (dynobj == NULL)
2323 abort ();
2324
2325 if (htab->elf.dynamic_sections_created)
2326 {
2327 /* Set the contents of the .interp section to the interpreter. */
2328 if (info->executable)
2329 {
2330 s = bfd_get_section_by_name (dynobj, ".interp");
2331 if (s == NULL)
2332 abort ();
2333 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2334 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2335 }
2336 }
2337
2338 /* Set up .got offsets for local syms, and space for local dynamic
2339 relocs. */
2340 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2341 {
2342 bfd_signed_vma *local_got;
2343 bfd_signed_vma *end_local_got;
2344 char *local_tls_type;
2345 bfd_vma *local_tlsdesc_gotent;
2346 bfd_size_type locsymcount;
2347 Elf_Internal_Shdr *symtab_hdr;
2348 asection *srel;
2349
2350 if (! is_i386_elf (ibfd))
2351 continue;
2352
2353 for (s = ibfd->sections; s != NULL; s = s->next)
2354 {
2355 struct elf_i386_dyn_relocs *p;
2356
2357 for (p = ((struct elf_i386_dyn_relocs *)
2358 elf_section_data (s)->local_dynrel);
2359 p != NULL;
2360 p = p->next)
2361 {
2362 if (!bfd_is_abs_section (p->sec)
2363 && bfd_is_abs_section (p->sec->output_section))
2364 {
2365 /* Input section has been discarded, either because
2366 it is a copy of a linkonce section or due to
2367 linker script /DISCARD/, so we'll be discarding
2368 the relocs too. */
2369 }
2370 else if (htab->is_vxworks
2371 && strcmp (p->sec->output_section->name,
2372 ".tls_vars") == 0)
2373 {
2374 /* Relocations in vxworks .tls_vars sections are
2375 handled specially by the loader. */
2376 }
2377 else if (p->count != 0)
2378 {
2379 srel = elf_section_data (p->sec)->sreloc;
2380 srel->size += p->count * sizeof (Elf32_External_Rel);
2381 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2382 info->flags |= DF_TEXTREL;
2383 }
2384 }
2385 }
2386
2387 local_got = elf_local_got_refcounts (ibfd);
2388 if (!local_got)
2389 continue;
2390
2391 symtab_hdr = &elf_symtab_hdr (ibfd);
2392 locsymcount = symtab_hdr->sh_info;
2393 end_local_got = local_got + locsymcount;
2394 local_tls_type = elf_i386_local_got_tls_type (ibfd);
2395 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd);
2396 s = htab->sgot;
2397 srel = htab->srelgot;
2398 for (; local_got < end_local_got;
2399 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2400 {
2401 *local_tlsdesc_gotent = (bfd_vma) -1;
2402 if (*local_got > 0)
2403 {
2404 if (GOT_TLS_GDESC_P (*local_tls_type))
2405 {
2406 *local_tlsdesc_gotent = htab->sgotplt->size
2407 - elf_i386_compute_jump_table_size (htab);
2408 htab->sgotplt->size += 8;
2409 *local_got = (bfd_vma) -2;
2410 }
2411 if (! GOT_TLS_GDESC_P (*local_tls_type)
2412 || GOT_TLS_GD_P (*local_tls_type))
2413 {
2414 *local_got = s->size;
2415 s->size += 4;
2416 if (GOT_TLS_GD_P (*local_tls_type)
2417 || *local_tls_type == GOT_TLS_IE_BOTH)
2418 s->size += 4;
2419 }
2420 if (info->shared
2421 || GOT_TLS_GD_ANY_P (*local_tls_type)
2422 || (*local_tls_type & GOT_TLS_IE))
2423 {
2424 if (*local_tls_type == GOT_TLS_IE_BOTH)
2425 srel->size += 2 * sizeof (Elf32_External_Rel);
2426 else if (GOT_TLS_GD_P (*local_tls_type)
2427 || ! GOT_TLS_GDESC_P (*local_tls_type))
2428 srel->size += sizeof (Elf32_External_Rel);
2429 if (GOT_TLS_GDESC_P (*local_tls_type))
2430 htab->srelplt->size += sizeof (Elf32_External_Rel);
2431 }
2432 }
2433 else
2434 *local_got = (bfd_vma) -1;
2435 }
2436 }
2437
2438 if (htab->tls_ldm_got.refcount > 0)
2439 {
2440 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
2441 relocs. */
2442 htab->tls_ldm_got.offset = htab->sgot->size;
2443 htab->sgot->size += 8;
2444 htab->srelgot->size += sizeof (Elf32_External_Rel);
2445 }
2446 else
2447 htab->tls_ldm_got.offset = -1;
2448
2449 /* Allocate global sym .plt and .got entries, and space for global
2450 sym dynamic relocs. */
2451 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info);
2452
2453 /* For every jump slot reserved in the sgotplt, reloc_count is
2454 incremented. However, when we reserve space for TLS descriptors,
2455 it's not incremented, so in order to compute the space reserved
2456 for them, it suffices to multiply the reloc count by the jump
2457 slot size. */
2458 if (htab->srelplt)
2459 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4;
2460
2461 /* We now have determined the sizes of the various dynamic sections.
2462 Allocate memory for them. */
2463 relocs = FALSE;
2464 for (s = dynobj->sections; s != NULL; s = s->next)
2465 {
2466 bfd_boolean strip_section = TRUE;
2467
2468 if ((s->flags & SEC_LINKER_CREATED) == 0)
2469 continue;
2470
2471 if (s == htab->splt
2472 || s == htab->sgot
2473 || s == htab->sgotplt
2474 || s == htab->iplt
2475 || s == htab->igotplt
2476 || s == htab->sdynbss)
2477 {
2478 /* Strip this section if we don't need it; see the
2479 comment below. */
2480 /* We'd like to strip these sections if they aren't needed, but if
2481 we've exported dynamic symbols from them we must leave them.
2482 It's too late to tell BFD to get rid of the symbols. */
2483
2484 if (htab->elf.hplt != NULL)
2485 strip_section = FALSE;
2486 }
2487 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
2488 {
2489 if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
2490 relocs = TRUE;
2491
2492 /* We use the reloc_count field as a counter if we need
2493 to copy relocs into the output file. */
2494 s->reloc_count = 0;
2495 }
2496 else
2497 {
2498 /* It's not one of our sections, so don't allocate space. */
2499 continue;
2500 }
2501
2502 if (s->size == 0)
2503 {
2504 /* If we don't need this section, strip it from the
2505 output file. This is mostly to handle .rel.bss and
2506 .rel.plt. We must create both sections in
2507 create_dynamic_sections, because they must be created
2508 before the linker maps input sections to output
2509 sections. The linker does that before
2510 adjust_dynamic_symbol is called, and it is that
2511 function which decides whether anything needs to go
2512 into these sections. */
2513 if (strip_section)
2514 s->flags |= SEC_EXCLUDE;
2515 continue;
2516 }
2517
2518 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2519 continue;
2520
2521 /* Allocate memory for the section contents. We use bfd_zalloc
2522 here in case unused entries are not reclaimed before the
2523 section's contents are written out. This should not happen,
2524 but this way if it does, we get a R_386_NONE reloc instead
2525 of garbage. */
2526 s->contents = bfd_zalloc (dynobj, s->size);
2527 if (s->contents == NULL)
2528 return FALSE;
2529 }
2530
2531 if (htab->elf.dynamic_sections_created)
2532 {
2533 /* Add some entries to the .dynamic section. We fill in the
2534 values later, in elf_i386_finish_dynamic_sections, but we
2535 must add the entries now so that we get the correct size for
2536 the .dynamic section. The DT_DEBUG entry is filled in by the
2537 dynamic linker and used by the debugger. */
2538 #define add_dynamic_entry(TAG, VAL) \
2539 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2540
2541 if (info->executable)
2542 {
2543 if (!add_dynamic_entry (DT_DEBUG, 0))
2544 return FALSE;
2545 }
2546
2547 if (htab->splt->size != 0)
2548 {
2549 if (!add_dynamic_entry (DT_PLTGOT, 0)
2550 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2551 || !add_dynamic_entry (DT_PLTREL, DT_REL)
2552 || !add_dynamic_entry (DT_JMPREL, 0))
2553 return FALSE;
2554 }
2555
2556 if (relocs)
2557 {
2558 if (!add_dynamic_entry (DT_REL, 0)
2559 || !add_dynamic_entry (DT_RELSZ, 0)
2560 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
2561 return FALSE;
2562
2563 /* If any dynamic relocs apply to a read-only section,
2564 then we need a DT_TEXTREL entry. */
2565 if ((info->flags & DF_TEXTREL) == 0)
2566 elf_link_hash_traverse (&htab->elf,
2567 elf_i386_readonly_dynrelocs, info);
2568
2569 if ((info->flags & DF_TEXTREL) != 0)
2570 {
2571 if (!add_dynamic_entry (DT_TEXTREL, 0))
2572 return FALSE;
2573 }
2574 }
2575 if (htab->is_vxworks
2576 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
2577 return FALSE;
2578 }
2579 #undef add_dynamic_entry
2580
2581 return TRUE;
2582 }
2583
2584 static bfd_boolean
2585 elf_i386_always_size_sections (bfd *output_bfd,
2586 struct bfd_link_info *info)
2587 {
2588 asection *tls_sec = elf_hash_table (info)->tls_sec;
2589
2590 if (tls_sec)
2591 {
2592 struct elf_link_hash_entry *tlsbase;
2593
2594 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2595 "_TLS_MODULE_BASE_",
2596 FALSE, FALSE, FALSE);
2597
2598 if (tlsbase && tlsbase->type == STT_TLS)
2599 {
2600 struct bfd_link_hash_entry *bh = NULL;
2601 const struct elf_backend_data *bed
2602 = get_elf_backend_data (output_bfd);
2603
2604 if (!(_bfd_generic_link_add_one_symbol
2605 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2606 tls_sec, 0, NULL, FALSE,
2607 bed->collect, &bh)))
2608 return FALSE;
2609
2610 elf_i386_hash_table (info)->tls_module_base = bh;
2611
2612 tlsbase = (struct elf_link_hash_entry *)bh;
2613 tlsbase->def_regular = 1;
2614 tlsbase->other = STV_HIDDEN;
2615 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2616 }
2617 }
2618
2619 return TRUE;
2620 }
2621
2622 /* Set the correct type for an x86 ELF section. We do this by the
2623 section name, which is a hack, but ought to work. */
2624
2625 static bfd_boolean
2626 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2627 Elf_Internal_Shdr *hdr,
2628 asection *sec)
2629 {
2630 register const char *name;
2631
2632 name = bfd_get_section_name (abfd, sec);
2633
2634 /* This is an ugly, but unfortunately necessary hack that is
2635 needed when producing EFI binaries on x86. It tells
2636 elf.c:elf_fake_sections() not to consider ".reloc" as a section
2637 containing ELF relocation info. We need this hack in order to
2638 be able to generate ELF binaries that can be translated into
2639 EFI applications (which are essentially COFF objects). Those
2640 files contain a COFF ".reloc" section inside an ELFNN object,
2641 which would normally cause BFD to segfault because it would
2642 attempt to interpret this section as containing relocation
2643 entries for section "oc". With this hack enabled, ".reloc"
2644 will be treated as a normal data section, which will avoid the
2645 segfault. However, you won't be able to create an ELFNN binary
2646 with a section named "oc" that needs relocations, but that's
2647 the kind of ugly side-effects you get when detecting section
2648 types based on their names... In practice, this limitation is
2649 unlikely to bite. */
2650 if (strcmp (name, ".reloc") == 0)
2651 hdr->sh_type = SHT_PROGBITS;
2652
2653 return TRUE;
2654 }
2655
2656 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2657 executables. Rather than setting it to the beginning of the TLS
2658 section, we have to set it to the end. This function may be called
2659 multiple times, it is idempotent. */
2660
2661 static void
2662 elf_i386_set_tls_module_base (struct bfd_link_info *info)
2663 {
2664 struct bfd_link_hash_entry *base;
2665
2666 if (!info->executable)
2667 return;
2668
2669 base = elf_i386_hash_table (info)->tls_module_base;
2670
2671 if (!base)
2672 return;
2673
2674 base->u.def.value = elf_hash_table (info)->tls_size;
2675 }
2676
2677 /* Return the base VMA address which should be subtracted from real addresses
2678 when resolving @dtpoff relocation.
2679 This is PT_TLS segment p_vaddr. */
2680
2681 static bfd_vma
2682 elf_i386_dtpoff_base (struct bfd_link_info *info)
2683 {
2684 /* If tls_sec is NULL, we should have signalled an error already. */
2685 if (elf_hash_table (info)->tls_sec == NULL)
2686 return 0;
2687 return elf_hash_table (info)->tls_sec->vma;
2688 }
2689
2690 /* Return the relocation value for @tpoff relocation
2691 if STT_TLS virtual address is ADDRESS. */
2692
2693 static bfd_vma
2694 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address)
2695 {
2696 struct elf_link_hash_table *htab = elf_hash_table (info);
2697
2698 /* If tls_sec is NULL, we should have signalled an error already. */
2699 if (htab->tls_sec == NULL)
2700 return 0;
2701 return htab->tls_size + htab->tls_sec->vma - address;
2702 }
2703
2704 /* Relocate an i386 ELF section. */
2705
2706 static bfd_boolean
2707 elf_i386_relocate_section (bfd *output_bfd,
2708 struct bfd_link_info *info,
2709 bfd *input_bfd,
2710 asection *input_section,
2711 bfd_byte *contents,
2712 Elf_Internal_Rela *relocs,
2713 Elf_Internal_Sym *local_syms,
2714 asection **local_sections)
2715 {
2716 struct elf_i386_link_hash_table *htab;
2717 Elf_Internal_Shdr *symtab_hdr;
2718 struct elf_link_hash_entry **sym_hashes;
2719 bfd_vma *local_got_offsets;
2720 bfd_vma *local_tlsdesc_gotents;
2721 Elf_Internal_Rela *rel;
2722 Elf_Internal_Rela *relend;
2723 bfd_boolean is_vxworks_tls;
2724
2725 BFD_ASSERT (is_i386_elf (input_bfd));
2726
2727 htab = elf_i386_hash_table (info);
2728 symtab_hdr = &elf_symtab_hdr (input_bfd);
2729 sym_hashes = elf_sym_hashes (input_bfd);
2730 local_got_offsets = elf_local_got_offsets (input_bfd);
2731 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd);
2732 /* We have to handle relocations in vxworks .tls_vars sections
2733 specially, because the dynamic loader is 'weird'. */
2734 is_vxworks_tls = (htab->is_vxworks && info->shared
2735 && !strcmp (input_section->output_section->name,
2736 ".tls_vars"));
2737
2738 elf_i386_set_tls_module_base (info);
2739
2740 rel = relocs;
2741 relend = relocs + input_section->reloc_count;
2742 for (; rel < relend; rel++)
2743 {
2744 unsigned int r_type;
2745 reloc_howto_type *howto;
2746 unsigned long r_symndx;
2747 struct elf_link_hash_entry *h;
2748 Elf_Internal_Sym *sym;
2749 asection *sec;
2750 bfd_vma off, offplt;
2751 bfd_vma relocation;
2752 bfd_boolean unresolved_reloc;
2753 bfd_reloc_status_type r;
2754 unsigned int indx;
2755 int tls_type;
2756
2757 r_type = ELF32_R_TYPE (rel->r_info);
2758 if (r_type == R_386_GNU_VTINHERIT
2759 || r_type == R_386_GNU_VTENTRY)
2760 continue;
2761
2762 if ((indx = r_type) >= R_386_standard
2763 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2764 >= R_386_ext - R_386_standard)
2765 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2766 >= R_386_irelative - R_386_ext))
2767 {
2768 (*_bfd_error_handler)
2769 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2770 input_bfd, input_section, r_type);
2771 bfd_set_error (bfd_error_bad_value);
2772 return FALSE;
2773 }
2774 howto = elf_howto_table + indx;
2775
2776 r_symndx = ELF32_R_SYM (rel->r_info);
2777 h = NULL;
2778 sym = NULL;
2779 sec = NULL;
2780 unresolved_reloc = FALSE;
2781 if (r_symndx < symtab_hdr->sh_info)
2782 {
2783 sym = local_syms + r_symndx;
2784 sec = local_sections[r_symndx];
2785 relocation = (sec->output_section->vma
2786 + sec->output_offset
2787 + sym->st_value);
2788
2789 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
2790 && ((sec->flags & SEC_MERGE) != 0
2791 || (info->relocatable
2792 && sec->output_offset != 0)))
2793 {
2794 bfd_vma addend;
2795 bfd_byte *where = contents + rel->r_offset;
2796
2797 switch (howto->size)
2798 {
2799 case 0:
2800 addend = bfd_get_8 (input_bfd, where);
2801 if (howto->pc_relative)
2802 {
2803 addend = (addend ^ 0x80) - 0x80;
2804 addend += 1;
2805 }
2806 break;
2807 case 1:
2808 addend = bfd_get_16 (input_bfd, where);
2809 if (howto->pc_relative)
2810 {
2811 addend = (addend ^ 0x8000) - 0x8000;
2812 addend += 2;
2813 }
2814 break;
2815 case 2:
2816 addend = bfd_get_32 (input_bfd, where);
2817 if (howto->pc_relative)
2818 {
2819 addend = (addend ^ 0x80000000) - 0x80000000;
2820 addend += 4;
2821 }
2822 break;
2823 default:
2824 abort ();
2825 }
2826
2827 if (info->relocatable)
2828 addend += sec->output_offset;
2829 else
2830 {
2831 asection *msec = sec;
2832 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
2833 addend);
2834 addend -= relocation;
2835 addend += msec->output_section->vma + msec->output_offset;
2836 }
2837
2838 switch (howto->size)
2839 {
2840 case 0:
2841 /* FIXME: overflow checks. */
2842 if (howto->pc_relative)
2843 addend -= 1;
2844 bfd_put_8 (input_bfd, addend, where);
2845 break;
2846 case 1:
2847 if (howto->pc_relative)
2848 addend -= 2;
2849 bfd_put_16 (input_bfd, addend, where);
2850 break;
2851 case 2:
2852 if (howto->pc_relative)
2853 addend -= 4;
2854 bfd_put_32 (input_bfd, addend, where);
2855 break;
2856 }
2857 }
2858 }
2859 else
2860 {
2861 bfd_boolean warned;
2862
2863 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2864 r_symndx, symtab_hdr, sym_hashes,
2865 h, sec, relocation,
2866 unresolved_reloc, warned);
2867 }
2868
2869 if (sec != NULL && elf_discarded_section (sec))
2870 {
2871 /* For relocs against symbols from removed linkonce sections,
2872 or sections discarded by a linker script, we just want the
2873 section contents zeroed. Avoid any special processing. */
2874 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2875 rel->r_info = 0;
2876 rel->r_addend = 0;
2877 continue;
2878 }
2879
2880 if (info->relocatable)
2881 continue;
2882
2883 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2884 it here if it is defined in a non-shared object. */
2885 if (h != NULL
2886 && h->type == STT_GNU_IFUNC
2887 && h->def_regular)
2888 {
2889 asection *plt, *gotplt, *base_got;
2890 bfd_vma plt_index;
2891
2892 if ((input_section->flags & SEC_ALLOC) == 0
2893 || h->plt.offset == (bfd_vma) -1)
2894 abort ();
2895
2896 /* STT_GNU_IFUNC symbol must go through PLT. */
2897 if (htab->splt != NULL)
2898 {
2899 plt = htab->splt;
2900 gotplt = htab->sgotplt;
2901 }
2902 else
2903 {
2904 plt = htab->iplt;
2905 gotplt = htab->igotplt;
2906 }
2907
2908 relocation = (plt->output_section->vma
2909 + plt->output_offset + h->plt.offset);
2910
2911 switch (r_type)
2912 {
2913 default:
2914 (*_bfd_error_handler)
2915 (_("%B: relocation %s against STT_GNU_IFUNC "
2916 "symbol `%s' isn't handled by %s"), input_bfd,
2917 elf_howto_table[r_type].name,
2918 h->root.root.string, __FUNCTION__);
2919 bfd_set_error (bfd_error_bad_value);
2920 return FALSE;
2921
2922 case R_386_32:
2923 case R_386_PC32:
2924 case R_386_PLT32:
2925 goto do_relocation;
2926
2927 case R_386_GOT32:
2928 base_got = htab->sgot;
2929 off = h->got.offset;
2930
2931 if (base_got == NULL)
2932 abort ();
2933
2934 if (off == (bfd_vma) -1)
2935 {
2936 /* We can't use h->got.offset here to save state, or
2937 even just remember the offset, as finish_dynamic_symbol
2938 would use that as offset into .got. */
2939
2940 if (htab->splt != NULL)
2941 {
2942 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2943 off = (plt_index + 3) * 4;
2944 base_got = htab->sgotplt;
2945 }
2946 else
2947 {
2948 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2949 off = plt_index * 4;
2950 base_got = htab->igotplt;
2951 }
2952
2953 if (h->dynindx == -1
2954 || h->forced_local
2955 || info->symbolic)
2956 {
2957 /* This references the local defitionion. We must
2958 initialize this entry in the global offset table.
2959 Since the offset must always be a multiple of 8,
2960 we use the least significant bit to record
2961 whether we have initialized it already.
2962
2963 When doing a dynamic link, we create a .rela.got
2964 relocation entry to initialize the value. This
2965 is done in the finish_dynamic_symbol routine. */
2966 if ((off & 1) != 0)
2967 off &= ~1;
2968 else
2969 {
2970 bfd_put_32 (output_bfd, relocation,
2971 base_got->contents + off);
2972 h->got.offset |= 1;
2973 }
2974 }
2975
2976 relocation = off;
2977
2978 /* Adjust for static executables. */
2979 if (htab->splt == NULL)
2980 relocation += gotplt->output_offset;
2981 }
2982 else
2983 {
2984 relocation = (base_got->output_section->vma
2985 + base_got->output_offset + off
2986 - gotplt->output_section->vma
2987 - gotplt->output_offset);
2988 /* Adjust for static executables. */
2989 if (htab->splt == NULL)
2990 relocation += gotplt->output_offset;
2991 }
2992
2993 goto do_relocation;
2994
2995 case R_386_GOTOFF:
2996 relocation -= (gotplt->output_section->vma
2997 + gotplt->output_offset);
2998 goto do_relocation;
2999 }
3000 }
3001
3002 switch (r_type)
3003 {
3004 case R_386_GOT32:
3005 /* Relocation is to the entry for this symbol in the global
3006 offset table. */
3007 if (htab->sgot == NULL)
3008 abort ();
3009
3010 if (h != NULL)
3011 {
3012 bfd_boolean dyn;
3013
3014 off = h->got.offset;
3015 dyn = htab->elf.dynamic_sections_created;
3016 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3017 || (info->shared
3018 && SYMBOL_REFERENCES_LOCAL (info, h))
3019 || (ELF_ST_VISIBILITY (h->other)
3020 && h->root.type == bfd_link_hash_undefweak))
3021 {
3022 /* This is actually a static link, or it is a
3023 -Bsymbolic link and the symbol is defined
3024 locally, or the symbol was forced to be local
3025 because of a version file. We must initialize
3026 this entry in the global offset table. Since the
3027 offset must always be a multiple of 4, we use the
3028 least significant bit to record whether we have
3029 initialized it already.
3030
3031 When doing a dynamic link, we create a .rel.got
3032 relocation entry to initialize the value. This
3033 is done in the finish_dynamic_symbol routine. */
3034 if ((off & 1) != 0)
3035 off &= ~1;
3036 else
3037 {
3038 bfd_put_32 (output_bfd, relocation,
3039 htab->sgot->contents + off);
3040 h->got.offset |= 1;
3041 }
3042 }
3043 else
3044 unresolved_reloc = FALSE;
3045 }
3046 else
3047 {
3048 if (local_got_offsets == NULL)
3049 abort ();
3050
3051 off = local_got_offsets[r_symndx];
3052
3053 /* The offset must always be a multiple of 4. We use
3054 the least significant bit to record whether we have
3055 already generated the necessary reloc. */
3056 if ((off & 1) != 0)
3057 off &= ~1;
3058 else
3059 {
3060 bfd_put_32 (output_bfd, relocation,
3061 htab->sgot->contents + off);
3062
3063 if (info->shared)
3064 {
3065 asection *s;
3066 Elf_Internal_Rela outrel;
3067 bfd_byte *loc;
3068
3069 s = htab->srelgot;
3070 if (s == NULL)
3071 abort ();
3072
3073 outrel.r_offset = (htab->sgot->output_section->vma
3074 + htab->sgot->output_offset
3075 + off);
3076 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3077 loc = s->contents;
3078 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
3079 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3080 }
3081
3082 local_got_offsets[r_symndx] |= 1;
3083 }
3084 }
3085
3086 if (off >= (bfd_vma) -2)
3087 abort ();
3088
3089 relocation = htab->sgot->output_section->vma
3090 + htab->sgot->output_offset + off
3091 - htab->sgotplt->output_section->vma
3092 - htab->sgotplt->output_offset;
3093 break;
3094
3095 case R_386_GOTOFF:
3096 /* Relocation is relative to the start of the global offset
3097 table. */
3098
3099 /* Check to make sure it isn't a protected function symbol
3100 for shared library since it may not be local when used
3101 as function address. We also need to make sure that a
3102 symbol is defined locally. */
3103 if (info->shared && h)
3104 {
3105 if (!h->def_regular)
3106 {
3107 const char *v;
3108
3109 switch (ELF_ST_VISIBILITY (h->other))
3110 {
3111 case STV_HIDDEN:
3112 v = _("hidden symbol");
3113 break;
3114 case STV_INTERNAL:
3115 v = _("internal symbol");
3116 break;
3117 case STV_PROTECTED:
3118 v = _("protected symbol");
3119 break;
3120 default:
3121 v = _("symbol");
3122 break;
3123 }
3124
3125 (*_bfd_error_handler)
3126 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"),
3127 input_bfd, v, h->root.root.string);
3128 bfd_set_error (bfd_error_bad_value);
3129 return FALSE;
3130 }
3131 else if (!info->executable
3132 && h->type == STT_FUNC
3133 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3134 {
3135 (*_bfd_error_handler)
3136 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
3137 input_bfd, h->root.root.string);
3138 bfd_set_error (bfd_error_bad_value);
3139 return FALSE;
3140 }
3141 }
3142
3143 /* Note that sgot is not involved in this
3144 calculation. We always want the start of .got.plt. If we
3145 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3146 permitted by the ABI, we might have to change this
3147 calculation. */
3148 relocation -= htab->sgotplt->output_section->vma
3149 + htab->sgotplt->output_offset;
3150 break;
3151
3152 case R_386_GOTPC:
3153 /* Use global offset table as symbol value. */
3154 relocation = htab->sgotplt->output_section->vma
3155 + htab->sgotplt->output_offset;
3156 unresolved_reloc = FALSE;
3157 break;
3158
3159 case R_386_PLT32:
3160 /* Relocation is to the entry for this symbol in the
3161 procedure linkage table. */
3162
3163 /* Resolve a PLT32 reloc against a local symbol directly,
3164 without using the procedure linkage table. */
3165 if (h == NULL)
3166 break;
3167
3168 if (h->plt.offset == (bfd_vma) -1
3169 || htab->splt == NULL)
3170 {
3171 /* We didn't make a PLT entry for this symbol. This
3172 happens when statically linking PIC code, or when
3173 using -Bsymbolic. */
3174 break;
3175 }
3176
3177 relocation = (htab->splt->output_section->vma
3178 + htab->splt->output_offset
3179 + h->plt.offset);
3180 unresolved_reloc = FALSE;
3181 break;
3182
3183 case R_386_32:
3184 case R_386_PC32:
3185 if ((input_section->flags & SEC_ALLOC) == 0
3186 || is_vxworks_tls)
3187 break;
3188
3189 if ((info->shared
3190 && (h == NULL
3191 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3192 || h->root.type != bfd_link_hash_undefweak)
3193 && (r_type != R_386_PC32
3194 || !SYMBOL_CALLS_LOCAL (info, h)))
3195 || (ELIMINATE_COPY_RELOCS
3196 && !info->shared
3197 && h != NULL
3198 && h->dynindx != -1
3199 && !h->non_got_ref
3200 && ((h->def_dynamic
3201 && !h->def_regular)
3202 || h->root.type == bfd_link_hash_undefweak
3203 || h->root.type == bfd_link_hash_undefined)))
3204 {
3205 Elf_Internal_Rela outrel;
3206 bfd_byte *loc;
3207 bfd_boolean skip, relocate;
3208 asection *sreloc;
3209
3210 /* When generating a shared object, these relocations
3211 are copied into the output file to be resolved at run
3212 time. */
3213
3214 skip = FALSE;
3215 relocate = FALSE;
3216
3217 outrel.r_offset =
3218 _bfd_elf_section_offset (output_bfd, info, input_section,
3219 rel->r_offset);
3220 if (outrel.r_offset == (bfd_vma) -1)
3221 skip = TRUE;
3222 else if (outrel.r_offset == (bfd_vma) -2)
3223 skip = TRUE, relocate = TRUE;
3224 outrel.r_offset += (input_section->output_section->vma
3225 + input_section->output_offset);
3226
3227 if (skip)
3228 memset (&outrel, 0, sizeof outrel);
3229 else if (h != NULL
3230 && h->dynindx != -1
3231 && (r_type == R_386_PC32
3232 || !info->shared
3233 || !SYMBOLIC_BIND (info, h)
3234 || !h->def_regular))
3235 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3236 else
3237 {
3238 /* This symbol is local, or marked to become local. */
3239 relocate = TRUE;
3240 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3241 }
3242
3243 sreloc = elf_section_data (input_section)->sreloc;
3244
3245 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3246
3247 loc = sreloc->contents;
3248 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3249
3250 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3251
3252 /* If this reloc is against an external symbol, we do
3253 not want to fiddle with the addend. Otherwise, we
3254 need to include the symbol value so that it becomes
3255 an addend for the dynamic reloc. */
3256 if (! relocate)
3257 continue;
3258 }
3259 break;
3260
3261 case R_386_TLS_IE:
3262 if (info->shared)
3263 {
3264 Elf_Internal_Rela outrel;
3265 bfd_byte *loc;
3266 asection *sreloc;
3267
3268 outrel.r_offset = rel->r_offset
3269 + input_section->output_section->vma
3270 + input_section->output_offset;
3271 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3272 sreloc = elf_section_data (input_section)->sreloc;
3273 if (sreloc == NULL)
3274 abort ();
3275 loc = sreloc->contents;
3276 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3277 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3278 }
3279 /* Fall through */
3280
3281 case R_386_TLS_GD:
3282 case R_386_TLS_GOTDESC:
3283 case R_386_TLS_DESC_CALL:
3284 case R_386_TLS_IE_32:
3285 case R_386_TLS_GOTIE:
3286 tls_type = GOT_UNKNOWN;
3287 if (h == NULL && local_got_offsets)
3288 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
3289 else if (h != NULL)
3290 tls_type = elf_i386_hash_entry(h)->tls_type;
3291 if (tls_type == GOT_TLS_IE)
3292 tls_type = GOT_TLS_IE_NEG;
3293
3294 if (! elf_i386_tls_transition (info, input_bfd,
3295 input_section, contents,
3296 symtab_hdr, sym_hashes,
3297 &r_type, tls_type, rel,
3298 relend, h))
3299 return FALSE;
3300
3301 if (r_type == R_386_TLS_LE_32)
3302 {
3303 BFD_ASSERT (! unresolved_reloc);
3304 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3305 {
3306 unsigned int type;
3307 bfd_vma roff;
3308
3309 /* GD->LE transition. */
3310 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3311 if (type == 0x04)
3312 {
3313 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3314 Change it into:
3315 movl %gs:0, %eax; subl $foo@tpoff, %eax
3316 (6 byte form of subl). */
3317 memcpy (contents + rel->r_offset - 3,
3318 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3319 roff = rel->r_offset + 5;
3320 }
3321 else
3322 {
3323 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3324 Change it into:
3325 movl %gs:0, %eax; subl $foo@tpoff, %eax
3326 (6 byte form of subl). */
3327 memcpy (contents + rel->r_offset - 2,
3328 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3329 roff = rel->r_offset + 6;
3330 }
3331 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3332 contents + roff);
3333 /* Skip R_386_PC32/R_386_PLT32. */
3334 rel++;
3335 continue;
3336 }
3337 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3338 {
3339 /* GDesc -> LE transition.
3340 It's originally something like:
3341 leal x@tlsdesc(%ebx), %eax
3342
3343 leal x@ntpoff, %eax
3344
3345 Registers other than %eax may be set up here. */
3346
3347 unsigned int val;
3348 bfd_vma roff;
3349
3350 roff = rel->r_offset;
3351 val = bfd_get_8 (input_bfd, contents + roff - 1);
3352
3353 /* Now modify the instruction as appropriate. */
3354 /* aoliva FIXME: remove the above and xor the byte
3355 below with 0x86. */
3356 bfd_put_8 (output_bfd, val ^ 0x86,
3357 contents + roff - 1);
3358 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3359 contents + roff);
3360 continue;
3361 }
3362 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3363 {
3364 /* GDesc -> LE transition.
3365 It's originally:
3366 call *(%eax)
3367 Turn it into:
3368 xchg %ax,%ax */
3369
3370 bfd_vma roff;
3371
3372 roff = rel->r_offset;
3373 bfd_put_8 (output_bfd, 0x66, contents + roff);
3374 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3375 continue;
3376 }
3377 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
3378 {
3379 unsigned int val;
3380
3381 /* IE->LE transition:
3382 Originally it can be one of:
3383 movl foo, %eax
3384 movl foo, %reg
3385 addl foo, %reg
3386 We change it into:
3387 movl $foo, %eax
3388 movl $foo, %reg
3389 addl $foo, %reg. */
3390 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3391 if (val == 0xa1)
3392 {
3393 /* movl foo, %eax. */
3394 bfd_put_8 (output_bfd, 0xb8,
3395 contents + rel->r_offset - 1);
3396 }
3397 else
3398 {
3399 unsigned int type;
3400
3401 type = bfd_get_8 (input_bfd,
3402 contents + rel->r_offset - 2);
3403 switch (type)
3404 {
3405 case 0x8b:
3406 /* movl */
3407 bfd_put_8 (output_bfd, 0xc7,
3408 contents + rel->r_offset - 2);
3409 bfd_put_8 (output_bfd,
3410 0xc0 | ((val >> 3) & 7),
3411 contents + rel->r_offset - 1);
3412 break;
3413 case 0x03:
3414 /* addl */
3415 bfd_put_8 (output_bfd, 0x81,
3416 contents + rel->r_offset - 2);
3417 bfd_put_8 (output_bfd,
3418 0xc0 | ((val >> 3) & 7),
3419 contents + rel->r_offset - 1);
3420 break;
3421 default:
3422 BFD_FAIL ();
3423 break;
3424 }
3425 }
3426 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3427 contents + rel->r_offset);
3428 continue;
3429 }
3430 else
3431 {
3432 unsigned int val, type;
3433
3434 /* {IE_32,GOTIE}->LE transition:
3435 Originally it can be one of:
3436 subl foo(%reg1), %reg2
3437 movl foo(%reg1), %reg2
3438 addl foo(%reg1), %reg2
3439 We change it into:
3440 subl $foo, %reg2
3441 movl $foo, %reg2 (6 byte form)
3442 addl $foo, %reg2. */
3443 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3444 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3445 if (type == 0x8b)
3446 {
3447 /* movl */
3448 bfd_put_8 (output_bfd, 0xc7,
3449 contents + rel->r_offset - 2);
3450 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3451 contents + rel->r_offset - 1);
3452 }
3453 else if (type == 0x2b)
3454 {
3455 /* subl */
3456 bfd_put_8 (output_bfd, 0x81,
3457 contents + rel->r_offset - 2);
3458 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
3459 contents + rel->r_offset - 1);
3460 }
3461 else if (type == 0x03)
3462 {
3463 /* addl */
3464 bfd_put_8 (output_bfd, 0x81,
3465 contents + rel->r_offset - 2);
3466 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3467 contents + rel->r_offset - 1);
3468 }
3469 else
3470 BFD_FAIL ();
3471 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
3472 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation),
3473 contents + rel->r_offset);
3474 else
3475 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation),
3476 contents + rel->r_offset);
3477 continue;
3478 }
3479 }
3480
3481 if (htab->sgot == NULL)
3482 abort ();
3483
3484 if (h != NULL)
3485 {
3486 off = h->got.offset;
3487 offplt = elf_i386_hash_entry (h)->tlsdesc_got;
3488 }
3489 else
3490 {
3491 if (local_got_offsets == NULL)
3492 abort ();
3493
3494 off = local_got_offsets[r_symndx];
3495 offplt = local_tlsdesc_gotents[r_symndx];
3496 }
3497
3498 if ((off & 1) != 0)
3499 off &= ~1;
3500 else
3501 {
3502 Elf_Internal_Rela outrel;
3503 bfd_byte *loc;
3504 int dr_type, indx;
3505 asection *sreloc;
3506
3507 if (htab->srelgot == NULL)
3508 abort ();
3509
3510 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3511
3512 if (GOT_TLS_GDESC_P (tls_type))
3513 {
3514 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC);
3515 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8
3516 <= htab->sgotplt->size);
3517 outrel.r_offset = (htab->sgotplt->output_section->vma
3518 + htab->sgotplt->output_offset
3519 + offplt
3520 + htab->sgotplt_jump_table_size);
3521 sreloc = htab->srelplt;
3522 loc = sreloc->contents;
3523 loc += (htab->next_tls_desc_index++
3524 * sizeof (Elf32_External_Rel));
3525 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3526 <= sreloc->contents + sreloc->size);
3527 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3528 if (indx == 0)
3529 {
3530 BFD_ASSERT (! unresolved_reloc);
3531 bfd_put_32 (output_bfd,
3532 relocation - elf_i386_dtpoff_base (info),
3533 htab->sgotplt->contents + offplt
3534 + htab->sgotplt_jump_table_size + 4);
3535 }
3536 else
3537 {
3538 bfd_put_32 (output_bfd, 0,
3539 htab->sgotplt->contents + offplt
3540 + htab->sgotplt_jump_table_size + 4);
3541 }
3542 }
3543
3544 sreloc = htab->srelgot;
3545
3546 outrel.r_offset = (htab->sgot->output_section->vma
3547 + htab->sgot->output_offset + off);
3548
3549 if (GOT_TLS_GD_P (tls_type))
3550 dr_type = R_386_TLS_DTPMOD32;
3551 else if (GOT_TLS_GDESC_P (tls_type))
3552 goto dr_done;
3553 else if (tls_type == GOT_TLS_IE_POS)
3554 dr_type = R_386_TLS_TPOFF;
3555 else
3556 dr_type = R_386_TLS_TPOFF32;
3557
3558 if (dr_type == R_386_TLS_TPOFF && indx == 0)
3559 bfd_put_32 (output_bfd,
3560 relocation - elf_i386_dtpoff_base (info),
3561 htab->sgot->contents + off);
3562 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
3563 bfd_put_32 (output_bfd,
3564 elf_i386_dtpoff_base (info) - relocation,
3565 htab->sgot->contents + off);
3566 else if (dr_type != R_386_TLS_DESC)
3567 bfd_put_32 (output_bfd, 0,
3568 htab->sgot->contents + off);
3569 outrel.r_info = ELF32_R_INFO (indx, dr_type);
3570
3571 loc = sreloc->contents;
3572 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3573 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3574 <= sreloc->contents + sreloc->size);
3575 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3576
3577 if (GOT_TLS_GD_P (tls_type))
3578 {
3579 if (indx == 0)
3580 {
3581 BFD_ASSERT (! unresolved_reloc);
3582 bfd_put_32 (output_bfd,
3583 relocation - elf_i386_dtpoff_base (info),
3584 htab->sgot->contents + off + 4);
3585 }
3586 else
3587 {
3588 bfd_put_32 (output_bfd, 0,
3589 htab->sgot->contents + off + 4);
3590 outrel.r_info = ELF32_R_INFO (indx,
3591 R_386_TLS_DTPOFF32);
3592 outrel.r_offset += 4;
3593 sreloc->reloc_count++;
3594 loc += sizeof (Elf32_External_Rel);
3595 BFD_ASSERT (loc + sizeof (Elf32_External_Rel)
3596 <= sreloc->contents + sreloc->size);
3597 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3598 }
3599 }
3600 else if (tls_type == GOT_TLS_IE_BOTH)
3601 {
3602 bfd_put_32 (output_bfd,
3603 (indx == 0
3604 ? relocation - elf_i386_dtpoff_base (info)
3605 : 0),
3606 htab->sgot->contents + off + 4);
3607 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3608 outrel.r_offset += 4;
3609 sreloc->reloc_count++;
3610 loc += sizeof (Elf32_External_Rel);
3611 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3612 }
3613
3614 dr_done:
3615 if (h != NULL)
3616 h->got.offset |= 1;
3617 else
3618 local_got_offsets[r_symndx] |= 1;
3619 }
3620
3621 if (off >= (bfd_vma) -2
3622 && ! GOT_TLS_GDESC_P (tls_type))
3623 abort ();
3624 if (r_type == R_386_TLS_GOTDESC
3625 || r_type == R_386_TLS_DESC_CALL)
3626 {
3627 relocation = htab->sgotplt_jump_table_size + offplt;
3628 unresolved_reloc = FALSE;
3629 }
3630 else if (r_type == ELF32_R_TYPE (rel->r_info))
3631 {
3632 bfd_vma g_o_t = htab->sgotplt->output_section->vma
3633 + htab->sgotplt->output_offset;
3634 relocation = htab->sgot->output_section->vma
3635 + htab->sgot->output_offset + off - g_o_t;
3636 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
3637 && tls_type == GOT_TLS_IE_BOTH)
3638 relocation += 4;
3639 if (r_type == R_386_TLS_IE)
3640 relocation += g_o_t;
3641 unresolved_reloc = FALSE;
3642 }
3643 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
3644 {
3645 unsigned int val, type;
3646 bfd_vma roff;
3647
3648 /* GD->IE transition. */
3649 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
3650 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
3651 if (type == 0x04)
3652 {
3653 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
3654 Change it into:
3655 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3656 val >>= 3;
3657 roff = rel->r_offset - 3;
3658 }
3659 else
3660 {
3661 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
3662 Change it into:
3663 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
3664 roff = rel->r_offset - 2;
3665 }
3666 memcpy (contents + roff,
3667 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
3668 contents[roff + 7] = 0x80 | (val & 7);
3669 /* If foo is used only with foo@gotntpoff(%reg) and
3670 foo@indntpoff, but not with foo@gottpoff(%reg), change
3671 subl $foo@gottpoff(%reg), %eax
3672 into:
3673 addl $foo@gotntpoff(%reg), %eax. */
3674 if (tls_type == GOT_TLS_IE_POS)
3675 contents[roff + 6] = 0x03;
3676 bfd_put_32 (output_bfd,
3677 htab->sgot->output_section->vma
3678 + htab->sgot->output_offset + off
3679 - htab->sgotplt->output_section->vma
3680 - htab->sgotplt->output_offset,
3681 contents + roff + 8);
3682 /* Skip R_386_PLT32. */
3683 rel++;
3684 continue;
3685 }
3686 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC)
3687 {
3688 /* GDesc -> IE transition.
3689 It's originally something like:
3690 leal x@tlsdesc(%ebx), %eax
3691
3692 Change it to:
3693 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax
3694 or:
3695 movl x@gottpoff(%ebx), %eax # before negl %eax
3696
3697 Registers other than %eax may be set up here. */
3698
3699 bfd_vma roff;
3700
3701 /* First, make sure it's a leal adding ebx to a 32-bit
3702 offset into any register, although it's probably
3703 almost always going to be eax. */
3704 roff = rel->r_offset;
3705
3706 /* Now modify the instruction as appropriate. */
3707 /* To turn a leal into a movl in the form we use it, it
3708 suffices to change the first byte from 0x8d to 0x8b.
3709 aoliva FIXME: should we decide to keep the leal, all
3710 we have to do is remove the statement below, and
3711 adjust the relaxation of R_386_TLS_DESC_CALL. */
3712 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3713
3714 if (tls_type == GOT_TLS_IE_BOTH)
3715 off += 4;
3716
3717 bfd_put_32 (output_bfd,
3718 htab->sgot->output_section->vma
3719 + htab->sgot->output_offset + off
3720 - htab->sgotplt->output_section->vma
3721 - htab->sgotplt->output_offset,
3722 contents + roff);
3723 continue;
3724 }
3725 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL)
3726 {
3727 /* GDesc -> IE transition.
3728 It's originally:
3729 call *(%eax)
3730
3731 Change it to:
3732 xchg %ax,%ax
3733 or
3734 negl %eax
3735 depending on how we transformed the TLS_GOTDESC above.
3736 */
3737
3738 bfd_vma roff;
3739
3740 roff = rel->r_offset;
3741
3742 /* Now modify the instruction as appropriate. */
3743 if (tls_type != GOT_TLS_IE_NEG)
3744 {
3745 /* xchg %ax,%ax */
3746 bfd_put_8 (output_bfd, 0x66, contents + roff);
3747 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3748 }
3749 else
3750 {
3751 /* negl %eax */
3752 bfd_put_8 (output_bfd, 0xf7, contents + roff);
3753 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1);
3754 }
3755
3756 continue;
3757 }
3758 else
3759 BFD_ASSERT (FALSE);
3760 break;
3761
3762 case R_386_TLS_LDM:
3763 if (! elf_i386_tls_transition (info, input_bfd,
3764 input_section, contents,
3765 symtab_hdr, sym_hashes,
3766 &r_type, GOT_UNKNOWN, rel,
3767 relend, h))
3768 return FALSE;
3769
3770 if (r_type != R_386_TLS_LDM)
3771 {
3772 /* LD->LE transition:
3773 leal foo(%reg), %eax; call ___tls_get_addr.
3774 We change it into:
3775 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
3776 BFD_ASSERT (r_type == R_386_TLS_LE_32);
3777 memcpy (contents + rel->r_offset - 2,
3778 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
3779 /* Skip R_386_PC32/R_386_PLT32. */
3780 rel++;
3781 continue;
3782 }
3783
3784 if (htab->sgot == NULL)
3785 abort ();
3786
3787 off = htab->tls_ldm_got.offset;
3788 if (off & 1)
3789 off &= ~1;
3790 else
3791 {
3792 Elf_Internal_Rela outrel;
3793 bfd_byte *loc;
3794
3795 if (htab->srelgot == NULL)
3796 abort ();
3797
3798 outrel.r_offset = (htab->sgot->output_section->vma
3799 + htab->sgot->output_offset + off);
3800
3801 bfd_put_32 (output_bfd, 0,
3802 htab->sgot->contents + off);
3803 bfd_put_32 (output_bfd, 0,
3804 htab->sgot->contents + off + 4);
3805 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
3806 loc = htab->srelgot->contents;
3807 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3808 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3809 htab->tls_ldm_got.offset |= 1;
3810 }
3811 relocation = htab->sgot->output_section->vma
3812 + htab->sgot->output_offset + off
3813 - htab->sgotplt->output_section->vma
3814 - htab->sgotplt->output_offset;
3815 unresolved_reloc = FALSE;
3816 break;
3817
3818 case R_386_TLS_LDO_32:
3819 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3820 relocation -= elf_i386_dtpoff_base (info);
3821 else
3822 /* When converting LDO to LE, we must negate. */
3823 relocation = -elf_i386_tpoff (info, relocation);
3824 break;
3825
3826 case R_386_TLS_LE_32:
3827 case R_386_TLS_LE:
3828 if (info->shared)
3829 {
3830 Elf_Internal_Rela outrel;
3831 asection *sreloc;
3832 bfd_byte *loc;
3833 int indx;
3834
3835 outrel.r_offset = rel->r_offset
3836 + input_section->output_section->vma
3837 + input_section->output_offset;
3838 if (h != NULL && h->dynindx != -1)
3839 indx = h->dynindx;
3840 else
3841 indx = 0;
3842 if (r_type == R_386_TLS_LE_32)
3843 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
3844 else
3845 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
3846 sreloc = elf_section_data (input_section)->sreloc;
3847 if (sreloc == NULL)
3848 abort ();
3849 loc = sreloc->contents;
3850 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
3851 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
3852 if (indx)
3853 continue;
3854 else if (r_type == R_386_TLS_LE_32)
3855 relocation = elf_i386_dtpoff_base (info) - relocation;
3856 else
3857 relocation -= elf_i386_dtpoff_base (info);
3858 }
3859 else if (r_type == R_386_TLS_LE_32)
3860 relocation = elf_i386_tpoff (info, relocation);
3861 else
3862 relocation = -elf_i386_tpoff (info, relocation);
3863 break;
3864
3865 default:
3866 break;
3867 }
3868
3869 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3870 because such sections are not SEC_ALLOC and thus ld.so will
3871 not process them. */
3872 if (unresolved_reloc
3873 && !((input_section->flags & SEC_DEBUGGING) != 0
3874 && h->def_dynamic))
3875 {
3876 (*_bfd_error_handler)
3877 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3878 input_bfd,
3879 input_section,
3880 (long) rel->r_offset,
3881 howto->name,
3882 h->root.root.string);
3883 return FALSE;
3884 }
3885
3886 do_relocation:
3887 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3888 contents, rel->r_offset,
3889 relocation, 0);
3890
3891 if (r != bfd_reloc_ok)
3892 {
3893 const char *name;
3894
3895 if (h != NULL)
3896 name = h->root.root.string;
3897 else
3898 {
3899 name = bfd_elf_string_from_elf_section (input_bfd,
3900 symtab_hdr->sh_link,
3901 sym->st_name);
3902 if (name == NULL)
3903 return FALSE;
3904 if (*name == '\0')
3905 name = bfd_section_name (input_bfd, sec);
3906 }
3907
3908 if (r == bfd_reloc_overflow)
3909 {
3910 if (! ((*info->callbacks->reloc_overflow)
3911 (info, (h ? &h->root : NULL), name, howto->name,
3912 (bfd_vma) 0, input_bfd, input_section,
3913 rel->r_offset)))
3914 return FALSE;
3915 }
3916 else
3917 {
3918 (*_bfd_error_handler)
3919 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3920 input_bfd, input_section,
3921 (long) rel->r_offset, name, (int) r);
3922 return FALSE;
3923 }
3924 }
3925 }
3926
3927 return TRUE;
3928 }
3929
3930 /* Finish up dynamic symbol handling. We set the contents of various
3931 dynamic sections here. */
3932
3933 static bfd_boolean
3934 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
3935 struct bfd_link_info *info,
3936 struct elf_link_hash_entry *h,
3937 Elf_Internal_Sym *sym)
3938 {
3939 struct elf_i386_link_hash_table *htab;
3940
3941 htab = elf_i386_hash_table (info);
3942
3943 if (h->plt.offset != (bfd_vma) -1)
3944 {
3945 bfd_vma plt_index;
3946 bfd_vma got_offset;
3947 Elf_Internal_Rela rel;
3948 bfd_byte *loc;
3949 asection *plt, *gotplt, *relplt;
3950
3951 /* When building a static executable, use .iplt, .igot.plt and
3952 .rel.iplt sections for STT_GNU_IFUNC symbols. */
3953 if (htab->splt != 0)
3954 {
3955 plt = htab->splt;
3956 gotplt = htab->sgotplt;
3957 relplt = htab->srelplt;
3958 }
3959 else
3960 {
3961 plt = htab->iplt;
3962 gotplt = htab->igotplt;
3963 relplt = htab->irelplt;
3964 }
3965
3966 /* This symbol has an entry in the procedure linkage table. Set
3967 it up. */
3968
3969 if ((h->dynindx == -1
3970 && !((h->forced_local || info->executable)
3971 && h->def_regular
3972 && h->type == STT_GNU_IFUNC))
3973 || plt == NULL
3974 || gotplt == NULL
3975 || relplt == NULL)
3976 abort ();
3977
3978 /* Get the index in the procedure linkage table which
3979 corresponds to this symbol. This is the index of this symbol
3980 in all the symbols for which we are making plt entries. The
3981 first entry in the procedure linkage table is reserved.
3982
3983 Get the offset into the .got table of the entry that
3984 corresponds to this function. Each .got entry is 4 bytes.
3985 The first three are reserved.
3986
3987 For static executables, we don't reserve anything. */
3988
3989 if (plt == htab->splt)
3990 {
3991 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3992 got_offset = (plt_index + 3) * 4;
3993 }
3994 else
3995 {
3996 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3997 got_offset = plt_index * 4;
3998 }
3999
4000 /* Fill in the entry in the procedure linkage table. */
4001 if (! info->shared)
4002 {
4003 memcpy (plt->contents + h->plt.offset, elf_i386_plt_entry,
4004 PLT_ENTRY_SIZE);
4005 bfd_put_32 (output_bfd,
4006 (gotplt->output_section->vma
4007 + gotplt->output_offset
4008 + got_offset),
4009 plt->contents + h->plt.offset + 2);
4010
4011 if (htab->is_vxworks)
4012 {
4013 int s, k, reloc_index;
4014
4015 /* Create the R_386_32 relocation referencing the GOT
4016 for this PLT entry. */
4017
4018 /* S: Current slot number (zero-based). */
4019 s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
4020 /* K: Number of relocations for PLTResolve. */
4021 if (info->shared)
4022 k = PLTRESOLVE_RELOCS_SHLIB;
4023 else
4024 k = PLTRESOLVE_RELOCS;
4025 /* Skip the PLTresolve relocations, and the relocations for
4026 the other PLT slots. */
4027 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
4028 loc = (htab->srelplt2->contents + reloc_index
4029 * sizeof (Elf32_External_Rel));
4030
4031 rel.r_offset = (htab->splt->output_section->vma
4032 + htab->splt->output_offset
4033 + h->plt.offset + 2),
4034 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4035 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4036
4037 /* Create the R_386_32 relocation referencing the beginning of
4038 the PLT for this GOT entry. */
4039 rel.r_offset = (htab->sgotplt->output_section->vma
4040 + htab->sgotplt->output_offset
4041 + got_offset);
4042 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4043 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4044 loc + sizeof (Elf32_External_Rel));
4045 }
4046 }
4047 else
4048 {
4049 memcpy (plt->contents + h->plt.offset, elf_i386_pic_plt_entry,
4050 PLT_ENTRY_SIZE);
4051 bfd_put_32 (output_bfd, got_offset,
4052 plt->contents + h->plt.offset + 2);
4053 }
4054
4055 /* Don't fill PLT entry for static executables. */
4056 if (plt == htab->splt)
4057 {
4058 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
4059 plt->contents + h->plt.offset + 7);
4060 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
4061 plt->contents + h->plt.offset + 12);
4062 }
4063
4064 /* Fill in the entry in the global offset table. */
4065 bfd_put_32 (output_bfd,
4066 (plt->output_section->vma
4067 + plt->output_offset
4068 + h->plt.offset
4069 + 6),
4070 gotplt->contents + got_offset);
4071
4072 /* Fill in the entry in the .rel.plt section. */
4073 rel.r_offset = (gotplt->output_section->vma
4074 + gotplt->output_offset
4075 + got_offset);
4076 if (h->dynindx == -1
4077 || ((info->executable
4078 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4079 && h->def_regular
4080 && h->type == STT_GNU_IFUNC))
4081 {
4082 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4083 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend
4084 in the .got.plt section. */
4085 bfd_put_32 (output_bfd,
4086 (h->root.u.def.value
4087 + h->root.u.def.section->output_section->vma
4088 + h->root.u.def.section->output_offset),
4089 gotplt->contents + got_offset);
4090 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE);
4091 }
4092 else
4093 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
4094 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel);
4095 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4096
4097 if (!h->def_regular)
4098 {
4099 /* Mark the symbol as undefined, rather than as defined in
4100 the .plt section. Leave the value if there were any
4101 relocations where pointer equality matters (this is a clue
4102 for the dynamic linker, to make function pointer
4103 comparisons work between an application and shared
4104 library), otherwise set it to zero. If a function is only
4105 called from a binary, there is no need to slow down
4106 shared libraries because of that. */
4107 sym->st_shndx = SHN_UNDEF;
4108 if (!h->pointer_equality_needed)
4109 sym->st_value = 0;
4110 }
4111 }
4112
4113 if (h->got.offset != (bfd_vma) -1
4114 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type)
4115 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
4116 {
4117 Elf_Internal_Rela rel;
4118 bfd_byte *loc;
4119
4120 /* This symbol has an entry in the global offset table. Set it
4121 up. */
4122
4123 if (htab->sgot == NULL || htab->srelgot == NULL)
4124 abort ();
4125
4126 rel.r_offset = (htab->sgot->output_section->vma
4127 + htab->sgot->output_offset
4128 + (h->got.offset & ~(bfd_vma) 1));
4129
4130 /* If this is a static link, or it is a -Bsymbolic link and the
4131 symbol is defined locally or was forced to be local because
4132 of a version file, we just want to emit a RELATIVE reloc.
4133 The entry in the global offset table will already have been
4134 initialized in the relocate_section function. */
4135 if ((info->executable
4136 || info->symbolic
4137 || h->forced_local)
4138 && h->def_regular
4139 && h->pointer_equality_needed
4140 && h->type == STT_GNU_IFUNC)
4141 {
4142 /* The STT_GNU_IFUNC symbol is locally defined. But we can't
4143 use .got.plt, which contains the real function addres,
4144 since we need pointer equality. We load the GOT entry
4145 with the PLT entry without relocation. */
4146 asection *plt = htab->splt ? htab->splt : htab->iplt;
4147 if (htab->sgot == NULL
4148 || h->plt.offset == (bfd_vma) -1)
4149 abort ();
4150 bfd_put_32 (output_bfd, (plt->output_section->vma
4151 + plt->output_offset + h->plt.offset),
4152 htab->sgot->contents + h->got.offset);
4153 return TRUE;
4154 }
4155 else if (info->shared
4156 && SYMBOL_REFERENCES_LOCAL (info, h))
4157 {
4158 BFD_ASSERT((h->got.offset & 1) != 0);
4159 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
4160 }
4161 else
4162 {
4163 BFD_ASSERT((h->got.offset & 1) == 0);
4164 bfd_put_32 (output_bfd, (bfd_vma) 0,
4165 htab->sgot->contents + h->got.offset);
4166 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
4167 }
4168
4169 loc = htab->srelgot->contents;
4170 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
4171 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4172 }
4173
4174 if (h->needs_copy)
4175 {
4176 Elf_Internal_Rela rel;
4177 bfd_byte *loc;
4178
4179 /* This symbol needs a copy reloc. Set it up. */
4180
4181 if (h->dynindx == -1
4182 || (h->root.type != bfd_link_hash_defined
4183 && h->root.type != bfd_link_hash_defweak)
4184 || htab->srelbss == NULL)
4185 abort ();
4186
4187 rel.r_offset = (h->root.u.def.value
4188 + h->root.u.def.section->output_section->vma
4189 + h->root.u.def.section->output_offset);
4190 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
4191 loc = htab->srelbss->contents;
4192 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
4193 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
4194 }
4195
4196 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
4197 On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
4198 is relative to the ".got" section. */
4199 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4200 || (!htab->is_vxworks && h == htab->elf.hgot))
4201 sym->st_shndx = SHN_ABS;
4202
4203 return TRUE;
4204 }
4205
4206 /* Used to decide how to sort relocs in an optimal manner for the
4207 dynamic linker, before writing them out. */
4208
4209 static enum elf_reloc_type_class
4210 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
4211 {
4212 switch (ELF32_R_TYPE (rela->r_info))
4213 {
4214 case R_386_RELATIVE:
4215 return reloc_class_relative;
4216 case R_386_JUMP_SLOT:
4217 return reloc_class_plt;
4218 case R_386_COPY:
4219 return reloc_class_copy;
4220 default:
4221 return reloc_class_normal;
4222 }
4223 }
4224
4225 /* Finish up the dynamic sections. */
4226
4227 static bfd_boolean
4228 elf_i386_finish_dynamic_sections (bfd *output_bfd,
4229 struct bfd_link_info *info)
4230 {
4231 struct elf_i386_link_hash_table *htab;
4232 bfd *dynobj;
4233 asection *sdyn;
4234
4235 htab = elf_i386_hash_table (info);
4236 dynobj = htab->elf.dynobj;
4237 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4238
4239 if (htab->elf.dynamic_sections_created)
4240 {
4241 Elf32_External_Dyn *dyncon, *dynconend;
4242
4243 if (sdyn == NULL || htab->sgot == NULL)
4244 abort ();
4245
4246 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4247 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4248 for (; dyncon < dynconend; dyncon++)
4249 {
4250 Elf_Internal_Dyn dyn;
4251 asection *s;
4252
4253 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4254
4255 switch (dyn.d_tag)
4256 {
4257 default:
4258 if (htab->is_vxworks
4259 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
4260 break;
4261 continue;
4262
4263 case DT_PLTGOT:
4264 s = htab->sgotplt;
4265 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4266 break;
4267
4268 case DT_JMPREL:
4269 s = htab->srelplt;
4270 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4271 break;
4272
4273 case DT_PLTRELSZ:
4274 s = htab->srelplt;
4275 dyn.d_un.d_val = s->size;
4276 break;
4277
4278 case DT_RELSZ:
4279 /* My reading of the SVR4 ABI indicates that the
4280 procedure linkage table relocs (DT_JMPREL) should be
4281 included in the overall relocs (DT_REL). This is
4282 what Solaris does. However, UnixWare can not handle
4283 that case. Therefore, we override the DT_RELSZ entry
4284 here to make it not include the JMPREL relocs. */
4285 s = htab->srelplt;
4286 if (s == NULL)
4287 continue;
4288 dyn.d_un.d_val -= s->size;
4289 break;
4290
4291 case DT_REL:
4292 /* We may not be using the standard ELF linker script.
4293 If .rel.plt is the first .rel section, we adjust
4294 DT_REL to not include it. */
4295 s = htab->srelplt;
4296 if (s == NULL)
4297 continue;
4298 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4299 continue;
4300 dyn.d_un.d_ptr += s->size;
4301 break;
4302 }
4303
4304 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4305 }
4306
4307 /* Fill in the first entry in the procedure linkage table. */
4308 if (htab->splt && htab->splt->size > 0)
4309 {
4310 if (info->shared)
4311 {
4312 memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
4313 sizeof (elf_i386_pic_plt0_entry));
4314 memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
4315 htab->plt0_pad_byte,
4316 PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
4317 }
4318 else
4319 {
4320 memcpy (htab->splt->contents, elf_i386_plt0_entry,
4321 sizeof(elf_i386_plt0_entry));
4322 memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
4323 htab->plt0_pad_byte,
4324 PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
4325 bfd_put_32 (output_bfd,
4326 (htab->sgotplt->output_section->vma
4327 + htab->sgotplt->output_offset
4328 + 4),
4329 htab->splt->contents + 2);
4330 bfd_put_32 (output_bfd,
4331 (htab->sgotplt->output_section->vma
4332 + htab->sgotplt->output_offset
4333 + 8),
4334 htab->splt->contents + 8);
4335
4336 if (htab->is_vxworks)
4337 {
4338 Elf_Internal_Rela rel;
4339
4340 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
4341 On IA32 we use REL relocations so the addend goes in
4342 the PLT directly. */
4343 rel.r_offset = (htab->splt->output_section->vma
4344 + htab->splt->output_offset
4345 + 2);
4346 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4347 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4348 htab->srelplt2->contents);
4349 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
4350 rel.r_offset = (htab->splt->output_section->vma
4351 + htab->splt->output_offset
4352 + 8);
4353 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4354 bfd_elf32_swap_reloc_out (output_bfd, &rel,
4355 htab->srelplt2->contents +
4356 sizeof (Elf32_External_Rel));
4357 }
4358 }
4359
4360 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4361 really seem like the right value. */
4362 elf_section_data (htab->splt->output_section)
4363 ->this_hdr.sh_entsize = 4;
4364
4365 /* Correct the .rel.plt.unloaded relocations. */
4366 if (htab->is_vxworks && !info->shared)
4367 {
4368 int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
4369 unsigned char *p;
4370
4371 p = htab->srelplt2->contents;
4372 if (info->shared)
4373 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
4374 else
4375 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
4376
4377 for (; num_plts; num_plts--)
4378 {
4379 Elf_Internal_Rela rel;
4380 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4381 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32);
4382 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4383 p += sizeof (Elf32_External_Rel);
4384
4385 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
4386 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32);
4387 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
4388 p += sizeof (Elf32_External_Rel);
4389 }
4390 }
4391 }
4392 }
4393
4394 if (htab->sgotplt)
4395 {
4396 /* Fill in the first three entries in the global offset table. */
4397 if (htab->sgotplt->size > 0)
4398 {
4399 bfd_put_32 (output_bfd,
4400 (sdyn == NULL ? 0
4401 : sdyn->output_section->vma + sdyn->output_offset),
4402 htab->sgotplt->contents);
4403 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
4404 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
4405 }
4406
4407 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
4408 }
4409
4410 if (htab->sgot && htab->sgot->size > 0)
4411 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
4412
4413 return TRUE;
4414 }
4415
4416 /* Return address for Ith PLT stub in section PLT, for relocation REL
4417 or (bfd_vma) -1 if it should not be included. */
4418
4419 static bfd_vma
4420 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
4421 const arelent *rel ATTRIBUTE_UNUSED)
4422 {
4423 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4424 }
4425
4426 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4427
4428 static bfd_boolean
4429 elf_i386_hash_symbol (struct elf_link_hash_entry *h)
4430 {
4431 if (h->plt.offset != (bfd_vma) -1
4432 && !h->def_regular
4433 && !h->pointer_equality_needed)
4434 return FALSE;
4435
4436 return _bfd_elf_hash_symbol (h);
4437 }
4438
4439 /* Hook called by the linker routine which adds symbols from an object
4440 file. */
4441
4442 static bfd_boolean
4443 elf_i386_add_symbol_hook (bfd * abfd ATTRIBUTE_UNUSED,
4444 struct bfd_link_info * info ATTRIBUTE_UNUSED,
4445 Elf_Internal_Sym * sym,
4446 const char ** namep ATTRIBUTE_UNUSED,
4447 flagword * flagsp ATTRIBUTE_UNUSED,
4448 asection ** secp ATTRIBUTE_UNUSED,
4449 bfd_vma * valp ATTRIBUTE_UNUSED)
4450 {
4451 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4452 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4453
4454 return TRUE;
4455 }
4456
4457 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
4458 #define TARGET_LITTLE_NAME "elf32-i386"
4459 #define ELF_ARCH bfd_arch_i386
4460 #define ELF_MACHINE_CODE EM_386
4461 #define ELF_MAXPAGESIZE 0x1000
4462
4463 #define elf_backend_can_gc_sections 1
4464 #define elf_backend_can_refcount 1
4465 #define elf_backend_want_got_plt 1
4466 #define elf_backend_plt_readonly 1
4467 #define elf_backend_want_plt_sym 0
4468 #define elf_backend_got_header_size 12
4469
4470 /* Support RELA for objdump of prelink objects. */
4471 #define elf_info_to_howto elf_i386_info_to_howto_rel
4472 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
4473
4474 #define bfd_elf32_mkobject elf_i386_mkobject
4475
4476 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
4477 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
4478 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
4479 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup
4480
4481 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
4482 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4483 #define elf_backend_check_relocs elf_i386_check_relocs
4484 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
4485 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
4486 #define elf_backend_fake_sections elf_i386_fake_sections
4487 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
4488 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
4489 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
4490 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
4491 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
4492 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
4493 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
4494 #define elf_backend_relocate_section elf_i386_relocate_section
4495 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
4496 #define elf_backend_always_size_sections elf_i386_always_size_sections
4497 #define elf_backend_omit_section_dynsym \
4498 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4499 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
4500 #define elf_backend_hash_symbol elf_i386_hash_symbol
4501 #define elf_backend_add_symbol_hook elf_i386_add_symbol_hook
4502 #undef elf_backend_post_process_headers
4503 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4504
4505 #include "elf32-target.h"
4506
4507 /* FreeBSD support. */
4508
4509 #undef TARGET_LITTLE_SYM
4510 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
4511 #undef TARGET_LITTLE_NAME
4512 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
4513 #undef ELF_OSABI
4514 #define ELF_OSABI ELFOSABI_FREEBSD
4515
4516 /* The kernel recognizes executables as valid only if they carry a
4517 "FreeBSD" label in the ELF header. So we put this label on all
4518 executables and (for simplicity) also all other object files. */
4519
4520 static void
4521 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info)
4522 {
4523 _bfd_elf_set_osabi (abfd, info);
4524
4525 #ifdef OLD_FREEBSD_ABI_LABEL
4526 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
4527 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
4528 #endif
4529 }
4530
4531 #undef elf_backend_post_process_headers
4532 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers
4533 #undef elf32_bed
4534 #define elf32_bed elf32_i386_fbsd_bed
4535
4536 #undef elf_backend_add_symbol_hook
4537
4538 #include "elf32-target.h"
4539
4540 /* VxWorks support. */
4541
4542 #undef TARGET_LITTLE_SYM
4543 #define TARGET_LITTLE_SYM bfd_elf32_i386_vxworks_vec
4544 #undef TARGET_LITTLE_NAME
4545 #define TARGET_LITTLE_NAME "elf32-i386-vxworks"
4546 #undef ELF_OSABI
4547
4548 /* Like elf_i386_link_hash_table_create but with tweaks for VxWorks. */
4549
4550 static struct bfd_link_hash_table *
4551 elf_i386_vxworks_link_hash_table_create (bfd *abfd)
4552 {
4553 struct bfd_link_hash_table *ret;
4554 struct elf_i386_link_hash_table *htab;
4555
4556 ret = elf_i386_link_hash_table_create (abfd);
4557 if (ret)
4558 {
4559 htab = (struct elf_i386_link_hash_table *) ret;
4560 htab->is_vxworks = 1;
4561 htab->plt0_pad_byte = 0x90;
4562 }
4563
4564 return ret;
4565 }
4566
4567
4568 #undef elf_backend_relocs_compatible
4569 #undef elf_backend_post_process_headers
4570 #undef bfd_elf32_bfd_link_hash_table_create
4571 #define bfd_elf32_bfd_link_hash_table_create \
4572 elf_i386_vxworks_link_hash_table_create
4573 #undef elf_backend_add_symbol_hook
4574 #define elf_backend_add_symbol_hook \
4575 elf_vxworks_add_symbol_hook
4576 #undef elf_backend_link_output_symbol_hook
4577 #define elf_backend_link_output_symbol_hook \
4578 elf_vxworks_link_output_symbol_hook
4579 #undef elf_backend_emit_relocs
4580 #define elf_backend_emit_relocs elf_vxworks_emit_relocs
4581 #undef elf_backend_final_write_processing
4582 #define elf_backend_final_write_processing \
4583 elf_vxworks_final_write_processing
4584
4585 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
4586 define it. */
4587 #undef elf_backend_want_plt_sym
4588 #define elf_backend_want_plt_sym 1
4589
4590 #undef elf32_bed
4591 #define elf32_bed elf32_i386_vxworks_bed
4592
4593 #include "elf32-target.h"