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 (*sym_hash
)->type
= sym
.n_type
;
385 (*sym_hash
)->numaux
= sym
.n_numaux
;
386 (*sym_hash
)->auxbfd
= abfd
;
387 if (sym
.n_numaux
!= 0)
389 union internal_auxent
*alloc
;
392 union internal_auxent
*iaux
;
394 alloc
= ((union internal_auxent
*)
395 bfd_hash_allocate (&info
->hash
->table
,
397 * sizeof (*alloc
))));
400 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
402 i
++, eaux
+= symesz
, iaux
++)
403 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
404 sym
.n_sclass
, i
, sym
.n_numaux
,
406 (*sym_hash
)->aux
= alloc
;
412 esym
+= (sym
.n_numaux
+ 1) * symesz
;
413 sym_hash
+= sym
.n_numaux
+ 1;
416 /* If this is a non-traditional, non-relocateable link, try to
417 optimize the handling of any .stab/.stabstr sections. */
418 if (! info
->relocateable
419 && ! info
->traditional_format
420 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
421 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
423 asection
*stab
, *stabstr
;
425 stab
= bfd_get_section_by_name (abfd
, ".stab");
428 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
432 struct coff_link_hash_table
*table
;
433 struct coff_section_tdata
*secdata
;
435 secdata
= coff_section_data (abfd
, stab
);
439 (PTR
) bfd_zalloc (abfd
,
440 sizeof (struct coff_section_tdata
));
441 if (stab
->used_by_bfd
== NULL
)
443 secdata
= coff_section_data (abfd
, stab
);
446 table
= coff_hash_table (info
);
448 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
450 &secdata
->stab_info
))
459 /* Do the final link step. */
462 _bfd_coff_final_link (abfd
, info
)
464 struct bfd_link_info
*info
;
466 bfd_size_type symesz
;
467 struct coff_final_link_info finfo
;
468 boolean debug_merge_allocated
;
470 struct bfd_link_order
*p
;
471 size_t max_sym_count
;
472 size_t max_lineno_count
;
473 size_t max_reloc_count
;
474 size_t max_output_reloc_count
;
475 size_t max_contents_size
;
476 file_ptr rel_filepos
;
478 file_ptr line_filepos
;
481 bfd_byte
*external_relocs
= NULL
;
482 char strbuf
[STRING_SIZE_SIZE
];
484 symesz
= bfd_coff_symesz (abfd
);
487 finfo
.output_bfd
= abfd
;
489 finfo
.section_info
= NULL
;
490 finfo
.last_file_index
= -1;
491 finfo
.last_bf_index
= -1;
492 finfo
.internal_syms
= NULL
;
493 finfo
.sec_ptrs
= NULL
;
494 finfo
.sym_indices
= NULL
;
495 finfo
.outsyms
= NULL
;
496 finfo
.linenos
= NULL
;
497 finfo
.contents
= NULL
;
498 finfo
.external_relocs
= NULL
;
499 finfo
.internal_relocs
= NULL
;
500 debug_merge_allocated
= false;
502 coff_data (abfd
)->link_info
= info
;
504 finfo
.strtab
= _bfd_stringtab_init ();
505 if (finfo
.strtab
== NULL
)
508 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
510 debug_merge_allocated
= true;
512 /* Compute the file positions for all the sections. */
513 if (! abfd
->output_has_begun
)
514 bfd_coff_compute_section_file_positions (abfd
);
516 /* Count the line numbers and relocation entries required for the
517 output file. Set the file positions for the relocs. */
518 rel_filepos
= obj_relocbase (abfd
);
519 relsz
= bfd_coff_relsz (abfd
);
520 max_contents_size
= 0;
521 max_lineno_count
= 0;
524 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
528 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
530 if (p
->type
== bfd_indirect_link_order
)
534 sec
= p
->u
.indirect
.section
;
536 /* Mark all sections which are to be included in the
537 link. This will normally be every section. We need
538 to do this so that we can identify any sections which
539 the linker has decided to not include. */
540 sec
->linker_mark
= true;
542 if (info
->strip
== strip_none
543 || info
->strip
== strip_some
)
544 o
->lineno_count
+= sec
->lineno_count
;
546 if (info
->relocateable
)
547 o
->reloc_count
+= sec
->reloc_count
;
549 if (sec
->_raw_size
> max_contents_size
)
550 max_contents_size
= sec
->_raw_size
;
551 if (sec
->lineno_count
> max_lineno_count
)
552 max_lineno_count
= sec
->lineno_count
;
553 if (sec
->reloc_count
> max_reloc_count
)
554 max_reloc_count
= sec
->reloc_count
;
556 else if (info
->relocateable
557 && (p
->type
== bfd_section_reloc_link_order
558 || p
->type
== bfd_symbol_reloc_link_order
))
561 if (o
->reloc_count
== 0)
565 o
->flags
|= SEC_RELOC
;
566 o
->rel_filepos
= rel_filepos
;
567 rel_filepos
+= o
->reloc_count
* relsz
;
571 /* If doing a relocateable link, allocate space for the pointers we
573 if (info
->relocateable
)
577 /* We use section_count + 1, rather than section_count, because
578 the target_index fields are 1 based. */
580 ((struct coff_link_section_info
*)
581 bfd_malloc ((abfd
->section_count
+ 1)
582 * sizeof (struct coff_link_section_info
)));
583 if (finfo
.section_info
== NULL
)
585 for (i
= 0; i
<= abfd
->section_count
; i
++)
587 finfo
.section_info
[i
].relocs
= NULL
;
588 finfo
.section_info
[i
].rel_hashes
= NULL
;
592 /* We now know the size of the relocs, so we can determine the file
593 positions of the line numbers. */
594 line_filepos
= rel_filepos
;
595 linesz
= bfd_coff_linesz (abfd
);
596 max_output_reloc_count
= 0;
597 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
599 if (o
->lineno_count
== 0)
603 o
->line_filepos
= line_filepos
;
604 line_filepos
+= o
->lineno_count
* linesz
;
607 if (o
->reloc_count
!= 0)
609 /* We don't know the indices of global symbols until we have
610 written out all the local symbols. For each section in
611 the output file, we keep an array of pointers to hash
612 table entries. Each entry in the array corresponds to a
613 reloc. When we find a reloc against a global symbol, we
614 set the corresponding entry in this array so that we can
615 fix up the symbol index after we have written out all the
618 Because of this problem, we also keep the relocs in
619 memory until the end of the link. This wastes memory,
620 but only when doing a relocateable link, which is not the
622 BFD_ASSERT (info
->relocateable
);
623 finfo
.section_info
[o
->target_index
].relocs
=
624 ((struct internal_reloc
*)
625 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
626 finfo
.section_info
[o
->target_index
].rel_hashes
=
627 ((struct coff_link_hash_entry
**)
628 bfd_malloc (o
->reloc_count
629 * sizeof (struct coff_link_hash_entry
*)));
630 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
631 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
634 if (o
->reloc_count
> max_output_reloc_count
)
635 max_output_reloc_count
= o
->reloc_count
;
638 /* Reset the reloc and lineno counts, so that we can use them to
639 count the number of entries we have output so far. */
644 obj_sym_filepos (abfd
) = line_filepos
;
646 /* Figure out the largest number of symbols in an input BFD. Take
647 the opportunity to clear the output_has_begun fields of all the
650 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
654 sub
->output_has_begun
= false;
655 sz
= obj_raw_syment_count (sub
);
656 if (sz
> max_sym_count
)
660 /* Allocate some buffers used while linking. */
661 finfo
.internal_syms
= ((struct internal_syment
*)
662 bfd_malloc (max_sym_count
663 * sizeof (struct internal_syment
)));
664 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
665 * sizeof (asection
*));
666 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
667 finfo
.outsyms
= ((bfd_byte
*)
668 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
669 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
670 * bfd_coff_linesz (abfd
));
671 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
672 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
673 if (! info
->relocateable
)
674 finfo
.internal_relocs
= ((struct internal_reloc
*)
675 bfd_malloc (max_reloc_count
676 * sizeof (struct internal_reloc
)));
677 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
678 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
679 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
680 || finfo
.outsyms
== NULL
681 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
682 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
683 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
684 || (! info
->relocateable
685 && finfo
.internal_relocs
== NULL
686 && max_reloc_count
> 0))
689 /* We now know the position of everything in the file, except that
690 we don't know the size of the symbol table and therefore we don't
691 know where the string table starts. We just build the string
692 table in memory as we go along. We process all the relocations
693 for a single input file at once. */
694 obj_raw_syment_count (abfd
) = 0;
696 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
698 if (! bfd_coff_start_final_link (abfd
, info
))
702 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
704 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
706 if (p
->type
== bfd_indirect_link_order
707 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
708 == bfd_target_coff_flavour
))
710 sub
= p
->u
.indirect
.section
->owner
;
711 if (! sub
->output_has_begun
)
713 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
715 sub
->output_has_begun
= true;
718 else if (p
->type
== bfd_section_reloc_link_order
719 || p
->type
== bfd_symbol_reloc_link_order
)
721 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
726 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
732 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
734 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
735 debug_merge_allocated
= false;
737 if (finfo
.internal_syms
!= NULL
)
739 free (finfo
.internal_syms
);
740 finfo
.internal_syms
= NULL
;
742 if (finfo
.sec_ptrs
!= NULL
)
744 free (finfo
.sec_ptrs
);
745 finfo
.sec_ptrs
= NULL
;
747 if (finfo
.sym_indices
!= NULL
)
749 free (finfo
.sym_indices
);
750 finfo
.sym_indices
= NULL
;
752 if (finfo
.linenos
!= NULL
)
754 free (finfo
.linenos
);
755 finfo
.linenos
= NULL
;
757 if (finfo
.contents
!= NULL
)
759 free (finfo
.contents
);
760 finfo
.contents
= NULL
;
762 if (finfo
.external_relocs
!= NULL
)
764 free (finfo
.external_relocs
);
765 finfo
.external_relocs
= NULL
;
767 if (finfo
.internal_relocs
!= NULL
)
769 free (finfo
.internal_relocs
);
770 finfo
.internal_relocs
= NULL
;
773 /* The value of the last C_FILE symbol is supposed to be the symbol
774 index of the first external symbol. Write it out again if
776 if (finfo
.last_file_index
!= -1
777 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
779 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
780 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
781 (PTR
) finfo
.outsyms
);
783 (obj_sym_filepos (abfd
)
784 + finfo
.last_file_index
* symesz
),
786 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
790 /* Write out the global symbols. */
791 finfo
.failed
= false;
792 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
797 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
798 if (finfo
.outsyms
!= NULL
)
800 free (finfo
.outsyms
);
801 finfo
.outsyms
= NULL
;
804 if (info
->relocateable
)
806 /* Now that we have written out all the global symbols, we know
807 the symbol indices to use for relocs against them, and we can
808 finally write out the relocs. */
809 external_relocs
= ((bfd_byte
*)
810 bfd_malloc (max_output_reloc_count
* relsz
));
811 if (external_relocs
== NULL
)
814 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
816 struct internal_reloc
*irel
;
817 struct internal_reloc
*irelend
;
818 struct coff_link_hash_entry
**rel_hash
;
821 if (o
->reloc_count
== 0)
824 irel
= finfo
.section_info
[o
->target_index
].relocs
;
825 irelend
= irel
+ o
->reloc_count
;
826 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
827 erel
= external_relocs
;
828 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
830 if (*rel_hash
!= NULL
)
832 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
833 irel
->r_symndx
= (*rel_hash
)->indx
;
835 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
838 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
839 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
840 abfd
) != relsz
* o
->reloc_count
)
844 free (external_relocs
);
845 external_relocs
= NULL
;
848 /* Free up the section information. */
849 if (finfo
.section_info
!= NULL
)
853 for (i
= 0; i
< abfd
->section_count
; i
++)
855 if (finfo
.section_info
[i
].relocs
!= NULL
)
856 free (finfo
.section_info
[i
].relocs
);
857 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
858 free (finfo
.section_info
[i
].rel_hashes
);
860 free (finfo
.section_info
);
861 finfo
.section_info
= NULL
;
864 /* If we have optimized stabs strings, output them. */
865 if (coff_hash_table (info
)->stab_info
!= NULL
)
867 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
871 /* Write out the string table. */
872 if (obj_raw_syment_count (abfd
) != 0)
875 (obj_sym_filepos (abfd
)
876 + obj_raw_syment_count (abfd
) * symesz
),
880 #if STRING_SIZE_SIZE == 4
882 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
883 (bfd_byte
*) strbuf
);
885 #error Change bfd_h_put_32
888 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
891 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
895 _bfd_stringtab_free (finfo
.strtab
);
897 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
898 not try to write out the symbols. */
899 bfd_get_symcount (abfd
) = 0;
904 if (debug_merge_allocated
)
905 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
906 if (finfo
.strtab
!= NULL
)
907 _bfd_stringtab_free (finfo
.strtab
);
908 if (finfo
.section_info
!= NULL
)
912 for (i
= 0; i
< abfd
->section_count
; i
++)
914 if (finfo
.section_info
[i
].relocs
!= NULL
)
915 free (finfo
.section_info
[i
].relocs
);
916 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
917 free (finfo
.section_info
[i
].rel_hashes
);
919 free (finfo
.section_info
);
921 if (finfo
.internal_syms
!= NULL
)
922 free (finfo
.internal_syms
);
923 if (finfo
.sec_ptrs
!= NULL
)
924 free (finfo
.sec_ptrs
);
925 if (finfo
.sym_indices
!= NULL
)
926 free (finfo
.sym_indices
);
927 if (finfo
.outsyms
!= NULL
)
928 free (finfo
.outsyms
);
929 if (finfo
.linenos
!= NULL
)
930 free (finfo
.linenos
);
931 if (finfo
.contents
!= NULL
)
932 free (finfo
.contents
);
933 if (finfo
.external_relocs
!= NULL
)
934 free (finfo
.external_relocs
);
935 if (finfo
.internal_relocs
!= NULL
)
936 free (finfo
.internal_relocs
);
937 if (external_relocs
!= NULL
)
938 free (external_relocs
);
942 /* parse out a -heap <reserved>,<commit> line */
945 dores_com (ptr
, output_bfd
, heap
)
950 if (coff_data(output_bfd
)->pe
)
952 int val
= strtoul (ptr
, &ptr
, 0);
954 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
956 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
960 int val
= strtoul (ptr
+1, &ptr
, 0);
962 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
964 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
970 static char *get_name(ptr
, dst
)
977 while (*ptr
&& *ptr
!= ' ')
983 /* Process any magic embedded commands in a section called .drectve */
986 process_embedded_commands (output_bfd
, info
, abfd
)
988 struct bfd_link_info
*info
;
991 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
998 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1001 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1006 e
= copy
+ sec
->_raw_size
;
1007 for (s
= copy
; s
< e
; )
1013 if (strncmp (s
,"-attr", 5) == 0)
1025 s
= get_name(s
, &name
);
1026 s
= get_name(s
, &attribs
);
1046 asec
= bfd_get_section_by_name (abfd
, name
);
1049 asec
->flags
|= SEC_CODE
;
1051 asec
->flags
|= SEC_READONLY
;
1054 else if (strncmp (s
,"-heap", 5) == 0)
1056 s
= dores_com (s
+5, output_bfd
, 1);
1058 else if (strncmp (s
,"-stack", 6) == 0)
1060 s
= dores_com (s
+6, output_bfd
, 0);
1069 /* Link an input file into the linker output file. This function
1070 handles all the sections and relocations of the input file at once. */
1073 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1074 struct coff_final_link_info
*finfo
;
1077 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1078 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1079 asection
*, struct internal_reloc
*,
1082 const char *strings
;
1083 bfd_size_type syment_base
;
1084 unsigned int n_tmask
;
1085 unsigned int n_btshft
;
1087 bfd_size_type isymesz
;
1088 bfd_size_type osymesz
;
1089 bfd_size_type linesz
;
1092 struct internal_syment
*isymp
;
1095 unsigned long output_index
;
1097 struct coff_link_hash_entry
**sym_hash
;
1100 /* Move all the symbols to the output file. */
1102 output_bfd
= finfo
->output_bfd
;
1103 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1105 syment_base
= obj_raw_syment_count (output_bfd
);
1106 isymesz
= bfd_coff_symesz (input_bfd
);
1107 osymesz
= bfd_coff_symesz (output_bfd
);
1108 linesz
= bfd_coff_linesz (input_bfd
);
1109 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1111 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1112 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1114 /* Define macros so that ISFCN, et. al., macros work correctly. */
1115 #define N_TMASK n_tmask
1116 #define N_BTSHFT n_btshft
1119 if (! finfo
->info
->keep_memory
)
1122 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1125 if (! _bfd_coff_get_external_symbols (input_bfd
))
1128 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1129 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1130 isymp
= finfo
->internal_syms
;
1131 secpp
= finfo
->sec_ptrs
;
1132 indexp
= finfo
->sym_indices
;
1133 output_index
= syment_base
;
1134 outsym
= finfo
->outsyms
;
1136 if (coff_data (output_bfd
)->pe
)
1138 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1142 while (esym
< esym_end
)
1144 struct internal_syment isym
;
1149 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1151 /* Make a copy of *isymp so that the relocate_section function
1152 always sees the original values. This is more reliable than
1153 always recomputing the symbol value even if we are stripping
1157 if (isym
.n_scnum
!= 0)
1158 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1161 if (isym
.n_value
== 0)
1162 *secpp
= bfd_und_section_ptr
;
1164 *secpp
= bfd_com_section_ptr
;
1171 add
= 1 + isym
.n_numaux
;
1173 /* If we are stripping all symbols, we want to skip this one. */
1174 if (finfo
->info
->strip
== strip_all
)
1179 if (isym
.n_sclass
== C_EXT
1180 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1182 /* This is a global symbol. Global symbols come at the
1183 end of the symbol table, so skip them for now.
1184 Function symbols, however, are an exception, and are
1185 not moved to the end. */
1187 if (! ISFCN (isym
.n_type
))
1192 /* This is a local symbol. Skip it if we are discarding
1194 if (finfo
->info
->discard
== discard_all
)
1199 /* If we stripping debugging symbols, and this is a debugging
1200 symbol, then skip it. */
1202 && finfo
->info
->strip
== strip_debugger
1203 && isym
.n_scnum
== N_DEBUG
)
1206 /* If some symbols are stripped based on the name, work out the
1207 name and decide whether to skip this symbol. */
1209 && (finfo
->info
->strip
== strip_some
1210 || finfo
->info
->discard
== discard_l
))
1213 char buf
[SYMNMLEN
+ 1];
1215 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1219 if ((finfo
->info
->strip
== strip_some
1220 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1223 && finfo
->info
->discard
== discard_l
1224 && strncmp (name
, finfo
->info
->lprefix
,
1225 finfo
->info
->lprefix_len
) == 0))
1229 /* If this is an enum, struct, or union tag, see if we have
1230 already output an identical type. */
1232 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1233 && (isym
.n_sclass
== C_ENTAG
1234 || isym
.n_sclass
== C_STRTAG
1235 || isym
.n_sclass
== C_UNTAG
)
1236 && isym
.n_numaux
== 1)
1239 char buf
[SYMNMLEN
+ 1];
1240 struct coff_debug_merge_hash_entry
*mh
;
1241 struct coff_debug_merge_type
*mt
;
1242 union internal_auxent aux
;
1243 struct coff_debug_merge_element
**epp
;
1244 bfd_byte
*esl
, *eslend
;
1245 struct internal_syment
*islp
;
1247 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1251 /* Ignore fake names invented by compiler; treat them all as
1253 if (*name
== '~' || *name
== '.' || *name
== '$'
1254 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1255 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1258 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1263 /* Allocate memory to hold type information. If this turns
1264 out to be a duplicate, we pass this address to
1266 mt
= ((struct coff_debug_merge_type
*)
1267 bfd_alloc (input_bfd
,
1268 sizeof (struct coff_debug_merge_type
)));
1271 mt
->class = isym
.n_sclass
;
1273 /* Pick up the aux entry, which points to the end of the tag
1275 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1276 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1279 /* Gather the elements. */
1280 epp
= &mt
->elements
;
1281 mt
->elements
= NULL
;
1283 esl
= esym
+ 2 * isymesz
;
1284 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1285 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1286 while (esl
< eslend
)
1288 const char *elename
;
1289 char elebuf
[SYMNMLEN
+ 1];
1292 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1294 *epp
= ((struct coff_debug_merge_element
*)
1295 bfd_alloc (input_bfd
,
1296 sizeof (struct coff_debug_merge_element
)));
1300 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1302 if (elename
== NULL
)
1305 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1308 strcpy (copy
, elename
);
1310 (*epp
)->name
= copy
;
1311 (*epp
)->type
= islp
->n_type
;
1313 if (islp
->n_numaux
>= 1
1314 && islp
->n_type
!= T_NULL
1315 && islp
->n_sclass
!= C_EOS
)
1317 union internal_auxent eleaux
;
1320 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1321 islp
->n_type
, islp
->n_sclass
, 0,
1322 islp
->n_numaux
, (PTR
) &eleaux
);
1323 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1325 /* FIXME: If this tagndx entry refers to a symbol
1326 defined later in this file, we just ignore it.
1327 Handling this correctly would be tedious, and may
1333 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1336 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1337 if ((*epp
)->tagndx
< 0)
1341 epp
= &(*epp
)->next
;
1344 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1345 islp
+= islp
->n_numaux
+ 1;
1348 /* See if we already have a definition which matches this
1349 type. We always output the type if it has no elements,
1351 if (mt
->elements
== NULL
)
1352 bfd_release (input_bfd
, (PTR
) mt
);
1355 struct coff_debug_merge_type
*mtl
;
1357 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1359 struct coff_debug_merge_element
*me
, *mel
;
1361 if (mtl
->class != mt
->class)
1364 for (me
= mt
->elements
, mel
= mtl
->elements
;
1365 me
!= NULL
&& mel
!= NULL
;
1366 me
= me
->next
, mel
= mel
->next
)
1368 if (strcmp (me
->name
, mel
->name
) != 0
1369 || me
->type
!= mel
->type
1370 || me
->tagndx
!= mel
->tagndx
)
1374 if (me
== NULL
&& mel
== NULL
)
1378 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1380 /* This is the first definition of this type. */
1381 mt
->indx
= output_index
;
1382 mt
->next
= mh
->types
;
1387 /* This is a redefinition which can be merged. */
1388 bfd_release (input_bfd
, (PTR
) mt
);
1389 *indexp
= mtl
->indx
;
1390 add
= (eslend
- esym
) / isymesz
;
1396 /* We now know whether we are to skip this symbol or not. */
1399 /* Adjust the symbol in order to output it. */
1401 if (isym
._n
._n_n
._n_zeroes
== 0
1402 && isym
._n
._n_n
._n_offset
!= 0)
1407 /* This symbol has a long name. Enter it in the string
1408 table we are building. Note that we do not check
1409 bfd_coff_symname_in_debug. That is only true for
1410 XCOFF, and XCOFF requires different linking code
1412 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1416 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1417 if (indx
== (bfd_size_type
) -1)
1419 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1422 if (isym
.n_scnum
> 0)
1424 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1425 isym
.n_value
+= ((*secpp
)->output_section
->vma
1426 + (*secpp
)->output_offset
1430 /* The value of a C_FILE symbol is the symbol index of the
1431 next C_FILE symbol. The value of the last C_FILE symbol
1432 is the symbol index to the first external symbol
1433 (actually, coff_renumber_symbols does not get this
1434 right--it just sets the value of the last C_FILE symbol
1435 to zero--and nobody has ever complained about it). We
1436 try to get this right, below, just before we write the
1437 symbols out, but in the general case we may have to write
1438 the symbol out twice. */
1439 if (isym
.n_sclass
== C_FILE
)
1441 if (finfo
->last_file_index
!= -1
1442 && finfo
->last_file
.n_value
!= (long) output_index
)
1444 /* We must correct the value of the last C_FILE entry. */
1445 finfo
->last_file
.n_value
= output_index
;
1446 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1448 /* The last C_FILE symbol is in this input file. */
1449 bfd_coff_swap_sym_out (output_bfd
,
1450 (PTR
) &finfo
->last_file
,
1451 (PTR
) (finfo
->outsyms
1452 + ((finfo
->last_file_index
1458 /* We have already written out the last C_FILE
1459 symbol. We need to write it out again. We
1460 borrow *outsym temporarily. */
1461 bfd_coff_swap_sym_out (output_bfd
,
1462 (PTR
) &finfo
->last_file
,
1464 if (bfd_seek (output_bfd
,
1465 (obj_sym_filepos (output_bfd
)
1466 + finfo
->last_file_index
* osymesz
),
1468 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1474 finfo
->last_file_index
= output_index
;
1475 finfo
->last_file
= isym
;
1478 /* Output the symbol. */
1480 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1482 *indexp
= output_index
;
1487 struct coff_link_hash_entry
*h
;
1489 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1491 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1492 BFD_ASSERT (h
!= NULL
);
1493 h
->indx
= output_index
;
1496 output_index
+= add
;
1497 outsym
+= add
* osymesz
;
1500 esym
+= add
* isymesz
;
1504 for (--add
; add
> 0; --add
)
1511 /* Fix up the aux entries. This must be done in a separate pass,
1512 because we don't know the correct symbol indices until we have
1513 already decided which symbols we are going to keep. */
1515 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1516 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1517 isymp
= finfo
->internal_syms
;
1518 indexp
= finfo
->sym_indices
;
1519 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1520 outsym
= finfo
->outsyms
;
1521 while (esym
< esym_end
)
1525 add
= 1 + isymp
->n_numaux
;
1528 || (bfd_size_type
) *indexp
< syment_base
)
1529 && (*sym_hash
== NULL
1530 || (*sym_hash
)->auxbfd
!= input_bfd
))
1531 esym
+= add
* isymesz
;
1534 struct coff_link_hash_entry
*h
;
1541 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1549 /* Handle the aux entries. This handling is based on
1550 coff_pointerize_aux. I don't know if it always correct. */
1551 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1553 union internal_auxent aux
;
1554 union internal_auxent
*auxp
;
1560 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1561 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1566 if (isymp
->n_sclass
== C_FILE
)
1568 /* If this is a long filename, we must put it in the
1570 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1571 && auxp
->x_file
.x_n
.x_offset
!= 0)
1573 const char *filename
;
1576 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1577 >= STRING_SIZE_SIZE
);
1578 if (strings
== NULL
)
1580 strings
= _bfd_coff_read_string_table (input_bfd
);
1581 if (strings
== NULL
)
1584 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1585 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1587 if (indx
== (bfd_size_type
) -1)
1589 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1592 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1596 if (ISFCN (isymp
->n_type
)
1597 || ISTAG (isymp
->n_sclass
)
1598 || isymp
->n_sclass
== C_BLOCK
1599 || isymp
->n_sclass
== C_FCN
)
1601 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1603 && indx
< obj_raw_syment_count (input_bfd
))
1605 /* We look forward through the symbol for
1606 the index of the next symbol we are going
1607 to include. I don't know if this is
1609 while ((finfo
->sym_indices
[indx
] < 0
1610 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1612 && indx
< obj_raw_syment_count (input_bfd
))
1614 if (indx
>= obj_raw_syment_count (input_bfd
))
1615 indx
= output_index
;
1617 indx
= finfo
->sym_indices
[indx
];
1618 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1622 indx
= auxp
->x_sym
.x_tagndx
.l
;
1623 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1627 symindx
= finfo
->sym_indices
[indx
];
1629 auxp
->x_sym
.x_tagndx
.l
= 0;
1631 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1634 /* The .bf symbols are supposed to be linked through
1635 the endndx field. We need to carry this list
1636 across object files. */
1639 && isymp
->n_sclass
== C_FCN
1640 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1641 || isymp
->_n
._n_n
._n_offset
== 0)
1642 && isymp
->_n
._n_name
[0] == '.'
1643 && isymp
->_n
._n_name
[1] == 'b'
1644 && isymp
->_n
._n_name
[2] == 'f'
1645 && isymp
->_n
._n_name
[3] == '\0')
1647 if (finfo
->last_bf_index
!= -1)
1649 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1652 if ((bfd_size_type
) finfo
->last_bf_index
1657 /* The last .bf symbol is in this input
1658 file. This will only happen if the
1659 assembler did not set up the .bf
1660 endndx symbols correctly. */
1661 auxout
= (PTR
) (finfo
->outsyms
1662 + ((finfo
->last_bf_index
1665 bfd_coff_swap_aux_out (output_bfd
,
1666 (PTR
) &finfo
->last_bf
,
1674 /* We have already written out the last
1675 .bf aux entry. We need to write it
1676 out again. We borrow *outsym
1677 temporarily. FIXME: This case should
1679 bfd_coff_swap_aux_out (output_bfd
,
1680 (PTR
) &finfo
->last_bf
,
1685 if (bfd_seek (output_bfd
,
1686 (obj_sym_filepos (output_bfd
)
1687 + finfo
->last_bf_index
* osymesz
),
1689 || bfd_write (outsym
, osymesz
, 1,
1690 output_bfd
) != osymesz
)
1695 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1696 finfo
->last_bf_index
= -1;
1699 /* The endndx field of this aux entry must
1700 be updated with the symbol number of the
1702 finfo
->last_bf
= *auxp
;
1703 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1712 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1713 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1727 /* Relocate the line numbers, unless we are stripping them. */
1728 if (finfo
->info
->strip
== strip_none
1729 || finfo
->info
->strip
== strip_some
)
1731 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1737 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1738 build_link_order in ldwrite.c will not have created a
1739 link order, which means that we will not have seen this
1740 input section in _bfd_coff_final_link, which means that
1741 we will not have allocated space for the line numbers of
1742 this section. I don't think line numbers can be
1743 meaningful for a section which does not have
1744 SEC_HAS_CONTENTS set, but, if they do, this must be
1746 if (o
->lineno_count
== 0
1747 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1750 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1751 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1752 input_bfd
) != linesz
* o
->lineno_count
)
1755 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1756 eline
= finfo
->linenos
;
1757 elineend
= eline
+ linesz
* o
->lineno_count
;
1758 for (; eline
< elineend
; eline
+= linesz
)
1760 struct internal_lineno iline
;
1762 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1764 if (iline
.l_lnno
!= 0)
1765 iline
.l_addr
.l_paddr
+= offset
;
1766 else if (iline
.l_addr
.l_symndx
>= 0
1767 && ((unsigned long) iline
.l_addr
.l_symndx
1768 < obj_raw_syment_count (input_bfd
)))
1772 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1776 /* These line numbers are attached to a symbol
1777 which we are stripping. We should really
1778 just discard the line numbers, but that would
1779 be a pain because we have already counted
1785 struct internal_syment is
;
1786 union internal_auxent ia
;
1788 /* Fix up the lnnoptr field in the aux entry of
1789 the symbol. It turns out that we can't do
1790 this when we modify the symbol aux entries,
1791 because gas sometimes screws up the lnnoptr
1792 field and makes it an offset from the start
1793 of the line numbers rather than an absolute
1795 bfd_coff_swap_sym_in (output_bfd
,
1796 (PTR
) (finfo
->outsyms
1797 + ((indx
- syment_base
)
1800 if ((ISFCN (is
.n_type
)
1801 || is
.n_sclass
== C_BLOCK
)
1802 && is
.n_numaux
>= 1)
1806 auxptr
= (PTR
) (finfo
->outsyms
1807 + ((indx
- syment_base
+ 1)
1809 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1810 is
.n_type
, is
.n_sclass
,
1811 0, is
.n_numaux
, (PTR
) &ia
);
1812 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1813 (o
->output_section
->line_filepos
1814 + o
->output_section
->lineno_count
* linesz
1815 + eline
- finfo
->linenos
);
1816 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1817 is
.n_type
, is
.n_sclass
, 0,
1818 is
.n_numaux
, auxptr
);
1822 iline
.l_addr
.l_symndx
= indx
;
1825 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1828 if (bfd_seek (output_bfd
,
1829 (o
->output_section
->line_filepos
1830 + o
->output_section
->lineno_count
* linesz
),
1832 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1833 output_bfd
) != linesz
* o
->lineno_count
)
1836 o
->output_section
->lineno_count
+= o
->lineno_count
;
1840 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1841 symbol will be the first symbol in the next input file. In the
1842 normal case, this will save us from writing out the C_FILE symbol
1844 if (finfo
->last_file_index
!= -1
1845 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1847 finfo
->last_file
.n_value
= output_index
;
1848 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1849 (PTR
) (finfo
->outsyms
1850 + ((finfo
->last_file_index
- syment_base
)
1854 /* Write the modified symbols to the output file. */
1855 if (outsym
> finfo
->outsyms
)
1857 if (bfd_seek (output_bfd
,
1858 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1860 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1862 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1865 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1866 + (outsym
- finfo
->outsyms
) / osymesz
)
1869 obj_raw_syment_count (output_bfd
) = output_index
;
1872 /* Relocate the contents of each section. */
1873 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1874 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1877 struct coff_section_tdata
*secdata
;
1879 if (! o
->linker_mark
)
1881 /* This section was omitted from the link. */
1885 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
1886 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
1888 if ((o
->flags
& SEC_RELOC
) != 0
1889 && o
->reloc_count
!= 0)
1891 ((*_bfd_error_handler
)
1892 ("%s: relocs in section `%s', but it has no contents",
1893 bfd_get_filename (input_bfd
),
1894 bfd_get_section_name (input_bfd
, o
)));
1895 bfd_set_error (bfd_error_no_contents
);
1902 secdata
= coff_section_data (input_bfd
, o
);
1903 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
1904 contents
= secdata
->contents
;
1907 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1908 (file_ptr
) 0, o
->_raw_size
))
1910 contents
= finfo
->contents
;
1913 if ((o
->flags
& SEC_RELOC
) != 0)
1916 struct internal_reloc
*internal_relocs
;
1917 struct internal_reloc
*irel
;
1919 /* Read in the relocs. */
1920 target_index
= o
->output_section
->target_index
;
1921 internal_relocs
= (_bfd_coff_read_internal_relocs
1922 (input_bfd
, o
, false, finfo
->external_relocs
,
1923 finfo
->info
->relocateable
,
1924 (finfo
->info
->relocateable
1925 ? (finfo
->section_info
[target_index
].relocs
1926 + o
->output_section
->reloc_count
)
1927 : finfo
->internal_relocs
)));
1928 if (internal_relocs
== NULL
)
1931 /* Call processor specific code to relocate the section
1933 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1937 finfo
->internal_syms
,
1941 if (finfo
->info
->relocateable
)
1944 struct internal_reloc
*irelend
;
1945 struct coff_link_hash_entry
**rel_hash
;
1947 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1948 irel
= internal_relocs
;
1949 irelend
= irel
+ o
->reloc_count
;
1950 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1951 + o
->output_section
->reloc_count
);
1952 for (; irel
< irelend
; irel
++, rel_hash
++)
1954 struct coff_link_hash_entry
*h
;
1959 /* Adjust the reloc address and symbol index. */
1961 irel
->r_vaddr
+= offset
;
1963 if (irel
->r_symndx
== -1)
1968 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1976 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1979 /* This is a global symbol. */
1981 irel
->r_symndx
= h
->indx
;
1984 /* This symbol is being written at the end
1985 of the file, and we do not yet know the
1986 symbol index. We save the pointer to the
1987 hash table entry in the rel_hash list.
1988 We set the indx field to -2 to indicate
1989 that this symbol must not be stripped. */
1998 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2000 irel
->r_symndx
= indx
;
2003 struct internal_syment
*is
;
2005 char buf
[SYMNMLEN
+ 1];
2007 /* This reloc is against a symbol we are
2008 stripping. It would be possible to
2009 handle this case, but I don't think it's
2011 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2013 name
= (_bfd_coff_internal_syment_name
2014 (input_bfd
, is
, buf
));
2018 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2019 (finfo
->info
, name
, input_bfd
, o
,
2026 o
->output_section
->reloc_count
+= o
->reloc_count
;
2030 /* Write out the modified section contents. */
2031 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2033 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2034 contents
, o
->output_offset
,
2035 (o
->_cooked_size
!= 0
2042 if (! _bfd_write_section_stabs (output_bfd
, o
, &secdata
->stab_info
,
2048 if (! finfo
->info
->keep_memory
)
2050 if (! _bfd_coff_free_symbols (input_bfd
))
2057 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2060 _bfd_coff_write_global_sym (h
, data
)
2061 struct coff_link_hash_entry
*h
;
2064 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2066 struct internal_syment isym
;
2067 bfd_size_type symesz
;
2070 output_bfd
= finfo
->output_bfd
;
2076 && (finfo
->info
->strip
== strip_all
2077 || (finfo
->info
->strip
== strip_some
2078 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2079 h
->root
.root
.string
, false, false)
2083 switch (h
->root
.type
)
2086 case bfd_link_hash_new
:
2090 case bfd_link_hash_undefined
:
2091 case bfd_link_hash_undefweak
:
2092 isym
.n_scnum
= N_UNDEF
;
2096 case bfd_link_hash_defined
:
2097 case bfd_link_hash_defweak
:
2101 sec
= h
->root
.u
.def
.section
->output_section
;
2102 if (bfd_is_abs_section (sec
))
2103 isym
.n_scnum
= N_ABS
;
2105 isym
.n_scnum
= sec
->target_index
;
2106 isym
.n_value
= (h
->root
.u
.def
.value
2108 + h
->root
.u
.def
.section
->output_offset
);
2112 case bfd_link_hash_common
:
2113 isym
.n_scnum
= N_UNDEF
;
2114 isym
.n_value
= h
->root
.u
.c
.size
;
2117 case bfd_link_hash_indirect
:
2118 case bfd_link_hash_warning
:
2119 /* Just ignore these. They can't be handled anyhow. */
2123 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2124 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2131 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2133 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2135 if (indx
== (bfd_size_type
) -1)
2137 finfo
->failed
= true;
2140 isym
._n
._n_n
._n_zeroes
= 0;
2141 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2144 isym
.n_sclass
= h
->class;
2145 isym
.n_type
= h
->type
;
2147 if (isym
.n_sclass
== C_NULL
)
2148 isym
.n_sclass
= C_EXT
;
2150 isym
.n_numaux
= h
->numaux
;
2152 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2154 symesz
= bfd_coff_symesz (output_bfd
);
2156 if (bfd_seek (output_bfd
,
2157 (obj_sym_filepos (output_bfd
)
2158 + obj_raw_syment_count (output_bfd
) * symesz
),
2160 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2162 finfo
->failed
= true;
2166 h
->indx
= obj_raw_syment_count (output_bfd
);
2168 ++obj_raw_syment_count (output_bfd
);
2170 /* Write out any associated aux entries. There normally will be
2171 none. If there are any, I have no idea how to modify them. */
2172 for (i
= 0; i
< isym
.n_numaux
; i
++)
2174 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2175 isym
.n_sclass
, i
, isym
.n_numaux
,
2176 (PTR
) finfo
->outsyms
);
2177 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2179 finfo
->failed
= true;
2182 ++obj_raw_syment_count (output_bfd
);
2188 /* Handle a link order which is supposed to generate a reloc. */
2191 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2193 struct coff_final_link_info
*finfo
;
2194 asection
*output_section
;
2195 struct bfd_link_order
*link_order
;
2197 reloc_howto_type
*howto
;
2198 struct internal_reloc
*irel
;
2199 struct coff_link_hash_entry
**rel_hash_ptr
;
2201 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2204 bfd_set_error (bfd_error_bad_value
);
2208 if (link_order
->u
.reloc
.p
->addend
!= 0)
2212 bfd_reloc_status_type rstat
;
2215 size
= bfd_get_reloc_size (howto
);
2216 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2220 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2221 link_order
->u
.reloc
.p
->addend
, buf
);
2227 case bfd_reloc_outofrange
:
2229 case bfd_reloc_overflow
:
2230 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2232 (link_order
->type
== bfd_section_reloc_link_order
2233 ? bfd_section_name (output_bfd
,
2234 link_order
->u
.reloc
.p
->u
.section
)
2235 : link_order
->u
.reloc
.p
->u
.name
),
2236 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2237 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2244 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2245 (file_ptr
) link_order
->offset
, size
);
2251 /* Store the reloc information in the right place. It will get
2252 swapped and written out at the end of the final_link routine. */
2254 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2255 + output_section
->reloc_count
);
2256 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2257 + output_section
->reloc_count
);
2259 memset (irel
, 0, sizeof (struct internal_reloc
));
2260 *rel_hash_ptr
= NULL
;
2262 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2264 if (link_order
->type
== bfd_section_reloc_link_order
)
2266 /* We need to somehow locate a symbol in the right section. The
2267 symbol must either have a value of zero, or we must adjust
2268 the addend by the value of the symbol. FIXME: Write this
2269 when we need it. The old linker couldn't handle this anyhow. */
2271 *rel_hash_ptr
= NULL
;
2276 struct coff_link_hash_entry
*h
;
2278 h
= ((struct coff_link_hash_entry
*)
2279 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2280 link_order
->u
.reloc
.p
->u
.name
,
2281 false, false, true));
2285 irel
->r_symndx
= h
->indx
;
2288 /* Set the index to -2 to force this symbol to get
2297 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2298 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2299 (asection
*) NULL
, (bfd_vma
) 0)))
2305 /* FIXME: Is this always right? */
2306 irel
->r_type
= howto
->type
;
2308 /* r_size is only used on the RS/6000, which needs its own linker
2309 routines anyhow. r_extern is only used for ECOFF. */
2311 /* FIXME: What is the right value for r_offset? Is zero OK? */
2313 ++output_section
->reloc_count
;
2318 /* A basic reloc handling routine which may be used by processors with
2322 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2323 input_section
, contents
, relocs
, syms
,
2326 struct bfd_link_info
*info
;
2328 asection
*input_section
;
2330 struct internal_reloc
*relocs
;
2331 struct internal_syment
*syms
;
2332 asection
**sections
;
2334 struct internal_reloc
*rel
;
2335 struct internal_reloc
*relend
;
2338 relend
= rel
+ input_section
->reloc_count
;
2339 for (; rel
< relend
; rel
++)
2342 struct coff_link_hash_entry
*h
;
2343 struct internal_syment
*sym
;
2346 reloc_howto_type
*howto
;
2347 bfd_reloc_status_type rstat
;
2349 symndx
= rel
->r_symndx
;
2358 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2359 sym
= syms
+ symndx
;
2362 /* COFF treats common symbols in one of two ways. Either the
2363 size of the symbol is included in the section contents, or it
2364 is not. We assume that the size is not included, and force
2365 the rtype_to_howto function to adjust the addend as needed. */
2367 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2368 addend
= - sym
->n_value
;
2373 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2386 sec
= bfd_abs_section_ptr
;
2391 sec
= sections
[symndx
];
2392 val
= (sec
->output_section
->vma
2393 + sec
->output_offset
2400 if (h
->root
.type
== bfd_link_hash_defined
2401 || h
->root
.type
== bfd_link_hash_defweak
)
2405 sec
= h
->root
.u
.def
.section
;
2406 val
= (h
->root
.u
.def
.value
2407 + sec
->output_section
->vma
2408 + sec
->output_offset
);
2411 else if (! info
->relocateable
)
2413 if (! ((*info
->callbacks
->undefined_symbol
)
2414 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2415 rel
->r_vaddr
- input_section
->vma
)))
2420 if (info
->base_file
)
2422 /* Emit a reloc if the backend thinks it needs it. */
2423 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2425 /* relocation to a symbol in a section which
2426 isn't absolute - we output the address here
2428 bfd_vma addr
= rel
->r_vaddr
2429 - input_section
->vma
2430 + input_section
->output_offset
2431 + input_section
->output_section
->vma
;
2432 if (coff_data(output_bfd
)->pe
)
2433 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2434 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2438 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2440 rel
->r_vaddr
- input_section
->vma
,
2449 case bfd_reloc_overflow
:
2452 char buf
[SYMNMLEN
+ 1];
2457 name
= h
->root
.root
.string
;
2460 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2465 if (! ((*info
->callbacks
->reloc_overflow
)
2466 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2467 input_section
, rel
->r_vaddr
- input_section
->vma
)))