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
];
1517 BFD_ASSERT (h
!= NULL
);
1518 h
->indx
= output_index
;
1521 output_index
+= add
;
1522 outsym
+= add
* osymesz
;
1525 esym
+= add
* isymesz
;
1529 for (--add
; add
> 0; --add
)
1536 /* Fix up the aux entries. This must be done in a separate pass,
1537 because we don't know the correct symbol indices until we have
1538 already decided which symbols we are going to keep. */
1540 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1541 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1542 isymp
= finfo
->internal_syms
;
1543 indexp
= finfo
->sym_indices
;
1544 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1545 outsym
= finfo
->outsyms
;
1546 while (esym
< esym_end
)
1550 add
= 1 + isymp
->n_numaux
;
1553 || (bfd_size_type
) *indexp
< syment_base
)
1554 && (*sym_hash
== NULL
1555 || (*sym_hash
)->auxbfd
!= input_bfd
))
1556 esym
+= add
* isymesz
;
1559 struct coff_link_hash_entry
*h
;
1567 /* The m68k-motorola-sysv assembler will sometimes
1568 generate two symbols with the same name, but only one
1569 will have aux entries. */
1570 BFD_ASSERT (isymp
->n_numaux
== 0
1571 || h
->numaux
== isymp
->n_numaux
);
1579 /* Handle the aux entries. This handling is based on
1580 coff_pointerize_aux. I don't know if it always correct. */
1581 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1583 union internal_auxent aux
;
1584 union internal_auxent
*auxp
;
1590 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1591 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1596 if (isymp
->n_sclass
== C_FILE
)
1598 /* If this is a long filename, we must put it in the
1600 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1601 && auxp
->x_file
.x_n
.x_offset
!= 0)
1603 const char *filename
;
1606 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1607 >= STRING_SIZE_SIZE
);
1608 if (strings
== NULL
)
1610 strings
= _bfd_coff_read_string_table (input_bfd
);
1611 if (strings
== NULL
)
1614 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1615 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1617 if (indx
== (bfd_size_type
) -1)
1619 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1622 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1626 if (ISFCN (isymp
->n_type
)
1627 || ISTAG (isymp
->n_sclass
)
1628 || isymp
->n_sclass
== C_BLOCK
1629 || isymp
->n_sclass
== C_FCN
)
1631 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1633 && indx
< obj_raw_syment_count (input_bfd
))
1635 /* We look forward through the symbol for
1636 the index of the next symbol we are going
1637 to include. I don't know if this is
1639 while ((finfo
->sym_indices
[indx
] < 0
1640 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1642 && indx
< obj_raw_syment_count (input_bfd
))
1644 if (indx
>= obj_raw_syment_count (input_bfd
))
1645 indx
= output_index
;
1647 indx
= finfo
->sym_indices
[indx
];
1648 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1652 indx
= auxp
->x_sym
.x_tagndx
.l
;
1653 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1657 symindx
= finfo
->sym_indices
[indx
];
1659 auxp
->x_sym
.x_tagndx
.l
= 0;
1661 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1664 /* The .bf symbols are supposed to be linked through
1665 the endndx field. We need to carry this list
1666 across object files. */
1669 && isymp
->n_sclass
== C_FCN
1670 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1671 || isymp
->_n
._n_n
._n_offset
== 0)
1672 && isymp
->_n
._n_name
[0] == '.'
1673 && isymp
->_n
._n_name
[1] == 'b'
1674 && isymp
->_n
._n_name
[2] == 'f'
1675 && isymp
->_n
._n_name
[3] == '\0')
1677 if (finfo
->last_bf_index
!= -1)
1679 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1682 if ((bfd_size_type
) finfo
->last_bf_index
1687 /* The last .bf symbol is in this input
1688 file. This will only happen if the
1689 assembler did not set up the .bf
1690 endndx symbols correctly. */
1691 auxout
= (PTR
) (finfo
->outsyms
1692 + ((finfo
->last_bf_index
1695 bfd_coff_swap_aux_out (output_bfd
,
1696 (PTR
) &finfo
->last_bf
,
1704 /* We have already written out the last
1705 .bf aux entry. We need to write it
1706 out again. We borrow *outsym
1707 temporarily. FIXME: This case should
1709 bfd_coff_swap_aux_out (output_bfd
,
1710 (PTR
) &finfo
->last_bf
,
1715 if (bfd_seek (output_bfd
,
1716 (obj_sym_filepos (output_bfd
)
1717 + finfo
->last_bf_index
* osymesz
),
1719 || bfd_write (outsym
, osymesz
, 1,
1720 output_bfd
) != osymesz
)
1725 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1726 finfo
->last_bf_index
= -1;
1729 /* The endndx field of this aux entry must
1730 be updated with the symbol number of the
1732 finfo
->last_bf
= *auxp
;
1733 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1742 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1743 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1757 /* Relocate the line numbers, unless we are stripping them. */
1758 if (finfo
->info
->strip
== strip_none
1759 || finfo
->info
->strip
== strip_some
)
1761 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1767 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1768 build_link_order in ldwrite.c will not have created a
1769 link order, which means that we will not have seen this
1770 input section in _bfd_coff_final_link, which means that
1771 we will not have allocated space for the line numbers of
1772 this section. I don't think line numbers can be
1773 meaningful for a section which does not have
1774 SEC_HAS_CONTENTS set, but, if they do, this must be
1776 if (o
->lineno_count
== 0
1777 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1780 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1781 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1782 input_bfd
) != linesz
* o
->lineno_count
)
1785 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1786 eline
= finfo
->linenos
;
1787 elineend
= eline
+ linesz
* o
->lineno_count
;
1788 for (; eline
< elineend
; eline
+= linesz
)
1790 struct internal_lineno iline
;
1792 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1794 if (iline
.l_lnno
!= 0)
1795 iline
.l_addr
.l_paddr
+= offset
;
1796 else if (iline
.l_addr
.l_symndx
>= 0
1797 && ((unsigned long) iline
.l_addr
.l_symndx
1798 < obj_raw_syment_count (input_bfd
)))
1802 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1806 /* These line numbers are attached to a symbol
1807 which we are stripping. We should really
1808 just discard the line numbers, but that would
1809 be a pain because we have already counted
1815 struct internal_syment is
;
1816 union internal_auxent ia
;
1818 /* Fix up the lnnoptr field in the aux entry of
1819 the symbol. It turns out that we can't do
1820 this when we modify the symbol aux entries,
1821 because gas sometimes screws up the lnnoptr
1822 field and makes it an offset from the start
1823 of the line numbers rather than an absolute
1825 bfd_coff_swap_sym_in (output_bfd
,
1826 (PTR
) (finfo
->outsyms
1827 + ((indx
- syment_base
)
1830 if ((ISFCN (is
.n_type
)
1831 || is
.n_sclass
== C_BLOCK
)
1832 && is
.n_numaux
>= 1)
1836 auxptr
= (PTR
) (finfo
->outsyms
1837 + ((indx
- syment_base
+ 1)
1839 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1840 is
.n_type
, is
.n_sclass
,
1841 0, is
.n_numaux
, (PTR
) &ia
);
1842 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1843 (o
->output_section
->line_filepos
1844 + o
->output_section
->lineno_count
* linesz
1845 + eline
- finfo
->linenos
);
1846 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1847 is
.n_type
, is
.n_sclass
, 0,
1848 is
.n_numaux
, auxptr
);
1852 iline
.l_addr
.l_symndx
= indx
;
1855 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1858 if (bfd_seek (output_bfd
,
1859 (o
->output_section
->line_filepos
1860 + o
->output_section
->lineno_count
* linesz
),
1862 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1863 output_bfd
) != linesz
* o
->lineno_count
)
1866 o
->output_section
->lineno_count
+= o
->lineno_count
;
1870 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1871 symbol will be the first symbol in the next input file. In the
1872 normal case, this will save us from writing out the C_FILE symbol
1874 if (finfo
->last_file_index
!= -1
1875 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1877 finfo
->last_file
.n_value
= output_index
;
1878 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1879 (PTR
) (finfo
->outsyms
1880 + ((finfo
->last_file_index
- syment_base
)
1884 /* Write the modified symbols to the output file. */
1885 if (outsym
> finfo
->outsyms
)
1887 if (bfd_seek (output_bfd
,
1888 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1890 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1892 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1895 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1896 + (outsym
- finfo
->outsyms
) / osymesz
)
1899 obj_raw_syment_count (output_bfd
) = output_index
;
1902 /* Relocate the contents of each section. */
1903 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1904 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1907 struct coff_section_tdata
*secdata
;
1909 if (! o
->linker_mark
)
1911 /* This section was omitted from the link. */
1915 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
1916 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
1918 if ((o
->flags
& SEC_RELOC
) != 0
1919 && o
->reloc_count
!= 0)
1921 ((*_bfd_error_handler
)
1922 ("%s: relocs in section `%s', but it has no contents",
1923 bfd_get_filename (input_bfd
),
1924 bfd_get_section_name (input_bfd
, o
)));
1925 bfd_set_error (bfd_error_no_contents
);
1932 secdata
= coff_section_data (input_bfd
, o
);
1933 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
1934 contents
= secdata
->contents
;
1937 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1938 (file_ptr
) 0, o
->_raw_size
))
1940 contents
= finfo
->contents
;
1943 if ((o
->flags
& SEC_RELOC
) != 0)
1946 struct internal_reloc
*internal_relocs
;
1947 struct internal_reloc
*irel
;
1949 /* Read in the relocs. */
1950 target_index
= o
->output_section
->target_index
;
1951 internal_relocs
= (_bfd_coff_read_internal_relocs
1952 (input_bfd
, o
, false, finfo
->external_relocs
,
1953 finfo
->info
->relocateable
,
1954 (finfo
->info
->relocateable
1955 ? (finfo
->section_info
[target_index
].relocs
1956 + o
->output_section
->reloc_count
)
1957 : finfo
->internal_relocs
)));
1958 if (internal_relocs
== NULL
)
1961 /* Call processor specific code to relocate the section
1963 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1967 finfo
->internal_syms
,
1971 if (finfo
->info
->relocateable
)
1974 struct internal_reloc
*irelend
;
1975 struct coff_link_hash_entry
**rel_hash
;
1977 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1978 irel
= internal_relocs
;
1979 irelend
= irel
+ o
->reloc_count
;
1980 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1981 + o
->output_section
->reloc_count
);
1982 for (; irel
< irelend
; irel
++, rel_hash
++)
1984 struct coff_link_hash_entry
*h
;
1989 /* Adjust the reloc address and symbol index. */
1991 irel
->r_vaddr
+= offset
;
1993 if (irel
->r_symndx
== -1)
1998 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2006 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2009 /* This is a global symbol. */
2011 irel
->r_symndx
= h
->indx
;
2014 /* This symbol is being written at the end
2015 of the file, and we do not yet know the
2016 symbol index. We save the pointer to the
2017 hash table entry in the rel_hash list.
2018 We set the indx field to -2 to indicate
2019 that this symbol must not be stripped. */
2028 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2030 irel
->r_symndx
= indx
;
2033 struct internal_syment
*is
;
2035 char buf
[SYMNMLEN
+ 1];
2037 /* This reloc is against a symbol we are
2038 stripping. It would be possible to
2039 handle this case, but I don't think it's
2041 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2043 name
= (_bfd_coff_internal_syment_name
2044 (input_bfd
, is
, buf
));
2048 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2049 (finfo
->info
, name
, input_bfd
, o
,
2056 o
->output_section
->reloc_count
+= o
->reloc_count
;
2060 /* Write out the modified section contents. */
2061 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2063 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2064 contents
, o
->output_offset
,
2065 (o
->_cooked_size
!= 0
2072 if (! _bfd_write_section_stabs (output_bfd
, o
, &secdata
->stab_info
,
2078 if (! finfo
->info
->keep_memory
)
2080 if (! _bfd_coff_free_symbols (input_bfd
))
2087 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2090 _bfd_coff_write_global_sym (h
, data
)
2091 struct coff_link_hash_entry
*h
;
2094 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2096 struct internal_syment isym
;
2097 bfd_size_type symesz
;
2100 output_bfd
= finfo
->output_bfd
;
2106 && (finfo
->info
->strip
== strip_all
2107 || (finfo
->info
->strip
== strip_some
2108 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2109 h
->root
.root
.string
, false, false)
2113 switch (h
->root
.type
)
2116 case bfd_link_hash_new
:
2120 case bfd_link_hash_undefined
:
2121 case bfd_link_hash_undefweak
:
2122 isym
.n_scnum
= N_UNDEF
;
2126 case bfd_link_hash_defined
:
2127 case bfd_link_hash_defweak
:
2131 sec
= h
->root
.u
.def
.section
->output_section
;
2132 if (bfd_is_abs_section (sec
))
2133 isym
.n_scnum
= N_ABS
;
2135 isym
.n_scnum
= sec
->target_index
;
2136 isym
.n_value
= (h
->root
.u
.def
.value
2138 + h
->root
.u
.def
.section
->output_offset
);
2142 case bfd_link_hash_common
:
2143 isym
.n_scnum
= N_UNDEF
;
2144 isym
.n_value
= h
->root
.u
.c
.size
;
2147 case bfd_link_hash_indirect
:
2148 case bfd_link_hash_warning
:
2149 /* Just ignore these. They can't be handled anyhow. */
2153 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2154 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2161 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2163 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2165 if (indx
== (bfd_size_type
) -1)
2167 finfo
->failed
= true;
2170 isym
._n
._n_n
._n_zeroes
= 0;
2171 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2174 isym
.n_sclass
= h
->class;
2175 isym
.n_type
= h
->type
;
2177 if (isym
.n_sclass
== C_NULL
)
2178 isym
.n_sclass
= C_EXT
;
2180 isym
.n_numaux
= h
->numaux
;
2182 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2184 symesz
= bfd_coff_symesz (output_bfd
);
2186 if (bfd_seek (output_bfd
,
2187 (obj_sym_filepos (output_bfd
)
2188 + obj_raw_syment_count (output_bfd
) * symesz
),
2190 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2192 finfo
->failed
= true;
2196 h
->indx
= obj_raw_syment_count (output_bfd
);
2198 ++obj_raw_syment_count (output_bfd
);
2200 /* Write out any associated aux entries. There normally will be
2201 none. If there are any, I have no idea how to modify them. */
2202 for (i
= 0; i
< isym
.n_numaux
; i
++)
2204 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2205 isym
.n_sclass
, i
, isym
.n_numaux
,
2206 (PTR
) finfo
->outsyms
);
2207 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2209 finfo
->failed
= true;
2212 ++obj_raw_syment_count (output_bfd
);
2218 /* Handle a link order which is supposed to generate a reloc. */
2221 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2223 struct coff_final_link_info
*finfo
;
2224 asection
*output_section
;
2225 struct bfd_link_order
*link_order
;
2227 reloc_howto_type
*howto
;
2228 struct internal_reloc
*irel
;
2229 struct coff_link_hash_entry
**rel_hash_ptr
;
2231 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2234 bfd_set_error (bfd_error_bad_value
);
2238 if (link_order
->u
.reloc
.p
->addend
!= 0)
2242 bfd_reloc_status_type rstat
;
2245 size
= bfd_get_reloc_size (howto
);
2246 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2250 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2251 link_order
->u
.reloc
.p
->addend
, buf
);
2257 case bfd_reloc_outofrange
:
2259 case bfd_reloc_overflow
:
2260 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2262 (link_order
->type
== bfd_section_reloc_link_order
2263 ? bfd_section_name (output_bfd
,
2264 link_order
->u
.reloc
.p
->u
.section
)
2265 : link_order
->u
.reloc
.p
->u
.name
),
2266 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2267 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2274 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2275 (file_ptr
) link_order
->offset
, size
);
2281 /* Store the reloc information in the right place. It will get
2282 swapped and written out at the end of the final_link routine. */
2284 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2285 + output_section
->reloc_count
);
2286 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2287 + output_section
->reloc_count
);
2289 memset (irel
, 0, sizeof (struct internal_reloc
));
2290 *rel_hash_ptr
= NULL
;
2292 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2294 if (link_order
->type
== bfd_section_reloc_link_order
)
2296 /* We need to somehow locate a symbol in the right section. The
2297 symbol must either have a value of zero, or we must adjust
2298 the addend by the value of the symbol. FIXME: Write this
2299 when we need it. The old linker couldn't handle this anyhow. */
2301 *rel_hash_ptr
= NULL
;
2306 struct coff_link_hash_entry
*h
;
2308 h
= ((struct coff_link_hash_entry
*)
2309 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2310 link_order
->u
.reloc
.p
->u
.name
,
2311 false, false, true));
2315 irel
->r_symndx
= h
->indx
;
2318 /* Set the index to -2 to force this symbol to get
2327 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2328 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2329 (asection
*) NULL
, (bfd_vma
) 0)))
2335 /* FIXME: Is this always right? */
2336 irel
->r_type
= howto
->type
;
2338 /* r_size is only used on the RS/6000, which needs its own linker
2339 routines anyhow. r_extern is only used for ECOFF. */
2341 /* FIXME: What is the right value for r_offset? Is zero OK? */
2343 ++output_section
->reloc_count
;
2348 /* A basic reloc handling routine which may be used by processors with
2352 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2353 input_section
, contents
, relocs
, syms
,
2356 struct bfd_link_info
*info
;
2358 asection
*input_section
;
2360 struct internal_reloc
*relocs
;
2361 struct internal_syment
*syms
;
2362 asection
**sections
;
2364 struct internal_reloc
*rel
;
2365 struct internal_reloc
*relend
;
2368 relend
= rel
+ input_section
->reloc_count
;
2369 for (; rel
< relend
; rel
++)
2372 struct coff_link_hash_entry
*h
;
2373 struct internal_syment
*sym
;
2376 reloc_howto_type
*howto
;
2377 bfd_reloc_status_type rstat
;
2379 symndx
= rel
->r_symndx
;
2388 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2389 sym
= syms
+ symndx
;
2392 /* COFF treats common symbols in one of two ways. Either the
2393 size of the symbol is included in the section contents, or it
2394 is not. We assume that the size is not included, and force
2395 the rtype_to_howto function to adjust the addend as needed. */
2397 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2398 addend
= - sym
->n_value
;
2403 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2416 sec
= bfd_abs_section_ptr
;
2421 sec
= sections
[symndx
];
2422 val
= (sec
->output_section
->vma
2423 + sec
->output_offset
2430 if (h
->root
.type
== bfd_link_hash_defined
2431 || h
->root
.type
== bfd_link_hash_defweak
)
2435 sec
= h
->root
.u
.def
.section
;
2436 val
= (h
->root
.u
.def
.value
2437 + sec
->output_section
->vma
2438 + sec
->output_offset
);
2441 else if (! info
->relocateable
)
2443 if (! ((*info
->callbacks
->undefined_symbol
)
2444 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2445 rel
->r_vaddr
- input_section
->vma
)))
2450 if (info
->base_file
)
2452 /* Emit a reloc if the backend thinks it needs it. */
2453 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2455 /* relocation to a symbol in a section which
2456 isn't absolute - we output the address here
2458 bfd_vma addr
= rel
->r_vaddr
2459 - input_section
->vma
2460 + input_section
->output_offset
2461 + input_section
->output_section
->vma
;
2462 if (coff_data(output_bfd
)->pe
)
2463 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2464 /* FIXME: Shouldn't 4 be sizeof (addr)? */
2465 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2469 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2471 rel
->r_vaddr
- input_section
->vma
,
2480 case bfd_reloc_overflow
:
2483 char buf
[SYMNMLEN
+ 1];
2488 name
= h
->root
.root
.string
;
2491 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2496 if (! ((*info
->callbacks
->reloc_overflow
)
2497 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2498 input_section
, rel
->r_vaddr
- input_section
->vma
)))