1 /* COFF specific linker code.
2 Copyright 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file contains the COFF backend linker code. */
27 #include "coff/internal.h"
30 #define STRING_SIZE_SIZE (4)
32 /* Information we keep for each section in the output file when doing
33 a relocateable link. */
35 struct coff_link_section_info
37 /* The relocs to be output. */
38 struct internal_reloc
*relocs
;
39 /* For each reloc against a global symbol whose index was not known
40 when the reloc was handled, the global hash table entry. */
41 struct coff_link_hash_entry
**rel_hashes
;
44 /* Information that we pass around while doing the final link step. */
46 struct coff_final_link_info
48 /* General link information. */
49 struct bfd_link_info
*info
;
52 /* Used to indicate failure in traversal routine. */
54 /* Hash table for long symbol name. */
55 struct bfd_strtab_hash
*strtab
;
56 /* When doing a relocateable link, an array of information kept for
57 each output section, indexed by the target_index field. */
58 struct coff_link_section_info
*section_info
;
59 /* Symbol index of last C_FILE symbol (-1 if none). */
61 /* Contents of last C_FILE symbol. */
62 struct internal_syment last_file
;
63 /* Buffer large enough to hold swapped symbols of any input file. */
64 struct internal_syment
*internal_syms
;
65 /* Buffer large enough to hold sections of symbols of any input file. */
67 /* Buffer large enough to hold output indices of symbols of any
70 /* Buffer large enough to hold output symbols for any input file. */
72 /* Buffer large enough to hold external line numbers for any input
75 /* Buffer large enough to hold any input section. */
77 /* Buffer large enough to hold external relocs of any input section. */
78 bfd_byte
*external_relocs
;
79 /* Buffer large enough to hold swapped relocs of any input section. */
80 struct internal_reloc
*internal_relocs
;
83 static struct bfd_hash_entry
*coff_link_hash_newfunc
84 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
85 static boolean coff_link_add_object_symbols
86 PARAMS ((bfd
*, struct bfd_link_info
*));
87 static boolean coff_link_check_archive_element
88 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
89 static boolean coff_link_get_symbols
PARAMS ((bfd
*));
90 static const char *coff_read_string_table
PARAMS ((bfd
*));
91 static boolean coff_link_free_symbols
PARAMS ((bfd
*));
92 static boolean coff_link_check_ar_symbols
93 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
94 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
95 static boolean coff_link_input_bfd
96 PARAMS ((struct coff_final_link_info
*, bfd
*));
97 static boolean coff_write_global_sym
98 PARAMS ((struct coff_link_hash_entry
*, PTR
));
99 static boolean coff_reloc_link_order
100 PARAMS ((bfd
*, struct coff_final_link_info
*, asection
*,
101 struct bfd_link_order
*));
103 /* Create an entry in a COFF linker hash table. */
105 static struct bfd_hash_entry
*
106 coff_link_hash_newfunc (entry
, table
, string
)
107 struct bfd_hash_entry
*entry
;
108 struct bfd_hash_table
*table
;
111 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
113 /* Allocate the structure if it has not already been allocated by a
115 if (ret
== (struct coff_link_hash_entry
*) NULL
)
116 ret
= ((struct coff_link_hash_entry
*)
117 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
118 if (ret
== (struct coff_link_hash_entry
*) NULL
)
120 bfd_set_error (bfd_error_no_memory
);
121 return (struct bfd_hash_entry
*) ret
;
124 /* Call the allocation method of the superclass. */
125 ret
= ((struct coff_link_hash_entry
*)
126 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
128 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
130 /* Set local fields. */
139 return (struct bfd_hash_entry
*) ret
;
142 /* Create a COFF linker hash table. */
144 struct bfd_link_hash_table
*
145 _bfd_coff_link_hash_table_create (abfd
)
148 struct coff_link_hash_table
*ret
;
150 ret
= ((struct coff_link_hash_table
*)
151 malloc (sizeof (struct coff_link_hash_table
)));
154 bfd_set_error (bfd_error_no_memory
);
157 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
158 coff_link_hash_newfunc
))
161 return (struct bfd_link_hash_table
*) NULL
;
166 /* Given a COFF BFD, add symbols to the global hash table as
170 _bfd_coff_link_add_symbols (abfd
, info
)
172 struct bfd_link_info
*info
;
174 switch (bfd_get_format (abfd
))
177 return coff_link_add_object_symbols (abfd
, info
);
179 return (_bfd_generic_link_add_archive_symbols
180 (abfd
, info
, coff_link_check_archive_element
));
182 bfd_set_error (bfd_error_wrong_format
);
187 /* Add symbols from a COFF object file. */
190 coff_link_add_object_symbols (abfd
, info
)
192 struct bfd_link_info
*info
;
194 if (! coff_link_get_symbols (abfd
))
196 if (! coff_link_add_symbols (abfd
, info
))
198 if (! info
->keep_memory
)
200 if (! coff_link_free_symbols (abfd
))
206 /* Check a single archive element to see if we need to include it in
207 the link. *PNEEDED is set according to whether this element is
208 needed in the link or not. This is called via
209 _bfd_generic_link_add_archive_symbols. */
212 coff_link_check_archive_element (abfd
, info
, pneeded
)
214 struct bfd_link_info
*info
;
217 if (! coff_link_get_symbols (abfd
))
220 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
225 if (! coff_link_add_symbols (abfd
, info
))
229 if (! info
->keep_memory
|| ! *pneeded
)
231 if (! coff_link_free_symbols (abfd
))
238 /* Read in the external symbols. */
241 coff_link_get_symbols (abfd
)
244 bfd_size_type symesz
;
248 if (obj_coff_external_syms (abfd
) != NULL
)
251 symesz
= bfd_coff_symesz (abfd
);
253 size
= obj_raw_syment_count (abfd
) * symesz
;
255 syms
= malloc (size
);
256 if (syms
== NULL
&& size
!= 0)
258 bfd_set_error (bfd_error_no_memory
);
262 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
263 || bfd_read (syms
, size
, 1, abfd
) != size
)
270 obj_coff_external_syms (abfd
) = syms
;
275 /* Read in the external strings. The strings are not loaded until
276 they are needed. This is because we have no simple way of
277 detecting a missing string table in an archive. */
280 coff_read_string_table (abfd
)
283 char extstrsize
[STRING_SIZE_SIZE
];
287 if (obj_coff_strings (abfd
) != NULL
)
288 return obj_coff_strings (abfd
);
291 (obj_sym_filepos (abfd
)
292 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
296 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
298 if (bfd_get_error () != bfd_error_file_truncated
)
301 /* There is no string table. */
302 strsize
= STRING_SIZE_SIZE
;
306 #if STRING_SIZE_SIZE == 4
307 strsize
= bfd_h_get_32 (abfd
, extstrsize
);
309 #error Change bfd_h_get_32
313 strings
= malloc (strsize
);
316 bfd_set_error (bfd_error_no_memory
);
320 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
321 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
322 != strsize
- STRING_SIZE_SIZE
)
328 obj_coff_strings (abfd
) = strings
;
333 /* Free up the external symbols and strings read from a COFF file. */
336 coff_link_free_symbols (abfd
)
339 if (obj_coff_external_syms (abfd
) != NULL
)
341 free (obj_coff_external_syms (abfd
));
342 obj_coff_external_syms (abfd
) = NULL
;
344 if (obj_coff_strings (abfd
) != NULL
)
346 free (obj_coff_strings (abfd
));
347 obj_coff_strings (abfd
) = NULL
;
352 /* Look through the symbols to see if this object file should be
353 included in the link. */
356 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
358 struct bfd_link_info
*info
;
361 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
363 bfd_size_type symesz
;
369 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
372 symesz
= bfd_coff_symesz (abfd
);
373 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
374 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
375 while (esym
< esym_end
)
377 struct internal_syment sym
;
379 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
381 if ((sym
.n_sclass
== C_EXT
382 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
383 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
386 char buf
[SYMNMLEN
+ 1];
387 struct bfd_link_hash_entry
*h
;
389 /* This symbol is externally visible, and is defined by this
392 /* FIXME: It's not clear this will work correctly if sizeof
394 if (sym
._n
._n_n
._n_zeroes
!= 0
395 || sym
._n
._n_n
._n_offset
== 0)
397 memcpy (buf
, sym
._n
._n_name
, SYMNMLEN
);
398 buf
[SYMNMLEN
] = '\0';
403 BFD_ASSERT (sym
._n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
406 strings
= coff_read_string_table (abfd
);
410 name
= strings
+ sym
._n
._n_n
._n_offset
;
413 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
415 /* We are only interested in symbols that are currently
416 undefined. If a symbol is currently known to be common,
417 COFF linkers do not bring in an object file which defines
419 if (h
!= (struct bfd_link_hash_entry
*) NULL
420 && h
->type
== bfd_link_hash_undefined
)
422 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
429 esym
+= (sym
.n_numaux
+ 1) * symesz
;
432 /* We do not need this object file. */
436 /* Add all the symbols from an object file to the hash table. */
439 coff_link_add_symbols (abfd
, info
)
441 struct bfd_link_info
*info
;
443 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
445 boolean default_copy
;
446 bfd_size_type symcount
;
447 struct coff_link_hash_entry
**sym_hash
;
448 bfd_size_type symesz
;
452 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
455 if (info
->keep_memory
)
456 default_copy
= false;
460 symcount
= obj_raw_syment_count (abfd
);
462 /* We keep a list of the linker hash table entries that correspond
463 to particular symbols. */
464 sym_hash
= ((struct coff_link_hash_entry
**)
467 * sizeof (struct coff_link_hash_entry
*))));
468 if (sym_hash
== NULL
&& symcount
!= 0)
470 bfd_set_error (bfd_error_no_memory
);
473 obj_coff_sym_hashes (abfd
) = sym_hash
;
475 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
477 symesz
= bfd_coff_symesz (abfd
);
478 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
479 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
480 esym_end
= esym
+ symcount
* symesz
;
481 while (esym
< esym_end
)
483 struct internal_syment sym
;
486 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
488 if (sym
.n_sclass
== C_EXT
489 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
492 char buf
[SYMNMLEN
+ 1];
497 /* This symbol is externally visible. */
499 /* FIXME: It's not clear this will work correctly if sizeof
502 if (sym
._n
._n_n
._n_zeroes
== 0
503 && sym
._n
._n_n
._n_offset
!= 0)
505 BFD_ASSERT (sym
._n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
508 strings
= coff_read_string_table (abfd
);
512 name
= strings
+ sym
._n
._n_n
._n_offset
;
516 memcpy (buf
, sym
._n
._n_name
, SYMNMLEN
);
517 buf
[SYMNMLEN
] = '\0';
524 if (sym
.n_scnum
== 0)
529 section
= bfd_und_section_ptr
;
534 section
= bfd_com_section_ptr
;
539 flags
= BSF_EXPORT
| BSF_GLOBAL
;
540 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
541 value
-= section
->vma
;
544 if (! (_bfd_generic_link_add_one_symbol
545 (info
, abfd
, name
, flags
, section
, value
,
546 (const char *) NULL
, copy
, false,
547 (struct bfd_link_hash_entry
**) sym_hash
)))
550 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
552 if (((*sym_hash
)->class == C_NULL
553 && (*sym_hash
)->type
== T_NULL
)
556 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
558 (*sym_hash
)->class = sym
.n_sclass
;
559 (*sym_hash
)->type
= sym
.n_type
;
560 (*sym_hash
)->numaux
= sym
.n_numaux
;
561 (*sym_hash
)->auxbfd
= abfd
;
562 if (sym
.n_numaux
!= 0)
564 union internal_auxent
*alloc
;
567 union internal_auxent
*iaux
;
569 alloc
= bfd_hash_allocate (&info
->hash
->table
,
574 bfd_set_error (bfd_error_no_memory
);
577 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
579 i
++, eaux
+= symesz
, iaux
++)
580 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
581 sym
.n_sclass
, i
, sym
.n_numaux
,
583 (*sym_hash
)->aux
= alloc
;
589 esym
+= (sym
.n_numaux
+ 1) * symesz
;
590 sym_hash
+= sym
.n_numaux
+ 1;
596 /* Do the final link step. */
599 _bfd_coff_final_link (abfd
, info
)
601 struct bfd_link_info
*info
;
603 bfd_size_type symesz
;
604 struct coff_final_link_info finfo
;
606 struct bfd_link_order
*p
;
607 size_t max_contents_size
;
608 size_t max_sym_count
;
609 size_t max_lineno_count
;
610 size_t max_reloc_count
;
611 size_t max_output_reloc_count
;
612 file_ptr rel_filepos
;
614 file_ptr line_filepos
;
617 bfd_byte
*external_relocs
= NULL
;
618 char strbuf
[STRING_SIZE_SIZE
];
620 symesz
= bfd_coff_symesz (abfd
);
623 finfo
.output_bfd
= abfd
;
625 finfo
.section_info
= NULL
;
626 finfo
.last_file_index
= -1;
627 finfo
.internal_syms
= NULL
;
628 finfo
.sec_ptrs
= NULL
;
629 finfo
.sym_indices
= NULL
;
630 finfo
.outsyms
= NULL
;
631 finfo
.linenos
= NULL
;
632 finfo
.contents
= NULL
;
633 finfo
.external_relocs
= NULL
;
634 finfo
.internal_relocs
= NULL
;
636 finfo
.strtab
= _bfd_stringtab_init ();
637 if (finfo
.strtab
== NULL
)
640 /* Compute the file positions for all the sections. */
641 if (! abfd
->output_has_begun
)
642 bfd_coff_compute_section_file_positions (abfd
);
644 /* Count the line numbers and relocation entries required for the
645 output file. Set the file positions for the relocs. */
646 rel_filepos
= obj_relocbase (abfd
);
647 relsz
= bfd_coff_relsz (abfd
);
648 max_contents_size
= 0;
649 max_lineno_count
= 0;
651 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
655 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
657 if (p
->type
== bfd_indirect_link_order
)
661 sec
= p
->u
.indirect
.section
;
663 if (info
->strip
== strip_none
664 || info
->strip
== strip_some
)
665 o
->lineno_count
+= sec
->lineno_count
;
667 if (info
->relocateable
)
668 o
->reloc_count
+= sec
->reloc_count
;
670 if (sec
->_raw_size
> max_contents_size
)
671 max_contents_size
= sec
->_raw_size
;
672 if (sec
->lineno_count
> max_lineno_count
)
673 max_lineno_count
= sec
->lineno_count
;
674 if (sec
->reloc_count
> max_reloc_count
)
675 max_reloc_count
= sec
->reloc_count
;
677 else if (info
->relocateable
678 && (p
->type
== bfd_section_reloc_link_order
679 || p
->type
== bfd_symbol_reloc_link_order
))
682 if (o
->reloc_count
== 0)
686 o
->flags
|= SEC_RELOC
;
687 o
->rel_filepos
= rel_filepos
;
688 rel_filepos
+= o
->reloc_count
* relsz
;
692 /* If doing a relocateable link, allocate space for the pointers we
694 if (info
->relocateable
)
698 /* We use section_count + 1, rather than section_count, because
699 the target_index fields are 1 based. */
700 finfo
.section_info
= ((struct coff_link_section_info
*)
701 malloc ((abfd
->section_count
+ 1)
702 * sizeof (struct coff_link_section_info
)));
703 if (finfo
.section_info
== NULL
)
705 bfd_set_error (bfd_error_no_memory
);
708 for (i
= 0; i
<= abfd
->section_count
; i
++)
710 finfo
.section_info
[i
].relocs
= NULL
;
711 finfo
.section_info
[i
].rel_hashes
= NULL
;
715 /* We now know the size of the relocs, so we can determine the file
716 positions of the line numbers. */
717 line_filepos
= rel_filepos
;
718 linesz
= bfd_coff_linesz (abfd
);
719 max_output_reloc_count
= 0;
720 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
722 if (o
->lineno_count
== 0)
726 o
->line_filepos
= line_filepos
;
727 line_filepos
+= o
->lineno_count
* linesz
;
730 if (o
->reloc_count
!= 0)
732 /* We don't know the indices of global symbols until we have
733 written out all the local symbols. For each section in
734 the output file, we keep an array of pointers to hash
735 table entries. Each entry in the array corresponds to a
736 reloc. When we find a reloc against a global symbol, we
737 set the corresponding entry in this array so that we can
738 fix up the symbol index after we have written out all the
741 Because of this problem, we also keep the relocs in
742 memory until the end of the link. This wastes memory,
743 but only when doing a relocateable link, which is not the
745 BFD_ASSERT (info
->relocateable
);
746 finfo
.section_info
[o
->target_index
].relocs
=
747 ((struct internal_reloc
*)
748 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
749 finfo
.section_info
[o
->target_index
].rel_hashes
=
750 ((struct coff_link_hash_entry
**)
751 malloc (o
->reloc_count
752 * sizeof (struct coff_link_hash_entry
*)));
753 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
754 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
756 bfd_set_error (bfd_error_no_memory
);
760 if (o
->reloc_count
> max_output_reloc_count
)
761 max_output_reloc_count
= o
->reloc_count
;
764 /* Reset the reloc and lineno counts, so that we can use them to
765 count the number of entries we have output so far. */
770 obj_sym_filepos (abfd
) = line_filepos
;
772 /* Figure out the largest number of symbols in an input BFD. Take
773 the opportunity to clear the output_has_begun fields of all the
776 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
780 sub
->output_has_begun
= false;
781 sz
= obj_raw_syment_count (sub
);
782 if (sz
> max_sym_count
)
786 /* Allocate some buffers used while linking. */
787 finfo
.internal_syms
= ((struct internal_syment
*)
788 malloc (max_sym_count
789 * sizeof (struct internal_syment
)));
790 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
791 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
792 finfo
.outsyms
= (bfd_byte
*) malloc ((max_sym_count
+ 1) * symesz
);
793 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
794 * bfd_coff_linesz (abfd
));
795 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
796 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
797 if (! info
->relocateable
)
798 finfo
.internal_relocs
= ((struct internal_reloc
*)
799 malloc (max_reloc_count
800 * sizeof (struct internal_reloc
)));
801 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
802 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
803 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
804 || finfo
.outsyms
== NULL
805 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
806 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
807 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
808 || (! info
->relocateable
809 && finfo
.internal_relocs
== NULL
810 && max_reloc_count
> 0))
812 bfd_set_error (bfd_error_no_memory
);
816 /* We now know the position of everything in the file, except that
817 we don't know the size of the symbol table and therefore we don't
818 know where the string table starts. We just build the string
819 table in memory as we go along. We process all the relocations
820 for a single input file at once. */
821 obj_raw_syment_count (abfd
) = 0;
822 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
824 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
826 if (p
->type
== bfd_indirect_link_order
827 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
828 == bfd_target_coff_flavour
))
830 sub
= p
->u
.indirect
.section
->owner
;
831 if (! sub
->output_has_begun
)
833 if (! coff_link_input_bfd (&finfo
, sub
))
835 sub
->output_has_begun
= true;
838 else if (p
->type
== bfd_section_reloc_link_order
839 || p
->type
== bfd_symbol_reloc_link_order
)
841 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
846 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
852 /* Free up the buffers used by coff_link_input_bfd. */
853 if (finfo
.internal_syms
!= NULL
)
855 free (finfo
.internal_syms
);
856 finfo
.internal_syms
= NULL
;
858 if (finfo
.sec_ptrs
!= NULL
)
860 free (finfo
.sec_ptrs
);
861 finfo
.sec_ptrs
= NULL
;
863 if (finfo
.sym_indices
!= NULL
)
865 free (finfo
.sym_indices
);
866 finfo
.sym_indices
= NULL
;
868 if (finfo
.linenos
!= NULL
)
870 free (finfo
.linenos
);
871 finfo
.linenos
= NULL
;
873 if (finfo
.contents
!= NULL
)
875 free (finfo
.contents
);
876 finfo
.contents
= NULL
;
878 if (finfo
.external_relocs
!= NULL
)
880 free (finfo
.external_relocs
);
881 finfo
.external_relocs
= NULL
;
883 if (finfo
.internal_relocs
!= NULL
)
885 free (finfo
.internal_relocs
);
886 finfo
.internal_relocs
= NULL
;
889 /* The value of the last C_FILE symbol is supposed to be the symbol
890 index of the first external symbol. Write it out again if
892 if (finfo
.last_file_index
!= -1
893 && finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
895 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
896 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
897 (PTR
) finfo
.outsyms
);
899 (obj_sym_filepos (abfd
)
900 + finfo
.last_file_index
* symesz
),
902 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
906 /* Write out the global symbols. */
907 finfo
.failed
= false;
908 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
913 /* The outsyms buffer is used by coff_write_global_sym. */
914 if (finfo
.outsyms
!= NULL
)
916 free (finfo
.outsyms
);
917 finfo
.outsyms
= NULL
;
920 if (info
->relocateable
)
922 /* Now that we have written out all the global symbols, we know
923 the symbol indices to use for relocs against them, and we can
924 finally write out the relocs. */
925 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
926 if (external_relocs
== NULL
)
928 bfd_set_error (bfd_error_no_memory
);
932 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
934 struct internal_reloc
*irel
;
935 struct internal_reloc
*irelend
;
936 struct coff_link_hash_entry
**rel_hash
;
939 if (o
->reloc_count
== 0)
942 irel
= finfo
.section_info
[o
->target_index
].relocs
;
943 irelend
= irel
+ o
->reloc_count
;
944 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
945 erel
= external_relocs
;
946 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
948 if (*rel_hash
!= NULL
)
950 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
951 irel
->r_symndx
= (*rel_hash
)->indx
;
953 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
956 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
957 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
958 abfd
) != relsz
* o
->reloc_count
)
962 free (external_relocs
);
963 external_relocs
= NULL
;
966 /* Free up the section information. */
967 if (finfo
.section_info
!= NULL
)
971 for (i
= 0; i
< abfd
->section_count
; i
++)
973 if (finfo
.section_info
[i
].relocs
!= NULL
)
974 free (finfo
.section_info
[i
].relocs
);
975 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
976 free (finfo
.section_info
[i
].rel_hashes
);
978 free (finfo
.section_info
);
979 finfo
.section_info
= NULL
;
982 /* Write out the string table. */
984 (obj_sym_filepos (abfd
)
985 + obj_raw_syment_count (abfd
) * symesz
),
989 #if STRING_SIZE_SIZE == 4
991 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
994 #error Change bfd_h_put_32
997 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1000 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1003 _bfd_stringtab_free (finfo
.strtab
);
1005 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1006 not try to write out the symbols. */
1007 bfd_get_symcount (abfd
) = 0;
1012 if (finfo
.strtab
!= NULL
)
1013 _bfd_stringtab_free (finfo
.strtab
);
1014 if (finfo
.section_info
!= NULL
)
1018 for (i
= 0; i
< abfd
->section_count
; i
++)
1020 if (finfo
.section_info
[i
].relocs
!= NULL
)
1021 free (finfo
.section_info
[i
].relocs
);
1022 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1023 free (finfo
.section_info
[i
].rel_hashes
);
1025 free (finfo
.section_info
);
1027 if (finfo
.internal_syms
!= NULL
)
1028 free (finfo
.internal_syms
);
1029 if (finfo
.sec_ptrs
!= NULL
)
1030 free (finfo
.sec_ptrs
);
1031 if (finfo
.sym_indices
!= NULL
)
1032 free (finfo
.sym_indices
);
1033 if (finfo
.outsyms
!= NULL
)
1034 free (finfo
.outsyms
);
1035 if (finfo
.linenos
!= NULL
)
1036 free (finfo
.linenos
);
1037 if (finfo
.contents
!= NULL
)
1038 free (finfo
.contents
);
1039 if (finfo
.external_relocs
!= NULL
)
1040 free (finfo
.external_relocs
);
1041 if (finfo
.internal_relocs
!= NULL
)
1042 free (finfo
.internal_relocs
);
1043 if (external_relocs
!= NULL
)
1044 free (external_relocs
);
1048 /* Link an input file into the linker output file. This function
1049 handles all the sections and relocations of the input file at once. */
1052 coff_link_input_bfd (finfo
, input_bfd
)
1053 struct coff_final_link_info
*finfo
;
1056 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1058 const char *strings
;
1059 bfd_size_type syment_base
;
1060 unsigned int n_tmask
;
1061 unsigned int n_btshft
;
1063 bfd_size_type isymesz
;
1064 bfd_size_type osymesz
;
1065 bfd_size_type linesz
;
1068 struct internal_syment
*isymp
;
1073 struct coff_link_hash_entry
**sym_hash
;
1074 bfd_size_type relsz
;
1077 /* Move all the symbols to the output file. */
1079 output_bfd
= finfo
->output_bfd
;
1080 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1082 syment_base
= obj_raw_syment_count (output_bfd
);
1083 isymesz
= bfd_coff_symesz (input_bfd
);
1084 osymesz
= bfd_coff_symesz (output_bfd
);
1085 linesz
= bfd_coff_linesz (input_bfd
);
1086 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1088 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1089 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1091 /* Define macros so that ISFCN, et. al., macros work correctly. */
1092 #define N_TMASK n_tmask
1093 #define N_BTSHFT n_btshft
1096 if (! finfo
->info
->keep_memory
)
1099 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1102 if (! coff_link_get_symbols (input_bfd
))
1105 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1106 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1107 isymp
= finfo
->internal_syms
;
1108 secpp
= finfo
->sec_ptrs
;
1109 indexp
= finfo
->sym_indices
;
1110 output_index
= syment_base
;
1111 outsym
= finfo
->outsyms
;
1112 while (esym
< esym_end
)
1114 struct internal_syment isym
;
1119 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1121 /* Make a copy of *isymp so that the relocate_section function
1122 always sees the original values. This is more reliable than
1123 always recomputing the symbol value even if we are stripping
1127 if (isym
.n_scnum
!= 0)
1128 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1131 if (isym
.n_value
== 0)
1132 *secpp
= bfd_und_section_ptr
;
1134 *secpp
= bfd_com_section_ptr
;
1141 add
= 1 + isym
.n_numaux
;
1143 /* If we are stripping all symbols, we want to skip this one. */
1144 if (finfo
->info
->strip
== strip_all
)
1149 if (isym
.n_sclass
== C_EXT
1150 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1152 /* This is a global symbol. Global symbols come at the
1153 end of the symbol table, so skip them for now.
1154 Function symbols, however, are an exception, and are
1155 not moved to the end. */
1157 if (! ISFCN (isym
.n_type
))
1162 /* This is a local symbol. Skip it if we are discarding
1164 if (finfo
->info
->discard
== discard_all
)
1169 /* If we stripping debugging symbols, and this is a debugging
1170 symbol, then skip it. */
1172 && finfo
->info
->strip
== strip_debugger
1173 && isym
.n_scnum
== N_DEBUG
)
1176 /* If some symbols are stripped based on the name, work out the
1177 name and decide whether to skip this symbol. */
1179 && (finfo
->info
->strip
== strip_some
1180 || finfo
->info
->discard
== discard_l
))
1183 char buf
[SYMNMLEN
+ 1];
1185 if (isym
._n
._n_n
._n_zeroes
== 0
1186 && isym
._n
._n_n
._n_offset
!= 0)
1188 if (strings
== NULL
)
1190 strings
= coff_read_string_table (input_bfd
);
1191 if (strings
== NULL
)
1194 name
= strings
+ isym
._n
._n_n
._n_offset
;
1198 memcpy (buf
, isym
._n
._n_name
, SYMNMLEN
);
1199 buf
[SYMNMLEN
] = '\0';
1203 if ((finfo
->info
->strip
== strip_some
1204 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1207 && finfo
->info
->discard
== discard_l
1208 && strncmp (name
, finfo
->info
->lprefix
,
1209 finfo
->info
->lprefix_len
) == 0))
1213 /* We now know whether we are to skip this symbol or not. */
1216 /* Adjust the symbol in order to output it. */
1218 if (isym
._n
._n_n
._n_zeroes
== 0
1219 && isym
._n
._n_n
._n_offset
!= 0)
1224 /* This symbol has a long name. Enter it in the string
1225 table we are building. Note that we do not check
1226 bfd_coff_symname_in_debug. That is only true for
1227 XCOFF, and XCOFF requires different linking code
1229 BFD_ASSERT (isym
._n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
1230 if (strings
== NULL
)
1232 strings
= coff_read_string_table (input_bfd
);
1233 if (strings
== NULL
)
1236 name
= strings
+ isym
._n
._n_n
._n_offset
;
1237 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1238 if (indx
== (bfd_size_type
) -1)
1240 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1243 if (isym
.n_scnum
> 0)
1245 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1246 isym
.n_value
+= ((*secpp
)->output_section
->vma
1247 + (*secpp
)->output_offset
1251 /* The value of a C_FILE symbol is the symbol index of the
1252 next C_FILE symbol. The value of the last C_FILE symbol
1253 is the symbol index to the first external symbol
1254 (actually, coff_renumber_symbols does not get this
1255 right--it just sets the value of the last C_FILE symbol
1256 to zero--and nobody has ever complained about it). We
1257 try to get this right, below, just before we write the
1258 symbols out, but in the general case we may have to write
1259 the symbol out twice. */
1260 if (isym
.n_sclass
== C_FILE
)
1262 if (finfo
->last_file_index
!= -1
1263 && finfo
->last_file
.n_value
!= output_index
)
1265 /* We must correct the value of the last C_FILE entry. */
1266 finfo
->last_file
.n_value
= output_index
;
1267 if (finfo
->last_file_index
>= syment_base
)
1269 /* The last C_FILE symbol is in this input file. */
1270 bfd_coff_swap_sym_out (output_bfd
,
1271 (PTR
) &finfo
->last_file
,
1272 (PTR
) (finfo
->outsyms
1273 + ((finfo
->last_file_index
1279 /* We have already written out the last C_FILE
1280 symbol. We need to write it out again. We
1281 borrow *outsym temporarily. */
1282 bfd_coff_swap_sym_out (output_bfd
,
1283 (PTR
) &finfo
->last_file
,
1285 if (bfd_seek (output_bfd
,
1286 (obj_sym_filepos (output_bfd
)
1287 + finfo
->last_file_index
* osymesz
),
1289 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1295 finfo
->last_file_index
= output_index
;
1296 finfo
->last_file
= isym
;
1299 /* Output the symbol. */
1301 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1303 *indexp
= output_index
;
1308 struct coff_link_hash_entry
*h
;
1310 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1312 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1313 BFD_ASSERT (h
!= NULL
);
1314 h
->indx
= output_index
;
1317 output_index
+= add
;
1318 outsym
+= add
* osymesz
;
1321 esym
+= add
* isymesz
;
1325 for (--add
; add
> 0; --add
)
1332 /* Fix up the aux entries. This must be done in a separate pass,
1333 because we don't know the correct symbol indices until we have
1334 already decided which symbols we are going to keep. */
1336 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1337 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1338 isymp
= finfo
->internal_syms
;
1339 indexp
= finfo
->sym_indices
;
1340 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1341 outsym
= finfo
->outsyms
;
1342 while (esym
< esym_end
)
1346 add
= 1 + isymp
->n_numaux
;
1349 && (*sym_hash
== NULL
1350 || (*sym_hash
)->auxbfd
!= input_bfd
))
1351 esym
+= add
* isymesz
;
1354 struct coff_link_hash_entry
*h
;
1361 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1369 /* Handle the aux entries. This handling is based on
1370 coff_pointerize_aux. I don't know if it always correct. */
1371 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1373 union internal_auxent aux
;
1374 union internal_auxent
*auxp
;
1380 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1381 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1386 if (isymp
->n_sclass
== C_FILE
)
1388 /* If this is a long filename, we must put it in the
1390 if (auxp
->x_file
.x_n
.x_zeroes
== 0)
1392 const char *filename
;
1395 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1396 >= STRING_SIZE_SIZE
);
1397 if (strings
== NULL
)
1399 strings
= coff_read_string_table (input_bfd
);
1400 if (strings
== NULL
)
1403 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1404 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1406 if (indx
== (bfd_size_type
) -1)
1408 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1411 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1415 if (ISFCN (isymp
->n_type
)
1416 || ISTAG (isymp
->n_sclass
)
1417 || isymp
->n_sclass
== C_BLOCK
)
1419 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1421 && indx
< obj_raw_syment_count (input_bfd
))
1423 /* We look forward through the symbol for
1424 the index of the next symbol we are going
1425 to include. I don't know if this is
1427 while (finfo
->sym_indices
[indx
] < 0
1428 && indx
< obj_raw_syment_count (input_bfd
))
1430 if (indx
>= obj_raw_syment_count (input_bfd
))
1431 indx
= output_index
;
1433 indx
= finfo
->sym_indices
[indx
];
1434 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1438 indx
= auxp
->x_sym
.x_tagndx
.l
;
1439 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1441 indx
= finfo
->sym_indices
[indx
];
1443 auxp
->x_sym
.x_tagndx
.l
= 0;
1445 auxp
->x_sym
.x_tagndx
.l
= indx
;
1451 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1452 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1466 /* Relocate the line numbers, unless we are stripping them. */
1467 if (finfo
->info
->strip
== strip_none
1468 || finfo
->info
->strip
== strip_some
)
1470 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1476 if (o
->lineno_count
== 0)
1479 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1480 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1481 input_bfd
) != linesz
* o
->lineno_count
)
1484 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1485 eline
= finfo
->linenos
;
1486 elineend
= eline
+ linesz
* o
->lineno_count
;
1487 for (; eline
< elineend
; eline
+= linesz
)
1489 struct internal_lineno iline
;
1491 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1493 if (iline
.l_lnno
!= 0)
1494 iline
.l_addr
.l_paddr
+= offset
;
1495 else if (iline
.l_addr
.l_symndx
>= 0
1496 && (iline
.l_addr
.l_symndx
1497 < obj_raw_syment_count (input_bfd
)))
1501 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1505 /* These line numbers are attached to a symbol
1506 which we are stripping. We should really
1507 just discard the line numbers, but that would
1508 be a pain because we have already counted
1514 struct internal_syment is
;
1515 union internal_auxent ia
;
1517 /* Fix up the lnnoptr field in the aux entry of
1518 the symbol. It turns out that we can't do
1519 this when we modify the symbol aux entries,
1520 because gas sometimes screws up the lnnoptr
1521 field and makes it an offset from the start
1522 of the line numbers rather than an absolute
1524 bfd_coff_swap_sym_in (output_bfd
,
1525 (PTR
) (finfo
->outsyms
1526 + ((indx
- syment_base
)
1529 if ((ISFCN (is
.n_type
)
1530 || is
.n_sclass
== C_BLOCK
)
1531 && is
.n_numaux
>= 1)
1535 auxptr
= (PTR
) (finfo
->outsyms
1536 + ((indx
- syment_base
+ 1)
1538 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1539 is
.n_type
, is
.n_sclass
,
1540 0, is
.n_numaux
, (PTR
) &ia
);
1541 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1542 (o
->output_section
->line_filepos
1543 + o
->output_section
->lineno_count
* linesz
1544 + eline
- finfo
->linenos
);
1545 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1546 is
.n_type
, is
.n_sclass
, 0,
1547 is
.n_numaux
, auxptr
);
1551 iline
.l_addr
.l_symndx
= indx
;
1554 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1557 if (bfd_seek (output_bfd
,
1558 (o
->output_section
->line_filepos
1559 + o
->output_section
->lineno_count
* linesz
),
1561 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1562 output_bfd
) != linesz
* o
->lineno_count
)
1565 o
->output_section
->lineno_count
+= o
->lineno_count
;
1569 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1570 symbol will be the first symbol in the next input file. In the
1571 normal case, this will save us from writing out the C_FILE symbol
1573 if (finfo
->last_file_index
>= syment_base
)
1575 finfo
->last_file
.n_value
= output_index
;
1576 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1577 (PTR
) (finfo
->outsyms
1578 + ((finfo
->last_file_index
- syment_base
)
1582 /* Write the modified symbols to the output file. */
1583 if (outsym
> finfo
->outsyms
)
1585 if (bfd_seek (output_bfd
,
1586 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1588 || bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1589 output_bfd
) != outsym
- finfo
->outsyms
)
1592 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1593 + (outsym
- finfo
->outsyms
) / osymesz
)
1596 obj_raw_syment_count (output_bfd
) = output_index
;
1599 /* Relocate the contents of each section. */
1600 relsz
= bfd_coff_relsz (input_bfd
);
1601 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1603 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1606 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1607 (file_ptr
) 0, o
->_raw_size
))
1610 if ((o
->flags
& SEC_RELOC
) != 0)
1613 struct internal_reloc
*internal_relocs
;
1616 struct internal_reloc
*irel
;
1618 /* Read in the relocs. */
1619 if (bfd_seek (input_bfd
, o
->rel_filepos
, SEEK_SET
) != 0
1620 || (bfd_read (finfo
->external_relocs
, relsz
, o
->reloc_count
,
1621 input_bfd
) != relsz
* o
->reloc_count
))
1624 /* If we are doing a relocateable link, we keep the swapped
1625 in relocs in memory, and don't write them out until the
1627 target_index
= o
->output_section
->target_index
;
1628 if (! finfo
->info
->relocateable
)
1629 internal_relocs
= finfo
->internal_relocs
;
1631 internal_relocs
= (finfo
->section_info
[target_index
].relocs
1632 + o
->output_section
->reloc_count
);
1634 /* Swap in the relocs. */
1635 erel
= finfo
->external_relocs
;
1636 erel_end
= erel
+ relsz
* o
->reloc_count
;
1637 irel
= internal_relocs
;
1638 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
1639 bfd_coff_swap_reloc_in (input_bfd
, (PTR
) erel
, (PTR
) irel
);
1641 /* Call processor specific code to relocate the section
1643 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1647 finfo
->internal_syms
,
1651 if (finfo
->info
->relocateable
)
1654 struct internal_reloc
*irelend
;
1655 struct coff_link_hash_entry
**rel_hash
;
1657 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1659 irel
= internal_relocs
;
1660 irelend
= irel
+ o
->reloc_count
;
1661 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1662 + o
->output_section
->reloc_count
);
1663 for (; irel
< irelend
; irel
++, rel_hash
++)
1665 struct coff_link_hash_entry
*h
;
1669 /* Adjust the reloc address and symbol index. */
1671 irel
->r_vaddr
+= offset
;
1673 if (irel
->r_symndx
== -1)
1676 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1679 /* This is a global symbol. */
1681 irel
->r_symndx
= h
->indx
;
1684 /* This symbol is being written at the end
1685 of the file, and we do not yet know the
1686 symbol index. We save the pointer to the
1687 hash table entry in the rel_hash list.
1688 We set the indx field to -2 to indicate
1689 that this symbol must not be stripped. */
1698 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1700 irel
->r_symndx
= indx
;
1703 struct internal_syment
*is
;
1705 char buf
[SYMNMLEN
+ 1];
1707 /* This reloc is against a symbol we are
1708 stripping. It would be possible to
1709 handle this case, but I don't think it's
1711 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1713 if (is
->_n
._n_n
._n_zeroes
== 0
1714 && is
->_n
._n_n
._n_offset
!= 0)
1716 if (strings
== NULL
)
1718 strings
= coff_read_string_table (input_bfd
);
1719 if (strings
== NULL
)
1722 name
= strings
+ is
->_n
._n_n
._n_offset
;
1726 memcpy (buf
, is
->_n
._n_name
, SYMNMLEN
);
1727 buf
[SYMNMLEN
] = '\0';
1731 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1732 (finfo
->info
, name
, input_bfd
, o
,
1739 o
->output_section
->reloc_count
+= o
->reloc_count
;
1743 /* Write out the modified section contents. */
1744 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1745 finfo
->contents
, o
->output_offset
,
1746 (o
->_cooked_size
!= 0
1752 if (! finfo
->info
->keep_memory
)
1754 if (! coff_link_free_symbols (input_bfd
))
1761 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1764 coff_write_global_sym (h
, data
)
1765 struct coff_link_hash_entry
*h
;
1768 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1770 struct internal_syment isym
;
1771 bfd_size_type symesz
;
1774 output_bfd
= finfo
->output_bfd
;
1780 && (finfo
->info
->strip
== strip_all
1781 || (finfo
->info
->strip
== strip_some
1782 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1783 h
->root
.root
.string
, false, false)
1787 switch (h
->root
.type
)
1790 case bfd_link_hash_new
:
1794 case bfd_link_hash_undefined
:
1795 case bfd_link_hash_weak
:
1796 isym
.n_scnum
= N_UNDEF
;
1800 case bfd_link_hash_defined
:
1804 sec
= h
->root
.u
.def
.section
->output_section
;
1805 if (bfd_is_abs_section (sec
))
1806 isym
.n_scnum
= N_ABS
;
1808 isym
.n_scnum
= sec
->target_index
;
1809 isym
.n_value
= (h
->root
.u
.def
.value
1811 + h
->root
.u
.def
.section
->output_offset
);
1815 case bfd_link_hash_common
:
1816 isym
.n_scnum
= N_UNDEF
;
1817 isym
.n_value
= h
->root
.u
.c
.size
;
1820 case bfd_link_hash_indirect
:
1821 case bfd_link_hash_warning
:
1822 /* Just ignore these. They can't be handled anyhow. */
1826 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1827 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1834 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1836 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1838 if (indx
== (bfd_size_type
) -1)
1840 finfo
->failed
= true;
1843 isym
._n
._n_n
._n_zeroes
= 0;
1844 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1847 isym
.n_sclass
= h
->class;
1848 isym
.n_type
= h
->type
;
1850 if (isym
.n_sclass
== C_NULL
)
1851 isym
.n_sclass
= C_EXT
;
1853 isym
.n_numaux
= h
->numaux
;
1855 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
1857 symesz
= bfd_coff_symesz (output_bfd
);
1859 if (bfd_seek (output_bfd
,
1860 (obj_sym_filepos (output_bfd
)
1861 + obj_raw_syment_count (output_bfd
) * symesz
),
1863 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
1865 finfo
->failed
= true;
1869 h
->indx
= obj_raw_syment_count (output_bfd
);
1871 ++obj_raw_syment_count (output_bfd
);
1873 /* Write out any associated aux entries. There normally will be
1874 none. If there are any, I have no idea how to modify them. */
1875 for (i
= 0; i
< isym
.n_numaux
; i
++)
1877 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
1878 isym
.n_sclass
, i
, isym
.n_numaux
,
1879 (PTR
) finfo
->outsyms
);
1880 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
1882 finfo
->failed
= true;
1885 ++obj_raw_syment_count (output_bfd
);
1891 /* Handle a link order which is supposed to generate a reloc. */
1894 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
1896 struct coff_final_link_info
*finfo
;
1897 asection
*output_section
;
1898 struct bfd_link_order
*link_order
;
1900 const reloc_howto_type
*howto
;
1901 struct internal_reloc
*irel
;
1902 struct coff_link_hash_entry
**rel_hash_ptr
;
1904 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
1907 bfd_set_error (bfd_error_bad_value
);
1911 if (link_order
->u
.reloc
.p
->addend
!= 0)
1915 bfd_reloc_status_type rstat
;
1918 size
= bfd_get_reloc_size (howto
);
1919 buf
= (bfd_byte
*) bfd_zmalloc (size
);
1922 bfd_set_error (bfd_error_no_memory
);
1926 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
1927 link_order
->u
.reloc
.p
->addend
, buf
);
1933 case bfd_reloc_outofrange
:
1935 case bfd_reloc_overflow
:
1936 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
1938 (link_order
->type
== bfd_section_reloc_link_order
1939 ? bfd_section_name (output_bfd
,
1940 link_order
->u
.reloc
.p
->u
.section
)
1941 : link_order
->u
.reloc
.p
->u
.name
),
1942 howto
->name
, link_order
->u
.reloc
.p
->addend
,
1943 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
1950 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
1951 (file_ptr
) link_order
->offset
, size
);
1957 /* Store the reloc information in the right place. It will get
1958 swapped and written out at the end of the final_link routine. */
1960 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
1961 + output_section
->reloc_count
);
1962 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
1963 + output_section
->reloc_count
);
1965 memset (irel
, 0, sizeof (struct internal_reloc
));
1966 *rel_hash_ptr
= NULL
;
1968 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
1970 if (link_order
->type
== bfd_section_reloc_link_order
)
1972 /* We need to somehow locate a symbol in the right section. The
1973 symbol must either have a value of zero, or we must adjust
1974 the addend by the value of the symbol. FIXME: Write this
1975 when we need it. The old linker couldn't handle this anyhow. */
1977 *rel_hash_ptr
= NULL
;
1982 struct coff_link_hash_entry
*h
;
1984 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
1985 link_order
->u
.reloc
.p
->u
.name
,
1986 false, false, true);
1990 irel
->r_symndx
= h
->indx
;
1993 /* Set the index to -2 to force this symbol to get
2002 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2003 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2004 (asection
*) NULL
, (bfd_vma
) 0)))
2010 /* FIXME: Is this always right? */
2011 irel
->r_type
= howto
->type
;
2013 /* r_size is only used on the RS/6000, which needs its own linker
2014 routines anyhow. r_extern is only used for ECOFF. */
2016 /* FIXME: What is the right value for r_offset? Is zero OK? */
2018 ++output_section
->reloc_count
;
2023 /* A basic reloc handling routine which may be used by processors with
2027 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2028 input_section
, contents
, relocs
, syms
,
2031 struct bfd_link_info
*info
;
2033 asection
*input_section
;
2035 struct internal_reloc
*relocs
;
2036 struct internal_syment
*syms
;
2037 asection
**sections
;
2039 struct internal_reloc
*rel
;
2040 struct internal_reloc
*relend
;
2043 relend
= rel
+ input_section
->reloc_count
;
2044 for (; rel
< relend
; rel
++)
2047 struct coff_link_hash_entry
*h
;
2048 struct internal_syment
*sym
;
2051 const reloc_howto_type
*howto
;
2052 bfd_reloc_status_type rstat
;
2054 symndx
= rel
->r_symndx
;
2063 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2064 sym
= syms
+ symndx
;
2067 /* COFF treats common symbols in one of two ways. Either the
2068 size of the symbol is included in the section contents, or it
2069 is not. We assume that the size is not included, and force
2070 the rtype_to_howto function to adjust the addend as needed. */
2071 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2072 addend
= - sym
->n_value
;
2076 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2089 sec
= bfd_abs_section_ptr
;
2094 sec
= sections
[symndx
];
2095 val
= (sec
->output_section
->vma
2096 + sec
->output_offset
2103 if (h
->root
.type
== bfd_link_hash_defined
)
2107 sec
= h
->root
.u
.def
.section
;
2108 val
= (h
->root
.u
.def
.value
2109 + sec
->output_section
->vma
2110 + sec
->output_offset
);
2112 else if (! info
->relocateable
)
2114 if (! ((*info
->callbacks
->undefined_symbol
)
2115 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2116 rel
->r_vaddr
- input_section
->vma
)))
2121 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2123 rel
->r_vaddr
- input_section
->vma
,
2132 case bfd_reloc_overflow
:
2135 char buf
[SYMNMLEN
+ 1];
2140 name
= h
->root
.root
.string
;
2141 else if (sym
->_n
._n_n
._n_zeroes
== 0
2142 && sym
->_n
._n_n
._n_offset
!= 0)
2143 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
2146 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
2147 buf
[SYMNMLEN
] = '\0';
2151 if (! ((*info
->callbacks
->reloc_overflow
)
2152 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2153 input_section
, rel
->r_vaddr
- input_section
->vma
)))