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