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