Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Intel 80386/80486-specific support for 32-bit ELF |
638632bd AM |
2 | Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 |
3 | Free Software Foundation, Inc. | |
252b5132 RH |
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 | static reloc_howto_type *elf_i386_reloc_type_lookup | |
28 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
29 | static void elf_i386_info_to_howto | |
30 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); | |
31 | static void elf_i386_info_to_howto_rel | |
32 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *)); | |
33 | static boolean elf_i386_is_local_label_name PARAMS ((bfd *, const char *)); | |
34 | static struct bfd_hash_entry *elf_i386_link_hash_newfunc | |
35 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
36 | static struct bfd_link_hash_table *elf_i386_link_hash_table_create | |
37 | PARAMS ((bfd *)); | |
38 | static boolean elf_i386_check_relocs | |
39 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
40 | const Elf_Internal_Rela *)); | |
41 | static boolean elf_i386_adjust_dynamic_symbol | |
42 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
43 | static boolean elf_i386_size_dynamic_sections | |
44 | PARAMS ((bfd *, struct bfd_link_info *)); | |
45 | static boolean elf_i386_relocate_section | |
46 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
47 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
48 | static boolean elf_i386_finish_dynamic_symbol | |
49 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
50 | Elf_Internal_Sym *)); | |
51 | static boolean elf_i386_finish_dynamic_sections | |
52 | PARAMS ((bfd *, struct bfd_link_info *)); | |
53 | ||
54 | #define USE_REL 1 /* 386 uses REL relocations instead of RELA */ | |
55 | ||
56 | #include "elf/i386.h" | |
57 | ||
58 | static reloc_howto_type elf_howto_table[]= | |
59 | { | |
1b452ec6 AM |
60 | HOWTO(R_386_NONE, 0, 0, 0, false, 0, complain_overflow_bitfield, |
61 | bfd_elf_generic_reloc, "R_386_NONE", | |
62 | true, 0x00000000, 0x00000000, false), | |
63 | HOWTO(R_386_32, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
64 | bfd_elf_generic_reloc, "R_386_32", | |
65 | true, 0xffffffff, 0xffffffff, false), | |
66 | HOWTO(R_386_PC32, 0, 2, 32, true, 0, complain_overflow_bitfield, | |
67 | bfd_elf_generic_reloc, "R_386_PC32", | |
68 | true, 0xffffffff, 0xffffffff, true), | |
69 | HOWTO(R_386_GOT32, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
70 | bfd_elf_generic_reloc, "R_386_GOT32", | |
71 | true, 0xffffffff, 0xffffffff, false), | |
72 | HOWTO(R_386_PLT32, 0, 2, 32, true, 0, complain_overflow_bitfield, | |
73 | bfd_elf_generic_reloc, "R_386_PLT32", | |
74 | true, 0xffffffff, 0xffffffff, true), | |
75 | HOWTO(R_386_COPY, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
76 | bfd_elf_generic_reloc, "R_386_COPY", | |
77 | true, 0xffffffff, 0xffffffff, false), | |
78 | HOWTO(R_386_GLOB_DAT, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
79 | bfd_elf_generic_reloc, "R_386_GLOB_DAT", | |
80 | true, 0xffffffff, 0xffffffff, false), | |
81 | HOWTO(R_386_JUMP_SLOT, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
82 | bfd_elf_generic_reloc, "R_386_JUMP_SLOT", | |
83 | true, 0xffffffff, 0xffffffff, false), | |
84 | HOWTO(R_386_RELATIVE, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
85 | bfd_elf_generic_reloc, "R_386_RELATIVE", | |
86 | true, 0xffffffff, 0xffffffff, false), | |
87 | HOWTO(R_386_GOTOFF, 0, 2, 32, false, 0, complain_overflow_bitfield, | |
88 | bfd_elf_generic_reloc, "R_386_GOTOFF", | |
89 | true, 0xffffffff, 0xffffffff, false), | |
90 | HOWTO(R_386_GOTPC, 0, 2, 32, true, 0, complain_overflow_bitfield, | |
91 | bfd_elf_generic_reloc, "R_386_GOTPC", | |
92 | true, 0xffffffff, 0xffffffff, true), | |
93 | ||
94 | #define R_386_standard ((unsigned int) R_386_GOTPC + 1) | |
95 | #define R_386_ext_offset ((unsigned int) R_386_16 - R_386_standard) | |
96 | ||
252b5132 | 97 | /* The remaining relocs are a GNU extension. */ |
1b452ec6 AM |
98 | HOWTO(R_386_16, 0, 1, 16, false, 0, complain_overflow_bitfield, |
99 | bfd_elf_generic_reloc, "R_386_16", | |
100 | true, 0xffff, 0xffff, false), | |
101 | HOWTO(R_386_PC16, 0, 1, 16, true, 0, complain_overflow_bitfield, | |
102 | bfd_elf_generic_reloc, "R_386_PC16", | |
103 | true, 0xffff, 0xffff, true), | |
104 | HOWTO(R_386_8, 0, 0, 8, false, 0, complain_overflow_bitfield, | |
105 | bfd_elf_generic_reloc, "R_386_8", | |
106 | true, 0xff, 0xff, false), | |
107 | HOWTO(R_386_PC8, 0, 0, 8, true, 0, complain_overflow_signed, | |
108 | bfd_elf_generic_reloc, "R_386_PC8", | |
109 | true, 0xff, 0xff, true) | |
252b5132 RH |
110 | }; |
111 | ||
112 | /* GNU extension to record C++ vtable hierarchy. */ | |
113 | static reloc_howto_type elf32_i386_vtinherit_howto = | |
114 | HOWTO (R_386_GNU_VTINHERIT, /* type */ | |
115 | 0, /* rightshift */ | |
116 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
117 | 0, /* bitsize */ | |
118 | false, /* pc_relative */ | |
119 | 0, /* bitpos */ | |
120 | complain_overflow_dont, /* complain_on_overflow */ | |
121 | NULL, /* special_function */ | |
122 | "R_386_GNU_VTINHERIT", /* name */ | |
123 | false, /* partial_inplace */ | |
124 | 0, /* src_mask */ | |
125 | 0, /* dst_mask */ | |
126 | false); | |
127 | ||
128 | /* GNU extension to record C++ vtable member usage. */ | |
129 | static reloc_howto_type elf32_i386_vtentry_howto = | |
130 | HOWTO (R_386_GNU_VTENTRY, /* type */ | |
131 | 0, /* rightshift */ | |
132 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
133 | 0, /* bitsize */ | |
134 | false, /* pc_relative */ | |
135 | 0, /* bitpos */ | |
136 | complain_overflow_dont, /* complain_on_overflow */ | |
137 | _bfd_elf_rel_vtable_reloc_fn, /* special_function */ | |
138 | "R_386_GNU_VTENTRY", /* name */ | |
139 | false, /* partial_inplace */ | |
140 | 0, /* src_mask */ | |
141 | 0, /* dst_mask */ | |
142 | false); | |
143 | ||
144 | #ifdef DEBUG_GEN_RELOC | |
145 | #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str) | |
146 | #else | |
147 | #define TRACE(str) | |
148 | #endif | |
149 | ||
150 | static reloc_howto_type * | |
151 | elf_i386_reloc_type_lookup (abfd, code) | |
7442e600 | 152 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
153 | bfd_reloc_code_real_type code; |
154 | { | |
155 | switch (code) | |
156 | { | |
157 | case BFD_RELOC_NONE: | |
158 | TRACE ("BFD_RELOC_NONE"); | |
1b452ec6 | 159 | return &elf_howto_table[(unsigned int) R_386_NONE ]; |
252b5132 RH |
160 | |
161 | case BFD_RELOC_32: | |
162 | TRACE ("BFD_RELOC_32"); | |
1b452ec6 | 163 | return &elf_howto_table[(unsigned int) R_386_32 ]; |
252b5132 RH |
164 | |
165 | case BFD_RELOC_CTOR: | |
166 | TRACE ("BFD_RELOC_CTOR"); | |
1b452ec6 | 167 | return &elf_howto_table[(unsigned int) R_386_32 ]; |
252b5132 RH |
168 | |
169 | case BFD_RELOC_32_PCREL: | |
170 | TRACE ("BFD_RELOC_PC32"); | |
1b452ec6 | 171 | return &elf_howto_table[(unsigned int) R_386_PC32 ]; |
252b5132 RH |
172 | |
173 | case BFD_RELOC_386_GOT32: | |
174 | TRACE ("BFD_RELOC_386_GOT32"); | |
1b452ec6 | 175 | return &elf_howto_table[(unsigned int) R_386_GOT32 ]; |
252b5132 RH |
176 | |
177 | case BFD_RELOC_386_PLT32: | |
178 | TRACE ("BFD_RELOC_386_PLT32"); | |
1b452ec6 | 179 | return &elf_howto_table[(unsigned int) R_386_PLT32 ]; |
252b5132 RH |
180 | |
181 | case BFD_RELOC_386_COPY: | |
182 | TRACE ("BFD_RELOC_386_COPY"); | |
1b452ec6 | 183 | return &elf_howto_table[(unsigned int) R_386_COPY ]; |
252b5132 RH |
184 | |
185 | case BFD_RELOC_386_GLOB_DAT: | |
186 | TRACE ("BFD_RELOC_386_GLOB_DAT"); | |
1b452ec6 | 187 | return &elf_howto_table[(unsigned int) R_386_GLOB_DAT ]; |
252b5132 RH |
188 | |
189 | case BFD_RELOC_386_JUMP_SLOT: | |
190 | TRACE ("BFD_RELOC_386_JUMP_SLOT"); | |
1b452ec6 | 191 | return &elf_howto_table[(unsigned int) R_386_JUMP_SLOT ]; |
252b5132 RH |
192 | |
193 | case BFD_RELOC_386_RELATIVE: | |
194 | TRACE ("BFD_RELOC_386_RELATIVE"); | |
1b452ec6 | 195 | return &elf_howto_table[(unsigned int) R_386_RELATIVE ]; |
252b5132 RH |
196 | |
197 | case BFD_RELOC_386_GOTOFF: | |
198 | TRACE ("BFD_RELOC_386_GOTOFF"); | |
1b452ec6 | 199 | return &elf_howto_table[(unsigned int) R_386_GOTOFF ]; |
252b5132 RH |
200 | |
201 | case BFD_RELOC_386_GOTPC: | |
202 | TRACE ("BFD_RELOC_386_GOTPC"); | |
1b452ec6 | 203 | return &elf_howto_table[(unsigned int) R_386_GOTPC ]; |
252b5132 RH |
204 | |
205 | /* The remaining relocs are a GNU extension. */ | |
206 | case BFD_RELOC_16: | |
207 | TRACE ("BFD_RELOC_16"); | |
1b452ec6 | 208 | return &elf_howto_table[(unsigned int) R_386_16 - R_386_ext_offset]; |
252b5132 RH |
209 | |
210 | case BFD_RELOC_16_PCREL: | |
211 | TRACE ("BFD_RELOC_16_PCREL"); | |
1b452ec6 | 212 | return &elf_howto_table[(unsigned int) R_386_PC16 - R_386_ext_offset]; |
252b5132 RH |
213 | |
214 | case BFD_RELOC_8: | |
215 | TRACE ("BFD_RELOC_8"); | |
1b452ec6 | 216 | return &elf_howto_table[(unsigned int) R_386_8 - R_386_ext_offset]; |
252b5132 RH |
217 | |
218 | case BFD_RELOC_8_PCREL: | |
219 | TRACE ("BFD_RELOC_8_PCREL"); | |
1b452ec6 | 220 | return &elf_howto_table[(unsigned int) R_386_PC8 - R_386_ext_offset]; |
252b5132 RH |
221 | |
222 | case BFD_RELOC_VTABLE_INHERIT: | |
223 | TRACE ("BFD_RELOC_VTABLE_INHERIT"); | |
224 | return &elf32_i386_vtinherit_howto; | |
225 | ||
226 | case BFD_RELOC_VTABLE_ENTRY: | |
227 | TRACE ("BFD_RELOC_VTABLE_ENTRY"); | |
228 | return &elf32_i386_vtentry_howto; | |
229 | ||
230 | default: | |
231 | break; | |
232 | } | |
233 | ||
234 | TRACE ("Unknown"); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | static void | |
239 | elf_i386_info_to_howto (abfd, cache_ptr, dst) | |
7442e600 ILT |
240 | bfd *abfd ATTRIBUTE_UNUSED; |
241 | arelent *cache_ptr ATTRIBUTE_UNUSED; | |
242 | Elf32_Internal_Rela *dst ATTRIBUTE_UNUSED; | |
252b5132 RH |
243 | { |
244 | abort (); | |
245 | } | |
246 | ||
247 | static void | |
248 | elf_i386_info_to_howto_rel (abfd, cache_ptr, dst) | |
7442e600 | 249 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
250 | arelent *cache_ptr; |
251 | Elf32_Internal_Rel *dst; | |
252 | { | |
253 | enum elf_i386_reloc_type type; | |
254 | ||
255 | type = (enum elf_i386_reloc_type) ELF32_R_TYPE (dst->r_info); | |
256 | if (type == R_386_GNU_VTINHERIT) | |
257 | cache_ptr->howto = &elf32_i386_vtinherit_howto; | |
258 | else if (type == R_386_GNU_VTENTRY) | |
259 | cache_ptr->howto = &elf32_i386_vtentry_howto; | |
260 | else | |
261 | { | |
1b452ec6 AM |
262 | unsigned int indx; |
263 | ||
264 | if ((indx = (unsigned int) type) >= R_386_standard | |
265 | && ((indx = (unsigned int) type - R_386_ext_offset) | |
266 | >= sizeof (elf_howto_table) / sizeof (elf_howto_table[0]))) | |
267 | { | |
268 | (*_bfd_error_handler) (_("%s: invalid relocation type %d"), | |
269 | bfd_get_filename (abfd), (int) type); | |
270 | indx = (unsigned int) R_386_NONE; | |
271 | } | |
272 | cache_ptr->howto = &elf_howto_table[indx]; | |
252b5132 RH |
273 | } |
274 | } | |
275 | ||
276 | /* Return whether a symbol name implies a local label. The UnixWare | |
277 | 2.1 cc generates temporary symbols that start with .X, so we | |
278 | recognize them here. FIXME: do other SVR4 compilers also use .X?. | |
279 | If so, we should move the .X recognition into | |
280 | _bfd_elf_is_local_label_name. */ | |
281 | ||
282 | static boolean | |
283 | elf_i386_is_local_label_name (abfd, name) | |
284 | bfd *abfd; | |
285 | const char *name; | |
286 | { | |
287 | if (name[0] == '.' && name[1] == 'X') | |
288 | return true; | |
289 | ||
290 | return _bfd_elf_is_local_label_name (abfd, name); | |
291 | } | |
292 | \f | |
293 | /* Functions for the i386 ELF linker. */ | |
294 | ||
295 | /* The name of the dynamic interpreter. This is put in the .interp | |
296 | section. */ | |
297 | ||
298 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" | |
299 | ||
300 | /* The size in bytes of an entry in the procedure linkage table. */ | |
301 | ||
302 | #define PLT_ENTRY_SIZE 16 | |
303 | ||
304 | /* The first entry in an absolute procedure linkage table looks like | |
305 | this. See the SVR4 ABI i386 supplement to see how this works. */ | |
306 | ||
307 | static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] = | |
308 | { | |
309 | 0xff, 0x35, /* pushl contents of address */ | |
310 | 0, 0, 0, 0, /* replaced with address of .got + 4. */ | |
311 | 0xff, 0x25, /* jmp indirect */ | |
312 | 0, 0, 0, 0, /* replaced with address of .got + 8. */ | |
313 | 0, 0, 0, 0 /* pad out to 16 bytes. */ | |
314 | }; | |
315 | ||
316 | /* Subsequent entries in an absolute procedure linkage table look like | |
317 | this. */ | |
318 | ||
319 | static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] = | |
320 | { | |
321 | 0xff, 0x25, /* jmp indirect */ | |
322 | 0, 0, 0, 0, /* replaced with address of this symbol in .got. */ | |
323 | 0x68, /* pushl immediate */ | |
324 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
325 | 0xe9, /* jmp relative */ | |
326 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
327 | }; | |
328 | ||
329 | /* The first entry in a PIC procedure linkage table look like this. */ | |
330 | ||
331 | static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] = | |
332 | { | |
333 | 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */ | |
334 | 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */ | |
335 | 0, 0, 0, 0 /* pad out to 16 bytes. */ | |
336 | }; | |
337 | ||
338 | /* Subsequent entries in a PIC procedure linkage table look like this. */ | |
339 | ||
340 | static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] = | |
341 | { | |
342 | 0xff, 0xa3, /* jmp *offset(%ebx) */ | |
343 | 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ | |
344 | 0x68, /* pushl immediate */ | |
345 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
346 | 0xe9, /* jmp relative */ | |
347 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
348 | }; | |
349 | ||
350 | /* The i386 linker needs to keep track of the number of relocs that it | |
351 | decides to copy in check_relocs for each symbol. This is so that | |
352 | it can discard PC relative relocs if it doesn't need them when | |
353 | linking with -Bsymbolic. We store the information in a field | |
354 | extending the regular ELF linker hash table. */ | |
355 | ||
356 | /* This structure keeps track of the number of PC relative relocs we | |
357 | have copied for a given symbol. */ | |
358 | ||
359 | struct elf_i386_pcrel_relocs_copied | |
360 | { | |
361 | /* Next section. */ | |
362 | struct elf_i386_pcrel_relocs_copied *next; | |
363 | /* A section in dynobj. */ | |
364 | asection *section; | |
365 | /* Number of relocs copied in this section. */ | |
366 | bfd_size_type count; | |
367 | }; | |
368 | ||
369 | /* i386 ELF linker hash entry. */ | |
370 | ||
371 | struct elf_i386_link_hash_entry | |
372 | { | |
373 | struct elf_link_hash_entry root; | |
374 | ||
375 | /* Number of PC relative relocs copied for this symbol. */ | |
376 | struct elf_i386_pcrel_relocs_copied *pcrel_relocs_copied; | |
377 | }; | |
378 | ||
379 | /* i386 ELF linker hash table. */ | |
380 | ||
381 | struct elf_i386_link_hash_table | |
382 | { | |
383 | struct elf_link_hash_table root; | |
384 | }; | |
385 | ||
386 | /* Declare this now that the above structures are defined. */ | |
387 | ||
388 | static boolean elf_i386_discard_copies | |
389 | PARAMS ((struct elf_i386_link_hash_entry *, PTR)); | |
390 | ||
391 | /* Traverse an i386 ELF linker hash table. */ | |
392 | ||
393 | #define elf_i386_link_hash_traverse(table, func, info) \ | |
394 | (elf_link_hash_traverse \ | |
395 | (&(table)->root, \ | |
396 | (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ | |
397 | (info))) | |
398 | ||
399 | /* Get the i386 ELF linker hash table from a link_info structure. */ | |
400 | ||
401 | #define elf_i386_hash_table(p) \ | |
402 | ((struct elf_i386_link_hash_table *) ((p)->hash)) | |
403 | ||
404 | /* Create an entry in an i386 ELF linker hash table. */ | |
405 | ||
406 | static struct bfd_hash_entry * | |
407 | elf_i386_link_hash_newfunc (entry, table, string) | |
408 | struct bfd_hash_entry *entry; | |
409 | struct bfd_hash_table *table; | |
410 | const char *string; | |
411 | { | |
412 | struct elf_i386_link_hash_entry *ret = | |
413 | (struct elf_i386_link_hash_entry *) entry; | |
414 | ||
415 | /* Allocate the structure if it has not already been allocated by a | |
416 | subclass. */ | |
417 | if (ret == (struct elf_i386_link_hash_entry *) NULL) | |
418 | ret = ((struct elf_i386_link_hash_entry *) | |
419 | bfd_hash_allocate (table, | |
420 | sizeof (struct elf_i386_link_hash_entry))); | |
421 | if (ret == (struct elf_i386_link_hash_entry *) NULL) | |
422 | return (struct bfd_hash_entry *) ret; | |
423 | ||
424 | /* Call the allocation method of the superclass. */ | |
425 | ret = ((struct elf_i386_link_hash_entry *) | |
426 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, | |
427 | table, string)); | |
428 | if (ret != (struct elf_i386_link_hash_entry *) NULL) | |
429 | { | |
430 | ret->pcrel_relocs_copied = NULL; | |
431 | } | |
432 | ||
433 | return (struct bfd_hash_entry *) ret; | |
434 | } | |
435 | ||
436 | /* Create an i386 ELF linker hash table. */ | |
437 | ||
438 | static struct bfd_link_hash_table * | |
439 | elf_i386_link_hash_table_create (abfd) | |
440 | bfd *abfd; | |
441 | { | |
442 | struct elf_i386_link_hash_table *ret; | |
443 | ||
444 | ret = ((struct elf_i386_link_hash_table *) | |
445 | bfd_alloc (abfd, sizeof (struct elf_i386_link_hash_table))); | |
446 | if (ret == (struct elf_i386_link_hash_table *) NULL) | |
447 | return NULL; | |
448 | ||
449 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, | |
450 | elf_i386_link_hash_newfunc)) | |
451 | { | |
452 | bfd_release (abfd, ret); | |
453 | return NULL; | |
454 | } | |
455 | ||
456 | return &ret->root.root; | |
457 | } | |
458 | ||
459 | /* Look through the relocs for a section during the first phase, and | |
460 | allocate space in the global offset table or procedure linkage | |
461 | table. */ | |
462 | ||
463 | static boolean | |
464 | elf_i386_check_relocs (abfd, info, sec, relocs) | |
465 | bfd *abfd; | |
466 | struct bfd_link_info *info; | |
467 | asection *sec; | |
468 | const Elf_Internal_Rela *relocs; | |
469 | { | |
470 | bfd *dynobj; | |
471 | Elf_Internal_Shdr *symtab_hdr; | |
472 | struct elf_link_hash_entry **sym_hashes; | |
dd5724d5 | 473 | bfd_signed_vma *local_got_refcounts; |
252b5132 RH |
474 | const Elf_Internal_Rela *rel; |
475 | const Elf_Internal_Rela *rel_end; | |
476 | asection *sgot; | |
477 | asection *srelgot; | |
478 | asection *sreloc; | |
479 | ||
480 | if (info->relocateable) | |
481 | return true; | |
482 | ||
483 | dynobj = elf_hash_table (info)->dynobj; | |
484 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
485 | sym_hashes = elf_sym_hashes (abfd); | |
dd5724d5 | 486 | local_got_refcounts = elf_local_got_refcounts (abfd); |
252b5132 RH |
487 | |
488 | sgot = NULL; | |
489 | srelgot = NULL; | |
490 | sreloc = NULL; | |
491 | ||
492 | rel_end = relocs + sec->reloc_count; | |
493 | for (rel = relocs; rel < rel_end; rel++) | |
494 | { | |
495 | unsigned long r_symndx; | |
496 | struct elf_link_hash_entry *h; | |
497 | ||
498 | r_symndx = ELF32_R_SYM (rel->r_info); | |
499 | ||
500 | if (r_symndx < symtab_hdr->sh_info) | |
501 | h = NULL; | |
502 | else | |
503 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
504 | ||
505 | /* Some relocs require a global offset table. */ | |
506 | if (dynobj == NULL) | |
507 | { | |
508 | switch (ELF32_R_TYPE (rel->r_info)) | |
509 | { | |
510 | case R_386_GOT32: | |
511 | case R_386_GOTOFF: | |
512 | case R_386_GOTPC: | |
513 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
514 | if (! _bfd_elf_create_got_section (dynobj, info)) | |
515 | return false; | |
516 | break; | |
517 | ||
518 | default: | |
519 | break; | |
520 | } | |
521 | } | |
522 | ||
523 | switch (ELF32_R_TYPE (rel->r_info)) | |
524 | { | |
525 | case R_386_GOT32: | |
526 | /* This symbol requires a global offset table entry. */ | |
527 | ||
528 | if (sgot == NULL) | |
529 | { | |
530 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
531 | BFD_ASSERT (sgot != NULL); | |
532 | } | |
533 | ||
534 | if (srelgot == NULL | |
535 | && (h != NULL || info->shared)) | |
536 | { | |
537 | srelgot = bfd_get_section_by_name (dynobj, ".rel.got"); | |
538 | if (srelgot == NULL) | |
539 | { | |
540 | srelgot = bfd_make_section (dynobj, ".rel.got"); | |
541 | if (srelgot == NULL | |
542 | || ! bfd_set_section_flags (dynobj, srelgot, | |
543 | (SEC_ALLOC | |
544 | | SEC_LOAD | |
545 | | SEC_HAS_CONTENTS | |
546 | | SEC_IN_MEMORY | |
547 | | SEC_LINKER_CREATED | |
548 | | SEC_READONLY)) | |
549 | || ! bfd_set_section_alignment (dynobj, srelgot, 2)) | |
550 | return false; | |
551 | } | |
552 | } | |
553 | ||
554 | if (h != NULL) | |
555 | { | |
dd5724d5 | 556 | if (h->got.refcount == -1) |
252b5132 | 557 | { |
dd5724d5 | 558 | h->got.refcount = 1; |
252b5132 | 559 | |
dd5724d5 AM |
560 | /* Make sure this symbol is output as a dynamic symbol. */ |
561 | if (h->dynindx == -1) | |
562 | { | |
563 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
564 | return false; | |
565 | } | |
252b5132 | 566 | |
dd5724d5 AM |
567 | sgot->_raw_size += 4; |
568 | srelgot->_raw_size += sizeof (Elf32_External_Rel); | |
569 | } | |
570 | else | |
571 | h->got.refcount += 1; | |
252b5132 RH |
572 | } |
573 | else | |
574 | { | |
dd5724d5 AM |
575 | /* This is a global offset table entry for a local symbol. */ |
576 | if (local_got_refcounts == NULL) | |
252b5132 RH |
577 | { |
578 | size_t size; | |
252b5132 | 579 | |
dd5724d5 AM |
580 | size = symtab_hdr->sh_info * sizeof (bfd_signed_vma); |
581 | local_got_refcounts = ((bfd_signed_vma *) | |
582 | bfd_alloc (abfd, size)); | |
583 | if (local_got_refcounts == NULL) | |
252b5132 | 584 | return false; |
dd5724d5 AM |
585 | elf_local_got_refcounts (abfd) = local_got_refcounts; |
586 | memset (local_got_refcounts, -1, size); | |
252b5132 | 587 | } |
dd5724d5 | 588 | if (local_got_refcounts[r_symndx] == -1) |
252b5132 | 589 | { |
dd5724d5 | 590 | local_got_refcounts[r_symndx] = 1; |
252b5132 | 591 | |
dd5724d5 AM |
592 | sgot->_raw_size += 4; |
593 | if (info->shared) | |
594 | { | |
595 | /* If we are generating a shared object, we need to | |
596 | output a R_386_RELATIVE reloc so that the dynamic | |
597 | linker can adjust this GOT entry. */ | |
598 | srelgot->_raw_size += sizeof (Elf32_External_Rel); | |
599 | } | |
252b5132 | 600 | } |
dd5724d5 AM |
601 | else |
602 | local_got_refcounts[r_symndx] += 1; | |
252b5132 | 603 | } |
252b5132 RH |
604 | break; |
605 | ||
606 | case R_386_PLT32: | |
607 | /* This symbol requires a procedure linkage table entry. We | |
608 | actually build the entry in adjust_dynamic_symbol, | |
609 | because this might be a case of linking PIC code which is | |
610 | never referenced by a dynamic object, in which case we | |
611 | don't need to generate a procedure linkage table entry | |
612 | after all. */ | |
613 | ||
614 | /* If this is a local symbol, we resolve it directly without | |
615 | creating a procedure linkage table entry. */ | |
616 | if (h == NULL) | |
617 | continue; | |
618 | ||
dd5724d5 AM |
619 | if (h->plt.refcount == -1) |
620 | { | |
621 | h->plt.refcount = 1; | |
622 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT; | |
623 | } | |
624 | else | |
625 | h->plt.refcount += 1; | |
252b5132 RH |
626 | break; |
627 | ||
628 | case R_386_32: | |
629 | case R_386_PC32: | |
7843f00e ILT |
630 | if (h != NULL) |
631 | h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF; | |
632 | ||
252b5132 RH |
633 | /* If we are creating a shared library, and this is a reloc |
634 | against a global symbol, or a non PC relative reloc | |
635 | against a local symbol, then we need to copy the reloc | |
636 | into the shared library. However, if we are linking with | |
637 | -Bsymbolic, we do not need to copy a reloc against a | |
638 | global symbol which is defined in an object we are | |
639 | including in the link (i.e., DEF_REGULAR is set). At | |
640 | this point we have not seen all the input files, so it is | |
641 | possible that DEF_REGULAR is not set now but will be set | |
642 | later (it is never cleared). We account for that | |
643 | possibility below by storing information in the | |
644 | pcrel_relocs_copied field of the hash table entry. */ | |
645 | if (info->shared | |
646 | && (sec->flags & SEC_ALLOC) != 0 | |
647 | && (ELF32_R_TYPE (rel->r_info) != R_386_PC32 | |
648 | || (h != NULL | |
649 | && (! info->symbolic | |
650 | || (h->elf_link_hash_flags | |
651 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
652 | { | |
653 | /* When creating a shared object, we must copy these | |
654 | reloc types into the output file. We create a reloc | |
655 | section in dynobj and make room for this reloc. */ | |
656 | if (sreloc == NULL) | |
657 | { | |
658 | const char *name; | |
659 | ||
660 | name = (bfd_elf_string_from_elf_section | |
661 | (abfd, | |
662 | elf_elfheader (abfd)->e_shstrndx, | |
663 | elf_section_data (sec)->rel_hdr.sh_name)); | |
664 | if (name == NULL) | |
665 | return false; | |
666 | ||
667 | BFD_ASSERT (strncmp (name, ".rel", 4) == 0 | |
668 | && strcmp (bfd_get_section_name (abfd, sec), | |
669 | name + 4) == 0); | |
670 | ||
671 | sreloc = bfd_get_section_by_name (dynobj, name); | |
672 | if (sreloc == NULL) | |
673 | { | |
674 | flagword flags; | |
675 | ||
676 | sreloc = bfd_make_section (dynobj, name); | |
677 | flags = (SEC_HAS_CONTENTS | SEC_READONLY | |
678 | | SEC_IN_MEMORY | SEC_LINKER_CREATED); | |
679 | if ((sec->flags & SEC_ALLOC) != 0) | |
680 | flags |= SEC_ALLOC | SEC_LOAD; | |
681 | if (sreloc == NULL | |
682 | || ! bfd_set_section_flags (dynobj, sreloc, flags) | |
683 | || ! bfd_set_section_alignment (dynobj, sreloc, 2)) | |
684 | return false; | |
685 | } | |
686 | } | |
687 | ||
688 | sreloc->_raw_size += sizeof (Elf32_External_Rel); | |
689 | ||
690 | /* If we are linking with -Bsymbolic, and this is a | |
691 | global symbol, we count the number of PC relative | |
692 | relocations we have entered for this symbol, so that | |
693 | we can discard them again if the symbol is later | |
694 | defined by a regular object. Note that this function | |
695 | is only called if we are using an elf_i386 linker | |
696 | hash table, which means that h is really a pointer to | |
697 | an elf_i386_link_hash_entry. */ | |
698 | if (h != NULL && info->symbolic | |
699 | && ELF32_R_TYPE (rel->r_info) == R_386_PC32) | |
700 | { | |
701 | struct elf_i386_link_hash_entry *eh; | |
702 | struct elf_i386_pcrel_relocs_copied *p; | |
703 | ||
704 | eh = (struct elf_i386_link_hash_entry *) h; | |
705 | ||
706 | for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next) | |
707 | if (p->section == sreloc) | |
708 | break; | |
709 | ||
710 | if (p == NULL) | |
711 | { | |
712 | p = ((struct elf_i386_pcrel_relocs_copied *) | |
713 | bfd_alloc (dynobj, sizeof *p)); | |
714 | if (p == NULL) | |
715 | return false; | |
716 | p->next = eh->pcrel_relocs_copied; | |
717 | eh->pcrel_relocs_copied = p; | |
718 | p->section = sreloc; | |
719 | p->count = 0; | |
720 | } | |
721 | ||
722 | ++p->count; | |
723 | } | |
724 | } | |
725 | ||
726 | break; | |
727 | ||
728 | /* This relocation describes the C++ object vtable hierarchy. | |
729 | Reconstruct it for later use during GC. */ | |
730 | case R_386_GNU_VTINHERIT: | |
731 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
732 | return false; | |
733 | break; | |
734 | ||
735 | /* This relocation describes which C++ vtable entries are actually | |
736 | used. Record for later use during GC. */ | |
737 | case R_386_GNU_VTENTRY: | |
738 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset)) | |
739 | return false; | |
740 | break; | |
741 | ||
742 | default: | |
743 | break; | |
744 | } | |
745 | } | |
746 | ||
747 | return true; | |
748 | } | |
749 | ||
750 | /* Return the section that should be marked against GC for a given | |
751 | relocation. */ | |
752 | ||
753 | static asection * | |
754 | elf_i386_gc_mark_hook (abfd, info, rel, h, sym) | |
755 | bfd *abfd; | |
7442e600 | 756 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
757 | Elf_Internal_Rela *rel; |
758 | struct elf_link_hash_entry *h; | |
759 | Elf_Internal_Sym *sym; | |
760 | { | |
761 | if (h != NULL) | |
762 | { | |
763 | switch (ELF32_R_TYPE (rel->r_info)) | |
764 | { | |
765 | case R_386_GNU_VTINHERIT: | |
766 | case R_386_GNU_VTENTRY: | |
767 | break; | |
768 | ||
769 | default: | |
770 | switch (h->root.type) | |
771 | { | |
772 | case bfd_link_hash_defined: | |
773 | case bfd_link_hash_defweak: | |
774 | return h->root.u.def.section; | |
775 | ||
776 | case bfd_link_hash_common: | |
777 | return h->root.u.c.p->section; | |
778 | ||
779 | default: | |
780 | break; | |
781 | } | |
782 | } | |
783 | } | |
784 | else | |
785 | { | |
786 | if (!(elf_bad_symtab (abfd) | |
787 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
788 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
789 | && sym->st_shndx != SHN_COMMON)) | |
790 | { | |
791 | return bfd_section_from_elf_index (abfd, sym->st_shndx); | |
792 | } | |
793 | } | |
794 | ||
795 | return NULL; | |
796 | } | |
797 | ||
798 | /* Update the got entry reference counts for the section being removed. */ | |
799 | ||
800 | static boolean | |
801 | elf_i386_gc_sweep_hook (abfd, info, sec, relocs) | |
dd5724d5 | 802 | bfd *abfd; |
7442e600 | 803 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
dd5724d5 AM |
804 | asection *sec; |
805 | const Elf_Internal_Rela *relocs; | |
252b5132 | 806 | { |
dd5724d5 AM |
807 | Elf_Internal_Shdr *symtab_hdr; |
808 | struct elf_link_hash_entry **sym_hashes; | |
809 | bfd_signed_vma *local_got_refcounts; | |
810 | const Elf_Internal_Rela *rel, *relend; | |
811 | unsigned long r_symndx; | |
812 | struct elf_link_hash_entry *h; | |
813 | bfd *dynobj; | |
814 | asection *sgot; | |
815 | asection *srelgot; | |
816 | ||
817 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
818 | sym_hashes = elf_sym_hashes (abfd); | |
819 | local_got_refcounts = elf_local_got_refcounts (abfd); | |
820 | ||
821 | dynobj = elf_hash_table (info)->dynobj; | |
822 | if (dynobj == NULL) | |
823 | return true; | |
824 | ||
825 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
826 | srelgot = bfd_get_section_by_name (dynobj, ".rel.got"); | |
827 | ||
828 | relend = relocs + sec->reloc_count; | |
829 | for (rel = relocs; rel < relend; rel++) | |
830 | switch (ELF32_R_TYPE (rel->r_info)) | |
831 | { | |
832 | case R_386_GOT32: | |
833 | case R_386_GOTOFF: | |
834 | case R_386_GOTPC: | |
835 | r_symndx = ELF32_R_SYM (rel->r_info); | |
836 | if (r_symndx >= symtab_hdr->sh_info) | |
837 | { | |
838 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
839 | if (h->got.refcount > 0) | |
840 | { | |
841 | h->got.refcount -= 1; | |
842 | if (h->got.refcount == 0) | |
843 | { | |
844 | sgot->_raw_size -= 4; | |
845 | srelgot->_raw_size -= sizeof (Elf32_External_Rel); | |
846 | } | |
847 | } | |
848 | } | |
849 | else if (local_got_refcounts != NULL) | |
850 | { | |
851 | if (local_got_refcounts[r_symndx] > 0) | |
852 | { | |
853 | local_got_refcounts[r_symndx] -= 1; | |
854 | if (local_got_refcounts[r_symndx] == 0) | |
855 | { | |
856 | sgot->_raw_size -= 4; | |
857 | if (info->shared) | |
858 | srelgot->_raw_size -= sizeof (Elf32_External_Rel); | |
859 | } | |
860 | } | |
861 | } | |
862 | break; | |
863 | ||
864 | case R_386_PLT32: | |
865 | r_symndx = ELF32_R_SYM (rel->r_info); | |
866 | if (r_symndx >= symtab_hdr->sh_info) | |
867 | { | |
868 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
869 | if (h->plt.refcount > 0) | |
870 | h->plt.refcount -= 1; | |
871 | } | |
872 | break; | |
873 | ||
874 | default: | |
875 | break; | |
876 | } | |
252b5132 RH |
877 | |
878 | return true; | |
879 | } | |
880 | ||
881 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
882 | regular object. The current definition is in some section of the | |
883 | dynamic object, but we're not including those sections. We have to | |
884 | change the definition to something the rest of the link can | |
885 | understand. */ | |
886 | ||
887 | static boolean | |
888 | elf_i386_adjust_dynamic_symbol (info, h) | |
889 | struct bfd_link_info *info; | |
890 | struct elf_link_hash_entry *h; | |
891 | { | |
892 | bfd *dynobj; | |
893 | asection *s; | |
894 | unsigned int power_of_two; | |
895 | ||
896 | dynobj = elf_hash_table (info)->dynobj; | |
897 | ||
898 | /* Make sure we know what is going on here. */ | |
899 | BFD_ASSERT (dynobj != NULL | |
900 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) | |
901 | || h->weakdef != NULL | |
902 | || ((h->elf_link_hash_flags | |
903 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
904 | && (h->elf_link_hash_flags | |
905 | & ELF_LINK_HASH_REF_REGULAR) != 0 | |
906 | && (h->elf_link_hash_flags | |
907 | & ELF_LINK_HASH_DEF_REGULAR) == 0))); | |
908 | ||
909 | /* If this is a function, put it in the procedure linkage table. We | |
910 | will fill in the contents of the procedure linkage table later, | |
911 | when we know the address of the .got section. */ | |
912 | if (h->type == STT_FUNC | |
913 | || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) | |
914 | { | |
dd5724d5 AM |
915 | if ((! info->shared |
916 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
917 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) | |
918 | || (info->shared && h->plt.refcount <= 0)) | |
252b5132 RH |
919 | { |
920 | /* This case can occur if we saw a PLT32 reloc in an input | |
dd5724d5 AM |
921 | file, but the symbol was never referred to by a dynamic |
922 | object, or if all references were garbage collected. In | |
923 | such a case, we don't actually need to build a procedure | |
924 | linkage table, and we can just do a PC32 reloc instead. */ | |
925 | h->plt.offset = (bfd_vma) -1; | |
926 | h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; | |
252b5132 RH |
927 | return true; |
928 | } | |
929 | ||
930 | /* Make sure this symbol is output as a dynamic symbol. */ | |
931 | if (h->dynindx == -1) | |
932 | { | |
933 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
934 | return false; | |
935 | } | |
936 | ||
937 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
938 | BFD_ASSERT (s != NULL); | |
939 | ||
940 | /* If this is the first .plt entry, make room for the special | |
941 | first entry. */ | |
942 | if (s->_raw_size == 0) | |
943 | s->_raw_size += PLT_ENTRY_SIZE; | |
944 | ||
945 | /* If this symbol is not defined in a regular file, and we are | |
946 | not generating a shared library, then set the symbol to this | |
947 | location in the .plt. This is required to make function | |
948 | pointers compare as equal between the normal executable and | |
949 | the shared library. */ | |
950 | if (! info->shared | |
951 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
952 | { | |
953 | h->root.u.def.section = s; | |
954 | h->root.u.def.value = s->_raw_size; | |
955 | } | |
956 | ||
957 | h->plt.offset = s->_raw_size; | |
958 | ||
959 | /* Make room for this entry. */ | |
960 | s->_raw_size += PLT_ENTRY_SIZE; | |
961 | ||
962 | /* We also need to make an entry in the .got.plt section, which | |
963 | will be placed in the .got section by the linker script. */ | |
252b5132 RH |
964 | s = bfd_get_section_by_name (dynobj, ".got.plt"); |
965 | BFD_ASSERT (s != NULL); | |
966 | s->_raw_size += 4; | |
967 | ||
968 | /* We also need to make an entry in the .rel.plt section. */ | |
252b5132 RH |
969 | s = bfd_get_section_by_name (dynobj, ".rel.plt"); |
970 | BFD_ASSERT (s != NULL); | |
971 | s->_raw_size += sizeof (Elf32_External_Rel); | |
972 | ||
973 | return true; | |
974 | } | |
975 | ||
976 | /* If this is a weak symbol, and there is a real definition, the | |
977 | processor independent code will have arranged for us to see the | |
978 | real definition first, and we can just use the same value. */ | |
979 | if (h->weakdef != NULL) | |
980 | { | |
981 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined | |
982 | || h->weakdef->root.type == bfd_link_hash_defweak); | |
983 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
984 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
985 | return true; | |
986 | } | |
987 | ||
988 | /* This is a reference to a symbol defined by a dynamic object which | |
989 | is not a function. */ | |
990 | ||
991 | /* If we are creating a shared library, we must presume that the | |
992 | only references to the symbol are via the global offset table. | |
993 | For such cases we need not do anything here; the relocations will | |
994 | be handled correctly by relocate_section. */ | |
995 | if (info->shared) | |
996 | return true; | |
997 | ||
7843f00e ILT |
998 | /* If there are no references to this symbol that do not use the |
999 | GOT, we don't need to generate a copy reloc. */ | |
1000 | if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) | |
1001 | return true; | |
1002 | ||
252b5132 RH |
1003 | /* We must allocate the symbol in our .dynbss section, which will |
1004 | become part of the .bss section of the executable. There will be | |
1005 | an entry for this symbol in the .dynsym section. The dynamic | |
1006 | object will contain position independent code, so all references | |
1007 | from the dynamic object to this symbol will go through the global | |
1008 | offset table. The dynamic linker will use the .dynsym entry to | |
1009 | determine the address it must put in the global offset table, so | |
1010 | both the dynamic object and the regular object will refer to the | |
1011 | same memory location for the variable. */ | |
1012 | ||
1013 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
1014 | BFD_ASSERT (s != NULL); | |
1015 | ||
1016 | /* We must generate a R_386_COPY reloc to tell the dynamic linker to | |
1017 | copy the initial value out of the dynamic object and into the | |
1018 | runtime process image. We need to remember the offset into the | |
1019 | .rel.bss section we are going to use. */ | |
1020 | if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) | |
1021 | { | |
1022 | asection *srel; | |
1023 | ||
1024 | srel = bfd_get_section_by_name (dynobj, ".rel.bss"); | |
1025 | BFD_ASSERT (srel != NULL); | |
1026 | srel->_raw_size += sizeof (Elf32_External_Rel); | |
1027 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; | |
1028 | } | |
1029 | ||
1030 | /* We need to figure out the alignment required for this symbol. I | |
1031 | have no idea how ELF linkers handle this. */ | |
1032 | power_of_two = bfd_log2 (h->size); | |
1033 | if (power_of_two > 3) | |
1034 | power_of_two = 3; | |
1035 | ||
1036 | /* Apply the required alignment. */ | |
1037 | s->_raw_size = BFD_ALIGN (s->_raw_size, | |
1038 | (bfd_size_type) (1 << power_of_two)); | |
1039 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) | |
1040 | { | |
1041 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
1042 | return false; | |
1043 | } | |
1044 | ||
1045 | /* Define the symbol as being at this point in the section. */ | |
1046 | h->root.u.def.section = s; | |
1047 | h->root.u.def.value = s->_raw_size; | |
1048 | ||
1049 | /* Increment the section size to make room for the symbol. */ | |
1050 | s->_raw_size += h->size; | |
1051 | ||
1052 | return true; | |
1053 | } | |
1054 | ||
1055 | /* Set the sizes of the dynamic sections. */ | |
1056 | ||
1057 | static boolean | |
1058 | elf_i386_size_dynamic_sections (output_bfd, info) | |
1059 | bfd *output_bfd; | |
1060 | struct bfd_link_info *info; | |
1061 | { | |
1062 | bfd *dynobj; | |
1063 | asection *s; | |
1064 | boolean plt; | |
1065 | boolean relocs; | |
1066 | boolean reltext; | |
1067 | ||
1068 | dynobj = elf_hash_table (info)->dynobj; | |
1069 | BFD_ASSERT (dynobj != NULL); | |
1070 | ||
1071 | if (elf_hash_table (info)->dynamic_sections_created) | |
1072 | { | |
1073 | /* Set the contents of the .interp section to the interpreter. */ | |
1074 | if (! info->shared) | |
1075 | { | |
1076 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
1077 | BFD_ASSERT (s != NULL); | |
1078 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
1079 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
1080 | } | |
1081 | } | |
1082 | else | |
1083 | { | |
1084 | /* We may have created entries in the .rel.got section. | |
1085 | However, if we are not creating the dynamic sections, we will | |
1086 | not actually use these entries. Reset the size of .rel.got, | |
1087 | which will cause it to get stripped from the output file | |
1088 | below. */ | |
1089 | s = bfd_get_section_by_name (dynobj, ".rel.got"); | |
1090 | if (s != NULL) | |
1091 | s->_raw_size = 0; | |
1092 | } | |
1093 | ||
1094 | /* If this is a -Bsymbolic shared link, then we need to discard all | |
1095 | PC relative relocs against symbols defined in a regular object. | |
1096 | We allocated space for them in the check_relocs routine, but we | |
1097 | will not fill them in in the relocate_section routine. */ | |
1098 | if (info->shared && info->symbolic) | |
1099 | elf_i386_link_hash_traverse (elf_i386_hash_table (info), | |
1100 | elf_i386_discard_copies, | |
1101 | (PTR) NULL); | |
1102 | ||
1103 | /* The check_relocs and adjust_dynamic_symbol entry points have | |
1104 | determined the sizes of the various dynamic sections. Allocate | |
1105 | memory for them. */ | |
1106 | plt = false; | |
1107 | relocs = false; | |
1108 | reltext = false; | |
1109 | for (s = dynobj->sections; s != NULL; s = s->next) | |
1110 | { | |
1111 | const char *name; | |
1112 | boolean strip; | |
1113 | ||
1114 | if ((s->flags & SEC_LINKER_CREATED) == 0) | |
1115 | continue; | |
1116 | ||
1117 | /* It's OK to base decisions on the section name, because none | |
1118 | of the dynobj section names depend upon the input files. */ | |
1119 | name = bfd_get_section_name (dynobj, s); | |
1120 | ||
1121 | strip = false; | |
1122 | ||
1123 | if (strcmp (name, ".plt") == 0) | |
1124 | { | |
1125 | if (s->_raw_size == 0) | |
1126 | { | |
1127 | /* Strip this section if we don't need it; see the | |
1128 | comment below. */ | |
1129 | strip = true; | |
1130 | } | |
1131 | else | |
1132 | { | |
1133 | /* Remember whether there is a PLT. */ | |
1134 | plt = true; | |
1135 | } | |
1136 | } | |
1137 | else if (strncmp (name, ".rel", 4) == 0) | |
1138 | { | |
1139 | if (s->_raw_size == 0) | |
1140 | { | |
1141 | /* If we don't need this section, strip it from the | |
1142 | output file. This is mostly to handle .rel.bss and | |
1143 | .rel.plt. We must create both sections in | |
1144 | create_dynamic_sections, because they must be created | |
1145 | before the linker maps input sections to output | |
1146 | sections. The linker does that before | |
1147 | adjust_dynamic_symbol is called, and it is that | |
1148 | function which decides whether anything needs to go | |
1149 | into these sections. */ | |
1150 | strip = true; | |
1151 | } | |
1152 | else | |
1153 | { | |
1154 | asection *target; | |
1155 | ||
1156 | /* Remember whether there are any reloc sections other | |
1157 | than .rel.plt. */ | |
1158 | if (strcmp (name, ".rel.plt") != 0) | |
1159 | { | |
1160 | const char *outname; | |
1161 | ||
1162 | relocs = true; | |
1163 | ||
1164 | /* If this relocation section applies to a read only | |
1165 | section, then we probably need a DT_TEXTREL | |
1166 | entry. The entries in the .rel.plt section | |
1167 | really apply to the .got section, which we | |
1168 | created ourselves and so know is not readonly. */ | |
1169 | outname = bfd_get_section_name (output_bfd, | |
1170 | s->output_section); | |
1171 | target = bfd_get_section_by_name (output_bfd, outname + 4); | |
1172 | if (target != NULL | |
1173 | && (target->flags & SEC_READONLY) != 0 | |
1174 | && (target->flags & SEC_ALLOC) != 0) | |
1175 | reltext = true; | |
1176 | } | |
1177 | ||
1178 | /* We use the reloc_count field as a counter if we need | |
1179 | to copy relocs into the output file. */ | |
1180 | s->reloc_count = 0; | |
1181 | } | |
1182 | } | |
1183 | else if (strncmp (name, ".got", 4) != 0) | |
1184 | { | |
1185 | /* It's not one of our sections, so don't allocate space. */ | |
1186 | continue; | |
1187 | } | |
1188 | ||
1189 | if (strip) | |
1190 | { | |
7f8d5fc9 | 1191 | _bfd_strip_section_from_output (info, s); |
252b5132 RH |
1192 | continue; |
1193 | } | |
1194 | ||
1195 | /* Allocate memory for the section contents. */ | |
1196 | s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size); | |
1197 | if (s->contents == NULL && s->_raw_size != 0) | |
1198 | return false; | |
1199 | } | |
1200 | ||
1201 | if (elf_hash_table (info)->dynamic_sections_created) | |
1202 | { | |
1203 | /* Add some entries to the .dynamic section. We fill in the | |
1204 | values later, in elf_i386_finish_dynamic_sections, but we | |
1205 | must add the entries now so that we get the correct size for | |
1206 | the .dynamic section. The DT_DEBUG entry is filled in by the | |
1207 | dynamic linker and used by the debugger. */ | |
1208 | if (! info->shared) | |
1209 | { | |
1210 | if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)) | |
1211 | return false; | |
1212 | } | |
1213 | ||
1214 | if (plt) | |
1215 | { | |
1216 | if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0) | |
1217 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
1218 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL) | |
1219 | || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)) | |
1220 | return false; | |
1221 | } | |
1222 | ||
1223 | if (relocs) | |
1224 | { | |
1225 | if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0) | |
1226 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0) | |
1227 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT, | |
1228 | sizeof (Elf32_External_Rel))) | |
1229 | return false; | |
1230 | } | |
1231 | ||
1232 | if (reltext) | |
1233 | { | |
1234 | if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0)) | |
1235 | return false; | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | return true; | |
1240 | } | |
1241 | ||
1242 | /* This function is called via elf_i386_link_hash_traverse if we are | |
1243 | creating a shared object with -Bsymbolic. It discards the space | |
1244 | allocated to copy PC relative relocs against symbols which are | |
1245 | defined in regular objects. We allocated space for them in the | |
1246 | check_relocs routine, but we won't fill them in in the | |
1247 | relocate_section routine. */ | |
1248 | ||
1249 | /*ARGSUSED*/ | |
1250 | static boolean | |
1251 | elf_i386_discard_copies (h, ignore) | |
1252 | struct elf_i386_link_hash_entry *h; | |
7442e600 | 1253 | PTR ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
1254 | { |
1255 | struct elf_i386_pcrel_relocs_copied *s; | |
1256 | ||
1257 | /* We only discard relocs for symbols defined in a regular object. */ | |
1258 | if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1259 | return true; | |
1260 | ||
1261 | for (s = h->pcrel_relocs_copied; s != NULL; s = s->next) | |
1262 | s->section->_raw_size -= s->count * sizeof (Elf32_External_Rel); | |
1263 | ||
1264 | return true; | |
1265 | } | |
1266 | ||
1267 | /* Relocate an i386 ELF section. */ | |
1268 | ||
1269 | static boolean | |
1270 | elf_i386_relocate_section (output_bfd, info, input_bfd, input_section, | |
1271 | contents, relocs, local_syms, local_sections) | |
1272 | bfd *output_bfd; | |
1273 | struct bfd_link_info *info; | |
1274 | bfd *input_bfd; | |
1275 | asection *input_section; | |
1276 | bfd_byte *contents; | |
1277 | Elf_Internal_Rela *relocs; | |
1278 | Elf_Internal_Sym *local_syms; | |
1279 | asection **local_sections; | |
1280 | { | |
1281 | bfd *dynobj; | |
1282 | Elf_Internal_Shdr *symtab_hdr; | |
1283 | struct elf_link_hash_entry **sym_hashes; | |
1284 | bfd_vma *local_got_offsets; | |
1285 | asection *sgot; | |
1286 | asection *splt; | |
1287 | asection *sreloc; | |
1288 | Elf_Internal_Rela *rel; | |
1289 | Elf_Internal_Rela *relend; | |
1290 | ||
1291 | dynobj = elf_hash_table (info)->dynobj; | |
1292 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
1293 | sym_hashes = elf_sym_hashes (input_bfd); | |
1294 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
1295 | ||
252b5132 | 1296 | sreloc = NULL; |
dd5724d5 AM |
1297 | splt = NULL; |
1298 | sgot = NULL; | |
1299 | if (dynobj != NULL) | |
1300 | { | |
1301 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1302 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1303 | } | |
252b5132 RH |
1304 | |
1305 | rel = relocs; | |
1306 | relend = relocs + input_section->reloc_count; | |
1307 | for (; rel < relend; rel++) | |
1308 | { | |
1309 | int r_type; | |
1310 | reloc_howto_type *howto; | |
1311 | unsigned long r_symndx; | |
1312 | struct elf_link_hash_entry *h; | |
1313 | Elf_Internal_Sym *sym; | |
1314 | asection *sec; | |
1315 | bfd_vma relocation; | |
1316 | bfd_reloc_status_type r; | |
1b452ec6 | 1317 | unsigned int indx; |
252b5132 RH |
1318 | |
1319 | r_type = ELF32_R_TYPE (rel->r_info); | |
1320 | if (r_type == R_386_GNU_VTINHERIT | |
1321 | || r_type == R_386_GNU_VTENTRY) | |
1322 | continue; | |
1b452ec6 AM |
1323 | if ((indx = (unsigned) r_type) >= R_386_standard |
1324 | && ((indx = (unsigned) r_type - R_386_ext_offset) | |
1325 | >= sizeof (elf_howto_table) / sizeof (elf_howto_table[0]))) | |
252b5132 RH |
1326 | { |
1327 | bfd_set_error (bfd_error_bad_value); | |
1328 | return false; | |
1329 | } | |
1b452ec6 | 1330 | howto = elf_howto_table + indx; |
252b5132 RH |
1331 | |
1332 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1333 | ||
1334 | if (info->relocateable) | |
1335 | { | |
1336 | /* This is a relocateable link. We don't have to change | |
1337 | anything, unless the reloc is against a section symbol, | |
1338 | in which case we have to adjust according to where the | |
1339 | section symbol winds up in the output section. */ | |
1340 | if (r_symndx < symtab_hdr->sh_info) | |
1341 | { | |
1342 | sym = local_syms + r_symndx; | |
1343 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
1344 | { | |
1345 | bfd_vma val; | |
1346 | ||
1347 | sec = local_sections[r_symndx]; | |
1348 | val = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
1349 | val += sec->output_offset + sym->st_value; | |
1350 | bfd_put_32 (input_bfd, val, contents + rel->r_offset); | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | continue; | |
1355 | } | |
1356 | ||
1357 | /* This is a final link. */ | |
1358 | h = NULL; | |
1359 | sym = NULL; | |
1360 | sec = NULL; | |
1361 | if (r_symndx < symtab_hdr->sh_info) | |
1362 | { | |
1363 | sym = local_syms + r_symndx; | |
1364 | sec = local_sections[r_symndx]; | |
1365 | relocation = (sec->output_section->vma | |
1366 | + sec->output_offset | |
1367 | + sym->st_value); | |
1368 | } | |
1369 | else | |
1370 | { | |
1371 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
1372 | while (h->root.type == bfd_link_hash_indirect | |
1373 | || h->root.type == bfd_link_hash_warning) | |
1374 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1375 | if (h->root.type == bfd_link_hash_defined | |
1376 | || h->root.type == bfd_link_hash_defweak) | |
1377 | { | |
1378 | sec = h->root.u.def.section; | |
1379 | if (r_type == R_386_GOTPC | |
1380 | || (r_type == R_386_PLT32 | |
dd5724d5 | 1381 | && splt != NULL |
252b5132 RH |
1382 | && h->plt.offset != (bfd_vma) -1) |
1383 | || (r_type == R_386_GOT32 | |
1384 | && elf_hash_table (info)->dynamic_sections_created | |
1385 | && (! info->shared | |
1386 | || (! info->symbolic && h->dynindx != -1) | |
1387 | || (h->elf_link_hash_flags | |
1388 | & ELF_LINK_HASH_DEF_REGULAR) == 0)) | |
1389 | || (info->shared | |
1390 | && ((! info->symbolic && h->dynindx != -1) | |
1391 | || (h->elf_link_hash_flags | |
1392 | & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1393 | && (r_type == R_386_32 | |
1394 | || r_type == R_386_PC32) | |
1395 | && ((input_section->flags & SEC_ALLOC) != 0 | |
1396 | /* DWARF will emit R_386_32 relocations in its | |
1397 | sections against symbols defined externally | |
1398 | in shared libraries. We can't do anything | |
1399 | with them here. */ | |
1319985e AM |
1400 | || ((input_section->flags & SEC_DEBUGGING) != 0 |
1401 | && (h->elf_link_hash_flags | |
1402 | & ELF_LINK_HASH_DEF_DYNAMIC) != 0)))) | |
252b5132 RH |
1403 | { |
1404 | /* In these cases, we don't need the relocation | |
1405 | value. We check specially because in some | |
1406 | obscure cases sec->output_section will be NULL. */ | |
1407 | relocation = 0; | |
1408 | } | |
1409 | else if (sec->output_section == NULL) | |
1410 | { | |
1411 | (*_bfd_error_handler) | |
1412 | (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"), | |
1413 | bfd_get_filename (input_bfd), h->root.root.string, | |
1414 | bfd_get_section_name (input_bfd, input_section)); | |
1415 | relocation = 0; | |
1416 | } | |
1417 | else | |
1418 | relocation = (h->root.u.def.value | |
1419 | + sec->output_section->vma | |
1420 | + sec->output_offset); | |
1421 | } | |
1422 | else if (h->root.type == bfd_link_hash_undefweak) | |
1423 | relocation = 0; | |
3a27a730 L |
1424 | else if (info->shared && !info->symbolic |
1425 | && !info->no_undefined | |
1426 | && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) | |
252b5132 RH |
1427 | relocation = 0; |
1428 | else | |
1429 | { | |
1430 | if (! ((*info->callbacks->undefined_symbol) | |
1431 | (info, h->root.root.string, input_bfd, | |
5cc7c785 | 1432 | input_section, rel->r_offset, |
3a27a730 L |
1433 | (!info->shared || info->no_undefined |
1434 | || ELF_ST_VISIBILITY (h->other))))) | |
252b5132 RH |
1435 | return false; |
1436 | relocation = 0; | |
1437 | } | |
1438 | } | |
1439 | ||
1440 | switch (r_type) | |
1441 | { | |
1442 | case R_386_GOT32: | |
1443 | /* Relocation is to the entry for this symbol in the global | |
1444 | offset table. */ | |
dd5724d5 | 1445 | BFD_ASSERT (sgot != NULL); |
252b5132 RH |
1446 | |
1447 | if (h != NULL) | |
1448 | { | |
1449 | bfd_vma off; | |
1450 | ||
1451 | off = h->got.offset; | |
1452 | BFD_ASSERT (off != (bfd_vma) -1); | |
1453 | ||
1454 | if (! elf_hash_table (info)->dynamic_sections_created | |
1455 | || (info->shared | |
1456 | && (info->symbolic || h->dynindx == -1) | |
1457 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
1458 | { | |
1459 | /* This is actually a static link, or it is a | |
1460 | -Bsymbolic link and the symbol is defined | |
1461 | locally, or the symbol was forced to be local | |
1462 | because of a version file. We must initialize | |
1463 | this entry in the global offset table. Since the | |
1464 | offset must always be a multiple of 4, we use the | |
1465 | least significant bit to record whether we have | |
1466 | initialized it already. | |
1467 | ||
1468 | When doing a dynamic link, we create a .rel.got | |
1469 | relocation entry to initialize the value. This | |
1470 | is done in the finish_dynamic_symbol routine. */ | |
1471 | if ((off & 1) != 0) | |
1472 | off &= ~1; | |
1473 | else | |
1474 | { | |
1475 | bfd_put_32 (output_bfd, relocation, | |
1476 | sgot->contents + off); | |
1477 | h->got.offset |= 1; | |
1478 | } | |
1479 | } | |
1480 | ||
1481 | relocation = sgot->output_offset + off; | |
1482 | } | |
1483 | else | |
1484 | { | |
1485 | bfd_vma off; | |
1486 | ||
1487 | BFD_ASSERT (local_got_offsets != NULL | |
1488 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1489 | ||
1490 | off = local_got_offsets[r_symndx]; | |
1491 | ||
1492 | /* The offset must always be a multiple of 4. We use | |
1493 | the least significant bit to record whether we have | |
1494 | already generated the necessary reloc. */ | |
1495 | if ((off & 1) != 0) | |
1496 | off &= ~1; | |
1497 | else | |
1498 | { | |
1499 | bfd_put_32 (output_bfd, relocation, sgot->contents + off); | |
1500 | ||
1501 | if (info->shared) | |
1502 | { | |
1503 | asection *srelgot; | |
1504 | Elf_Internal_Rel outrel; | |
1505 | ||
1506 | srelgot = bfd_get_section_by_name (dynobj, ".rel.got"); | |
1507 | BFD_ASSERT (srelgot != NULL); | |
1508 | ||
1509 | outrel.r_offset = (sgot->output_section->vma | |
1510 | + sgot->output_offset | |
1511 | + off); | |
1512 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
1513 | bfd_elf32_swap_reloc_out (output_bfd, &outrel, | |
1514 | (((Elf32_External_Rel *) | |
1515 | srelgot->contents) | |
1516 | + srelgot->reloc_count)); | |
1517 | ++srelgot->reloc_count; | |
1518 | } | |
1519 | ||
1520 | local_got_offsets[r_symndx] |= 1; | |
1521 | } | |
1522 | ||
1523 | relocation = sgot->output_offset + off; | |
1524 | } | |
1525 | ||
1526 | break; | |
1527 | ||
1528 | case R_386_GOTOFF: | |
1529 | /* Relocation is relative to the start of the global offset | |
1530 | table. */ | |
1531 | ||
1532 | if (sgot == NULL) | |
1533 | { | |
1534 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1535 | BFD_ASSERT (sgot != NULL); | |
1536 | } | |
1537 | ||
1538 | /* Note that sgot->output_offset is not involved in this | |
1539 | calculation. We always want the start of .got. If we | |
1540 | defined _GLOBAL_OFFSET_TABLE in a different way, as is | |
1541 | permitted by the ABI, we might have to change this | |
1542 | calculation. */ | |
1543 | relocation -= sgot->output_section->vma; | |
1544 | ||
1545 | break; | |
1546 | ||
1547 | case R_386_GOTPC: | |
1548 | /* Use global offset table as symbol value. */ | |
1549 | ||
1550 | if (sgot == NULL) | |
1551 | { | |
1552 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1553 | BFD_ASSERT (sgot != NULL); | |
1554 | } | |
1555 | ||
1556 | relocation = sgot->output_section->vma; | |
1557 | ||
1558 | break; | |
1559 | ||
1560 | case R_386_PLT32: | |
1561 | /* Relocation is to the entry for this symbol in the | |
1562 | procedure linkage table. */ | |
1563 | ||
dd5724d5 | 1564 | /* Resolve a PLT32 reloc against a local symbol directly, |
252b5132 RH |
1565 | without using the procedure linkage table. */ |
1566 | if (h == NULL) | |
1567 | break; | |
1568 | ||
dd5724d5 AM |
1569 | if (h->plt.offset == (bfd_vma) -1 |
1570 | || splt == NULL) | |
252b5132 RH |
1571 | { |
1572 | /* We didn't make a PLT entry for this symbol. This | |
1573 | happens when statically linking PIC code, or when | |
1574 | using -Bsymbolic. */ | |
1575 | break; | |
1576 | } | |
1577 | ||
252b5132 RH |
1578 | relocation = (splt->output_section->vma |
1579 | + splt->output_offset | |
1580 | + h->plt.offset); | |
1581 | ||
1582 | break; | |
1583 | ||
1584 | case R_386_32: | |
1585 | case R_386_PC32: | |
1586 | if (info->shared | |
1587 | && (input_section->flags & SEC_ALLOC) != 0 | |
1588 | && (r_type != R_386_PC32 | |
1589 | || (h != NULL | |
1590 | && h->dynindx != -1 | |
1591 | && (! info->symbolic | |
1592 | || (h->elf_link_hash_flags | |
1593 | & ELF_LINK_HASH_DEF_REGULAR) == 0)))) | |
1594 | { | |
1595 | Elf_Internal_Rel outrel; | |
1596 | boolean skip, relocate; | |
1597 | ||
1598 | /* When generating a shared object, these relocations | |
1599 | are copied into the output file to be resolved at run | |
1600 | time. */ | |
1601 | ||
1602 | if (sreloc == NULL) | |
1603 | { | |
1604 | const char *name; | |
1605 | ||
1606 | name = (bfd_elf_string_from_elf_section | |
1607 | (input_bfd, | |
1608 | elf_elfheader (input_bfd)->e_shstrndx, | |
1609 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1610 | if (name == NULL) | |
1611 | return false; | |
1612 | ||
1613 | BFD_ASSERT (strncmp (name, ".rel", 4) == 0 | |
1614 | && strcmp (bfd_get_section_name (input_bfd, | |
1615 | input_section), | |
1616 | name + 4) == 0); | |
1617 | ||
1618 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1619 | BFD_ASSERT (sreloc != NULL); | |
1620 | } | |
1621 | ||
1622 | skip = false; | |
1623 | ||
1624 | if (elf_section_data (input_section)->stab_info == NULL) | |
1625 | outrel.r_offset = rel->r_offset; | |
1626 | else | |
1627 | { | |
1628 | bfd_vma off; | |
1629 | ||
1630 | off = (_bfd_stab_section_offset | |
1631 | (output_bfd, &elf_hash_table (info)->stab_info, | |
1632 | input_section, | |
1633 | &elf_section_data (input_section)->stab_info, | |
1634 | rel->r_offset)); | |
1635 | if (off == (bfd_vma) -1) | |
1636 | skip = true; | |
1637 | outrel.r_offset = off; | |
1638 | } | |
1639 | ||
1640 | outrel.r_offset += (input_section->output_section->vma | |
1641 | + input_section->output_offset); | |
1642 | ||
1643 | if (skip) | |
1644 | { | |
1645 | memset (&outrel, 0, sizeof outrel); | |
1646 | relocate = false; | |
1647 | } | |
1648 | else if (r_type == R_386_PC32) | |
1649 | { | |
1650 | BFD_ASSERT (h != NULL && h->dynindx != -1); | |
1651 | relocate = false; | |
1652 | outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32); | |
1653 | } | |
1654 | else | |
1655 | { | |
1656 | /* h->dynindx may be -1 if this symbol was marked to | |
1657 | become local. */ | |
1658 | if (h == NULL | |
1659 | || ((info->symbolic || h->dynindx == -1) | |
1660 | && (h->elf_link_hash_flags | |
1661 | & ELF_LINK_HASH_DEF_REGULAR) != 0)) | |
1662 | { | |
1663 | relocate = true; | |
1664 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
1665 | } | |
1666 | else | |
1667 | { | |
1668 | BFD_ASSERT (h->dynindx != -1); | |
1669 | relocate = false; | |
1670 | outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32); | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | bfd_elf32_swap_reloc_out (output_bfd, &outrel, | |
1675 | (((Elf32_External_Rel *) | |
1676 | sreloc->contents) | |
1677 | + sreloc->reloc_count)); | |
1678 | ++sreloc->reloc_count; | |
1679 | ||
1680 | /* If this reloc is against an external symbol, we do | |
1681 | not want to fiddle with the addend. Otherwise, we | |
1682 | need to include the symbol value so that it becomes | |
1683 | an addend for the dynamic reloc. */ | |
1684 | if (! relocate) | |
1685 | continue; | |
1686 | } | |
1687 | ||
1688 | break; | |
1689 | ||
1690 | default: | |
1691 | break; | |
1692 | } | |
1693 | ||
1694 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
1695 | contents, rel->r_offset, | |
1696 | relocation, (bfd_vma) 0); | |
1697 | ||
1698 | if (r != bfd_reloc_ok) | |
1699 | { | |
1700 | switch (r) | |
1701 | { | |
1702 | default: | |
1703 | case bfd_reloc_outofrange: | |
1704 | abort (); | |
1705 | case bfd_reloc_overflow: | |
1706 | { | |
1707 | const char *name; | |
1708 | ||
1709 | if (h != NULL) | |
1710 | name = h->root.root.string; | |
1711 | else | |
1712 | { | |
1713 | name = bfd_elf_string_from_elf_section (input_bfd, | |
1714 | symtab_hdr->sh_link, | |
1715 | sym->st_name); | |
1716 | if (name == NULL) | |
1717 | return false; | |
1718 | if (*name == '\0') | |
1719 | name = bfd_section_name (input_bfd, sec); | |
1720 | } | |
1721 | if (! ((*info->callbacks->reloc_overflow) | |
1722 | (info, name, howto->name, (bfd_vma) 0, | |
1723 | input_bfd, input_section, rel->r_offset))) | |
1724 | return false; | |
1725 | } | |
1726 | break; | |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | return true; | |
1732 | } | |
1733 | ||
1734 | /* Finish up dynamic symbol handling. We set the contents of various | |
1735 | dynamic sections here. */ | |
1736 | ||
1737 | static boolean | |
1738 | elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1739 | bfd *output_bfd; | |
1740 | struct bfd_link_info *info; | |
1741 | struct elf_link_hash_entry *h; | |
1742 | Elf_Internal_Sym *sym; | |
1743 | { | |
1744 | bfd *dynobj; | |
1745 | ||
1746 | dynobj = elf_hash_table (info)->dynobj; | |
1747 | ||
1748 | if (h->plt.offset != (bfd_vma) -1) | |
1749 | { | |
1750 | asection *splt; | |
1751 | asection *sgot; | |
1752 | asection *srel; | |
1753 | bfd_vma plt_index; | |
1754 | bfd_vma got_offset; | |
1755 | Elf_Internal_Rel rel; | |
1756 | ||
1757 | /* This symbol has an entry in the procedure linkage table. Set | |
1758 | it up. */ | |
1759 | ||
1760 | BFD_ASSERT (h->dynindx != -1); | |
1761 | ||
1762 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1763 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1764 | srel = bfd_get_section_by_name (dynobj, ".rel.plt"); | |
1765 | BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL); | |
1766 | ||
1767 | /* Get the index in the procedure linkage table which | |
1768 | corresponds to this symbol. This is the index of this symbol | |
1769 | in all the symbols for which we are making plt entries. The | |
1770 | first entry in the procedure linkage table is reserved. */ | |
1771 | plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1; | |
1772 | ||
1773 | /* Get the offset into the .got table of the entry that | |
1774 | corresponds to this function. Each .got entry is 4 bytes. | |
1775 | The first three are reserved. */ | |
1776 | got_offset = (plt_index + 3) * 4; | |
1777 | ||
1778 | /* Fill in the entry in the procedure linkage table. */ | |
1779 | if (! info->shared) | |
1780 | { | |
1781 | memcpy (splt->contents + h->plt.offset, elf_i386_plt_entry, | |
1782 | PLT_ENTRY_SIZE); | |
1783 | bfd_put_32 (output_bfd, | |
1784 | (sgot->output_section->vma | |
1785 | + sgot->output_offset | |
1786 | + got_offset), | |
1787 | splt->contents + h->plt.offset + 2); | |
1788 | } | |
1789 | else | |
1790 | { | |
1791 | memcpy (splt->contents + h->plt.offset, elf_i386_pic_plt_entry, | |
1792 | PLT_ENTRY_SIZE); | |
1793 | bfd_put_32 (output_bfd, got_offset, | |
1794 | splt->contents + h->plt.offset + 2); | |
1795 | } | |
1796 | ||
1797 | bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel), | |
1798 | splt->contents + h->plt.offset + 7); | |
1799 | bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE), | |
1800 | splt->contents + h->plt.offset + 12); | |
1801 | ||
1802 | /* Fill in the entry in the global offset table. */ | |
1803 | bfd_put_32 (output_bfd, | |
1804 | (splt->output_section->vma | |
1805 | + splt->output_offset | |
1806 | + h->plt.offset | |
1807 | + 6), | |
1808 | sgot->contents + got_offset); | |
1809 | ||
1810 | /* Fill in the entry in the .rel.plt section. */ | |
1811 | rel.r_offset = (sgot->output_section->vma | |
1812 | + sgot->output_offset | |
1813 | + got_offset); | |
1814 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT); | |
1815 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
1816 | ((Elf32_External_Rel *) srel->contents | |
1817 | + plt_index)); | |
1818 | ||
1819 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) | |
1820 | { | |
1821 | /* Mark the symbol as undefined, rather than as defined in | |
1822 | the .plt section. Leave the value alone. */ | |
1823 | sym->st_shndx = SHN_UNDEF; | |
1824 | } | |
1825 | } | |
1826 | ||
1827 | if (h->got.offset != (bfd_vma) -1) | |
1828 | { | |
1829 | asection *sgot; | |
1830 | asection *srel; | |
1831 | Elf_Internal_Rel rel; | |
1832 | ||
1833 | /* This symbol has an entry in the global offset table. Set it | |
1834 | up. */ | |
1835 | ||
1836 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1837 | srel = bfd_get_section_by_name (dynobj, ".rel.got"); | |
1838 | BFD_ASSERT (sgot != NULL && srel != NULL); | |
1839 | ||
1840 | rel.r_offset = (sgot->output_section->vma | |
1841 | + sgot->output_offset | |
1842 | + (h->got.offset &~ 1)); | |
1843 | ||
dd5724d5 AM |
1844 | /* If this is a static link, or it is a -Bsymbolic link and the |
1845 | symbol is defined locally or was forced to be local because | |
1846 | of a version file, we just want to emit a RELATIVE reloc. | |
252b5132 RH |
1847 | The entry in the global offset table will already have been |
1848 | initialized in the relocate_section function. */ | |
dd5724d5 AM |
1849 | if (! elf_hash_table (info)->dynamic_sections_created |
1850 | || (info->shared | |
1851 | && (info->symbolic || h->dynindx == -1) | |
1852 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))) | |
1853 | { | |
1854 | rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
1855 | } | |
252b5132 RH |
1856 | else |
1857 | { | |
dd5724d5 | 1858 | BFD_ASSERT((h->got.offset & 1) == 0); |
252b5132 RH |
1859 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset); |
1860 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT); | |
1861 | } | |
1862 | ||
1863 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
1864 | ((Elf32_External_Rel *) srel->contents | |
1865 | + srel->reloc_count)); | |
1866 | ++srel->reloc_count; | |
1867 | } | |
1868 | ||
1869 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
1870 | { | |
1871 | asection *s; | |
1872 | Elf_Internal_Rel rel; | |
1873 | ||
1874 | /* This symbol needs a copy reloc. Set it up. */ | |
1875 | ||
1876 | BFD_ASSERT (h->dynindx != -1 | |
1877 | && (h->root.type == bfd_link_hash_defined | |
1878 | || h->root.type == bfd_link_hash_defweak)); | |
1879 | ||
1880 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
1881 | ".rel.bss"); | |
1882 | BFD_ASSERT (s != NULL); | |
1883 | ||
1884 | rel.r_offset = (h->root.u.def.value | |
1885 | + h->root.u.def.section->output_section->vma | |
1886 | + h->root.u.def.section->output_offset); | |
1887 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY); | |
1888 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
1889 | ((Elf32_External_Rel *) s->contents | |
1890 | + s->reloc_count)); | |
1891 | ++s->reloc_count; | |
1892 | } | |
1893 | ||
1894 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
1895 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
1896 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1897 | sym->st_shndx = SHN_ABS; | |
1898 | ||
1899 | return true; | |
1900 | } | |
1901 | ||
1902 | /* Finish up the dynamic sections. */ | |
1903 | ||
1904 | static boolean | |
1905 | elf_i386_finish_dynamic_sections (output_bfd, info) | |
1906 | bfd *output_bfd; | |
1907 | struct bfd_link_info *info; | |
1908 | { | |
1909 | bfd *dynobj; | |
1910 | asection *sgot; | |
1911 | asection *sdyn; | |
1912 | ||
1913 | dynobj = elf_hash_table (info)->dynobj; | |
1914 | ||
1915 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1916 | BFD_ASSERT (sgot != NULL); | |
1917 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
1918 | ||
1919 | if (elf_hash_table (info)->dynamic_sections_created) | |
1920 | { | |
1921 | asection *splt; | |
1922 | Elf32_External_Dyn *dyncon, *dynconend; | |
1923 | ||
1924 | BFD_ASSERT (sdyn != NULL); | |
1925 | ||
1926 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
1927 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
1928 | for (; dyncon < dynconend; dyncon++) | |
1929 | { | |
1930 | Elf_Internal_Dyn dyn; | |
1931 | const char *name; | |
1932 | asection *s; | |
1933 | ||
1934 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); | |
1935 | ||
1936 | switch (dyn.d_tag) | |
1937 | { | |
1938 | default: | |
1939 | break; | |
1940 | ||
1941 | case DT_PLTGOT: | |
1942 | name = ".got"; | |
1943 | goto get_vma; | |
1944 | case DT_JMPREL: | |
1945 | name = ".rel.plt"; | |
1946 | get_vma: | |
1947 | s = bfd_get_section_by_name (output_bfd, name); | |
1948 | BFD_ASSERT (s != NULL); | |
1949 | dyn.d_un.d_ptr = s->vma; | |
1950 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1951 | break; | |
1952 | ||
1953 | case DT_PLTRELSZ: | |
1954 | s = bfd_get_section_by_name (output_bfd, ".rel.plt"); | |
1955 | BFD_ASSERT (s != NULL); | |
1956 | if (s->_cooked_size != 0) | |
1957 | dyn.d_un.d_val = s->_cooked_size; | |
1958 | else | |
1959 | dyn.d_un.d_val = s->_raw_size; | |
1960 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1961 | break; | |
1962 | ||
1963 | case DT_RELSZ: | |
1964 | /* My reading of the SVR4 ABI indicates that the | |
1965 | procedure linkage table relocs (DT_JMPREL) should be | |
1966 | included in the overall relocs (DT_REL). This is | |
1967 | what Solaris does. However, UnixWare can not handle | |
1968 | that case. Therefore, we override the DT_RELSZ entry | |
1969 | here to make it not include the JMPREL relocs. Since | |
1970 | the linker script arranges for .rel.plt to follow all | |
1971 | other relocation sections, we don't have to worry | |
1972 | about changing the DT_REL entry. */ | |
1973 | s = bfd_get_section_by_name (output_bfd, ".rel.plt"); | |
1974 | if (s != NULL) | |
1975 | { | |
1976 | if (s->_cooked_size != 0) | |
1977 | dyn.d_un.d_val -= s->_cooked_size; | |
1978 | else | |
1979 | dyn.d_un.d_val -= s->_raw_size; | |
1980 | } | |
1981 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1982 | break; | |
1983 | } | |
1984 | } | |
1985 | ||
1986 | /* Fill in the first entry in the procedure linkage table. */ | |
1987 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1988 | if (splt && splt->_raw_size > 0) | |
1989 | { | |
1990 | if (info->shared) | |
1991 | memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE); | |
1992 | else | |
1993 | { | |
1994 | memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE); | |
1995 | bfd_put_32 (output_bfd, | |
1996 | sgot->output_section->vma + sgot->output_offset + 4, | |
1997 | splt->contents + 2); | |
1998 | bfd_put_32 (output_bfd, | |
1999 | sgot->output_section->vma + sgot->output_offset + 8, | |
2000 | splt->contents + 8); | |
2001 | } | |
2002 | ||
2003 | /* UnixWare sets the entsize of .plt to 4, although that doesn't | |
2004 | really seem like the right value. */ | |
2005 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4; | |
2006 | } | |
2007 | } | |
2008 | ||
2009 | /* Fill in the first three entries in the global offset table. */ | |
2010 | if (sgot->_raw_size > 0) | |
2011 | { | |
2012 | if (sdyn == NULL) | |
2013 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents); | |
2014 | else | |
2015 | bfd_put_32 (output_bfd, | |
2016 | sdyn->output_section->vma + sdyn->output_offset, | |
2017 | sgot->contents); | |
2018 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4); | |
2019 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8); | |
2020 | } | |
2021 | ||
2022 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; | |
2023 | ||
2024 | return true; | |
2025 | } | |
2026 | ||
2027 | #define TARGET_LITTLE_SYM bfd_elf32_i386_vec | |
2028 | #define TARGET_LITTLE_NAME "elf32-i386" | |
2029 | #define ELF_ARCH bfd_arch_i386 | |
2030 | #define ELF_MACHINE_CODE EM_386 | |
2031 | #define ELF_MAXPAGESIZE 0x1000 | |
252b5132 RH |
2032 | |
2033 | #define elf_backend_can_gc_sections 1 | |
2034 | #define elf_backend_want_got_plt 1 | |
2035 | #define elf_backend_plt_readonly 1 | |
2036 | #define elf_backend_want_plt_sym 0 | |
2037 | #define elf_backend_got_header_size 12 | |
2038 | #define elf_backend_plt_header_size PLT_ENTRY_SIZE | |
2039 | ||
dd5724d5 AM |
2040 | #define elf_info_to_howto elf_i386_info_to_howto |
2041 | #define elf_info_to_howto_rel elf_i386_info_to_howto_rel | |
2042 | ||
2043 | #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link | |
2044 | #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name | |
2045 | #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create | |
2046 | #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup | |
2047 | ||
2048 | #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol | |
2049 | #define elf_backend_check_relocs elf_i386_check_relocs | |
2050 | #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections | |
2051 | #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections | |
2052 | #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol | |
2053 | #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook | |
2054 | #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook | |
2055 | #define elf_backend_relocate_section elf_i386_relocate_section | |
2056 | #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections | |
2057 | ||
252b5132 | 2058 | #include "elf32-target.h" |