1 /* COFF specific linker code.
2 Copyright 1994, 1995 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file contains the COFF backend linker code. */
27 #include "coff/internal.h"
30 #define STRING_SIZE_SIZE (4)
32 /* We use a hash table to merge identical enum, struct, and union
33 definitions in the linker. */
35 /* Information we keep for a single element (an enum value, a
36 structure or union field) in the debug merge hash table. */
38 struct coff_debug_merge_element
41 struct coff_debug_merge_element
*next
;
49 /* Symbol index for complex type. */
53 /* A linked list of debug merge entries for a given name. */
55 struct coff_debug_merge_type
57 /* Next type with the same name. */
58 struct coff_debug_merge_type
*next
;
63 /* Symbol index where this type is defined. */
66 /* List of elements. */
67 struct coff_debug_merge_element
*elements
;
70 /* Information we store in the debug merge hash table. */
72 struct coff_debug_merge_hash_entry
74 struct bfd_hash_entry root
;
76 /* A list of types with this name. */
77 struct coff_debug_merge_type
*types
;
80 /* The debug merge hash table. */
82 struct coff_debug_merge_hash_table
84 struct bfd_hash_table root
;
87 /* Initialize a COFF debug merge hash table. */
89 #define coff_debug_merge_hash_table_init(table) \
90 (bfd_hash_table_init (&(table)->root, coff_debug_merge_hash_newfunc))
92 /* Free a COFF debug merge hash table. */
94 #define coff_debug_merge_hash_table_free(table) \
95 (bfd_hash_table_free (&(table)->root))
97 /* Look up an entry in a COFF debug merge hash table. */
99 #define coff_debug_merge_hash_lookup(table, string, create, copy) \
100 ((struct coff_debug_merge_hash_entry *) \
101 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
103 /* Information we keep for each section in the output file when doing
104 a relocateable link. */
106 struct coff_link_section_info
108 /* The relocs to be output. */
109 struct internal_reloc
*relocs
;
110 /* For each reloc against a global symbol whose index was not known
111 when the reloc was handled, the global hash table entry. */
112 struct coff_link_hash_entry
**rel_hashes
;
115 /* Information that we pass around while doing the final link step. */
117 struct coff_final_link_info
119 /* General link information. */
120 struct bfd_link_info
*info
;
123 /* Used to indicate failure in traversal routine. */
125 /* Hash table for long symbol names. */
126 struct bfd_strtab_hash
*strtab
;
127 /* When doing a relocateable link, an array of information kept for
128 each output section, indexed by the target_index field. */
129 struct coff_link_section_info
*section_info
;
130 /* Symbol index of last C_FILE symbol (-1 if none). */
131 long last_file_index
;
132 /* Contents of last C_FILE symbol. */
133 struct internal_syment last_file
;
134 /* Hash table used to merge debug information. */
135 struct coff_debug_merge_hash_table debug_merge
;
136 /* Buffer large enough to hold swapped symbols of any input file. */
137 struct internal_syment
*internal_syms
;
138 /* Buffer large enough to hold sections of symbols of any input file. */
140 /* Buffer large enough to hold output indices of symbols of any
143 /* Buffer large enough to hold output symbols for any input file. */
145 /* Buffer large enough to hold external line numbers for any input
148 /* Buffer large enough to hold any input section. */
150 /* Buffer large enough to hold external relocs of any input section. */
151 bfd_byte
*external_relocs
;
152 /* Buffer large enough to hold swapped relocs of any input section. */
153 struct internal_reloc
*internal_relocs
;
156 static struct bfd_hash_entry
*coff_debug_merge_hash_newfunc
157 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
158 static boolean coff_link_add_object_symbols
159 PARAMS ((bfd
*, struct bfd_link_info
*));
160 static boolean coff_link_check_archive_element
161 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
162 static boolean coff_link_check_ar_symbols
163 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
164 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
165 static boolean coff_link_input_bfd
166 PARAMS ((struct coff_final_link_info
*, bfd
*));
167 static boolean coff_write_global_sym
168 PARAMS ((struct coff_link_hash_entry
*, PTR
));
169 static boolean coff_reloc_link_order
170 PARAMS ((bfd
*, struct coff_final_link_info
*, asection
*,
171 struct bfd_link_order
*));
173 /* Create an entry in a COFF linker hash table. */
175 struct bfd_hash_entry
*
176 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
177 struct bfd_hash_entry
*entry
;
178 struct bfd_hash_table
*table
;
181 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
183 /* Allocate the structure if it has not already been allocated by a
185 if (ret
== (struct coff_link_hash_entry
*) NULL
)
186 ret
= ((struct coff_link_hash_entry
*)
187 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
188 if (ret
== (struct coff_link_hash_entry
*) NULL
)
190 bfd_set_error (bfd_error_no_memory
);
191 return (struct bfd_hash_entry
*) ret
;
194 /* Call the allocation method of the superclass. */
195 ret
= ((struct coff_link_hash_entry
*)
196 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
198 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
200 /* Set local fields. */
207 ret
->toc_offset
= 1; /* invalid toc address, sets the high bit */
210 return (struct bfd_hash_entry
*) ret
;
213 /* Initialize a COFF linker hash table. */
216 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
217 struct coff_link_hash_table
*table
;
219 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
220 struct bfd_hash_table
*,
223 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
226 /* Create a COFF linker hash table. */
228 struct bfd_link_hash_table
*
229 _bfd_coff_link_hash_table_create (abfd
)
232 struct coff_link_hash_table
*ret
;
234 ret
= ((struct coff_link_hash_table
*)
235 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
238 bfd_set_error (bfd_error_no_memory
);
241 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
242 _bfd_coff_link_hash_newfunc
))
244 bfd_release (abfd
, ret
);
245 return (struct bfd_link_hash_table
*) NULL
;
250 /* Create an entry in a COFF debug merge hash table. */
252 static struct bfd_hash_entry
*
253 coff_debug_merge_hash_newfunc (entry
, table
, string
)
254 struct bfd_hash_entry
*entry
;
255 struct bfd_hash_table
*table
;
258 struct coff_debug_merge_hash_entry
*ret
=
259 (struct coff_debug_merge_hash_entry
*) entry
;
261 /* Allocate the structure if it has not already been allocated by a
263 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
264 ret
= ((struct coff_debug_merge_hash_entry
*)
265 bfd_hash_allocate (table
,
266 sizeof (struct coff_debug_merge_hash_entry
)));
267 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
269 bfd_set_error (bfd_error_no_memory
);
270 return (struct bfd_hash_entry
*) ret
;
273 /* Call the allocation method of the superclass. */
274 ret
= ((struct coff_debug_merge_hash_entry
*)
275 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
276 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
278 /* Set local fields. */
282 return (struct bfd_hash_entry
*) ret
;
285 /* Given a COFF BFD, add symbols to the global hash table as
289 _bfd_coff_link_add_symbols (abfd
, info
)
291 struct bfd_link_info
*info
;
293 switch (bfd_get_format (abfd
))
296 return coff_link_add_object_symbols (abfd
, info
);
298 return (_bfd_generic_link_add_archive_symbols
299 (abfd
, info
, coff_link_check_archive_element
));
301 bfd_set_error (bfd_error_wrong_format
);
306 /* Add symbols from a COFF object file. */
309 coff_link_add_object_symbols (abfd
, info
)
311 struct bfd_link_info
*info
;
313 if (! _bfd_coff_get_external_symbols (abfd
))
315 if (! coff_link_add_symbols (abfd
, info
))
318 if (! info
->keep_memory
)
320 if (! _bfd_coff_free_symbols (abfd
))
326 /* Check a single archive element to see if we need to include it in
327 the link. *PNEEDED is set according to whether this element is
328 needed in the link or not. This is called via
329 _bfd_generic_link_add_archive_symbols. */
332 coff_link_check_archive_element (abfd
, info
, pneeded
)
334 struct bfd_link_info
*info
;
337 if (! _bfd_coff_get_external_symbols (abfd
))
340 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
345 if (! coff_link_add_symbols (abfd
, info
))
349 if (! info
->keep_memory
|| ! *pneeded
)
351 if (! _bfd_coff_free_symbols (abfd
))
358 /* Look through the symbols to see if this object file should be
359 included in the link. */
362 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
364 struct bfd_link_info
*info
;
367 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
368 bfd_size_type symesz
;
374 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
376 symesz
= bfd_coff_symesz (abfd
);
377 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
378 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
379 while (esym
< esym_end
)
381 struct internal_syment sym
;
383 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
385 if ((sym
.n_sclass
== C_EXT
386 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
387 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
390 char buf
[SYMNMLEN
+ 1];
391 struct bfd_link_hash_entry
*h
;
393 /* This symbol is externally visible, and is defined by this
396 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
399 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
401 /* We are only interested in symbols that are currently
402 undefined. If a symbol is currently known to be common,
403 COFF linkers do not bring in an object file which defines
405 if (h
!= (struct bfd_link_hash_entry
*) NULL
406 && h
->type
== bfd_link_hash_undefined
)
408 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
415 esym
+= (sym
.n_numaux
+ 1) * symesz
;
418 /* We do not need this object file. */
422 /* Add all the symbols from an object file to the hash table. */
425 coff_link_add_symbols (abfd
, info
)
427 struct bfd_link_info
*info
;
429 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
430 boolean default_copy
;
431 bfd_size_type symcount
;
432 struct coff_link_hash_entry
**sym_hash
;
433 bfd_size_type symesz
;
437 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
439 if (info
->keep_memory
)
440 default_copy
= false;
444 symcount
= obj_raw_syment_count (abfd
);
446 /* We keep a list of the linker hash table entries that correspond
447 to particular symbols. */
448 sym_hash
= ((struct coff_link_hash_entry
**)
451 * sizeof (struct coff_link_hash_entry
*))));
452 if (sym_hash
== NULL
&& symcount
!= 0)
454 bfd_set_error (bfd_error_no_memory
);
457 obj_coff_sym_hashes (abfd
) = sym_hash
;
459 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
461 symesz
= bfd_coff_symesz (abfd
);
462 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
463 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
464 esym_end
= esym
+ symcount
* symesz
;
465 while (esym
< esym_end
)
467 struct internal_syment sym
;
470 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
472 if (sym
.n_sclass
== C_EXT
473 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
476 char buf
[SYMNMLEN
+ 1];
481 /* This symbol is externally visible. */
483 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
487 /* We must copy the name into memory if we got it from the
488 syment itself, rather than the string table. */
490 if (sym
._n
._n_n
._n_zeroes
!= 0
491 || sym
._n
._n_n
._n_offset
== 0)
496 if (sym
.n_scnum
== 0)
501 section
= bfd_und_section_ptr
;
506 section
= bfd_com_section_ptr
;
511 flags
= BSF_EXPORT
| BSF_GLOBAL
;
512 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
513 value
-= section
->vma
;
516 if (! (_bfd_generic_link_add_one_symbol
517 (info
, abfd
, name
, flags
, section
, value
,
518 (const char *) NULL
, copy
, false,
519 (struct bfd_link_hash_entry
**) sym_hash
)))
522 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
524 if (((*sym_hash
)->class == C_NULL
525 && (*sym_hash
)->type
== T_NULL
)
528 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
530 (*sym_hash
)->class = sym
.n_sclass
;
531 (*sym_hash
)->type
= sym
.n_type
;
532 (*sym_hash
)->numaux
= sym
.n_numaux
;
533 (*sym_hash
)->auxbfd
= abfd
;
534 (*sym_hash
)->toc_offset
= 1;
535 if (sym
.n_numaux
!= 0)
537 union internal_auxent
*alloc
;
540 union internal_auxent
*iaux
;
542 alloc
= ((union internal_auxent
*)
543 bfd_hash_allocate (&info
->hash
->table
,
545 * sizeof (*alloc
))));
548 bfd_set_error (bfd_error_no_memory
);
551 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
553 i
++, eaux
+= symesz
, iaux
++)
554 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
555 sym
.n_sclass
, i
, sym
.n_numaux
,
557 (*sym_hash
)->aux
= alloc
;
563 esym
+= (sym
.n_numaux
+ 1) * symesz
;
564 sym_hash
+= sym
.n_numaux
+ 1;
570 /* Do the final link step. */
573 _bfd_coff_final_link (abfd
, info
)
575 struct bfd_link_info
*info
;
577 bfd_size_type symesz
;
578 struct coff_final_link_info finfo
;
579 boolean debug_merge_allocated
;
581 struct bfd_link_order
*p
;
582 size_t max_contents_size
;
583 size_t max_sym_count
;
584 size_t max_lineno_count
;
585 size_t max_reloc_count
;
586 size_t max_output_reloc_count
;
587 file_ptr rel_filepos
;
589 file_ptr line_filepos
;
592 bfd_byte
*external_relocs
= NULL
;
593 char strbuf
[STRING_SIZE_SIZE
];
595 symesz
= bfd_coff_symesz (abfd
);
598 finfo
.output_bfd
= abfd
;
600 finfo
.section_info
= NULL
;
601 finfo
.last_file_index
= -1;
602 finfo
.internal_syms
= NULL
;
603 finfo
.sec_ptrs
= NULL
;
604 finfo
.sym_indices
= NULL
;
605 finfo
.outsyms
= NULL
;
606 finfo
.linenos
= NULL
;
607 finfo
.contents
= NULL
;
608 finfo
.external_relocs
= NULL
;
609 finfo
.internal_relocs
= NULL
;
610 debug_merge_allocated
= false;
612 coff_data (abfd
)->link_info
= info
;
614 finfo
.strtab
= _bfd_stringtab_init ();
615 if (finfo
.strtab
== NULL
)
618 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
620 debug_merge_allocated
= true;
622 /* Compute the file positions for all the sections. */
623 if (! abfd
->output_has_begun
)
624 bfd_coff_compute_section_file_positions (abfd
);
626 /* Count the line numbers and relocation entries required for the
627 output file. Set the file positions for the relocs. */
628 rel_filepos
= obj_relocbase (abfd
);
629 relsz
= bfd_coff_relsz (abfd
);
630 max_contents_size
= 0;
631 max_lineno_count
= 0;
633 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
637 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
639 if (p
->type
== bfd_indirect_link_order
)
643 sec
= p
->u
.indirect
.section
;
645 if (info
->strip
== strip_none
646 || info
->strip
== strip_some
)
647 o
->lineno_count
+= sec
->lineno_count
;
649 if (info
->relocateable
)
650 o
->reloc_count
+= sec
->reloc_count
;
652 if (sec
->_raw_size
> max_contents_size
)
653 max_contents_size
= sec
->_raw_size
;
654 if (sec
->lineno_count
> max_lineno_count
)
655 max_lineno_count
= sec
->lineno_count
;
656 if (sec
->reloc_count
> max_reloc_count
)
657 max_reloc_count
= sec
->reloc_count
;
659 else if (info
->relocateable
660 && (p
->type
== bfd_section_reloc_link_order
661 || p
->type
== bfd_symbol_reloc_link_order
))
664 if (o
->reloc_count
== 0)
668 o
->flags
|= SEC_RELOC
;
669 o
->rel_filepos
= rel_filepos
;
670 rel_filepos
+= o
->reloc_count
* relsz
;
674 /* If doing a relocateable link, allocate space for the pointers we
676 if (info
->relocateable
)
680 /* We use section_count + 1, rather than section_count, because
681 the target_index fields are 1 based. */
682 finfo
.section_info
= ((struct coff_link_section_info
*)
683 malloc ((abfd
->section_count
+ 1)
684 * sizeof (struct coff_link_section_info
)));
685 if (finfo
.section_info
== NULL
)
687 bfd_set_error (bfd_error_no_memory
);
690 for (i
= 0; i
<= abfd
->section_count
; i
++)
692 finfo
.section_info
[i
].relocs
= NULL
;
693 finfo
.section_info
[i
].rel_hashes
= NULL
;
697 /* We now know the size of the relocs, so we can determine the file
698 positions of the line numbers. */
699 line_filepos
= rel_filepos
;
700 linesz
= bfd_coff_linesz (abfd
);
701 max_output_reloc_count
= 0;
702 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
704 if (o
->lineno_count
== 0)
708 o
->line_filepos
= line_filepos
;
709 line_filepos
+= o
->lineno_count
* linesz
;
712 if (o
->reloc_count
!= 0)
714 /* We don't know the indices of global symbols until we have
715 written out all the local symbols. For each section in
716 the output file, we keep an array of pointers to hash
717 table entries. Each entry in the array corresponds to a
718 reloc. When we find a reloc against a global symbol, we
719 set the corresponding entry in this array so that we can
720 fix up the symbol index after we have written out all the
723 Because of this problem, we also keep the relocs in
724 memory until the end of the link. This wastes memory,
725 but only when doing a relocateable link, which is not the
727 BFD_ASSERT (info
->relocateable
);
728 finfo
.section_info
[o
->target_index
].relocs
=
729 ((struct internal_reloc
*)
730 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
731 finfo
.section_info
[o
->target_index
].rel_hashes
=
732 ((struct coff_link_hash_entry
**)
733 malloc (o
->reloc_count
734 * sizeof (struct coff_link_hash_entry
*)));
735 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
736 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
738 bfd_set_error (bfd_error_no_memory
);
742 if (o
->reloc_count
> max_output_reloc_count
)
743 max_output_reloc_count
= o
->reloc_count
;
746 /* Reset the reloc and lineno counts, so that we can use them to
747 count the number of entries we have output so far. */
752 obj_sym_filepos (abfd
) = line_filepos
;
754 /* Figure out the largest number of symbols in an input BFD. Take
755 the opportunity to clear the output_has_begun fields of all the
758 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
762 sub
->output_has_begun
= false;
763 sz
= obj_raw_syment_count (sub
);
764 if (sz
> max_sym_count
)
768 /* Allocate some buffers used while linking. */
769 finfo
.internal_syms
= ((struct internal_syment
*)
770 malloc (max_sym_count
771 * sizeof (struct internal_syment
)));
772 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
773 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
774 finfo
.outsyms
= ((bfd_byte
*)
775 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
776 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
777 * bfd_coff_linesz (abfd
));
778 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
779 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
780 if (! info
->relocateable
)
781 finfo
.internal_relocs
= ((struct internal_reloc
*)
782 malloc (max_reloc_count
783 * sizeof (struct internal_reloc
)));
784 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
785 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
786 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
787 || finfo
.outsyms
== NULL
788 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
789 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
790 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
791 || (! info
->relocateable
792 && finfo
.internal_relocs
== NULL
793 && max_reloc_count
> 0))
795 bfd_set_error (bfd_error_no_memory
);
799 /* We now know the position of everything in the file, except that
800 we don't know the size of the symbol table and therefore we don't
801 know where the string table starts. We just build the string
802 table in memory as we go along. We process all the relocations
803 for a single input file at once. */
804 obj_raw_syment_count (abfd
) = 0;
806 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
808 if (! bfd_coff_start_final_link (abfd
, info
))
812 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
814 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
816 if (p
->type
== bfd_indirect_link_order
817 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
818 == bfd_target_coff_flavour
))
820 sub
= p
->u
.indirect
.section
->owner
;
821 if (! sub
->output_has_begun
)
823 if (! coff_link_input_bfd (&finfo
, sub
))
825 sub
->output_has_begun
= true;
828 else if (p
->type
== bfd_section_reloc_link_order
829 || p
->type
== bfd_symbol_reloc_link_order
)
831 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
836 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
842 /* Free up the buffers used by coff_link_input_bfd. */
844 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
845 debug_merge_allocated
= false;
847 if (finfo
.internal_syms
!= NULL
)
849 free (finfo
.internal_syms
);
850 finfo
.internal_syms
= NULL
;
852 if (finfo
.sec_ptrs
!= NULL
)
854 free (finfo
.sec_ptrs
);
855 finfo
.sec_ptrs
= NULL
;
857 if (finfo
.sym_indices
!= NULL
)
859 free (finfo
.sym_indices
);
860 finfo
.sym_indices
= NULL
;
862 if (finfo
.linenos
!= NULL
)
864 free (finfo
.linenos
);
865 finfo
.linenos
= NULL
;
867 if (finfo
.contents
!= NULL
)
869 free (finfo
.contents
);
870 finfo
.contents
= NULL
;
872 if (finfo
.external_relocs
!= NULL
)
874 free (finfo
.external_relocs
);
875 finfo
.external_relocs
= NULL
;
877 if (finfo
.internal_relocs
!= NULL
)
879 free (finfo
.internal_relocs
);
880 finfo
.internal_relocs
= NULL
;
883 /* The value of the last C_FILE symbol is supposed to be the symbol
884 index of the first external symbol. Write it out again if
886 if (finfo
.last_file_index
!= -1
887 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
889 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
890 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
891 (PTR
) finfo
.outsyms
);
893 (obj_sym_filepos (abfd
)
894 + finfo
.last_file_index
* symesz
),
896 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
900 /* Write out the global symbols. */
901 finfo
.failed
= false;
902 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
907 /* The outsyms buffer is used by coff_write_global_sym. */
908 if (finfo
.outsyms
!= NULL
)
910 free (finfo
.outsyms
);
911 finfo
.outsyms
= NULL
;
914 if (info
->relocateable
)
916 /* Now that we have written out all the global symbols, we know
917 the symbol indices to use for relocs against them, and we can
918 finally write out the relocs. */
919 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
920 if (external_relocs
== NULL
)
922 bfd_set_error (bfd_error_no_memory
);
926 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
928 struct internal_reloc
*irel
;
929 struct internal_reloc
*irelend
;
930 struct coff_link_hash_entry
**rel_hash
;
933 if (o
->reloc_count
== 0)
936 irel
= finfo
.section_info
[o
->target_index
].relocs
;
937 irelend
= irel
+ o
->reloc_count
;
938 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
939 erel
= external_relocs
;
940 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
942 if (*rel_hash
!= NULL
)
944 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
945 irel
->r_symndx
= (*rel_hash
)->indx
;
947 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
950 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
951 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
952 abfd
) != relsz
* o
->reloc_count
)
956 free (external_relocs
);
957 external_relocs
= NULL
;
960 /* Free up the section information. */
961 if (finfo
.section_info
!= NULL
)
965 for (i
= 0; i
< abfd
->section_count
; i
++)
967 if (finfo
.section_info
[i
].relocs
!= NULL
)
968 free (finfo
.section_info
[i
].relocs
);
969 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
970 free (finfo
.section_info
[i
].rel_hashes
);
972 free (finfo
.section_info
);
973 finfo
.section_info
= NULL
;
976 /* Write out the string table. */
978 (obj_sym_filepos (abfd
)
979 + obj_raw_syment_count (abfd
) * symesz
),
983 #if STRING_SIZE_SIZE == 4
985 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
986 (bfd_byte
*) strbuf
);
988 #error Change bfd_h_put_32
991 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
994 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
997 _bfd_stringtab_free (finfo
.strtab
);
999 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1000 not try to write out the symbols. */
1001 bfd_get_symcount (abfd
) = 0;
1006 if (debug_merge_allocated
)
1007 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1008 if (finfo
.strtab
!= NULL
)
1009 _bfd_stringtab_free (finfo
.strtab
);
1010 if (finfo
.section_info
!= NULL
)
1014 for (i
= 0; i
< abfd
->section_count
; i
++)
1016 if (finfo
.section_info
[i
].relocs
!= NULL
)
1017 free (finfo
.section_info
[i
].relocs
);
1018 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1019 free (finfo
.section_info
[i
].rel_hashes
);
1021 free (finfo
.section_info
);
1023 if (finfo
.internal_syms
!= NULL
)
1024 free (finfo
.internal_syms
);
1025 if (finfo
.sec_ptrs
!= NULL
)
1026 free (finfo
.sec_ptrs
);
1027 if (finfo
.sym_indices
!= NULL
)
1028 free (finfo
.sym_indices
);
1029 if (finfo
.outsyms
!= NULL
)
1030 free (finfo
.outsyms
);
1031 if (finfo
.linenos
!= NULL
)
1032 free (finfo
.linenos
);
1033 if (finfo
.contents
!= NULL
)
1034 free (finfo
.contents
);
1035 if (finfo
.external_relocs
!= NULL
)
1036 free (finfo
.external_relocs
);
1037 if (finfo
.internal_relocs
!= NULL
)
1038 free (finfo
.internal_relocs
);
1039 if (external_relocs
!= NULL
)
1040 free (external_relocs
);
1044 /* parse out a -heap <reserved>,<commit> line */
1047 dores_com (ptr
, output_bfd
, heap
)
1052 if (coff_data(output_bfd
)->pe
)
1054 int val
= strtoul (ptr
, &ptr
, 0);
1056 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1058 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1062 int val
= strtoul (ptr
+1, &ptr
, 0);
1064 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1066 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1072 static char *get_name(ptr
, dst
)
1079 while (*ptr
&& *ptr
!= ' ')
1085 /* Process any magic embedded commands in a section called .drectve */
1088 process_embedded_commands (output_bfd
, info
, abfd
)
1090 struct bfd_link_info
*info
;
1093 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1100 copy
= malloc ((size_t) sec
->_raw_size
);
1103 bfd_set_error (bfd_error_no_memory
);
1106 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1111 e
= copy
+ sec
->_raw_size
;
1112 for (s
= copy
; s
< e
; )
1118 if (strncmp (s
,"-attr", 5) == 0)
1130 s
= get_name(s
, &name
);
1131 s
= get_name(s
, &attribs
);
1151 asec
= bfd_get_section_by_name (abfd
, name
);
1154 asec
->flags
|= SEC_CODE
;
1156 asec
->flags
|= SEC_READONLY
;
1159 else if (strncmp (s
,"-heap", 5) == 0)
1161 s
= dores_com (s
+5, output_bfd
, 1);
1163 else if (strncmp (s
,"-stack", 6) == 0)
1165 s
= dores_com (s
+6, output_bfd
, 0);
1174 /* Link an input file into the linker output file. This function
1175 handles all the sections and relocations of the input file at once. */
1178 coff_link_input_bfd (finfo
, input_bfd
)
1179 struct coff_final_link_info
*finfo
;
1182 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1183 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1184 asection
*, struct internal_reloc
*,
1187 const char *strings
;
1188 bfd_size_type syment_base
;
1189 unsigned int n_tmask
;
1190 unsigned int n_btshft
;
1192 bfd_size_type isymesz
;
1193 bfd_size_type osymesz
;
1194 bfd_size_type linesz
;
1197 struct internal_syment
*isymp
;
1200 unsigned long output_index
;
1202 struct coff_link_hash_entry
**sym_hash
;
1205 /* Move all the symbols to the output file. */
1207 output_bfd
= finfo
->output_bfd
;
1208 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1210 syment_base
= obj_raw_syment_count (output_bfd
);
1211 isymesz
= bfd_coff_symesz (input_bfd
);
1212 osymesz
= bfd_coff_symesz (output_bfd
);
1213 linesz
= bfd_coff_linesz (input_bfd
);
1214 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1216 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1217 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1219 /* Define macros so that ISFCN, et. al., macros work correctly. */
1220 #define N_TMASK n_tmask
1221 #define N_BTSHFT n_btshft
1224 if (! finfo
->info
->keep_memory
)
1227 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1230 if (! _bfd_coff_get_external_symbols (input_bfd
))
1233 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1234 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1235 isymp
= finfo
->internal_syms
;
1236 secpp
= finfo
->sec_ptrs
;
1237 indexp
= finfo
->sym_indices
;
1238 output_index
= syment_base
;
1239 outsym
= finfo
->outsyms
;
1241 if (coff_data(output_bfd
)->pe
)
1243 if (!process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1247 while (esym
< esym_end
)
1249 struct internal_syment isym
;
1254 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1256 /* Make a copy of *isymp so that the relocate_section function
1257 always sees the original values. This is more reliable than
1258 always recomputing the symbol value even if we are stripping
1262 if (isym
.n_scnum
!= 0)
1263 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1266 if (isym
.n_value
== 0)
1267 *secpp
= bfd_und_section_ptr
;
1269 *secpp
= bfd_com_section_ptr
;
1276 add
= 1 + isym
.n_numaux
;
1278 /* If we are stripping all symbols, we want to skip this one. */
1279 if (finfo
->info
->strip
== strip_all
)
1284 if (isym
.n_sclass
== C_EXT
1285 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1287 /* This is a global symbol. Global symbols come at the
1288 end of the symbol table, so skip them for now.
1289 Function symbols, however, are an exception, and are
1290 not moved to the end. */
1292 if (! ISFCN (isym
.n_type
))
1297 /* This is a local symbol. Skip it if we are discarding
1299 if (finfo
->info
->discard
== discard_all
)
1304 /* If we stripping debugging symbols, and this is a debugging
1305 symbol, then skip it. */
1307 && finfo
->info
->strip
== strip_debugger
1308 && isym
.n_scnum
== N_DEBUG
)
1311 /* If some symbols are stripped based on the name, work out the
1312 name and decide whether to skip this symbol. */
1314 && (finfo
->info
->strip
== strip_some
1315 || finfo
->info
->discard
== discard_l
))
1318 char buf
[SYMNMLEN
+ 1];
1320 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1324 if ((finfo
->info
->strip
== strip_some
1325 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1328 && finfo
->info
->discard
== discard_l
1329 && strncmp (name
, finfo
->info
->lprefix
,
1330 finfo
->info
->lprefix_len
) == 0))
1334 /* If this is an enum, struct, or union tag, see if we have
1335 already output an identical type. */
1337 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1338 && (isym
.n_sclass
== C_ENTAG
1339 || isym
.n_sclass
== C_STRTAG
1340 || isym
.n_sclass
== C_UNTAG
)
1341 && isym
.n_numaux
== 1)
1344 char buf
[SYMNMLEN
+ 1];
1345 struct coff_debug_merge_hash_entry
*mh
;
1346 struct coff_debug_merge_type
*mt
;
1347 union internal_auxent aux
;
1348 struct coff_debug_merge_element
**epp
;
1349 bfd_byte
*esl
, *eslend
;
1350 struct internal_syment
*islp
;
1351 struct coff_debug_merge_type
*mtl
;
1353 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1357 /* Ignore fake names invented by compiler; treat them all as
1359 if (*name
== '~' || *name
== '.'
1360 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1361 && (name
[1] == '~' || name
[1] == '.')))
1364 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1369 /* Allocate memory to hold type information. If this turns
1370 out to be a duplicate, we pass this address to
1372 mt
= ((struct coff_debug_merge_type
*)
1373 bfd_alloc (input_bfd
,
1374 sizeof (struct coff_debug_merge_type
)));
1377 bfd_set_error (bfd_error_no_memory
);
1380 mt
->class = isym
.n_sclass
;
1382 /* Pick up the aux entry, which points to the end of the tag
1384 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1385 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1388 /* Gather the elements. */
1389 epp
= &mt
->elements
;
1390 mt
->elements
= NULL
;
1392 esl
= esym
+ 2 * isymesz
;
1393 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1394 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1395 while (esl
< eslend
)
1397 const char *elename
;
1398 char elebuf
[SYMNMLEN
+ 1];
1401 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1403 *epp
= ((struct coff_debug_merge_element
*)
1404 bfd_alloc (input_bfd
,
1405 sizeof (struct coff_debug_merge_element
)));
1408 bfd_set_error (bfd_error_no_memory
);
1412 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1414 if (elename
== NULL
)
1417 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1420 bfd_set_error (bfd_error_no_memory
);
1423 strcpy (copy
, elename
);
1425 (*epp
)->name
= copy
;
1426 (*epp
)->type
= islp
->n_type
;
1428 if (islp
->n_numaux
>= 1
1429 && islp
->n_type
!= T_NULL
1430 && islp
->n_sclass
!= C_EOS
)
1432 union internal_auxent eleaux
;
1435 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1436 islp
->n_type
, islp
->n_sclass
, 0,
1437 islp
->n_numaux
, (PTR
) &eleaux
);
1438 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1440 /* FIXME: If this tagndx entry refers to a symbol
1441 defined later in this file, we just ignore it.
1442 Handling this correctly would be tedious, and may
1448 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1451 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1452 if ((*epp
)->tagndx
< 0)
1456 epp
= &(*epp
)->next
;
1459 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1460 islp
+= islp
->n_numaux
+ 1;
1463 /* See if we already have a definition which matches this
1465 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1467 struct coff_debug_merge_element
*me
, *mel
;
1469 if (mtl
->class != mt
->class)
1472 for (me
= mt
->elements
, mel
= mtl
->elements
;
1473 me
!= NULL
&& mel
!= NULL
;
1474 me
= me
->next
, mel
= mel
->next
)
1476 if (strcmp (me
->name
, mel
->name
) != 0
1477 || me
->type
!= mel
->type
1478 || me
->tagndx
!= mel
->tagndx
)
1482 if (me
== NULL
&& mel
== NULL
)
1486 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1488 /* This is the first definition of this type. */
1489 mt
->indx
= output_index
;
1490 mt
->next
= mh
->types
;
1495 /* This is a redefinition which can be merged. */
1496 bfd_release (input_bfd
, (PTR
) mt
);
1497 *indexp
= mtl
->indx
;
1498 add
= (eslend
- esym
) / isymesz
;
1503 /* We now know whether we are to skip this symbol or not. */
1506 /* Adjust the symbol in order to output it. */
1508 if (isym
._n
._n_n
._n_zeroes
== 0
1509 && isym
._n
._n_n
._n_offset
!= 0)
1514 /* This symbol has a long name. Enter it in the string
1515 table we are building. Note that we do not check
1516 bfd_coff_symname_in_debug. That is only true for
1517 XCOFF, and XCOFF requires different linking code
1519 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1523 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1524 if (indx
== (bfd_size_type
) -1)
1526 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1529 if (isym
.n_scnum
> 0)
1531 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1532 isym
.n_value
+= ((*secpp
)->output_section
->vma
1533 + (*secpp
)->output_offset
1537 /* The value of a C_FILE symbol is the symbol index of the
1538 next C_FILE symbol. The value of the last C_FILE symbol
1539 is the symbol index to the first external symbol
1540 (actually, coff_renumber_symbols does not get this
1541 right--it just sets the value of the last C_FILE symbol
1542 to zero--and nobody has ever complained about it). We
1543 try to get this right, below, just before we write the
1544 symbols out, but in the general case we may have to write
1545 the symbol out twice. */
1546 if (isym
.n_sclass
== C_FILE
)
1548 if (finfo
->last_file_index
!= -1
1549 && finfo
->last_file
.n_value
!= (long) output_index
)
1551 /* We must correct the value of the last C_FILE entry. */
1552 finfo
->last_file
.n_value
= output_index
;
1553 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1555 /* The last C_FILE symbol is in this input file. */
1556 bfd_coff_swap_sym_out (output_bfd
,
1557 (PTR
) &finfo
->last_file
,
1558 (PTR
) (finfo
->outsyms
1559 + ((finfo
->last_file_index
1565 /* We have already written out the last C_FILE
1566 symbol. We need to write it out again. We
1567 borrow *outsym temporarily. */
1568 bfd_coff_swap_sym_out (output_bfd
,
1569 (PTR
) &finfo
->last_file
,
1571 if (bfd_seek (output_bfd
,
1572 (obj_sym_filepos (output_bfd
)
1573 + finfo
->last_file_index
* osymesz
),
1575 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1581 finfo
->last_file_index
= output_index
;
1582 finfo
->last_file
= isym
;
1585 /* Output the symbol. */
1587 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1589 *indexp
= output_index
;
1594 struct coff_link_hash_entry
*h
;
1596 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1598 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1599 BFD_ASSERT (h
!= NULL
);
1600 h
->indx
= output_index
;
1603 output_index
+= add
;
1604 outsym
+= add
* osymesz
;
1607 esym
+= add
* isymesz
;
1611 for (--add
; add
> 0; --add
)
1618 /* Fix up the aux entries. This must be done in a separate pass,
1619 because we don't know the correct symbol indices until we have
1620 already decided which symbols we are going to keep. */
1622 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1623 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1624 isymp
= finfo
->internal_syms
;
1625 indexp
= finfo
->sym_indices
;
1626 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1627 outsym
= finfo
->outsyms
;
1628 while (esym
< esym_end
)
1632 add
= 1 + isymp
->n_numaux
;
1635 || (bfd_size_type
) *indexp
< syment_base
)
1636 && (*sym_hash
== NULL
1637 || (*sym_hash
)->auxbfd
!= input_bfd
))
1638 esym
+= add
* isymesz
;
1641 struct coff_link_hash_entry
*h
;
1648 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1656 /* Handle the aux entries. This handling is based on
1657 coff_pointerize_aux. I don't know if it always correct. */
1658 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1660 union internal_auxent aux
;
1661 union internal_auxent
*auxp
;
1667 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1668 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1673 if (isymp
->n_sclass
== C_FILE
)
1675 /* If this is a long filename, we must put it in the
1677 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1678 && auxp
->x_file
.x_n
.x_offset
!= 0)
1680 const char *filename
;
1683 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1684 >= STRING_SIZE_SIZE
);
1685 if (strings
== NULL
)
1687 strings
= _bfd_coff_read_string_table (input_bfd
);
1688 if (strings
== NULL
)
1691 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1692 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1694 if (indx
== (bfd_size_type
) -1)
1696 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1699 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1703 if (ISFCN (isymp
->n_type
)
1704 || ISTAG (isymp
->n_sclass
)
1705 || isymp
->n_sclass
== C_BLOCK
)
1707 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1709 && indx
< obj_raw_syment_count (input_bfd
))
1711 /* We look forward through the symbol for
1712 the index of the next symbol we are going
1713 to include. I don't know if this is
1715 while (finfo
->sym_indices
[indx
] < 0
1716 && indx
< obj_raw_syment_count (input_bfd
))
1718 if (indx
>= obj_raw_syment_count (input_bfd
))
1719 indx
= output_index
;
1721 indx
= finfo
->sym_indices
[indx
];
1722 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1726 indx
= auxp
->x_sym
.x_tagndx
.l
;
1727 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1731 symindx
= finfo
->sym_indices
[indx
];
1733 auxp
->x_sym
.x_tagndx
.l
= 0;
1735 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1741 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1742 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1756 /* Relocate the line numbers, unless we are stripping them. */
1757 if (finfo
->info
->strip
== strip_none
1758 || finfo
->info
->strip
== strip_some
)
1760 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1766 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1767 build_link_order in ldwrite.c will not have created a
1768 link order, which means that we will not have seen this
1769 input section in _bfd_coff_final_link, which means that
1770 we will not have allocated space for the line numbers of
1771 this section. I don't think line numbers can be
1772 meaningful for a section which does not have
1773 SEC_HAS_CONTENTS set, but, if they do, this must be
1775 if (o
->lineno_count
== 0
1776 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1779 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1780 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1781 input_bfd
) != linesz
* o
->lineno_count
)
1784 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1785 eline
= finfo
->linenos
;
1786 elineend
= eline
+ linesz
* o
->lineno_count
;
1787 for (; eline
< elineend
; eline
+= linesz
)
1789 struct internal_lineno iline
;
1791 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1793 if (iline
.l_lnno
!= 0)
1794 iline
.l_addr
.l_paddr
+= offset
;
1795 else if (iline
.l_addr
.l_symndx
>= 0
1796 && ((unsigned long) iline
.l_addr
.l_symndx
1797 < obj_raw_syment_count (input_bfd
)))
1801 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1805 /* These line numbers are attached to a symbol
1806 which we are stripping. We should really
1807 just discard the line numbers, but that would
1808 be a pain because we have already counted
1814 struct internal_syment is
;
1815 union internal_auxent ia
;
1817 /* Fix up the lnnoptr field in the aux entry of
1818 the symbol. It turns out that we can't do
1819 this when we modify the symbol aux entries,
1820 because gas sometimes screws up the lnnoptr
1821 field and makes it an offset from the start
1822 of the line numbers rather than an absolute
1824 bfd_coff_swap_sym_in (output_bfd
,
1825 (PTR
) (finfo
->outsyms
1826 + ((indx
- syment_base
)
1829 if ((ISFCN (is
.n_type
)
1830 || is
.n_sclass
== C_BLOCK
)
1831 && is
.n_numaux
>= 1)
1835 auxptr
= (PTR
) (finfo
->outsyms
1836 + ((indx
- syment_base
+ 1)
1838 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1839 is
.n_type
, is
.n_sclass
,
1840 0, is
.n_numaux
, (PTR
) &ia
);
1841 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1842 (o
->output_section
->line_filepos
1843 + o
->output_section
->lineno_count
* linesz
1844 + eline
- finfo
->linenos
);
1845 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1846 is
.n_type
, is
.n_sclass
, 0,
1847 is
.n_numaux
, auxptr
);
1851 iline
.l_addr
.l_symndx
= indx
;
1854 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1857 if (bfd_seek (output_bfd
,
1858 (o
->output_section
->line_filepos
1859 + o
->output_section
->lineno_count
* linesz
),
1861 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1862 output_bfd
) != linesz
* o
->lineno_count
)
1865 o
->output_section
->lineno_count
+= o
->lineno_count
;
1869 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1870 symbol will be the first symbol in the next input file. In the
1871 normal case, this will save us from writing out the C_FILE symbol
1873 if (finfo
->last_file_index
!= -1
1874 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1876 finfo
->last_file
.n_value
= output_index
;
1877 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1878 (PTR
) (finfo
->outsyms
1879 + ((finfo
->last_file_index
- syment_base
)
1883 /* Write the modified symbols to the output file. */
1884 if (outsym
> finfo
->outsyms
)
1886 if (bfd_seek (output_bfd
,
1887 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1889 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1891 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1894 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1895 + (outsym
- finfo
->outsyms
) / osymesz
)
1898 obj_raw_syment_count (output_bfd
) = output_index
;
1901 /* Relocate the contents of each section. */
1902 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1903 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1907 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1909 if ((o
->flags
& SEC_RELOC
) != 0
1910 && o
->reloc_count
!= 0)
1912 ((*_bfd_error_handler
)
1913 ("%s: relocs in section `%s', but it has no contents",
1914 bfd_get_filename (input_bfd
),
1915 bfd_get_section_name (input_bfd
, o
)));
1916 bfd_set_error (bfd_error_no_contents
);
1923 if (coff_section_data (input_bfd
, o
) != NULL
1924 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1925 contents
= coff_section_data (input_bfd
, o
)->contents
;
1928 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1929 (file_ptr
) 0, o
->_raw_size
))
1931 contents
= finfo
->contents
;
1934 if ((o
->flags
& SEC_RELOC
) != 0)
1937 struct internal_reloc
*internal_relocs
;
1938 struct internal_reloc
*irel
;
1940 /* Read in the relocs. */
1941 target_index
= o
->output_section
->target_index
;
1942 internal_relocs
= (_bfd_coff_read_internal_relocs
1943 (input_bfd
, o
, false, finfo
->external_relocs
,
1944 finfo
->info
->relocateable
,
1945 (finfo
->info
->relocateable
1946 ? (finfo
->section_info
[target_index
].relocs
1947 + o
->output_section
->reloc_count
)
1948 : finfo
->internal_relocs
)));
1949 if (internal_relocs
== NULL
)
1952 /* Call processor specific code to relocate the section
1954 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1958 finfo
->internal_syms
,
1962 if (finfo
->info
->relocateable
)
1965 struct internal_reloc
*irelend
;
1966 struct coff_link_hash_entry
**rel_hash
;
1968 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1969 irel
= internal_relocs
;
1970 irelend
= irel
+ o
->reloc_count
;
1971 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1972 + o
->output_section
->reloc_count
);
1973 for (; irel
< irelend
; irel
++, rel_hash
++)
1975 struct coff_link_hash_entry
*h
;
1980 /* Adjust the reloc address and symbol index. */
1982 irel
->r_vaddr
+= offset
;
1984 if (irel
->r_symndx
== -1)
1989 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1997 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2000 /* This is a global symbol. */
2002 irel
->r_symndx
= h
->indx
;
2005 /* This symbol is being written at the end
2006 of the file, and we do not yet know the
2007 symbol index. We save the pointer to the
2008 hash table entry in the rel_hash list.
2009 We set the indx field to -2 to indicate
2010 that this symbol must not be stripped. */
2019 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2021 irel
->r_symndx
= indx
;
2024 struct internal_syment
*is
;
2026 char buf
[SYMNMLEN
+ 1];
2028 /* This reloc is against a symbol we are
2029 stripping. It would be possible to
2030 handle this case, but I don't think it's
2032 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2034 name
= (_bfd_coff_internal_syment_name
2035 (input_bfd
, is
, buf
));
2039 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2040 (finfo
->info
, name
, input_bfd
, o
,
2047 o
->output_section
->reloc_count
+= o
->reloc_count
;
2051 /* Write out the modified section contents. */
2052 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2053 contents
, o
->output_offset
,
2054 (o
->_cooked_size
!= 0
2060 if (! finfo
->info
->keep_memory
)
2062 if (! _bfd_coff_free_symbols (input_bfd
))
2069 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2072 coff_write_global_sym (h
, data
)
2073 struct coff_link_hash_entry
*h
;
2076 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2078 struct internal_syment isym
;
2079 bfd_size_type symesz
;
2082 output_bfd
= finfo
->output_bfd
;
2088 && (finfo
->info
->strip
== strip_all
2089 || (finfo
->info
->strip
== strip_some
2090 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2091 h
->root
.root
.string
, false, false)
2095 switch (h
->root
.type
)
2098 case bfd_link_hash_new
:
2102 case bfd_link_hash_undefined
:
2103 case bfd_link_hash_undefweak
:
2104 isym
.n_scnum
= N_UNDEF
;
2108 case bfd_link_hash_defined
:
2109 case bfd_link_hash_defweak
:
2113 sec
= h
->root
.u
.def
.section
->output_section
;
2114 if (bfd_is_abs_section (sec
))
2115 isym
.n_scnum
= N_ABS
;
2117 isym
.n_scnum
= sec
->target_index
;
2118 isym
.n_value
= (h
->root
.u
.def
.value
2120 + h
->root
.u
.def
.section
->output_offset
);
2124 case bfd_link_hash_common
:
2125 isym
.n_scnum
= N_UNDEF
;
2126 isym
.n_value
= h
->root
.u
.c
.size
;
2129 case bfd_link_hash_indirect
:
2130 case bfd_link_hash_warning
:
2131 /* Just ignore these. They can't be handled anyhow. */
2135 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2136 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2143 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2145 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2147 if (indx
== (bfd_size_type
) -1)
2149 finfo
->failed
= true;
2152 isym
._n
._n_n
._n_zeroes
= 0;
2153 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2156 isym
.n_sclass
= h
->class;
2157 isym
.n_type
= h
->type
;
2159 if (isym
.n_sclass
== C_NULL
)
2160 isym
.n_sclass
= C_EXT
;
2162 isym
.n_numaux
= h
->numaux
;
2164 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2166 symesz
= bfd_coff_symesz (output_bfd
);
2168 if (bfd_seek (output_bfd
,
2169 (obj_sym_filepos (output_bfd
)
2170 + obj_raw_syment_count (output_bfd
) * symesz
),
2172 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2174 finfo
->failed
= true;
2178 h
->indx
= obj_raw_syment_count (output_bfd
);
2180 ++obj_raw_syment_count (output_bfd
);
2182 /* Write out any associated aux entries. There normally will be
2183 none. If there are any, I have no idea how to modify them. */
2184 for (i
= 0; i
< isym
.n_numaux
; i
++)
2186 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2187 isym
.n_sclass
, i
, isym
.n_numaux
,
2188 (PTR
) finfo
->outsyms
);
2189 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2191 finfo
->failed
= true;
2194 ++obj_raw_syment_count (output_bfd
);
2200 /* Handle a link order which is supposed to generate a reloc. */
2203 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2205 struct coff_final_link_info
*finfo
;
2206 asection
*output_section
;
2207 struct bfd_link_order
*link_order
;
2209 reloc_howto_type
*howto
;
2210 struct internal_reloc
*irel
;
2211 struct coff_link_hash_entry
**rel_hash_ptr
;
2213 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2216 bfd_set_error (bfd_error_bad_value
);
2220 if (link_order
->u
.reloc
.p
->addend
!= 0)
2224 bfd_reloc_status_type rstat
;
2227 size
= bfd_get_reloc_size (howto
);
2228 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2231 bfd_set_error (bfd_error_no_memory
);
2235 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2236 link_order
->u
.reloc
.p
->addend
, buf
);
2242 case bfd_reloc_outofrange
:
2244 case bfd_reloc_overflow
:
2245 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2247 (link_order
->type
== bfd_section_reloc_link_order
2248 ? bfd_section_name (output_bfd
,
2249 link_order
->u
.reloc
.p
->u
.section
)
2250 : link_order
->u
.reloc
.p
->u
.name
),
2251 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2252 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2259 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2260 (file_ptr
) link_order
->offset
, size
);
2266 /* Store the reloc information in the right place. It will get
2267 swapped and written out at the end of the final_link routine. */
2269 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2270 + output_section
->reloc_count
);
2271 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2272 + output_section
->reloc_count
);
2274 memset (irel
, 0, sizeof (struct internal_reloc
));
2275 *rel_hash_ptr
= NULL
;
2277 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2279 if (link_order
->type
== bfd_section_reloc_link_order
)
2281 /* We need to somehow locate a symbol in the right section. The
2282 symbol must either have a value of zero, or we must adjust
2283 the addend by the value of the symbol. FIXME: Write this
2284 when we need it. The old linker couldn't handle this anyhow. */
2286 *rel_hash_ptr
= NULL
;
2291 struct coff_link_hash_entry
*h
;
2293 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2294 link_order
->u
.reloc
.p
->u
.name
,
2295 false, false, true);
2299 irel
->r_symndx
= h
->indx
;
2302 /* Set the index to -2 to force this symbol to get
2311 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2312 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2313 (asection
*) NULL
, (bfd_vma
) 0)))
2319 /* FIXME: Is this always right? */
2320 irel
->r_type
= howto
->type
;
2322 /* r_size is only used on the RS/6000, which needs its own linker
2323 routines anyhow. r_extern is only used for ECOFF. */
2325 /* FIXME: What is the right value for r_offset? Is zero OK? */
2327 ++output_section
->reloc_count
;
2332 /* A basic reloc handling routine which may be used by processors with
2336 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2337 input_section
, contents
, relocs
, syms
,
2340 struct bfd_link_info
*info
;
2342 asection
*input_section
;
2344 struct internal_reloc
*relocs
;
2345 struct internal_syment
*syms
;
2346 asection
**sections
;
2348 struct internal_reloc
*rel
;
2349 struct internal_reloc
*relend
;
2352 relend
= rel
+ input_section
->reloc_count
;
2353 for (; rel
< relend
; rel
++)
2356 struct coff_link_hash_entry
*h
;
2357 struct internal_syment
*sym
;
2360 reloc_howto_type
*howto
;
2361 bfd_reloc_status_type rstat
;
2363 symndx
= rel
->r_symndx
;
2372 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2373 sym
= syms
+ symndx
;
2376 /* COFF treats common symbols in one of two ways. Either the
2377 size of the symbol is included in the section contents, or it
2378 is not. We assume that the size is not included, and force
2379 the rtype_to_howto function to adjust the addend as needed. */
2381 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2382 addend
= - sym
->n_value
;
2387 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2400 sec
= bfd_abs_section_ptr
;
2405 sec
= sections
[symndx
];
2406 val
= (sec
->output_section
->vma
2407 + sec
->output_offset
2414 if (h
->root
.type
== bfd_link_hash_defined
2415 || h
->root
.type
== bfd_link_hash_defweak
)
2419 sec
= h
->root
.u
.def
.section
;
2420 val
= (h
->root
.u
.def
.value
2421 + sec
->output_section
->vma
2422 + sec
->output_offset
);
2425 else if (! info
->relocateable
)
2427 if (! ((*info
->callbacks
->undefined_symbol
)
2428 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2429 rel
->r_vaddr
- input_section
->vma
)))
2434 if (info
->base_file
)
2436 /* Emit a reloc if the backend thinks it needs it. */
2437 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2439 /* relocation to a symbol in a section which
2440 isn't absolute - we output the address here
2442 bfd_vma addr
= rel
->r_vaddr
2443 - input_section
->vma
2444 + input_section
->output_offset
2445 + input_section
->output_section
->vma
;
2446 if (coff_data(output_bfd
)->pe
)
2447 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2448 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2452 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2454 rel
->r_vaddr
- input_section
->vma
,
2463 case bfd_reloc_overflow
:
2466 char buf
[SYMNMLEN
+ 1];
2471 name
= h
->root
.root
.string
;
2474 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2479 if (! ((*info
->callbacks
->reloc_overflow
)
2480 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2481 input_section
, rel
->r_vaddr
- input_section
->vma
)))