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