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
)
189 return (struct bfd_hash_entry
*) ret
;
191 /* Call the allocation method of the superclass. */
192 ret
= ((struct coff_link_hash_entry
*)
193 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
195 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
197 /* Set local fields. */
206 return (struct bfd_hash_entry
*) ret
;
209 /* Initialize a COFF linker hash table. */
212 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
213 struct coff_link_hash_table
*table
;
215 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
216 struct bfd_hash_table
*,
219 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
222 /* Create a COFF linker hash table. */
224 struct bfd_link_hash_table
*
225 _bfd_coff_link_hash_table_create (abfd
)
228 struct coff_link_hash_table
*ret
;
230 ret
= ((struct coff_link_hash_table
*)
231 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
234 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
235 _bfd_coff_link_hash_newfunc
))
237 bfd_release (abfd
, ret
);
238 return (struct bfd_link_hash_table
*) NULL
;
243 /* Create an entry in a COFF debug merge hash table. */
245 static struct bfd_hash_entry
*
246 coff_debug_merge_hash_newfunc (entry
, table
, string
)
247 struct bfd_hash_entry
*entry
;
248 struct bfd_hash_table
*table
;
251 struct coff_debug_merge_hash_entry
*ret
=
252 (struct coff_debug_merge_hash_entry
*) entry
;
254 /* Allocate the structure if it has not already been allocated by a
256 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
257 ret
= ((struct coff_debug_merge_hash_entry
*)
258 bfd_hash_allocate (table
,
259 sizeof (struct coff_debug_merge_hash_entry
)));
260 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
261 return (struct bfd_hash_entry
*) ret
;
263 /* Call the allocation method of the superclass. */
264 ret
= ((struct coff_debug_merge_hash_entry
*)
265 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
266 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
268 /* Set local fields. */
272 return (struct bfd_hash_entry
*) ret
;
275 /* Given a COFF BFD, add symbols to the global hash table as
279 _bfd_coff_link_add_symbols (abfd
, info
)
281 struct bfd_link_info
*info
;
283 switch (bfd_get_format (abfd
))
286 return coff_link_add_object_symbols (abfd
, info
);
288 return (_bfd_generic_link_add_archive_symbols
289 (abfd
, info
, coff_link_check_archive_element
));
291 bfd_set_error (bfd_error_wrong_format
);
296 /* Add symbols from a COFF object file. */
299 coff_link_add_object_symbols (abfd
, info
)
301 struct bfd_link_info
*info
;
303 if (! _bfd_coff_get_external_symbols (abfd
))
305 if (! coff_link_add_symbols (abfd
, info
))
308 if (! info
->keep_memory
)
310 if (! _bfd_coff_free_symbols (abfd
))
316 /* Check a single archive element to see if we need to include it in
317 the link. *PNEEDED is set according to whether this element is
318 needed in the link or not. This is called via
319 _bfd_generic_link_add_archive_symbols. */
322 coff_link_check_archive_element (abfd
, info
, pneeded
)
324 struct bfd_link_info
*info
;
327 if (! _bfd_coff_get_external_symbols (abfd
))
330 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
335 if (! coff_link_add_symbols (abfd
, info
))
339 if (! info
->keep_memory
|| ! *pneeded
)
341 if (! _bfd_coff_free_symbols (abfd
))
348 /* Look through the symbols to see if this object file should be
349 included in the link. */
352 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
354 struct bfd_link_info
*info
;
357 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
358 bfd_size_type symesz
;
364 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
366 symesz
= bfd_coff_symesz (abfd
);
367 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
368 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
369 while (esym
< esym_end
)
371 struct internal_syment sym
;
373 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
375 if ((sym
.n_sclass
== C_EXT
376 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
377 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
380 char buf
[SYMNMLEN
+ 1];
381 struct bfd_link_hash_entry
*h
;
383 /* This symbol is externally visible, and is defined by this
386 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
389 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
391 /* We are only interested in symbols that are currently
392 undefined. If a symbol is currently known to be common,
393 COFF linkers do not bring in an object file which defines
395 if (h
!= (struct bfd_link_hash_entry
*) NULL
396 && h
->type
== bfd_link_hash_undefined
)
398 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
405 esym
+= (sym
.n_numaux
+ 1) * symesz
;
408 /* We do not need this object file. */
412 /* Add all the symbols from an object file to the hash table. */
415 coff_link_add_symbols (abfd
, info
)
417 struct bfd_link_info
*info
;
419 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
420 boolean default_copy
;
421 bfd_size_type symcount
;
422 struct coff_link_hash_entry
**sym_hash
;
423 bfd_size_type symesz
;
427 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
429 if (info
->keep_memory
)
430 default_copy
= false;
434 symcount
= obj_raw_syment_count (abfd
);
436 /* We keep a list of the linker hash table entries that correspond
437 to particular symbols. */
438 sym_hash
= ((struct coff_link_hash_entry
**)
441 * sizeof (struct coff_link_hash_entry
*))));
442 if (sym_hash
== NULL
&& symcount
!= 0)
444 obj_coff_sym_hashes (abfd
) = sym_hash
;
446 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
448 symesz
= bfd_coff_symesz (abfd
);
449 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
450 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
451 esym_end
= esym
+ symcount
* symesz
;
452 while (esym
< esym_end
)
454 struct internal_syment sym
;
457 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
459 if (sym
.n_sclass
== C_EXT
460 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
463 char buf
[SYMNMLEN
+ 1];
468 /* This symbol is externally visible. */
470 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
474 /* We must copy the name into memory if we got it from the
475 syment itself, rather than the string table. */
477 if (sym
._n
._n_n
._n_zeroes
!= 0
478 || sym
._n
._n_n
._n_offset
== 0)
483 if (sym
.n_scnum
== 0)
488 section
= bfd_und_section_ptr
;
493 section
= bfd_com_section_ptr
;
498 flags
= BSF_EXPORT
| BSF_GLOBAL
;
499 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
500 value
-= section
->vma
;
503 if (! (_bfd_generic_link_add_one_symbol
504 (info
, abfd
, name
, flags
, section
, value
,
505 (const char *) NULL
, copy
, false,
506 (struct bfd_link_hash_entry
**) sym_hash
)))
509 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
511 if (((*sym_hash
)->class == C_NULL
512 && (*sym_hash
)->type
== T_NULL
)
515 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
517 (*sym_hash
)->class = sym
.n_sclass
;
518 (*sym_hash
)->type
= sym
.n_type
;
519 (*sym_hash
)->numaux
= sym
.n_numaux
;
520 (*sym_hash
)->auxbfd
= abfd
;
521 if (sym
.n_numaux
!= 0)
523 union internal_auxent
*alloc
;
526 union internal_auxent
*iaux
;
528 alloc
= ((union internal_auxent
*)
529 bfd_hash_allocate (&info
->hash
->table
,
531 * sizeof (*alloc
))));
534 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
536 i
++, eaux
+= symesz
, iaux
++)
537 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
538 sym
.n_sclass
, i
, sym
.n_numaux
,
540 (*sym_hash
)->aux
= alloc
;
546 esym
+= (sym
.n_numaux
+ 1) * symesz
;
547 sym_hash
+= sym
.n_numaux
+ 1;
553 /* Do the final link step. */
556 _bfd_coff_final_link (abfd
, info
)
558 struct bfd_link_info
*info
;
560 bfd_size_type symesz
;
561 struct coff_final_link_info finfo
;
562 boolean debug_merge_allocated
;
564 struct bfd_link_order
*p
;
565 size_t max_contents_size
;
566 size_t max_sym_count
;
567 size_t max_lineno_count
;
568 size_t max_reloc_count
;
569 size_t max_output_reloc_count
;
570 file_ptr rel_filepos
;
572 file_ptr line_filepos
;
575 bfd_byte
*external_relocs
= NULL
;
576 char strbuf
[STRING_SIZE_SIZE
];
578 symesz
= bfd_coff_symesz (abfd
);
581 finfo
.output_bfd
= abfd
;
583 finfo
.section_info
= NULL
;
584 finfo
.last_file_index
= -1;
585 finfo
.internal_syms
= NULL
;
586 finfo
.sec_ptrs
= NULL
;
587 finfo
.sym_indices
= NULL
;
588 finfo
.outsyms
= NULL
;
589 finfo
.linenos
= NULL
;
590 finfo
.contents
= NULL
;
591 finfo
.external_relocs
= NULL
;
592 finfo
.internal_relocs
= NULL
;
593 debug_merge_allocated
= false;
595 coff_data (abfd
)->link_info
= info
;
597 finfo
.strtab
= _bfd_stringtab_init ();
598 if (finfo
.strtab
== NULL
)
601 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
603 debug_merge_allocated
= true;
605 /* Compute the file positions for all the sections. */
606 if (! abfd
->output_has_begun
)
607 bfd_coff_compute_section_file_positions (abfd
);
609 /* Count the line numbers and relocation entries required for the
610 output file. Set the file positions for the relocs. */
611 rel_filepos
= obj_relocbase (abfd
);
612 relsz
= bfd_coff_relsz (abfd
);
613 max_contents_size
= 0;
614 max_lineno_count
= 0;
616 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
620 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
622 if (p
->type
== bfd_indirect_link_order
)
626 sec
= p
->u
.indirect
.section
;
628 if (info
->strip
== strip_none
629 || info
->strip
== strip_some
)
630 o
->lineno_count
+= sec
->lineno_count
;
632 if (info
->relocateable
)
633 o
->reloc_count
+= sec
->reloc_count
;
635 if (sec
->_raw_size
> max_contents_size
)
636 max_contents_size
= sec
->_raw_size
;
637 if (sec
->lineno_count
> max_lineno_count
)
638 max_lineno_count
= sec
->lineno_count
;
639 if (sec
->reloc_count
> max_reloc_count
)
640 max_reloc_count
= sec
->reloc_count
;
642 else if (info
->relocateable
643 && (p
->type
== bfd_section_reloc_link_order
644 || p
->type
== bfd_symbol_reloc_link_order
))
647 if (o
->reloc_count
== 0)
651 o
->flags
|= SEC_RELOC
;
652 o
->rel_filepos
= rel_filepos
;
653 rel_filepos
+= o
->reloc_count
* relsz
;
657 /* If doing a relocateable link, allocate space for the pointers we
659 if (info
->relocateable
)
663 /* We use section_count + 1, rather than section_count, because
664 the target_index fields are 1 based. */
665 finfo
.section_info
= ((struct coff_link_section_info
*)
666 malloc ((abfd
->section_count
+ 1)
667 * sizeof (struct coff_link_section_info
)));
668 if (finfo
.section_info
== NULL
)
670 bfd_set_error (bfd_error_no_memory
);
673 for (i
= 0; i
<= abfd
->section_count
; i
++)
675 finfo
.section_info
[i
].relocs
= NULL
;
676 finfo
.section_info
[i
].rel_hashes
= NULL
;
680 /* We now know the size of the relocs, so we can determine the file
681 positions of the line numbers. */
682 line_filepos
= rel_filepos
;
683 linesz
= bfd_coff_linesz (abfd
);
684 max_output_reloc_count
= 0;
685 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
687 if (o
->lineno_count
== 0)
691 o
->line_filepos
= line_filepos
;
692 line_filepos
+= o
->lineno_count
* linesz
;
695 if (o
->reloc_count
!= 0)
697 /* We don't know the indices of global symbols until we have
698 written out all the local symbols. For each section in
699 the output file, we keep an array of pointers to hash
700 table entries. Each entry in the array corresponds to a
701 reloc. When we find a reloc against a global symbol, we
702 set the corresponding entry in this array so that we can
703 fix up the symbol index after we have written out all the
706 Because of this problem, we also keep the relocs in
707 memory until the end of the link. This wastes memory,
708 but only when doing a relocateable link, which is not the
710 BFD_ASSERT (info
->relocateable
);
711 finfo
.section_info
[o
->target_index
].relocs
=
712 ((struct internal_reloc
*)
713 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
714 finfo
.section_info
[o
->target_index
].rel_hashes
=
715 ((struct coff_link_hash_entry
**)
716 malloc (o
->reloc_count
717 * sizeof (struct coff_link_hash_entry
*)));
718 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
719 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
721 bfd_set_error (bfd_error_no_memory
);
725 if (o
->reloc_count
> max_output_reloc_count
)
726 max_output_reloc_count
= o
->reloc_count
;
729 /* Reset the reloc and lineno counts, so that we can use them to
730 count the number of entries we have output so far. */
735 obj_sym_filepos (abfd
) = line_filepos
;
737 /* Figure out the largest number of symbols in an input BFD. Take
738 the opportunity to clear the output_has_begun fields of all the
741 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
745 sub
->output_has_begun
= false;
746 sz
= obj_raw_syment_count (sub
);
747 if (sz
> max_sym_count
)
751 /* Allocate some buffers used while linking. */
752 finfo
.internal_syms
= ((struct internal_syment
*)
753 malloc (max_sym_count
754 * sizeof (struct internal_syment
)));
755 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
756 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
757 finfo
.outsyms
= ((bfd_byte
*)
758 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
759 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
760 * bfd_coff_linesz (abfd
));
761 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
762 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
763 if (! info
->relocateable
)
764 finfo
.internal_relocs
= ((struct internal_reloc
*)
765 malloc (max_reloc_count
766 * sizeof (struct internal_reloc
)));
767 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
768 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
769 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
770 || finfo
.outsyms
== NULL
771 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
772 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
773 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
774 || (! info
->relocateable
775 && finfo
.internal_relocs
== NULL
776 && max_reloc_count
> 0))
778 bfd_set_error (bfd_error_no_memory
);
782 /* We now know the position of everything in the file, except that
783 we don't know the size of the symbol table and therefore we don't
784 know where the string table starts. We just build the string
785 table in memory as we go along. We process all the relocations
786 for a single input file at once. */
787 obj_raw_syment_count (abfd
) = 0;
789 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
791 if (! bfd_coff_start_final_link (abfd
, info
))
795 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
797 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
799 if (p
->type
== bfd_indirect_link_order
800 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
801 == bfd_target_coff_flavour
))
803 sub
= p
->u
.indirect
.section
->owner
;
804 if (! sub
->output_has_begun
)
806 if (! coff_link_input_bfd (&finfo
, sub
))
808 sub
->output_has_begun
= true;
811 else if (p
->type
== bfd_section_reloc_link_order
812 || p
->type
== bfd_symbol_reloc_link_order
)
814 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
819 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
825 /* Free up the buffers used by coff_link_input_bfd. */
827 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
828 debug_merge_allocated
= false;
830 if (finfo
.internal_syms
!= NULL
)
832 free (finfo
.internal_syms
);
833 finfo
.internal_syms
= NULL
;
835 if (finfo
.sec_ptrs
!= NULL
)
837 free (finfo
.sec_ptrs
);
838 finfo
.sec_ptrs
= NULL
;
840 if (finfo
.sym_indices
!= NULL
)
842 free (finfo
.sym_indices
);
843 finfo
.sym_indices
= NULL
;
845 if (finfo
.linenos
!= NULL
)
847 free (finfo
.linenos
);
848 finfo
.linenos
= NULL
;
850 if (finfo
.contents
!= NULL
)
852 free (finfo
.contents
);
853 finfo
.contents
= NULL
;
855 if (finfo
.external_relocs
!= NULL
)
857 free (finfo
.external_relocs
);
858 finfo
.external_relocs
= NULL
;
860 if (finfo
.internal_relocs
!= NULL
)
862 free (finfo
.internal_relocs
);
863 finfo
.internal_relocs
= NULL
;
866 /* The value of the last C_FILE symbol is supposed to be the symbol
867 index of the first external symbol. Write it out again if
869 if (finfo
.last_file_index
!= -1
870 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
872 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
873 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
874 (PTR
) finfo
.outsyms
);
876 (obj_sym_filepos (abfd
)
877 + finfo
.last_file_index
* symesz
),
879 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
883 /* Write out the global symbols. */
884 finfo
.failed
= false;
885 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
890 /* The outsyms buffer is used by coff_write_global_sym. */
891 if (finfo
.outsyms
!= NULL
)
893 free (finfo
.outsyms
);
894 finfo
.outsyms
= NULL
;
897 if (info
->relocateable
)
899 /* Now that we have written out all the global symbols, we know
900 the symbol indices to use for relocs against them, and we can
901 finally write out the relocs. */
902 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
903 if (external_relocs
== NULL
)
905 bfd_set_error (bfd_error_no_memory
);
909 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
911 struct internal_reloc
*irel
;
912 struct internal_reloc
*irelend
;
913 struct coff_link_hash_entry
**rel_hash
;
916 if (o
->reloc_count
== 0)
919 irel
= finfo
.section_info
[o
->target_index
].relocs
;
920 irelend
= irel
+ o
->reloc_count
;
921 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
922 erel
= external_relocs
;
923 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
925 if (*rel_hash
!= NULL
)
927 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
928 irel
->r_symndx
= (*rel_hash
)->indx
;
930 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
933 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
934 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
935 abfd
) != relsz
* o
->reloc_count
)
939 free (external_relocs
);
940 external_relocs
= NULL
;
943 /* Free up the section information. */
944 if (finfo
.section_info
!= NULL
)
948 for (i
= 0; i
< abfd
->section_count
; i
++)
950 if (finfo
.section_info
[i
].relocs
!= NULL
)
951 free (finfo
.section_info
[i
].relocs
);
952 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
953 free (finfo
.section_info
[i
].rel_hashes
);
955 free (finfo
.section_info
);
956 finfo
.section_info
= NULL
;
959 /* Write out the string table. */
960 if (obj_raw_syment_count (abfd
) != 0)
963 (obj_sym_filepos (abfd
)
964 + obj_raw_syment_count (abfd
) * symesz
),
968 #if STRING_SIZE_SIZE == 4
970 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
971 (bfd_byte
*) strbuf
);
973 #error Change bfd_h_put_32
976 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
979 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
983 _bfd_stringtab_free (finfo
.strtab
);
985 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
986 not try to write out the symbols. */
987 bfd_get_symcount (abfd
) = 0;
992 if (debug_merge_allocated
)
993 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
994 if (finfo
.strtab
!= NULL
)
995 _bfd_stringtab_free (finfo
.strtab
);
996 if (finfo
.section_info
!= NULL
)
1000 for (i
= 0; i
< abfd
->section_count
; i
++)
1002 if (finfo
.section_info
[i
].relocs
!= NULL
)
1003 free (finfo
.section_info
[i
].relocs
);
1004 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1005 free (finfo
.section_info
[i
].rel_hashes
);
1007 free (finfo
.section_info
);
1009 if (finfo
.internal_syms
!= NULL
)
1010 free (finfo
.internal_syms
);
1011 if (finfo
.sec_ptrs
!= NULL
)
1012 free (finfo
.sec_ptrs
);
1013 if (finfo
.sym_indices
!= NULL
)
1014 free (finfo
.sym_indices
);
1015 if (finfo
.outsyms
!= NULL
)
1016 free (finfo
.outsyms
);
1017 if (finfo
.linenos
!= NULL
)
1018 free (finfo
.linenos
);
1019 if (finfo
.contents
!= NULL
)
1020 free (finfo
.contents
);
1021 if (finfo
.external_relocs
!= NULL
)
1022 free (finfo
.external_relocs
);
1023 if (finfo
.internal_relocs
!= NULL
)
1024 free (finfo
.internal_relocs
);
1025 if (external_relocs
!= NULL
)
1026 free (external_relocs
);
1030 /* parse out a -heap <reserved>,<commit> line */
1033 dores_com (ptr
, output_bfd
, heap
)
1038 if (coff_data(output_bfd
)->pe
)
1040 int val
= strtoul (ptr
, &ptr
, 0);
1042 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1044 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1048 int val
= strtoul (ptr
+1, &ptr
, 0);
1050 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1052 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1058 static char *get_name(ptr
, dst
)
1065 while (*ptr
&& *ptr
!= ' ')
1071 /* Process any magic embedded commands in a section called .drectve */
1074 process_embedded_commands (output_bfd
, info
, abfd
)
1076 struct bfd_link_info
*info
;
1079 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1086 copy
= malloc ((size_t) sec
->_raw_size
);
1089 bfd_set_error (bfd_error_no_memory
);
1092 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1097 e
= copy
+ sec
->_raw_size
;
1098 for (s
= copy
; s
< e
; )
1104 if (strncmp (s
,"-attr", 5) == 0)
1116 s
= get_name(s
, &name
);
1117 s
= get_name(s
, &attribs
);
1137 asec
= bfd_get_section_by_name (abfd
, name
);
1140 asec
->flags
|= SEC_CODE
;
1142 asec
->flags
|= SEC_READONLY
;
1145 else if (strncmp (s
,"-heap", 5) == 0)
1147 s
= dores_com (s
+5, output_bfd
, 1);
1149 else if (strncmp (s
,"-stack", 6) == 0)
1151 s
= dores_com (s
+6, output_bfd
, 0);
1160 /* Link an input file into the linker output file. This function
1161 handles all the sections and relocations of the input file at once. */
1164 coff_link_input_bfd (finfo
, input_bfd
)
1165 struct coff_final_link_info
*finfo
;
1168 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1169 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1170 asection
*, struct internal_reloc
*,
1173 const char *strings
;
1174 bfd_size_type syment_base
;
1175 unsigned int n_tmask
;
1176 unsigned int n_btshft
;
1178 bfd_size_type isymesz
;
1179 bfd_size_type osymesz
;
1180 bfd_size_type linesz
;
1183 struct internal_syment
*isymp
;
1186 unsigned long output_index
;
1188 struct coff_link_hash_entry
**sym_hash
;
1191 /* Move all the symbols to the output file. */
1193 output_bfd
= finfo
->output_bfd
;
1194 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1196 syment_base
= obj_raw_syment_count (output_bfd
);
1197 isymesz
= bfd_coff_symesz (input_bfd
);
1198 osymesz
= bfd_coff_symesz (output_bfd
);
1199 linesz
= bfd_coff_linesz (input_bfd
);
1200 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1202 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1203 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1205 /* Define macros so that ISFCN, et. al., macros work correctly. */
1206 #define N_TMASK n_tmask
1207 #define N_BTSHFT n_btshft
1210 if (! finfo
->info
->keep_memory
)
1213 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1216 if (! _bfd_coff_get_external_symbols (input_bfd
))
1219 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1220 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1221 isymp
= finfo
->internal_syms
;
1222 secpp
= finfo
->sec_ptrs
;
1223 indexp
= finfo
->sym_indices
;
1224 output_index
= syment_base
;
1225 outsym
= finfo
->outsyms
;
1227 if (coff_data(output_bfd
)->pe
)
1229 if (!process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1233 while (esym
< esym_end
)
1235 struct internal_syment isym
;
1240 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1242 /* Make a copy of *isymp so that the relocate_section function
1243 always sees the original values. This is more reliable than
1244 always recomputing the symbol value even if we are stripping
1248 if (isym
.n_scnum
!= 0)
1249 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1252 if (isym
.n_value
== 0)
1253 *secpp
= bfd_und_section_ptr
;
1255 *secpp
= bfd_com_section_ptr
;
1262 add
= 1 + isym
.n_numaux
;
1264 /* If we are stripping all symbols, we want to skip this one. */
1265 if (finfo
->info
->strip
== strip_all
)
1270 if (isym
.n_sclass
== C_EXT
1271 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1273 /* This is a global symbol. Global symbols come at the
1274 end of the symbol table, so skip them for now.
1275 Function symbols, however, are an exception, and are
1276 not moved to the end. */
1278 if (! ISFCN (isym
.n_type
))
1283 /* This is a local symbol. Skip it if we are discarding
1285 if (finfo
->info
->discard
== discard_all
)
1290 /* If we stripping debugging symbols, and this is a debugging
1291 symbol, then skip it. */
1293 && finfo
->info
->strip
== strip_debugger
1294 && isym
.n_scnum
== N_DEBUG
)
1297 /* If some symbols are stripped based on the name, work out the
1298 name and decide whether to skip this symbol. */
1300 && (finfo
->info
->strip
== strip_some
1301 || finfo
->info
->discard
== discard_l
))
1304 char buf
[SYMNMLEN
+ 1];
1306 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1310 if ((finfo
->info
->strip
== strip_some
1311 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1314 && finfo
->info
->discard
== discard_l
1315 && strncmp (name
, finfo
->info
->lprefix
,
1316 finfo
->info
->lprefix_len
) == 0))
1320 /* If this is an enum, struct, or union tag, see if we have
1321 already output an identical type. */
1323 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1324 && (isym
.n_sclass
== C_ENTAG
1325 || isym
.n_sclass
== C_STRTAG
1326 || isym
.n_sclass
== C_UNTAG
)
1327 && isym
.n_numaux
== 1)
1330 char buf
[SYMNMLEN
+ 1];
1331 struct coff_debug_merge_hash_entry
*mh
;
1332 struct coff_debug_merge_type
*mt
;
1333 union internal_auxent aux
;
1334 struct coff_debug_merge_element
**epp
;
1335 bfd_byte
*esl
, *eslend
;
1336 struct internal_syment
*islp
;
1337 struct coff_debug_merge_type
*mtl
;
1339 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1343 /* Ignore fake names invented by compiler; treat them all as
1345 if (*name
== '~' || *name
== '.'
1346 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1347 && (name
[1] == '~' || name
[1] == '.')))
1350 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1355 /* Allocate memory to hold type information. If this turns
1356 out to be a duplicate, we pass this address to
1358 mt
= ((struct coff_debug_merge_type
*)
1359 bfd_alloc (input_bfd
,
1360 sizeof (struct coff_debug_merge_type
)));
1363 mt
->class = isym
.n_sclass
;
1365 /* Pick up the aux entry, which points to the end of the tag
1367 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1368 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1371 /* Gather the elements. */
1372 epp
= &mt
->elements
;
1373 mt
->elements
= NULL
;
1375 esl
= esym
+ 2 * isymesz
;
1376 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1377 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1378 while (esl
< eslend
)
1380 const char *elename
;
1381 char elebuf
[SYMNMLEN
+ 1];
1384 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1386 *epp
= ((struct coff_debug_merge_element
*)
1387 bfd_alloc (input_bfd
,
1388 sizeof (struct coff_debug_merge_element
)));
1392 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1394 if (elename
== NULL
)
1397 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1400 strcpy (copy
, elename
);
1402 (*epp
)->name
= copy
;
1403 (*epp
)->type
= islp
->n_type
;
1405 if (islp
->n_numaux
>= 1
1406 && islp
->n_type
!= T_NULL
1407 && islp
->n_sclass
!= C_EOS
)
1409 union internal_auxent eleaux
;
1412 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1413 islp
->n_type
, islp
->n_sclass
, 0,
1414 islp
->n_numaux
, (PTR
) &eleaux
);
1415 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1417 /* FIXME: If this tagndx entry refers to a symbol
1418 defined later in this file, we just ignore it.
1419 Handling this correctly would be tedious, and may
1425 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1428 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1429 if ((*epp
)->tagndx
< 0)
1433 epp
= &(*epp
)->next
;
1436 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1437 islp
+= islp
->n_numaux
+ 1;
1440 /* See if we already have a definition which matches this
1442 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1444 struct coff_debug_merge_element
*me
, *mel
;
1446 if (mtl
->class != mt
->class)
1449 for (me
= mt
->elements
, mel
= mtl
->elements
;
1450 me
!= NULL
&& mel
!= NULL
;
1451 me
= me
->next
, mel
= mel
->next
)
1453 if (strcmp (me
->name
, mel
->name
) != 0
1454 || me
->type
!= mel
->type
1455 || me
->tagndx
!= mel
->tagndx
)
1459 if (me
== NULL
&& mel
== NULL
)
1463 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1465 /* This is the first definition of this type. */
1466 mt
->indx
= output_index
;
1467 mt
->next
= mh
->types
;
1472 /* This is a redefinition which can be merged. */
1473 bfd_release (input_bfd
, (PTR
) mt
);
1474 *indexp
= mtl
->indx
;
1475 add
= (eslend
- esym
) / isymesz
;
1480 /* We now know whether we are to skip this symbol or not. */
1483 /* Adjust the symbol in order to output it. */
1485 if (isym
._n
._n_n
._n_zeroes
== 0
1486 && isym
._n
._n_n
._n_offset
!= 0)
1491 /* This symbol has a long name. Enter it in the string
1492 table we are building. Note that we do not check
1493 bfd_coff_symname_in_debug. That is only true for
1494 XCOFF, and XCOFF requires different linking code
1496 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1500 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1501 if (indx
== (bfd_size_type
) -1)
1503 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1506 if (isym
.n_scnum
> 0)
1508 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1509 isym
.n_value
+= ((*secpp
)->output_section
->vma
1510 + (*secpp
)->output_offset
1514 /* The value of a C_FILE symbol is the symbol index of the
1515 next C_FILE symbol. The value of the last C_FILE symbol
1516 is the symbol index to the first external symbol
1517 (actually, coff_renumber_symbols does not get this
1518 right--it just sets the value of the last C_FILE symbol
1519 to zero--and nobody has ever complained about it). We
1520 try to get this right, below, just before we write the
1521 symbols out, but in the general case we may have to write
1522 the symbol out twice. */
1523 if (isym
.n_sclass
== C_FILE
)
1525 if (finfo
->last_file_index
!= -1
1526 && finfo
->last_file
.n_value
!= (long) output_index
)
1528 /* We must correct the value of the last C_FILE entry. */
1529 finfo
->last_file
.n_value
= output_index
;
1530 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1532 /* The last C_FILE symbol is in this input file. */
1533 bfd_coff_swap_sym_out (output_bfd
,
1534 (PTR
) &finfo
->last_file
,
1535 (PTR
) (finfo
->outsyms
1536 + ((finfo
->last_file_index
1542 /* We have already written out the last C_FILE
1543 symbol. We need to write it out again. We
1544 borrow *outsym temporarily. */
1545 bfd_coff_swap_sym_out (output_bfd
,
1546 (PTR
) &finfo
->last_file
,
1548 if (bfd_seek (output_bfd
,
1549 (obj_sym_filepos (output_bfd
)
1550 + finfo
->last_file_index
* osymesz
),
1552 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1558 finfo
->last_file_index
= output_index
;
1559 finfo
->last_file
= isym
;
1562 /* Output the symbol. */
1564 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1566 *indexp
= output_index
;
1571 struct coff_link_hash_entry
*h
;
1573 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1575 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1576 BFD_ASSERT (h
!= NULL
);
1577 h
->indx
= output_index
;
1580 output_index
+= add
;
1581 outsym
+= add
* osymesz
;
1584 esym
+= add
* isymesz
;
1588 for (--add
; add
> 0; --add
)
1595 /* Fix up the aux entries. This must be done in a separate pass,
1596 because we don't know the correct symbol indices until we have
1597 already decided which symbols we are going to keep. */
1599 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1600 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1601 isymp
= finfo
->internal_syms
;
1602 indexp
= finfo
->sym_indices
;
1603 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1604 outsym
= finfo
->outsyms
;
1605 while (esym
< esym_end
)
1609 add
= 1 + isymp
->n_numaux
;
1612 || (bfd_size_type
) *indexp
< syment_base
)
1613 && (*sym_hash
== NULL
1614 || (*sym_hash
)->auxbfd
!= input_bfd
))
1615 esym
+= add
* isymesz
;
1618 struct coff_link_hash_entry
*h
;
1625 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1633 /* Handle the aux entries. This handling is based on
1634 coff_pointerize_aux. I don't know if it always correct. */
1635 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1637 union internal_auxent aux
;
1638 union internal_auxent
*auxp
;
1644 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1645 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1650 if (isymp
->n_sclass
== C_FILE
)
1652 /* If this is a long filename, we must put it in the
1654 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1655 && auxp
->x_file
.x_n
.x_offset
!= 0)
1657 const char *filename
;
1660 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1661 >= STRING_SIZE_SIZE
);
1662 if (strings
== NULL
)
1664 strings
= _bfd_coff_read_string_table (input_bfd
);
1665 if (strings
== NULL
)
1668 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1669 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1671 if (indx
== (bfd_size_type
) -1)
1673 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1676 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1680 if (ISFCN (isymp
->n_type
)
1681 || ISTAG (isymp
->n_sclass
)
1682 || isymp
->n_sclass
== C_BLOCK
)
1684 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1686 && indx
< obj_raw_syment_count (input_bfd
))
1688 /* We look forward through the symbol for
1689 the index of the next symbol we are going
1690 to include. I don't know if this is
1692 while (finfo
->sym_indices
[indx
] < 0
1693 && indx
< obj_raw_syment_count (input_bfd
))
1695 if (indx
>= obj_raw_syment_count (input_bfd
))
1696 indx
= output_index
;
1698 indx
= finfo
->sym_indices
[indx
];
1699 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1703 indx
= auxp
->x_sym
.x_tagndx
.l
;
1704 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1708 symindx
= finfo
->sym_indices
[indx
];
1710 auxp
->x_sym
.x_tagndx
.l
= 0;
1712 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1718 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1719 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1733 /* Relocate the line numbers, unless we are stripping them. */
1734 if (finfo
->info
->strip
== strip_none
1735 || finfo
->info
->strip
== strip_some
)
1737 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1743 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1744 build_link_order in ldwrite.c will not have created a
1745 link order, which means that we will not have seen this
1746 input section in _bfd_coff_final_link, which means that
1747 we will not have allocated space for the line numbers of
1748 this section. I don't think line numbers can be
1749 meaningful for a section which does not have
1750 SEC_HAS_CONTENTS set, but, if they do, this must be
1752 if (o
->lineno_count
== 0
1753 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1756 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1757 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1758 input_bfd
) != linesz
* o
->lineno_count
)
1761 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1762 eline
= finfo
->linenos
;
1763 elineend
= eline
+ linesz
* o
->lineno_count
;
1764 for (; eline
< elineend
; eline
+= linesz
)
1766 struct internal_lineno iline
;
1768 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1770 if (iline
.l_lnno
!= 0)
1771 iline
.l_addr
.l_paddr
+= offset
;
1772 else if (iline
.l_addr
.l_symndx
>= 0
1773 && ((unsigned long) iline
.l_addr
.l_symndx
1774 < obj_raw_syment_count (input_bfd
)))
1778 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1782 /* These line numbers are attached to a symbol
1783 which we are stripping. We should really
1784 just discard the line numbers, but that would
1785 be a pain because we have already counted
1791 struct internal_syment is
;
1792 union internal_auxent ia
;
1794 /* Fix up the lnnoptr field in the aux entry of
1795 the symbol. It turns out that we can't do
1796 this when we modify the symbol aux entries,
1797 because gas sometimes screws up the lnnoptr
1798 field and makes it an offset from the start
1799 of the line numbers rather than an absolute
1801 bfd_coff_swap_sym_in (output_bfd
,
1802 (PTR
) (finfo
->outsyms
1803 + ((indx
- syment_base
)
1806 if ((ISFCN (is
.n_type
)
1807 || is
.n_sclass
== C_BLOCK
)
1808 && is
.n_numaux
>= 1)
1812 auxptr
= (PTR
) (finfo
->outsyms
1813 + ((indx
- syment_base
+ 1)
1815 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1816 is
.n_type
, is
.n_sclass
,
1817 0, is
.n_numaux
, (PTR
) &ia
);
1818 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1819 (o
->output_section
->line_filepos
1820 + o
->output_section
->lineno_count
* linesz
1821 + eline
- finfo
->linenos
);
1822 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1823 is
.n_type
, is
.n_sclass
, 0,
1824 is
.n_numaux
, auxptr
);
1828 iline
.l_addr
.l_symndx
= indx
;
1831 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1834 if (bfd_seek (output_bfd
,
1835 (o
->output_section
->line_filepos
1836 + o
->output_section
->lineno_count
* linesz
),
1838 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1839 output_bfd
) != linesz
* o
->lineno_count
)
1842 o
->output_section
->lineno_count
+= o
->lineno_count
;
1846 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1847 symbol will be the first symbol in the next input file. In the
1848 normal case, this will save us from writing out the C_FILE symbol
1850 if (finfo
->last_file_index
!= -1
1851 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1853 finfo
->last_file
.n_value
= output_index
;
1854 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1855 (PTR
) (finfo
->outsyms
1856 + ((finfo
->last_file_index
- syment_base
)
1860 /* Write the modified symbols to the output file. */
1861 if (outsym
> finfo
->outsyms
)
1863 if (bfd_seek (output_bfd
,
1864 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1866 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1868 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1871 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1872 + (outsym
- finfo
->outsyms
) / osymesz
)
1875 obj_raw_syment_count (output_bfd
) = output_index
;
1878 /* Relocate the contents of each section. */
1879 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1880 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1884 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1886 if ((o
->flags
& SEC_RELOC
) != 0
1887 && o
->reloc_count
!= 0)
1889 ((*_bfd_error_handler
)
1890 ("%s: relocs in section `%s', but it has no contents",
1891 bfd_get_filename (input_bfd
),
1892 bfd_get_section_name (input_bfd
, o
)));
1893 bfd_set_error (bfd_error_no_contents
);
1900 if (coff_section_data (input_bfd
, o
) != NULL
1901 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1902 contents
= coff_section_data (input_bfd
, o
)->contents
;
1905 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1906 (file_ptr
) 0, o
->_raw_size
))
1908 contents
= finfo
->contents
;
1911 if ((o
->flags
& SEC_RELOC
) != 0)
1914 struct internal_reloc
*internal_relocs
;
1915 struct internal_reloc
*irel
;
1917 /* Read in the relocs. */
1918 target_index
= o
->output_section
->target_index
;
1919 internal_relocs
= (_bfd_coff_read_internal_relocs
1920 (input_bfd
, o
, false, finfo
->external_relocs
,
1921 finfo
->info
->relocateable
,
1922 (finfo
->info
->relocateable
1923 ? (finfo
->section_info
[target_index
].relocs
1924 + o
->output_section
->reloc_count
)
1925 : finfo
->internal_relocs
)));
1926 if (internal_relocs
== NULL
)
1929 /* Call processor specific code to relocate the section
1931 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1935 finfo
->internal_syms
,
1939 if (finfo
->info
->relocateable
)
1942 struct internal_reloc
*irelend
;
1943 struct coff_link_hash_entry
**rel_hash
;
1945 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1946 irel
= internal_relocs
;
1947 irelend
= irel
+ o
->reloc_count
;
1948 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1949 + o
->output_section
->reloc_count
);
1950 for (; irel
< irelend
; irel
++, rel_hash
++)
1952 struct coff_link_hash_entry
*h
;
1957 /* Adjust the reloc address and symbol index. */
1959 irel
->r_vaddr
+= offset
;
1961 if (irel
->r_symndx
== -1)
1966 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1974 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1977 /* This is a global symbol. */
1979 irel
->r_symndx
= h
->indx
;
1982 /* This symbol is being written at the end
1983 of the file, and we do not yet know the
1984 symbol index. We save the pointer to the
1985 hash table entry in the rel_hash list.
1986 We set the indx field to -2 to indicate
1987 that this symbol must not be stripped. */
1996 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1998 irel
->r_symndx
= indx
;
2001 struct internal_syment
*is
;
2003 char buf
[SYMNMLEN
+ 1];
2005 /* This reloc is against a symbol we are
2006 stripping. It would be possible to
2007 handle this case, but I don't think it's
2009 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2011 name
= (_bfd_coff_internal_syment_name
2012 (input_bfd
, is
, buf
));
2016 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2017 (finfo
->info
, name
, input_bfd
, o
,
2024 o
->output_section
->reloc_count
+= o
->reloc_count
;
2028 /* Write out the modified section contents. */
2029 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2030 contents
, o
->output_offset
,
2031 (o
->_cooked_size
!= 0
2037 if (! finfo
->info
->keep_memory
)
2039 if (! _bfd_coff_free_symbols (input_bfd
))
2046 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2049 coff_write_global_sym (h
, data
)
2050 struct coff_link_hash_entry
*h
;
2053 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2055 struct internal_syment isym
;
2056 bfd_size_type symesz
;
2059 output_bfd
= finfo
->output_bfd
;
2065 && (finfo
->info
->strip
== strip_all
2066 || (finfo
->info
->strip
== strip_some
2067 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2068 h
->root
.root
.string
, false, false)
2072 switch (h
->root
.type
)
2075 case bfd_link_hash_new
:
2079 case bfd_link_hash_undefined
:
2080 case bfd_link_hash_undefweak
:
2081 isym
.n_scnum
= N_UNDEF
;
2085 case bfd_link_hash_defined
:
2086 case bfd_link_hash_defweak
:
2090 sec
= h
->root
.u
.def
.section
->output_section
;
2091 if (bfd_is_abs_section (sec
))
2092 isym
.n_scnum
= N_ABS
;
2094 isym
.n_scnum
= sec
->target_index
;
2095 isym
.n_value
= (h
->root
.u
.def
.value
2097 + h
->root
.u
.def
.section
->output_offset
);
2101 case bfd_link_hash_common
:
2102 isym
.n_scnum
= N_UNDEF
;
2103 isym
.n_value
= h
->root
.u
.c
.size
;
2106 case bfd_link_hash_indirect
:
2107 case bfd_link_hash_warning
:
2108 /* Just ignore these. They can't be handled anyhow. */
2112 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2113 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2120 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2122 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2124 if (indx
== (bfd_size_type
) -1)
2126 finfo
->failed
= true;
2129 isym
._n
._n_n
._n_zeroes
= 0;
2130 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2133 isym
.n_sclass
= h
->class;
2134 isym
.n_type
= h
->type
;
2136 if (isym
.n_sclass
== C_NULL
)
2137 isym
.n_sclass
= C_EXT
;
2139 isym
.n_numaux
= h
->numaux
;
2141 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2143 symesz
= bfd_coff_symesz (output_bfd
);
2145 if (bfd_seek (output_bfd
,
2146 (obj_sym_filepos (output_bfd
)
2147 + obj_raw_syment_count (output_bfd
) * symesz
),
2149 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2151 finfo
->failed
= true;
2155 h
->indx
= obj_raw_syment_count (output_bfd
);
2157 ++obj_raw_syment_count (output_bfd
);
2159 /* Write out any associated aux entries. There normally will be
2160 none. If there are any, I have no idea how to modify them. */
2161 for (i
= 0; i
< isym
.n_numaux
; i
++)
2163 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2164 isym
.n_sclass
, i
, isym
.n_numaux
,
2165 (PTR
) finfo
->outsyms
);
2166 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2168 finfo
->failed
= true;
2171 ++obj_raw_syment_count (output_bfd
);
2177 /* Handle a link order which is supposed to generate a reloc. */
2180 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2182 struct coff_final_link_info
*finfo
;
2183 asection
*output_section
;
2184 struct bfd_link_order
*link_order
;
2186 reloc_howto_type
*howto
;
2187 struct internal_reloc
*irel
;
2188 struct coff_link_hash_entry
**rel_hash_ptr
;
2190 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2193 bfd_set_error (bfd_error_bad_value
);
2197 if (link_order
->u
.reloc
.p
->addend
!= 0)
2201 bfd_reloc_status_type rstat
;
2204 size
= bfd_get_reloc_size (howto
);
2205 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2209 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2210 link_order
->u
.reloc
.p
->addend
, buf
);
2216 case bfd_reloc_outofrange
:
2218 case bfd_reloc_overflow
:
2219 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2221 (link_order
->type
== bfd_section_reloc_link_order
2222 ? bfd_section_name (output_bfd
,
2223 link_order
->u
.reloc
.p
->u
.section
)
2224 : link_order
->u
.reloc
.p
->u
.name
),
2225 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2226 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2233 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2234 (file_ptr
) link_order
->offset
, size
);
2240 /* Store the reloc information in the right place. It will get
2241 swapped and written out at the end of the final_link routine. */
2243 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2244 + output_section
->reloc_count
);
2245 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2246 + output_section
->reloc_count
);
2248 memset (irel
, 0, sizeof (struct internal_reloc
));
2249 *rel_hash_ptr
= NULL
;
2251 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2253 if (link_order
->type
== bfd_section_reloc_link_order
)
2255 /* We need to somehow locate a symbol in the right section. The
2256 symbol must either have a value of zero, or we must adjust
2257 the addend by the value of the symbol. FIXME: Write this
2258 when we need it. The old linker couldn't handle this anyhow. */
2260 *rel_hash_ptr
= NULL
;
2265 struct coff_link_hash_entry
*h
;
2267 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2268 link_order
->u
.reloc
.p
->u
.name
,
2269 false, false, true);
2273 irel
->r_symndx
= h
->indx
;
2276 /* Set the index to -2 to force this symbol to get
2285 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2286 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2287 (asection
*) NULL
, (bfd_vma
) 0)))
2293 /* FIXME: Is this always right? */
2294 irel
->r_type
= howto
->type
;
2296 /* r_size is only used on the RS/6000, which needs its own linker
2297 routines anyhow. r_extern is only used for ECOFF. */
2299 /* FIXME: What is the right value for r_offset? Is zero OK? */
2301 ++output_section
->reloc_count
;
2306 /* A basic reloc handling routine which may be used by processors with
2310 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2311 input_section
, contents
, relocs
, syms
,
2314 struct bfd_link_info
*info
;
2316 asection
*input_section
;
2318 struct internal_reloc
*relocs
;
2319 struct internal_syment
*syms
;
2320 asection
**sections
;
2322 struct internal_reloc
*rel
;
2323 struct internal_reloc
*relend
;
2326 relend
= rel
+ input_section
->reloc_count
;
2327 for (; rel
< relend
; rel
++)
2330 struct coff_link_hash_entry
*h
;
2331 struct internal_syment
*sym
;
2334 reloc_howto_type
*howto
;
2335 bfd_reloc_status_type rstat
;
2337 symndx
= rel
->r_symndx
;
2346 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2347 sym
= syms
+ symndx
;
2350 /* COFF treats common symbols in one of two ways. Either the
2351 size of the symbol is included in the section contents, or it
2352 is not. We assume that the size is not included, and force
2353 the rtype_to_howto function to adjust the addend as needed. */
2355 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2356 addend
= - sym
->n_value
;
2361 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2374 sec
= bfd_abs_section_ptr
;
2379 sec
= sections
[symndx
];
2380 val
= (sec
->output_section
->vma
2381 + sec
->output_offset
2388 if (h
->root
.type
== bfd_link_hash_defined
2389 || h
->root
.type
== bfd_link_hash_defweak
)
2393 sec
= h
->root
.u
.def
.section
;
2394 val
= (h
->root
.u
.def
.value
2395 + sec
->output_section
->vma
2396 + sec
->output_offset
);
2399 else if (! info
->relocateable
)
2401 if (! ((*info
->callbacks
->undefined_symbol
)
2402 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2403 rel
->r_vaddr
- input_section
->vma
)))
2408 if (info
->base_file
)
2410 /* Emit a reloc if the backend thinks it needs it. */
2411 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2413 /* relocation to a symbol in a section which
2414 isn't absolute - we output the address here
2416 bfd_vma addr
= rel
->r_vaddr
2417 - input_section
->vma
2418 + input_section
->output_offset
2419 + input_section
->output_section
->vma
;
2420 if (coff_data(output_bfd
)->pe
)
2421 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2422 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2426 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2428 rel
->r_vaddr
- input_section
->vma
,
2437 case bfd_reloc_overflow
:
2440 char buf
[SYMNMLEN
+ 1];
2445 name
= h
->root
.root
.string
;
2448 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2453 if (! ((*info
->callbacks
->reloc_overflow
)
2454 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2455 input_section
, rel
->r_vaddr
- input_section
->vma
)))