1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file contains the COFF backend linker code. */
27 #include "coff/internal.h"
30 static boolean coff_link_add_object_symbols
31 PARAMS ((bfd
*, struct bfd_link_info
*));
32 static boolean coff_link_check_archive_element
33 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
34 static boolean coff_link_check_ar_symbols
35 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
36 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
37 static char *dores_com
PARAMS ((char *, bfd
*, int));
38 static char *get_name
PARAMS ((char *, char **));
39 static int process_embedded_commands
40 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*));
41 static void mark_relocs
PARAMS ((struct coff_final_link_info
*, bfd
*));
43 /* Create an entry in a COFF linker hash table. */
45 struct bfd_hash_entry
*
46 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
47 struct bfd_hash_entry
*entry
;
48 struct bfd_hash_table
*table
;
51 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
53 /* Allocate the structure if it has not already been allocated by a
55 if (ret
== (struct coff_link_hash_entry
*) NULL
)
56 ret
= ((struct coff_link_hash_entry
*)
57 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
58 if (ret
== (struct coff_link_hash_entry
*) NULL
)
59 return (struct bfd_hash_entry
*) ret
;
61 /* Call the allocation method of the superclass. */
62 ret
= ((struct coff_link_hash_entry
*)
63 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
65 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
67 /* Set local fields. */
76 return (struct bfd_hash_entry
*) ret
;
79 /* Initialize a COFF linker hash table. */
82 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
83 struct coff_link_hash_table
*table
;
85 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
86 struct bfd_hash_table
*,
89 table
->stab_info
= NULL
;
90 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
93 /* Create a COFF linker hash table. */
95 struct bfd_link_hash_table
*
96 _bfd_coff_link_hash_table_create (abfd
)
99 struct coff_link_hash_table
*ret
;
101 ret
= ((struct coff_link_hash_table
*)
102 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
105 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
106 _bfd_coff_link_hash_newfunc
))
108 bfd_release (abfd
, ret
);
109 return (struct bfd_link_hash_table
*) NULL
;
114 /* Create an entry in a COFF debug merge hash table. */
116 struct bfd_hash_entry
*
117 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
118 struct bfd_hash_entry
*entry
;
119 struct bfd_hash_table
*table
;
122 struct coff_debug_merge_hash_entry
*ret
=
123 (struct coff_debug_merge_hash_entry
*) entry
;
125 /* Allocate the structure if it has not already been allocated by a
127 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
128 ret
= ((struct coff_debug_merge_hash_entry
*)
129 bfd_hash_allocate (table
,
130 sizeof (struct coff_debug_merge_hash_entry
)));
131 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
132 return (struct bfd_hash_entry
*) ret
;
134 /* Call the allocation method of the superclass. */
135 ret
= ((struct coff_debug_merge_hash_entry
*)
136 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
137 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
139 /* Set local fields. */
143 return (struct bfd_hash_entry
*) ret
;
146 /* Given a COFF BFD, add symbols to the global hash table as
150 _bfd_coff_link_add_symbols (abfd
, info
)
152 struct bfd_link_info
*info
;
154 switch (bfd_get_format (abfd
))
157 return coff_link_add_object_symbols (abfd
, info
);
159 return (_bfd_generic_link_add_archive_symbols
160 (abfd
, info
, coff_link_check_archive_element
));
162 bfd_set_error (bfd_error_wrong_format
);
167 /* Add symbols from a COFF object file. */
170 coff_link_add_object_symbols (abfd
, info
)
172 struct bfd_link_info
*info
;
174 if (! _bfd_coff_get_external_symbols (abfd
))
176 if (! coff_link_add_symbols (abfd
, info
))
179 if (! info
->keep_memory
)
181 if (! _bfd_coff_free_symbols (abfd
))
187 /* Check a single archive element to see if we need to include it in
188 the link. *PNEEDED is set according to whether this element is
189 needed in the link or not. This is called via
190 _bfd_generic_link_add_archive_symbols. */
193 coff_link_check_archive_element (abfd
, info
, pneeded
)
195 struct bfd_link_info
*info
;
198 if (! _bfd_coff_get_external_symbols (abfd
))
201 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
206 if (! coff_link_add_symbols (abfd
, info
))
210 if (! info
->keep_memory
|| ! *pneeded
)
212 if (! _bfd_coff_free_symbols (abfd
))
219 /* Look through the symbols to see if this object file should be
220 included in the link. */
223 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
225 struct bfd_link_info
*info
;
228 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
229 bfd_size_type symesz
;
235 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
237 symesz
= bfd_coff_symesz (abfd
);
238 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
239 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
240 while (esym
< esym_end
)
242 struct internal_syment sym
;
244 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
246 if ((sym
.n_sclass
== C_EXT
247 || sym
.n_sclass
== C_WEAKEXT
248 || (obj_pe (abfd
) && sym
.n_sclass
== C_NT_WEAK
)
250 || sym
.n_sclass
== C_SYSTEM
252 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
253 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
256 char buf
[SYMNMLEN
+ 1];
257 struct bfd_link_hash_entry
*h
;
259 /* This symbol is externally visible, and is defined by this
262 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
265 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
267 /* We are only interested in symbols that are currently
268 undefined. If a symbol is currently known to be common,
269 COFF linkers do not bring in an object file which defines
271 if (h
!= (struct bfd_link_hash_entry
*) NULL
272 && h
->type
== bfd_link_hash_undefined
)
274 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
281 esym
+= (sym
.n_numaux
+ 1) * symesz
;
284 /* We do not need this object file. */
288 /* Add all the symbols from an object file to the hash table. */
291 coff_link_add_symbols (abfd
, info
)
293 struct bfd_link_info
*info
;
295 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
297 boolean default_copy
;
298 bfd_size_type symcount
;
299 struct coff_link_hash_entry
**sym_hash
;
300 bfd_size_type symesz
;
304 /* Keep the symbols during this function, in case the linker needs
305 to read the generic symbols in order to report an error message. */
306 keep_syms
= obj_coff_keep_syms (abfd
);
307 obj_coff_keep_syms (abfd
) = true;
309 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
311 if (info
->keep_memory
)
312 default_copy
= false;
316 symcount
= obj_raw_syment_count (abfd
);
318 /* We keep a list of the linker hash table entries that correspond
319 to particular symbols. */
320 sym_hash
= ((struct coff_link_hash_entry
**)
323 * sizeof (struct coff_link_hash_entry
*))));
324 if (sym_hash
== NULL
&& symcount
!= 0)
326 obj_coff_sym_hashes (abfd
) = sym_hash
;
328 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
330 symesz
= bfd_coff_symesz (abfd
);
331 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
332 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
333 esym_end
= esym
+ symcount
* symesz
;
334 while (esym
< esym_end
)
336 struct internal_syment sym
;
339 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
341 if (sym
.n_sclass
== C_EXT
342 || sym
.n_sclass
== C_WEAKEXT
343 || (obj_pe (abfd
) && sym
.n_sclass
== C_NT_WEAK
)
345 || sym
.n_sclass
== C_SYSTEM
347 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
350 char buf
[SYMNMLEN
+ 1];
355 /* This symbol is externally visible. */
357 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
361 /* We must copy the name into memory if we got it from the
362 syment itself, rather than the string table. */
364 if (sym
._n
._n_n
._n_zeroes
!= 0
365 || sym
._n
._n_n
._n_offset
== 0)
370 if (sym
.n_scnum
== 0)
375 section
= bfd_und_section_ptr
;
380 section
= bfd_com_section_ptr
;
385 flags
= BSF_EXPORT
| BSF_GLOBAL
;
386 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
388 value
-= section
->vma
;
391 if (sym
.n_sclass
== C_WEAKEXT
392 || (obj_pe (abfd
) && sym
.n_sclass
== C_NT_WEAK
))
395 if (! (bfd_coff_link_add_one_symbol
396 (info
, abfd
, name
, flags
, section
, value
,
397 (const char *) NULL
, copy
, false,
398 (struct bfd_link_hash_entry
**) sym_hash
)))
401 if (section
== bfd_com_section_ptr
402 && (*sym_hash
)->root
.type
== bfd_link_hash_common
403 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
404 > bfd_coff_default_section_alignment_power (abfd
)))
405 (*sym_hash
)->root
.u
.c
.p
->alignment_power
406 = bfd_coff_default_section_alignment_power (abfd
);
408 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
410 if (((*sym_hash
)->class == C_NULL
411 && (*sym_hash
)->type
== T_NULL
)
414 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
415 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
417 (*sym_hash
)->class = sym
.n_sclass
;
418 if (sym
.n_type
!= T_NULL
)
420 if ((*sym_hash
)->type
!= T_NULL
421 && (*sym_hash
)->type
!= sym
.n_type
)
422 (*_bfd_error_handler
)
423 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
424 name
, (*sym_hash
)->type
, sym
.n_type
,
425 bfd_get_filename (abfd
));
426 (*sym_hash
)->type
= sym
.n_type
;
428 (*sym_hash
)->auxbfd
= abfd
;
429 if (sym
.n_numaux
!= 0)
431 union internal_auxent
*alloc
;
434 union internal_auxent
*iaux
;
436 (*sym_hash
)->numaux
= sym
.n_numaux
;
437 alloc
= ((union internal_auxent
*)
438 bfd_hash_allocate (&info
->hash
->table
,
440 * sizeof (*alloc
))));
443 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
445 i
++, eaux
+= symesz
, iaux
++)
446 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
447 sym
.n_sclass
, i
, sym
.n_numaux
,
449 (*sym_hash
)->aux
= alloc
;
455 esym
+= (sym
.n_numaux
+ 1) * symesz
;
456 sym_hash
+= sym
.n_numaux
+ 1;
459 /* If this is a non-traditional, non-relocateable link, try to
460 optimize the handling of any .stab/.stabstr sections. */
461 if (! info
->relocateable
462 && ! info
->traditional_format
463 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
464 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
466 asection
*stab
, *stabstr
;
468 stab
= bfd_get_section_by_name (abfd
, ".stab");
471 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
475 struct coff_link_hash_table
*table
;
476 struct coff_section_tdata
*secdata
;
478 secdata
= coff_section_data (abfd
, stab
);
482 (PTR
) bfd_zalloc (abfd
,
483 sizeof (struct coff_section_tdata
));
484 if (stab
->used_by_bfd
== NULL
)
486 secdata
= coff_section_data (abfd
, stab
);
489 table
= coff_hash_table (info
);
491 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
493 &secdata
->stab_info
))
499 obj_coff_keep_syms (abfd
) = keep_syms
;
504 obj_coff_keep_syms (abfd
) = keep_syms
;
508 /* Do the final link step. */
511 _bfd_coff_final_link (abfd
, info
)
513 struct bfd_link_info
*info
;
515 bfd_size_type symesz
;
516 struct coff_final_link_info finfo
;
517 boolean debug_merge_allocated
;
518 boolean long_section_names
;
520 struct bfd_link_order
*p
;
521 size_t max_sym_count
;
522 size_t max_lineno_count
;
523 size_t max_reloc_count
;
524 size_t max_output_reloc_count
;
525 size_t max_contents_size
;
526 file_ptr rel_filepos
;
528 file_ptr line_filepos
;
531 bfd_byte
*external_relocs
= NULL
;
532 char strbuf
[STRING_SIZE_SIZE
];
534 symesz
= bfd_coff_symesz (abfd
);
537 finfo
.output_bfd
= abfd
;
539 finfo
.section_info
= NULL
;
540 finfo
.last_file_index
= -1;
541 finfo
.last_bf_index
= -1;
542 finfo
.internal_syms
= NULL
;
543 finfo
.sec_ptrs
= NULL
;
544 finfo
.sym_indices
= NULL
;
545 finfo
.outsyms
= NULL
;
546 finfo
.linenos
= NULL
;
547 finfo
.contents
= NULL
;
548 finfo
.external_relocs
= NULL
;
549 finfo
.internal_relocs
= NULL
;
550 finfo
.global_to_static
= false;
551 debug_merge_allocated
= false;
553 coff_data (abfd
)->link_info
= info
;
555 finfo
.strtab
= _bfd_stringtab_init ();
556 if (finfo
.strtab
== NULL
)
559 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
561 debug_merge_allocated
= true;
563 /* Compute the file positions for all the sections. */
564 if (! abfd
->output_has_begun
)
566 if (! bfd_coff_compute_section_file_positions (abfd
))
570 /* Count the line numbers and relocation entries required for the
571 output file. Set the file positions for the relocs. */
572 rel_filepos
= obj_relocbase (abfd
);
573 relsz
= bfd_coff_relsz (abfd
);
574 max_contents_size
= 0;
575 max_lineno_count
= 0;
578 long_section_names
= false;
579 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
583 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
585 if (p
->type
== bfd_indirect_link_order
)
589 sec
= p
->u
.indirect
.section
;
591 /* Mark all sections which are to be included in the
592 link. This will normally be every section. We need
593 to do this so that we can identify any sections which
594 the linker has decided to not include. */
595 sec
->linker_mark
= true;
597 if (info
->strip
== strip_none
598 || info
->strip
== strip_some
)
599 o
->lineno_count
+= sec
->lineno_count
;
601 if (info
->relocateable
)
602 o
->reloc_count
+= sec
->reloc_count
;
604 if (sec
->_raw_size
> max_contents_size
)
605 max_contents_size
= sec
->_raw_size
;
606 if (sec
->lineno_count
> max_lineno_count
)
607 max_lineno_count
= sec
->lineno_count
;
608 if (sec
->reloc_count
> max_reloc_count
)
609 max_reloc_count
= sec
->reloc_count
;
611 else if (info
->relocateable
612 && (p
->type
== bfd_section_reloc_link_order
613 || p
->type
== bfd_symbol_reloc_link_order
))
616 if (o
->reloc_count
== 0)
620 o
->flags
|= SEC_RELOC
;
621 o
->rel_filepos
= rel_filepos
;
622 rel_filepos
+= o
->reloc_count
* relsz
;
625 if (bfd_coff_long_section_names (abfd
)
626 && strlen (o
->name
) > SCNNMLEN
)
628 /* This section has a long name which must go in the string
629 table. This must correspond to the code in
630 coff_write_object_contents which puts the string index
631 into the s_name field of the section header. That is why
632 we pass hash as false. */
633 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
634 == (bfd_size_type
) -1)
636 long_section_names
= true;
640 /* If doing a relocateable link, allocate space for the pointers we
642 if (info
->relocateable
)
646 /* We use section_count + 1, rather than section_count, because
647 the target_index fields are 1 based. */
649 ((struct coff_link_section_info
*)
650 bfd_malloc ((abfd
->section_count
+ 1)
651 * sizeof (struct coff_link_section_info
)));
652 if (finfo
.section_info
== NULL
)
654 for (i
= 0; i
<= abfd
->section_count
; i
++)
656 finfo
.section_info
[i
].relocs
= NULL
;
657 finfo
.section_info
[i
].rel_hashes
= NULL
;
661 /* We now know the size of the relocs, so we can determine the file
662 positions of the line numbers. */
663 line_filepos
= rel_filepos
;
664 linesz
= bfd_coff_linesz (abfd
);
665 max_output_reloc_count
= 0;
666 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
668 if (o
->lineno_count
== 0)
672 o
->line_filepos
= line_filepos
;
673 line_filepos
+= o
->lineno_count
* linesz
;
676 if (o
->reloc_count
!= 0)
678 /* We don't know the indices of global symbols until we have
679 written out all the local symbols. For each section in
680 the output file, we keep an array of pointers to hash
681 table entries. Each entry in the array corresponds to a
682 reloc. When we find a reloc against a global symbol, we
683 set the corresponding entry in this array so that we can
684 fix up the symbol index after we have written out all the
687 Because of this problem, we also keep the relocs in
688 memory until the end of the link. This wastes memory,
689 but only when doing a relocateable link, which is not the
691 BFD_ASSERT (info
->relocateable
);
692 finfo
.section_info
[o
->target_index
].relocs
=
693 ((struct internal_reloc
*)
694 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
695 finfo
.section_info
[o
->target_index
].rel_hashes
=
696 ((struct coff_link_hash_entry
**)
697 bfd_malloc (o
->reloc_count
698 * sizeof (struct coff_link_hash_entry
*)));
699 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
700 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
703 if (o
->reloc_count
> max_output_reloc_count
)
704 max_output_reloc_count
= o
->reloc_count
;
707 /* Reset the reloc and lineno counts, so that we can use them to
708 count the number of entries we have output so far. */
713 obj_sym_filepos (abfd
) = line_filepos
;
715 /* Figure out the largest number of symbols in an input BFD. Take
716 the opportunity to clear the output_has_begun fields of all the
719 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
723 sub
->output_has_begun
= false;
724 sz
= obj_raw_syment_count (sub
);
725 if (sz
> max_sym_count
)
729 /* Allocate some buffers used while linking. */
730 finfo
.internal_syms
= ((struct internal_syment
*)
731 bfd_malloc (max_sym_count
732 * sizeof (struct internal_syment
)));
733 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
734 * sizeof (asection
*));
735 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
736 finfo
.outsyms
= ((bfd_byte
*)
737 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
738 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
739 * bfd_coff_linesz (abfd
));
740 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
741 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
742 if (! info
->relocateable
)
743 finfo
.internal_relocs
= ((struct internal_reloc
*)
744 bfd_malloc (max_reloc_count
745 * sizeof (struct internal_reloc
)));
746 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
747 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
748 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
749 || finfo
.outsyms
== NULL
750 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
751 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
752 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
753 || (! info
->relocateable
754 && finfo
.internal_relocs
== NULL
755 && max_reloc_count
> 0))
758 /* We now know the position of everything in the file, except that
759 we don't know the size of the symbol table and therefore we don't
760 know where the string table starts. We just build the string
761 table in memory as we go along. We process all the relocations
762 for a single input file at once. */
763 obj_raw_syment_count (abfd
) = 0;
765 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
767 if (! bfd_coff_start_final_link (abfd
, info
))
771 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
773 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
775 if (p
->type
== bfd_indirect_link_order
776 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
777 == bfd_target_coff_flavour
))
779 sub
= p
->u
.indirect
.section
->owner
;
780 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
782 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
784 sub
->output_has_begun
= true;
787 else if (p
->type
== bfd_section_reloc_link_order
788 || p
->type
== bfd_symbol_reloc_link_order
)
790 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
795 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
801 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
804 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
806 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
807 debug_merge_allocated
= false;
809 if (finfo
.internal_syms
!= NULL
)
811 free (finfo
.internal_syms
);
812 finfo
.internal_syms
= NULL
;
814 if (finfo
.sec_ptrs
!= NULL
)
816 free (finfo
.sec_ptrs
);
817 finfo
.sec_ptrs
= NULL
;
819 if (finfo
.sym_indices
!= NULL
)
821 free (finfo
.sym_indices
);
822 finfo
.sym_indices
= NULL
;
824 if (finfo
.linenos
!= NULL
)
826 free (finfo
.linenos
);
827 finfo
.linenos
= NULL
;
829 if (finfo
.contents
!= NULL
)
831 free (finfo
.contents
);
832 finfo
.contents
= NULL
;
834 if (finfo
.external_relocs
!= NULL
)
836 free (finfo
.external_relocs
);
837 finfo
.external_relocs
= NULL
;
839 if (finfo
.internal_relocs
!= NULL
)
841 free (finfo
.internal_relocs
);
842 finfo
.internal_relocs
= NULL
;
845 /* The value of the last C_FILE symbol is supposed to be the symbol
846 index of the first external symbol. Write it out again if
848 if (finfo
.last_file_index
!= -1
849 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
851 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
852 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
853 (PTR
) finfo
.outsyms
);
855 (obj_sym_filepos (abfd
)
856 + finfo
.last_file_index
* symesz
),
858 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
862 /* If doing task linking (ld --task-link) then make a pass through the
863 global symbols, writing out any that are defined, and making them
867 finfo
.failed
= false;
868 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
874 /* Write out the global symbols. */
875 finfo
.failed
= false;
876 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
881 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
882 if (finfo
.outsyms
!= NULL
)
884 free (finfo
.outsyms
);
885 finfo
.outsyms
= NULL
;
888 if (info
->relocateable
&& max_output_reloc_count
> 0)
890 /* Now that we have written out all the global symbols, we know
891 the symbol indices to use for relocs against them, and we can
892 finally write out the relocs. */
893 external_relocs
= ((bfd_byte
*)
894 bfd_malloc (max_output_reloc_count
* relsz
));
895 if (external_relocs
== NULL
)
898 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
900 struct internal_reloc
*irel
;
901 struct internal_reloc
*irelend
;
902 struct coff_link_hash_entry
**rel_hash
;
905 if (o
->reloc_count
== 0)
908 irel
= finfo
.section_info
[o
->target_index
].relocs
;
909 irelend
= irel
+ o
->reloc_count
;
910 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
911 erel
= external_relocs
;
912 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
914 if (*rel_hash
!= NULL
)
916 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
917 irel
->r_symndx
= (*rel_hash
)->indx
;
919 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
922 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
923 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
924 abfd
) != relsz
* o
->reloc_count
)
928 free (external_relocs
);
929 external_relocs
= NULL
;
932 /* Free up the section information. */
933 if (finfo
.section_info
!= NULL
)
937 for (i
= 0; i
< abfd
->section_count
; i
++)
939 if (finfo
.section_info
[i
].relocs
!= NULL
)
940 free (finfo
.section_info
[i
].relocs
);
941 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
942 free (finfo
.section_info
[i
].rel_hashes
);
944 free (finfo
.section_info
);
945 finfo
.section_info
= NULL
;
948 /* If we have optimized stabs strings, output them. */
949 if (coff_hash_table (info
)->stab_info
!= NULL
)
951 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
955 /* Write out the string table. */
956 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
959 (obj_sym_filepos (abfd
)
960 + obj_raw_syment_count (abfd
) * symesz
),
964 #if STRING_SIZE_SIZE == 4
966 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
967 (bfd_byte
*) strbuf
);
969 #error Change bfd_h_put_32
972 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
975 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
979 _bfd_stringtab_free (finfo
.strtab
);
981 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
982 not try to write out the symbols. */
983 bfd_get_symcount (abfd
) = 0;
988 if (debug_merge_allocated
)
989 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
990 if (finfo
.strtab
!= NULL
)
991 _bfd_stringtab_free (finfo
.strtab
);
992 if (finfo
.section_info
!= NULL
)
996 for (i
= 0; i
< abfd
->section_count
; i
++)
998 if (finfo
.section_info
[i
].relocs
!= NULL
)
999 free (finfo
.section_info
[i
].relocs
);
1000 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1001 free (finfo
.section_info
[i
].rel_hashes
);
1003 free (finfo
.section_info
);
1005 if (finfo
.internal_syms
!= NULL
)
1006 free (finfo
.internal_syms
);
1007 if (finfo
.sec_ptrs
!= NULL
)
1008 free (finfo
.sec_ptrs
);
1009 if (finfo
.sym_indices
!= NULL
)
1010 free (finfo
.sym_indices
);
1011 if (finfo
.outsyms
!= NULL
)
1012 free (finfo
.outsyms
);
1013 if (finfo
.linenos
!= NULL
)
1014 free (finfo
.linenos
);
1015 if (finfo
.contents
!= NULL
)
1016 free (finfo
.contents
);
1017 if (finfo
.external_relocs
!= NULL
)
1018 free (finfo
.external_relocs
);
1019 if (finfo
.internal_relocs
!= NULL
)
1020 free (finfo
.internal_relocs
);
1021 if (external_relocs
!= NULL
)
1022 free (external_relocs
);
1026 /* parse out a -heap <reserved>,<commit> line */
1029 dores_com (ptr
, output_bfd
, heap
)
1034 if (coff_data(output_bfd
)->pe
)
1036 int val
= strtoul (ptr
, &ptr
, 0);
1038 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1040 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1044 int val
= strtoul (ptr
+1, &ptr
, 0);
1046 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1048 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1054 static char *get_name(ptr
, dst
)
1061 while (*ptr
&& *ptr
!= ' ')
1067 /* Process any magic embedded commands in a section called .drectve */
1070 process_embedded_commands (output_bfd
, info
, abfd
)
1072 struct bfd_link_info
*info
;
1075 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1082 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1085 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1090 e
= copy
+ sec
->_raw_size
;
1091 for (s
= copy
; s
< e
; )
1097 if (strncmp (s
,"-attr", 5) == 0)
1109 s
= get_name(s
, &name
);
1110 s
= get_name(s
, &attribs
);
1130 asec
= bfd_get_section_by_name (abfd
, name
);
1133 asec
->flags
|= SEC_CODE
;
1135 asec
->flags
|= SEC_READONLY
;
1138 else if (strncmp (s
,"-heap", 5) == 0)
1140 s
= dores_com (s
+5, output_bfd
, 1);
1142 else if (strncmp (s
,"-stack", 6) == 0)
1144 s
= dores_com (s
+6, output_bfd
, 0);
1153 /* Place a marker against all symbols which are used by relocations.
1154 This marker can be picked up by the 'do we skip this symbol ?'
1155 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1160 mark_relocs (finfo
, input_bfd
)
1161 struct coff_final_link_info
* finfo
;
1166 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1169 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1171 struct internal_reloc
* internal_relocs
;
1172 struct internal_reloc
* irel
;
1173 struct internal_reloc
* irelend
;
1176 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1179 /* Read in the relocs. */
1180 internal_relocs
= _bfd_coff_read_internal_relocs
1181 (input_bfd
, a
, false,
1182 finfo
->external_relocs
,
1183 finfo
->info
->relocateable
,
1184 (finfo
->info
->relocateable
1185 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1186 : finfo
->internal_relocs
)
1189 if (internal_relocs
== NULL
)
1192 irel
= internal_relocs
;
1193 irelend
= irel
+ a
->reloc_count
;
1195 /* Place a mark in the sym_indices array (whose entries have
1196 been initialised to 0) for all of the symbols that are used
1197 in the relocation table. This will then be picked up in the
1200 for (; irel
< irelend
; irel
++)
1202 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1207 /* Link an input file into the linker output file. This function
1208 handles all the sections and relocations of the input file at once. */
1211 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1212 struct coff_final_link_info
*finfo
;
1215 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1216 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1217 asection
*, struct internal_reloc
*,
1220 const char *strings
;
1221 bfd_size_type syment_base
;
1222 unsigned int n_tmask
;
1223 unsigned int n_btshft
;
1225 bfd_size_type isymesz
;
1226 bfd_size_type osymesz
;
1227 bfd_size_type linesz
;
1230 struct internal_syment
*isymp
;
1233 unsigned long output_index
;
1235 struct coff_link_hash_entry
**sym_hash
;
1238 /* Move all the symbols to the output file. */
1240 output_bfd
= finfo
->output_bfd
;
1241 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1243 syment_base
= obj_raw_syment_count (output_bfd
);
1244 isymesz
= bfd_coff_symesz (input_bfd
);
1245 osymesz
= bfd_coff_symesz (output_bfd
);
1246 linesz
= bfd_coff_linesz (input_bfd
);
1247 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1249 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1250 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1252 /* Define macros so that ISFCN, et. al., macros work correctly. */
1253 #define N_TMASK n_tmask
1254 #define N_BTSHFT n_btshft
1257 if (! finfo
->info
->keep_memory
)
1260 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1263 if (! _bfd_coff_get_external_symbols (input_bfd
))
1266 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1267 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1268 isymp
= finfo
->internal_syms
;
1269 secpp
= finfo
->sec_ptrs
;
1270 indexp
= finfo
->sym_indices
;
1271 output_index
= syment_base
;
1272 outsym
= finfo
->outsyms
;
1274 if (coff_data (output_bfd
)->pe
)
1276 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1280 /* If we are going to perform relocations and also strip/discard some symbols
1281 then we must make sure that we do not strip/discard those symbols that are
1282 going to be involved in the relocations */
1283 if (( finfo
->info
->strip
!= strip_none
1284 || finfo
->info
->discard
!= discard_none
)
1285 && finfo
->info
->relocateable
)
1287 /* mark the symbol array as 'not-used' */
1288 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1290 mark_relocs (finfo
, input_bfd
);
1293 while (esym
< esym_end
)
1295 struct internal_syment isym
;
1298 boolean dont_skip_symbol
;
1301 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1303 /* Make a copy of *isymp so that the relocate_section function
1304 always sees the original values. This is more reliable than
1305 always recomputing the symbol value even if we are stripping
1309 if (isym
.n_scnum
!= 0)
1310 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1313 if (isym
.n_value
== 0)
1314 *secpp
= bfd_und_section_ptr
;
1316 *secpp
= bfd_com_section_ptr
;
1319 /* Extract the flag indicating if this symbol is used by a
1321 if ((finfo
->info
->strip
!= strip_none
1322 || finfo
->info
->discard
!= discard_none
)
1323 && finfo
->info
->relocateable
)
1324 dont_skip_symbol
= *indexp
;
1326 dont_skip_symbol
= false;
1332 add
= 1 + isym
.n_numaux
;
1334 /* If we are stripping all symbols, we want to skip this one. */
1335 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1340 if (isym
.n_sclass
== C_EXT
1341 || isym
.n_sclass
== C_WEAKEXT
1342 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)
1344 || isym
.n_sclass
== C_SYSTEM
1346 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1348 /* This is a global symbol. Global symbols come at the
1349 end of the symbol table, so skip them for now.
1350 Locally defined function symbols, however, are an
1351 exception, and are not moved to the end. */
1353 if (! ISFCN (isym
.n_type
) || isym
.n_scnum
== 0)
1358 /* This is a local symbol. Skip it if we are discarding
1360 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1365 /* If we stripping debugging symbols, and this is a debugging
1366 symbol, then skip it. FIXME: gas sets the section to N_ABS
1367 for some types of debugging symbols; I don't know if this is
1368 a bug or not. In any case, we handle it here. */
1370 && finfo
->info
->strip
== strip_debugger
1371 && ! dont_skip_symbol
1372 && (isym
.n_scnum
== N_DEBUG
1373 || (isym
.n_scnum
== N_ABS
1374 && (isym
.n_sclass
== C_AUTO
1375 || isym
.n_sclass
== C_REG
1376 || isym
.n_sclass
== C_MOS
1377 || isym
.n_sclass
== C_MOE
1378 || isym
.n_sclass
== C_MOU
1379 || isym
.n_sclass
== C_ARG
1380 || isym
.n_sclass
== C_REGPARM
1381 || isym
.n_sclass
== C_FIELD
1382 || isym
.n_sclass
== C_EOS
))))
1385 /* If some symbols are stripped based on the name, work out the
1386 name and decide whether to skip this symbol. */
1388 && (finfo
->info
->strip
== strip_some
1389 || finfo
->info
->discard
== discard_l
))
1392 char buf
[SYMNMLEN
+ 1];
1394 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1398 if (! dont_skip_symbol
1399 && ((finfo
->info
->strip
== strip_some
1400 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1403 && finfo
->info
->discard
== discard_l
1404 && bfd_is_local_label_name (input_bfd
, name
))))
1408 /* If this is an enum, struct, or union tag, see if we have
1409 already output an identical type. */
1411 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1412 && (isym
.n_sclass
== C_ENTAG
1413 || isym
.n_sclass
== C_STRTAG
1414 || isym
.n_sclass
== C_UNTAG
)
1415 && isym
.n_numaux
== 1)
1418 char buf
[SYMNMLEN
+ 1];
1419 struct coff_debug_merge_hash_entry
*mh
;
1420 struct coff_debug_merge_type
*mt
;
1421 union internal_auxent aux
;
1422 struct coff_debug_merge_element
**epp
;
1423 bfd_byte
*esl
, *eslend
;
1424 struct internal_syment
*islp
;
1426 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1430 /* Ignore fake names invented by compiler; treat them all as
1432 if (*name
== '~' || *name
== '.' || *name
== '$'
1433 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1434 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1437 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1442 /* Allocate memory to hold type information. If this turns
1443 out to be a duplicate, we pass this address to
1445 mt
= ((struct coff_debug_merge_type
*)
1446 bfd_alloc (input_bfd
,
1447 sizeof (struct coff_debug_merge_type
)));
1450 mt
->class = isym
.n_sclass
;
1452 /* Pick up the aux entry, which points to the end of the tag
1454 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1455 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1458 /* Gather the elements. */
1459 epp
= &mt
->elements
;
1460 mt
->elements
= NULL
;
1462 esl
= esym
+ 2 * isymesz
;
1463 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1464 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1465 while (esl
< eslend
)
1467 const char *elename
;
1468 char elebuf
[SYMNMLEN
+ 1];
1471 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1473 *epp
= ((struct coff_debug_merge_element
*)
1474 bfd_alloc (input_bfd
,
1475 sizeof (struct coff_debug_merge_element
)));
1479 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1481 if (elename
== NULL
)
1484 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1487 strcpy (copy
, elename
);
1489 (*epp
)->name
= copy
;
1490 (*epp
)->type
= islp
->n_type
;
1492 if (islp
->n_numaux
>= 1
1493 && islp
->n_type
!= T_NULL
1494 && islp
->n_sclass
!= C_EOS
)
1496 union internal_auxent eleaux
;
1499 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1500 islp
->n_type
, islp
->n_sclass
, 0,
1501 islp
->n_numaux
, (PTR
) &eleaux
);
1502 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1504 /* FIXME: If this tagndx entry refers to a symbol
1505 defined later in this file, we just ignore it.
1506 Handling this correctly would be tedious, and may
1512 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1515 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1516 if ((*epp
)->tagndx
< 0)
1520 epp
= &(*epp
)->next
;
1523 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1524 islp
+= islp
->n_numaux
+ 1;
1527 /* See if we already have a definition which matches this
1528 type. We always output the type if it has no elements,
1530 if (mt
->elements
== NULL
)
1531 bfd_release (input_bfd
, (PTR
) mt
);
1534 struct coff_debug_merge_type
*mtl
;
1536 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1538 struct coff_debug_merge_element
*me
, *mel
;
1540 if (mtl
->class != mt
->class)
1543 for (me
= mt
->elements
, mel
= mtl
->elements
;
1544 me
!= NULL
&& mel
!= NULL
;
1545 me
= me
->next
, mel
= mel
->next
)
1547 if (strcmp (me
->name
, mel
->name
) != 0
1548 || me
->type
!= mel
->type
1549 || me
->tagndx
!= mel
->tagndx
)
1553 if (me
== NULL
&& mel
== NULL
)
1557 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1559 /* This is the first definition of this type. */
1560 mt
->indx
= output_index
;
1561 mt
->next
= mh
->types
;
1566 /* This is a redefinition which can be merged. */
1567 bfd_release (input_bfd
, (PTR
) mt
);
1568 *indexp
= mtl
->indx
;
1569 add
= (eslend
- esym
) / isymesz
;
1575 /* We now know whether we are to skip this symbol or not. */
1578 /* Adjust the symbol in order to output it. */
1580 if (isym
._n
._n_n
._n_zeroes
== 0
1581 && isym
._n
._n_n
._n_offset
!= 0)
1586 /* This symbol has a long name. Enter it in the string
1587 table we are building. Note that we do not check
1588 bfd_coff_symname_in_debug. That is only true for
1589 XCOFF, and XCOFF requires different linking code
1591 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1595 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1596 if (indx
== (bfd_size_type
) -1)
1598 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1601 if (isym
.n_scnum
> 0)
1603 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1604 isym
.n_value
+= (*secpp
)->output_offset
;
1605 if (! obj_pe (input_bfd
))
1606 isym
.n_value
-= (*secpp
)->vma
;
1607 if (! obj_pe (finfo
->output_bfd
))
1608 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1611 /* The value of a C_FILE symbol is the symbol index of the
1612 next C_FILE symbol. The value of the last C_FILE symbol
1613 is the symbol index to the first external symbol
1614 (actually, coff_renumber_symbols does not get this
1615 right--it just sets the value of the last C_FILE symbol
1616 to zero--and nobody has ever complained about it). We
1617 try to get this right, below, just before we write the
1618 symbols out, but in the general case we may have to write
1619 the symbol out twice. */
1620 if (isym
.n_sclass
== C_FILE
)
1622 if (finfo
->last_file_index
!= -1
1623 && finfo
->last_file
.n_value
!= (long) output_index
)
1625 /* We must correct the value of the last C_FILE entry. */
1626 finfo
->last_file
.n_value
= output_index
;
1627 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1629 /* The last C_FILE symbol is in this input file. */
1630 bfd_coff_swap_sym_out (output_bfd
,
1631 (PTR
) &finfo
->last_file
,
1632 (PTR
) (finfo
->outsyms
1633 + ((finfo
->last_file_index
1639 /* We have already written out the last C_FILE
1640 symbol. We need to write it out again. We
1641 borrow *outsym temporarily. */
1642 bfd_coff_swap_sym_out (output_bfd
,
1643 (PTR
) &finfo
->last_file
,
1645 if (bfd_seek (output_bfd
,
1646 (obj_sym_filepos (output_bfd
)
1647 + finfo
->last_file_index
* osymesz
),
1649 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1655 finfo
->last_file_index
= output_index
;
1656 finfo
->last_file
= isym
;
1659 /* If doing task linking, convert normal global function symbols to
1660 static functions. */
1662 if (finfo
->info
->task_link
1663 && (isym
.n_sclass
== C_EXT
1664 || isym
.n_sclass
== C_WEAKEXT
1665 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)))
1666 isym
.n_sclass
= C_STAT
;
1668 /* Output the symbol. */
1670 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1672 *indexp
= output_index
;
1677 struct coff_link_hash_entry
*h
;
1679 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1681 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1684 /* This can happen if there were errors earlier in
1686 bfd_set_error (bfd_error_bad_value
);
1689 h
->indx
= output_index
;
1692 output_index
+= add
;
1693 outsym
+= add
* osymesz
;
1696 esym
+= add
* isymesz
;
1700 for (--add
; add
> 0; --add
)
1707 /* Fix up the aux entries. This must be done in a separate pass,
1708 because we don't know the correct symbol indices until we have
1709 already decided which symbols we are going to keep. */
1711 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1712 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1713 isymp
= finfo
->internal_syms
;
1714 indexp
= finfo
->sym_indices
;
1715 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1716 outsym
= finfo
->outsyms
;
1717 while (esym
< esym_end
)
1721 add
= 1 + isymp
->n_numaux
;
1724 || (bfd_size_type
) *indexp
< syment_base
)
1725 && (*sym_hash
== NULL
1726 || (*sym_hash
)->auxbfd
!= input_bfd
))
1727 esym
+= add
* isymesz
;
1730 struct coff_link_hash_entry
*h
;
1738 /* The m68k-motorola-sysv assembler will sometimes
1739 generate two symbols with the same name, but only one
1740 will have aux entries. */
1741 BFD_ASSERT (isymp
->n_numaux
== 0
1742 || h
->numaux
== isymp
->n_numaux
);
1750 /* Handle the aux entries. This handling is based on
1751 coff_pointerize_aux. I don't know if it always correct. */
1752 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1754 union internal_auxent aux
;
1755 union internal_auxent
*auxp
;
1761 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1762 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1767 if (isymp
->n_sclass
== C_FILE
)
1769 /* If this is a long filename, we must put it in the
1771 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1772 && auxp
->x_file
.x_n
.x_offset
!= 0)
1774 const char *filename
;
1777 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1778 >= STRING_SIZE_SIZE
);
1779 if (strings
== NULL
)
1781 strings
= _bfd_coff_read_string_table (input_bfd
);
1782 if (strings
== NULL
)
1785 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1786 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1788 if (indx
== (bfd_size_type
) -1)
1790 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1793 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1797 if (ISFCN (isymp
->n_type
)
1798 || ISTAG (isymp
->n_sclass
)
1799 || isymp
->n_sclass
== C_BLOCK
1800 || isymp
->n_sclass
== C_FCN
)
1802 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1804 && indx
< obj_raw_syment_count (input_bfd
))
1806 /* We look forward through the symbol for
1807 the index of the next symbol we are going
1808 to include. I don't know if this is
1810 while ((finfo
->sym_indices
[indx
] < 0
1811 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1813 && indx
< obj_raw_syment_count (input_bfd
))
1815 if (indx
>= obj_raw_syment_count (input_bfd
))
1816 indx
= output_index
;
1818 indx
= finfo
->sym_indices
[indx
];
1819 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1823 indx
= auxp
->x_sym
.x_tagndx
.l
;
1824 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1828 symindx
= finfo
->sym_indices
[indx
];
1830 auxp
->x_sym
.x_tagndx
.l
= 0;
1832 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1835 /* The .bf symbols are supposed to be linked through
1836 the endndx field. We need to carry this list
1837 across object files. */
1840 && isymp
->n_sclass
== C_FCN
1841 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1842 || isymp
->_n
._n_n
._n_offset
== 0)
1843 && isymp
->_n
._n_name
[0] == '.'
1844 && isymp
->_n
._n_name
[1] == 'b'
1845 && isymp
->_n
._n_name
[2] == 'f'
1846 && isymp
->_n
._n_name
[3] == '\0')
1848 if (finfo
->last_bf_index
!= -1)
1850 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1853 if ((bfd_size_type
) finfo
->last_bf_index
1858 /* The last .bf symbol is in this input
1859 file. This will only happen if the
1860 assembler did not set up the .bf
1861 endndx symbols correctly. */
1862 auxout
= (PTR
) (finfo
->outsyms
1863 + ((finfo
->last_bf_index
1866 bfd_coff_swap_aux_out (output_bfd
,
1867 (PTR
) &finfo
->last_bf
,
1875 /* We have already written out the last
1876 .bf aux entry. We need to write it
1877 out again. We borrow *outsym
1878 temporarily. FIXME: This case should
1880 bfd_coff_swap_aux_out (output_bfd
,
1881 (PTR
) &finfo
->last_bf
,
1886 if (bfd_seek (output_bfd
,
1887 (obj_sym_filepos (output_bfd
)
1888 + finfo
->last_bf_index
* osymesz
),
1890 || bfd_write (outsym
, osymesz
, 1,
1891 output_bfd
) != osymesz
)
1896 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1897 finfo
->last_bf_index
= -1;
1900 /* The endndx field of this aux entry must
1901 be updated with the symbol number of the
1903 finfo
->last_bf
= *auxp
;
1904 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1913 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1914 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1928 /* Relocate the line numbers, unless we are stripping them. */
1929 if (finfo
->info
->strip
== strip_none
1930 || finfo
->info
->strip
== strip_some
)
1932 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1938 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1939 build_link_order in ldwrite.c will not have created a
1940 link order, which means that we will not have seen this
1941 input section in _bfd_coff_final_link, which means that
1942 we will not have allocated space for the line numbers of
1943 this section. I don't think line numbers can be
1944 meaningful for a section which does not have
1945 SEC_HAS_CONTENTS set, but, if they do, this must be
1947 if (o
->lineno_count
== 0
1948 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1951 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1952 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1953 input_bfd
) != linesz
* o
->lineno_count
)
1956 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1957 eline
= finfo
->linenos
;
1958 elineend
= eline
+ linesz
* o
->lineno_count
;
1959 for (; eline
< elineend
; eline
+= linesz
)
1961 struct internal_lineno iline
;
1963 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1965 if (iline
.l_lnno
!= 0)
1966 iline
.l_addr
.l_paddr
+= offset
;
1967 else if (iline
.l_addr
.l_symndx
>= 0
1968 && ((unsigned long) iline
.l_addr
.l_symndx
1969 < obj_raw_syment_count (input_bfd
)))
1973 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1977 /* These line numbers are attached to a symbol
1978 which we are stripping. We should really
1979 just discard the line numbers, but that would
1980 be a pain because we have already counted
1986 struct internal_syment is
;
1987 union internal_auxent ia
;
1989 /* Fix up the lnnoptr field in the aux entry of
1990 the symbol. It turns out that we can't do
1991 this when we modify the symbol aux entries,
1992 because gas sometimes screws up the lnnoptr
1993 field and makes it an offset from the start
1994 of the line numbers rather than an absolute
1996 bfd_coff_swap_sym_in (output_bfd
,
1997 (PTR
) (finfo
->outsyms
1998 + ((indx
- syment_base
)
2001 if ((ISFCN (is
.n_type
)
2002 || is
.n_sclass
== C_BLOCK
)
2003 && is
.n_numaux
>= 1)
2007 auxptr
= (PTR
) (finfo
->outsyms
2008 + ((indx
- syment_base
+ 1)
2010 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2011 is
.n_type
, is
.n_sclass
,
2012 0, is
.n_numaux
, (PTR
) &ia
);
2013 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2014 (o
->output_section
->line_filepos
2015 + o
->output_section
->lineno_count
* linesz
2016 + eline
- finfo
->linenos
);
2017 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2018 is
.n_type
, is
.n_sclass
, 0,
2019 is
.n_numaux
, auxptr
);
2023 iline
.l_addr
.l_symndx
= indx
;
2026 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
2029 if (bfd_seek (output_bfd
,
2030 (o
->output_section
->line_filepos
2031 + o
->output_section
->lineno_count
* linesz
),
2033 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
2034 output_bfd
) != linesz
* o
->lineno_count
)
2037 o
->output_section
->lineno_count
+= o
->lineno_count
;
2041 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2042 symbol will be the first symbol in the next input file. In the
2043 normal case, this will save us from writing out the C_FILE symbol
2045 if (finfo
->last_file_index
!= -1
2046 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2048 finfo
->last_file
.n_value
= output_index
;
2049 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2050 (PTR
) (finfo
->outsyms
2051 + ((finfo
->last_file_index
- syment_base
)
2055 /* Write the modified symbols to the output file. */
2056 if (outsym
> finfo
->outsyms
)
2058 if (bfd_seek (output_bfd
,
2059 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2061 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2063 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2066 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2067 + (outsym
- finfo
->outsyms
) / osymesz
)
2070 obj_raw_syment_count (output_bfd
) = output_index
;
2073 /* Relocate the contents of each section. */
2074 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2075 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2078 struct coff_section_tdata
*secdata
;
2080 if (! o
->linker_mark
)
2082 /* This section was omitted from the link. */
2086 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2087 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2089 if ((o
->flags
& SEC_RELOC
) != 0
2090 && o
->reloc_count
!= 0)
2092 ((*_bfd_error_handler
)
2093 (_("%s: relocs in section `%s', but it has no contents"),
2094 bfd_get_filename (input_bfd
),
2095 bfd_get_section_name (input_bfd
, o
)));
2096 bfd_set_error (bfd_error_no_contents
);
2103 secdata
= coff_section_data (input_bfd
, o
);
2104 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2105 contents
= secdata
->contents
;
2108 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2109 (file_ptr
) 0, o
->_raw_size
))
2111 contents
= finfo
->contents
;
2114 if ((o
->flags
& SEC_RELOC
) != 0)
2117 struct internal_reloc
*internal_relocs
;
2118 struct internal_reloc
*irel
;
2120 /* Read in the relocs. */
2121 target_index
= o
->output_section
->target_index
;
2122 internal_relocs
= (_bfd_coff_read_internal_relocs
2123 (input_bfd
, o
, false, finfo
->external_relocs
,
2124 finfo
->info
->relocateable
,
2125 (finfo
->info
->relocateable
2126 ? (finfo
->section_info
[target_index
].relocs
2127 + o
->output_section
->reloc_count
)
2128 : finfo
->internal_relocs
)));
2129 if (internal_relocs
== NULL
)
2132 /* Call processor specific code to relocate the section
2134 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2138 finfo
->internal_syms
,
2142 if (finfo
->info
->relocateable
)
2145 struct internal_reloc
*irelend
;
2146 struct coff_link_hash_entry
**rel_hash
;
2148 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2149 irel
= internal_relocs
;
2150 irelend
= irel
+ o
->reloc_count
;
2151 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2152 + o
->output_section
->reloc_count
);
2153 for (; irel
< irelend
; irel
++, rel_hash
++)
2155 struct coff_link_hash_entry
*h
;
2160 /* Adjust the reloc address and symbol index. */
2162 irel
->r_vaddr
+= offset
;
2164 if (irel
->r_symndx
== -1)
2169 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2177 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2180 /* This is a global symbol. */
2182 irel
->r_symndx
= h
->indx
;
2185 /* This symbol is being written at the end
2186 of the file, and we do not yet know the
2187 symbol index. We save the pointer to the
2188 hash table entry in the rel_hash list.
2189 We set the indx field to -2 to indicate
2190 that this symbol must not be stripped. */
2199 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2201 irel
->r_symndx
= indx
;
2204 struct internal_syment
*is
;
2206 char buf
[SYMNMLEN
+ 1];
2208 /* This reloc is against a symbol we are
2209 stripping. This should have been handled
2210 by the 'dont_skip_symbol' code in the while
2211 loop at the top of this function. */
2213 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2215 name
= (_bfd_coff_internal_syment_name
2216 (input_bfd
, is
, buf
));
2220 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2221 (finfo
->info
, name
, input_bfd
, o
,
2228 o
->output_section
->reloc_count
+= o
->reloc_count
;
2232 /* Write out the modified section contents. */
2233 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2235 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2236 contents
, o
->output_offset
,
2237 (o
->_cooked_size
!= 0
2244 if (! (_bfd_write_section_stabs
2245 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2246 o
, &secdata
->stab_info
, contents
)))
2251 if (! finfo
->info
->keep_memory
)
2253 if (! _bfd_coff_free_symbols (input_bfd
))
2260 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2263 _bfd_coff_write_global_sym (h
, data
)
2264 struct coff_link_hash_entry
*h
;
2267 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2269 struct internal_syment isym
;
2270 bfd_size_type symesz
;
2273 output_bfd
= finfo
->output_bfd
;
2279 && (finfo
->info
->strip
== strip_all
2280 || (finfo
->info
->strip
== strip_some
2281 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2282 h
->root
.root
.string
, false, false)
2286 switch (h
->root
.type
)
2289 case bfd_link_hash_new
:
2293 case bfd_link_hash_undefined
:
2294 case bfd_link_hash_undefweak
:
2295 isym
.n_scnum
= N_UNDEF
;
2299 case bfd_link_hash_defined
:
2300 case bfd_link_hash_defweak
:
2304 sec
= h
->root
.u
.def
.section
->output_section
;
2305 if (bfd_is_abs_section (sec
))
2306 isym
.n_scnum
= N_ABS
;
2308 isym
.n_scnum
= sec
->target_index
;
2309 isym
.n_value
= (h
->root
.u
.def
.value
2310 + h
->root
.u
.def
.section
->output_offset
);
2311 if (! obj_pe (finfo
->output_bfd
))
2312 isym
.n_value
+= sec
->vma
;
2316 case bfd_link_hash_common
:
2317 isym
.n_scnum
= N_UNDEF
;
2318 isym
.n_value
= h
->root
.u
.c
.size
;
2321 case bfd_link_hash_indirect
:
2322 case bfd_link_hash_warning
:
2323 /* Just ignore these. They can't be handled anyhow. */
2327 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2328 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2335 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2337 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2339 if (indx
== (bfd_size_type
) -1)
2341 finfo
->failed
= true;
2344 isym
._n
._n_n
._n_zeroes
= 0;
2345 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2348 isym
.n_sclass
= h
->class;
2349 isym
.n_type
= h
->type
;
2351 if (isym
.n_sclass
== C_NULL
)
2352 isym
.n_sclass
= C_EXT
;
2354 /* If doing task linking and this is the pass where we convert
2355 defined globals to statics, then do that conversion now. If the
2356 symbol is not being converted, just ignore it and it will be
2357 output during a later pass. */
2358 if (finfo
->global_to_static
)
2360 if (isym
.n_sclass
!= C_EXT
2361 && isym
.n_sclass
!= C_WEAKEXT
2362 && (! obj_pe (output_bfd
) || isym
.n_sclass
!= C_NT_WEAK
))
2366 isym
.n_sclass
= C_STAT
;
2369 isym
.n_numaux
= h
->numaux
;
2371 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2373 symesz
= bfd_coff_symesz (output_bfd
);
2375 if (bfd_seek (output_bfd
,
2376 (obj_sym_filepos (output_bfd
)
2377 + obj_raw_syment_count (output_bfd
) * symesz
),
2379 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2381 finfo
->failed
= true;
2385 h
->indx
= obj_raw_syment_count (output_bfd
);
2387 ++obj_raw_syment_count (output_bfd
);
2389 /* Write out any associated aux entries. There normally will be
2390 none. If there are any, I have no idea how to modify them. */
2391 for (i
= 0; i
< isym
.n_numaux
; i
++)
2393 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2394 isym
.n_sclass
, i
, isym
.n_numaux
,
2395 (PTR
) finfo
->outsyms
);
2396 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2398 finfo
->failed
= true;
2401 ++obj_raw_syment_count (output_bfd
);
2407 /* Write out task global symbols, converting them to statics. Called
2408 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2409 the dirty work, if the symbol we are processing needs conversion. */
2412 _bfd_coff_write_task_globals (h
, data
)
2413 struct coff_link_hash_entry
*h
;
2416 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2417 boolean rtnval
= true;
2418 boolean save_global_to_static
;
2422 switch (h
->root
.type
)
2424 case bfd_link_hash_defined
:
2425 case bfd_link_hash_defweak
:
2426 save_global_to_static
= finfo
->global_to_static
;
2427 finfo
->global_to_static
= true;
2428 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2429 finfo
->global_to_static
= save_global_to_static
;
2438 /* Handle a link order which is supposed to generate a reloc. */
2441 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2443 struct coff_final_link_info
*finfo
;
2444 asection
*output_section
;
2445 struct bfd_link_order
*link_order
;
2447 reloc_howto_type
*howto
;
2448 struct internal_reloc
*irel
;
2449 struct coff_link_hash_entry
**rel_hash_ptr
;
2451 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2454 bfd_set_error (bfd_error_bad_value
);
2458 if (link_order
->u
.reloc
.p
->addend
!= 0)
2462 bfd_reloc_status_type rstat
;
2465 size
= bfd_get_reloc_size (howto
);
2466 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2470 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2471 link_order
->u
.reloc
.p
->addend
, buf
);
2477 case bfd_reloc_outofrange
:
2479 case bfd_reloc_overflow
:
2480 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2482 (link_order
->type
== bfd_section_reloc_link_order
2483 ? bfd_section_name (output_bfd
,
2484 link_order
->u
.reloc
.p
->u
.section
)
2485 : link_order
->u
.reloc
.p
->u
.name
),
2486 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2487 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2494 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2495 (file_ptr
) link_order
->offset
, size
);
2501 /* Store the reloc information in the right place. It will get
2502 swapped and written out at the end of the final_link routine. */
2504 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2505 + output_section
->reloc_count
);
2506 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2507 + output_section
->reloc_count
);
2509 memset (irel
, 0, sizeof (struct internal_reloc
));
2510 *rel_hash_ptr
= NULL
;
2512 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2514 if (link_order
->type
== bfd_section_reloc_link_order
)
2516 /* We need to somehow locate a symbol in the right section. The
2517 symbol must either have a value of zero, or we must adjust
2518 the addend by the value of the symbol. FIXME: Write this
2519 when we need it. The old linker couldn't handle this anyhow. */
2521 *rel_hash_ptr
= NULL
;
2526 struct coff_link_hash_entry
*h
;
2528 h
= ((struct coff_link_hash_entry
*)
2529 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2530 link_order
->u
.reloc
.p
->u
.name
,
2531 false, false, true));
2535 irel
->r_symndx
= h
->indx
;
2538 /* Set the index to -2 to force this symbol to get
2547 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2548 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2549 (asection
*) NULL
, (bfd_vma
) 0)))
2555 /* FIXME: Is this always right? */
2556 irel
->r_type
= howto
->type
;
2558 /* r_size is only used on the RS/6000, which needs its own linker
2559 routines anyhow. r_extern is only used for ECOFF. */
2561 /* FIXME: What is the right value for r_offset? Is zero OK? */
2563 ++output_section
->reloc_count
;
2568 /* A basic reloc handling routine which may be used by processors with
2572 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2573 input_section
, contents
, relocs
, syms
,
2576 struct bfd_link_info
*info
;
2578 asection
*input_section
;
2580 struct internal_reloc
*relocs
;
2581 struct internal_syment
*syms
;
2582 asection
**sections
;
2584 struct internal_reloc
*rel
;
2585 struct internal_reloc
*relend
;
2588 relend
= rel
+ input_section
->reloc_count
;
2589 for (; rel
< relend
; rel
++)
2592 struct coff_link_hash_entry
*h
;
2593 struct internal_syment
*sym
;
2596 reloc_howto_type
*howto
;
2597 bfd_reloc_status_type rstat
;
2599 symndx
= rel
->r_symndx
;
2608 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2609 sym
= syms
+ symndx
;
2612 /* COFF treats common symbols in one of two ways. Either the
2613 size of the symbol is included in the section contents, or it
2614 is not. We assume that the size is not included, and force
2615 the rtype_to_howto function to adjust the addend as needed. */
2617 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2618 addend
= - sym
->n_value
;
2623 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2628 /* If we are doing a relocateable link, then we can just ignore
2629 a PC relative reloc that is pcrel_offset. It will already
2630 have the correct value. If this is not a relocateable link,
2631 then we should ignore the symbol value. */
2632 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2634 if (info
->relocateable
)
2636 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2637 addend
+= sym
->n_value
;
2648 sec
= bfd_abs_section_ptr
;
2653 sec
= sections
[symndx
];
2654 val
= (sec
->output_section
->vma
2655 + sec
->output_offset
2657 if (! obj_pe (input_bfd
))
2663 if (h
->root
.type
== bfd_link_hash_defined
2664 || h
->root
.type
== bfd_link_hash_defweak
)
2668 sec
= h
->root
.u
.def
.section
;
2669 val
= (h
->root
.u
.def
.value
2670 + sec
->output_section
->vma
2671 + sec
->output_offset
);
2674 else if (! info
->relocateable
)
2676 if (! ((*info
->callbacks
->undefined_symbol
)
2677 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2678 rel
->r_vaddr
- input_section
->vma
)))
2683 if (info
->base_file
)
2685 /* Emit a reloc if the backend thinks it needs it. */
2686 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2688 /* Relocation to a symbol in a section which isn't
2689 absolute. We output the address here to a file.
2690 This file is then read by dlltool when generating the
2691 reloc section. Note that the base file is not
2692 portable between systems. We write out a long here,
2693 and dlltool reads in a long. */
2694 long addr
= (rel
->r_vaddr
2695 - input_section
->vma
2696 + input_section
->output_offset
2697 + input_section
->output_section
->vma
);
2698 if (coff_data (output_bfd
)->pe
)
2699 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2700 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2703 bfd_set_error (bfd_error_system_call
);
2709 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2711 rel
->r_vaddr
- input_section
->vma
,
2720 case bfd_reloc_outofrange
:
2721 (*_bfd_error_handler
)
2722 (_("%s: bad reloc address 0x%lx in section `%s'"),
2723 bfd_get_filename (input_bfd
),
2724 (unsigned long) rel
->r_vaddr
,
2725 bfd_get_section_name (input_bfd
, input_section
));
2727 case bfd_reloc_overflow
:
2730 char buf
[SYMNMLEN
+ 1];
2735 name
= h
->root
.root
.string
;
2738 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2743 if (! ((*info
->callbacks
->reloc_overflow
)
2744 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2745 input_section
, rel
->r_vaddr
- input_section
->vma
)))