2005-05-05 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
1 /* Intel 80386/80486-specific support for 32-bit ELF
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005 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 2 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, MA 02110-1301, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26
27 /* 386 uses REL relocations instead of RELA. */
28 #define USE_REL 1
29
30 #include "elf/i386.h"
31
32 static reloc_howto_type elf_howto_table[]=
33 {
34 HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield,
35 bfd_elf_generic_reloc, "R_386_NONE",
36 TRUE, 0x00000000, 0x00000000, FALSE),
37 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
38 bfd_elf_generic_reloc, "R_386_32",
39 TRUE, 0xffffffff, 0xffffffff, FALSE),
40 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
41 bfd_elf_generic_reloc, "R_386_PC32",
42 TRUE, 0xffffffff, 0xffffffff, TRUE),
43 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_386_GOT32",
45 TRUE, 0xffffffff, 0xffffffff, FALSE),
46 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
47 bfd_elf_generic_reloc, "R_386_PLT32",
48 TRUE, 0xffffffff, 0xffffffff, TRUE),
49 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
50 bfd_elf_generic_reloc, "R_386_COPY",
51 TRUE, 0xffffffff, 0xffffffff, FALSE),
52 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
53 bfd_elf_generic_reloc, "R_386_GLOB_DAT",
54 TRUE, 0xffffffff, 0xffffffff, FALSE),
55 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_386_JUMP_SLOT",
57 TRUE, 0xffffffff, 0xffffffff, FALSE),
58 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_386_RELATIVE",
60 TRUE, 0xffffffff, 0xffffffff, FALSE),
61 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_386_GOTOFF",
63 TRUE, 0xffffffff, 0xffffffff, FALSE),
64 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_386_GOTPC",
66 TRUE, 0xffffffff, 0xffffffff, TRUE),
67
68 /* We have a gap in the reloc numbers here.
69 R_386_standard counts the number up to this point, and
70 R_386_ext_offset is the value to subtract from a reloc type of
71 R_386_16 thru R_386_PC8 to form an index into this table. */
72 #define R_386_standard (R_386_GOTPC + 1)
73 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard)
74
75 /* These relocs are a GNU extension. */
76 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_386_TLS_TPOFF",
78 TRUE, 0xffffffff, 0xffffffff, FALSE),
79 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
80 bfd_elf_generic_reloc, "R_386_TLS_IE",
81 TRUE, 0xffffffff, 0xffffffff, FALSE),
82 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_386_TLS_GOTIE",
84 TRUE, 0xffffffff, 0xffffffff, FALSE),
85 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_386_TLS_LE",
87 TRUE, 0xffffffff, 0xffffffff, FALSE),
88 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_386_TLS_GD",
90 TRUE, 0xffffffff, 0xffffffff, FALSE),
91 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
92 bfd_elf_generic_reloc, "R_386_TLS_LDM",
93 TRUE, 0xffffffff, 0xffffffff, FALSE),
94 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_386_16",
96 TRUE, 0xffff, 0xffff, FALSE),
97 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_386_PC16",
99 TRUE, 0xffff, 0xffff, TRUE),
100 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_386_8",
102 TRUE, 0xff, 0xff, FALSE),
103 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_386_PC8",
105 TRUE, 0xff, 0xff, TRUE),
106
107 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset)
108 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext)
109 /* These are common with Solaris TLS implementation. */
110 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
111 bfd_elf_generic_reloc, "R_386_TLS_LDO_32",
112 TRUE, 0xffffffff, 0xffffffff, FALSE),
113 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
114 bfd_elf_generic_reloc, "R_386_TLS_IE_32",
115 TRUE, 0xffffffff, 0xffffffff, FALSE),
116 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
117 bfd_elf_generic_reloc, "R_386_TLS_LE_32",
118 TRUE, 0xffffffff, 0xffffffff, FALSE),
119 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
120 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32",
121 TRUE, 0xffffffff, 0xffffffff, FALSE),
122 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
123 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32",
124 TRUE, 0xffffffff, 0xffffffff, FALSE),
125 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
126 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32",
127 TRUE, 0xffffffff, 0xffffffff, FALSE),
128
129 /* Another gap. */
130 #define R_386_tls (R_386_TLS_TPOFF32 + 1 - R_386_tls_offset)
131 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_tls)
132
133 /* GNU extension to record C++ vtable hierarchy. */
134 HOWTO (R_386_GNU_VTINHERIT, /* type */
135 0, /* rightshift */
136 2, /* size (0 = byte, 1 = short, 2 = long) */
137 0, /* bitsize */
138 FALSE, /* pc_relative */
139 0, /* bitpos */
140 complain_overflow_dont, /* complain_on_overflow */
141 NULL, /* special_function */
142 "R_386_GNU_VTINHERIT", /* name */
143 FALSE, /* partial_inplace */
144 0, /* src_mask */
145 0, /* dst_mask */
146 FALSE), /* pcrel_offset */
147
148 /* GNU extension to record C++ vtable member usage. */
149 HOWTO (R_386_GNU_VTENTRY, /* type */
150 0, /* rightshift */
151 2, /* size (0 = byte, 1 = short, 2 = long) */
152 0, /* bitsize */
153 FALSE, /* pc_relative */
154 0, /* bitpos */
155 complain_overflow_dont, /* complain_on_overflow */
156 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
157 "R_386_GNU_VTENTRY", /* name */
158 FALSE, /* partial_inplace */
159 0, /* src_mask */
160 0, /* dst_mask */
161 FALSE) /* pcrel_offset */
162
163 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
164
165 };
166
167 #ifdef DEBUG_GEN_RELOC
168 #define TRACE(str) \
169 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
170 #else
171 #define TRACE(str)
172 #endif
173
174 static reloc_howto_type *
175 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
176 bfd_reloc_code_real_type code)
177 {
178 switch (code)
179 {
180 case BFD_RELOC_NONE:
181 TRACE ("BFD_RELOC_NONE");
182 return &elf_howto_table[R_386_NONE];
183
184 case BFD_RELOC_32:
185 TRACE ("BFD_RELOC_32");
186 return &elf_howto_table[R_386_32];
187
188 case BFD_RELOC_CTOR:
189 TRACE ("BFD_RELOC_CTOR");
190 return &elf_howto_table[R_386_32];
191
192 case BFD_RELOC_32_PCREL:
193 TRACE ("BFD_RELOC_PC32");
194 return &elf_howto_table[R_386_PC32];
195
196 case BFD_RELOC_386_GOT32:
197 TRACE ("BFD_RELOC_386_GOT32");
198 return &elf_howto_table[R_386_GOT32];
199
200 case BFD_RELOC_386_PLT32:
201 TRACE ("BFD_RELOC_386_PLT32");
202 return &elf_howto_table[R_386_PLT32];
203
204 case BFD_RELOC_386_COPY:
205 TRACE ("BFD_RELOC_386_COPY");
206 return &elf_howto_table[R_386_COPY];
207
208 case BFD_RELOC_386_GLOB_DAT:
209 TRACE ("BFD_RELOC_386_GLOB_DAT");
210 return &elf_howto_table[R_386_GLOB_DAT];
211
212 case BFD_RELOC_386_JUMP_SLOT:
213 TRACE ("BFD_RELOC_386_JUMP_SLOT");
214 return &elf_howto_table[R_386_JUMP_SLOT];
215
216 case BFD_RELOC_386_RELATIVE:
217 TRACE ("BFD_RELOC_386_RELATIVE");
218 return &elf_howto_table[R_386_RELATIVE];
219
220 case BFD_RELOC_386_GOTOFF:
221 TRACE ("BFD_RELOC_386_GOTOFF");
222 return &elf_howto_table[R_386_GOTOFF];
223
224 case BFD_RELOC_386_GOTPC:
225 TRACE ("BFD_RELOC_386_GOTPC");
226 return &elf_howto_table[R_386_GOTPC];
227
228 /* These relocs are a GNU extension. */
229 case BFD_RELOC_386_TLS_TPOFF:
230 TRACE ("BFD_RELOC_386_TLS_TPOFF");
231 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset];
232
233 case BFD_RELOC_386_TLS_IE:
234 TRACE ("BFD_RELOC_386_TLS_IE");
235 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset];
236
237 case BFD_RELOC_386_TLS_GOTIE:
238 TRACE ("BFD_RELOC_386_TLS_GOTIE");
239 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset];
240
241 case BFD_RELOC_386_TLS_LE:
242 TRACE ("BFD_RELOC_386_TLS_LE");
243 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset];
244
245 case BFD_RELOC_386_TLS_GD:
246 TRACE ("BFD_RELOC_386_TLS_GD");
247 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset];
248
249 case BFD_RELOC_386_TLS_LDM:
250 TRACE ("BFD_RELOC_386_TLS_LDM");
251 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset];
252
253 case BFD_RELOC_16:
254 TRACE ("BFD_RELOC_16");
255 return &elf_howto_table[R_386_16 - R_386_ext_offset];
256
257 case BFD_RELOC_16_PCREL:
258 TRACE ("BFD_RELOC_16_PCREL");
259 return &elf_howto_table[R_386_PC16 - R_386_ext_offset];
260
261 case BFD_RELOC_8:
262 TRACE ("BFD_RELOC_8");
263 return &elf_howto_table[R_386_8 - R_386_ext_offset];
264
265 case BFD_RELOC_8_PCREL:
266 TRACE ("BFD_RELOC_8_PCREL");
267 return &elf_howto_table[R_386_PC8 - R_386_ext_offset];
268
269 /* Common with Sun TLS implementation. */
270 case BFD_RELOC_386_TLS_LDO_32:
271 TRACE ("BFD_RELOC_386_TLS_LDO_32");
272 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset];
273
274 case BFD_RELOC_386_TLS_IE_32:
275 TRACE ("BFD_RELOC_386_TLS_IE_32");
276 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset];
277
278 case BFD_RELOC_386_TLS_LE_32:
279 TRACE ("BFD_RELOC_386_TLS_LE_32");
280 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset];
281
282 case BFD_RELOC_386_TLS_DTPMOD32:
283 TRACE ("BFD_RELOC_386_TLS_DTPMOD32");
284 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset];
285
286 case BFD_RELOC_386_TLS_DTPOFF32:
287 TRACE ("BFD_RELOC_386_TLS_DTPOFF32");
288 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset];
289
290 case BFD_RELOC_386_TLS_TPOFF32:
291 TRACE ("BFD_RELOC_386_TLS_TPOFF32");
292 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset];
293
294 case BFD_RELOC_VTABLE_INHERIT:
295 TRACE ("BFD_RELOC_VTABLE_INHERIT");
296 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset];
297
298 case BFD_RELOC_VTABLE_ENTRY:
299 TRACE ("BFD_RELOC_VTABLE_ENTRY");
300 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset];
301
302 default:
303 break;
304 }
305
306 TRACE ("Unknown");
307 return 0;
308 }
309
310 static void
311 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
312 arelent *cache_ptr,
313 Elf_Internal_Rela *dst)
314 {
315 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
316 unsigned int indx;
317
318 if ((indx = r_type) >= R_386_standard
319 && ((indx = r_type - R_386_ext_offset) - R_386_standard
320 >= R_386_ext - R_386_standard)
321 && ((indx = r_type - R_386_tls_offset) - R_386_ext
322 >= R_386_tls - R_386_ext)
323 && ((indx = r_type - R_386_vt_offset) - R_386_tls
324 >= R_386_vt - R_386_tls))
325 {
326 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
327 abfd, (int) r_type);
328 indx = R_386_NONE;
329 }
330 cache_ptr->howto = &elf_howto_table[indx];
331 }
332
333 /* Return whether a symbol name implies a local label. The UnixWare
334 2.1 cc generates temporary symbols that start with .X, so we
335 recognize them here. FIXME: do other SVR4 compilers also use .X?.
336 If so, we should move the .X recognition into
337 _bfd_elf_is_local_label_name. */
338
339 static bfd_boolean
340 elf_i386_is_local_label_name (bfd *abfd, const char *name)
341 {
342 if (name[0] == '.' && name[1] == 'X')
343 return TRUE;
344
345 return _bfd_elf_is_local_label_name (abfd, name);
346 }
347 \f
348 /* Support for core dump NOTE sections. */
349
350 static bfd_boolean
351 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
352 {
353 int offset;
354 size_t size;
355
356 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
357 {
358 int pr_version = bfd_get_32 (abfd, note->descdata);
359
360 if (pr_version != 1)
361 return FALSE;
362
363 /* pr_cursig */
364 elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
365
366 /* pr_pid */
367 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
368
369 /* pr_reg */
370 offset = 28;
371 size = bfd_get_32 (abfd, note->descdata + 8);
372 }
373 else
374 {
375 switch (note->descsz)
376 {
377 default:
378 return FALSE;
379
380 case 144: /* Linux/i386 */
381 /* pr_cursig */
382 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
383
384 /* pr_pid */
385 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
386
387 /* pr_reg */
388 offset = 72;
389 size = 68;
390
391 break;
392 }
393 }
394
395 /* Make a ".reg/999" section. */
396 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
397 size, note->descpos + offset);
398 }
399
400 static bfd_boolean
401 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
402 {
403 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
404 {
405 int pr_version = bfd_get_32 (abfd, note->descdata);
406
407 if (pr_version != 1)
408 return FALSE;
409
410 elf_tdata (abfd)->core_program
411 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
412 elf_tdata (abfd)->core_command
413 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
414 }
415 else
416 {
417 switch (note->descsz)
418 {
419 default:
420 return FALSE;
421
422 case 124: /* Linux/i386 elf_prpsinfo. */
423 elf_tdata (abfd)->core_program
424 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
425 elf_tdata (abfd)->core_command
426 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
427 }
428 }
429
430 /* Note that for some reason, a spurious space is tacked
431 onto the end of the args in some (at least one anyway)
432 implementations, so strip it off if it exists. */
433 {
434 char *command = elf_tdata (abfd)->core_command;
435 int n = strlen (command);
436
437 if (0 < n && command[n - 1] == ' ')
438 command[n - 1] = '\0';
439 }
440
441 return TRUE;
442 }
443 \f
444 /* Functions for the i386 ELF linker.
445
446 In order to gain some understanding of code in this file without
447 knowing all the intricate details of the linker, note the
448 following:
449
450 Functions named elf_i386_* are called by external routines, other
451 functions are only called locally. elf_i386_* functions appear
452 in this file more or less in the order in which they are called
453 from external routines. eg. elf_i386_check_relocs is called
454 early in the link process, elf_i386_finish_dynamic_sections is
455 one of the last functions. */
456
457
458 /* The name of the dynamic interpreter. This is put in the .interp
459 section. */
460
461 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
462
463 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
464 copying dynamic variables from a shared lib into an app's dynbss
465 section, and instead use a dynamic relocation to point into the
466 shared lib. */
467 #define ELIMINATE_COPY_RELOCS 1
468
469 /* The size in bytes of an entry in the procedure linkage table. */
470
471 #define PLT_ENTRY_SIZE 16
472
473 /* The first entry in an absolute procedure linkage table looks like
474 this. See the SVR4 ABI i386 supplement to see how this works. */
475
476 static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
477 {
478 0xff, 0x35, /* pushl contents of address */
479 0, 0, 0, 0, /* replaced with address of .got + 4. */
480 0xff, 0x25, /* jmp indirect */
481 0, 0, 0, 0, /* replaced with address of .got + 8. */
482 0, 0, 0, 0 /* pad out to 16 bytes. */
483 };
484
485 /* Subsequent entries in an absolute procedure linkage table look like
486 this. */
487
488 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
489 {
490 0xff, 0x25, /* jmp indirect */
491 0, 0, 0, 0, /* replaced with address of this symbol in .got. */
492 0x68, /* pushl immediate */
493 0, 0, 0, 0, /* replaced with offset into relocation table. */
494 0xe9, /* jmp relative */
495 0, 0, 0, 0 /* replaced with offset to start of .plt. */
496 };
497
498 /* The first entry in a PIC procedure linkage table look like this. */
499
500 static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
501 {
502 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */
503 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */
504 0, 0, 0, 0 /* pad out to 16 bytes. */
505 };
506
507 /* Subsequent entries in a PIC procedure linkage table look like this. */
508
509 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
510 {
511 0xff, 0xa3, /* jmp *offset(%ebx) */
512 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */
513 0x68, /* pushl immediate */
514 0, 0, 0, 0, /* replaced with offset into relocation table. */
515 0xe9, /* jmp relative */
516 0, 0, 0, 0 /* replaced with offset to start of .plt. */
517 };
518
519 /* The i386 linker needs to keep track of the number of relocs that it
520 decides to copy as dynamic relocs in check_relocs for each symbol.
521 This is so that it can later discard them if they are found to be
522 unnecessary. We store the information in a field extending the
523 regular ELF linker hash table. */
524
525 struct elf_i386_dyn_relocs
526 {
527 struct elf_i386_dyn_relocs *next;
528
529 /* The input section of the reloc. */
530 asection *sec;
531
532 /* Total number of relocs copied for the input section. */
533 bfd_size_type count;
534
535 /* Number of pc-relative relocs copied for the input section. */
536 bfd_size_type pc_count;
537 };
538
539 /* i386 ELF linker hash entry. */
540
541 struct elf_i386_link_hash_entry
542 {
543 struct elf_link_hash_entry elf;
544
545 /* Track dynamic relocs copied for this symbol. */
546 struct elf_i386_dyn_relocs *dyn_relocs;
547
548 #define GOT_UNKNOWN 0
549 #define GOT_NORMAL 1
550 #define GOT_TLS_GD 2
551 #define GOT_TLS_IE 4
552 #define GOT_TLS_IE_POS 5
553 #define GOT_TLS_IE_NEG 6
554 #define GOT_TLS_IE_BOTH 7
555 unsigned char tls_type;
556 };
557
558 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
559
560 struct elf_i386_obj_tdata
561 {
562 struct elf_obj_tdata root;
563
564 /* tls_type for each local got entry. */
565 char *local_got_tls_type;
566 };
567
568 #define elf_i386_tdata(abfd) \
569 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
570
571 #define elf_i386_local_got_tls_type(abfd) \
572 (elf_i386_tdata (abfd)->local_got_tls_type)
573
574 static bfd_boolean
575 elf_i386_mkobject (bfd *abfd)
576 {
577 bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
578 abfd->tdata.any = bfd_zalloc (abfd, amt);
579 if (abfd->tdata.any == NULL)
580 return FALSE;
581 return TRUE;
582 }
583
584 /* i386 ELF linker hash table. */
585
586 struct elf_i386_link_hash_table
587 {
588 struct elf_link_hash_table elf;
589
590 /* Short-cuts to get to dynamic linker sections. */
591 asection *sgot;
592 asection *sgotplt;
593 asection *srelgot;
594 asection *splt;
595 asection *srelplt;
596 asection *sdynbss;
597 asection *srelbss;
598
599 union {
600 bfd_signed_vma refcount;
601 bfd_vma offset;
602 } tls_ldm_got;
603
604 /* Small local sym to section mapping cache. */
605 struct sym_sec_cache sym_sec;
606 };
607
608 /* Get the i386 ELF linker hash table from a link_info structure. */
609
610 #define elf_i386_hash_table(p) \
611 ((struct elf_i386_link_hash_table *) ((p)->hash))
612
613 /* Create an entry in an i386 ELF linker hash table. */
614
615 static struct bfd_hash_entry *
616 link_hash_newfunc (struct bfd_hash_entry *entry,
617 struct bfd_hash_table *table,
618 const char *string)
619 {
620 /* Allocate the structure if it has not already been allocated by a
621 subclass. */
622 if (entry == NULL)
623 {
624 entry = bfd_hash_allocate (table,
625 sizeof (struct elf_i386_link_hash_entry));
626 if (entry == NULL)
627 return entry;
628 }
629
630 /* Call the allocation method of the superclass. */
631 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
632 if (entry != NULL)
633 {
634 struct elf_i386_link_hash_entry *eh;
635
636 eh = (struct elf_i386_link_hash_entry *) entry;
637 eh->dyn_relocs = NULL;
638 eh->tls_type = GOT_UNKNOWN;
639 }
640
641 return entry;
642 }
643
644 /* Create an i386 ELF linker hash table. */
645
646 static struct bfd_link_hash_table *
647 elf_i386_link_hash_table_create (bfd *abfd)
648 {
649 struct elf_i386_link_hash_table *ret;
650 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table);
651
652 ret = bfd_malloc (amt);
653 if (ret == NULL)
654 return NULL;
655
656 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
657 {
658 free (ret);
659 return NULL;
660 }
661
662 ret->sgot = NULL;
663 ret->sgotplt = NULL;
664 ret->srelgot = NULL;
665 ret->splt = NULL;
666 ret->srelplt = NULL;
667 ret->sdynbss = NULL;
668 ret->srelbss = NULL;
669 ret->tls_ldm_got.refcount = 0;
670 ret->sym_sec.abfd = NULL;
671
672 return &ret->elf.root;
673 }
674
675 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
676 shortcuts to them in our hash table. */
677
678 static bfd_boolean
679 create_got_section (bfd *dynobj, struct bfd_link_info *info)
680 {
681 struct elf_i386_link_hash_table *htab;
682
683 if (! _bfd_elf_create_got_section (dynobj, info))
684 return FALSE;
685
686 htab = elf_i386_hash_table (info);
687 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
688 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
689 if (!htab->sgot || !htab->sgotplt)
690 abort ();
691
692 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
693 (SEC_ALLOC | SEC_LOAD
694 | SEC_HAS_CONTENTS
695 | SEC_IN_MEMORY
696 | SEC_LINKER_CREATED
697 | SEC_READONLY));
698 if (htab->srelgot == NULL
699 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
700 return FALSE;
701 return TRUE;
702 }
703
704 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and
705 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our
706 hash table. */
707
708 static bfd_boolean
709 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
710 {
711 struct elf_i386_link_hash_table *htab;
712
713 htab = elf_i386_hash_table (info);
714 if (!htab->sgot && !create_got_section (dynobj, info))
715 return FALSE;
716
717 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
718 return FALSE;
719
720 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
721 htab->srelplt = bfd_get_section_by_name (dynobj, ".rel.plt");
722 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
723 if (!info->shared)
724 htab->srelbss = bfd_get_section_by_name (dynobj, ".rel.bss");
725
726 if (!htab->splt || !htab->srelplt || !htab->sdynbss
727 || (!info->shared && !htab->srelbss))
728 abort ();
729
730 return TRUE;
731 }
732
733 /* Copy the extra info we tack onto an elf_link_hash_entry. */
734
735 static void
736 elf_i386_copy_indirect_symbol (const struct elf_backend_data *bed,
737 struct elf_link_hash_entry *dir,
738 struct elf_link_hash_entry *ind)
739 {
740 struct elf_i386_link_hash_entry *edir, *eind;
741
742 edir = (struct elf_i386_link_hash_entry *) dir;
743 eind = (struct elf_i386_link_hash_entry *) ind;
744
745 if (eind->dyn_relocs != NULL)
746 {
747 if (edir->dyn_relocs != NULL)
748 {
749 struct elf_i386_dyn_relocs **pp;
750 struct elf_i386_dyn_relocs *p;
751
752 if (ind->root.type == bfd_link_hash_indirect)
753 abort ();
754
755 /* Add reloc counts against the weak sym to the strong sym
756 list. Merge any entries against the same section. */
757 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
758 {
759 struct elf_i386_dyn_relocs *q;
760
761 for (q = edir->dyn_relocs; q != NULL; q = q->next)
762 if (q->sec == p->sec)
763 {
764 q->pc_count += p->pc_count;
765 q->count += p->count;
766 *pp = p->next;
767 break;
768 }
769 if (q == NULL)
770 pp = &p->next;
771 }
772 *pp = edir->dyn_relocs;
773 }
774
775 edir->dyn_relocs = eind->dyn_relocs;
776 eind->dyn_relocs = NULL;
777 }
778
779 if (ind->root.type == bfd_link_hash_indirect
780 && dir->got.refcount <= 0)
781 {
782 edir->tls_type = eind->tls_type;
783 eind->tls_type = GOT_UNKNOWN;
784 }
785
786 if (ELIMINATE_COPY_RELOCS
787 && ind->root.type != bfd_link_hash_indirect
788 && dir->dynamic_adjusted)
789 {
790 /* If called to transfer flags for a weakdef during processing
791 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
792 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
793 dir->ref_dynamic |= ind->ref_dynamic;
794 dir->ref_regular |= ind->ref_regular;
795 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
796 dir->needs_plt |= ind->needs_plt;
797 dir->pointer_equality_needed |= ind->pointer_equality_needed;
798 }
799 else
800 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
801 }
802
803 static int
804 elf_i386_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
805 {
806 if (info->shared)
807 return r_type;
808
809 switch (r_type)
810 {
811 case R_386_TLS_GD:
812 case R_386_TLS_IE_32:
813 if (is_local)
814 return R_386_TLS_LE_32;
815 return R_386_TLS_IE_32;
816 case R_386_TLS_IE:
817 case R_386_TLS_GOTIE:
818 if (is_local)
819 return R_386_TLS_LE_32;
820 return r_type;
821 case R_386_TLS_LDM:
822 return R_386_TLS_LE_32;
823 }
824
825 return r_type;
826 }
827
828 /* Look through the relocs for a section during the first phase, and
829 calculate needed space in the global offset table, procedure linkage
830 table, and dynamic reloc sections. */
831
832 static bfd_boolean
833 elf_i386_check_relocs (bfd *abfd,
834 struct bfd_link_info *info,
835 asection *sec,
836 const Elf_Internal_Rela *relocs)
837 {
838 struct elf_i386_link_hash_table *htab;
839 Elf_Internal_Shdr *symtab_hdr;
840 struct elf_link_hash_entry **sym_hashes;
841 const Elf_Internal_Rela *rel;
842 const Elf_Internal_Rela *rel_end;
843 asection *sreloc;
844
845 if (info->relocatable)
846 return TRUE;
847
848 htab = elf_i386_hash_table (info);
849 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
850 sym_hashes = elf_sym_hashes (abfd);
851
852 sreloc = NULL;
853
854 rel_end = relocs + sec->reloc_count;
855 for (rel = relocs; rel < rel_end; rel++)
856 {
857 unsigned int r_type;
858 unsigned long r_symndx;
859 struct elf_link_hash_entry *h;
860
861 r_symndx = ELF32_R_SYM (rel->r_info);
862 r_type = ELF32_R_TYPE (rel->r_info);
863
864 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
865 {
866 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
867 abfd,
868 r_symndx);
869 return FALSE;
870 }
871
872 if (r_symndx < symtab_hdr->sh_info)
873 h = NULL;
874 else
875 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
876
877 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
878
879 switch (r_type)
880 {
881 case R_386_TLS_LDM:
882 htab->tls_ldm_got.refcount += 1;
883 goto create_got;
884
885 case R_386_PLT32:
886 /* This symbol requires a procedure linkage table entry. We
887 actually build the entry in adjust_dynamic_symbol,
888 because this might be a case of linking PIC code which is
889 never referenced by a dynamic object, in which case we
890 don't need to generate a procedure linkage table entry
891 after all. */
892
893 /* If this is a local symbol, we resolve it directly without
894 creating a procedure linkage table entry. */
895 if (h == NULL)
896 continue;
897
898 h->needs_plt = 1;
899 h->plt.refcount += 1;
900 break;
901
902 case R_386_TLS_IE_32:
903 case R_386_TLS_IE:
904 case R_386_TLS_GOTIE:
905 if (info->shared)
906 info->flags |= DF_STATIC_TLS;
907 /* Fall through */
908
909 case R_386_GOT32:
910 case R_386_TLS_GD:
911 /* This symbol requires a global offset table entry. */
912 {
913 int tls_type, old_tls_type;
914
915 switch (r_type)
916 {
917 default:
918 case R_386_GOT32: tls_type = GOT_NORMAL; break;
919 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break;
920 case R_386_TLS_IE_32:
921 if (ELF32_R_TYPE (rel->r_info) == r_type)
922 tls_type = GOT_TLS_IE_NEG;
923 else
924 /* If this is a GD->IE transition, we may use either of
925 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */
926 tls_type = GOT_TLS_IE;
927 break;
928 case R_386_TLS_IE:
929 case R_386_TLS_GOTIE:
930 tls_type = GOT_TLS_IE_POS; break;
931 }
932
933 if (h != NULL)
934 {
935 h->got.refcount += 1;
936 old_tls_type = elf_i386_hash_entry(h)->tls_type;
937 }
938 else
939 {
940 bfd_signed_vma *local_got_refcounts;
941
942 /* This is a global offset table entry for a local symbol. */
943 local_got_refcounts = elf_local_got_refcounts (abfd);
944 if (local_got_refcounts == NULL)
945 {
946 bfd_size_type size;
947
948 size = symtab_hdr->sh_info;
949 size *= (sizeof (bfd_signed_vma) + sizeof(char));
950 local_got_refcounts = bfd_zalloc (abfd, size);
951 if (local_got_refcounts == NULL)
952 return FALSE;
953 elf_local_got_refcounts (abfd) = local_got_refcounts;
954 elf_i386_local_got_tls_type (abfd)
955 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
956 }
957 local_got_refcounts[r_symndx] += 1;
958 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx];
959 }
960
961 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE))
962 tls_type |= old_tls_type;
963 /* If a TLS symbol is accessed using IE at least once,
964 there is no point to use dynamic model for it. */
965 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
966 && (old_tls_type != GOT_TLS_GD
967 || (tls_type & GOT_TLS_IE) == 0))
968 {
969 if ((old_tls_type & GOT_TLS_IE) && tls_type == GOT_TLS_GD)
970 tls_type = old_tls_type;
971 else
972 {
973 (*_bfd_error_handler)
974 (_("%B: `%s' accessed both as normal and "
975 "thread local symbol"),
976 abfd,
977 h ? h->root.root.string : "<local>");
978 return FALSE;
979 }
980 }
981
982 if (old_tls_type != tls_type)
983 {
984 if (h != NULL)
985 elf_i386_hash_entry (h)->tls_type = tls_type;
986 else
987 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type;
988 }
989 }
990 /* Fall through */
991
992 case R_386_GOTOFF:
993 case R_386_GOTPC:
994 create_got:
995 if (htab->sgot == NULL)
996 {
997 if (htab->elf.dynobj == NULL)
998 htab->elf.dynobj = abfd;
999 if (!create_got_section (htab->elf.dynobj, info))
1000 return FALSE;
1001 }
1002 if (r_type != R_386_TLS_IE)
1003 break;
1004 /* Fall through */
1005
1006 case R_386_TLS_LE_32:
1007 case R_386_TLS_LE:
1008 if (!info->shared)
1009 break;
1010 info->flags |= DF_STATIC_TLS;
1011 /* Fall through */
1012
1013 case R_386_32:
1014 case R_386_PC32:
1015 if (h != NULL && !info->shared)
1016 {
1017 /* If this reloc is in a read-only section, we might
1018 need a copy reloc. We can't check reliably at this
1019 stage whether the section is read-only, as input
1020 sections have not yet been mapped to output sections.
1021 Tentatively set the flag for now, and correct in
1022 adjust_dynamic_symbol. */
1023 h->non_got_ref = 1;
1024
1025 /* We may need a .plt entry if the function this reloc
1026 refers to is in a shared lib. */
1027 h->plt.refcount += 1;
1028 if (r_type != R_386_PC32)
1029 h->pointer_equality_needed = 1;
1030 }
1031
1032 /* If we are creating a shared library, and this is a reloc
1033 against a global symbol, or a non PC relative reloc
1034 against a local symbol, then we need to copy the reloc
1035 into the shared library. However, if we are linking with
1036 -Bsymbolic, we do not need to copy a reloc against a
1037 global symbol which is defined in an object we are
1038 including in the link (i.e., DEF_REGULAR is set). At
1039 this point we have not seen all the input files, so it is
1040 possible that DEF_REGULAR is not set now but will be set
1041 later (it is never cleared). In case of a weak definition,
1042 DEF_REGULAR may be cleared later by a strong definition in
1043 a shared library. We account for that possibility below by
1044 storing information in the relocs_copied field of the hash
1045 table entry. A similar situation occurs when creating
1046 shared libraries and symbol visibility changes render the
1047 symbol local.
1048
1049 If on the other hand, we are creating an executable, we
1050 may need to keep relocations for symbols satisfied by a
1051 dynamic library if we manage to avoid copy relocs for the
1052 symbol. */
1053 if ((info->shared
1054 && (sec->flags & SEC_ALLOC) != 0
1055 && (r_type != R_386_PC32
1056 || (h != NULL
1057 && (! info->symbolic
1058 || h->root.type == bfd_link_hash_defweak
1059 || !h->def_regular))))
1060 || (ELIMINATE_COPY_RELOCS
1061 && !info->shared
1062 && (sec->flags & SEC_ALLOC) != 0
1063 && h != NULL
1064 && (h->root.type == bfd_link_hash_defweak
1065 || !h->def_regular)))
1066 {
1067 struct elf_i386_dyn_relocs *p;
1068 struct elf_i386_dyn_relocs **head;
1069
1070 /* We must copy these reloc types into the output file.
1071 Create a reloc section in dynobj and make room for
1072 this reloc. */
1073 if (sreloc == NULL)
1074 {
1075 const char *name;
1076 bfd *dynobj;
1077 unsigned int strndx = elf_elfheader (abfd)->e_shstrndx;
1078 unsigned int shnam = elf_section_data (sec)->rel_hdr.sh_name;
1079
1080 name = bfd_elf_string_from_elf_section (abfd, strndx, shnam);
1081 if (name == NULL)
1082 return FALSE;
1083
1084 if (strncmp (name, ".rel", 4) != 0
1085 || strcmp (bfd_get_section_name (abfd, sec),
1086 name + 4) != 0)
1087 {
1088 (*_bfd_error_handler)
1089 (_("%B: bad relocation section name `%s\'"),
1090 abfd, name);
1091 }
1092
1093 if (htab->elf.dynobj == NULL)
1094 htab->elf.dynobj = abfd;
1095
1096 dynobj = htab->elf.dynobj;
1097 sreloc = bfd_get_section_by_name (dynobj, name);
1098 if (sreloc == NULL)
1099 {
1100 flagword flags;
1101
1102 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1103 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1104 if ((sec->flags & SEC_ALLOC) != 0)
1105 flags |= SEC_ALLOC | SEC_LOAD;
1106 sreloc = bfd_make_section_with_flags (dynobj,
1107 name,
1108 flags);
1109 if (sreloc == NULL
1110 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
1111 return FALSE;
1112 }
1113 elf_section_data (sec)->sreloc = sreloc;
1114 }
1115
1116 /* If this is a global symbol, we count the number of
1117 relocations we need for this symbol. */
1118 if (h != NULL)
1119 {
1120 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs;
1121 }
1122 else
1123 {
1124 /* Track dynamic relocs needed for local syms too.
1125 We really need local syms available to do this
1126 easily. Oh well. */
1127
1128 asection *s;
1129 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1130 sec, r_symndx);
1131 if (s == NULL)
1132 return FALSE;
1133
1134 head = ((struct elf_i386_dyn_relocs **)
1135 &elf_section_data (s)->local_dynrel);
1136 }
1137
1138 p = *head;
1139 if (p == NULL || p->sec != sec)
1140 {
1141 bfd_size_type amt = sizeof *p;
1142 p = bfd_alloc (htab->elf.dynobj, amt);
1143 if (p == NULL)
1144 return FALSE;
1145 p->next = *head;
1146 *head = p;
1147 p->sec = sec;
1148 p->count = 0;
1149 p->pc_count = 0;
1150 }
1151
1152 p->count += 1;
1153 if (r_type == R_386_PC32)
1154 p->pc_count += 1;
1155 }
1156 break;
1157
1158 /* This relocation describes the C++ object vtable hierarchy.
1159 Reconstruct it for later use during GC. */
1160 case R_386_GNU_VTINHERIT:
1161 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1162 return FALSE;
1163 break;
1164
1165 /* This relocation describes which C++ vtable entries are actually
1166 used. Record for later use during GC. */
1167 case R_386_GNU_VTENTRY:
1168 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
1169 return FALSE;
1170 break;
1171
1172 default:
1173 break;
1174 }
1175 }
1176
1177 return TRUE;
1178 }
1179
1180 /* Return the section that should be marked against GC for a given
1181 relocation. */
1182
1183 static asection *
1184 elf_i386_gc_mark_hook (asection *sec,
1185 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1186 Elf_Internal_Rela *rel,
1187 struct elf_link_hash_entry *h,
1188 Elf_Internal_Sym *sym)
1189 {
1190 if (h != NULL)
1191 {
1192 switch (ELF32_R_TYPE (rel->r_info))
1193 {
1194 case R_386_GNU_VTINHERIT:
1195 case R_386_GNU_VTENTRY:
1196 break;
1197
1198 default:
1199 switch (h->root.type)
1200 {
1201 case bfd_link_hash_defined:
1202 case bfd_link_hash_defweak:
1203 return h->root.u.def.section;
1204
1205 case bfd_link_hash_common:
1206 return h->root.u.c.p->section;
1207
1208 default:
1209 break;
1210 }
1211 }
1212 }
1213 else
1214 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1215
1216 return NULL;
1217 }
1218
1219 /* Update the got entry reference counts for the section being removed. */
1220
1221 static bfd_boolean
1222 elf_i386_gc_sweep_hook (bfd *abfd,
1223 struct bfd_link_info *info,
1224 asection *sec,
1225 const Elf_Internal_Rela *relocs)
1226 {
1227 Elf_Internal_Shdr *symtab_hdr;
1228 struct elf_link_hash_entry **sym_hashes;
1229 bfd_signed_vma *local_got_refcounts;
1230 const Elf_Internal_Rela *rel, *relend;
1231
1232 elf_section_data (sec)->local_dynrel = NULL;
1233
1234 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1235 sym_hashes = elf_sym_hashes (abfd);
1236 local_got_refcounts = elf_local_got_refcounts (abfd);
1237
1238 relend = relocs + sec->reloc_count;
1239 for (rel = relocs; rel < relend; rel++)
1240 {
1241 unsigned long r_symndx;
1242 unsigned int r_type;
1243 struct elf_link_hash_entry *h = NULL;
1244
1245 r_symndx = ELF32_R_SYM (rel->r_info);
1246 if (r_symndx >= symtab_hdr->sh_info)
1247 {
1248 struct elf_i386_link_hash_entry *eh;
1249 struct elf_i386_dyn_relocs **pp;
1250 struct elf_i386_dyn_relocs *p;
1251
1252 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1253 while (h->root.type == bfd_link_hash_indirect
1254 || h->root.type == bfd_link_hash_warning)
1255 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1256 eh = (struct elf_i386_link_hash_entry *) h;
1257
1258 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1259 if (p->sec == sec)
1260 {
1261 /* Everything must go for SEC. */
1262 *pp = p->next;
1263 break;
1264 }
1265 }
1266
1267 r_type = ELF32_R_TYPE (rel->r_info);
1268 r_type = elf_i386_tls_transition (info, r_type, h != NULL);
1269 switch (r_type)
1270 {
1271 case R_386_TLS_LDM:
1272 if (elf_i386_hash_table (info)->tls_ldm_got.refcount > 0)
1273 elf_i386_hash_table (info)->tls_ldm_got.refcount -= 1;
1274 break;
1275
1276 case R_386_TLS_GD:
1277 case R_386_TLS_IE_32:
1278 case R_386_TLS_IE:
1279 case R_386_TLS_GOTIE:
1280 case R_386_GOT32:
1281 if (h != NULL)
1282 {
1283 if (h->got.refcount > 0)
1284 h->got.refcount -= 1;
1285 }
1286 else if (local_got_refcounts != NULL)
1287 {
1288 if (local_got_refcounts[r_symndx] > 0)
1289 local_got_refcounts[r_symndx] -= 1;
1290 }
1291 break;
1292
1293 case R_386_32:
1294 case R_386_PC32:
1295 if (info->shared)
1296 break;
1297 /* Fall through */
1298
1299 case R_386_PLT32:
1300 if (h != NULL)
1301 {
1302 if (h->plt.refcount > 0)
1303 h->plt.refcount -= 1;
1304 }
1305 break;
1306
1307 default:
1308 break;
1309 }
1310 }
1311
1312 return TRUE;
1313 }
1314
1315 /* Adjust a symbol defined by a dynamic object and referenced by a
1316 regular object. The current definition is in some section of the
1317 dynamic object, but we're not including those sections. We have to
1318 change the definition to something the rest of the link can
1319 understand. */
1320
1321 static bfd_boolean
1322 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
1323 struct elf_link_hash_entry *h)
1324 {
1325 struct elf_i386_link_hash_table *htab;
1326 asection *s;
1327 unsigned int power_of_two;
1328
1329 /* If this is a function, put it in the procedure linkage table. We
1330 will fill in the contents of the procedure linkage table later,
1331 when we know the address of the .got section. */
1332 if (h->type == STT_FUNC
1333 || h->needs_plt)
1334 {
1335 if (h->plt.refcount <= 0
1336 || SYMBOL_CALLS_LOCAL (info, h)
1337 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1338 && h->root.type == bfd_link_hash_undefweak))
1339 {
1340 /* This case can occur if we saw a PLT32 reloc in an input
1341 file, but the symbol was never referred to by a dynamic
1342 object, or if all references were garbage collected. In
1343 such a case, we don't actually need to build a procedure
1344 linkage table, and we can just do a PC32 reloc instead. */
1345 h->plt.offset = (bfd_vma) -1;
1346 h->needs_plt = 0;
1347 }
1348
1349 return TRUE;
1350 }
1351 else
1352 /* It's possible that we incorrectly decided a .plt reloc was
1353 needed for an R_386_PC32 reloc to a non-function sym in
1354 check_relocs. We can't decide accurately between function and
1355 non-function syms in check-relocs; Objects loaded later in
1356 the link may change h->type. So fix it now. */
1357 h->plt.offset = (bfd_vma) -1;
1358
1359 /* If this is a weak symbol, and there is a real definition, the
1360 processor independent code will have arranged for us to see the
1361 real definition first, and we can just use the same value. */
1362 if (h->u.weakdef != NULL)
1363 {
1364 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1365 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1366 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1367 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1368 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1369 h->non_got_ref = h->u.weakdef->non_got_ref;
1370 return TRUE;
1371 }
1372
1373 /* This is a reference to a symbol defined by a dynamic object which
1374 is not a function. */
1375
1376 /* If we are creating a shared library, we must presume that the
1377 only references to the symbol are via the global offset table.
1378 For such cases we need not do anything here; the relocations will
1379 be handled correctly by relocate_section. */
1380 if (info->shared)
1381 return TRUE;
1382
1383 /* If there are no references to this symbol that do not use the
1384 GOT, we don't need to generate a copy reloc. */
1385 if (!h->non_got_ref)
1386 return TRUE;
1387
1388 /* If -z nocopyreloc was given, we won't generate them either. */
1389 if (info->nocopyreloc)
1390 {
1391 h->non_got_ref = 0;
1392 return TRUE;
1393 }
1394
1395 if (ELIMINATE_COPY_RELOCS)
1396 {
1397 struct elf_i386_link_hash_entry * eh;
1398 struct elf_i386_dyn_relocs *p;
1399
1400 eh = (struct elf_i386_link_hash_entry *) h;
1401 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1402 {
1403 s = p->sec->output_section;
1404 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1405 break;
1406 }
1407
1408 /* If we didn't find any dynamic relocs in read-only sections, then
1409 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1410 if (p == NULL)
1411 {
1412 h->non_got_ref = 0;
1413 return TRUE;
1414 }
1415 }
1416
1417 /* We must allocate the symbol in our .dynbss section, which will
1418 become part of the .bss section of the executable. There will be
1419 an entry for this symbol in the .dynsym section. The dynamic
1420 object will contain position independent code, so all references
1421 from the dynamic object to this symbol will go through the global
1422 offset table. The dynamic linker will use the .dynsym entry to
1423 determine the address it must put in the global offset table, so
1424 both the dynamic object and the regular object will refer to the
1425 same memory location for the variable. */
1426
1427 htab = elf_i386_hash_table (info);
1428
1429 /* We must generate a R_386_COPY reloc to tell the dynamic linker to
1430 copy the initial value out of the dynamic object and into the
1431 runtime process image. */
1432 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1433 {
1434 htab->srelbss->size += sizeof (Elf32_External_Rel);
1435 h->needs_copy = 1;
1436 }
1437
1438 /* We need to figure out the alignment required for this symbol. I
1439 have no idea how ELF linkers handle this. */
1440 power_of_two = bfd_log2 (h->size);
1441 if (power_of_two > 3)
1442 power_of_two = 3;
1443
1444 /* Apply the required alignment. */
1445 s = htab->sdynbss;
1446 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1447 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1448 {
1449 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1450 return FALSE;
1451 }
1452
1453 /* Define the symbol as being at this point in the section. */
1454 h->root.u.def.section = s;
1455 h->root.u.def.value = s->size;
1456
1457 /* Increment the section size to make room for the symbol. */
1458 s->size += h->size;
1459
1460 return TRUE;
1461 }
1462
1463 /* Allocate space in .plt, .got and associated reloc sections for
1464 dynamic relocs. */
1465
1466 static bfd_boolean
1467 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1468 {
1469 struct bfd_link_info *info;
1470 struct elf_i386_link_hash_table *htab;
1471 struct elf_i386_link_hash_entry *eh;
1472 struct elf_i386_dyn_relocs *p;
1473
1474 if (h->root.type == bfd_link_hash_indirect)
1475 return TRUE;
1476
1477 if (h->root.type == bfd_link_hash_warning)
1478 /* When warning symbols are created, they **replace** the "real"
1479 entry in the hash table, thus we never get to see the real
1480 symbol in a hash traversal. So look at it now. */
1481 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1482
1483 info = (struct bfd_link_info *) inf;
1484 htab = elf_i386_hash_table (info);
1485
1486 if (htab->elf.dynamic_sections_created
1487 && h->plt.refcount > 0)
1488 {
1489 /* Make sure this symbol is output as a dynamic symbol.
1490 Undefined weak syms won't yet be marked as dynamic. */
1491 if (h->dynindx == -1
1492 && !h->forced_local)
1493 {
1494 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1495 return FALSE;
1496 }
1497
1498 if (info->shared
1499 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1500 {
1501 asection *s = htab->splt;
1502
1503 /* If this is the first .plt entry, make room for the special
1504 first entry. */
1505 if (s->size == 0)
1506 s->size += PLT_ENTRY_SIZE;
1507
1508 h->plt.offset = s->size;
1509
1510 /* If this symbol is not defined in a regular file, and we are
1511 not generating a shared library, then set the symbol to this
1512 location in the .plt. This is required to make function
1513 pointers compare as equal between the normal executable and
1514 the shared library. */
1515 if (! info->shared
1516 && !h->def_regular)
1517 {
1518 h->root.u.def.section = s;
1519 h->root.u.def.value = h->plt.offset;
1520 }
1521
1522 /* Make room for this entry. */
1523 s->size += PLT_ENTRY_SIZE;
1524
1525 /* We also need to make an entry in the .got.plt section, which
1526 will be placed in the .got section by the linker script. */
1527 htab->sgotplt->size += 4;
1528
1529 /* We also need to make an entry in the .rel.plt section. */
1530 htab->srelplt->size += sizeof (Elf32_External_Rel);
1531 }
1532 else
1533 {
1534 h->plt.offset = (bfd_vma) -1;
1535 h->needs_plt = 0;
1536 }
1537 }
1538 else
1539 {
1540 h->plt.offset = (bfd_vma) -1;
1541 h->needs_plt = 0;
1542 }
1543
1544 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
1545 make it a R_386_TLS_LE_32 requiring no TLS entry. */
1546 if (h->got.refcount > 0
1547 && !info->shared
1548 && h->dynindx == -1
1549 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE))
1550 h->got.offset = (bfd_vma) -1;
1551 else if (h->got.refcount > 0)
1552 {
1553 asection *s;
1554 bfd_boolean dyn;
1555 int tls_type = elf_i386_hash_entry(h)->tls_type;
1556
1557 /* Make sure this symbol is output as a dynamic symbol.
1558 Undefined weak syms won't yet be marked as dynamic. */
1559 if (h->dynindx == -1
1560 && !h->forced_local)
1561 {
1562 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1563 return FALSE;
1564 }
1565
1566 s = htab->sgot;
1567 h->got.offset = s->size;
1568 s->size += 4;
1569 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1570 if (tls_type == GOT_TLS_GD || tls_type == GOT_TLS_IE_BOTH)
1571 s->size += 4;
1572 dyn = htab->elf.dynamic_sections_created;
1573 /* R_386_TLS_IE_32 needs one dynamic relocation,
1574 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
1575 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
1576 need two), R_386_TLS_GD needs one if local symbol and two if
1577 global. */
1578 if (tls_type == GOT_TLS_IE_BOTH)
1579 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1580 else if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1581 || (tls_type & GOT_TLS_IE))
1582 htab->srelgot->size += sizeof (Elf32_External_Rel);
1583 else if (tls_type == GOT_TLS_GD)
1584 htab->srelgot->size += 2 * sizeof (Elf32_External_Rel);
1585 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1586 || h->root.type != bfd_link_hash_undefweak)
1587 && (info->shared
1588 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1589 htab->srelgot->size += sizeof (Elf32_External_Rel);
1590 }
1591 else
1592 h->got.offset = (bfd_vma) -1;
1593
1594 eh = (struct elf_i386_link_hash_entry *) h;
1595 if (eh->dyn_relocs == NULL)
1596 return TRUE;
1597
1598 /* In the shared -Bsymbolic case, discard space allocated for
1599 dynamic pc-relative relocs against symbols which turn out to be
1600 defined in regular objects. For the normal shared case, discard
1601 space for pc-relative relocs that have become local due to symbol
1602 visibility changes. */
1603
1604 if (info->shared)
1605 {
1606 /* The only reloc that uses pc_count is R_386_PC32, which will
1607 appear on a call or on something like ".long foo - .". We
1608 want calls to protected symbols to resolve directly to the
1609 function rather than going via the plt. If people want
1610 function pointer comparisons to work as expected then they
1611 should avoid writing assembly like ".long foo - .". */
1612 if (SYMBOL_CALLS_LOCAL (info, h))
1613 {
1614 struct elf_i386_dyn_relocs **pp;
1615
1616 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1617 {
1618 p->count -= p->pc_count;
1619 p->pc_count = 0;
1620 if (p->count == 0)
1621 *pp = p->next;
1622 else
1623 pp = &p->next;
1624 }
1625 }
1626
1627 /* Also discard relocs on undefined weak syms with non-default
1628 visibility. */
1629 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1630 && h->root.type == bfd_link_hash_undefweak)
1631 eh->dyn_relocs = NULL;
1632 }
1633 else if (ELIMINATE_COPY_RELOCS)
1634 {
1635 /* For the non-shared case, discard space for relocs against
1636 symbols which turn out to need copy relocs or are not
1637 dynamic. */
1638
1639 if (!h->non_got_ref
1640 && ((h->def_dynamic
1641 && !h->def_regular)
1642 || (htab->elf.dynamic_sections_created
1643 && (h->root.type == bfd_link_hash_undefweak
1644 || h->root.type == bfd_link_hash_undefined))))
1645 {
1646 /* Make sure this symbol is output as a dynamic symbol.
1647 Undefined weak syms won't yet be marked as dynamic. */
1648 if (h->dynindx == -1
1649 && !h->forced_local)
1650 {
1651 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1652 return FALSE;
1653 }
1654
1655 /* If that succeeded, we know we'll be keeping all the
1656 relocs. */
1657 if (h->dynindx != -1)
1658 goto keep;
1659 }
1660
1661 eh->dyn_relocs = NULL;
1662
1663 keep: ;
1664 }
1665
1666 /* Finally, allocate space. */
1667 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1668 {
1669 asection *sreloc = elf_section_data (p->sec)->sreloc;
1670 sreloc->size += p->count * sizeof (Elf32_External_Rel);
1671 }
1672
1673 return TRUE;
1674 }
1675
1676 /* Find any dynamic relocs that apply to read-only sections. */
1677
1678 static bfd_boolean
1679 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1680 {
1681 struct elf_i386_link_hash_entry *eh;
1682 struct elf_i386_dyn_relocs *p;
1683
1684 if (h->root.type == bfd_link_hash_warning)
1685 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1686
1687 eh = (struct elf_i386_link_hash_entry *) h;
1688 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1689 {
1690 asection *s = p->sec->output_section;
1691
1692 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1693 {
1694 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1695
1696 info->flags |= DF_TEXTREL;
1697
1698 /* Not an error, just cut short the traversal. */
1699 return FALSE;
1700 }
1701 }
1702 return TRUE;
1703 }
1704
1705 /* Set the sizes of the dynamic sections. */
1706
1707 static bfd_boolean
1708 elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1709 struct bfd_link_info *info)
1710 {
1711 struct elf_i386_link_hash_table *htab;
1712 bfd *dynobj;
1713 asection *s;
1714 bfd_boolean relocs;
1715 bfd *ibfd;
1716
1717 htab = elf_i386_hash_table (info);
1718 dynobj = htab->elf.dynobj;
1719 if (dynobj == NULL)
1720 abort ();
1721
1722 if (htab->elf.dynamic_sections_created)
1723 {
1724 /* Set the contents of the .interp section to the interpreter. */
1725 if (info->executable)
1726 {
1727 s = bfd_get_section_by_name (dynobj, ".interp");
1728 if (s == NULL)
1729 abort ();
1730 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1731 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1732 }
1733 }
1734
1735 /* Set up .got offsets for local syms, and space for local dynamic
1736 relocs. */
1737 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1738 {
1739 bfd_signed_vma *local_got;
1740 bfd_signed_vma *end_local_got;
1741 char *local_tls_type;
1742 bfd_size_type locsymcount;
1743 Elf_Internal_Shdr *symtab_hdr;
1744 asection *srel;
1745
1746 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1747 continue;
1748
1749 for (s = ibfd->sections; s != NULL; s = s->next)
1750 {
1751 struct elf_i386_dyn_relocs *p;
1752
1753 for (p = *((struct elf_i386_dyn_relocs **)
1754 &elf_section_data (s)->local_dynrel);
1755 p != NULL;
1756 p = p->next)
1757 {
1758 if (!bfd_is_abs_section (p->sec)
1759 && bfd_is_abs_section (p->sec->output_section))
1760 {
1761 /* Input section has been discarded, either because
1762 it is a copy of a linkonce section or due to
1763 linker script /DISCARD/, so we'll be discarding
1764 the relocs too. */
1765 }
1766 else if (p->count != 0)
1767 {
1768 srel = elf_section_data (p->sec)->sreloc;
1769 srel->size += p->count * sizeof (Elf32_External_Rel);
1770 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1771 info->flags |= DF_TEXTREL;
1772 }
1773 }
1774 }
1775
1776 local_got = elf_local_got_refcounts (ibfd);
1777 if (!local_got)
1778 continue;
1779
1780 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1781 locsymcount = symtab_hdr->sh_info;
1782 end_local_got = local_got + locsymcount;
1783 local_tls_type = elf_i386_local_got_tls_type (ibfd);
1784 s = htab->sgot;
1785 srel = htab->srelgot;
1786 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1787 {
1788 if (*local_got > 0)
1789 {
1790 *local_got = s->size;
1791 s->size += 4;
1792 if (*local_tls_type == GOT_TLS_GD
1793 || *local_tls_type == GOT_TLS_IE_BOTH)
1794 s->size += 4;
1795 if (info->shared
1796 || *local_tls_type == GOT_TLS_GD
1797 || (*local_tls_type & GOT_TLS_IE))
1798 {
1799 if (*local_tls_type == GOT_TLS_IE_BOTH)
1800 srel->size += 2 * sizeof (Elf32_External_Rel);
1801 else
1802 srel->size += sizeof (Elf32_External_Rel);
1803 }
1804 }
1805 else
1806 *local_got = (bfd_vma) -1;
1807 }
1808 }
1809
1810 if (htab->tls_ldm_got.refcount > 0)
1811 {
1812 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1813 relocs. */
1814 htab->tls_ldm_got.offset = htab->sgot->size;
1815 htab->sgot->size += 8;
1816 htab->srelgot->size += sizeof (Elf32_External_Rel);
1817 }
1818 else
1819 htab->tls_ldm_got.offset = -1;
1820
1821 /* Allocate global sym .plt and .got entries, and space for global
1822 sym dynamic relocs. */
1823 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1824
1825 /* We now have determined the sizes of the various dynamic sections.
1826 Allocate memory for them. */
1827 relocs = FALSE;
1828 for (s = dynobj->sections; s != NULL; s = s->next)
1829 {
1830 if ((s->flags & SEC_LINKER_CREATED) == 0)
1831 continue;
1832
1833 if (s == htab->splt
1834 || s == htab->sgot
1835 || s == htab->sgotplt)
1836 {
1837 /* Strip this section if we don't need it; see the
1838 comment below. */
1839 }
1840 else if (strncmp (bfd_get_section_name (dynobj, s), ".rel", 4) == 0)
1841 {
1842 if (s->size != 0 && s != htab->srelplt)
1843 relocs = TRUE;
1844
1845 /* We use the reloc_count field as a counter if we need
1846 to copy relocs into the output file. */
1847 s->reloc_count = 0;
1848 }
1849 else
1850 {
1851 /* It's not one of our sections, so don't allocate space. */
1852 continue;
1853 }
1854
1855 if (s->size == 0)
1856 {
1857 /* If we don't need this section, strip it from the
1858 output file. This is mostly to handle .rel.bss and
1859 .rel.plt. We must create both sections in
1860 create_dynamic_sections, because they must be created
1861 before the linker maps input sections to output
1862 sections. The linker does that before
1863 adjust_dynamic_symbol is called, and it is that
1864 function which decides whether anything needs to go
1865 into these sections. */
1866
1867 s->flags |= SEC_EXCLUDE;
1868 continue;
1869 }
1870
1871 /* Allocate memory for the section contents. We use bfd_zalloc
1872 here in case unused entries are not reclaimed before the
1873 section's contents are written out. This should not happen,
1874 but this way if it does, we get a R_386_NONE reloc instead
1875 of garbage. */
1876 s->contents = bfd_zalloc (dynobj, s->size);
1877 if (s->contents == NULL)
1878 return FALSE;
1879 }
1880
1881 if (htab->elf.dynamic_sections_created)
1882 {
1883 /* Add some entries to the .dynamic section. We fill in the
1884 values later, in elf_i386_finish_dynamic_sections, but we
1885 must add the entries now so that we get the correct size for
1886 the .dynamic section. The DT_DEBUG entry is filled in by the
1887 dynamic linker and used by the debugger. */
1888 #define add_dynamic_entry(TAG, VAL) \
1889 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1890
1891 if (info->executable)
1892 {
1893 if (!add_dynamic_entry (DT_DEBUG, 0))
1894 return FALSE;
1895 }
1896
1897 if (htab->splt->size != 0)
1898 {
1899 if (!add_dynamic_entry (DT_PLTGOT, 0)
1900 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1901 || !add_dynamic_entry (DT_PLTREL, DT_REL)
1902 || !add_dynamic_entry (DT_JMPREL, 0))
1903 return FALSE;
1904 }
1905
1906 if (relocs)
1907 {
1908 if (!add_dynamic_entry (DT_REL, 0)
1909 || !add_dynamic_entry (DT_RELSZ, 0)
1910 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
1911 return FALSE;
1912
1913 /* If any dynamic relocs apply to a read-only section,
1914 then we need a DT_TEXTREL entry. */
1915 if ((info->flags & DF_TEXTREL) == 0)
1916 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1917 (PTR) info);
1918
1919 if ((info->flags & DF_TEXTREL) != 0)
1920 {
1921 if (!add_dynamic_entry (DT_TEXTREL, 0))
1922 return FALSE;
1923 }
1924 }
1925 }
1926 #undef add_dynamic_entry
1927
1928 return TRUE;
1929 }
1930
1931 /* Set the correct type for an x86 ELF section. We do this by the
1932 section name, which is a hack, but ought to work. */
1933
1934 static bfd_boolean
1935 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1936 Elf_Internal_Shdr *hdr,
1937 asection *sec)
1938 {
1939 register const char *name;
1940
1941 name = bfd_get_section_name (abfd, sec);
1942
1943 /* This is an ugly, but unfortunately necessary hack that is
1944 needed when producing EFI binaries on x86. It tells
1945 elf.c:elf_fake_sections() not to consider ".reloc" as a section
1946 containing ELF relocation info. We need this hack in order to
1947 be able to generate ELF binaries that can be translated into
1948 EFI applications (which are essentially COFF objects). Those
1949 files contain a COFF ".reloc" section inside an ELFNN object,
1950 which would normally cause BFD to segfault because it would
1951 attempt to interpret this section as containing relocation
1952 entries for section "oc". With this hack enabled, ".reloc"
1953 will be treated as a normal data section, which will avoid the
1954 segfault. However, you won't be able to create an ELFNN binary
1955 with a section named "oc" that needs relocations, but that's
1956 the kind of ugly side-effects you get when detecting section
1957 types based on their names... In practice, this limitation is
1958 unlikely to bite. */
1959 if (strcmp (name, ".reloc") == 0)
1960 hdr->sh_type = SHT_PROGBITS;
1961
1962 return TRUE;
1963 }
1964
1965 /* Return the base VMA address which should be subtracted from real addresses
1966 when resolving @dtpoff relocation.
1967 This is PT_TLS segment p_vaddr. */
1968
1969 static bfd_vma
1970 dtpoff_base (struct bfd_link_info *info)
1971 {
1972 /* If tls_sec is NULL, we should have signalled an error already. */
1973 if (elf_hash_table (info)->tls_sec == NULL)
1974 return 0;
1975 return elf_hash_table (info)->tls_sec->vma;
1976 }
1977
1978 /* Return the relocation value for @tpoff relocation
1979 if STT_TLS virtual address is ADDRESS. */
1980
1981 static bfd_vma
1982 tpoff (struct bfd_link_info *info, bfd_vma address)
1983 {
1984 struct elf_link_hash_table *htab = elf_hash_table (info);
1985
1986 /* If tls_sec is NULL, we should have signalled an error already. */
1987 if (htab->tls_sec == NULL)
1988 return 0;
1989 return htab->tls_size + htab->tls_sec->vma - address;
1990 }
1991
1992 /* Relocate an i386 ELF section. */
1993
1994 static bfd_boolean
1995 elf_i386_relocate_section (bfd *output_bfd,
1996 struct bfd_link_info *info,
1997 bfd *input_bfd,
1998 asection *input_section,
1999 bfd_byte *contents,
2000 Elf_Internal_Rela *relocs,
2001 Elf_Internal_Sym *local_syms,
2002 asection **local_sections)
2003 {
2004 struct elf_i386_link_hash_table *htab;
2005 Elf_Internal_Shdr *symtab_hdr;
2006 struct elf_link_hash_entry **sym_hashes;
2007 bfd_vma *local_got_offsets;
2008 Elf_Internal_Rela *rel;
2009 Elf_Internal_Rela *relend;
2010
2011 htab = elf_i386_hash_table (info);
2012 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2013 sym_hashes = elf_sym_hashes (input_bfd);
2014 local_got_offsets = elf_local_got_offsets (input_bfd);
2015
2016 rel = relocs;
2017 relend = relocs + input_section->reloc_count;
2018 for (; rel < relend; rel++)
2019 {
2020 unsigned int r_type;
2021 reloc_howto_type *howto;
2022 unsigned long r_symndx;
2023 struct elf_link_hash_entry *h;
2024 Elf_Internal_Sym *sym;
2025 asection *sec;
2026 bfd_vma off;
2027 bfd_vma relocation;
2028 bfd_boolean unresolved_reloc;
2029 bfd_reloc_status_type r;
2030 unsigned int indx;
2031 int tls_type;
2032
2033 r_type = ELF32_R_TYPE (rel->r_info);
2034 if (r_type == R_386_GNU_VTINHERIT
2035 || r_type == R_386_GNU_VTENTRY)
2036 continue;
2037
2038 if ((indx = r_type) >= R_386_standard
2039 && ((indx = r_type - R_386_ext_offset) - R_386_standard
2040 >= R_386_ext - R_386_standard)
2041 && ((indx = r_type - R_386_tls_offset) - R_386_ext
2042 >= R_386_tls - R_386_ext))
2043 {
2044 (*_bfd_error_handler)
2045 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
2046 input_bfd, input_section, r_type);
2047 bfd_set_error (bfd_error_bad_value);
2048 return FALSE;
2049 }
2050 howto = elf_howto_table + indx;
2051
2052 r_symndx = ELF32_R_SYM (rel->r_info);
2053
2054 if (info->relocatable)
2055 {
2056 bfd_vma val;
2057 bfd_byte *where;
2058
2059 /* This is a relocatable link. We don't have to change
2060 anything, unless the reloc is against a section symbol,
2061 in which case we have to adjust according to where the
2062 section symbol winds up in the output section. */
2063 if (r_symndx >= symtab_hdr->sh_info)
2064 continue;
2065
2066 sym = local_syms + r_symndx;
2067 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2068 continue;
2069
2070 sec = local_sections[r_symndx];
2071 val = sec->output_offset;
2072 if (val == 0)
2073 continue;
2074
2075 where = contents + rel->r_offset;
2076 switch (howto->size)
2077 {
2078 /* FIXME: overflow checks. */
2079 case 0:
2080 val += bfd_get_8 (input_bfd, where);
2081 bfd_put_8 (input_bfd, val, where);
2082 break;
2083 case 1:
2084 val += bfd_get_16 (input_bfd, where);
2085 bfd_put_16 (input_bfd, val, where);
2086 break;
2087 case 2:
2088 val += bfd_get_32 (input_bfd, where);
2089 bfd_put_32 (input_bfd, val, where);
2090 break;
2091 default:
2092 abort ();
2093 }
2094 continue;
2095 }
2096
2097 /* This is a final link. */
2098 h = NULL;
2099 sym = NULL;
2100 sec = NULL;
2101 unresolved_reloc = FALSE;
2102 if (r_symndx < symtab_hdr->sh_info)
2103 {
2104 sym = local_syms + r_symndx;
2105 sec = local_sections[r_symndx];
2106 relocation = (sec->output_section->vma
2107 + sec->output_offset
2108 + sym->st_value);
2109 if ((sec->flags & SEC_MERGE)
2110 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2111 {
2112 asection *msec;
2113 bfd_vma addend;
2114 bfd_byte *where = contents + rel->r_offset;
2115
2116 switch (howto->size)
2117 {
2118 case 0:
2119 addend = bfd_get_8 (input_bfd, where);
2120 if (howto->pc_relative)
2121 {
2122 addend = (addend ^ 0x80) - 0x80;
2123 addend += 1;
2124 }
2125 break;
2126 case 1:
2127 addend = bfd_get_16 (input_bfd, where);
2128 if (howto->pc_relative)
2129 {
2130 addend = (addend ^ 0x8000) - 0x8000;
2131 addend += 2;
2132 }
2133 break;
2134 case 2:
2135 addend = bfd_get_32 (input_bfd, where);
2136 if (howto->pc_relative)
2137 {
2138 addend = (addend ^ 0x80000000) - 0x80000000;
2139 addend += 4;
2140 }
2141 break;
2142 default:
2143 abort ();
2144 }
2145
2146 msec = sec;
2147 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend);
2148 addend -= relocation;
2149 addend += msec->output_section->vma + msec->output_offset;
2150
2151 switch (howto->size)
2152 {
2153 case 0:
2154 /* FIXME: overflow checks. */
2155 if (howto->pc_relative)
2156 addend -= 1;
2157 bfd_put_8 (input_bfd, addend, where);
2158 break;
2159 case 1:
2160 if (howto->pc_relative)
2161 addend -= 2;
2162 bfd_put_16 (input_bfd, addend, where);
2163 break;
2164 case 2:
2165 if (howto->pc_relative)
2166 addend -= 4;
2167 bfd_put_32 (input_bfd, addend, where);
2168 break;
2169 }
2170 }
2171 }
2172 else
2173 {
2174 bfd_boolean warned;
2175
2176 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2177 r_symndx, symtab_hdr, sym_hashes,
2178 h, sec, relocation,
2179 unresolved_reloc, warned);
2180 }
2181
2182 switch (r_type)
2183 {
2184 case R_386_GOT32:
2185 /* Relocation is to the entry for this symbol in the global
2186 offset table. */
2187 if (htab->sgot == NULL)
2188 abort ();
2189
2190 if (h != NULL)
2191 {
2192 bfd_boolean dyn;
2193
2194 off = h->got.offset;
2195 dyn = htab->elf.dynamic_sections_created;
2196 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2197 || (info->shared
2198 && SYMBOL_REFERENCES_LOCAL (info, h))
2199 || (ELF_ST_VISIBILITY (h->other)
2200 && h->root.type == bfd_link_hash_undefweak))
2201 {
2202 /* This is actually a static link, or it is a
2203 -Bsymbolic link and the symbol is defined
2204 locally, or the symbol was forced to be local
2205 because of a version file. We must initialize
2206 this entry in the global offset table. Since the
2207 offset must always be a multiple of 4, we use the
2208 least significant bit to record whether we have
2209 initialized it already.
2210
2211 When doing a dynamic link, we create a .rel.got
2212 relocation entry to initialize the value. This
2213 is done in the finish_dynamic_symbol routine. */
2214 if ((off & 1) != 0)
2215 off &= ~1;
2216 else
2217 {
2218 bfd_put_32 (output_bfd, relocation,
2219 htab->sgot->contents + off);
2220 h->got.offset |= 1;
2221 }
2222 }
2223 else
2224 unresolved_reloc = FALSE;
2225 }
2226 else
2227 {
2228 if (local_got_offsets == NULL)
2229 abort ();
2230
2231 off = local_got_offsets[r_symndx];
2232
2233 /* The offset must always be a multiple of 4. We use
2234 the least significant bit to record whether we have
2235 already generated the necessary reloc. */
2236 if ((off & 1) != 0)
2237 off &= ~1;
2238 else
2239 {
2240 bfd_put_32 (output_bfd, relocation,
2241 htab->sgot->contents + off);
2242
2243 if (info->shared)
2244 {
2245 asection *s;
2246 Elf_Internal_Rela outrel;
2247 bfd_byte *loc;
2248
2249 s = htab->srelgot;
2250 if (s == NULL)
2251 abort ();
2252
2253 outrel.r_offset = (htab->sgot->output_section->vma
2254 + htab->sgot->output_offset
2255 + off);
2256 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2257 loc = s->contents;
2258 loc += s->reloc_count++ * sizeof (Elf32_External_Rel);
2259 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2260 }
2261
2262 local_got_offsets[r_symndx] |= 1;
2263 }
2264 }
2265
2266 if (off >= (bfd_vma) -2)
2267 abort ();
2268
2269 relocation = htab->sgot->output_section->vma
2270 + htab->sgot->output_offset + off
2271 - htab->sgotplt->output_section->vma
2272 - htab->sgotplt->output_offset;
2273 break;
2274
2275 case R_386_GOTOFF:
2276 /* Relocation is relative to the start of the global offset
2277 table. */
2278
2279 /* Check to make sure it isn't a protected function symbol
2280 for shared library since it may not be local when used
2281 as function address. */
2282 if (info->shared
2283 && h
2284 && h->def_regular
2285 && h->type == STT_FUNC
2286 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2287 {
2288 (*_bfd_error_handler)
2289 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
2290 input_bfd, h->root.root.string);
2291 bfd_set_error (bfd_error_bad_value);
2292 return FALSE;
2293 }
2294
2295 /* Note that sgot is not involved in this
2296 calculation. We always want the start of .got.plt. If we
2297 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2298 permitted by the ABI, we might have to change this
2299 calculation. */
2300 relocation -= htab->sgotplt->output_section->vma
2301 + htab->sgotplt->output_offset;
2302 break;
2303
2304 case R_386_GOTPC:
2305 /* Use global offset table as symbol value. */
2306 relocation = htab->sgotplt->output_section->vma
2307 + htab->sgotplt->output_offset;
2308 unresolved_reloc = FALSE;
2309 break;
2310
2311 case R_386_PLT32:
2312 /* Relocation is to the entry for this symbol in the
2313 procedure linkage table. */
2314
2315 /* Resolve a PLT32 reloc against a local symbol directly,
2316 without using the procedure linkage table. */
2317 if (h == NULL)
2318 break;
2319
2320 if (h->plt.offset == (bfd_vma) -1
2321 || htab->splt == NULL)
2322 {
2323 /* We didn't make a PLT entry for this symbol. This
2324 happens when statically linking PIC code, or when
2325 using -Bsymbolic. */
2326 break;
2327 }
2328
2329 relocation = (htab->splt->output_section->vma
2330 + htab->splt->output_offset
2331 + h->plt.offset);
2332 unresolved_reloc = FALSE;
2333 break;
2334
2335 case R_386_32:
2336 case R_386_PC32:
2337 /* r_symndx will be zero only for relocs against symbols
2338 from removed linkonce sections, or sections discarded by
2339 a linker script. */
2340 if (r_symndx == 0)
2341 {
2342 /* Zero the section contents. eh_frame generated by old
2343 versions of gcc isn't edited by elf-eh-frame.c, so
2344 FDEs for discarded linkonce functions might remain.
2345 Putting zeros here will zero such FDE's address range.
2346 This is a hint to unwinders and other consumers of
2347 exception handling info that the FDE is invalid. */
2348 bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
2349 break;
2350 }
2351
2352 if ((input_section->flags & SEC_ALLOC) == 0)
2353 break;
2354
2355 if ((info->shared
2356 && (h == NULL
2357 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2358 || h->root.type != bfd_link_hash_undefweak)
2359 && (r_type != R_386_PC32
2360 || !SYMBOL_CALLS_LOCAL (info, h)))
2361 || (ELIMINATE_COPY_RELOCS
2362 && !info->shared
2363 && h != NULL
2364 && h->dynindx != -1
2365 && !h->non_got_ref
2366 && ((h->def_dynamic
2367 && !h->def_regular)
2368 || h->root.type == bfd_link_hash_undefweak
2369 || h->root.type == bfd_link_hash_undefined)))
2370 {
2371 Elf_Internal_Rela outrel;
2372 bfd_byte *loc;
2373 bfd_boolean skip, relocate;
2374 asection *sreloc;
2375
2376 /* When generating a shared object, these relocations
2377 are copied into the output file to be resolved at run
2378 time. */
2379
2380 skip = FALSE;
2381 relocate = FALSE;
2382
2383 outrel.r_offset =
2384 _bfd_elf_section_offset (output_bfd, info, input_section,
2385 rel->r_offset);
2386 if (outrel.r_offset == (bfd_vma) -1)
2387 skip = TRUE;
2388 else if (outrel.r_offset == (bfd_vma) -2)
2389 skip = TRUE, relocate = TRUE;
2390 outrel.r_offset += (input_section->output_section->vma
2391 + input_section->output_offset);
2392
2393 if (skip)
2394 memset (&outrel, 0, sizeof outrel);
2395 else if (h != NULL
2396 && h->dynindx != -1
2397 && (r_type == R_386_PC32
2398 || !info->shared
2399 || !info->symbolic
2400 || !h->def_regular))
2401 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2402 else
2403 {
2404 /* This symbol is local, or marked to become local. */
2405 relocate = TRUE;
2406 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2407 }
2408
2409 sreloc = elf_section_data (input_section)->sreloc;
2410 if (sreloc == NULL)
2411 abort ();
2412
2413 loc = sreloc->contents;
2414 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2415 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2416
2417 /* If this reloc is against an external symbol, we do
2418 not want to fiddle with the addend. Otherwise, we
2419 need to include the symbol value so that it becomes
2420 an addend for the dynamic reloc. */
2421 if (! relocate)
2422 continue;
2423 }
2424 break;
2425
2426 case R_386_TLS_IE:
2427 if (info->shared)
2428 {
2429 Elf_Internal_Rela outrel;
2430 bfd_byte *loc;
2431 asection *sreloc;
2432
2433 outrel.r_offset = rel->r_offset
2434 + input_section->output_section->vma
2435 + input_section->output_offset;
2436 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
2437 sreloc = elf_section_data (input_section)->sreloc;
2438 if (sreloc == NULL)
2439 abort ();
2440 loc = sreloc->contents;
2441 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2442 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2443 }
2444 /* Fall through */
2445
2446 case R_386_TLS_GD:
2447 case R_386_TLS_IE_32:
2448 case R_386_TLS_GOTIE:
2449 r_type = elf_i386_tls_transition (info, r_type, h == NULL);
2450 tls_type = GOT_UNKNOWN;
2451 if (h == NULL && local_got_offsets)
2452 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx];
2453 else if (h != NULL)
2454 {
2455 tls_type = elf_i386_hash_entry(h)->tls_type;
2456 if (!info->shared && h->dynindx == -1 && (tls_type & GOT_TLS_IE))
2457 r_type = R_386_TLS_LE_32;
2458 }
2459 if (tls_type == GOT_TLS_IE)
2460 tls_type = GOT_TLS_IE_NEG;
2461 if (r_type == R_386_TLS_GD)
2462 {
2463 if (tls_type == GOT_TLS_IE_POS)
2464 r_type = R_386_TLS_GOTIE;
2465 else if (tls_type & GOT_TLS_IE)
2466 r_type = R_386_TLS_IE_32;
2467 }
2468
2469 if (r_type == R_386_TLS_LE_32)
2470 {
2471 BFD_ASSERT (! unresolved_reloc);
2472 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD)
2473 {
2474 unsigned int val, type;
2475 bfd_vma roff;
2476
2477 /* GD->LE transition. */
2478 BFD_ASSERT (rel->r_offset >= 2);
2479 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2480 BFD_ASSERT (type == 0x8d || type == 0x04);
2481 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2482 BFD_ASSERT (bfd_get_8 (input_bfd,
2483 contents + rel->r_offset + 4)
2484 == 0xe8);
2485 BFD_ASSERT (rel + 1 < relend);
2486 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2487 roff = rel->r_offset + 5;
2488 val = bfd_get_8 (input_bfd,
2489 contents + rel->r_offset - 1);
2490 if (type == 0x04)
2491 {
2492 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2493 Change it into:
2494 movl %gs:0, %eax; subl $foo@tpoff, %eax
2495 (6 byte form of subl). */
2496 BFD_ASSERT (rel->r_offset >= 3);
2497 BFD_ASSERT (bfd_get_8 (input_bfd,
2498 contents + rel->r_offset - 3)
2499 == 0x8d);
2500 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2501 memcpy (contents + rel->r_offset - 3,
2502 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2503 }
2504 else
2505 {
2506 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2507 if (rel->r_offset + 10 <= input_section->size
2508 && bfd_get_8 (input_bfd,
2509 contents + rel->r_offset + 9) == 0x90)
2510 {
2511 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2512 Change it into:
2513 movl %gs:0, %eax; subl $foo@tpoff, %eax
2514 (6 byte form of subl). */
2515 memcpy (contents + rel->r_offset - 2,
2516 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2517 roff = rel->r_offset + 6;
2518 }
2519 else
2520 {
2521 /* leal foo(%reg), %eax; call ___tls_get_addr
2522 Change it into:
2523 movl %gs:0, %eax; subl $foo@tpoff, %eax
2524 (5 byte form of subl). */
2525 memcpy (contents + rel->r_offset - 2,
2526 "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2527 }
2528 }
2529 bfd_put_32 (output_bfd, tpoff (info, relocation),
2530 contents + roff);
2531 /* Skip R_386_PLT32. */
2532 rel++;
2533 continue;
2534 }
2535 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE)
2536 {
2537 unsigned int val, type;
2538
2539 /* IE->LE transition:
2540 Originally it can be one of:
2541 movl foo, %eax
2542 movl foo, %reg
2543 addl foo, %reg
2544 We change it into:
2545 movl $foo, %eax
2546 movl $foo, %reg
2547 addl $foo, %reg. */
2548 BFD_ASSERT (rel->r_offset >= 1);
2549 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2550 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2551 if (val == 0xa1)
2552 {
2553 /* movl foo, %eax. */
2554 bfd_put_8 (output_bfd, 0xb8,
2555 contents + rel->r_offset - 1);
2556 }
2557 else
2558 {
2559 BFD_ASSERT (rel->r_offset >= 2);
2560 type = bfd_get_8 (input_bfd,
2561 contents + rel->r_offset - 2);
2562 switch (type)
2563 {
2564 case 0x8b:
2565 /* movl */
2566 BFD_ASSERT ((val & 0xc7) == 0x05);
2567 bfd_put_8 (output_bfd, 0xc7,
2568 contents + rel->r_offset - 2);
2569 bfd_put_8 (output_bfd,
2570 0xc0 | ((val >> 3) & 7),
2571 contents + rel->r_offset - 1);
2572 break;
2573 case 0x03:
2574 /* addl */
2575 BFD_ASSERT ((val & 0xc7) == 0x05);
2576 bfd_put_8 (output_bfd, 0x81,
2577 contents + rel->r_offset - 2);
2578 bfd_put_8 (output_bfd,
2579 0xc0 | ((val >> 3) & 7),
2580 contents + rel->r_offset - 1);
2581 break;
2582 default:
2583 BFD_FAIL ();
2584 break;
2585 }
2586 }
2587 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2588 contents + rel->r_offset);
2589 continue;
2590 }
2591 else
2592 {
2593 unsigned int val, type;
2594
2595 /* {IE_32,GOTIE}->LE transition:
2596 Originally it can be one of:
2597 subl foo(%reg1), %reg2
2598 movl foo(%reg1), %reg2
2599 addl foo(%reg1), %reg2
2600 We change it into:
2601 subl $foo, %reg2
2602 movl $foo, %reg2 (6 byte form)
2603 addl $foo, %reg2. */
2604 BFD_ASSERT (rel->r_offset >= 2);
2605 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2606 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2607 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2608 BFD_ASSERT ((val & 0xc0) == 0x80 && (val & 7) != 4);
2609 if (type == 0x8b)
2610 {
2611 /* movl */
2612 bfd_put_8 (output_bfd, 0xc7,
2613 contents + rel->r_offset - 2);
2614 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2615 contents + rel->r_offset - 1);
2616 }
2617 else if (type == 0x2b)
2618 {
2619 /* subl */
2620 bfd_put_8 (output_bfd, 0x81,
2621 contents + rel->r_offset - 2);
2622 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7),
2623 contents + rel->r_offset - 1);
2624 }
2625 else if (type == 0x03)
2626 {
2627 /* addl */
2628 bfd_put_8 (output_bfd, 0x81,
2629 contents + rel->r_offset - 2);
2630 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2631 contents + rel->r_offset - 1);
2632 }
2633 else
2634 BFD_FAIL ();
2635 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE)
2636 bfd_put_32 (output_bfd, -tpoff (info, relocation),
2637 contents + rel->r_offset);
2638 else
2639 bfd_put_32 (output_bfd, tpoff (info, relocation),
2640 contents + rel->r_offset);
2641 continue;
2642 }
2643 }
2644
2645 if (htab->sgot == NULL)
2646 abort ();
2647
2648 if (h != NULL)
2649 off = h->got.offset;
2650 else
2651 {
2652 if (local_got_offsets == NULL)
2653 abort ();
2654
2655 off = local_got_offsets[r_symndx];
2656 }
2657
2658 if ((off & 1) != 0)
2659 off &= ~1;
2660 else
2661 {
2662 Elf_Internal_Rela outrel;
2663 bfd_byte *loc;
2664 int dr_type, indx;
2665
2666 if (htab->srelgot == NULL)
2667 abort ();
2668
2669 outrel.r_offset = (htab->sgot->output_section->vma
2670 + htab->sgot->output_offset + off);
2671
2672 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2673 if (r_type == R_386_TLS_GD)
2674 dr_type = R_386_TLS_DTPMOD32;
2675 else if (tls_type == GOT_TLS_IE_POS)
2676 dr_type = R_386_TLS_TPOFF;
2677 else
2678 dr_type = R_386_TLS_TPOFF32;
2679 if (dr_type == R_386_TLS_TPOFF && indx == 0)
2680 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
2681 htab->sgot->contents + off);
2682 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0)
2683 bfd_put_32 (output_bfd, dtpoff_base (info) - relocation,
2684 htab->sgot->contents + off);
2685 else
2686 bfd_put_32 (output_bfd, 0,
2687 htab->sgot->contents + off);
2688 outrel.r_info = ELF32_R_INFO (indx, dr_type);
2689 loc = htab->srelgot->contents;
2690 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
2691 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2692
2693 if (r_type == R_386_TLS_GD)
2694 {
2695 if (indx == 0)
2696 {
2697 BFD_ASSERT (! unresolved_reloc);
2698 bfd_put_32 (output_bfd,
2699 relocation - dtpoff_base (info),
2700 htab->sgot->contents + off + 4);
2701 }
2702 else
2703 {
2704 bfd_put_32 (output_bfd, 0,
2705 htab->sgot->contents + off + 4);
2706 outrel.r_info = ELF32_R_INFO (indx,
2707 R_386_TLS_DTPOFF32);
2708 outrel.r_offset += 4;
2709 htab->srelgot->reloc_count++;
2710 loc += sizeof (Elf32_External_Rel);
2711 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2712 }
2713 }
2714 else if (tls_type == GOT_TLS_IE_BOTH)
2715 {
2716 bfd_put_32 (output_bfd,
2717 indx == 0 ? relocation - dtpoff_base (info) : 0,
2718 htab->sgot->contents + off + 4);
2719 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2720 outrel.r_offset += 4;
2721 htab->srelgot->reloc_count++;
2722 loc += sizeof (Elf32_External_Rel);
2723 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2724 }
2725
2726 if (h != NULL)
2727 h->got.offset |= 1;
2728 else
2729 local_got_offsets[r_symndx] |= 1;
2730 }
2731
2732 if (off >= (bfd_vma) -2)
2733 abort ();
2734 if (r_type == ELF32_R_TYPE (rel->r_info))
2735 {
2736 bfd_vma g_o_t = htab->sgotplt->output_section->vma
2737 + htab->sgotplt->output_offset;
2738 relocation = htab->sgot->output_section->vma
2739 + htab->sgot->output_offset + off - g_o_t;
2740 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
2741 && tls_type == GOT_TLS_IE_BOTH)
2742 relocation += 4;
2743 if (r_type == R_386_TLS_IE)
2744 relocation += g_o_t;
2745 unresolved_reloc = FALSE;
2746 }
2747 else
2748 {
2749 unsigned int val, type;
2750 bfd_vma roff;
2751
2752 /* GD->IE transition. */
2753 BFD_ASSERT (rel->r_offset >= 2);
2754 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2755 BFD_ASSERT (type == 0x8d || type == 0x04);
2756 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2757 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2758 == 0xe8);
2759 BFD_ASSERT (rel + 1 < relend);
2760 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2761 roff = rel->r_offset - 3;
2762 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2763 if (type == 0x04)
2764 {
2765 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
2766 Change it into:
2767 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
2768 BFD_ASSERT (rel->r_offset >= 3);
2769 BFD_ASSERT (bfd_get_8 (input_bfd,
2770 contents + rel->r_offset - 3)
2771 == 0x8d);
2772 BFD_ASSERT ((val & 0xc7) == 0x05 && val != (4 << 3));
2773 val >>= 3;
2774 }
2775 else
2776 {
2777 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
2778 Change it into:
2779 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */
2780 BFD_ASSERT (rel->r_offset + 10 <= input_section->size);
2781 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2782 BFD_ASSERT (bfd_get_8 (input_bfd,
2783 contents + rel->r_offset + 9)
2784 == 0x90);
2785 roff = rel->r_offset - 2;
2786 }
2787 memcpy (contents + roff,
2788 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12);
2789 contents[roff + 7] = 0x80 | (val & 7);
2790 /* If foo is used only with foo@gotntpoff(%reg) and
2791 foo@indntpoff, but not with foo@gottpoff(%reg), change
2792 subl $foo@gottpoff(%reg), %eax
2793 into:
2794 addl $foo@gotntpoff(%reg), %eax. */
2795 if (r_type == R_386_TLS_GOTIE)
2796 {
2797 contents[roff + 6] = 0x03;
2798 if (tls_type == GOT_TLS_IE_BOTH)
2799 off += 4;
2800 }
2801 bfd_put_32 (output_bfd,
2802 htab->sgot->output_section->vma
2803 + htab->sgot->output_offset + off
2804 - htab->sgotplt->output_section->vma
2805 - htab->sgotplt->output_offset,
2806 contents + roff + 8);
2807 /* Skip R_386_PLT32. */
2808 rel++;
2809 continue;
2810 }
2811 break;
2812
2813 case R_386_TLS_LDM:
2814 if (! info->shared)
2815 {
2816 unsigned int val;
2817
2818 /* LD->LE transition:
2819 Ensure it is:
2820 leal foo(%reg), %eax; call ___tls_get_addr.
2821 We change it into:
2822 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */
2823 BFD_ASSERT (rel->r_offset >= 2);
2824 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2825 == 0x8d);
2826 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2827 BFD_ASSERT ((val & 0xf8) == 0x80 && (val & 7) != 4);
2828 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2829 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2830 == 0xe8);
2831 BFD_ASSERT (rel + 1 < relend);
2832 BFD_ASSERT (ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32);
2833 memcpy (contents + rel->r_offset - 2,
2834 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11);
2835 /* Skip R_386_PLT32. */
2836 rel++;
2837 continue;
2838 }
2839
2840 if (htab->sgot == NULL)
2841 abort ();
2842
2843 off = htab->tls_ldm_got.offset;
2844 if (off & 1)
2845 off &= ~1;
2846 else
2847 {
2848 Elf_Internal_Rela outrel;
2849 bfd_byte *loc;
2850
2851 if (htab->srelgot == NULL)
2852 abort ();
2853
2854 outrel.r_offset = (htab->sgot->output_section->vma
2855 + htab->sgot->output_offset + off);
2856
2857 bfd_put_32 (output_bfd, 0,
2858 htab->sgot->contents + off);
2859 bfd_put_32 (output_bfd, 0,
2860 htab->sgot->contents + off + 4);
2861 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32);
2862 loc = htab->srelgot->contents;
2863 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
2864 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2865 htab->tls_ldm_got.offset |= 1;
2866 }
2867 relocation = htab->sgot->output_section->vma
2868 + htab->sgot->output_offset + off
2869 - htab->sgotplt->output_section->vma
2870 - htab->sgotplt->output_offset;
2871 unresolved_reloc = FALSE;
2872 break;
2873
2874 case R_386_TLS_LDO_32:
2875 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2876 relocation -= dtpoff_base (info);
2877 else
2878 /* When converting LDO to LE, we must negate. */
2879 relocation = -tpoff (info, relocation);
2880 break;
2881
2882 case R_386_TLS_LE_32:
2883 case R_386_TLS_LE:
2884 if (info->shared)
2885 {
2886 Elf_Internal_Rela outrel;
2887 asection *sreloc;
2888 bfd_byte *loc;
2889 int indx;
2890
2891 outrel.r_offset = rel->r_offset
2892 + input_section->output_section->vma
2893 + input_section->output_offset;
2894 if (h != NULL && h->dynindx != -1)
2895 indx = h->dynindx;
2896 else
2897 indx = 0;
2898 if (r_type == R_386_TLS_LE_32)
2899 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32);
2900 else
2901 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF);
2902 sreloc = elf_section_data (input_section)->sreloc;
2903 if (sreloc == NULL)
2904 abort ();
2905 loc = sreloc->contents;
2906 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rel);
2907 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
2908 if (indx)
2909 continue;
2910 else if (r_type == R_386_TLS_LE_32)
2911 relocation = dtpoff_base (info) - relocation;
2912 else
2913 relocation -= dtpoff_base (info);
2914 }
2915 else if (r_type == R_386_TLS_LE_32)
2916 relocation = tpoff (info, relocation);
2917 else
2918 relocation = -tpoff (info, relocation);
2919 break;
2920
2921 default:
2922 break;
2923 }
2924
2925 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2926 because such sections are not SEC_ALLOC and thus ld.so will
2927 not process them. */
2928 if (unresolved_reloc
2929 && !((input_section->flags & SEC_DEBUGGING) != 0
2930 && h->def_dynamic))
2931 {
2932 (*_bfd_error_handler)
2933 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2934 input_bfd,
2935 input_section,
2936 (long) rel->r_offset,
2937 h->root.root.string);
2938 return FALSE;
2939 }
2940
2941 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2942 contents, rel->r_offset,
2943 relocation, 0);
2944
2945 if (r != bfd_reloc_ok)
2946 {
2947 const char *name;
2948
2949 if (h != NULL)
2950 name = h->root.root.string;
2951 else
2952 {
2953 name = bfd_elf_string_from_elf_section (input_bfd,
2954 symtab_hdr->sh_link,
2955 sym->st_name);
2956 if (name == NULL)
2957 return FALSE;
2958 if (*name == '\0')
2959 name = bfd_section_name (input_bfd, sec);
2960 }
2961
2962 if (r == bfd_reloc_overflow)
2963 {
2964 if (! ((*info->callbacks->reloc_overflow)
2965 (info, (h ? &h->root : NULL), name, howto->name,
2966 (bfd_vma) 0, input_bfd, input_section,
2967 rel->r_offset)))
2968 return FALSE;
2969 }
2970 else
2971 {
2972 (*_bfd_error_handler)
2973 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2974 input_bfd, input_section,
2975 (long) rel->r_offset, name, (int) r);
2976 return FALSE;
2977 }
2978 }
2979 }
2980
2981 return TRUE;
2982 }
2983
2984 /* Finish up dynamic symbol handling. We set the contents of various
2985 dynamic sections here. */
2986
2987 static bfd_boolean
2988 elf_i386_finish_dynamic_symbol (bfd *output_bfd,
2989 struct bfd_link_info *info,
2990 struct elf_link_hash_entry *h,
2991 Elf_Internal_Sym *sym)
2992 {
2993 struct elf_i386_link_hash_table *htab;
2994
2995 htab = elf_i386_hash_table (info);
2996
2997 if (h->plt.offset != (bfd_vma) -1)
2998 {
2999 bfd_vma plt_index;
3000 bfd_vma got_offset;
3001 Elf_Internal_Rela rel;
3002 bfd_byte *loc;
3003
3004 /* This symbol has an entry in the procedure linkage table. Set
3005 it up. */
3006
3007 if (h->dynindx == -1
3008 || htab->splt == NULL
3009 || htab->sgotplt == NULL
3010 || htab->srelplt == NULL)
3011 abort ();
3012
3013 /* Get the index in the procedure linkage table which
3014 corresponds to this symbol. This is the index of this symbol
3015 in all the symbols for which we are making plt entries. The
3016 first entry in the procedure linkage table is reserved. */
3017 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3018
3019 /* Get the offset into the .got table of the entry that
3020 corresponds to this function. Each .got entry is 4 bytes.
3021 The first three are reserved. */
3022 got_offset = (plt_index + 3) * 4;
3023
3024 /* Fill in the entry in the procedure linkage table. */
3025 if (! info->shared)
3026 {
3027 memcpy (htab->splt->contents + h->plt.offset, elf_i386_plt_entry,
3028 PLT_ENTRY_SIZE);
3029 bfd_put_32 (output_bfd,
3030 (htab->sgotplt->output_section->vma
3031 + htab->sgotplt->output_offset
3032 + got_offset),
3033 htab->splt->contents + h->plt.offset + 2);
3034 }
3035 else
3036 {
3037 memcpy (htab->splt->contents + h->plt.offset, elf_i386_pic_plt_entry,
3038 PLT_ENTRY_SIZE);
3039 bfd_put_32 (output_bfd, got_offset,
3040 htab->splt->contents + h->plt.offset + 2);
3041 }
3042
3043 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel),
3044 htab->splt->contents + h->plt.offset + 7);
3045 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3046 htab->splt->contents + h->plt.offset + 12);
3047
3048 /* Fill in the entry in the global offset table. */
3049 bfd_put_32 (output_bfd,
3050 (htab->splt->output_section->vma
3051 + htab->splt->output_offset
3052 + h->plt.offset
3053 + 6),
3054 htab->sgotplt->contents + got_offset);
3055
3056 /* Fill in the entry in the .rel.plt section. */
3057 rel.r_offset = (htab->sgotplt->output_section->vma
3058 + htab->sgotplt->output_offset
3059 + got_offset);
3060 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT);
3061 loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
3062 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3063
3064 if (!h->def_regular)
3065 {
3066 /* Mark the symbol as undefined, rather than as defined in
3067 the .plt section. Leave the value if there were any
3068 relocations where pointer equality matters (this is a clue
3069 for the dynamic linker, to make function pointer
3070 comparisons work between an application and shared
3071 library), otherwise set it to zero. If a function is only
3072 called from a binary, there is no need to slow down
3073 shared libraries because of that. */
3074 sym->st_shndx = SHN_UNDEF;
3075 if (!h->pointer_equality_needed)
3076 sym->st_value = 0;
3077 }
3078 }
3079
3080 if (h->got.offset != (bfd_vma) -1
3081 && elf_i386_hash_entry(h)->tls_type != GOT_TLS_GD
3082 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0)
3083 {
3084 Elf_Internal_Rela rel;
3085 bfd_byte *loc;
3086
3087 /* This symbol has an entry in the global offset table. Set it
3088 up. */
3089
3090 if (htab->sgot == NULL || htab->srelgot == NULL)
3091 abort ();
3092
3093 rel.r_offset = (htab->sgot->output_section->vma
3094 + htab->sgot->output_offset
3095 + (h->got.offset & ~(bfd_vma) 1));
3096
3097 /* If this is a static link, or it is a -Bsymbolic link and the
3098 symbol is defined locally or was forced to be local because
3099 of a version file, we just want to emit a RELATIVE reloc.
3100 The entry in the global offset table will already have been
3101 initialized in the relocate_section function. */
3102 if (info->shared
3103 && SYMBOL_REFERENCES_LOCAL (info, h))
3104 {
3105 BFD_ASSERT((h->got.offset & 1) != 0);
3106 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE);
3107 }
3108 else
3109 {
3110 BFD_ASSERT((h->got.offset & 1) == 0);
3111 bfd_put_32 (output_bfd, (bfd_vma) 0,
3112 htab->sgot->contents + h->got.offset);
3113 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT);
3114 }
3115
3116 loc = htab->srelgot->contents;
3117 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rel);
3118 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3119 }
3120
3121 if (h->needs_copy)
3122 {
3123 Elf_Internal_Rela rel;
3124 bfd_byte *loc;
3125
3126 /* This symbol needs a copy reloc. Set it up. */
3127
3128 if (h->dynindx == -1
3129 || (h->root.type != bfd_link_hash_defined
3130 && h->root.type != bfd_link_hash_defweak)
3131 || htab->srelbss == NULL)
3132 abort ();
3133
3134 rel.r_offset = (h->root.u.def.value
3135 + h->root.u.def.section->output_section->vma
3136 + h->root.u.def.section->output_offset);
3137 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY);
3138 loc = htab->srelbss->contents;
3139 loc += htab->srelbss->reloc_count++ * sizeof (Elf32_External_Rel);
3140 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
3141 }
3142
3143 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3144 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3145 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3146 sym->st_shndx = SHN_ABS;
3147
3148 return TRUE;
3149 }
3150
3151 /* Used to decide how to sort relocs in an optimal manner for the
3152 dynamic linker, before writing them out. */
3153
3154 static enum elf_reloc_type_class
3155 elf_i386_reloc_type_class (const Elf_Internal_Rela *rela)
3156 {
3157 switch (ELF32_R_TYPE (rela->r_info))
3158 {
3159 case R_386_RELATIVE:
3160 return reloc_class_relative;
3161 case R_386_JUMP_SLOT:
3162 return reloc_class_plt;
3163 case R_386_COPY:
3164 return reloc_class_copy;
3165 default:
3166 return reloc_class_normal;
3167 }
3168 }
3169
3170 /* Finish up the dynamic sections. */
3171
3172 static bfd_boolean
3173 elf_i386_finish_dynamic_sections (bfd *output_bfd,
3174 struct bfd_link_info *info)
3175 {
3176 struct elf_i386_link_hash_table *htab;
3177 bfd *dynobj;
3178 asection *sdyn;
3179
3180 htab = elf_i386_hash_table (info);
3181 dynobj = htab->elf.dynobj;
3182 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3183
3184 if (htab->elf.dynamic_sections_created)
3185 {
3186 Elf32_External_Dyn *dyncon, *dynconend;
3187
3188 if (sdyn == NULL || htab->sgot == NULL)
3189 abort ();
3190
3191 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3192 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
3193 for (; dyncon < dynconend; dyncon++)
3194 {
3195 Elf_Internal_Dyn dyn;
3196 asection *s;
3197
3198 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3199
3200 switch (dyn.d_tag)
3201 {
3202 default:
3203 continue;
3204
3205 case DT_PLTGOT:
3206 s = htab->sgotplt;
3207 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3208 break;
3209
3210 case DT_JMPREL:
3211 s = htab->srelplt;
3212 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3213 break;
3214
3215 case DT_PLTRELSZ:
3216 s = htab->srelplt;
3217 dyn.d_un.d_val = s->size;
3218 break;
3219
3220 case DT_RELSZ:
3221 /* My reading of the SVR4 ABI indicates that the
3222 procedure linkage table relocs (DT_JMPREL) should be
3223 included in the overall relocs (DT_REL). This is
3224 what Solaris does. However, UnixWare can not handle
3225 that case. Therefore, we override the DT_RELSZ entry
3226 here to make it not include the JMPREL relocs. */
3227 s = htab->srelplt;
3228 if (s == NULL)
3229 continue;
3230 dyn.d_un.d_val -= s->size;
3231 break;
3232
3233 case DT_REL:
3234 /* We may not be using the standard ELF linker script.
3235 If .rel.plt is the first .rel section, we adjust
3236 DT_REL to not include it. */
3237 s = htab->srelplt;
3238 if (s == NULL)
3239 continue;
3240 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
3241 continue;
3242 dyn.d_un.d_ptr += s->size;
3243 break;
3244 }
3245
3246 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3247 }
3248
3249 /* Fill in the first entry in the procedure linkage table. */
3250 if (htab->splt && htab->splt->size > 0)
3251 {
3252 if (info->shared)
3253 memcpy (htab->splt->contents,
3254 elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
3255 else
3256 {
3257 memcpy (htab->splt->contents,
3258 elf_i386_plt0_entry, PLT_ENTRY_SIZE);
3259 bfd_put_32 (output_bfd,
3260 (htab->sgotplt->output_section->vma
3261 + htab->sgotplt->output_offset
3262 + 4),
3263 htab->splt->contents + 2);
3264 bfd_put_32 (output_bfd,
3265 (htab->sgotplt->output_section->vma
3266 + htab->sgotplt->output_offset
3267 + 8),
3268 htab->splt->contents + 8);
3269 }
3270
3271 /* UnixWare sets the entsize of .plt to 4, although that doesn't
3272 really seem like the right value. */
3273 elf_section_data (htab->splt->output_section)
3274 ->this_hdr.sh_entsize = 4;
3275 }
3276 }
3277
3278 if (htab->sgotplt)
3279 {
3280 /* Fill in the first three entries in the global offset table. */
3281 if (htab->sgotplt->size > 0)
3282 {
3283 bfd_put_32 (output_bfd,
3284 (sdyn == NULL ? 0
3285 : sdyn->output_section->vma + sdyn->output_offset),
3286 htab->sgotplt->contents);
3287 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 4);
3288 bfd_put_32 (output_bfd, 0, htab->sgotplt->contents + 8);
3289 }
3290
3291 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
3292 }
3293
3294 if (htab->sgot && htab->sgot->size > 0)
3295 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
3296
3297 return TRUE;
3298 }
3299
3300 /* Return address for Ith PLT stub in section PLT, for relocation REL
3301 or (bfd_vma) -1 if it should not be included. */
3302
3303 static bfd_vma
3304 elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
3305 const arelent *rel ATTRIBUTE_UNUSED)
3306 {
3307 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3308 }
3309
3310
3311 #define TARGET_LITTLE_SYM bfd_elf32_i386_vec
3312 #define TARGET_LITTLE_NAME "elf32-i386"
3313 #define ELF_ARCH bfd_arch_i386
3314 #define ELF_MACHINE_CODE EM_386
3315 #define ELF_MAXPAGESIZE 0x1000
3316
3317 #define elf_backend_can_gc_sections 1
3318 #define elf_backend_can_refcount 1
3319 #define elf_backend_want_got_plt 1
3320 #define elf_backend_plt_readonly 1
3321 #define elf_backend_want_plt_sym 0
3322 #define elf_backend_got_header_size 12
3323
3324 /* Support RELA for objdump of prelink objects. */
3325 #define elf_info_to_howto elf_i386_info_to_howto_rel
3326 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel
3327
3328 #define bfd_elf32_mkobject elf_i386_mkobject
3329
3330 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name
3331 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create
3332 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup
3333
3334 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol
3335 #define elf_backend_check_relocs elf_i386_check_relocs
3336 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol
3337 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections
3338 #define elf_backend_fake_sections elf_i386_fake_sections
3339 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections
3340 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol
3341 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook
3342 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook
3343 #define elf_backend_grok_prstatus elf_i386_grok_prstatus
3344 #define elf_backend_grok_psinfo elf_i386_grok_psinfo
3345 #define elf_backend_reloc_type_class elf_i386_reloc_type_class
3346 #define elf_backend_relocate_section elf_i386_relocate_section
3347 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections
3348 #define elf_backend_plt_sym_val elf_i386_plt_sym_val
3349
3350 #include "elf32-target.h"
3351
3352 /* FreeBSD support. */
3353
3354 #undef TARGET_LITTLE_SYM
3355 #define TARGET_LITTLE_SYM bfd_elf32_i386_freebsd_vec
3356 #undef TARGET_LITTLE_NAME
3357 #define TARGET_LITTLE_NAME "elf32-i386-freebsd"
3358
3359 /* The kernel recognizes executables as valid only if they carry a
3360 "FreeBSD" label in the ELF header. So we put this label on all
3361 executables and (for simplicity) also all other object files. */
3362
3363 static void
3364 elf_i386_post_process_headers (bfd *abfd,
3365 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3366 {
3367 Elf_Internal_Ehdr *i_ehdrp;
3368
3369 i_ehdrp = elf_elfheader (abfd);
3370
3371 /* Put an ABI label supported by FreeBSD >= 4.1. */
3372 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
3373 #ifdef OLD_FREEBSD_ABI_LABEL
3374 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
3375 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
3376 #endif
3377 }
3378
3379 #undef elf_backend_post_process_headers
3380 #define elf_backend_post_process_headers elf_i386_post_process_headers
3381 #undef elf32_bed
3382 #define elf32_bed elf32_i386_fbsd_bed
3383
3384 #include "elf32-target.h"
This page took 0.149361 seconds and 5 git commands to generate.