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