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