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.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 /* 386 uses REL relocations instead of RELA. */
32 static reloc_howto_type elf_howto_table
[]=
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
),
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)
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
),
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
),
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)
133 /* GNU extension to record C++ vtable hierarchy. */
134 HOWTO (R_386_GNU_VTINHERIT
, /* type */
136 2, /* size (0 = byte, 1 = short, 2 = long) */
138 FALSE
, /* pc_relative */
140 complain_overflow_dont
, /* complain_on_overflow */
141 NULL
, /* special_function */
142 "R_386_GNU_VTINHERIT", /* name */
143 FALSE
, /* partial_inplace */
146 FALSE
), /* pcrel_offset */
148 /* GNU extension to record C++ vtable member usage. */
149 HOWTO (R_386_GNU_VTENTRY
, /* type */
151 2, /* size (0 = byte, 1 = short, 2 = long) */
153 FALSE
, /* pc_relative */
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 */
161 FALSE
) /* pcrel_offset */
163 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset)
167 #ifdef DEBUG_GEN_RELOC
169 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str)
174 static reloc_howto_type
*
175 elf_i386_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
176 bfd_reloc_code_real_type code
)
181 TRACE ("BFD_RELOC_NONE");
182 return &elf_howto_table
[R_386_NONE
];
185 TRACE ("BFD_RELOC_32");
186 return &elf_howto_table
[R_386_32
];
189 TRACE ("BFD_RELOC_CTOR");
190 return &elf_howto_table
[R_386_32
];
192 case BFD_RELOC_32_PCREL
:
193 TRACE ("BFD_RELOC_PC32");
194 return &elf_howto_table
[R_386_PC32
];
196 case BFD_RELOC_386_GOT32
:
197 TRACE ("BFD_RELOC_386_GOT32");
198 return &elf_howto_table
[R_386_GOT32
];
200 case BFD_RELOC_386_PLT32
:
201 TRACE ("BFD_RELOC_386_PLT32");
202 return &elf_howto_table
[R_386_PLT32
];
204 case BFD_RELOC_386_COPY
:
205 TRACE ("BFD_RELOC_386_COPY");
206 return &elf_howto_table
[R_386_COPY
];
208 case BFD_RELOC_386_GLOB_DAT
:
209 TRACE ("BFD_RELOC_386_GLOB_DAT");
210 return &elf_howto_table
[R_386_GLOB_DAT
];
212 case BFD_RELOC_386_JUMP_SLOT
:
213 TRACE ("BFD_RELOC_386_JUMP_SLOT");
214 return &elf_howto_table
[R_386_JUMP_SLOT
];
216 case BFD_RELOC_386_RELATIVE
:
217 TRACE ("BFD_RELOC_386_RELATIVE");
218 return &elf_howto_table
[R_386_RELATIVE
];
220 case BFD_RELOC_386_GOTOFF
:
221 TRACE ("BFD_RELOC_386_GOTOFF");
222 return &elf_howto_table
[R_386_GOTOFF
];
224 case BFD_RELOC_386_GOTPC
:
225 TRACE ("BFD_RELOC_386_GOTPC");
226 return &elf_howto_table
[R_386_GOTPC
];
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
];
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
];
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
];
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
];
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
];
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
];
254 TRACE ("BFD_RELOC_16");
255 return &elf_howto_table
[R_386_16
- R_386_ext_offset
];
257 case BFD_RELOC_16_PCREL
:
258 TRACE ("BFD_RELOC_16_PCREL");
259 return &elf_howto_table
[R_386_PC16
- R_386_ext_offset
];
262 TRACE ("BFD_RELOC_8");
263 return &elf_howto_table
[R_386_8
- R_386_ext_offset
];
265 case BFD_RELOC_8_PCREL
:
266 TRACE ("BFD_RELOC_8_PCREL");
267 return &elf_howto_table
[R_386_PC8
- R_386_ext_offset
];
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
];
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
];
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
];
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
];
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
];
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
];
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
];
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
];
311 elf_i386_info_to_howto_rel (bfd
*abfd ATTRIBUTE_UNUSED
,
313 Elf_Internal_Rela
*dst
)
315 unsigned int r_type
= ELF32_R_TYPE (dst
->r_info
);
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
))
326 (*_bfd_error_handler
) (_("%B: invalid relocation type %d"),
330 cache_ptr
->howto
= &elf_howto_table
[indx
];
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. */
340 elf_i386_is_local_label_name (bfd
*abfd
, const char *name
)
342 if (name
[0] == '.' && name
[1] == 'X')
345 return _bfd_elf_is_local_label_name (abfd
, name
);
348 /* Support for core dump NOTE sections. */
351 elf_i386_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
356 if (note
->namesz
== 8 && strcmp (note
->namedata
, "FreeBSD") == 0)
358 int pr_version
= bfd_get_32 (abfd
, note
->descdata
);
364 elf_tdata (abfd
)->core_signal
= bfd_get_32 (abfd
, note
->descdata
+ 20);
367 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
371 size
= bfd_get_32 (abfd
, note
->descdata
+ 8);
375 switch (note
->descsz
)
380 case 144: /* Linux/i386 */
382 elf_tdata (abfd
)->core_signal
= bfd_get_16 (abfd
, note
->descdata
+ 12);
385 elf_tdata (abfd
)->core_pid
= bfd_get_32 (abfd
, note
->descdata
+ 24);
395 /* Make a ".reg/999" section. */
396 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
397 size
, note
->descpos
+ offset
);
401 elf_i386_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
403 if (note
->namesz
== 8 && strcmp (note
->namedata
, "FreeBSD") == 0)
405 int pr_version
= bfd_get_32 (abfd
, note
->descdata
);
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);
417 switch (note
->descsz
)
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);
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. */
434 char *command
= elf_tdata (abfd
)->core_command
;
435 int n
= strlen (command
);
437 if (0 < n
&& command
[n
- 1] == ' ')
438 command
[n
- 1] = '\0';
444 /* Functions for the i386 ELF linker.
446 In order to gain some understanding of code in this file without
447 knowing all the intricate details of the linker, note the
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. */
458 /* The name of the dynamic interpreter. This is put in the .interp
461 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
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
467 #define ELIMINATE_COPY_RELOCS 1
469 /* The size in bytes of an entry in the procedure linkage table. */
471 #define PLT_ENTRY_SIZE 16
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. */
476 static const bfd_byte elf_i386_plt0_entry
[PLT_ENTRY_SIZE
] =
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. */
485 /* Subsequent entries in an absolute procedure linkage table look like
488 static const bfd_byte elf_i386_plt_entry
[PLT_ENTRY_SIZE
] =
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. */
498 /* The first entry in a PIC procedure linkage table look like this. */
500 static const bfd_byte elf_i386_pic_plt0_entry
[PLT_ENTRY_SIZE
] =
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. */
507 /* Subsequent entries in a PIC procedure linkage table look like this. */
509 static const bfd_byte elf_i386_pic_plt_entry
[PLT_ENTRY_SIZE
] =
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. */
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. */
525 struct elf_i386_dyn_relocs
527 struct elf_i386_dyn_relocs
*next
;
529 /* The input section of the reloc. */
532 /* Total number of relocs copied for the input section. */
535 /* Number of pc-relative relocs copied for the input section. */
536 bfd_size_type pc_count
;
539 /* i386 ELF linker hash entry. */
541 struct elf_i386_link_hash_entry
543 struct elf_link_hash_entry elf
;
545 /* Track dynamic relocs copied for this symbol. */
546 struct elf_i386_dyn_relocs
*dyn_relocs
;
548 #define GOT_UNKNOWN 0
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
;
558 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent))
560 struct elf_i386_obj_tdata
562 struct elf_obj_tdata root
;
564 /* tls_type for each local got entry. */
565 char *local_got_tls_type
;
568 #define elf_i386_tdata(abfd) \
569 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any)
571 #define elf_i386_local_got_tls_type(abfd) \
572 (elf_i386_tdata (abfd)->local_got_tls_type)
575 elf_i386_mkobject (bfd
*abfd
)
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
)
584 /* i386 ELF linker hash table. */
586 struct elf_i386_link_hash_table
588 struct elf_link_hash_table elf
;
590 /* Short-cuts to get to dynamic linker sections. */
600 bfd_signed_vma refcount
;
604 /* Small local sym to section mapping cache. */
605 struct sym_sec_cache sym_sec
;
608 /* Get the i386 ELF linker hash table from a link_info structure. */
610 #define elf_i386_hash_table(p) \
611 ((struct elf_i386_link_hash_table *) ((p)->hash))
613 /* Create an entry in an i386 ELF linker hash table. */
615 static struct bfd_hash_entry
*
616 link_hash_newfunc (struct bfd_hash_entry
*entry
,
617 struct bfd_hash_table
*table
,
620 /* Allocate the structure if it has not already been allocated by a
624 entry
= bfd_hash_allocate (table
,
625 sizeof (struct elf_i386_link_hash_entry
));
630 /* Call the allocation method of the superclass. */
631 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
634 struct elf_i386_link_hash_entry
*eh
;
636 eh
= (struct elf_i386_link_hash_entry
*) entry
;
637 eh
->dyn_relocs
= NULL
;
638 eh
->tls_type
= GOT_UNKNOWN
;
644 /* Create an i386 ELF linker hash table. */
646 static struct bfd_link_hash_table
*
647 elf_i386_link_hash_table_create (bfd
*abfd
)
649 struct elf_i386_link_hash_table
*ret
;
650 bfd_size_type amt
= sizeof (struct elf_i386_link_hash_table
);
652 ret
= bfd_malloc (amt
);
656 if (! _bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
))
669 ret
->tls_ldm_got
.refcount
= 0;
670 ret
->sym_sec
.abfd
= NULL
;
672 return &ret
->elf
.root
;
675 /* Create .got, .gotplt, and .rel.got sections in DYNOBJ, and set up
676 shortcuts to them in our hash table. */
679 create_got_section (bfd
*dynobj
, struct bfd_link_info
*info
)
681 struct elf_i386_link_hash_table
*htab
;
683 if (! _bfd_elf_create_got_section (dynobj
, info
))
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
)
692 htab
->srelgot
= bfd_make_section_with_flags (dynobj
, ".rel.got",
693 (SEC_ALLOC
| SEC_LOAD
698 if (htab
->srelgot
== NULL
699 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 2))
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
709 elf_i386_create_dynamic_sections (bfd
*dynobj
, struct bfd_link_info
*info
)
711 struct elf_i386_link_hash_table
*htab
;
713 htab
= elf_i386_hash_table (info
);
714 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
717 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
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");
724 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rel.bss");
726 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
727 || (!info
->shared
&& !htab
->srelbss
))
733 /* Copy the extra info we tack onto an elf_link_hash_entry. */
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
)
740 struct elf_i386_link_hash_entry
*edir
, *eind
;
742 edir
= (struct elf_i386_link_hash_entry
*) dir
;
743 eind
= (struct elf_i386_link_hash_entry
*) ind
;
745 if (eind
->dyn_relocs
!= NULL
)
747 if (edir
->dyn_relocs
!= NULL
)
749 struct elf_i386_dyn_relocs
**pp
;
750 struct elf_i386_dyn_relocs
*p
;
752 if (ind
->root
.type
== bfd_link_hash_indirect
)
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
; )
759 struct elf_i386_dyn_relocs
*q
;
761 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
762 if (q
->sec
== p
->sec
)
764 q
->pc_count
+= p
->pc_count
;
765 q
->count
+= p
->count
;
772 *pp
= edir
->dyn_relocs
;
775 edir
->dyn_relocs
= eind
->dyn_relocs
;
776 eind
->dyn_relocs
= NULL
;
779 if (ind
->root
.type
== bfd_link_hash_indirect
780 && dir
->got
.refcount
<= 0)
782 edir
->tls_type
= eind
->tls_type
;
783 eind
->tls_type
= GOT_UNKNOWN
;
786 if (ELIMINATE_COPY_RELOCS
787 && ind
->root
.type
!= bfd_link_hash_indirect
788 && dir
->dynamic_adjusted
)
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
;
800 _bfd_elf_link_hash_copy_indirect (bed
, dir
, ind
);
804 elf_i386_tls_transition (struct bfd_link_info
*info
, int r_type
, int is_local
)
812 case R_386_TLS_IE_32
:
814 return R_386_TLS_LE_32
;
815 return R_386_TLS_IE_32
;
817 case R_386_TLS_GOTIE
:
819 return R_386_TLS_LE_32
;
822 return R_386_TLS_LE_32
;
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. */
833 elf_i386_check_relocs (bfd
*abfd
,
834 struct bfd_link_info
*info
,
836 const Elf_Internal_Rela
*relocs
)
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
;
845 if (info
->relocatable
)
848 htab
= elf_i386_hash_table (info
);
849 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
850 sym_hashes
= elf_sym_hashes (abfd
);
854 rel_end
= relocs
+ sec
->reloc_count
;
855 for (rel
= relocs
; rel
< rel_end
; rel
++)
858 unsigned long r_symndx
;
859 struct elf_link_hash_entry
*h
;
861 r_symndx
= ELF32_R_SYM (rel
->r_info
);
862 r_type
= ELF32_R_TYPE (rel
->r_info
);
864 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
866 (*_bfd_error_handler
) (_("%B: bad symbol index: %d"),
872 if (r_symndx
< symtab_hdr
->sh_info
)
875 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
877 r_type
= elf_i386_tls_transition (info
, r_type
, h
== NULL
);
882 htab
->tls_ldm_got
.refcount
+= 1;
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
893 /* If this is a local symbol, we resolve it directly without
894 creating a procedure linkage table entry. */
899 h
->plt
.refcount
+= 1;
902 case R_386_TLS_IE_32
:
904 case R_386_TLS_GOTIE
:
906 info
->flags
|= DF_STATIC_TLS
;
911 /* This symbol requires a global offset table entry. */
913 int tls_type
, old_tls_type
;
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
;
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
;
929 case R_386_TLS_GOTIE
:
930 tls_type
= GOT_TLS_IE_POS
; break;
935 h
->got
.refcount
+= 1;
936 old_tls_type
= elf_i386_hash_entry(h
)->tls_type
;
940 bfd_signed_vma
*local_got_refcounts
;
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
)
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
)
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
);
957 local_got_refcounts
[r_symndx
] += 1;
958 old_tls_type
= elf_i386_local_got_tls_type (abfd
) [r_symndx
];
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))
969 if ((old_tls_type
& GOT_TLS_IE
) && tls_type
== GOT_TLS_GD
)
970 tls_type
= old_tls_type
;
973 (*_bfd_error_handler
)
974 (_("%B: `%s' accessed both as normal and "
975 "thread local symbol"),
977 h
? h
->root
.root
.string
: "<local>");
982 if (old_tls_type
!= tls_type
)
985 elf_i386_hash_entry (h
)->tls_type
= tls_type
;
987 elf_i386_local_got_tls_type (abfd
) [r_symndx
] = tls_type
;
995 if (htab
->sgot
== NULL
)
997 if (htab
->elf
.dynobj
== NULL
)
998 htab
->elf
.dynobj
= abfd
;
999 if (!create_got_section (htab
->elf
.dynobj
, info
))
1002 if (r_type
!= R_386_TLS_IE
)
1006 case R_386_TLS_LE_32
:
1010 info
->flags
|= DF_STATIC_TLS
;
1015 if (h
!= NULL
&& !info
->shared
)
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. */
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;
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
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
1054 && (sec
->flags
& SEC_ALLOC
) != 0
1055 && (r_type
!= R_386_PC32
1057 && (! info
->symbolic
1058 || h
->root
.type
== bfd_link_hash_defweak
1059 || !h
->def_regular
))))
1060 || (ELIMINATE_COPY_RELOCS
1062 && (sec
->flags
& SEC_ALLOC
) != 0
1064 && (h
->root
.type
== bfd_link_hash_defweak
1065 || !h
->def_regular
)))
1067 struct elf_i386_dyn_relocs
*p
;
1068 struct elf_i386_dyn_relocs
**head
;
1070 /* We must copy these reloc types into the output file.
1071 Create a reloc section in dynobj and make room for
1077 unsigned int strndx
= elf_elfheader (abfd
)->e_shstrndx
;
1078 unsigned int shnam
= elf_section_data (sec
)->rel_hdr
.sh_name
;
1080 name
= bfd_elf_string_from_elf_section (abfd
, strndx
, shnam
);
1084 if (strncmp (name
, ".rel", 4) != 0
1085 || strcmp (bfd_get_section_name (abfd
, sec
),
1088 (*_bfd_error_handler
)
1089 (_("%B: bad relocation section name `%s\'"),
1093 if (htab
->elf
.dynobj
== NULL
)
1094 htab
->elf
.dynobj
= abfd
;
1096 dynobj
= htab
->elf
.dynobj
;
1097 sreloc
= bfd_get_section_by_name (dynobj
, name
);
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
,
1110 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
1113 elf_section_data (sec
)->sreloc
= sreloc
;
1116 /* If this is a global symbol, we count the number of
1117 relocations we need for this symbol. */
1120 head
= &((struct elf_i386_link_hash_entry
*) h
)->dyn_relocs
;
1124 /* Track dynamic relocs needed for local syms too.
1125 We really need local syms available to do this
1129 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
1134 head
= ((struct elf_i386_dyn_relocs
**)
1135 &elf_section_data (s
)->local_dynrel
);
1139 if (p
== NULL
|| p
->sec
!= sec
)
1141 bfd_size_type amt
= sizeof *p
;
1142 p
= bfd_alloc (htab
->elf
.dynobj
, amt
);
1153 if (r_type
== R_386_PC32
)
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
))
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
))
1180 /* Return the section that should be marked against GC for a given
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
)
1192 switch (ELF32_R_TYPE (rel
->r_info
))
1194 case R_386_GNU_VTINHERIT
:
1195 case R_386_GNU_VTENTRY
:
1199 switch (h
->root
.type
)
1201 case bfd_link_hash_defined
:
1202 case bfd_link_hash_defweak
:
1203 return h
->root
.u
.def
.section
;
1205 case bfd_link_hash_common
:
1206 return h
->root
.u
.c
.p
->section
;
1214 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
1219 /* Update the got entry reference counts for the section being removed. */
1222 elf_i386_gc_sweep_hook (bfd
*abfd
,
1223 struct bfd_link_info
*info
,
1225 const Elf_Internal_Rela
*relocs
)
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
;
1232 elf_section_data (sec
)->local_dynrel
= NULL
;
1234 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1235 sym_hashes
= elf_sym_hashes (abfd
);
1236 local_got_refcounts
= elf_local_got_refcounts (abfd
);
1238 relend
= relocs
+ sec
->reloc_count
;
1239 for (rel
= relocs
; rel
< relend
; rel
++)
1241 unsigned long r_symndx
;
1242 unsigned int r_type
;
1243 struct elf_link_hash_entry
*h
= NULL
;
1245 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1246 if (r_symndx
>= symtab_hdr
->sh_info
)
1248 struct elf_i386_link_hash_entry
*eh
;
1249 struct elf_i386_dyn_relocs
**pp
;
1250 struct elf_i386_dyn_relocs
*p
;
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
;
1258 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
1261 /* Everything must go for SEC. */
1267 r_type
= ELF32_R_TYPE (rel
->r_info
);
1268 r_type
= elf_i386_tls_transition (info
, r_type
, h
!= NULL
);
1272 if (elf_i386_hash_table (info
)->tls_ldm_got
.refcount
> 0)
1273 elf_i386_hash_table (info
)->tls_ldm_got
.refcount
-= 1;
1277 case R_386_TLS_IE_32
:
1279 case R_386_TLS_GOTIE
:
1283 if (h
->got
.refcount
> 0)
1284 h
->got
.refcount
-= 1;
1286 else if (local_got_refcounts
!= NULL
)
1288 if (local_got_refcounts
[r_symndx
] > 0)
1289 local_got_refcounts
[r_symndx
] -= 1;
1302 if (h
->plt
.refcount
> 0)
1303 h
->plt
.refcount
-= 1;
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
1322 elf_i386_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1323 struct elf_link_hash_entry
*h
)
1325 struct elf_i386_link_hash_table
*htab
;
1327 unsigned int power_of_two
;
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
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
))
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;
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;
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
)
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
;
1373 /* This is a reference to a symbol defined by a dynamic object which
1374 is not a function. */
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. */
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
)
1388 /* If -z nocopyreloc was given, we won't generate them either. */
1389 if (info
->nocopyreloc
)
1395 if (ELIMINATE_COPY_RELOCS
)
1397 struct elf_i386_link_hash_entry
* eh
;
1398 struct elf_i386_dyn_relocs
*p
;
1400 eh
= (struct elf_i386_link_hash_entry
*) h
;
1401 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1403 s
= p
->sec
->output_section
;
1404 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
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. */
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. */
1427 htab
= elf_i386_hash_table (info
);
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)
1434 htab
->srelbss
->size
+= sizeof (Elf32_External_Rel
);
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)
1444 /* Apply the required alignment. */
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
))
1449 if (! bfd_set_section_alignment (htab
->elf
.dynobj
, s
, power_of_two
))
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
;
1457 /* Increment the section size to make room for the symbol. */
1463 /* Allocate space in .plt, .got and associated reloc sections for
1467 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
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
;
1474 if (h
->root
.type
== bfd_link_hash_indirect
)
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
;
1483 info
= (struct bfd_link_info
*) inf
;
1484 htab
= elf_i386_hash_table (info
);
1486 if (htab
->elf
.dynamic_sections_created
1487 && h
->plt
.refcount
> 0)
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
)
1494 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1499 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h
))
1501 asection
*s
= htab
->splt
;
1503 /* If this is the first .plt entry, make room for the special
1506 s
->size
+= PLT_ENTRY_SIZE
;
1508 h
->plt
.offset
= s
->size
;
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. */
1518 h
->root
.u
.def
.section
= s
;
1519 h
->root
.u
.def
.value
= h
->plt
.offset
;
1522 /* Make room for this entry. */
1523 s
->size
+= PLT_ENTRY_SIZE
;
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;
1529 /* We also need to make an entry in the .rel.plt section. */
1530 htab
->srelplt
->size
+= sizeof (Elf32_External_Rel
);
1534 h
->plt
.offset
= (bfd_vma
) -1;
1540 h
->plt
.offset
= (bfd_vma
) -1;
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
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)
1555 int tls_type
= elf_i386_hash_entry(h
)->tls_type
;
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
)
1562 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1567 h
->got
.offset
= s
->size
;
1569 /* R_386_TLS_GD needs 2 consecutive GOT slots. */
1570 if (tls_type
== GOT_TLS_GD
|| tls_type
== GOT_TLS_IE_BOTH
)
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
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
)
1588 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, 0, h
)))
1589 htab
->srelgot
->size
+= sizeof (Elf32_External_Rel
);
1592 h
->got
.offset
= (bfd_vma
) -1;
1594 eh
= (struct elf_i386_link_hash_entry
*) h
;
1595 if (eh
->dyn_relocs
== NULL
)
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. */
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
))
1614 struct elf_i386_dyn_relocs
**pp
;
1616 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1618 p
->count
-= p
->pc_count
;
1627 /* Also discard relocs on undefined weak syms with non-default
1629 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1630 && h
->root
.type
== bfd_link_hash_undefweak
)
1631 eh
->dyn_relocs
= NULL
;
1633 else if (ELIMINATE_COPY_RELOCS
)
1635 /* For the non-shared case, discard space for relocs against
1636 symbols which turn out to need copy relocs or are not
1642 || (htab
->elf
.dynamic_sections_created
1643 && (h
->root
.type
== bfd_link_hash_undefweak
1644 || h
->root
.type
== bfd_link_hash_undefined
))))
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
)
1651 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1655 /* If that succeeded, we know we'll be keeping all the
1657 if (h
->dynindx
!= -1)
1661 eh
->dyn_relocs
= NULL
;
1666 /* Finally, allocate space. */
1667 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1669 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1670 sreloc
->size
+= p
->count
* sizeof (Elf32_External_Rel
);
1676 /* Find any dynamic relocs that apply to read-only sections. */
1679 readonly_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1681 struct elf_i386_link_hash_entry
*eh
;
1682 struct elf_i386_dyn_relocs
*p
;
1684 if (h
->root
.type
== bfd_link_hash_warning
)
1685 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1687 eh
= (struct elf_i386_link_hash_entry
*) h
;
1688 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1690 asection
*s
= p
->sec
->output_section
;
1692 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1694 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1696 info
->flags
|= DF_TEXTREL
;
1698 /* Not an error, just cut short the traversal. */
1705 /* Set the sizes of the dynamic sections. */
1708 elf_i386_size_dynamic_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1709 struct bfd_link_info
*info
)
1711 struct elf_i386_link_hash_table
*htab
;
1717 htab
= elf_i386_hash_table (info
);
1718 dynobj
= htab
->elf
.dynobj
;
1722 if (htab
->elf
.dynamic_sections_created
)
1724 /* Set the contents of the .interp section to the interpreter. */
1725 if (info
->executable
)
1727 s
= bfd_get_section_by_name (dynobj
, ".interp");
1730 s
->size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1731 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1735 /* Set up .got offsets for local syms, and space for local dynamic
1737 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
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
;
1746 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
1749 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1751 struct elf_i386_dyn_relocs
*p
;
1753 for (p
= *((struct elf_i386_dyn_relocs
**)
1754 &elf_section_data (s
)->local_dynrel
);
1758 if (!bfd_is_abs_section (p
->sec
)
1759 && bfd_is_abs_section (p
->sec
->output_section
))
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
1766 else if (p
->count
!= 0)
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
;
1776 local_got
= elf_local_got_refcounts (ibfd
);
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
);
1785 srel
= htab
->srelgot
;
1786 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1790 *local_got
= s
->size
;
1792 if (*local_tls_type
== GOT_TLS_GD
1793 || *local_tls_type
== GOT_TLS_IE_BOTH
)
1796 || *local_tls_type
== GOT_TLS_GD
1797 || (*local_tls_type
& GOT_TLS_IE
))
1799 if (*local_tls_type
== GOT_TLS_IE_BOTH
)
1800 srel
->size
+= 2 * sizeof (Elf32_External_Rel
);
1802 srel
->size
+= sizeof (Elf32_External_Rel
);
1806 *local_got
= (bfd_vma
) -1;
1810 if (htab
->tls_ldm_got
.refcount
> 0)
1812 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
1814 htab
->tls_ldm_got
.offset
= htab
->sgot
->size
;
1815 htab
->sgot
->size
+= 8;
1816 htab
->srelgot
->size
+= sizeof (Elf32_External_Rel
);
1819 htab
->tls_ldm_got
.offset
= -1;
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
);
1825 /* We now have determined the sizes of the various dynamic sections.
1826 Allocate memory for them. */
1828 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1830 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1835 || s
== htab
->sgotplt
)
1837 /* Strip this section if we don't need it; see the
1840 else if (strncmp (bfd_get_section_name (dynobj
, s
), ".rel", 4) == 0)
1842 if (s
->size
!= 0 && s
!= htab
->srelplt
)
1845 /* We use the reloc_count field as a counter if we need
1846 to copy relocs into the output file. */
1851 /* It's not one of our sections, so don't allocate space. */
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. */
1867 s
->flags
|= SEC_EXCLUDE
;
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
1876 s
->contents
= bfd_zalloc (dynobj
, s
->size
);
1877 if (s
->contents
== NULL
)
1881 if (htab
->elf
.dynamic_sections_created
)
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)
1891 if (info
->executable
)
1893 if (!add_dynamic_entry (DT_DEBUG
, 0))
1897 if (htab
->splt
->size
!= 0)
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))
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
)))
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
,
1919 if ((info
->flags
& DF_TEXTREL
) != 0)
1921 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1926 #undef add_dynamic_entry
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. */
1935 elf_i386_fake_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
1936 Elf_Internal_Shdr
*hdr
,
1939 register const char *name
;
1941 name
= bfd_get_section_name (abfd
, sec
);
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
;
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. */
1970 dtpoff_base (struct bfd_link_info
*info
)
1972 /* If tls_sec is NULL, we should have signalled an error already. */
1973 if (elf_hash_table (info
)->tls_sec
== NULL
)
1975 return elf_hash_table (info
)->tls_sec
->vma
;
1978 /* Return the relocation value for @tpoff relocation
1979 if STT_TLS virtual address is ADDRESS. */
1982 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1984 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
1986 /* If tls_sec is NULL, we should have signalled an error already. */
1987 if (htab
->tls_sec
== NULL
)
1989 return htab
->tls_size
+ htab
->tls_sec
->vma
- address
;
1992 /* Relocate an i386 ELF section. */
1995 elf_i386_relocate_section (bfd
*output_bfd
,
1996 struct bfd_link_info
*info
,
1998 asection
*input_section
,
2000 Elf_Internal_Rela
*relocs
,
2001 Elf_Internal_Sym
*local_syms
,
2002 asection
**local_sections
)
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
;
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
);
2017 relend
= relocs
+ input_section
->reloc_count
;
2018 for (; rel
< relend
; rel
++)
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
;
2028 bfd_boolean unresolved_reloc
;
2029 bfd_reloc_status_type r
;
2033 r_type
= ELF32_R_TYPE (rel
->r_info
);
2034 if (r_type
== R_386_GNU_VTINHERIT
2035 || r_type
== R_386_GNU_VTENTRY
)
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
))
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
);
2050 howto
= elf_howto_table
+ indx
;
2052 r_symndx
= ELF32_R_SYM (rel
->r_info
);
2054 if (info
->relocatable
)
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
)
2066 sym
= local_syms
+ r_symndx
;
2067 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2070 sec
= local_sections
[r_symndx
];
2071 val
= sec
->output_offset
;
2075 where
= contents
+ rel
->r_offset
;
2076 switch (howto
->size
)
2078 /* FIXME: overflow checks. */
2080 val
+= bfd_get_8 (input_bfd
, where
);
2081 bfd_put_8 (input_bfd
, val
, where
);
2084 val
+= bfd_get_16 (input_bfd
, where
);
2085 bfd_put_16 (input_bfd
, val
, where
);
2088 val
+= bfd_get_32 (input_bfd
, where
);
2089 bfd_put_32 (input_bfd
, val
, where
);
2097 /* This is a final link. */
2101 unresolved_reloc
= FALSE
;
2102 if (r_symndx
< symtab_hdr
->sh_info
)
2104 sym
= local_syms
+ r_symndx
;
2105 sec
= local_sections
[r_symndx
];
2106 relocation
= (sec
->output_section
->vma
2107 + sec
->output_offset
2109 if ((sec
->flags
& SEC_MERGE
)
2110 && ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
2114 bfd_byte
*where
= contents
+ rel
->r_offset
;
2116 switch (howto
->size
)
2119 addend
= bfd_get_8 (input_bfd
, where
);
2120 if (howto
->pc_relative
)
2122 addend
= (addend
^ 0x80) - 0x80;
2127 addend
= bfd_get_16 (input_bfd
, where
);
2128 if (howto
->pc_relative
)
2130 addend
= (addend
^ 0x8000) - 0x8000;
2135 addend
= bfd_get_32 (input_bfd
, where
);
2136 if (howto
->pc_relative
)
2138 addend
= (addend
^ 0x80000000) - 0x80000000;
2147 addend
= _bfd_elf_rel_local_sym (output_bfd
, sym
, &msec
, addend
);
2148 addend
-= relocation
;
2149 addend
+= msec
->output_section
->vma
+ msec
->output_offset
;
2151 switch (howto
->size
)
2154 /* FIXME: overflow checks. */
2155 if (howto
->pc_relative
)
2157 bfd_put_8 (input_bfd
, addend
, where
);
2160 if (howto
->pc_relative
)
2162 bfd_put_16 (input_bfd
, addend
, where
);
2165 if (howto
->pc_relative
)
2167 bfd_put_32 (input_bfd
, addend
, where
);
2176 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2177 r_symndx
, symtab_hdr
, sym_hashes
,
2179 unresolved_reloc
, warned
);
2185 /* Relocation is to the entry for this symbol in the global
2187 if (htab
->sgot
== NULL
)
2194 off
= h
->got
.offset
;
2195 dyn
= htab
->elf
.dynamic_sections_created
;
2196 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
->shared
, h
)
2198 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2199 || (ELF_ST_VISIBILITY (h
->other
)
2200 && h
->root
.type
== bfd_link_hash_undefweak
))
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.
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. */
2218 bfd_put_32 (output_bfd
, relocation
,
2219 htab
->sgot
->contents
+ off
);
2224 unresolved_reloc
= FALSE
;
2228 if (local_got_offsets
== NULL
)
2231 off
= local_got_offsets
[r_symndx
];
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. */
2240 bfd_put_32 (output_bfd
, relocation
,
2241 htab
->sgot
->contents
+ off
);
2246 Elf_Internal_Rela outrel
;
2253 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2254 + htab
->sgot
->output_offset
2256 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
2258 loc
+= s
->reloc_count
++ * sizeof (Elf32_External_Rel
);
2259 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
, loc
);
2262 local_got_offsets
[r_symndx
] |= 1;
2266 if (off
>= (bfd_vma
) -2)
2269 relocation
= htab
->sgot
->output_section
->vma
2270 + htab
->sgot
->output_offset
+ off
2271 - htab
->sgotplt
->output_section
->vma
2272 - htab
->sgotplt
->output_offset
;
2276 /* Relocation is relative to the start of the global offset
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. */
2285 && h
->type
== STT_FUNC
2286 && ELF_ST_VISIBILITY (h
->other
) == STV_PROTECTED
)
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
);
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
2300 relocation
-= htab
->sgotplt
->output_section
->vma
2301 + htab
->sgotplt
->output_offset
;
2305 /* Use global offset table as symbol value. */
2306 relocation
= htab
->sgotplt
->output_section
->vma
2307 + htab
->sgotplt
->output_offset
;
2308 unresolved_reloc
= FALSE
;
2312 /* Relocation is to the entry for this symbol in the
2313 procedure linkage table. */
2315 /* Resolve a PLT32 reloc against a local symbol directly,
2316 without using the procedure linkage table. */
2320 if (h
->plt
.offset
== (bfd_vma
) -1
2321 || htab
->splt
== NULL
)
2323 /* We didn't make a PLT entry for this symbol. This
2324 happens when statically linking PIC code, or when
2325 using -Bsymbolic. */
2329 relocation
= (htab
->splt
->output_section
->vma
2330 + htab
->splt
->output_offset
2332 unresolved_reloc
= FALSE
;
2337 /* r_symndx will be zero only for relocs against symbols
2338 from removed linkonce sections, or sections discarded by
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
);
2352 if ((input_section
->flags
& SEC_ALLOC
) == 0)
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
2368 || h
->root
.type
== bfd_link_hash_undefweak
2369 || h
->root
.type
== bfd_link_hash_undefined
)))
2371 Elf_Internal_Rela outrel
;
2373 bfd_boolean skip
, relocate
;
2376 /* When generating a shared object, these relocations
2377 are copied into the output file to be resolved at run
2384 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2386 if (outrel
.r_offset
== (bfd_vma
) -1)
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
);
2394 memset (&outrel
, 0, sizeof outrel
);
2397 && (r_type
== R_386_PC32
2400 || !h
->def_regular
))
2401 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, r_type
);
2404 /* This symbol is local, or marked to become local. */
2406 outrel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
2409 sreloc
= elf_section_data (input_section
)->sreloc
;
2413 loc
= sreloc
->contents
;
2414 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rel
);
2415 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
, loc
);
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. */
2429 Elf_Internal_Rela outrel
;
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
;
2440 loc
= sreloc
->contents
;
2441 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rel
);
2442 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
, loc
);
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
];
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
;
2459 if (tls_type
== GOT_TLS_IE
)
2460 tls_type
= GOT_TLS_IE_NEG
;
2461 if (r_type
== R_386_TLS_GD
)
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
;
2469 if (r_type
== R_386_TLS_LE_32
)
2471 BFD_ASSERT (! unresolved_reloc
);
2472 if (ELF32_R_TYPE (rel
->r_info
) == R_386_TLS_GD
)
2474 unsigned int val
, type
;
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)
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);
2492 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
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)
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);
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)
2511 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
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;
2521 /* leal foo(%reg), %eax; call ___tls_get_addr
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);
2529 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2531 /* Skip R_386_PLT32. */
2535 else if (ELF32_R_TYPE (rel
->r_info
) == R_386_TLS_IE
)
2537 unsigned int val
, type
;
2539 /* IE->LE transition:
2540 Originally it can be one of:
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
);
2553 /* movl foo, %eax. */
2554 bfd_put_8 (output_bfd
, 0xb8,
2555 contents
+ rel
->r_offset
- 1);
2559 BFD_ASSERT (rel
->r_offset
>= 2);
2560 type
= bfd_get_8 (input_bfd
,
2561 contents
+ rel
->r_offset
- 2);
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);
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);
2587 bfd_put_32 (output_bfd
, -tpoff (info
, relocation
),
2588 contents
+ rel
->r_offset
);
2593 unsigned int val
, type
;
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
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);
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);
2617 else if (type
== 0x2b)
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);
2625 else if (type
== 0x03)
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);
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
);
2639 bfd_put_32 (output_bfd
, tpoff (info
, relocation
),
2640 contents
+ rel
->r_offset
);
2645 if (htab
->sgot
== NULL
)
2649 off
= h
->got
.offset
;
2652 if (local_got_offsets
== NULL
)
2655 off
= local_got_offsets
[r_symndx
];
2662 Elf_Internal_Rela outrel
;
2666 if (htab
->srelgot
== NULL
)
2669 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2670 + htab
->sgot
->output_offset
+ off
);
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
;
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
);
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
);
2693 if (r_type
== R_386_TLS_GD
)
2697 BFD_ASSERT (! unresolved_reloc
);
2698 bfd_put_32 (output_bfd
,
2699 relocation
- dtpoff_base (info
),
2700 htab
->sgot
->contents
+ off
+ 4);
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
);
2714 else if (tls_type
== GOT_TLS_IE_BOTH
)
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
);
2729 local_got_offsets
[r_symndx
] |= 1;
2732 if (off
>= (bfd_vma
) -2)
2734 if (r_type
== ELF32_R_TYPE (rel
->r_info
))
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
)
2743 if (r_type
== R_386_TLS_IE
)
2744 relocation
+= g_o_t
;
2745 unresolved_reloc
= FALSE
;
2749 unsigned int val
, type
;
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)
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);
2765 /* leal foo(,%reg,1), %eax; call ___tls_get_addr
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)
2772 BFD_ASSERT ((val
& 0xc7) == 0x05 && val
!= (4 << 3));
2777 /* leal foo(%reg), %eax; call ___tls_get_addr; nop
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)
2785 roff
= rel
->r_offset
- 2;
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
2794 addl $foo@gotntpoff(%reg), %eax. */
2795 if (r_type
== R_386_TLS_GOTIE
)
2797 contents
[roff
+ 6] = 0x03;
2798 if (tls_type
== GOT_TLS_IE_BOTH
)
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. */
2818 /* LD->LE transition:
2820 leal foo(%reg), %eax; call ___tls_get_addr.
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)
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)
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. */
2840 if (htab
->sgot
== NULL
)
2843 off
= htab
->tls_ldm_got
.offset
;
2848 Elf_Internal_Rela outrel
;
2851 if (htab
->srelgot
== NULL
)
2854 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
2855 + htab
->sgot
->output_offset
+ off
);
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;
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
;
2874 case R_386_TLS_LDO_32
:
2875 if (info
->shared
|| (input_section
->flags
& SEC_CODE
) == 0)
2876 relocation
-= dtpoff_base (info
);
2878 /* When converting LDO to LE, we must negate. */
2879 relocation
= -tpoff (info
, relocation
);
2882 case R_386_TLS_LE_32
:
2886 Elf_Internal_Rela outrel
;
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)
2898 if (r_type
== R_386_TLS_LE_32
)
2899 outrel
.r_info
= ELF32_R_INFO (indx
, R_386_TLS_TPOFF32
);
2901 outrel
.r_info
= ELF32_R_INFO (indx
, R_386_TLS_TPOFF
);
2902 sreloc
= elf_section_data (input_section
)->sreloc
;
2905 loc
= sreloc
->contents
;
2906 loc
+= sreloc
->reloc_count
++ * sizeof (Elf32_External_Rel
);
2907 bfd_elf32_swap_reloc_out (output_bfd
, &outrel
, loc
);
2910 else if (r_type
== R_386_TLS_LE_32
)
2911 relocation
= dtpoff_base (info
) - relocation
;
2913 relocation
-= dtpoff_base (info
);
2915 else if (r_type
== R_386_TLS_LE_32
)
2916 relocation
= tpoff (info
, relocation
);
2918 relocation
= -tpoff (info
, relocation
);
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
2932 (*_bfd_error_handler
)
2933 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2936 (long) rel
->r_offset
,
2937 h
->root
.root
.string
);
2941 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2942 contents
, rel
->r_offset
,
2945 if (r
!= bfd_reloc_ok
)
2950 name
= h
->root
.root
.string
;
2953 name
= bfd_elf_string_from_elf_section (input_bfd
,
2954 symtab_hdr
->sh_link
,
2959 name
= bfd_section_name (input_bfd
, sec
);
2962 if (r
== bfd_reloc_overflow
)
2964 if (! ((*info
->callbacks
->reloc_overflow
)
2965 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2966 (bfd_vma
) 0, input_bfd
, input_section
,
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
);
2984 /* Finish up dynamic symbol handling. We set the contents of various
2985 dynamic sections here. */
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
)
2993 struct elf_i386_link_hash_table
*htab
;
2995 htab
= elf_i386_hash_table (info
);
2997 if (h
->plt
.offset
!= (bfd_vma
) -1)
3001 Elf_Internal_Rela rel
;
3004 /* This symbol has an entry in the procedure linkage table. Set
3007 if (h
->dynindx
== -1
3008 || htab
->splt
== NULL
3009 || htab
->sgotplt
== NULL
3010 || htab
->srelplt
== NULL
)
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;
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;
3024 /* Fill in the entry in the procedure linkage table. */
3027 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf_i386_plt_entry
,
3029 bfd_put_32 (output_bfd
,
3030 (htab
->sgotplt
->output_section
->vma
3031 + htab
->sgotplt
->output_offset
3033 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
3037 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf_i386_pic_plt_entry
,
3039 bfd_put_32 (output_bfd
, got_offset
,
3040 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
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);
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
3054 htab
->sgotplt
->contents
+ got_offset
);
3056 /* Fill in the entry in the .rel.plt section. */
3057 rel
.r_offset
= (htab
->sgotplt
->output_section
->vma
3058 + htab
->sgotplt
->output_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
);
3064 if (!h
->def_regular
)
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
)
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)
3084 Elf_Internal_Rela rel
;
3087 /* This symbol has an entry in the global offset table. Set it
3090 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
3093 rel
.r_offset
= (htab
->sgot
->output_section
->vma
3094 + htab
->sgot
->output_offset
3095 + (h
->got
.offset
& ~(bfd_vma
) 1));
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. */
3103 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3105 BFD_ASSERT((h
->got
.offset
& 1) != 0);
3106 rel
.r_info
= ELF32_R_INFO (0, R_386_RELATIVE
);
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
);
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
);
3123 Elf_Internal_Rela rel
;
3126 /* This symbol needs a copy reloc. Set it up. */
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
)
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
);
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
;
3151 /* Used to decide how to sort relocs in an optimal manner for the
3152 dynamic linker, before writing them out. */
3154 static enum elf_reloc_type_class
3155 elf_i386_reloc_type_class (const Elf_Internal_Rela
*rela
)
3157 switch (ELF32_R_TYPE (rela
->r_info
))
3159 case R_386_RELATIVE
:
3160 return reloc_class_relative
;
3161 case R_386_JUMP_SLOT
:
3162 return reloc_class_plt
;
3164 return reloc_class_copy
;
3166 return reloc_class_normal
;
3170 /* Finish up the dynamic sections. */
3173 elf_i386_finish_dynamic_sections (bfd
*output_bfd
,
3174 struct bfd_link_info
*info
)
3176 struct elf_i386_link_hash_table
*htab
;
3180 htab
= elf_i386_hash_table (info
);
3181 dynobj
= htab
->elf
.dynobj
;
3182 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
3184 if (htab
->elf
.dynamic_sections_created
)
3186 Elf32_External_Dyn
*dyncon
, *dynconend
;
3188 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
3191 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
3192 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->size
);
3193 for (; dyncon
< dynconend
; dyncon
++)
3195 Elf_Internal_Dyn dyn
;
3198 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
3207 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3212 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3217 dyn
.d_un
.d_val
= s
->size
;
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. */
3230 dyn
.d_un
.d_val
-= s
->size
;
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. */
3240 if (dyn
.d_un
.d_ptr
!= s
->output_section
->vma
+ s
->output_offset
)
3242 dyn
.d_un
.d_ptr
+= s
->size
;
3246 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3249 /* Fill in the first entry in the procedure linkage table. */
3250 if (htab
->splt
&& htab
->splt
->size
> 0)
3253 memcpy (htab
->splt
->contents
,
3254 elf_i386_pic_plt0_entry
, PLT_ENTRY_SIZE
);
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
3263 htab
->splt
->contents
+ 2);
3264 bfd_put_32 (output_bfd
,
3265 (htab
->sgotplt
->output_section
->vma
3266 + htab
->sgotplt
->output_offset
3268 htab
->splt
->contents
+ 8);
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;
3280 /* Fill in the first three entries in the global offset table. */
3281 if (htab
->sgotplt
->size
> 0)
3283 bfd_put_32 (output_bfd
,
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);
3291 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
= 4;
3294 if (htab
->sgot
&& htab
->sgot
->size
> 0)
3295 elf_section_data (htab
->sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
3300 /* Return address for Ith PLT stub in section PLT, for relocation REL
3301 or (bfd_vma) -1 if it should not be included. */
3304 elf_i386_plt_sym_val (bfd_vma i
, const asection
*plt
,
3305 const arelent
*rel ATTRIBUTE_UNUSED
)
3307 return plt
->vma
+ (i
+ 1) * PLT_ENTRY_SIZE
;
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
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
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
3328 #define bfd_elf32_mkobject elf_i386_mkobject
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
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
3350 #include "elf32-target.h"
3352 /* FreeBSD support. */
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"
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. */
3364 elf_i386_post_process_headers (bfd
*abfd
,
3365 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
3367 Elf_Internal_Ehdr
*i_ehdrp
;
3369 i_ehdrp
= elf_elfheader (abfd
);
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);
3379 #undef elf_backend_post_process_headers
3380 #define elf_backend_post_process_headers elf_i386_post_process_headers
3382 #define elf32_bed elf32_i386_fbsd_bed
3384 #include "elf32-target.h"