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 finfo
.section_info
= ((struct coff_link_section_info
*)
699 malloc (abfd
->section_count
700 * sizeof (struct coff_link_section_info
)));
701 if (finfo
.section_info
== NULL
)
703 bfd_set_error (bfd_error_no_memory
);
706 for (i
= 0; i
< abfd
->section_count
; i
++)
708 finfo
.section_info
[i
].relocs
= NULL
;
709 finfo
.section_info
[i
].rel_hashes
= NULL
;
713 /* We now know the size of the relocs, so we can determine the file
714 positions of the line numbers. */
715 line_filepos
= rel_filepos
;
716 linesz
= bfd_coff_linesz (abfd
);
717 max_output_reloc_count
= 0;
718 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
720 if (o
->lineno_count
== 0)
724 o
->line_filepos
= line_filepos
;
725 line_filepos
+= o
->lineno_count
* linesz
;
728 if (o
->reloc_count
!= 0)
730 /* We don't know the indices of global symbols until we have
731 written out all the local symbols. For each section in
732 the output file, we keep an array of pointers to hash
733 table entries. Each entry in the array corresponds to a
734 reloc. When we find a reloc against a global symbol, we
735 set the corresponding entry in this array so that we can
736 fix up the symbol index after we have written out all the
739 Because of this problem, we also keep the relocs in
740 memory until the end of the link. This wastes memory,
741 but only when doing a relocateable link, which is not the
743 BFD_ASSERT (info
->relocateable
);
744 finfo
.section_info
[o
->target_index
].relocs
=
745 ((struct internal_reloc
*)
746 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
747 finfo
.section_info
[o
->target_index
].rel_hashes
=
748 ((struct coff_link_hash_entry
**)
749 malloc (o
->reloc_count
750 * sizeof (struct coff_link_hash_entry
*)));
751 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
752 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
754 bfd_set_error (bfd_error_no_memory
);
758 if (o
->reloc_count
> max_output_reloc_count
)
759 max_output_reloc_count
= o
->reloc_count
;
762 /* Reset the reloc and lineno counts, so that we can use them to
763 count the number of entries we have output so far. */
768 obj_sym_filepos (abfd
) = line_filepos
;
770 /* Figure out the largest number of symbols in an input BFD. Take
771 the opportunity to clear the output_has_begun fields of all the
774 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
778 sub
->output_has_begun
= false;
779 sz
= obj_raw_syment_count (sub
);
780 if (sz
> max_sym_count
)
784 /* Allocate some buffers used while linking. */
785 finfo
.internal_syms
= ((struct internal_syment
*)
786 malloc (max_sym_count
787 * sizeof (struct internal_syment
)));
788 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
789 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
790 finfo
.outsyms
= (bfd_byte
*) malloc ((max_sym_count
+ 1) * symesz
);
791 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
792 * bfd_coff_linesz (abfd
));
793 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
794 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
795 if (! info
->relocateable
)
796 finfo
.internal_relocs
= ((struct internal_reloc
*)
797 malloc (max_reloc_count
798 * sizeof (struct internal_reloc
)));
799 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
800 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
801 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
802 || finfo
.outsyms
== NULL
803 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
804 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
805 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
806 || (! info
->relocateable
807 && finfo
.internal_relocs
== NULL
808 && max_reloc_count
> 0))
810 bfd_set_error (bfd_error_no_memory
);
814 /* We now know the position of everything in the file, except that
815 we don't know the size of the symbol table and therefore we don't
816 know where the string table starts. We just build the string
817 table in memory as we go along. We process all the relocations
818 for a single input file at once. */
819 obj_raw_syment_count (abfd
) = 0;
820 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
822 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
824 if (p
->type
== bfd_indirect_link_order
825 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
826 == bfd_target_coff_flavour
))
828 sub
= p
->u
.indirect
.section
->owner
;
829 if (! sub
->output_has_begun
)
831 if (! coff_link_input_bfd (&finfo
, sub
))
833 sub
->output_has_begun
= true;
836 else if (p
->type
== bfd_section_reloc_link_order
837 || p
->type
== bfd_symbol_reloc_link_order
)
839 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
844 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
850 /* Free up the buffers used by coff_link_input_bfd. */
851 if (finfo
.internal_syms
!= NULL
)
853 free (finfo
.internal_syms
);
854 finfo
.internal_syms
= NULL
;
856 if (finfo
.sec_ptrs
!= NULL
)
858 free (finfo
.sec_ptrs
);
859 finfo
.sec_ptrs
= NULL
;
861 if (finfo
.sym_indices
!= NULL
)
863 free (finfo
.sym_indices
);
864 finfo
.sym_indices
= NULL
;
866 if (finfo
.linenos
!= NULL
)
868 free (finfo
.linenos
);
869 finfo
.linenos
= NULL
;
871 if (finfo
.contents
!= NULL
)
873 free (finfo
.contents
);
874 finfo
.contents
= NULL
;
876 if (finfo
.external_relocs
!= NULL
)
878 free (finfo
.external_relocs
);
879 finfo
.external_relocs
= NULL
;
881 if (finfo
.internal_relocs
!= NULL
)
883 free (finfo
.internal_relocs
);
884 finfo
.internal_relocs
= NULL
;
887 /* The value of the last C_FILE symbol is supposed to be the symbol
888 index of the first external symbol. Write it out again if
890 if (finfo
.last_file_index
!= -1
891 && finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
893 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
894 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
895 (PTR
) finfo
.outsyms
);
897 (obj_sym_filepos (abfd
)
898 + finfo
.last_file_index
* symesz
),
900 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
904 /* Write out the global symbols. */
905 finfo
.failed
= false;
906 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
911 /* The outsyms buffer is used by coff_write_global_sym. */
912 if (finfo
.outsyms
!= NULL
)
914 free (finfo
.outsyms
);
915 finfo
.outsyms
= NULL
;
918 if (info
->relocateable
)
920 /* Now that we have written out all the global symbols, we know
921 the symbol indices to use for relocs against them, and we can
922 finally write out the relocs. */
923 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
924 if (external_relocs
== NULL
)
926 bfd_set_error (bfd_error_no_memory
);
930 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
932 struct internal_reloc
*irel
;
933 struct internal_reloc
*irelend
;
934 struct coff_link_hash_entry
**rel_hash
;
937 if (o
->reloc_count
== 0)
940 irel
= finfo
.section_info
[o
->target_index
].relocs
;
941 irelend
= irel
+ o
->reloc_count
;
942 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
943 erel
= external_relocs
;
944 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
946 if (*rel_hash
!= NULL
)
948 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
949 irel
->r_symndx
= (*rel_hash
)->indx
;
951 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
954 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
955 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
956 abfd
) != relsz
* o
->reloc_count
)
960 free (external_relocs
);
961 external_relocs
= NULL
;
964 /* Free up the section information. */
965 if (finfo
.section_info
!= NULL
)
969 for (i
= 0; i
< abfd
->section_count
; i
++)
971 if (finfo
.section_info
[i
].relocs
!= NULL
)
972 free (finfo
.section_info
[i
].relocs
);
973 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
974 free (finfo
.section_info
[i
].rel_hashes
);
976 free (finfo
.section_info
);
977 finfo
.section_info
= NULL
;
980 /* Write out the string table. */
982 (obj_sym_filepos (abfd
)
983 + obj_raw_syment_count (abfd
) * symesz
),
987 #if STRING_SIZE_SIZE == 4
989 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
992 #error Change bfd_h_put_32
995 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
998 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1001 _bfd_stringtab_free (finfo
.strtab
);
1003 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1004 not try to write out the symbols. */
1005 bfd_get_symcount (abfd
) = 0;
1010 if (finfo
.strtab
!= NULL
)
1011 _bfd_stringtab_free (finfo
.strtab
);
1012 if (finfo
.section_info
!= NULL
)
1016 for (i
= 0; i
< abfd
->section_count
; i
++)
1018 if (finfo
.section_info
[i
].relocs
!= NULL
)
1019 free (finfo
.section_info
[i
].relocs
);
1020 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1021 free (finfo
.section_info
[i
].rel_hashes
);
1023 free (finfo
.section_info
);
1025 if (finfo
.internal_syms
!= NULL
)
1026 free (finfo
.internal_syms
);
1027 if (finfo
.sec_ptrs
!= NULL
)
1028 free (finfo
.sec_ptrs
);
1029 if (finfo
.sym_indices
!= NULL
)
1030 free (finfo
.sym_indices
);
1031 if (finfo
.outsyms
!= NULL
)
1032 free (finfo
.outsyms
);
1033 if (finfo
.linenos
!= NULL
)
1034 free (finfo
.linenos
);
1035 if (finfo
.contents
!= NULL
)
1036 free (finfo
.contents
);
1037 if (finfo
.external_relocs
!= NULL
)
1038 free (finfo
.external_relocs
);
1039 if (finfo
.internal_relocs
!= NULL
)
1040 free (finfo
.internal_relocs
);
1041 if (external_relocs
!= NULL
)
1042 free (external_relocs
);
1046 /* Link an input file into the linker output file. This function
1047 handles all the sections and relocations of the input file at once. */
1050 coff_link_input_bfd (finfo
, input_bfd
)
1051 struct coff_final_link_info
*finfo
;
1054 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1056 const char *strings
;
1057 bfd_size_type syment_base
;
1058 unsigned int n_tmask
;
1059 unsigned int n_btshft
;
1061 bfd_size_type isymesz
;
1062 bfd_size_type osymesz
;
1063 bfd_size_type linesz
;
1066 struct internal_syment
*isymp
;
1071 struct coff_link_hash_entry
**sym_hash
;
1072 bfd_size_type relsz
;
1075 /* Move all the symbols to the output file. */
1077 output_bfd
= finfo
->output_bfd
;
1078 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1080 syment_base
= obj_raw_syment_count (output_bfd
);
1081 isymesz
= bfd_coff_symesz (input_bfd
);
1082 osymesz
= bfd_coff_symesz (output_bfd
);
1083 linesz
= bfd_coff_linesz (input_bfd
);
1084 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1086 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1087 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1089 /* Define macros so that ISFCN, et. al., macros work correctly. */
1090 #define N_TMASK n_tmask
1091 #define N_BTSHFT n_btshft
1094 if (! finfo
->info
->keep_memory
)
1097 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1100 if (! coff_link_get_symbols (input_bfd
))
1103 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1104 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1105 isymp
= finfo
->internal_syms
;
1106 secpp
= finfo
->sec_ptrs
;
1107 indexp
= finfo
->sym_indices
;
1108 output_index
= syment_base
;
1109 outsym
= finfo
->outsyms
;
1110 while (esym
< esym_end
)
1112 struct internal_syment isym
;
1117 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1119 /* Make a copy of *isymp so that the relocate_section function
1120 always sees the original values. This is more reliable than
1121 always recomputing the symbol value even if we are stripping
1125 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1130 add
= 1 + isym
.n_numaux
;
1132 /* If we are stripping all symbols, we want to skip this one. */
1133 if (finfo
->info
->strip
== strip_all
)
1138 if (isym
.n_sclass
== C_EXT
1139 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1141 /* This is a global symbol. Global symbols come at the
1142 end of the symbol table, so skip them for now.
1143 Function symbols, however, are an exception, and are
1144 not moved to the end. */
1146 if (! ISFCN (isym
.n_type
))
1151 /* This is a local symbol. Skip it if we are discarding
1153 if (finfo
->info
->discard
== discard_all
)
1158 /* If we stripping debugging symbols, and this is a debugging
1159 symbol, then skip it. */
1161 && finfo
->info
->strip
== strip_debugger
1162 && isym
.n_scnum
== N_DEBUG
)
1165 /* If some symbols are stripped based on the name, work out the
1166 name and decide whether to skip this symbol. */
1168 && (finfo
->info
->strip
== strip_some
1169 || finfo
->info
->discard
== discard_l
))
1172 char buf
[SYMNMLEN
+ 1];
1174 if (isym
._n
._n_n
._n_zeroes
== 0
1175 && isym
._n
._n_n
._n_offset
!= 0)
1177 if (strings
== NULL
)
1179 strings
= coff_read_string_table (input_bfd
);
1180 if (strings
== NULL
)
1183 name
= strings
+ isym
._n
._n_n
._n_offset
;
1187 memcpy (buf
, isym
._n
._n_name
, SYMNMLEN
);
1188 buf
[SYMNMLEN
] = '\0';
1192 if ((finfo
->info
->strip
== strip_some
1193 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1196 && finfo
->info
->discard
== discard_l
1197 && strncmp (name
, finfo
->info
->lprefix
,
1198 finfo
->info
->lprefix_len
) == 0))
1202 /* We now know whether we are to skip this symbol or not. */
1205 /* Adjust the symbol in order to output it. */
1207 if (isym
._n
._n_n
._n_zeroes
== 0
1208 && isym
._n
._n_n
._n_offset
!= 0)
1213 /* This symbol has a long name. Enter it in the string
1214 table we are building. Note that we do not check
1215 bfd_coff_symname_in_debug. That is only true for
1216 XCOFF, and XCOFF requires different linking code
1218 BFD_ASSERT (isym
._n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
1219 if (strings
== NULL
)
1221 strings
= coff_read_string_table (input_bfd
);
1222 if (strings
== NULL
)
1225 name
= strings
+ isym
._n
._n_n
._n_offset
;
1226 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1227 if (indx
== (bfd_size_type
) -1)
1229 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1232 if (isym
.n_scnum
> 0)
1234 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1235 isym
.n_value
+= ((*secpp
)->output_section
->vma
1236 + (*secpp
)->output_offset
1240 /* The value of a C_FILE symbol is the symbol index of the
1241 next C_FILE symbol. The value of the last C_FILE symbol
1242 is the symbol index to the first external symbol
1243 (actually, coff_renumber_symbols does not get this
1244 right--it just sets the value of the last C_FILE symbol
1245 to zero--and nobody has ever complained about it). We
1246 try to get this right, below, just before we write the
1247 symbols out, but in the general case we may have to write
1248 the symbol out twice. */
1249 if (isym
.n_sclass
== C_FILE
)
1251 if (finfo
->last_file_index
!= -1
1252 && finfo
->last_file
.n_value
!= output_index
)
1254 /* We must correct the value of the last C_FILE entry. */
1255 finfo
->last_file
.n_value
= output_index
;
1256 if (finfo
->last_file_index
>= syment_base
)
1258 /* The last C_FILE symbol is in this input file. */
1259 bfd_coff_swap_sym_out (output_bfd
,
1260 (PTR
) &finfo
->last_file
,
1261 (PTR
) (finfo
->outsyms
1262 + ((finfo
->last_file_index
1268 /* We have already written out the last C_FILE
1269 symbol. We need to write it out again. We
1270 borrow *outsym temporarily. */
1271 bfd_coff_swap_sym_out (output_bfd
,
1272 (PTR
) &finfo
->last_file
,
1274 if (bfd_seek (output_bfd
,
1275 (obj_sym_filepos (output_bfd
)
1276 + finfo
->last_file_index
* osymesz
),
1278 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1284 finfo
->last_file_index
= output_index
;
1285 finfo
->last_file
= isym
;
1288 /* Output the symbol. */
1290 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1292 *indexp
= output_index
;
1297 struct coff_link_hash_entry
*h
;
1299 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1301 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1302 BFD_ASSERT (h
!= NULL
);
1303 h
->indx
= output_index
;
1306 output_index
+= add
;
1307 outsym
+= add
* osymesz
;
1310 esym
+= add
* isymesz
;
1314 for (--add
; add
> 0; --add
)
1321 /* Fix up the aux entries. This must be done in a separate pass,
1322 because we don't know the correct symbol indices until we have
1323 already decided which symbols we are going to keep. */
1325 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1326 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1327 isymp
= finfo
->internal_syms
;
1328 indexp
= finfo
->sym_indices
;
1329 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1330 outsym
= finfo
->outsyms
;
1331 while (esym
< esym_end
)
1335 add
= 1 + isymp
->n_numaux
;
1338 && (*sym_hash
== NULL
1339 || (*sym_hash
)->auxbfd
!= input_bfd
))
1340 esym
+= add
* isymesz
;
1343 struct coff_link_hash_entry
*h
;
1350 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1358 /* Handle the aux entries. This handling is based on
1359 coff_pointerize_aux. I don't know if it always correct. */
1360 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1362 union internal_auxent aux
;
1363 union internal_auxent
*auxp
;
1369 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1370 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1375 if (isymp
->n_sclass
== C_FILE
)
1377 /* If this is a long filename, we must put it in the
1379 if (auxp
->x_file
.x_n
.x_zeroes
== 0)
1381 const char *filename
;
1384 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1385 >= STRING_SIZE_SIZE
);
1386 if (strings
== NULL
)
1388 strings
= coff_read_string_table (input_bfd
);
1389 if (strings
== NULL
)
1392 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1393 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1395 if (indx
== (bfd_size_type
) -1)
1397 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1400 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1404 if (ISFCN (isymp
->n_type
)
1405 || ISTAG (isymp
->n_sclass
)
1406 || isymp
->n_sclass
== C_BLOCK
)
1408 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1410 && indx
< obj_raw_syment_count (input_bfd
))
1412 /* We look forward through the symbol for
1413 the index of the next symbol we are going
1414 to include. I don't know if this is
1416 while (finfo
->sym_indices
[indx
] < 0
1417 && indx
< obj_raw_syment_count (input_bfd
))
1419 if (indx
>= obj_raw_syment_count (input_bfd
))
1420 indx
= output_index
;
1422 indx
= finfo
->sym_indices
[indx
];
1423 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1427 indx
= auxp
->x_sym
.x_tagndx
.l
;
1428 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1430 indx
= finfo
->sym_indices
[indx
];
1432 auxp
->x_sym
.x_tagndx
.l
= 0;
1434 auxp
->x_sym
.x_tagndx
.l
= indx
;
1440 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1441 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1455 /* Relocate the line numbers, unless we are stripping them. */
1456 if (finfo
->info
->strip
== strip_none
1457 || finfo
->info
->strip
== strip_some
)
1459 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1465 if (o
->lineno_count
== 0)
1468 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1469 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1470 input_bfd
) != linesz
* o
->lineno_count
)
1473 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1474 eline
= finfo
->linenos
;
1475 elineend
= eline
+ linesz
* o
->lineno_count
;
1476 for (; eline
< elineend
; eline
+= linesz
)
1478 struct internal_lineno iline
;
1480 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1482 if (iline
.l_lnno
!= 0)
1483 iline
.l_addr
.l_paddr
+= offset
;
1484 else if (iline
.l_addr
.l_symndx
>= 0
1485 && (iline
.l_addr
.l_symndx
1486 < obj_raw_syment_count (input_bfd
)))
1490 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1494 /* These line numbers are attached to a symbol
1495 which we are stripping. We should really
1496 just discard the line numbers, but that would
1497 be a pain because we have already counted
1503 struct internal_syment is
;
1504 union internal_auxent ia
;
1506 /* Fix up the lnnoptr field in the aux entry of
1507 the symbol. It turns out that we can't do
1508 this when we modify the symbol aux entries,
1509 because gas sometimes screws up the lnnoptr
1510 field and makes it an offset from the start
1511 of the line numbers rather than an absolute
1513 bfd_coff_swap_sym_in (output_bfd
,
1514 (PTR
) (finfo
->outsyms
1515 + ((indx
- syment_base
)
1518 if ((ISFCN (is
.n_type
)
1519 || is
.n_sclass
== C_BLOCK
)
1520 && is
.n_numaux
>= 1)
1524 auxptr
= (PTR
) (finfo
->outsyms
1525 + ((indx
- syment_base
+ 1)
1527 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1528 is
.n_type
, is
.n_sclass
,
1529 0, is
.n_numaux
, (PTR
) &ia
);
1530 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1531 (o
->output_section
->line_filepos
1532 + o
->output_section
->lineno_count
* linesz
1533 + eline
- finfo
->linenos
);
1534 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1535 is
.n_type
, is
.n_sclass
, 0,
1536 is
.n_numaux
, auxptr
);
1540 iline
.l_addr
.l_symndx
= indx
;
1543 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1546 if (bfd_seek (output_bfd
,
1547 (o
->output_section
->line_filepos
1548 + o
->output_section
->lineno_count
* linesz
),
1550 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1551 output_bfd
) != linesz
* o
->lineno_count
)
1554 o
->output_section
->lineno_count
+= o
->lineno_count
;
1558 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1559 symbol will be the first symbol in the next input file. In the
1560 normal case, this will save us from writing out the C_FILE symbol
1562 if (finfo
->last_file_index
>= syment_base
)
1564 finfo
->last_file
.n_value
= output_index
;
1565 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1566 (PTR
) (finfo
->outsyms
1567 + ((finfo
->last_file_index
- syment_base
)
1571 /* Write the modified symbols to the output file. */
1572 if (outsym
> finfo
->outsyms
)
1574 if (bfd_seek (output_bfd
,
1575 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1577 || bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1578 output_bfd
) != outsym
- finfo
->outsyms
)
1581 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1582 + (outsym
- finfo
->outsyms
) / osymesz
)
1585 obj_raw_syment_count (output_bfd
) = output_index
;
1588 /* Relocate the contents of each section. */
1589 relsz
= bfd_coff_relsz (input_bfd
);
1590 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1592 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1595 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1596 (file_ptr
) 0, o
->_raw_size
))
1599 if ((o
->flags
& SEC_RELOC
) != 0)
1602 struct internal_reloc
*internal_relocs
;
1605 struct internal_reloc
*irel
;
1607 /* Read in the relocs. */
1608 if (bfd_seek (input_bfd
, o
->rel_filepos
, SEEK_SET
) != 0
1609 || (bfd_read (finfo
->external_relocs
, relsz
, o
->reloc_count
,
1610 input_bfd
) != relsz
* o
->reloc_count
))
1613 /* If we are doing a relocateable link, we keep the swapped
1614 in relocs in memory, and don't write them out until the
1616 target_index
= o
->output_section
->target_index
;
1617 if (! finfo
->info
->relocateable
)
1618 internal_relocs
= finfo
->internal_relocs
;
1620 internal_relocs
= (finfo
->section_info
[target_index
].relocs
1621 + o
->output_section
->reloc_count
);
1623 /* Swap in the relocs. */
1624 erel
= finfo
->external_relocs
;
1625 erel_end
= erel
+ relsz
* o
->reloc_count
;
1626 irel
= internal_relocs
;
1627 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
1628 bfd_coff_swap_reloc_in (input_bfd
, (PTR
) erel
, (PTR
) irel
);
1630 /* Call processor specific code to relocate the section
1632 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1636 finfo
->internal_syms
,
1640 if (finfo
->info
->relocateable
)
1643 struct internal_reloc
*irelend
;
1644 struct coff_link_hash_entry
**rel_hash
;
1646 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1648 irel
= internal_relocs
;
1649 irelend
= irel
+ o
->reloc_count
;
1650 rel_hash
= finfo
->section_info
[target_index
].rel_hashes
;
1651 for (; irel
< irelend
; irel
++, rel_hash
++)
1653 struct coff_link_hash_entry
*h
;
1657 /* Adjust the reloc address and symbol index. */
1659 irel
->r_vaddr
+= offset
;
1661 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1664 /* This is a global symbol. */
1666 irel
->r_symndx
= h
->indx
;
1669 /* This symbol is being written at the end
1670 of the file, and we do not yet know the
1671 symbol index. We save the pointer to the
1672 hash table entry in the rel_hash list.
1673 We set the indx field to -2 to indicate
1674 that this symbol must not be stripped. */
1683 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1685 irel
->r_symndx
= indx
;
1688 struct internal_syment
*is
;
1690 char buf
[SYMNMLEN
+ 1];
1692 /* This reloc is against a symbol we are
1693 stripping. It would be possible to
1694 handle this case, but I don't think it's
1696 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1698 if (is
->_n
._n_n
._n_zeroes
== 0
1699 && is
->_n
._n_n
._n_offset
!= 0)
1701 if (strings
== NULL
)
1703 strings
= coff_read_string_table (input_bfd
);
1704 if (strings
== NULL
)
1707 name
= strings
+ is
->_n
._n_n
._n_offset
;
1711 memcpy (buf
, is
->_n
._n_name
, SYMNMLEN
);
1712 buf
[SYMNMLEN
] = '\0';
1716 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1717 (finfo
->info
, name
, input_bfd
, o
,
1724 o
->output_section
->reloc_count
+= o
->reloc_count
;
1728 /* Write out the modified section contents. */
1729 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1730 finfo
->contents
, o
->output_offset
,
1731 (o
->_cooked_size
!= 0
1737 if (! finfo
->info
->keep_memory
)
1739 if (! coff_link_free_symbols (input_bfd
))
1746 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1749 coff_write_global_sym (h
, data
)
1750 struct coff_link_hash_entry
*h
;
1753 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1755 struct internal_syment isym
;
1756 bfd_size_type symesz
;
1759 output_bfd
= finfo
->output_bfd
;
1765 && (finfo
->info
->strip
== strip_all
1766 || (finfo
->info
->strip
== strip_some
1767 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1768 h
->root
.root
.string
, false, false)
1772 switch (h
->root
.type
)
1775 case bfd_link_hash_new
:
1779 case bfd_link_hash_undefined
:
1780 case bfd_link_hash_weak
:
1781 isym
.n_scnum
= N_UNDEF
;
1785 case bfd_link_hash_defined
:
1789 sec
= h
->root
.u
.def
.section
->output_section
;
1790 if (bfd_is_abs_section (sec
))
1791 isym
.n_scnum
= N_ABS
;
1793 isym
.n_scnum
= sec
->target_index
;
1794 isym
.n_value
= (h
->root
.u
.def
.value
1796 + h
->root
.u
.def
.section
->output_offset
);
1800 case bfd_link_hash_common
:
1801 isym
.n_scnum
= N_UNDEF
;
1802 isym
.n_value
= h
->root
.u
.c
.size
;
1805 case bfd_link_hash_indirect
:
1806 case bfd_link_hash_warning
:
1807 /* Just ignore these. They can't be handled anyhow. */
1811 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1812 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1819 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1821 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1823 if (indx
== (bfd_size_type
) -1)
1825 finfo
->failed
= true;
1828 isym
._n
._n_n
._n_zeroes
= 0;
1829 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1832 isym
.n_sclass
= h
->class;
1833 isym
.n_type
= h
->type
;
1835 if (isym
.n_sclass
== C_NULL
)
1836 isym
.n_sclass
= C_EXT
;
1838 isym
.n_numaux
= h
->numaux
;
1840 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
1842 symesz
= bfd_coff_symesz (output_bfd
);
1844 if (bfd_seek (output_bfd
,
1845 (obj_sym_filepos (output_bfd
)
1846 + obj_raw_syment_count (output_bfd
) * symesz
),
1848 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
1850 finfo
->failed
= true;
1854 h
->indx
= obj_raw_syment_count (output_bfd
);
1856 ++obj_raw_syment_count (output_bfd
);
1858 /* Write out any associated aux entries. There normally will be
1859 none. If there are any, I have no idea how to modify them. */
1860 for (i
= 0; i
< isym
.n_numaux
; i
++)
1862 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
1863 isym
.n_sclass
, i
, isym
.n_numaux
,
1864 (PTR
) finfo
->outsyms
);
1865 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
1867 finfo
->failed
= true;
1870 ++obj_raw_syment_count (output_bfd
);
1876 /* Handle a link order which is supposed to generate a reloc. */
1879 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
1881 struct coff_final_link_info
*finfo
;
1882 asection
*output_section
;
1883 struct bfd_link_order
*link_order
;
1885 const reloc_howto_type
*howto
;
1886 struct internal_reloc
*irel
;
1887 struct coff_link_hash_entry
**rel_hash_ptr
;
1889 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
1892 bfd_set_error (bfd_error_bad_value
);
1896 if (link_order
->u
.reloc
.p
->addend
!= 0)
1900 bfd_reloc_status_type rstat
;
1903 size
= bfd_get_reloc_size (howto
);
1904 buf
= (bfd_byte
*) bfd_zmalloc (size
);
1907 bfd_set_error (bfd_error_no_memory
);
1911 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
1912 link_order
->u
.reloc
.p
->addend
, buf
);
1918 case bfd_reloc_outofrange
:
1920 case bfd_reloc_overflow
:
1921 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
1923 (link_order
->type
== bfd_section_reloc_link_order
1924 ? bfd_section_name (output_bfd
,
1925 link_order
->u
.reloc
.p
->u
.section
)
1926 : link_order
->u
.reloc
.p
->u
.name
),
1927 howto
->name
, link_order
->u
.reloc
.p
->addend
,
1928 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
1935 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
1936 (file_ptr
) link_order
->offset
, size
);
1942 /* Store the reloc information in the right place. It will get
1943 swapped and written out at the end of the final_link routine. */
1945 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
1946 + output_section
->reloc_count
);
1947 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
1948 + output_section
->reloc_count
);
1950 memset (irel
, 0, sizeof (struct internal_reloc
));
1951 *rel_hash_ptr
= NULL
;
1953 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
1955 if (link_order
->type
== bfd_section_reloc_link_order
)
1957 /* We need to somehow locate a symbol in the right section. The
1958 symbol must either have a value of zero, or we must adjust
1959 the addend by the value of the symbol. FIXME: Write this
1960 when we need it. The old linker couldn't handle this anyhow. */
1962 *rel_hash_ptr
= NULL
;
1967 struct coff_link_hash_entry
*h
;
1969 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
1970 link_order
->u
.reloc
.p
->u
.name
,
1971 false, false, true);
1975 irel
->r_symndx
= h
->indx
;
1978 /* Set the index to -2 to force this symbol to get
1987 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1988 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
1989 (asection
*) NULL
, (bfd_vma
) 0)))
1995 /* FIXME: Is this always right? */
1996 irel
->r_type
= howto
->type
;
1998 /* r_size is only used on the RS/6000, which needs its own linker
1999 routines anyhow. r_extern is only used for ECOFF. */
2001 /* FIXME: What is the right value for r_offset? Is zero OK? */
2003 ++output_section
->reloc_count
;