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 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
87 /* Create a COFF linker hash table. */
89 struct bfd_link_hash_table
*
90 _bfd_coff_link_hash_table_create (abfd
)
93 struct coff_link_hash_table
*ret
;
95 ret
= ((struct coff_link_hash_table
*)
96 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
99 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
100 _bfd_coff_link_hash_newfunc
))
102 bfd_release (abfd
, ret
);
103 return (struct bfd_link_hash_table
*) NULL
;
108 /* Create an entry in a COFF debug merge hash table. */
110 struct bfd_hash_entry
*
111 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
112 struct bfd_hash_entry
*entry
;
113 struct bfd_hash_table
*table
;
116 struct coff_debug_merge_hash_entry
*ret
=
117 (struct coff_debug_merge_hash_entry
*) entry
;
119 /* Allocate the structure if it has not already been allocated by a
121 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
122 ret
= ((struct coff_debug_merge_hash_entry
*)
123 bfd_hash_allocate (table
,
124 sizeof (struct coff_debug_merge_hash_entry
)));
125 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
126 return (struct bfd_hash_entry
*) ret
;
128 /* Call the allocation method of the superclass. */
129 ret
= ((struct coff_debug_merge_hash_entry
*)
130 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
131 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
133 /* Set local fields. */
137 return (struct bfd_hash_entry
*) ret
;
140 /* Given a COFF BFD, add symbols to the global hash table as
144 _bfd_coff_link_add_symbols (abfd
, info
)
146 struct bfd_link_info
*info
;
148 switch (bfd_get_format (abfd
))
151 return coff_link_add_object_symbols (abfd
, info
);
153 return (_bfd_generic_link_add_archive_symbols
154 (abfd
, info
, coff_link_check_archive_element
));
156 bfd_set_error (bfd_error_wrong_format
);
161 /* Add symbols from a COFF object file. */
164 coff_link_add_object_symbols (abfd
, info
)
166 struct bfd_link_info
*info
;
168 if (! _bfd_coff_get_external_symbols (abfd
))
170 if (! coff_link_add_symbols (abfd
, info
))
173 if (! info
->keep_memory
)
175 if (! _bfd_coff_free_symbols (abfd
))
181 /* Check a single archive element to see if we need to include it in
182 the link. *PNEEDED is set according to whether this element is
183 needed in the link or not. This is called via
184 _bfd_generic_link_add_archive_symbols. */
187 coff_link_check_archive_element (abfd
, info
, pneeded
)
189 struct bfd_link_info
*info
;
192 if (! _bfd_coff_get_external_symbols (abfd
))
195 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
200 if (! coff_link_add_symbols (abfd
, info
))
204 if (! info
->keep_memory
|| ! *pneeded
)
206 if (! _bfd_coff_free_symbols (abfd
))
213 /* Look through the symbols to see if this object file should be
214 included in the link. */
217 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
219 struct bfd_link_info
*info
;
222 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
223 bfd_size_type symesz
;
229 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
231 symesz
= bfd_coff_symesz (abfd
);
232 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
233 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
234 while (esym
< esym_end
)
236 struct internal_syment sym
;
238 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
240 if ((sym
.n_sclass
== C_EXT
241 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
242 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
245 char buf
[SYMNMLEN
+ 1];
246 struct bfd_link_hash_entry
*h
;
248 /* This symbol is externally visible, and is defined by this
251 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
254 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
256 /* We are only interested in symbols that are currently
257 undefined. If a symbol is currently known to be common,
258 COFF linkers do not bring in an object file which defines
260 if (h
!= (struct bfd_link_hash_entry
*) NULL
261 && h
->type
== bfd_link_hash_undefined
)
263 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
270 esym
+= (sym
.n_numaux
+ 1) * symesz
;
273 /* We do not need this object file. */
277 /* Add all the symbols from an object file to the hash table. */
280 coff_link_add_symbols (abfd
, info
)
282 struct bfd_link_info
*info
;
284 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
285 boolean default_copy
;
286 bfd_size_type symcount
;
287 struct coff_link_hash_entry
**sym_hash
;
288 bfd_size_type symesz
;
292 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
294 if (info
->keep_memory
)
295 default_copy
= false;
299 symcount
= obj_raw_syment_count (abfd
);
301 /* We keep a list of the linker hash table entries that correspond
302 to particular symbols. */
303 sym_hash
= ((struct coff_link_hash_entry
**)
306 * sizeof (struct coff_link_hash_entry
*))));
307 if (sym_hash
== NULL
&& symcount
!= 0)
309 obj_coff_sym_hashes (abfd
) = sym_hash
;
311 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
313 symesz
= bfd_coff_symesz (abfd
);
314 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
315 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
316 esym_end
= esym
+ symcount
* symesz
;
317 while (esym
< esym_end
)
319 struct internal_syment sym
;
322 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
324 if (sym
.n_sclass
== C_EXT
325 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
328 char buf
[SYMNMLEN
+ 1];
333 /* This symbol is externally visible. */
335 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
339 /* We must copy the name into memory if we got it from the
340 syment itself, rather than the string table. */
342 if (sym
._n
._n_n
._n_zeroes
!= 0
343 || sym
._n
._n_n
._n_offset
== 0)
348 if (sym
.n_scnum
== 0)
353 section
= bfd_und_section_ptr
;
358 section
= bfd_com_section_ptr
;
363 flags
= BSF_EXPORT
| BSF_GLOBAL
;
364 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
365 value
-= section
->vma
;
368 if (! (bfd_coff_link_add_one_symbol
369 (info
, abfd
, name
, flags
, section
, value
,
370 (const char *) NULL
, copy
, false,
371 (struct bfd_link_hash_entry
**) sym_hash
)))
374 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
376 if (((*sym_hash
)->class == C_NULL
377 && (*sym_hash
)->type
== T_NULL
)
380 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
382 (*sym_hash
)->class = sym
.n_sclass
;
383 (*sym_hash
)->type
= sym
.n_type
;
384 (*sym_hash
)->numaux
= sym
.n_numaux
;
385 (*sym_hash
)->auxbfd
= abfd
;
386 if (sym
.n_numaux
!= 0)
388 union internal_auxent
*alloc
;
391 union internal_auxent
*iaux
;
393 alloc
= ((union internal_auxent
*)
394 bfd_hash_allocate (&info
->hash
->table
,
396 * sizeof (*alloc
))));
399 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
401 i
++, eaux
+= symesz
, iaux
++)
402 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
403 sym
.n_sclass
, i
, sym
.n_numaux
,
405 (*sym_hash
)->aux
= alloc
;
411 esym
+= (sym
.n_numaux
+ 1) * symesz
;
412 sym_hash
+= sym
.n_numaux
+ 1;
418 /* Do the final link step. */
421 _bfd_coff_final_link (abfd
, info
)
423 struct bfd_link_info
*info
;
425 bfd_size_type symesz
;
426 struct coff_final_link_info finfo
;
427 boolean debug_merge_allocated
;
429 struct bfd_link_order
*p
;
430 size_t max_sym_count
;
431 size_t max_lineno_count
;
432 size_t max_reloc_count
;
433 size_t max_output_reloc_count
;
434 size_t max_contents_size
;
435 file_ptr rel_filepos
;
437 file_ptr line_filepos
;
440 bfd_byte
*external_relocs
= NULL
;
441 char strbuf
[STRING_SIZE_SIZE
];
443 symesz
= bfd_coff_symesz (abfd
);
446 finfo
.output_bfd
= abfd
;
448 finfo
.section_info
= NULL
;
449 finfo
.last_file_index
= -1;
450 finfo
.last_bf_index
= -1;
451 finfo
.internal_syms
= NULL
;
452 finfo
.sec_ptrs
= NULL
;
453 finfo
.sym_indices
= NULL
;
454 finfo
.outsyms
= NULL
;
455 finfo
.linenos
= NULL
;
456 finfo
.contents
= NULL
;
457 finfo
.external_relocs
= NULL
;
458 finfo
.internal_relocs
= NULL
;
459 debug_merge_allocated
= false;
461 coff_data (abfd
)->link_info
= info
;
463 finfo
.strtab
= _bfd_stringtab_init ();
464 if (finfo
.strtab
== NULL
)
467 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
469 debug_merge_allocated
= true;
471 /* Compute the file positions for all the sections. */
472 if (! abfd
->output_has_begun
)
473 bfd_coff_compute_section_file_positions (abfd
);
475 /* Count the line numbers and relocation entries required for the
476 output file. Set the file positions for the relocs. */
477 rel_filepos
= obj_relocbase (abfd
);
478 relsz
= bfd_coff_relsz (abfd
);
479 max_contents_size
= 0;
480 max_lineno_count
= 0;
483 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
487 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
489 if (p
->type
== bfd_indirect_link_order
)
493 sec
= p
->u
.indirect
.section
;
495 /* Mark all sections which are to be included in the
496 link. This will normally be every section. We need
497 to do this so that we can identify any sections which
498 the linker has decided to not include. */
499 sec
->linker_mark
= true;
501 if (info
->strip
== strip_none
502 || info
->strip
== strip_some
)
503 o
->lineno_count
+= sec
->lineno_count
;
505 if (info
->relocateable
)
506 o
->reloc_count
+= sec
->reloc_count
;
508 if (sec
->_raw_size
> max_contents_size
)
509 max_contents_size
= sec
->_raw_size
;
510 if (sec
->lineno_count
> max_lineno_count
)
511 max_lineno_count
= sec
->lineno_count
;
512 if (sec
->reloc_count
> max_reloc_count
)
513 max_reloc_count
= sec
->reloc_count
;
515 else if (info
->relocateable
516 && (p
->type
== bfd_section_reloc_link_order
517 || p
->type
== bfd_symbol_reloc_link_order
))
520 if (o
->reloc_count
== 0)
524 o
->flags
|= SEC_RELOC
;
525 o
->rel_filepos
= rel_filepos
;
526 rel_filepos
+= o
->reloc_count
* relsz
;
530 /* If doing a relocateable link, allocate space for the pointers we
532 if (info
->relocateable
)
536 /* We use section_count + 1, rather than section_count, because
537 the target_index fields are 1 based. */
539 ((struct coff_link_section_info
*)
540 bfd_malloc ((abfd
->section_count
+ 1)
541 * sizeof (struct coff_link_section_info
)));
542 if (finfo
.section_info
== NULL
)
544 for (i
= 0; i
<= abfd
->section_count
; i
++)
546 finfo
.section_info
[i
].relocs
= NULL
;
547 finfo
.section_info
[i
].rel_hashes
= NULL
;
551 /* We now know the size of the relocs, so we can determine the file
552 positions of the line numbers. */
553 line_filepos
= rel_filepos
;
554 linesz
= bfd_coff_linesz (abfd
);
555 max_output_reloc_count
= 0;
556 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
558 if (o
->lineno_count
== 0)
562 o
->line_filepos
= line_filepos
;
563 line_filepos
+= o
->lineno_count
* linesz
;
566 if (o
->reloc_count
!= 0)
568 /* We don't know the indices of global symbols until we have
569 written out all the local symbols. For each section in
570 the output file, we keep an array of pointers to hash
571 table entries. Each entry in the array corresponds to a
572 reloc. When we find a reloc against a global symbol, we
573 set the corresponding entry in this array so that we can
574 fix up the symbol index after we have written out all the
577 Because of this problem, we also keep the relocs in
578 memory until the end of the link. This wastes memory,
579 but only when doing a relocateable link, which is not the
581 BFD_ASSERT (info
->relocateable
);
582 finfo
.section_info
[o
->target_index
].relocs
=
583 ((struct internal_reloc
*)
584 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
585 finfo
.section_info
[o
->target_index
].rel_hashes
=
586 ((struct coff_link_hash_entry
**)
587 bfd_malloc (o
->reloc_count
588 * sizeof (struct coff_link_hash_entry
*)));
589 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
590 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
593 if (o
->reloc_count
> max_output_reloc_count
)
594 max_output_reloc_count
= o
->reloc_count
;
597 /* Reset the reloc and lineno counts, so that we can use them to
598 count the number of entries we have output so far. */
603 obj_sym_filepos (abfd
) = line_filepos
;
605 /* Figure out the largest number of symbols in an input BFD. Take
606 the opportunity to clear the output_has_begun fields of all the
609 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
613 sub
->output_has_begun
= false;
614 sz
= obj_raw_syment_count (sub
);
615 if (sz
> max_sym_count
)
619 /* Allocate some buffers used while linking. */
620 finfo
.internal_syms
= ((struct internal_syment
*)
621 bfd_malloc (max_sym_count
622 * sizeof (struct internal_syment
)));
623 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
624 * sizeof (asection
*));
625 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
626 finfo
.outsyms
= ((bfd_byte
*)
627 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
628 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
629 * bfd_coff_linesz (abfd
));
630 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
631 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
632 if (! info
->relocateable
)
633 finfo
.internal_relocs
= ((struct internal_reloc
*)
634 bfd_malloc (max_reloc_count
635 * sizeof (struct internal_reloc
)));
636 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
637 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
638 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
639 || finfo
.outsyms
== NULL
640 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
641 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
642 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
643 || (! info
->relocateable
644 && finfo
.internal_relocs
== NULL
645 && max_reloc_count
> 0))
648 /* We now know the position of everything in the file, except that
649 we don't know the size of the symbol table and therefore we don't
650 know where the string table starts. We just build the string
651 table in memory as we go along. We process all the relocations
652 for a single input file at once. */
653 obj_raw_syment_count (abfd
) = 0;
655 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
657 if (! bfd_coff_start_final_link (abfd
, info
))
661 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
663 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
665 if (p
->type
== bfd_indirect_link_order
666 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
667 == bfd_target_coff_flavour
))
669 sub
= p
->u
.indirect
.section
->owner
;
670 if (! sub
->output_has_begun
)
672 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
674 sub
->output_has_begun
= true;
677 else if (p
->type
== bfd_section_reloc_link_order
678 || p
->type
== bfd_symbol_reloc_link_order
)
680 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
685 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
691 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
693 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
694 debug_merge_allocated
= false;
696 if (finfo
.internal_syms
!= NULL
)
698 free (finfo
.internal_syms
);
699 finfo
.internal_syms
= NULL
;
701 if (finfo
.sec_ptrs
!= NULL
)
703 free (finfo
.sec_ptrs
);
704 finfo
.sec_ptrs
= NULL
;
706 if (finfo
.sym_indices
!= NULL
)
708 free (finfo
.sym_indices
);
709 finfo
.sym_indices
= NULL
;
711 if (finfo
.linenos
!= NULL
)
713 free (finfo
.linenos
);
714 finfo
.linenos
= NULL
;
716 if (finfo
.contents
!= NULL
)
718 free (finfo
.contents
);
719 finfo
.contents
= NULL
;
721 if (finfo
.external_relocs
!= NULL
)
723 free (finfo
.external_relocs
);
724 finfo
.external_relocs
= NULL
;
726 if (finfo
.internal_relocs
!= NULL
)
728 free (finfo
.internal_relocs
);
729 finfo
.internal_relocs
= NULL
;
732 /* The value of the last C_FILE symbol is supposed to be the symbol
733 index of the first external symbol. Write it out again if
735 if (finfo
.last_file_index
!= -1
736 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
738 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
739 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
740 (PTR
) finfo
.outsyms
);
742 (obj_sym_filepos (abfd
)
743 + finfo
.last_file_index
* symesz
),
745 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
749 /* Write out the global symbols. */
750 finfo
.failed
= false;
751 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
756 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
757 if (finfo
.outsyms
!= NULL
)
759 free (finfo
.outsyms
);
760 finfo
.outsyms
= NULL
;
763 if (info
->relocateable
)
765 /* Now that we have written out all the global symbols, we know
766 the symbol indices to use for relocs against them, and we can
767 finally write out the relocs. */
768 external_relocs
= ((bfd_byte
*)
769 bfd_malloc (max_output_reloc_count
* relsz
));
770 if (external_relocs
== NULL
)
773 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
775 struct internal_reloc
*irel
;
776 struct internal_reloc
*irelend
;
777 struct coff_link_hash_entry
**rel_hash
;
780 if (o
->reloc_count
== 0)
783 irel
= finfo
.section_info
[o
->target_index
].relocs
;
784 irelend
= irel
+ o
->reloc_count
;
785 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
786 erel
= external_relocs
;
787 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
789 if (*rel_hash
!= NULL
)
791 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
792 irel
->r_symndx
= (*rel_hash
)->indx
;
794 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
797 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
798 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
799 abfd
) != relsz
* o
->reloc_count
)
803 free (external_relocs
);
804 external_relocs
= NULL
;
807 /* Free up the section information. */
808 if (finfo
.section_info
!= NULL
)
812 for (i
= 0; i
< abfd
->section_count
; i
++)
814 if (finfo
.section_info
[i
].relocs
!= NULL
)
815 free (finfo
.section_info
[i
].relocs
);
816 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
817 free (finfo
.section_info
[i
].rel_hashes
);
819 free (finfo
.section_info
);
820 finfo
.section_info
= NULL
;
823 /* Write out the string table. */
824 if (obj_raw_syment_count (abfd
) != 0)
827 (obj_sym_filepos (abfd
)
828 + obj_raw_syment_count (abfd
) * symesz
),
832 #if STRING_SIZE_SIZE == 4
834 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
835 (bfd_byte
*) strbuf
);
837 #error Change bfd_h_put_32
840 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
843 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
847 _bfd_stringtab_free (finfo
.strtab
);
849 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
850 not try to write out the symbols. */
851 bfd_get_symcount (abfd
) = 0;
856 if (debug_merge_allocated
)
857 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
858 if (finfo
.strtab
!= NULL
)
859 _bfd_stringtab_free (finfo
.strtab
);
860 if (finfo
.section_info
!= NULL
)
864 for (i
= 0; i
< abfd
->section_count
; i
++)
866 if (finfo
.section_info
[i
].relocs
!= NULL
)
867 free (finfo
.section_info
[i
].relocs
);
868 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
869 free (finfo
.section_info
[i
].rel_hashes
);
871 free (finfo
.section_info
);
873 if (finfo
.internal_syms
!= NULL
)
874 free (finfo
.internal_syms
);
875 if (finfo
.sec_ptrs
!= NULL
)
876 free (finfo
.sec_ptrs
);
877 if (finfo
.sym_indices
!= NULL
)
878 free (finfo
.sym_indices
);
879 if (finfo
.outsyms
!= NULL
)
880 free (finfo
.outsyms
);
881 if (finfo
.linenos
!= NULL
)
882 free (finfo
.linenos
);
883 if (finfo
.contents
!= NULL
)
884 free (finfo
.contents
);
885 if (finfo
.external_relocs
!= NULL
)
886 free (finfo
.external_relocs
);
887 if (finfo
.internal_relocs
!= NULL
)
888 free (finfo
.internal_relocs
);
889 if (external_relocs
!= NULL
)
890 free (external_relocs
);
894 /* parse out a -heap <reserved>,<commit> line */
897 dores_com (ptr
, output_bfd
, heap
)
902 if (coff_data(output_bfd
)->pe
)
904 int val
= strtoul (ptr
, &ptr
, 0);
906 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
908 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
912 int val
= strtoul (ptr
+1, &ptr
, 0);
914 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
916 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
922 static char *get_name(ptr
, dst
)
929 while (*ptr
&& *ptr
!= ' ')
935 /* Process any magic embedded commands in a section called .drectve */
938 process_embedded_commands (output_bfd
, info
, abfd
)
940 struct bfd_link_info
*info
;
943 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
950 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
953 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
958 e
= copy
+ sec
->_raw_size
;
959 for (s
= copy
; s
< e
; )
965 if (strncmp (s
,"-attr", 5) == 0)
977 s
= get_name(s
, &name
);
978 s
= get_name(s
, &attribs
);
998 asec
= bfd_get_section_by_name (abfd
, name
);
1001 asec
->flags
|= SEC_CODE
;
1003 asec
->flags
|= SEC_READONLY
;
1006 else if (strncmp (s
,"-heap", 5) == 0)
1008 s
= dores_com (s
+5, output_bfd
, 1);
1010 else if (strncmp (s
,"-stack", 6) == 0)
1012 s
= dores_com (s
+6, output_bfd
, 0);
1021 /* Link an input file into the linker output file. This function
1022 handles all the sections and relocations of the input file at once. */
1025 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1026 struct coff_final_link_info
*finfo
;
1029 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1030 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1031 asection
*, struct internal_reloc
*,
1034 const char *strings
;
1035 bfd_size_type syment_base
;
1036 unsigned int n_tmask
;
1037 unsigned int n_btshft
;
1039 bfd_size_type isymesz
;
1040 bfd_size_type osymesz
;
1041 bfd_size_type linesz
;
1044 struct internal_syment
*isymp
;
1047 unsigned long output_index
;
1049 struct coff_link_hash_entry
**sym_hash
;
1052 /* Move all the symbols to the output file. */
1054 output_bfd
= finfo
->output_bfd
;
1055 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1057 syment_base
= obj_raw_syment_count (output_bfd
);
1058 isymesz
= bfd_coff_symesz (input_bfd
);
1059 osymesz
= bfd_coff_symesz (output_bfd
);
1060 linesz
= bfd_coff_linesz (input_bfd
);
1061 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1063 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1064 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1066 /* Define macros so that ISFCN, et. al., macros work correctly. */
1067 #define N_TMASK n_tmask
1068 #define N_BTSHFT n_btshft
1071 if (! finfo
->info
->keep_memory
)
1074 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1077 if (! _bfd_coff_get_external_symbols (input_bfd
))
1080 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1081 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1082 isymp
= finfo
->internal_syms
;
1083 secpp
= finfo
->sec_ptrs
;
1084 indexp
= finfo
->sym_indices
;
1085 output_index
= syment_base
;
1086 outsym
= finfo
->outsyms
;
1088 if (coff_data (output_bfd
)->pe
)
1090 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1094 while (esym
< esym_end
)
1096 struct internal_syment isym
;
1101 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1103 /* Make a copy of *isymp so that the relocate_section function
1104 always sees the original values. This is more reliable than
1105 always recomputing the symbol value even if we are stripping
1109 if (isym
.n_scnum
!= 0)
1110 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1113 if (isym
.n_value
== 0)
1114 *secpp
= bfd_und_section_ptr
;
1116 *secpp
= bfd_com_section_ptr
;
1123 add
= 1 + isym
.n_numaux
;
1125 /* If we are stripping all symbols, we want to skip this one. */
1126 if (finfo
->info
->strip
== strip_all
)
1131 if (isym
.n_sclass
== C_EXT
1132 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1134 /* This is a global symbol. Global symbols come at the
1135 end of the symbol table, so skip them for now.
1136 Function symbols, however, are an exception, and are
1137 not moved to the end. */
1139 if (! ISFCN (isym
.n_type
))
1144 /* This is a local symbol. Skip it if we are discarding
1146 if (finfo
->info
->discard
== discard_all
)
1151 /* If we stripping debugging symbols, and this is a debugging
1152 symbol, then skip it. */
1154 && finfo
->info
->strip
== strip_debugger
1155 && isym
.n_scnum
== N_DEBUG
)
1158 /* If some symbols are stripped based on the name, work out the
1159 name and decide whether to skip this symbol. */
1161 && (finfo
->info
->strip
== strip_some
1162 || finfo
->info
->discard
== discard_l
))
1165 char buf
[SYMNMLEN
+ 1];
1167 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1171 if ((finfo
->info
->strip
== strip_some
1172 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1175 && finfo
->info
->discard
== discard_l
1176 && strncmp (name
, finfo
->info
->lprefix
,
1177 finfo
->info
->lprefix_len
) == 0))
1181 /* If this is an enum, struct, or union tag, see if we have
1182 already output an identical type. */
1184 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1185 && (isym
.n_sclass
== C_ENTAG
1186 || isym
.n_sclass
== C_STRTAG
1187 || isym
.n_sclass
== C_UNTAG
)
1188 && isym
.n_numaux
== 1)
1191 char buf
[SYMNMLEN
+ 1];
1192 struct coff_debug_merge_hash_entry
*mh
;
1193 struct coff_debug_merge_type
*mt
;
1194 union internal_auxent aux
;
1195 struct coff_debug_merge_element
**epp
;
1196 bfd_byte
*esl
, *eslend
;
1197 struct internal_syment
*islp
;
1199 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1203 /* Ignore fake names invented by compiler; treat them all as
1205 if (*name
== '~' || *name
== '.' || *name
== '$'
1206 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1207 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1210 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1215 /* Allocate memory to hold type information. If this turns
1216 out to be a duplicate, we pass this address to
1218 mt
= ((struct coff_debug_merge_type
*)
1219 bfd_alloc (input_bfd
,
1220 sizeof (struct coff_debug_merge_type
)));
1223 mt
->class = isym
.n_sclass
;
1225 /* Pick up the aux entry, which points to the end of the tag
1227 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1228 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1231 /* Gather the elements. */
1232 epp
= &mt
->elements
;
1233 mt
->elements
= NULL
;
1235 esl
= esym
+ 2 * isymesz
;
1236 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1237 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1238 while (esl
< eslend
)
1240 const char *elename
;
1241 char elebuf
[SYMNMLEN
+ 1];
1244 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1246 *epp
= ((struct coff_debug_merge_element
*)
1247 bfd_alloc (input_bfd
,
1248 sizeof (struct coff_debug_merge_element
)));
1252 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1254 if (elename
== NULL
)
1257 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1260 strcpy (copy
, elename
);
1262 (*epp
)->name
= copy
;
1263 (*epp
)->type
= islp
->n_type
;
1265 if (islp
->n_numaux
>= 1
1266 && islp
->n_type
!= T_NULL
1267 && islp
->n_sclass
!= C_EOS
)
1269 union internal_auxent eleaux
;
1272 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1273 islp
->n_type
, islp
->n_sclass
, 0,
1274 islp
->n_numaux
, (PTR
) &eleaux
);
1275 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1277 /* FIXME: If this tagndx entry refers to a symbol
1278 defined later in this file, we just ignore it.
1279 Handling this correctly would be tedious, and may
1285 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1288 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1289 if ((*epp
)->tagndx
< 0)
1293 epp
= &(*epp
)->next
;
1296 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1297 islp
+= islp
->n_numaux
+ 1;
1300 /* See if we already have a definition which matches this
1301 type. We always output the type if it has no elements,
1303 if (mt
->elements
== NULL
)
1304 bfd_release (input_bfd
, (PTR
) mt
);
1307 struct coff_debug_merge_type
*mtl
;
1309 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1311 struct coff_debug_merge_element
*me
, *mel
;
1313 if (mtl
->class != mt
->class)
1316 for (me
= mt
->elements
, mel
= mtl
->elements
;
1317 me
!= NULL
&& mel
!= NULL
;
1318 me
= me
->next
, mel
= mel
->next
)
1320 if (strcmp (me
->name
, mel
->name
) != 0
1321 || me
->type
!= mel
->type
1322 || me
->tagndx
!= mel
->tagndx
)
1326 if (me
== NULL
&& mel
== NULL
)
1330 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1332 /* This is the first definition of this type. */
1333 mt
->indx
= output_index
;
1334 mt
->next
= mh
->types
;
1339 /* This is a redefinition which can be merged. */
1340 bfd_release (input_bfd
, (PTR
) mt
);
1341 *indexp
= mtl
->indx
;
1342 add
= (eslend
- esym
) / isymesz
;
1348 /* We now know whether we are to skip this symbol or not. */
1351 /* Adjust the symbol in order to output it. */
1353 if (isym
._n
._n_n
._n_zeroes
== 0
1354 && isym
._n
._n_n
._n_offset
!= 0)
1359 /* This symbol has a long name. Enter it in the string
1360 table we are building. Note that we do not check
1361 bfd_coff_symname_in_debug. That is only true for
1362 XCOFF, and XCOFF requires different linking code
1364 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1368 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1369 if (indx
== (bfd_size_type
) -1)
1371 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1374 if (isym
.n_scnum
> 0)
1376 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1377 isym
.n_value
+= ((*secpp
)->output_section
->vma
1378 + (*secpp
)->output_offset
1382 /* The value of a C_FILE symbol is the symbol index of the
1383 next C_FILE symbol. The value of the last C_FILE symbol
1384 is the symbol index to the first external symbol
1385 (actually, coff_renumber_symbols does not get this
1386 right--it just sets the value of the last C_FILE symbol
1387 to zero--and nobody has ever complained about it). We
1388 try to get this right, below, just before we write the
1389 symbols out, but in the general case we may have to write
1390 the symbol out twice. */
1391 if (isym
.n_sclass
== C_FILE
)
1393 if (finfo
->last_file_index
!= -1
1394 && finfo
->last_file
.n_value
!= (long) output_index
)
1396 /* We must correct the value of the last C_FILE entry. */
1397 finfo
->last_file
.n_value
= output_index
;
1398 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1400 /* The last C_FILE symbol is in this input file. */
1401 bfd_coff_swap_sym_out (output_bfd
,
1402 (PTR
) &finfo
->last_file
,
1403 (PTR
) (finfo
->outsyms
1404 + ((finfo
->last_file_index
1410 /* We have already written out the last C_FILE
1411 symbol. We need to write it out again. We
1412 borrow *outsym temporarily. */
1413 bfd_coff_swap_sym_out (output_bfd
,
1414 (PTR
) &finfo
->last_file
,
1416 if (bfd_seek (output_bfd
,
1417 (obj_sym_filepos (output_bfd
)
1418 + finfo
->last_file_index
* osymesz
),
1420 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1426 finfo
->last_file_index
= output_index
;
1427 finfo
->last_file
= isym
;
1430 /* Output the symbol. */
1432 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1434 *indexp
= output_index
;
1439 struct coff_link_hash_entry
*h
;
1441 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1443 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1444 BFD_ASSERT (h
!= NULL
);
1445 h
->indx
= output_index
;
1448 output_index
+= add
;
1449 outsym
+= add
* osymesz
;
1452 esym
+= add
* isymesz
;
1456 for (--add
; add
> 0; --add
)
1463 /* Fix up the aux entries. This must be done in a separate pass,
1464 because we don't know the correct symbol indices until we have
1465 already decided which symbols we are going to keep. */
1467 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1468 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1469 isymp
= finfo
->internal_syms
;
1470 indexp
= finfo
->sym_indices
;
1471 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1472 outsym
= finfo
->outsyms
;
1473 while (esym
< esym_end
)
1477 add
= 1 + isymp
->n_numaux
;
1480 || (bfd_size_type
) *indexp
< syment_base
)
1481 && (*sym_hash
== NULL
1482 || (*sym_hash
)->auxbfd
!= input_bfd
))
1483 esym
+= add
* isymesz
;
1486 struct coff_link_hash_entry
*h
;
1493 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1501 /* Handle the aux entries. This handling is based on
1502 coff_pointerize_aux. I don't know if it always correct. */
1503 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1505 union internal_auxent aux
;
1506 union internal_auxent
*auxp
;
1512 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1513 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1518 if (isymp
->n_sclass
== C_FILE
)
1520 /* If this is a long filename, we must put it in the
1522 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1523 && auxp
->x_file
.x_n
.x_offset
!= 0)
1525 const char *filename
;
1528 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1529 >= STRING_SIZE_SIZE
);
1530 if (strings
== NULL
)
1532 strings
= _bfd_coff_read_string_table (input_bfd
);
1533 if (strings
== NULL
)
1536 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1537 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1539 if (indx
== (bfd_size_type
) -1)
1541 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1544 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1548 if (ISFCN (isymp
->n_type
)
1549 || ISTAG (isymp
->n_sclass
)
1550 || isymp
->n_sclass
== C_BLOCK
1551 || isymp
->n_sclass
== C_FCN
)
1553 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1555 && indx
< obj_raw_syment_count (input_bfd
))
1557 /* We look forward through the symbol for
1558 the index of the next symbol we are going
1559 to include. I don't know if this is
1561 while ((finfo
->sym_indices
[indx
] < 0
1562 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1564 && indx
< obj_raw_syment_count (input_bfd
))
1566 if (indx
>= obj_raw_syment_count (input_bfd
))
1567 indx
= output_index
;
1569 indx
= finfo
->sym_indices
[indx
];
1570 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1574 indx
= auxp
->x_sym
.x_tagndx
.l
;
1575 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1579 symindx
= finfo
->sym_indices
[indx
];
1581 auxp
->x_sym
.x_tagndx
.l
= 0;
1583 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1586 /* The .bf symbols are supposed to be linked through
1587 the endndx field. We need to carry this list
1588 across object files. */
1591 && isymp
->n_sclass
== C_FCN
1592 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1593 || isymp
->_n
._n_n
._n_offset
== 0)
1594 && isymp
->_n
._n_name
[0] == '.'
1595 && isymp
->_n
._n_name
[1] == 'b'
1596 && isymp
->_n
._n_name
[2] == 'f'
1597 && isymp
->_n
._n_name
[3] == '\0')
1599 if (finfo
->last_bf_index
!= -1)
1601 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1604 if ((bfd_size_type
) finfo
->last_bf_index
1609 /* The last .bf symbol is in this input
1610 file. This will only happen if the
1611 assembler did not set up the .bf
1612 endndx symbols correctly. */
1613 auxout
= (PTR
) (finfo
->outsyms
1614 + ((finfo
->last_bf_index
1617 bfd_coff_swap_aux_out (output_bfd
,
1618 (PTR
) &finfo
->last_bf
,
1626 /* We have already written out the last
1627 .bf aux entry. We need to write it
1628 out again. We borrow *outsym
1629 temporarily. FIXME: This case should
1631 bfd_coff_swap_aux_out (output_bfd
,
1632 (PTR
) &finfo
->last_bf
,
1637 if (bfd_seek (output_bfd
,
1638 (obj_sym_filepos (output_bfd
)
1639 + finfo
->last_bf_index
* osymesz
),
1641 || bfd_write (outsym
, osymesz
, 1,
1642 output_bfd
) != osymesz
)
1647 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1648 finfo
->last_bf_index
= -1;
1651 /* The endndx field of this aux entry must
1652 be updated with the symbol number of the
1654 finfo
->last_bf
= *auxp
;
1655 finfo
->last_bf_index
= ((outsym
- finfo
->outsyms
)
1663 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1664 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1678 /* Relocate the line numbers, unless we are stripping them. */
1679 if (finfo
->info
->strip
== strip_none
1680 || finfo
->info
->strip
== strip_some
)
1682 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1688 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1689 build_link_order in ldwrite.c will not have created a
1690 link order, which means that we will not have seen this
1691 input section in _bfd_coff_final_link, which means that
1692 we will not have allocated space for the line numbers of
1693 this section. I don't think line numbers can be
1694 meaningful for a section which does not have
1695 SEC_HAS_CONTENTS set, but, if they do, this must be
1697 if (o
->lineno_count
== 0
1698 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1701 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1702 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1703 input_bfd
) != linesz
* o
->lineno_count
)
1706 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1707 eline
= finfo
->linenos
;
1708 elineend
= eline
+ linesz
* o
->lineno_count
;
1709 for (; eline
< elineend
; eline
+= linesz
)
1711 struct internal_lineno iline
;
1713 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1715 if (iline
.l_lnno
!= 0)
1716 iline
.l_addr
.l_paddr
+= offset
;
1717 else if (iline
.l_addr
.l_symndx
>= 0
1718 && ((unsigned long) iline
.l_addr
.l_symndx
1719 < obj_raw_syment_count (input_bfd
)))
1723 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1727 /* These line numbers are attached to a symbol
1728 which we are stripping. We should really
1729 just discard the line numbers, but that would
1730 be a pain because we have already counted
1736 struct internal_syment is
;
1737 union internal_auxent ia
;
1739 /* Fix up the lnnoptr field in the aux entry of
1740 the symbol. It turns out that we can't do
1741 this when we modify the symbol aux entries,
1742 because gas sometimes screws up the lnnoptr
1743 field and makes it an offset from the start
1744 of the line numbers rather than an absolute
1746 bfd_coff_swap_sym_in (output_bfd
,
1747 (PTR
) (finfo
->outsyms
1748 + ((indx
- syment_base
)
1751 if ((ISFCN (is
.n_type
)
1752 || is
.n_sclass
== C_BLOCK
)
1753 && is
.n_numaux
>= 1)
1757 auxptr
= (PTR
) (finfo
->outsyms
1758 + ((indx
- syment_base
+ 1)
1760 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1761 is
.n_type
, is
.n_sclass
,
1762 0, is
.n_numaux
, (PTR
) &ia
);
1763 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1764 (o
->output_section
->line_filepos
1765 + o
->output_section
->lineno_count
* linesz
1766 + eline
- finfo
->linenos
);
1767 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1768 is
.n_type
, is
.n_sclass
, 0,
1769 is
.n_numaux
, auxptr
);
1773 iline
.l_addr
.l_symndx
= indx
;
1776 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1779 if (bfd_seek (output_bfd
,
1780 (o
->output_section
->line_filepos
1781 + o
->output_section
->lineno_count
* linesz
),
1783 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1784 output_bfd
) != linesz
* o
->lineno_count
)
1787 o
->output_section
->lineno_count
+= o
->lineno_count
;
1791 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1792 symbol will be the first symbol in the next input file. In the
1793 normal case, this will save us from writing out the C_FILE symbol
1795 if (finfo
->last_file_index
!= -1
1796 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1798 finfo
->last_file
.n_value
= output_index
;
1799 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1800 (PTR
) (finfo
->outsyms
1801 + ((finfo
->last_file_index
- syment_base
)
1805 /* Write the modified symbols to the output file. */
1806 if (outsym
> finfo
->outsyms
)
1808 if (bfd_seek (output_bfd
,
1809 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1811 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1813 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1816 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1817 + (outsym
- finfo
->outsyms
) / osymesz
)
1820 obj_raw_syment_count (output_bfd
) = output_index
;
1823 /* Relocate the contents of each section. */
1824 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1825 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1829 if (! o
->linker_mark
)
1831 /* This section was omitted from the link. */
1835 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1837 if ((o
->flags
& SEC_RELOC
) != 0
1838 && o
->reloc_count
!= 0)
1840 ((*_bfd_error_handler
)
1841 ("%s: relocs in section `%s', but it has no contents",
1842 bfd_get_filename (input_bfd
),
1843 bfd_get_section_name (input_bfd
, o
)));
1844 bfd_set_error (bfd_error_no_contents
);
1851 if (coff_section_data (input_bfd
, o
) != NULL
1852 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1853 contents
= coff_section_data (input_bfd
, o
)->contents
;
1856 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1857 (file_ptr
) 0, o
->_raw_size
))
1859 contents
= finfo
->contents
;
1862 if ((o
->flags
& SEC_RELOC
) != 0)
1865 struct internal_reloc
*internal_relocs
;
1866 struct internal_reloc
*irel
;
1868 /* Read in the relocs. */
1869 target_index
= o
->output_section
->target_index
;
1870 internal_relocs
= (_bfd_coff_read_internal_relocs
1871 (input_bfd
, o
, false, finfo
->external_relocs
,
1872 finfo
->info
->relocateable
,
1873 (finfo
->info
->relocateable
1874 ? (finfo
->section_info
[target_index
].relocs
1875 + o
->output_section
->reloc_count
)
1876 : finfo
->internal_relocs
)));
1877 if (internal_relocs
== NULL
)
1880 /* Call processor specific code to relocate the section
1882 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1886 finfo
->internal_syms
,
1890 if (finfo
->info
->relocateable
)
1893 struct internal_reloc
*irelend
;
1894 struct coff_link_hash_entry
**rel_hash
;
1896 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1897 irel
= internal_relocs
;
1898 irelend
= irel
+ o
->reloc_count
;
1899 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1900 + o
->output_section
->reloc_count
);
1901 for (; irel
< irelend
; irel
++, rel_hash
++)
1903 struct coff_link_hash_entry
*h
;
1908 /* Adjust the reloc address and symbol index. */
1910 irel
->r_vaddr
+= offset
;
1912 if (irel
->r_symndx
== -1)
1917 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1925 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1928 /* This is a global symbol. */
1930 irel
->r_symndx
= h
->indx
;
1933 /* This symbol is being written at the end
1934 of the file, and we do not yet know the
1935 symbol index. We save the pointer to the
1936 hash table entry in the rel_hash list.
1937 We set the indx field to -2 to indicate
1938 that this symbol must not be stripped. */
1947 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1949 irel
->r_symndx
= indx
;
1952 struct internal_syment
*is
;
1954 char buf
[SYMNMLEN
+ 1];
1956 /* This reloc is against a symbol we are
1957 stripping. It would be possible to
1958 handle this case, but I don't think it's
1960 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1962 name
= (_bfd_coff_internal_syment_name
1963 (input_bfd
, is
, buf
));
1967 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1968 (finfo
->info
, name
, input_bfd
, o
,
1975 o
->output_section
->reloc_count
+= o
->reloc_count
;
1979 /* Write out the modified section contents. */
1980 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1981 contents
, o
->output_offset
,
1982 (o
->_cooked_size
!= 0
1988 if (! finfo
->info
->keep_memory
)
1990 if (! _bfd_coff_free_symbols (input_bfd
))
1997 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2000 _bfd_coff_write_global_sym (h
, data
)
2001 struct coff_link_hash_entry
*h
;
2004 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2006 struct internal_syment isym
;
2007 bfd_size_type symesz
;
2010 output_bfd
= finfo
->output_bfd
;
2016 && (finfo
->info
->strip
== strip_all
2017 || (finfo
->info
->strip
== strip_some
2018 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2019 h
->root
.root
.string
, false, false)
2023 switch (h
->root
.type
)
2026 case bfd_link_hash_new
:
2030 case bfd_link_hash_undefined
:
2031 case bfd_link_hash_undefweak
:
2032 isym
.n_scnum
= N_UNDEF
;
2036 case bfd_link_hash_defined
:
2037 case bfd_link_hash_defweak
:
2041 sec
= h
->root
.u
.def
.section
->output_section
;
2042 if (bfd_is_abs_section (sec
))
2043 isym
.n_scnum
= N_ABS
;
2045 isym
.n_scnum
= sec
->target_index
;
2046 isym
.n_value
= (h
->root
.u
.def
.value
2048 + h
->root
.u
.def
.section
->output_offset
);
2052 case bfd_link_hash_common
:
2053 isym
.n_scnum
= N_UNDEF
;
2054 isym
.n_value
= h
->root
.u
.c
.size
;
2057 case bfd_link_hash_indirect
:
2058 case bfd_link_hash_warning
:
2059 /* Just ignore these. They can't be handled anyhow. */
2063 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2064 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2071 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2073 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2075 if (indx
== (bfd_size_type
) -1)
2077 finfo
->failed
= true;
2080 isym
._n
._n_n
._n_zeroes
= 0;
2081 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2084 isym
.n_sclass
= h
->class;
2085 isym
.n_type
= h
->type
;
2087 if (isym
.n_sclass
== C_NULL
)
2088 isym
.n_sclass
= C_EXT
;
2090 isym
.n_numaux
= h
->numaux
;
2092 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2094 symesz
= bfd_coff_symesz (output_bfd
);
2096 if (bfd_seek (output_bfd
,
2097 (obj_sym_filepos (output_bfd
)
2098 + obj_raw_syment_count (output_bfd
) * symesz
),
2100 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2102 finfo
->failed
= true;
2106 h
->indx
= obj_raw_syment_count (output_bfd
);
2108 ++obj_raw_syment_count (output_bfd
);
2110 /* Write out any associated aux entries. There normally will be
2111 none. If there are any, I have no idea how to modify them. */
2112 for (i
= 0; i
< isym
.n_numaux
; i
++)
2114 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2115 isym
.n_sclass
, i
, isym
.n_numaux
,
2116 (PTR
) finfo
->outsyms
);
2117 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2119 finfo
->failed
= true;
2122 ++obj_raw_syment_count (output_bfd
);
2128 /* Handle a link order which is supposed to generate a reloc. */
2131 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2133 struct coff_final_link_info
*finfo
;
2134 asection
*output_section
;
2135 struct bfd_link_order
*link_order
;
2137 reloc_howto_type
*howto
;
2138 struct internal_reloc
*irel
;
2139 struct coff_link_hash_entry
**rel_hash_ptr
;
2141 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2144 bfd_set_error (bfd_error_bad_value
);
2148 if (link_order
->u
.reloc
.p
->addend
!= 0)
2152 bfd_reloc_status_type rstat
;
2155 size
= bfd_get_reloc_size (howto
);
2156 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2160 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2161 link_order
->u
.reloc
.p
->addend
, buf
);
2167 case bfd_reloc_outofrange
:
2169 case bfd_reloc_overflow
:
2170 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2172 (link_order
->type
== bfd_section_reloc_link_order
2173 ? bfd_section_name (output_bfd
,
2174 link_order
->u
.reloc
.p
->u
.section
)
2175 : link_order
->u
.reloc
.p
->u
.name
),
2176 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2177 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2184 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2185 (file_ptr
) link_order
->offset
, size
);
2191 /* Store the reloc information in the right place. It will get
2192 swapped and written out at the end of the final_link routine. */
2194 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2195 + output_section
->reloc_count
);
2196 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2197 + output_section
->reloc_count
);
2199 memset (irel
, 0, sizeof (struct internal_reloc
));
2200 *rel_hash_ptr
= NULL
;
2202 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2204 if (link_order
->type
== bfd_section_reloc_link_order
)
2206 /* We need to somehow locate a symbol in the right section. The
2207 symbol must either have a value of zero, or we must adjust
2208 the addend by the value of the symbol. FIXME: Write this
2209 when we need it. The old linker couldn't handle this anyhow. */
2211 *rel_hash_ptr
= NULL
;
2216 struct coff_link_hash_entry
*h
;
2218 h
= ((struct coff_link_hash_entry
*)
2219 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2220 link_order
->u
.reloc
.p
->u
.name
,
2221 false, false, true));
2225 irel
->r_symndx
= h
->indx
;
2228 /* Set the index to -2 to force this symbol to get
2237 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2238 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2239 (asection
*) NULL
, (bfd_vma
) 0)))
2245 /* FIXME: Is this always right? */
2246 irel
->r_type
= howto
->type
;
2248 /* r_size is only used on the RS/6000, which needs its own linker
2249 routines anyhow. r_extern is only used for ECOFF. */
2251 /* FIXME: What is the right value for r_offset? Is zero OK? */
2253 ++output_section
->reloc_count
;
2258 /* A basic reloc handling routine which may be used by processors with
2262 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2263 input_section
, contents
, relocs
, syms
,
2266 struct bfd_link_info
*info
;
2268 asection
*input_section
;
2270 struct internal_reloc
*relocs
;
2271 struct internal_syment
*syms
;
2272 asection
**sections
;
2274 struct internal_reloc
*rel
;
2275 struct internal_reloc
*relend
;
2278 relend
= rel
+ input_section
->reloc_count
;
2279 for (; rel
< relend
; rel
++)
2282 struct coff_link_hash_entry
*h
;
2283 struct internal_syment
*sym
;
2286 reloc_howto_type
*howto
;
2287 bfd_reloc_status_type rstat
;
2289 symndx
= rel
->r_symndx
;
2298 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2299 sym
= syms
+ symndx
;
2302 /* COFF treats common symbols in one of two ways. Either the
2303 size of the symbol is included in the section contents, or it
2304 is not. We assume that the size is not included, and force
2305 the rtype_to_howto function to adjust the addend as needed. */
2307 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2308 addend
= - sym
->n_value
;
2313 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2326 sec
= bfd_abs_section_ptr
;
2331 sec
= sections
[symndx
];
2332 val
= (sec
->output_section
->vma
2333 + sec
->output_offset
2340 if (h
->root
.type
== bfd_link_hash_defined
2341 || h
->root
.type
== bfd_link_hash_defweak
)
2345 sec
= h
->root
.u
.def
.section
;
2346 val
= (h
->root
.u
.def
.value
2347 + sec
->output_section
->vma
2348 + sec
->output_offset
);
2351 else if (! info
->relocateable
)
2353 if (! ((*info
->callbacks
->undefined_symbol
)
2354 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2355 rel
->r_vaddr
- input_section
->vma
)))
2360 if (info
->base_file
)
2362 /* Emit a reloc if the backend thinks it needs it. */
2363 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2365 /* relocation to a symbol in a section which
2366 isn't absolute - we output the address here
2368 bfd_vma addr
= rel
->r_vaddr
2369 - input_section
->vma
2370 + input_section
->output_offset
2371 + input_section
->output_section
->vma
;
2372 if (coff_data(output_bfd
)->pe
)
2373 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2374 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2378 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2380 rel
->r_vaddr
- input_section
->vma
,
2389 case bfd_reloc_overflow
:
2392 char buf
[SYMNMLEN
+ 1];
2397 name
= h
->root
.root
.string
;
2400 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2405 if (! ((*info
->callbacks
->reloc_overflow
)
2406 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2407 input_section
, rel
->r_vaddr
- input_section
->vma
)))