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