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. */
209 return (struct bfd_hash_entry
*) ret
;
212 /* Initialize a COFF linker hash table. */
215 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
216 struct coff_link_hash_table
*table
;
218 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
219 struct bfd_hash_table
*,
222 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
225 /* Create a COFF linker hash table. */
227 struct bfd_link_hash_table
*
228 _bfd_coff_link_hash_table_create (abfd
)
231 struct coff_link_hash_table
*ret
;
233 ret
= ((struct coff_link_hash_table
*)
234 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
237 bfd_set_error (bfd_error_no_memory
);
240 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
241 _bfd_coff_link_hash_newfunc
))
243 bfd_release (abfd
, ret
);
244 return (struct bfd_link_hash_table
*) NULL
;
249 /* Create an entry in a COFF debug merge hash table. */
251 static struct bfd_hash_entry
*
252 coff_debug_merge_hash_newfunc (entry
, table
, string
)
253 struct bfd_hash_entry
*entry
;
254 struct bfd_hash_table
*table
;
257 struct coff_debug_merge_hash_entry
*ret
=
258 (struct coff_debug_merge_hash_entry
*) entry
;
260 /* Allocate the structure if it has not already been allocated by a
262 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
263 ret
= ((struct coff_debug_merge_hash_entry
*)
264 bfd_hash_allocate (table
,
265 sizeof (struct coff_debug_merge_hash_entry
)));
266 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
268 bfd_set_error (bfd_error_no_memory
);
269 return (struct bfd_hash_entry
*) ret
;
272 /* Call the allocation method of the superclass. */
273 ret
= ((struct coff_debug_merge_hash_entry
*)
274 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
275 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
277 /* Set local fields. */
281 return (struct bfd_hash_entry
*) ret
;
284 /* Given a COFF BFD, add symbols to the global hash table as
288 _bfd_coff_link_add_symbols (abfd
, info
)
290 struct bfd_link_info
*info
;
292 switch (bfd_get_format (abfd
))
295 return coff_link_add_object_symbols (abfd
, info
);
297 return (_bfd_generic_link_add_archive_symbols
298 (abfd
, info
, coff_link_check_archive_element
));
300 bfd_set_error (bfd_error_wrong_format
);
305 /* Add symbols from a COFF object file. */
308 coff_link_add_object_symbols (abfd
, info
)
310 struct bfd_link_info
*info
;
312 if (! _bfd_coff_get_external_symbols (abfd
))
314 if (! coff_link_add_symbols (abfd
, info
))
316 if (! info
->keep_memory
)
318 if (! _bfd_coff_free_symbols (abfd
))
324 /* Check a single archive element to see if we need to include it in
325 the link. *PNEEDED is set according to whether this element is
326 needed in the link or not. This is called via
327 _bfd_generic_link_add_archive_symbols. */
330 coff_link_check_archive_element (abfd
, info
, pneeded
)
332 struct bfd_link_info
*info
;
335 if (! _bfd_coff_get_external_symbols (abfd
))
338 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
343 if (! coff_link_add_symbols (abfd
, info
))
347 if (! info
->keep_memory
|| ! *pneeded
)
349 if (! _bfd_coff_free_symbols (abfd
))
356 /* Look through the symbols to see if this object file should be
357 included in the link. */
360 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
362 struct bfd_link_info
*info
;
365 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
366 bfd_size_type symesz
;
372 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
374 symesz
= bfd_coff_symesz (abfd
);
375 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
376 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
377 while (esym
< esym_end
)
379 struct internal_syment sym
;
381 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
383 if ((sym
.n_sclass
== C_EXT
384 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
385 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
388 char buf
[SYMNMLEN
+ 1];
389 struct bfd_link_hash_entry
*h
;
391 /* This symbol is externally visible, and is defined by this
394 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
397 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
399 /* We are only interested in symbols that are currently
400 undefined. If a symbol is currently known to be common,
401 COFF linkers do not bring in an object file which defines
403 if (h
!= (struct bfd_link_hash_entry
*) NULL
404 && h
->type
== bfd_link_hash_undefined
)
406 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
413 esym
+= (sym
.n_numaux
+ 1) * symesz
;
416 /* We do not need this object file. */
420 /* Add all the symbols from an object file to the hash table. */
423 coff_link_add_symbols (abfd
, info
)
425 struct bfd_link_info
*info
;
427 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
428 boolean default_copy
;
429 bfd_size_type symcount
;
430 struct coff_link_hash_entry
**sym_hash
;
431 bfd_size_type symesz
;
435 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
437 if (info
->keep_memory
)
438 default_copy
= false;
442 symcount
= obj_raw_syment_count (abfd
);
444 /* We keep a list of the linker hash table entries that correspond
445 to particular symbols. */
446 sym_hash
= ((struct coff_link_hash_entry
**)
449 * sizeof (struct coff_link_hash_entry
*))));
450 if (sym_hash
== NULL
&& symcount
!= 0)
452 bfd_set_error (bfd_error_no_memory
);
455 obj_coff_sym_hashes (abfd
) = sym_hash
;
457 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
459 symesz
= bfd_coff_symesz (abfd
);
460 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
461 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
462 esym_end
= esym
+ symcount
* symesz
;
463 while (esym
< esym_end
)
465 struct internal_syment sym
;
468 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
470 if (sym
.n_sclass
== C_EXT
471 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
474 char buf
[SYMNMLEN
+ 1];
479 /* This symbol is externally visible. */
481 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
485 /* We must copy the name into memory if we got it from the
486 syment itself, rather than the string table. */
488 if (sym
._n
._n_n
._n_zeroes
!= 0
489 || sym
._n
._n_n
._n_offset
== 0)
494 if (sym
.n_scnum
== 0)
499 section
= bfd_und_section_ptr
;
504 section
= bfd_com_section_ptr
;
509 flags
= BSF_EXPORT
| BSF_GLOBAL
;
510 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
511 value
-= section
->vma
;
514 if (! (_bfd_generic_link_add_one_symbol
515 (info
, abfd
, name
, flags
, section
, value
,
516 (const char *) NULL
, copy
, false,
517 (struct bfd_link_hash_entry
**) sym_hash
)))
520 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
522 if (((*sym_hash
)->class == C_NULL
523 && (*sym_hash
)->type
== T_NULL
)
526 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
528 (*sym_hash
)->class = sym
.n_sclass
;
529 (*sym_hash
)->type
= sym
.n_type
;
530 (*sym_hash
)->numaux
= sym
.n_numaux
;
531 (*sym_hash
)->auxbfd
= abfd
;
532 if (sym
.n_numaux
!= 0)
534 union internal_auxent
*alloc
;
537 union internal_auxent
*iaux
;
539 alloc
= ((union internal_auxent
*)
540 bfd_hash_allocate (&info
->hash
->table
,
542 * sizeof (*alloc
))));
545 bfd_set_error (bfd_error_no_memory
);
548 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
550 i
++, eaux
+= symesz
, iaux
++)
551 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
552 sym
.n_sclass
, i
, sym
.n_numaux
,
554 (*sym_hash
)->aux
= alloc
;
560 esym
+= (sym
.n_numaux
+ 1) * symesz
;
561 sym_hash
+= sym
.n_numaux
+ 1;
567 /* Do the final link step. */
570 _bfd_coff_final_link (abfd
, info
)
572 struct bfd_link_info
*info
;
574 bfd_size_type symesz
;
575 struct coff_final_link_info finfo
;
576 boolean debug_merge_allocated
;
578 struct bfd_link_order
*p
;
579 size_t max_contents_size
;
580 size_t max_sym_count
;
581 size_t max_lineno_count
;
582 size_t max_reloc_count
;
583 size_t max_output_reloc_count
;
584 file_ptr rel_filepos
;
586 file_ptr line_filepos
;
589 bfd_byte
*external_relocs
= NULL
;
590 char strbuf
[STRING_SIZE_SIZE
];
592 symesz
= bfd_coff_symesz (abfd
);
595 finfo
.output_bfd
= abfd
;
597 finfo
.section_info
= NULL
;
598 finfo
.last_file_index
= -1;
599 finfo
.internal_syms
= NULL
;
600 finfo
.sec_ptrs
= NULL
;
601 finfo
.sym_indices
= NULL
;
602 finfo
.outsyms
= NULL
;
603 finfo
.linenos
= NULL
;
604 finfo
.contents
= NULL
;
605 finfo
.external_relocs
= NULL
;
606 finfo
.internal_relocs
= NULL
;
607 debug_merge_allocated
= false;
609 coff_data (abfd
)->link_info
= info
;
611 finfo
.strtab
= _bfd_stringtab_init ();
612 if (finfo
.strtab
== NULL
)
615 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
617 debug_merge_allocated
= true;
619 /* Compute the file positions for all the sections. */
620 if (! abfd
->output_has_begun
)
621 bfd_coff_compute_section_file_positions (abfd
);
623 /* Count the line numbers and relocation entries required for the
624 output file. Set the file positions for the relocs. */
625 rel_filepos
= obj_relocbase (abfd
);
626 relsz
= bfd_coff_relsz (abfd
);
627 max_contents_size
= 0;
628 max_lineno_count
= 0;
630 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
634 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
636 if (p
->type
== bfd_indirect_link_order
)
640 sec
= p
->u
.indirect
.section
;
642 if (info
->strip
== strip_none
643 || info
->strip
== strip_some
)
644 o
->lineno_count
+= sec
->lineno_count
;
646 if (info
->relocateable
)
647 o
->reloc_count
+= sec
->reloc_count
;
649 if (sec
->_raw_size
> max_contents_size
)
650 max_contents_size
= sec
->_raw_size
;
651 if (sec
->lineno_count
> max_lineno_count
)
652 max_lineno_count
= sec
->lineno_count
;
653 if (sec
->reloc_count
> max_reloc_count
)
654 max_reloc_count
= sec
->reloc_count
;
656 else if (info
->relocateable
657 && (p
->type
== bfd_section_reloc_link_order
658 || p
->type
== bfd_symbol_reloc_link_order
))
661 if (o
->reloc_count
== 0)
665 o
->flags
|= SEC_RELOC
;
666 o
->rel_filepos
= rel_filepos
;
667 rel_filepos
+= o
->reloc_count
* relsz
;
671 /* If doing a relocateable link, allocate space for the pointers we
673 if (info
->relocateable
)
677 /* We use section_count + 1, rather than section_count, because
678 the target_index fields are 1 based. */
679 finfo
.section_info
= ((struct coff_link_section_info
*)
680 malloc ((abfd
->section_count
+ 1)
681 * sizeof (struct coff_link_section_info
)));
682 if (finfo
.section_info
== NULL
)
684 bfd_set_error (bfd_error_no_memory
);
687 for (i
= 0; i
<= abfd
->section_count
; i
++)
689 finfo
.section_info
[i
].relocs
= NULL
;
690 finfo
.section_info
[i
].rel_hashes
= NULL
;
694 /* We now know the size of the relocs, so we can determine the file
695 positions of the line numbers. */
696 line_filepos
= rel_filepos
;
697 linesz
= bfd_coff_linesz (abfd
);
698 max_output_reloc_count
= 0;
699 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
701 if (o
->lineno_count
== 0)
705 o
->line_filepos
= line_filepos
;
706 line_filepos
+= o
->lineno_count
* linesz
;
709 if (o
->reloc_count
!= 0)
711 /* We don't know the indices of global symbols until we have
712 written out all the local symbols. For each section in
713 the output file, we keep an array of pointers to hash
714 table entries. Each entry in the array corresponds to a
715 reloc. When we find a reloc against a global symbol, we
716 set the corresponding entry in this array so that we can
717 fix up the symbol index after we have written out all the
720 Because of this problem, we also keep the relocs in
721 memory until the end of the link. This wastes memory,
722 but only when doing a relocateable link, which is not the
724 BFD_ASSERT (info
->relocateable
);
725 finfo
.section_info
[o
->target_index
].relocs
=
726 ((struct internal_reloc
*)
727 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
728 finfo
.section_info
[o
->target_index
].rel_hashes
=
729 ((struct coff_link_hash_entry
**)
730 malloc (o
->reloc_count
731 * sizeof (struct coff_link_hash_entry
*)));
732 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
733 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
735 bfd_set_error (bfd_error_no_memory
);
739 if (o
->reloc_count
> max_output_reloc_count
)
740 max_output_reloc_count
= o
->reloc_count
;
743 /* Reset the reloc and lineno counts, so that we can use them to
744 count the number of entries we have output so far. */
749 obj_sym_filepos (abfd
) = line_filepos
;
751 /* Figure out the largest number of symbols in an input BFD. Take
752 the opportunity to clear the output_has_begun fields of all the
755 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
759 sub
->output_has_begun
= false;
760 sz
= obj_raw_syment_count (sub
);
761 if (sz
> max_sym_count
)
765 /* Allocate some buffers used while linking. */
766 finfo
.internal_syms
= ((struct internal_syment
*)
767 malloc (max_sym_count
768 * sizeof (struct internal_syment
)));
769 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
770 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
771 finfo
.outsyms
= ((bfd_byte
*)
772 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
773 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
774 * bfd_coff_linesz (abfd
));
775 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
776 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
777 if (! info
->relocateable
)
778 finfo
.internal_relocs
= ((struct internal_reloc
*)
779 malloc (max_reloc_count
780 * sizeof (struct internal_reloc
)));
781 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
782 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
783 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
784 || finfo
.outsyms
== NULL
785 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
786 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
787 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
788 || (! info
->relocateable
789 && finfo
.internal_relocs
== NULL
790 && max_reloc_count
> 0))
792 bfd_set_error (bfd_error_no_memory
);
796 /* We now know the position of everything in the file, except that
797 we don't know the size of the symbol table and therefore we don't
798 know where the string table starts. We just build the string
799 table in memory as we go along. We process all the relocations
800 for a single input file at once. */
801 obj_raw_syment_count (abfd
) = 0;
803 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
805 if (! bfd_coff_start_final_link (abfd
, info
))
809 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
811 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
813 if (p
->type
== bfd_indirect_link_order
814 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
815 == bfd_target_coff_flavour
))
817 sub
= p
->u
.indirect
.section
->owner
;
818 if (! sub
->output_has_begun
)
820 if (! coff_link_input_bfd (&finfo
, sub
))
822 sub
->output_has_begun
= true;
825 else if (p
->type
== bfd_section_reloc_link_order
826 || p
->type
== bfd_symbol_reloc_link_order
)
828 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
833 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
839 /* Free up the buffers used by coff_link_input_bfd. */
841 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
842 debug_merge_allocated
= false;
844 if (finfo
.internal_syms
!= NULL
)
846 free (finfo
.internal_syms
);
847 finfo
.internal_syms
= NULL
;
849 if (finfo
.sec_ptrs
!= NULL
)
851 free (finfo
.sec_ptrs
);
852 finfo
.sec_ptrs
= NULL
;
854 if (finfo
.sym_indices
!= NULL
)
856 free (finfo
.sym_indices
);
857 finfo
.sym_indices
= NULL
;
859 if (finfo
.linenos
!= NULL
)
861 free (finfo
.linenos
);
862 finfo
.linenos
= NULL
;
864 if (finfo
.contents
!= NULL
)
866 free (finfo
.contents
);
867 finfo
.contents
= NULL
;
869 if (finfo
.external_relocs
!= NULL
)
871 free (finfo
.external_relocs
);
872 finfo
.external_relocs
= NULL
;
874 if (finfo
.internal_relocs
!= NULL
)
876 free (finfo
.internal_relocs
);
877 finfo
.internal_relocs
= NULL
;
880 /* The value of the last C_FILE symbol is supposed to be the symbol
881 index of the first external symbol. Write it out again if
883 if (finfo
.last_file_index
!= -1
884 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
886 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
887 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
888 (PTR
) finfo
.outsyms
);
890 (obj_sym_filepos (abfd
)
891 + finfo
.last_file_index
* symesz
),
893 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
897 /* Write out the global symbols. */
898 finfo
.failed
= false;
899 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
904 /* The outsyms buffer is used by coff_write_global_sym. */
905 if (finfo
.outsyms
!= NULL
)
907 free (finfo
.outsyms
);
908 finfo
.outsyms
= NULL
;
911 if (info
->relocateable
)
913 /* Now that we have written out all the global symbols, we know
914 the symbol indices to use for relocs against them, and we can
915 finally write out the relocs. */
916 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
917 if (external_relocs
== NULL
)
919 bfd_set_error (bfd_error_no_memory
);
923 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
925 struct internal_reloc
*irel
;
926 struct internal_reloc
*irelend
;
927 struct coff_link_hash_entry
**rel_hash
;
930 if (o
->reloc_count
== 0)
933 irel
= finfo
.section_info
[o
->target_index
].relocs
;
934 irelend
= irel
+ o
->reloc_count
;
935 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
936 erel
= external_relocs
;
937 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
939 if (*rel_hash
!= NULL
)
941 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
942 irel
->r_symndx
= (*rel_hash
)->indx
;
944 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
947 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
948 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
949 abfd
) != relsz
* o
->reloc_count
)
953 free (external_relocs
);
954 external_relocs
= NULL
;
957 /* Free up the section information. */
958 if (finfo
.section_info
!= NULL
)
962 for (i
= 0; i
< abfd
->section_count
; i
++)
964 if (finfo
.section_info
[i
].relocs
!= NULL
)
965 free (finfo
.section_info
[i
].relocs
);
966 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
967 free (finfo
.section_info
[i
].rel_hashes
);
969 free (finfo
.section_info
);
970 finfo
.section_info
= NULL
;
973 /* Write out the string table. */
975 (obj_sym_filepos (abfd
)
976 + obj_raw_syment_count (abfd
) * symesz
),
980 #if STRING_SIZE_SIZE == 4
982 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
983 (bfd_byte
*) strbuf
);
985 #error Change bfd_h_put_32
988 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
991 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
994 _bfd_stringtab_free (finfo
.strtab
);
996 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
997 not try to write out the symbols. */
998 bfd_get_symcount (abfd
) = 0;
1003 if (debug_merge_allocated
)
1004 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1005 if (finfo
.strtab
!= NULL
)
1006 _bfd_stringtab_free (finfo
.strtab
);
1007 if (finfo
.section_info
!= NULL
)
1011 for (i
= 0; i
< abfd
->section_count
; i
++)
1013 if (finfo
.section_info
[i
].relocs
!= NULL
)
1014 free (finfo
.section_info
[i
].relocs
);
1015 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1016 free (finfo
.section_info
[i
].rel_hashes
);
1018 free (finfo
.section_info
);
1020 if (finfo
.internal_syms
!= NULL
)
1021 free (finfo
.internal_syms
);
1022 if (finfo
.sec_ptrs
!= NULL
)
1023 free (finfo
.sec_ptrs
);
1024 if (finfo
.sym_indices
!= NULL
)
1025 free (finfo
.sym_indices
);
1026 if (finfo
.outsyms
!= NULL
)
1027 free (finfo
.outsyms
);
1028 if (finfo
.linenos
!= NULL
)
1029 free (finfo
.linenos
);
1030 if (finfo
.contents
!= NULL
)
1031 free (finfo
.contents
);
1032 if (finfo
.external_relocs
!= NULL
)
1033 free (finfo
.external_relocs
);
1034 if (finfo
.internal_relocs
!= NULL
)
1035 free (finfo
.internal_relocs
);
1036 if (external_relocs
!= NULL
)
1037 free (external_relocs
);
1041 /* parse out a -heap <reserved>,<commit> line */
1044 dores_com (ptr
, output_bfd
, heap
)
1049 if (coff_data(output_bfd
)->pe
)
1051 int val
= strtoul (ptr
, &ptr
, 0);
1053 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1055 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1059 int val
= strtoul (ptr
+1, &ptr
, 0);
1061 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1063 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1069 static char *get_name(ptr
, dst
)
1076 while (*ptr
&& *ptr
!= ' ')
1082 /* Process any magic embedded commands in a section called .drectve */
1085 process_embedded_commands (output_bfd
, info
, abfd
)
1087 struct bfd_link_info
*info
;
1090 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1097 copy
= malloc ((size_t) sec
->_raw_size
);
1100 bfd_set_error (bfd_error_no_memory
);
1103 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1108 e
= copy
+ sec
->_raw_size
;
1109 for (s
= copy
; s
< e
; )
1115 if (strncmp (s
,"-attr", 5) == 0)
1127 s
= get_name(s
, &name
);
1128 s
= get_name(s
, &attribs
);
1148 asec
= bfd_get_section_by_name (abfd
, name
);
1151 asec
->flags
|= SEC_CODE
;
1153 asec
->flags
|= SEC_READONLY
;
1156 else if (strncmp (s
,"-heap", 5) == 0)
1158 s
= dores_com (s
+5, output_bfd
, 1);
1160 else if (strncmp (s
,"-stack", 6) == 0)
1162 s
= dores_com (s
+6, output_bfd
, 0);
1171 /* Link an input file into the linker output file. This function
1172 handles all the sections and relocations of the input file at once. */
1175 coff_link_input_bfd (finfo
, input_bfd
)
1176 struct coff_final_link_info
*finfo
;
1179 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1180 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1181 asection
*, struct internal_reloc
*,
1184 const char *strings
;
1185 bfd_size_type syment_base
;
1186 unsigned int n_tmask
;
1187 unsigned int n_btshft
;
1189 bfd_size_type isymesz
;
1190 bfd_size_type osymesz
;
1191 bfd_size_type linesz
;
1194 struct internal_syment
*isymp
;
1197 unsigned long output_index
;
1199 struct coff_link_hash_entry
**sym_hash
;
1202 /* Move all the symbols to the output file. */
1204 output_bfd
= finfo
->output_bfd
;
1205 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1207 syment_base
= obj_raw_syment_count (output_bfd
);
1208 isymesz
= bfd_coff_symesz (input_bfd
);
1209 osymesz
= bfd_coff_symesz (output_bfd
);
1210 linesz
= bfd_coff_linesz (input_bfd
);
1211 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1213 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1214 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1216 /* Define macros so that ISFCN, et. al., macros work correctly. */
1217 #define N_TMASK n_tmask
1218 #define N_BTSHFT n_btshft
1221 if (! finfo
->info
->keep_memory
)
1224 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1227 if (! _bfd_coff_get_external_symbols (input_bfd
))
1230 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1231 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1232 isymp
= finfo
->internal_syms
;
1233 secpp
= finfo
->sec_ptrs
;
1234 indexp
= finfo
->sym_indices
;
1235 output_index
= syment_base
;
1236 outsym
= finfo
->outsyms
;
1238 if (coff_data(output_bfd
)->pe
)
1240 if (!process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1244 while (esym
< esym_end
)
1246 struct internal_syment isym
;
1251 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1253 /* Make a copy of *isymp so that the relocate_section function
1254 always sees the original values. This is more reliable than
1255 always recomputing the symbol value even if we are stripping
1259 if (isym
.n_scnum
!= 0)
1260 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1263 if (isym
.n_value
== 0)
1264 *secpp
= bfd_und_section_ptr
;
1266 *secpp
= bfd_com_section_ptr
;
1273 add
= 1 + isym
.n_numaux
;
1275 /* If we are stripping all symbols, we want to skip this one. */
1276 if (finfo
->info
->strip
== strip_all
)
1281 if (isym
.n_sclass
== C_EXT
1282 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1284 /* This is a global symbol. Global symbols come at the
1285 end of the symbol table, so skip them for now.
1286 Function symbols, however, are an exception, and are
1287 not moved to the end. */
1289 if (! ISFCN (isym
.n_type
))
1294 /* This is a local symbol. Skip it if we are discarding
1296 if (finfo
->info
->discard
== discard_all
)
1301 /* If we stripping debugging symbols, and this is a debugging
1302 symbol, then skip it. */
1304 && finfo
->info
->strip
== strip_debugger
1305 && isym
.n_scnum
== N_DEBUG
)
1308 /* If some symbols are stripped based on the name, work out the
1309 name and decide whether to skip this symbol. */
1311 && (finfo
->info
->strip
== strip_some
1312 || finfo
->info
->discard
== discard_l
))
1315 char buf
[SYMNMLEN
+ 1];
1317 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1321 if ((finfo
->info
->strip
== strip_some
1322 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1325 && finfo
->info
->discard
== discard_l
1326 && strncmp (name
, finfo
->info
->lprefix
,
1327 finfo
->info
->lprefix_len
) == 0))
1331 /* If this is an enum, struct, or union tag, see if we have
1332 already output an identical type. */
1334 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1335 && (isym
.n_sclass
== C_ENTAG
1336 || isym
.n_sclass
== C_STRTAG
1337 || isym
.n_sclass
== C_UNTAG
)
1338 && isym
.n_numaux
== 1)
1341 char buf
[SYMNMLEN
+ 1];
1342 struct coff_debug_merge_hash_entry
*mh
;
1343 struct coff_debug_merge_type
*mt
;
1344 union internal_auxent aux
;
1345 struct coff_debug_merge_element
**epp
;
1346 bfd_byte
*esl
, *eslend
;
1347 struct internal_syment
*islp
;
1348 struct coff_debug_merge_type
*mtl
;
1350 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1354 /* Ignore fake names invented by compiler; treat them all as
1356 if (*name
== '~' || *name
== '.'
1357 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1358 && (name
[1] == '~' || name
[1] == '.')))
1361 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1366 /* Allocate memory to hold type information. If this turns
1367 out to be a duplicate, we pass this address to
1369 mt
= ((struct coff_debug_merge_type
*)
1370 bfd_alloc (input_bfd
,
1371 sizeof (struct coff_debug_merge_type
)));
1374 bfd_set_error (bfd_error_no_memory
);
1377 mt
->class = isym
.n_sclass
;
1379 /* Pick up the aux entry, which points to the end of the tag
1381 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1382 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1385 /* Gather the elements. */
1386 epp
= &mt
->elements
;
1387 mt
->elements
= NULL
;
1389 esl
= esym
+ 2 * isymesz
;
1390 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1391 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1392 while (esl
< eslend
)
1394 const char *elename
;
1395 char elebuf
[SYMNMLEN
+ 1];
1398 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1400 *epp
= ((struct coff_debug_merge_element
*)
1401 bfd_alloc (input_bfd
,
1402 sizeof (struct coff_debug_merge_element
)));
1405 bfd_set_error (bfd_error_no_memory
);
1409 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1411 if (elename
== NULL
)
1414 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1417 bfd_set_error (bfd_error_no_memory
);
1420 strcpy (copy
, elename
);
1422 (*epp
)->name
= copy
;
1423 (*epp
)->type
= islp
->n_type
;
1425 if (islp
->n_numaux
>= 1
1426 && islp
->n_type
!= T_NULL
1427 && islp
->n_sclass
!= C_EOS
)
1429 union internal_auxent eleaux
;
1432 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1433 islp
->n_type
, islp
->n_sclass
, 0,
1434 islp
->n_numaux
, (PTR
) &eleaux
);
1435 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1437 /* FIXME: If this tagndx entry refers to a symbol
1438 defined later in this file, we just ignore it.
1439 Handling this correctly would be tedious, and may
1445 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1448 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1449 if ((*epp
)->tagndx
< 0)
1453 epp
= &(*epp
)->next
;
1456 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1457 islp
+= islp
->n_numaux
+ 1;
1460 /* See if we already have a definition which matches this
1462 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1464 struct coff_debug_merge_element
*me
, *mel
;
1466 if (mtl
->class != mt
->class)
1469 for (me
= mt
->elements
, mel
= mtl
->elements
;
1470 me
!= NULL
&& mel
!= NULL
;
1471 me
= me
->next
, mel
= mel
->next
)
1473 if (strcmp (me
->name
, mel
->name
) != 0
1474 || me
->type
!= mel
->type
1475 || me
->tagndx
!= mel
->tagndx
)
1479 if (me
== NULL
&& mel
== NULL
)
1483 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1485 /* This is the first definition of this type. */
1486 mt
->indx
= output_index
;
1487 mt
->next
= mh
->types
;
1492 /* This is a redefinition which can be merged. */
1493 bfd_release (input_bfd
, (PTR
) mt
);
1494 *indexp
= mtl
->indx
;
1495 add
= (eslend
- esym
) / isymesz
;
1500 /* We now know whether we are to skip this symbol or not. */
1503 /* Adjust the symbol in order to output it. */
1505 if (isym
._n
._n_n
._n_zeroes
== 0
1506 && isym
._n
._n_n
._n_offset
!= 0)
1511 /* This symbol has a long name. Enter it in the string
1512 table we are building. Note that we do not check
1513 bfd_coff_symname_in_debug. That is only true for
1514 XCOFF, and XCOFF requires different linking code
1516 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1520 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1521 if (indx
== (bfd_size_type
) -1)
1523 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1526 if (isym
.n_scnum
> 0)
1528 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1529 isym
.n_value
+= ((*secpp
)->output_section
->vma
1530 + (*secpp
)->output_offset
1534 /* The value of a C_FILE symbol is the symbol index of the
1535 next C_FILE symbol. The value of the last C_FILE symbol
1536 is the symbol index to the first external symbol
1537 (actually, coff_renumber_symbols does not get this
1538 right--it just sets the value of the last C_FILE symbol
1539 to zero--and nobody has ever complained about it). We
1540 try to get this right, below, just before we write the
1541 symbols out, but in the general case we may have to write
1542 the symbol out twice. */
1543 if (isym
.n_sclass
== C_FILE
)
1545 if (finfo
->last_file_index
!= -1
1546 && finfo
->last_file
.n_value
!= (long) output_index
)
1548 /* We must correct the value of the last C_FILE entry. */
1549 finfo
->last_file
.n_value
= output_index
;
1550 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1552 /* The last C_FILE symbol is in this input file. */
1553 bfd_coff_swap_sym_out (output_bfd
,
1554 (PTR
) &finfo
->last_file
,
1555 (PTR
) (finfo
->outsyms
1556 + ((finfo
->last_file_index
1562 /* We have already written out the last C_FILE
1563 symbol. We need to write it out again. We
1564 borrow *outsym temporarily. */
1565 bfd_coff_swap_sym_out (output_bfd
,
1566 (PTR
) &finfo
->last_file
,
1568 if (bfd_seek (output_bfd
,
1569 (obj_sym_filepos (output_bfd
)
1570 + finfo
->last_file_index
* osymesz
),
1572 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1578 finfo
->last_file_index
= output_index
;
1579 finfo
->last_file
= isym
;
1582 /* Output the symbol. */
1584 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1586 *indexp
= output_index
;
1591 struct coff_link_hash_entry
*h
;
1593 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1595 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1596 BFD_ASSERT (h
!= NULL
);
1597 h
->indx
= output_index
;
1600 output_index
+= add
;
1601 outsym
+= add
* osymesz
;
1604 esym
+= add
* isymesz
;
1608 for (--add
; add
> 0; --add
)
1615 /* Fix up the aux entries. This must be done in a separate pass,
1616 because we don't know the correct symbol indices until we have
1617 already decided which symbols we are going to keep. */
1619 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1620 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1621 isymp
= finfo
->internal_syms
;
1622 indexp
= finfo
->sym_indices
;
1623 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1624 outsym
= finfo
->outsyms
;
1625 while (esym
< esym_end
)
1629 add
= 1 + isymp
->n_numaux
;
1632 || (bfd_size_type
) *indexp
< syment_base
)
1633 && (*sym_hash
== NULL
1634 || (*sym_hash
)->auxbfd
!= input_bfd
))
1635 esym
+= add
* isymesz
;
1638 struct coff_link_hash_entry
*h
;
1645 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1653 /* Handle the aux entries. This handling is based on
1654 coff_pointerize_aux. I don't know if it always correct. */
1655 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1657 union internal_auxent aux
;
1658 union internal_auxent
*auxp
;
1664 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1665 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1670 if (isymp
->n_sclass
== C_FILE
)
1672 /* If this is a long filename, we must put it in the
1674 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1675 && auxp
->x_file
.x_n
.x_offset
!= 0)
1677 const char *filename
;
1680 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1681 >= STRING_SIZE_SIZE
);
1682 if (strings
== NULL
)
1684 strings
= _bfd_coff_read_string_table (input_bfd
);
1685 if (strings
== NULL
)
1688 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1689 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1691 if (indx
== (bfd_size_type
) -1)
1693 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1696 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1700 if (ISFCN (isymp
->n_type
)
1701 || ISTAG (isymp
->n_sclass
)
1702 || isymp
->n_sclass
== C_BLOCK
)
1704 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1706 && indx
< obj_raw_syment_count (input_bfd
))
1708 /* We look forward through the symbol for
1709 the index of the next symbol we are going
1710 to include. I don't know if this is
1712 while (finfo
->sym_indices
[indx
] < 0
1713 && indx
< obj_raw_syment_count (input_bfd
))
1715 if (indx
>= obj_raw_syment_count (input_bfd
))
1716 indx
= output_index
;
1718 indx
= finfo
->sym_indices
[indx
];
1719 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1723 indx
= auxp
->x_sym
.x_tagndx
.l
;
1724 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1728 symindx
= finfo
->sym_indices
[indx
];
1730 auxp
->x_sym
.x_tagndx
.l
= 0;
1732 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1738 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1739 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1753 /* Relocate the line numbers, unless we are stripping them. */
1754 if (finfo
->info
->strip
== strip_none
1755 || finfo
->info
->strip
== strip_some
)
1757 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1763 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1764 build_link_order in ldwrite.c will not have created a
1765 link order, which means that we will not have seen this
1766 input section in _bfd_coff_final_link, which means that
1767 we will not have allocated space for the line numbers of
1768 this section. I don't think line numbers can be
1769 meaningful for a section which does not have
1770 SEC_HAS_CONTENTS set, but, if they do, this must be
1772 if (o
->lineno_count
== 0
1773 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1776 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1777 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1778 input_bfd
) != linesz
* o
->lineno_count
)
1781 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1782 eline
= finfo
->linenos
;
1783 elineend
= eline
+ linesz
* o
->lineno_count
;
1784 for (; eline
< elineend
; eline
+= linesz
)
1786 struct internal_lineno iline
;
1788 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1790 if (iline
.l_lnno
!= 0)
1791 iline
.l_addr
.l_paddr
+= offset
;
1792 else if (iline
.l_addr
.l_symndx
>= 0
1793 && ((unsigned long) iline
.l_addr
.l_symndx
1794 < obj_raw_syment_count (input_bfd
)))
1798 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1802 /* These line numbers are attached to a symbol
1803 which we are stripping. We should really
1804 just discard the line numbers, but that would
1805 be a pain because we have already counted
1811 struct internal_syment is
;
1812 union internal_auxent ia
;
1814 /* Fix up the lnnoptr field in the aux entry of
1815 the symbol. It turns out that we can't do
1816 this when we modify the symbol aux entries,
1817 because gas sometimes screws up the lnnoptr
1818 field and makes it an offset from the start
1819 of the line numbers rather than an absolute
1821 bfd_coff_swap_sym_in (output_bfd
,
1822 (PTR
) (finfo
->outsyms
1823 + ((indx
- syment_base
)
1826 if ((ISFCN (is
.n_type
)
1827 || is
.n_sclass
== C_BLOCK
)
1828 && is
.n_numaux
>= 1)
1832 auxptr
= (PTR
) (finfo
->outsyms
1833 + ((indx
- syment_base
+ 1)
1835 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1836 is
.n_type
, is
.n_sclass
,
1837 0, is
.n_numaux
, (PTR
) &ia
);
1838 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1839 (o
->output_section
->line_filepos
1840 + o
->output_section
->lineno_count
* linesz
1841 + eline
- finfo
->linenos
);
1842 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1843 is
.n_type
, is
.n_sclass
, 0,
1844 is
.n_numaux
, auxptr
);
1848 iline
.l_addr
.l_symndx
= indx
;
1851 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1854 if (bfd_seek (output_bfd
,
1855 (o
->output_section
->line_filepos
1856 + o
->output_section
->lineno_count
* linesz
),
1858 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1859 output_bfd
) != linesz
* o
->lineno_count
)
1862 o
->output_section
->lineno_count
+= o
->lineno_count
;
1866 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1867 symbol will be the first symbol in the next input file. In the
1868 normal case, this will save us from writing out the C_FILE symbol
1870 if (finfo
->last_file_index
!= -1
1871 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1873 finfo
->last_file
.n_value
= output_index
;
1874 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1875 (PTR
) (finfo
->outsyms
1876 + ((finfo
->last_file_index
- syment_base
)
1880 /* Write the modified symbols to the output file. */
1881 if (outsym
> finfo
->outsyms
)
1883 if (bfd_seek (output_bfd
,
1884 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1886 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1888 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1891 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1892 + (outsym
- finfo
->outsyms
) / osymesz
)
1895 obj_raw_syment_count (output_bfd
) = output_index
;
1898 /* Relocate the contents of each section. */
1899 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1900 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1904 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1906 if ((o
->flags
& SEC_RELOC
) != 0
1907 && o
->reloc_count
!= 0)
1909 ((*_bfd_error_handler
)
1910 ("%s: relocs in section `%s', but it has no contents",
1911 bfd_get_filename (input_bfd
),
1912 bfd_get_section_name (input_bfd
, o
)));
1913 bfd_set_error (bfd_error_no_contents
);
1920 if (coff_section_data (input_bfd
, o
) != NULL
1921 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1922 contents
= coff_section_data (input_bfd
, o
)->contents
;
1925 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1926 (file_ptr
) 0, o
->_raw_size
))
1928 contents
= finfo
->contents
;
1931 if ((o
->flags
& SEC_RELOC
) != 0)
1934 struct internal_reloc
*internal_relocs
;
1935 struct internal_reloc
*irel
;
1937 /* Read in the relocs. */
1938 target_index
= o
->output_section
->target_index
;
1939 internal_relocs
= (_bfd_coff_read_internal_relocs
1940 (input_bfd
, o
, false, finfo
->external_relocs
,
1941 finfo
->info
->relocateable
,
1942 (finfo
->info
->relocateable
1943 ? (finfo
->section_info
[target_index
].relocs
1944 + o
->output_section
->reloc_count
)
1945 : finfo
->internal_relocs
)));
1946 if (internal_relocs
== NULL
)
1949 /* Call processor specific code to relocate the section
1951 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1955 finfo
->internal_syms
,
1959 if (finfo
->info
->relocateable
)
1962 struct internal_reloc
*irelend
;
1963 struct coff_link_hash_entry
**rel_hash
;
1965 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1966 irel
= internal_relocs
;
1967 irelend
= irel
+ o
->reloc_count
;
1968 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1969 + o
->output_section
->reloc_count
);
1970 for (; irel
< irelend
; irel
++, rel_hash
++)
1972 struct coff_link_hash_entry
*h
;
1977 /* Adjust the reloc address and symbol index. */
1979 irel
->r_vaddr
+= offset
;
1981 if (irel
->r_symndx
== -1)
1986 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1994 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1997 /* This is a global symbol. */
1999 irel
->r_symndx
= h
->indx
;
2002 /* This symbol is being written at the end
2003 of the file, and we do not yet know the
2004 symbol index. We save the pointer to the
2005 hash table entry in the rel_hash list.
2006 We set the indx field to -2 to indicate
2007 that this symbol must not be stripped. */
2016 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2018 irel
->r_symndx
= indx
;
2021 struct internal_syment
*is
;
2023 char buf
[SYMNMLEN
+ 1];
2025 /* This reloc is against a symbol we are
2026 stripping. It would be possible to
2027 handle this case, but I don't think it's
2029 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2031 name
= (_bfd_coff_internal_syment_name
2032 (input_bfd
, is
, buf
));
2036 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2037 (finfo
->info
, name
, input_bfd
, o
,
2044 o
->output_section
->reloc_count
+= o
->reloc_count
;
2048 /* Write out the modified section contents. */
2049 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2050 contents
, o
->output_offset
,
2051 (o
->_cooked_size
!= 0
2057 if (! finfo
->info
->keep_memory
)
2059 if (! _bfd_coff_free_symbols (input_bfd
))
2066 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2069 coff_write_global_sym (h
, data
)
2070 struct coff_link_hash_entry
*h
;
2073 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2075 struct internal_syment isym
;
2076 bfd_size_type symesz
;
2079 output_bfd
= finfo
->output_bfd
;
2085 && (finfo
->info
->strip
== strip_all
2086 || (finfo
->info
->strip
== strip_some
2087 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2088 h
->root
.root
.string
, false, false)
2092 switch (h
->root
.type
)
2095 case bfd_link_hash_new
:
2099 case bfd_link_hash_undefined
:
2100 case bfd_link_hash_undefweak
:
2101 isym
.n_scnum
= N_UNDEF
;
2105 case bfd_link_hash_defined
:
2106 case bfd_link_hash_defweak
:
2110 sec
= h
->root
.u
.def
.section
->output_section
;
2111 if (bfd_is_abs_section (sec
))
2112 isym
.n_scnum
= N_ABS
;
2114 isym
.n_scnum
= sec
->target_index
;
2115 isym
.n_value
= (h
->root
.u
.def
.value
2117 + h
->root
.u
.def
.section
->output_offset
);
2121 case bfd_link_hash_common
:
2122 isym
.n_scnum
= N_UNDEF
;
2123 isym
.n_value
= h
->root
.u
.c
.size
;
2126 case bfd_link_hash_indirect
:
2127 case bfd_link_hash_warning
:
2128 /* Just ignore these. They can't be handled anyhow. */
2132 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2133 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2140 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2142 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2144 if (indx
== (bfd_size_type
) -1)
2146 finfo
->failed
= true;
2149 isym
._n
._n_n
._n_zeroes
= 0;
2150 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2153 isym
.n_sclass
= h
->class;
2154 isym
.n_type
= h
->type
;
2156 if (isym
.n_sclass
== C_NULL
)
2157 isym
.n_sclass
= C_EXT
;
2159 isym
.n_numaux
= h
->numaux
;
2161 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2163 symesz
= bfd_coff_symesz (output_bfd
);
2165 if (bfd_seek (output_bfd
,
2166 (obj_sym_filepos (output_bfd
)
2167 + obj_raw_syment_count (output_bfd
) * symesz
),
2169 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2171 finfo
->failed
= true;
2175 h
->indx
= obj_raw_syment_count (output_bfd
);
2177 ++obj_raw_syment_count (output_bfd
);
2179 /* Write out any associated aux entries. There normally will be
2180 none. If there are any, I have no idea how to modify them. */
2181 for (i
= 0; i
< isym
.n_numaux
; i
++)
2183 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2184 isym
.n_sclass
, i
, isym
.n_numaux
,
2185 (PTR
) finfo
->outsyms
);
2186 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2188 finfo
->failed
= true;
2191 ++obj_raw_syment_count (output_bfd
);
2197 /* Handle a link order which is supposed to generate a reloc. */
2200 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2202 struct coff_final_link_info
*finfo
;
2203 asection
*output_section
;
2204 struct bfd_link_order
*link_order
;
2206 reloc_howto_type
*howto
;
2207 struct internal_reloc
*irel
;
2208 struct coff_link_hash_entry
**rel_hash_ptr
;
2210 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2213 bfd_set_error (bfd_error_bad_value
);
2217 if (link_order
->u
.reloc
.p
->addend
!= 0)
2221 bfd_reloc_status_type rstat
;
2224 size
= bfd_get_reloc_size (howto
);
2225 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2228 bfd_set_error (bfd_error_no_memory
);
2232 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2233 link_order
->u
.reloc
.p
->addend
, buf
);
2239 case bfd_reloc_outofrange
:
2241 case bfd_reloc_overflow
:
2242 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2244 (link_order
->type
== bfd_section_reloc_link_order
2245 ? bfd_section_name (output_bfd
,
2246 link_order
->u
.reloc
.p
->u
.section
)
2247 : link_order
->u
.reloc
.p
->u
.name
),
2248 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2249 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2256 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2257 (file_ptr
) link_order
->offset
, size
);
2263 /* Store the reloc information in the right place. It will get
2264 swapped and written out at the end of the final_link routine. */
2266 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2267 + output_section
->reloc_count
);
2268 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2269 + output_section
->reloc_count
);
2271 memset (irel
, 0, sizeof (struct internal_reloc
));
2272 *rel_hash_ptr
= NULL
;
2274 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2276 if (link_order
->type
== bfd_section_reloc_link_order
)
2278 /* We need to somehow locate a symbol in the right section. The
2279 symbol must either have a value of zero, or we must adjust
2280 the addend by the value of the symbol. FIXME: Write this
2281 when we need it. The old linker couldn't handle this anyhow. */
2283 *rel_hash_ptr
= NULL
;
2288 struct coff_link_hash_entry
*h
;
2290 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2291 link_order
->u
.reloc
.p
->u
.name
,
2292 false, false, true);
2296 irel
->r_symndx
= h
->indx
;
2299 /* Set the index to -2 to force this symbol to get
2308 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2309 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2310 (asection
*) NULL
, (bfd_vma
) 0)))
2316 /* FIXME: Is this always right? */
2317 irel
->r_type
= howto
->type
;
2319 /* r_size is only used on the RS/6000, which needs its own linker
2320 routines anyhow. r_extern is only used for ECOFF. */
2322 /* FIXME: What is the right value for r_offset? Is zero OK? */
2324 ++output_section
->reloc_count
;
2329 /* A basic reloc handling routine which may be used by processors with
2333 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2334 input_section
, contents
, relocs
, syms
,
2337 struct bfd_link_info
*info
;
2339 asection
*input_section
;
2341 struct internal_reloc
*relocs
;
2342 struct internal_syment
*syms
;
2343 asection
**sections
;
2345 struct internal_reloc
*rel
;
2346 struct internal_reloc
*relend
;
2350 relend
= rel
+ input_section
->reloc_count
;
2351 for (; rel
< relend
; rel
++)
2354 struct coff_link_hash_entry
*h
;
2355 struct internal_syment
*sym
;
2358 reloc_howto_type
*howto
;
2359 bfd_reloc_status_type rstat
;
2361 symndx
= rel
->r_symndx
;
2370 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2371 sym
= syms
+ symndx
;
2374 /* COFF treats common symbols in one of two ways. Either the
2375 size of the symbol is included in the section contents, or it
2376 is not. We assume that the size is not included, and force
2377 the rtype_to_howto function to adjust the addend as needed. */
2379 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2380 addend
= - sym
->n_value
;
2385 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2398 sec
= bfd_abs_section_ptr
;
2403 sec
= sections
[symndx
];
2404 val
= (sec
->output_section
->vma
2405 + sec
->output_offset
2412 if (h
->root
.type
== bfd_link_hash_defined
2413 || h
->root
.type
== bfd_link_hash_defweak
)
2417 sec
= h
->root
.u
.def
.section
;
2418 val
= (h
->root
.u
.def
.value
2419 + sec
->output_section
->vma
2420 + sec
->output_offset
);
2423 else if (! info
->relocateable
)
2425 if (! ((*info
->callbacks
->undefined_symbol
)
2426 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2427 rel
->r_vaddr
- input_section
->vma
)))
2432 if (info
->base_file
)
2434 /* Emit a reloc if the backend thinks it needs it. */
2435 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2437 /* relocation to a symbol in a section which
2438 isn't absolute - we output the address here
2440 bfd_vma addr
= rel
->r_vaddr
2441 - input_section
->vma
2442 + input_section
->output_offset
2443 + input_section
->output_section
->vma
;
2444 if (coff_data(output_bfd
)->pe
)
2445 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2446 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2450 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2452 rel
->r_vaddr
- input_section
->vma
,
2461 case bfd_reloc_overflow
:
2464 char buf
[SYMNMLEN
+ 1];
2469 name
= h
->root
.root
.string
;
2472 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2477 if (! ((*info
->callbacks
->reloc_overflow
)
2478 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2479 input_section
, rel
->r_vaddr
- input_section
->vma
)))