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