1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996 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 static boolean coff_link_add_object_symbols
31 PARAMS ((bfd
*, struct bfd_link_info
*));
32 static boolean coff_link_check_archive_element
33 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
34 static boolean coff_link_check_ar_symbols
35 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
36 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
38 /* Create an entry in a COFF linker hash table. */
40 struct bfd_hash_entry
*
41 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
42 struct bfd_hash_entry
*entry
;
43 struct bfd_hash_table
*table
;
46 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
48 /* Allocate the structure if it has not already been allocated by a
50 if (ret
== (struct coff_link_hash_entry
*) NULL
)
51 ret
= ((struct coff_link_hash_entry
*)
52 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
53 if (ret
== (struct coff_link_hash_entry
*) NULL
)
54 return (struct bfd_hash_entry
*) ret
;
56 /* Call the allocation method of the superclass. */
57 ret
= ((struct coff_link_hash_entry
*)
58 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
60 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
62 /* Set local fields. */
71 return (struct bfd_hash_entry
*) ret
;
74 /* Initialize a COFF linker hash table. */
77 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
78 struct coff_link_hash_table
*table
;
80 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
81 struct bfd_hash_table
*,
84 table
->stab_info
= NULL
;
85 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
88 /* Create a COFF linker hash table. */
90 struct bfd_link_hash_table
*
91 _bfd_coff_link_hash_table_create (abfd
)
94 struct coff_link_hash_table
*ret
;
96 ret
= ((struct coff_link_hash_table
*)
97 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
100 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
101 _bfd_coff_link_hash_newfunc
))
103 bfd_release (abfd
, ret
);
104 return (struct bfd_link_hash_table
*) NULL
;
109 /* Create an entry in a COFF debug merge hash table. */
111 struct bfd_hash_entry
*
112 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
113 struct bfd_hash_entry
*entry
;
114 struct bfd_hash_table
*table
;
117 struct coff_debug_merge_hash_entry
*ret
=
118 (struct coff_debug_merge_hash_entry
*) entry
;
120 /* Allocate the structure if it has not already been allocated by a
122 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
123 ret
= ((struct coff_debug_merge_hash_entry
*)
124 bfd_hash_allocate (table
,
125 sizeof (struct coff_debug_merge_hash_entry
)));
126 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
127 return (struct bfd_hash_entry
*) ret
;
129 /* Call the allocation method of the superclass. */
130 ret
= ((struct coff_debug_merge_hash_entry
*)
131 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
132 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
134 /* Set local fields. */
138 return (struct bfd_hash_entry
*) ret
;
141 /* Given a COFF BFD, add symbols to the global hash table as
145 _bfd_coff_link_add_symbols (abfd
, info
)
147 struct bfd_link_info
*info
;
149 switch (bfd_get_format (abfd
))
152 return coff_link_add_object_symbols (abfd
, info
);
154 return (_bfd_generic_link_add_archive_symbols
155 (abfd
, info
, coff_link_check_archive_element
));
157 bfd_set_error (bfd_error_wrong_format
);
162 /* Add symbols from a COFF object file. */
165 coff_link_add_object_symbols (abfd
, info
)
167 struct bfd_link_info
*info
;
169 if (! _bfd_coff_get_external_symbols (abfd
))
171 if (! coff_link_add_symbols (abfd
, info
))
174 if (! info
->keep_memory
)
176 if (! _bfd_coff_free_symbols (abfd
))
182 /* Check a single archive element to see if we need to include it in
183 the link. *PNEEDED is set according to whether this element is
184 needed in the link or not. This is called via
185 _bfd_generic_link_add_archive_symbols. */
188 coff_link_check_archive_element (abfd
, info
, pneeded
)
190 struct bfd_link_info
*info
;
193 if (! _bfd_coff_get_external_symbols (abfd
))
196 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
201 if (! coff_link_add_symbols (abfd
, info
))
205 if (! info
->keep_memory
|| ! *pneeded
)
207 if (! _bfd_coff_free_symbols (abfd
))
214 /* Look through the symbols to see if this object file should be
215 included in the link. */
218 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
220 struct bfd_link_info
*info
;
223 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
224 bfd_size_type symesz
;
230 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
232 symesz
= bfd_coff_symesz (abfd
);
233 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
234 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
235 while (esym
< esym_end
)
237 struct internal_syment sym
;
239 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
241 if ((sym
.n_sclass
== C_EXT
242 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
243 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
246 char buf
[SYMNMLEN
+ 1];
247 struct bfd_link_hash_entry
*h
;
249 /* This symbol is externally visible, and is defined by this
252 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
255 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
257 /* We are only interested in symbols that are currently
258 undefined. If a symbol is currently known to be common,
259 COFF linkers do not bring in an object file which defines
261 if (h
!= (struct bfd_link_hash_entry
*) NULL
262 && h
->type
== bfd_link_hash_undefined
)
264 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
271 esym
+= (sym
.n_numaux
+ 1) * symesz
;
274 /* We do not need this object file. */
278 /* Add all the symbols from an object file to the hash table. */
281 coff_link_add_symbols (abfd
, info
)
283 struct bfd_link_info
*info
;
285 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
286 boolean default_copy
;
287 bfd_size_type symcount
;
288 struct coff_link_hash_entry
**sym_hash
;
289 bfd_size_type symesz
;
293 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
295 if (info
->keep_memory
)
296 default_copy
= false;
300 symcount
= obj_raw_syment_count (abfd
);
302 /* We keep a list of the linker hash table entries that correspond
303 to particular symbols. */
304 sym_hash
= ((struct coff_link_hash_entry
**)
307 * sizeof (struct coff_link_hash_entry
*))));
308 if (sym_hash
== NULL
&& symcount
!= 0)
310 obj_coff_sym_hashes (abfd
) = sym_hash
;
312 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
314 symesz
= bfd_coff_symesz (abfd
);
315 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
316 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
317 esym_end
= esym
+ symcount
* symesz
;
318 while (esym
< esym_end
)
320 struct internal_syment sym
;
323 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
325 if (sym
.n_sclass
== C_EXT
326 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
329 char buf
[SYMNMLEN
+ 1];
334 /* This symbol is externally visible. */
336 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
340 /* We must copy the name into memory if we got it from the
341 syment itself, rather than the string table. */
343 if (sym
._n
._n_n
._n_zeroes
!= 0
344 || sym
._n
._n_n
._n_offset
== 0)
349 if (sym
.n_scnum
== 0)
354 section
= bfd_und_section_ptr
;
359 section
= bfd_com_section_ptr
;
364 flags
= BSF_EXPORT
| BSF_GLOBAL
;
365 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
366 value
-= section
->vma
;
369 if (! (bfd_coff_link_add_one_symbol
370 (info
, abfd
, name
, flags
, section
, value
,
371 (const char *) NULL
, copy
, false,
372 (struct bfd_link_hash_entry
**) sym_hash
)))
375 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
377 if (((*sym_hash
)->class == C_NULL
378 && (*sym_hash
)->type
== T_NULL
)
381 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
383 (*sym_hash
)->class = sym
.n_sclass
;
384 if (sym
.n_type
!= T_NULL
)
386 if ((*sym_hash
)->type
!= T_NULL
387 && (*sym_hash
)->type
!= sym
.n_type
)
388 (*_bfd_error_handler
)
389 ("Warning: type of symbol `%s' changed from %d to %d in %s",
390 name
, (*sym_hash
)->type
, sym
.n_type
,
391 bfd_get_filename (abfd
));
392 (*sym_hash
)->type
= sym
.n_type
;
394 (*sym_hash
)->auxbfd
= abfd
;
395 if (sym
.n_numaux
!= 0)
397 union internal_auxent
*alloc
;
400 union internal_auxent
*iaux
;
402 (*sym_hash
)->numaux
= sym
.n_numaux
;
403 alloc
= ((union internal_auxent
*)
404 bfd_hash_allocate (&info
->hash
->table
,
406 * sizeof (*alloc
))));
409 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
411 i
++, eaux
+= symesz
, iaux
++)
412 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
413 sym
.n_sclass
, i
, sym
.n_numaux
,
415 (*sym_hash
)->aux
= alloc
;
421 esym
+= (sym
.n_numaux
+ 1) * symesz
;
422 sym_hash
+= sym
.n_numaux
+ 1;
425 /* If this is a non-traditional, non-relocateable link, try to
426 optimize the handling of any .stab/.stabstr sections. */
427 if (! info
->relocateable
428 && ! info
->traditional_format
429 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
430 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
432 asection
*stab
, *stabstr
;
434 stab
= bfd_get_section_by_name (abfd
, ".stab");
437 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
441 struct coff_link_hash_table
*table
;
442 struct coff_section_tdata
*secdata
;
444 secdata
= coff_section_data (abfd
, stab
);
448 (PTR
) bfd_zalloc (abfd
,
449 sizeof (struct coff_section_tdata
));
450 if (stab
->used_by_bfd
== NULL
)
452 secdata
= coff_section_data (abfd
, stab
);
455 table
= coff_hash_table (info
);
457 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
459 &secdata
->stab_info
))
468 /* Do the final link step. */
471 _bfd_coff_final_link (abfd
, info
)
473 struct bfd_link_info
*info
;
475 bfd_size_type symesz
;
476 struct coff_final_link_info finfo
;
477 boolean debug_merge_allocated
;
478 boolean long_section_names
;
480 struct bfd_link_order
*p
;
481 size_t max_sym_count
;
482 size_t max_lineno_count
;
483 size_t max_reloc_count
;
484 size_t max_output_reloc_count
;
485 size_t max_contents_size
;
486 file_ptr rel_filepos
;
488 file_ptr line_filepos
;
491 bfd_byte
*external_relocs
= NULL
;
492 char strbuf
[STRING_SIZE_SIZE
];
494 symesz
= bfd_coff_symesz (abfd
);
497 finfo
.output_bfd
= abfd
;
499 finfo
.section_info
= NULL
;
500 finfo
.last_file_index
= -1;
501 finfo
.last_bf_index
= -1;
502 finfo
.internal_syms
= NULL
;
503 finfo
.sec_ptrs
= NULL
;
504 finfo
.sym_indices
= NULL
;
505 finfo
.outsyms
= NULL
;
506 finfo
.linenos
= NULL
;
507 finfo
.contents
= NULL
;
508 finfo
.external_relocs
= NULL
;
509 finfo
.internal_relocs
= NULL
;
510 debug_merge_allocated
= false;
512 coff_data (abfd
)->link_info
= info
;
514 finfo
.strtab
= _bfd_stringtab_init ();
515 if (finfo
.strtab
== NULL
)
518 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
520 debug_merge_allocated
= true;
522 /* Compute the file positions for all the sections. */
523 if (! abfd
->output_has_begun
)
524 bfd_coff_compute_section_file_positions (abfd
);
526 /* Count the line numbers and relocation entries required for the
527 output file. Set the file positions for the relocs. */
528 rel_filepos
= obj_relocbase (abfd
);
529 relsz
= bfd_coff_relsz (abfd
);
530 max_contents_size
= 0;
531 max_lineno_count
= 0;
534 long_section_names
= false;
535 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
539 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
541 if (p
->type
== bfd_indirect_link_order
)
545 sec
= p
->u
.indirect
.section
;
547 /* Mark all sections which are to be included in the
548 link. This will normally be every section. We need
549 to do this so that we can identify any sections which
550 the linker has decided to not include. */
551 sec
->linker_mark
= true;
553 if (info
->strip
== strip_none
554 || info
->strip
== strip_some
)
555 o
->lineno_count
+= sec
->lineno_count
;
557 if (info
->relocateable
)
558 o
->reloc_count
+= sec
->reloc_count
;
560 if (sec
->_raw_size
> max_contents_size
)
561 max_contents_size
= sec
->_raw_size
;
562 if (sec
->lineno_count
> max_lineno_count
)
563 max_lineno_count
= sec
->lineno_count
;
564 if (sec
->reloc_count
> max_reloc_count
)
565 max_reloc_count
= sec
->reloc_count
;
567 else if (info
->relocateable
568 && (p
->type
== bfd_section_reloc_link_order
569 || p
->type
== bfd_symbol_reloc_link_order
))
572 if (o
->reloc_count
== 0)
576 o
->flags
|= SEC_RELOC
;
577 o
->rel_filepos
= rel_filepos
;
578 rel_filepos
+= o
->reloc_count
* relsz
;
581 if (bfd_coff_long_section_names (abfd
)
582 && strlen (o
->name
) > SCNNMLEN
)
584 /* This section has a long name which must go in the string
585 table. This must correspond to the code in
586 coff_write_object_contents which puts the string index
587 into the s_name field of the section header. That is why
588 we pass hash as false. */
589 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
590 == (bfd_size_type
) -1)
592 long_section_names
= true;
596 /* If doing a relocateable link, allocate space for the pointers we
598 if (info
->relocateable
)
602 /* We use section_count + 1, rather than section_count, because
603 the target_index fields are 1 based. */
605 ((struct coff_link_section_info
*)
606 bfd_malloc ((abfd
->section_count
+ 1)
607 * sizeof (struct coff_link_section_info
)));
608 if (finfo
.section_info
== NULL
)
610 for (i
= 0; i
<= abfd
->section_count
; i
++)
612 finfo
.section_info
[i
].relocs
= NULL
;
613 finfo
.section_info
[i
].rel_hashes
= NULL
;
617 /* We now know the size of the relocs, so we can determine the file
618 positions of the line numbers. */
619 line_filepos
= rel_filepos
;
620 linesz
= bfd_coff_linesz (abfd
);
621 max_output_reloc_count
= 0;
622 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
624 if (o
->lineno_count
== 0)
628 o
->line_filepos
= line_filepos
;
629 line_filepos
+= o
->lineno_count
* linesz
;
632 if (o
->reloc_count
!= 0)
634 /* We don't know the indices of global symbols until we have
635 written out all the local symbols. For each section in
636 the output file, we keep an array of pointers to hash
637 table entries. Each entry in the array corresponds to a
638 reloc. When we find a reloc against a global symbol, we
639 set the corresponding entry in this array so that we can
640 fix up the symbol index after we have written out all the
643 Because of this problem, we also keep the relocs in
644 memory until the end of the link. This wastes memory,
645 but only when doing a relocateable link, which is not the
647 BFD_ASSERT (info
->relocateable
);
648 finfo
.section_info
[o
->target_index
].relocs
=
649 ((struct internal_reloc
*)
650 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
651 finfo
.section_info
[o
->target_index
].rel_hashes
=
652 ((struct coff_link_hash_entry
**)
653 bfd_malloc (o
->reloc_count
654 * sizeof (struct coff_link_hash_entry
*)));
655 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
656 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
659 if (o
->reloc_count
> max_output_reloc_count
)
660 max_output_reloc_count
= o
->reloc_count
;
663 /* Reset the reloc and lineno counts, so that we can use them to
664 count the number of entries we have output so far. */
669 obj_sym_filepos (abfd
) = line_filepos
;
671 /* Figure out the largest number of symbols in an input BFD. Take
672 the opportunity to clear the output_has_begun fields of all the
675 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
679 sub
->output_has_begun
= false;
680 sz
= obj_raw_syment_count (sub
);
681 if (sz
> max_sym_count
)
685 /* Allocate some buffers used while linking. */
686 finfo
.internal_syms
= ((struct internal_syment
*)
687 bfd_malloc (max_sym_count
688 * sizeof (struct internal_syment
)));
689 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
690 * sizeof (asection
*));
691 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
692 finfo
.outsyms
= ((bfd_byte
*)
693 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
694 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
695 * bfd_coff_linesz (abfd
));
696 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
697 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
698 if (! info
->relocateable
)
699 finfo
.internal_relocs
= ((struct internal_reloc
*)
700 bfd_malloc (max_reloc_count
701 * sizeof (struct internal_reloc
)));
702 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
703 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
704 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
705 || finfo
.outsyms
== NULL
706 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
707 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
708 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
709 || (! info
->relocateable
710 && finfo
.internal_relocs
== NULL
711 && max_reloc_count
> 0))
714 /* We now know the position of everything in the file, except that
715 we don't know the size of the symbol table and therefore we don't
716 know where the string table starts. We just build the string
717 table in memory as we go along. We process all the relocations
718 for a single input file at once. */
719 obj_raw_syment_count (abfd
) = 0;
721 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
723 if (! bfd_coff_start_final_link (abfd
, info
))
727 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
729 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
731 if (p
->type
== bfd_indirect_link_order
732 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
733 == bfd_target_coff_flavour
))
735 sub
= p
->u
.indirect
.section
->owner
;
736 if (! sub
->output_has_begun
)
738 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
740 sub
->output_has_begun
= true;
743 else if (p
->type
== bfd_section_reloc_link_order
744 || p
->type
== bfd_symbol_reloc_link_order
)
746 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
751 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
757 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
759 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
760 debug_merge_allocated
= false;
762 if (finfo
.internal_syms
!= NULL
)
764 free (finfo
.internal_syms
);
765 finfo
.internal_syms
= NULL
;
767 if (finfo
.sec_ptrs
!= NULL
)
769 free (finfo
.sec_ptrs
);
770 finfo
.sec_ptrs
= NULL
;
772 if (finfo
.sym_indices
!= NULL
)
774 free (finfo
.sym_indices
);
775 finfo
.sym_indices
= NULL
;
777 if (finfo
.linenos
!= NULL
)
779 free (finfo
.linenos
);
780 finfo
.linenos
= NULL
;
782 if (finfo
.contents
!= NULL
)
784 free (finfo
.contents
);
785 finfo
.contents
= NULL
;
787 if (finfo
.external_relocs
!= NULL
)
789 free (finfo
.external_relocs
);
790 finfo
.external_relocs
= NULL
;
792 if (finfo
.internal_relocs
!= NULL
)
794 free (finfo
.internal_relocs
);
795 finfo
.internal_relocs
= NULL
;
798 /* The value of the last C_FILE symbol is supposed to be the symbol
799 index of the first external symbol. Write it out again if
801 if (finfo
.last_file_index
!= -1
802 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
804 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
805 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
806 (PTR
) finfo
.outsyms
);
808 (obj_sym_filepos (abfd
)
809 + finfo
.last_file_index
* symesz
),
811 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
815 /* Write out the global symbols. */
816 finfo
.failed
= false;
817 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
822 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
823 if (finfo
.outsyms
!= NULL
)
825 free (finfo
.outsyms
);
826 finfo
.outsyms
= NULL
;
829 if (info
->relocateable
)
831 /* Now that we have written out all the global symbols, we know
832 the symbol indices to use for relocs against them, and we can
833 finally write out the relocs. */
834 external_relocs
= ((bfd_byte
*)
835 bfd_malloc (max_output_reloc_count
* relsz
));
836 if (external_relocs
== NULL
)
839 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
841 struct internal_reloc
*irel
;
842 struct internal_reloc
*irelend
;
843 struct coff_link_hash_entry
**rel_hash
;
846 if (o
->reloc_count
== 0)
849 irel
= finfo
.section_info
[o
->target_index
].relocs
;
850 irelend
= irel
+ o
->reloc_count
;
851 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
852 erel
= external_relocs
;
853 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
855 if (*rel_hash
!= NULL
)
857 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
858 irel
->r_symndx
= (*rel_hash
)->indx
;
860 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
863 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
864 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
865 abfd
) != relsz
* o
->reloc_count
)
869 free (external_relocs
);
870 external_relocs
= NULL
;
873 /* Free up the section information. */
874 if (finfo
.section_info
!= NULL
)
878 for (i
= 0; i
< abfd
->section_count
; i
++)
880 if (finfo
.section_info
[i
].relocs
!= NULL
)
881 free (finfo
.section_info
[i
].relocs
);
882 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
883 free (finfo
.section_info
[i
].rel_hashes
);
885 free (finfo
.section_info
);
886 finfo
.section_info
= NULL
;
889 /* If we have optimized stabs strings, output them. */
890 if (coff_hash_table (info
)->stab_info
!= NULL
)
892 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
896 /* Write out the string table. */
897 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
900 (obj_sym_filepos (abfd
)
901 + obj_raw_syment_count (abfd
) * symesz
),
905 #if STRING_SIZE_SIZE == 4
907 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
908 (bfd_byte
*) strbuf
);
910 #error Change bfd_h_put_32
913 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
916 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
920 _bfd_stringtab_free (finfo
.strtab
);
922 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
923 not try to write out the symbols. */
924 bfd_get_symcount (abfd
) = 0;
929 if (debug_merge_allocated
)
930 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
931 if (finfo
.strtab
!= NULL
)
932 _bfd_stringtab_free (finfo
.strtab
);
933 if (finfo
.section_info
!= NULL
)
937 for (i
= 0; i
< abfd
->section_count
; i
++)
939 if (finfo
.section_info
[i
].relocs
!= NULL
)
940 free (finfo
.section_info
[i
].relocs
);
941 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
942 free (finfo
.section_info
[i
].rel_hashes
);
944 free (finfo
.section_info
);
946 if (finfo
.internal_syms
!= NULL
)
947 free (finfo
.internal_syms
);
948 if (finfo
.sec_ptrs
!= NULL
)
949 free (finfo
.sec_ptrs
);
950 if (finfo
.sym_indices
!= NULL
)
951 free (finfo
.sym_indices
);
952 if (finfo
.outsyms
!= NULL
)
953 free (finfo
.outsyms
);
954 if (finfo
.linenos
!= NULL
)
955 free (finfo
.linenos
);
956 if (finfo
.contents
!= NULL
)
957 free (finfo
.contents
);
958 if (finfo
.external_relocs
!= NULL
)
959 free (finfo
.external_relocs
);
960 if (finfo
.internal_relocs
!= NULL
)
961 free (finfo
.internal_relocs
);
962 if (external_relocs
!= NULL
)
963 free (external_relocs
);
967 /* parse out a -heap <reserved>,<commit> line */
970 dores_com (ptr
, output_bfd
, heap
)
975 if (coff_data(output_bfd
)->pe
)
977 int val
= strtoul (ptr
, &ptr
, 0);
979 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
981 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
985 int val
= strtoul (ptr
+1, &ptr
, 0);
987 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
989 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
995 static char *get_name(ptr
, dst
)
1002 while (*ptr
&& *ptr
!= ' ')
1008 /* Process any magic embedded commands in a section called .drectve */
1011 process_embedded_commands (output_bfd
, info
, abfd
)
1013 struct bfd_link_info
*info
;
1016 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1023 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1026 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1031 e
= copy
+ sec
->_raw_size
;
1032 for (s
= copy
; s
< e
; )
1038 if (strncmp (s
,"-attr", 5) == 0)
1050 s
= get_name(s
, &name
);
1051 s
= get_name(s
, &attribs
);
1071 asec
= bfd_get_section_by_name (abfd
, name
);
1074 asec
->flags
|= SEC_CODE
;
1076 asec
->flags
|= SEC_READONLY
;
1079 else if (strncmp (s
,"-heap", 5) == 0)
1081 s
= dores_com (s
+5, output_bfd
, 1);
1083 else if (strncmp (s
,"-stack", 6) == 0)
1085 s
= dores_com (s
+6, output_bfd
, 0);
1094 /* Link an input file into the linker output file. This function
1095 handles all the sections and relocations of the input file at once. */
1098 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1099 struct coff_final_link_info
*finfo
;
1102 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1103 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1104 asection
*, struct internal_reloc
*,
1107 const char *strings
;
1108 bfd_size_type syment_base
;
1109 unsigned int n_tmask
;
1110 unsigned int n_btshft
;
1112 bfd_size_type isymesz
;
1113 bfd_size_type osymesz
;
1114 bfd_size_type linesz
;
1117 struct internal_syment
*isymp
;
1120 unsigned long output_index
;
1122 struct coff_link_hash_entry
**sym_hash
;
1125 /* Move all the symbols to the output file. */
1127 output_bfd
= finfo
->output_bfd
;
1128 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1130 syment_base
= obj_raw_syment_count (output_bfd
);
1131 isymesz
= bfd_coff_symesz (input_bfd
);
1132 osymesz
= bfd_coff_symesz (output_bfd
);
1133 linesz
= bfd_coff_linesz (input_bfd
);
1134 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1136 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1137 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1139 /* Define macros so that ISFCN, et. al., macros work correctly. */
1140 #define N_TMASK n_tmask
1141 #define N_BTSHFT n_btshft
1144 if (! finfo
->info
->keep_memory
)
1147 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1150 if (! _bfd_coff_get_external_symbols (input_bfd
))
1153 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1154 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1155 isymp
= finfo
->internal_syms
;
1156 secpp
= finfo
->sec_ptrs
;
1157 indexp
= finfo
->sym_indices
;
1158 output_index
= syment_base
;
1159 outsym
= finfo
->outsyms
;
1161 if (coff_data (output_bfd
)->pe
)
1163 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1167 while (esym
< esym_end
)
1169 struct internal_syment isym
;
1174 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1176 /* Make a copy of *isymp so that the relocate_section function
1177 always sees the original values. This is more reliable than
1178 always recomputing the symbol value even if we are stripping
1182 if (isym
.n_scnum
!= 0)
1183 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1186 if (isym
.n_value
== 0)
1187 *secpp
= bfd_und_section_ptr
;
1189 *secpp
= bfd_com_section_ptr
;
1196 add
= 1 + isym
.n_numaux
;
1198 /* If we are stripping all symbols, we want to skip this one. */
1199 if (finfo
->info
->strip
== strip_all
)
1204 if (isym
.n_sclass
== C_EXT
1205 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1207 /* This is a global symbol. Global symbols come at the
1208 end of the symbol table, so skip them for now.
1209 Function symbols, however, are an exception, and are
1210 not moved to the end. */
1212 if (! ISFCN (isym
.n_type
))
1217 /* This is a local symbol. Skip it if we are discarding
1219 if (finfo
->info
->discard
== discard_all
)
1224 /* If we stripping debugging symbols, and this is a debugging
1225 symbol, then skip it. */
1227 && finfo
->info
->strip
== strip_debugger
1228 && isym
.n_scnum
== N_DEBUG
)
1231 /* If some symbols are stripped based on the name, work out the
1232 name and decide whether to skip this symbol. */
1234 && (finfo
->info
->strip
== strip_some
1235 || finfo
->info
->discard
== discard_l
))
1238 char buf
[SYMNMLEN
+ 1];
1240 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1244 if ((finfo
->info
->strip
== strip_some
1245 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1248 && finfo
->info
->discard
== discard_l
1249 && strncmp (name
, finfo
->info
->lprefix
,
1250 finfo
->info
->lprefix_len
) == 0))
1254 /* If this is an enum, struct, or union tag, see if we have
1255 already output an identical type. */
1257 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1258 && (isym
.n_sclass
== C_ENTAG
1259 || isym
.n_sclass
== C_STRTAG
1260 || isym
.n_sclass
== C_UNTAG
)
1261 && isym
.n_numaux
== 1)
1264 char buf
[SYMNMLEN
+ 1];
1265 struct coff_debug_merge_hash_entry
*mh
;
1266 struct coff_debug_merge_type
*mt
;
1267 union internal_auxent aux
;
1268 struct coff_debug_merge_element
**epp
;
1269 bfd_byte
*esl
, *eslend
;
1270 struct internal_syment
*islp
;
1272 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1276 /* Ignore fake names invented by compiler; treat them all as
1278 if (*name
== '~' || *name
== '.' || *name
== '$'
1279 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1280 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1283 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1288 /* Allocate memory to hold type information. If this turns
1289 out to be a duplicate, we pass this address to
1291 mt
= ((struct coff_debug_merge_type
*)
1292 bfd_alloc (input_bfd
,
1293 sizeof (struct coff_debug_merge_type
)));
1296 mt
->class = isym
.n_sclass
;
1298 /* Pick up the aux entry, which points to the end of the tag
1300 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1301 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1304 /* Gather the elements. */
1305 epp
= &mt
->elements
;
1306 mt
->elements
= NULL
;
1308 esl
= esym
+ 2 * isymesz
;
1309 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1310 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1311 while (esl
< eslend
)
1313 const char *elename
;
1314 char elebuf
[SYMNMLEN
+ 1];
1317 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1319 *epp
= ((struct coff_debug_merge_element
*)
1320 bfd_alloc (input_bfd
,
1321 sizeof (struct coff_debug_merge_element
)));
1325 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1327 if (elename
== NULL
)
1330 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1333 strcpy (copy
, elename
);
1335 (*epp
)->name
= copy
;
1336 (*epp
)->type
= islp
->n_type
;
1338 if (islp
->n_numaux
>= 1
1339 && islp
->n_type
!= T_NULL
1340 && islp
->n_sclass
!= C_EOS
)
1342 union internal_auxent eleaux
;
1345 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1346 islp
->n_type
, islp
->n_sclass
, 0,
1347 islp
->n_numaux
, (PTR
) &eleaux
);
1348 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1350 /* FIXME: If this tagndx entry refers to a symbol
1351 defined later in this file, we just ignore it.
1352 Handling this correctly would be tedious, and may
1358 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1361 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1362 if ((*epp
)->tagndx
< 0)
1366 epp
= &(*epp
)->next
;
1369 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1370 islp
+= islp
->n_numaux
+ 1;
1373 /* See if we already have a definition which matches this
1374 type. We always output the type if it has no elements,
1376 if (mt
->elements
== NULL
)
1377 bfd_release (input_bfd
, (PTR
) mt
);
1380 struct coff_debug_merge_type
*mtl
;
1382 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1384 struct coff_debug_merge_element
*me
, *mel
;
1386 if (mtl
->class != mt
->class)
1389 for (me
= mt
->elements
, mel
= mtl
->elements
;
1390 me
!= NULL
&& mel
!= NULL
;
1391 me
= me
->next
, mel
= mel
->next
)
1393 if (strcmp (me
->name
, mel
->name
) != 0
1394 || me
->type
!= mel
->type
1395 || me
->tagndx
!= mel
->tagndx
)
1399 if (me
== NULL
&& mel
== NULL
)
1403 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1405 /* This is the first definition of this type. */
1406 mt
->indx
= output_index
;
1407 mt
->next
= mh
->types
;
1412 /* This is a redefinition which can be merged. */
1413 bfd_release (input_bfd
, (PTR
) mt
);
1414 *indexp
= mtl
->indx
;
1415 add
= (eslend
- esym
) / isymesz
;
1421 /* We now know whether we are to skip this symbol or not. */
1424 /* Adjust the symbol in order to output it. */
1426 if (isym
._n
._n_n
._n_zeroes
== 0
1427 && isym
._n
._n_n
._n_offset
!= 0)
1432 /* This symbol has a long name. Enter it in the string
1433 table we are building. Note that we do not check
1434 bfd_coff_symname_in_debug. That is only true for
1435 XCOFF, and XCOFF requires different linking code
1437 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1441 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1442 if (indx
== (bfd_size_type
) -1)
1444 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1447 if (isym
.n_scnum
> 0)
1449 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1450 isym
.n_value
+= ((*secpp
)->output_section
->vma
1451 + (*secpp
)->output_offset
1455 /* The value of a C_FILE symbol is the symbol index of the
1456 next C_FILE symbol. The value of the last C_FILE symbol
1457 is the symbol index to the first external symbol
1458 (actually, coff_renumber_symbols does not get this
1459 right--it just sets the value of the last C_FILE symbol
1460 to zero--and nobody has ever complained about it). We
1461 try to get this right, below, just before we write the
1462 symbols out, but in the general case we may have to write
1463 the symbol out twice. */
1464 if (isym
.n_sclass
== C_FILE
)
1466 if (finfo
->last_file_index
!= -1
1467 && finfo
->last_file
.n_value
!= (long) output_index
)
1469 /* We must correct the value of the last C_FILE entry. */
1470 finfo
->last_file
.n_value
= output_index
;
1471 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1473 /* The last C_FILE symbol is in this input file. */
1474 bfd_coff_swap_sym_out (output_bfd
,
1475 (PTR
) &finfo
->last_file
,
1476 (PTR
) (finfo
->outsyms
1477 + ((finfo
->last_file_index
1483 /* We have already written out the last C_FILE
1484 symbol. We need to write it out again. We
1485 borrow *outsym temporarily. */
1486 bfd_coff_swap_sym_out (output_bfd
,
1487 (PTR
) &finfo
->last_file
,
1489 if (bfd_seek (output_bfd
,
1490 (obj_sym_filepos (output_bfd
)
1491 + finfo
->last_file_index
* osymesz
),
1493 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1499 finfo
->last_file_index
= output_index
;
1500 finfo
->last_file
= isym
;
1503 /* Output the symbol. */
1505 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1507 *indexp
= output_index
;
1512 struct coff_link_hash_entry
*h
;
1514 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1516 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1519 /* This can happen if there were errors earlier in
1521 bfd_set_error (bfd_error_bad_value
);
1524 h
->indx
= output_index
;
1527 output_index
+= add
;
1528 outsym
+= add
* osymesz
;
1531 esym
+= add
* isymesz
;
1535 for (--add
; add
> 0; --add
)
1542 /* Fix up the aux entries. This must be done in a separate pass,
1543 because we don't know the correct symbol indices until we have
1544 already decided which symbols we are going to keep. */
1546 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1547 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1548 isymp
= finfo
->internal_syms
;
1549 indexp
= finfo
->sym_indices
;
1550 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1551 outsym
= finfo
->outsyms
;
1552 while (esym
< esym_end
)
1556 add
= 1 + isymp
->n_numaux
;
1559 || (bfd_size_type
) *indexp
< syment_base
)
1560 && (*sym_hash
== NULL
1561 || (*sym_hash
)->auxbfd
!= input_bfd
))
1562 esym
+= add
* isymesz
;
1565 struct coff_link_hash_entry
*h
;
1573 /* The m68k-motorola-sysv assembler will sometimes
1574 generate two symbols with the same name, but only one
1575 will have aux entries. */
1576 BFD_ASSERT (isymp
->n_numaux
== 0
1577 || h
->numaux
== isymp
->n_numaux
);
1585 /* Handle the aux entries. This handling is based on
1586 coff_pointerize_aux. I don't know if it always correct. */
1587 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1589 union internal_auxent aux
;
1590 union internal_auxent
*auxp
;
1596 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1597 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1602 if (isymp
->n_sclass
== C_FILE
)
1604 /* If this is a long filename, we must put it in the
1606 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1607 && auxp
->x_file
.x_n
.x_offset
!= 0)
1609 const char *filename
;
1612 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1613 >= STRING_SIZE_SIZE
);
1614 if (strings
== NULL
)
1616 strings
= _bfd_coff_read_string_table (input_bfd
);
1617 if (strings
== NULL
)
1620 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1621 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1623 if (indx
== (bfd_size_type
) -1)
1625 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1628 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1632 if (ISFCN (isymp
->n_type
)
1633 || ISTAG (isymp
->n_sclass
)
1634 || isymp
->n_sclass
== C_BLOCK
1635 || isymp
->n_sclass
== C_FCN
)
1637 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1639 && indx
< obj_raw_syment_count (input_bfd
))
1641 /* We look forward through the symbol for
1642 the index of the next symbol we are going
1643 to include. I don't know if this is
1645 while ((finfo
->sym_indices
[indx
] < 0
1646 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1648 && indx
< obj_raw_syment_count (input_bfd
))
1650 if (indx
>= obj_raw_syment_count (input_bfd
))
1651 indx
= output_index
;
1653 indx
= finfo
->sym_indices
[indx
];
1654 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1658 indx
= auxp
->x_sym
.x_tagndx
.l
;
1659 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1663 symindx
= finfo
->sym_indices
[indx
];
1665 auxp
->x_sym
.x_tagndx
.l
= 0;
1667 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1670 /* The .bf symbols are supposed to be linked through
1671 the endndx field. We need to carry this list
1672 across object files. */
1675 && isymp
->n_sclass
== C_FCN
1676 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1677 || isymp
->_n
._n_n
._n_offset
== 0)
1678 && isymp
->_n
._n_name
[0] == '.'
1679 && isymp
->_n
._n_name
[1] == 'b'
1680 && isymp
->_n
._n_name
[2] == 'f'
1681 && isymp
->_n
._n_name
[3] == '\0')
1683 if (finfo
->last_bf_index
!= -1)
1685 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1688 if ((bfd_size_type
) finfo
->last_bf_index
1693 /* The last .bf symbol is in this input
1694 file. This will only happen if the
1695 assembler did not set up the .bf
1696 endndx symbols correctly. */
1697 auxout
= (PTR
) (finfo
->outsyms
1698 + ((finfo
->last_bf_index
1701 bfd_coff_swap_aux_out (output_bfd
,
1702 (PTR
) &finfo
->last_bf
,
1710 /* We have already written out the last
1711 .bf aux entry. We need to write it
1712 out again. We borrow *outsym
1713 temporarily. FIXME: This case should
1715 bfd_coff_swap_aux_out (output_bfd
,
1716 (PTR
) &finfo
->last_bf
,
1721 if (bfd_seek (output_bfd
,
1722 (obj_sym_filepos (output_bfd
)
1723 + finfo
->last_bf_index
* osymesz
),
1725 || bfd_write (outsym
, osymesz
, 1,
1726 output_bfd
) != osymesz
)
1731 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1732 finfo
->last_bf_index
= -1;
1735 /* The endndx field of this aux entry must
1736 be updated with the symbol number of the
1738 finfo
->last_bf
= *auxp
;
1739 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1748 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1749 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1763 /* Relocate the line numbers, unless we are stripping them. */
1764 if (finfo
->info
->strip
== strip_none
1765 || finfo
->info
->strip
== strip_some
)
1767 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1773 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1774 build_link_order in ldwrite.c will not have created a
1775 link order, which means that we will not have seen this
1776 input section in _bfd_coff_final_link, which means that
1777 we will not have allocated space for the line numbers of
1778 this section. I don't think line numbers can be
1779 meaningful for a section which does not have
1780 SEC_HAS_CONTENTS set, but, if they do, this must be
1782 if (o
->lineno_count
== 0
1783 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1786 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1787 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1788 input_bfd
) != linesz
* o
->lineno_count
)
1791 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1792 eline
= finfo
->linenos
;
1793 elineend
= eline
+ linesz
* o
->lineno_count
;
1794 for (; eline
< elineend
; eline
+= linesz
)
1796 struct internal_lineno iline
;
1798 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1800 if (iline
.l_lnno
!= 0)
1801 iline
.l_addr
.l_paddr
+= offset
;
1802 else if (iline
.l_addr
.l_symndx
>= 0
1803 && ((unsigned long) iline
.l_addr
.l_symndx
1804 < obj_raw_syment_count (input_bfd
)))
1808 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1812 /* These line numbers are attached to a symbol
1813 which we are stripping. We should really
1814 just discard the line numbers, but that would
1815 be a pain because we have already counted
1821 struct internal_syment is
;
1822 union internal_auxent ia
;
1824 /* Fix up the lnnoptr field in the aux entry of
1825 the symbol. It turns out that we can't do
1826 this when we modify the symbol aux entries,
1827 because gas sometimes screws up the lnnoptr
1828 field and makes it an offset from the start
1829 of the line numbers rather than an absolute
1831 bfd_coff_swap_sym_in (output_bfd
,
1832 (PTR
) (finfo
->outsyms
1833 + ((indx
- syment_base
)
1836 if ((ISFCN (is
.n_type
)
1837 || is
.n_sclass
== C_BLOCK
)
1838 && is
.n_numaux
>= 1)
1842 auxptr
= (PTR
) (finfo
->outsyms
1843 + ((indx
- syment_base
+ 1)
1845 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1846 is
.n_type
, is
.n_sclass
,
1847 0, is
.n_numaux
, (PTR
) &ia
);
1848 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1849 (o
->output_section
->line_filepos
1850 + o
->output_section
->lineno_count
* linesz
1851 + eline
- finfo
->linenos
);
1852 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1853 is
.n_type
, is
.n_sclass
, 0,
1854 is
.n_numaux
, auxptr
);
1858 iline
.l_addr
.l_symndx
= indx
;
1861 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1864 if (bfd_seek (output_bfd
,
1865 (o
->output_section
->line_filepos
1866 + o
->output_section
->lineno_count
* linesz
),
1868 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1869 output_bfd
) != linesz
* o
->lineno_count
)
1872 o
->output_section
->lineno_count
+= o
->lineno_count
;
1876 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1877 symbol will be the first symbol in the next input file. In the
1878 normal case, this will save us from writing out the C_FILE symbol
1880 if (finfo
->last_file_index
!= -1
1881 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1883 finfo
->last_file
.n_value
= output_index
;
1884 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1885 (PTR
) (finfo
->outsyms
1886 + ((finfo
->last_file_index
- syment_base
)
1890 /* Write the modified symbols to the output file. */
1891 if (outsym
> finfo
->outsyms
)
1893 if (bfd_seek (output_bfd
,
1894 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1896 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1898 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1901 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1902 + (outsym
- finfo
->outsyms
) / osymesz
)
1905 obj_raw_syment_count (output_bfd
) = output_index
;
1908 /* Relocate the contents of each section. */
1909 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1910 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1913 struct coff_section_tdata
*secdata
;
1915 if (! o
->linker_mark
)
1917 /* This section was omitted from the link. */
1921 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
1922 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
1924 if ((o
->flags
& SEC_RELOC
) != 0
1925 && o
->reloc_count
!= 0)
1927 ((*_bfd_error_handler
)
1928 ("%s: relocs in section `%s', but it has no contents",
1929 bfd_get_filename (input_bfd
),
1930 bfd_get_section_name (input_bfd
, o
)));
1931 bfd_set_error (bfd_error_no_contents
);
1938 secdata
= coff_section_data (input_bfd
, o
);
1939 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
1940 contents
= secdata
->contents
;
1943 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1944 (file_ptr
) 0, o
->_raw_size
))
1946 contents
= finfo
->contents
;
1949 if ((o
->flags
& SEC_RELOC
) != 0)
1952 struct internal_reloc
*internal_relocs
;
1953 struct internal_reloc
*irel
;
1955 /* Read in the relocs. */
1956 target_index
= o
->output_section
->target_index
;
1957 internal_relocs
= (_bfd_coff_read_internal_relocs
1958 (input_bfd
, o
, false, finfo
->external_relocs
,
1959 finfo
->info
->relocateable
,
1960 (finfo
->info
->relocateable
1961 ? (finfo
->section_info
[target_index
].relocs
1962 + o
->output_section
->reloc_count
)
1963 : finfo
->internal_relocs
)));
1964 if (internal_relocs
== NULL
)
1967 /* Call processor specific code to relocate the section
1969 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1973 finfo
->internal_syms
,
1977 if (finfo
->info
->relocateable
)
1980 struct internal_reloc
*irelend
;
1981 struct coff_link_hash_entry
**rel_hash
;
1983 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1984 irel
= internal_relocs
;
1985 irelend
= irel
+ o
->reloc_count
;
1986 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1987 + o
->output_section
->reloc_count
);
1988 for (; irel
< irelend
; irel
++, rel_hash
++)
1990 struct coff_link_hash_entry
*h
;
1995 /* Adjust the reloc address and symbol index. */
1997 irel
->r_vaddr
+= offset
;
1999 if (irel
->r_symndx
== -1)
2004 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2012 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2015 /* This is a global symbol. */
2017 irel
->r_symndx
= h
->indx
;
2020 /* This symbol is being written at the end
2021 of the file, and we do not yet know the
2022 symbol index. We save the pointer to the
2023 hash table entry in the rel_hash list.
2024 We set the indx field to -2 to indicate
2025 that this symbol must not be stripped. */
2034 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2036 irel
->r_symndx
= indx
;
2039 struct internal_syment
*is
;
2041 char buf
[SYMNMLEN
+ 1];
2043 /* This reloc is against a symbol we are
2044 stripping. It would be possible to
2045 handle this case, but I don't think it's
2047 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2049 name
= (_bfd_coff_internal_syment_name
2050 (input_bfd
, is
, buf
));
2054 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2055 (finfo
->info
, name
, input_bfd
, o
,
2062 o
->output_section
->reloc_count
+= o
->reloc_count
;
2066 /* Write out the modified section contents. */
2067 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2069 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2070 contents
, o
->output_offset
,
2071 (o
->_cooked_size
!= 0
2078 if (! _bfd_write_section_stabs (output_bfd
, o
, &secdata
->stab_info
,
2084 if (! finfo
->info
->keep_memory
)
2086 if (! _bfd_coff_free_symbols (input_bfd
))
2093 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2096 _bfd_coff_write_global_sym (h
, data
)
2097 struct coff_link_hash_entry
*h
;
2100 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2102 struct internal_syment isym
;
2103 bfd_size_type symesz
;
2106 output_bfd
= finfo
->output_bfd
;
2112 && (finfo
->info
->strip
== strip_all
2113 || (finfo
->info
->strip
== strip_some
2114 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2115 h
->root
.root
.string
, false, false)
2119 switch (h
->root
.type
)
2122 case bfd_link_hash_new
:
2126 case bfd_link_hash_undefined
:
2127 case bfd_link_hash_undefweak
:
2128 isym
.n_scnum
= N_UNDEF
;
2132 case bfd_link_hash_defined
:
2133 case bfd_link_hash_defweak
:
2137 sec
= h
->root
.u
.def
.section
->output_section
;
2138 if (bfd_is_abs_section (sec
))
2139 isym
.n_scnum
= N_ABS
;
2141 isym
.n_scnum
= sec
->target_index
;
2142 isym
.n_value
= (h
->root
.u
.def
.value
2144 + h
->root
.u
.def
.section
->output_offset
);
2148 case bfd_link_hash_common
:
2149 isym
.n_scnum
= N_UNDEF
;
2150 isym
.n_value
= h
->root
.u
.c
.size
;
2153 case bfd_link_hash_indirect
:
2154 case bfd_link_hash_warning
:
2155 /* Just ignore these. They can't be handled anyhow. */
2159 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2160 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2167 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2169 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2171 if (indx
== (bfd_size_type
) -1)
2173 finfo
->failed
= true;
2176 isym
._n
._n_n
._n_zeroes
= 0;
2177 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2180 isym
.n_sclass
= h
->class;
2181 isym
.n_type
= h
->type
;
2183 if (isym
.n_sclass
== C_NULL
)
2184 isym
.n_sclass
= C_EXT
;
2186 isym
.n_numaux
= h
->numaux
;
2188 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2190 symesz
= bfd_coff_symesz (output_bfd
);
2192 if (bfd_seek (output_bfd
,
2193 (obj_sym_filepos (output_bfd
)
2194 + obj_raw_syment_count (output_bfd
) * symesz
),
2196 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2198 finfo
->failed
= true;
2202 h
->indx
= obj_raw_syment_count (output_bfd
);
2204 ++obj_raw_syment_count (output_bfd
);
2206 /* Write out any associated aux entries. There normally will be
2207 none. If there are any, I have no idea how to modify them. */
2208 for (i
= 0; i
< isym
.n_numaux
; i
++)
2210 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2211 isym
.n_sclass
, i
, isym
.n_numaux
,
2212 (PTR
) finfo
->outsyms
);
2213 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2215 finfo
->failed
= true;
2218 ++obj_raw_syment_count (output_bfd
);
2224 /* Handle a link order which is supposed to generate a reloc. */
2227 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2229 struct coff_final_link_info
*finfo
;
2230 asection
*output_section
;
2231 struct bfd_link_order
*link_order
;
2233 reloc_howto_type
*howto
;
2234 struct internal_reloc
*irel
;
2235 struct coff_link_hash_entry
**rel_hash_ptr
;
2237 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2240 bfd_set_error (bfd_error_bad_value
);
2244 if (link_order
->u
.reloc
.p
->addend
!= 0)
2248 bfd_reloc_status_type rstat
;
2251 size
= bfd_get_reloc_size (howto
);
2252 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2256 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2257 link_order
->u
.reloc
.p
->addend
, buf
);
2263 case bfd_reloc_outofrange
:
2265 case bfd_reloc_overflow
:
2266 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2268 (link_order
->type
== bfd_section_reloc_link_order
2269 ? bfd_section_name (output_bfd
,
2270 link_order
->u
.reloc
.p
->u
.section
)
2271 : link_order
->u
.reloc
.p
->u
.name
),
2272 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2273 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2280 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2281 (file_ptr
) link_order
->offset
, size
);
2287 /* Store the reloc information in the right place. It will get
2288 swapped and written out at the end of the final_link routine. */
2290 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2291 + output_section
->reloc_count
);
2292 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2293 + output_section
->reloc_count
);
2295 memset (irel
, 0, sizeof (struct internal_reloc
));
2296 *rel_hash_ptr
= NULL
;
2298 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2300 if (link_order
->type
== bfd_section_reloc_link_order
)
2302 /* We need to somehow locate a symbol in the right section. The
2303 symbol must either have a value of zero, or we must adjust
2304 the addend by the value of the symbol. FIXME: Write this
2305 when we need it. The old linker couldn't handle this anyhow. */
2307 *rel_hash_ptr
= NULL
;
2312 struct coff_link_hash_entry
*h
;
2314 h
= ((struct coff_link_hash_entry
*)
2315 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2316 link_order
->u
.reloc
.p
->u
.name
,
2317 false, false, true));
2321 irel
->r_symndx
= h
->indx
;
2324 /* Set the index to -2 to force this symbol to get
2333 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2334 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2335 (asection
*) NULL
, (bfd_vma
) 0)))
2341 /* FIXME: Is this always right? */
2342 irel
->r_type
= howto
->type
;
2344 /* r_size is only used on the RS/6000, which needs its own linker
2345 routines anyhow. r_extern is only used for ECOFF. */
2347 /* FIXME: What is the right value for r_offset? Is zero OK? */
2349 ++output_section
->reloc_count
;
2354 /* A basic reloc handling routine which may be used by processors with
2358 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2359 input_section
, contents
, relocs
, syms
,
2362 struct bfd_link_info
*info
;
2364 asection
*input_section
;
2366 struct internal_reloc
*relocs
;
2367 struct internal_syment
*syms
;
2368 asection
**sections
;
2370 struct internal_reloc
*rel
;
2371 struct internal_reloc
*relend
;
2374 relend
= rel
+ input_section
->reloc_count
;
2375 for (; rel
< relend
; rel
++)
2378 struct coff_link_hash_entry
*h
;
2379 struct internal_syment
*sym
;
2382 reloc_howto_type
*howto
;
2383 bfd_reloc_status_type rstat
;
2385 symndx
= rel
->r_symndx
;
2394 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2395 sym
= syms
+ symndx
;
2398 /* COFF treats common symbols in one of two ways. Either the
2399 size of the symbol is included in the section contents, or it
2400 is not. We assume that the size is not included, and force
2401 the rtype_to_howto function to adjust the addend as needed. */
2403 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2404 addend
= - sym
->n_value
;
2409 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2422 sec
= bfd_abs_section_ptr
;
2427 sec
= sections
[symndx
];
2428 val
= (sec
->output_section
->vma
2429 + sec
->output_offset
2436 if (h
->root
.type
== bfd_link_hash_defined
2437 || h
->root
.type
== bfd_link_hash_defweak
)
2441 sec
= h
->root
.u
.def
.section
;
2442 val
= (h
->root
.u
.def
.value
2443 + sec
->output_section
->vma
2444 + sec
->output_offset
);
2447 else if (! info
->relocateable
)
2449 if (! ((*info
->callbacks
->undefined_symbol
)
2450 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2451 rel
->r_vaddr
- input_section
->vma
)))
2456 if (info
->base_file
)
2458 /* Emit a reloc if the backend thinks it needs it. */
2459 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2461 /* relocation to a symbol in a section which
2462 isn't absolute - we output the address here
2464 bfd_vma addr
= rel
->r_vaddr
2465 - input_section
->vma
2466 + input_section
->output_offset
2467 + input_section
->output_section
->vma
;
2468 if (coff_data(output_bfd
)->pe
)
2469 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2470 /* FIXME: Shouldn't 4 be sizeof (addr)? */
2471 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2475 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2477 rel
->r_vaddr
- input_section
->vma
,
2486 case bfd_reloc_overflow
:
2489 char buf
[SYMNMLEN
+ 1];
2494 name
= h
->root
.root
.string
;
2497 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2502 if (! ((*info
->callbacks
->reloc_overflow
)
2503 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2504 input_section
, rel
->r_vaddr
- input_section
->vma
)))