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
.internal_syms
= NULL
;
451 finfo
.sec_ptrs
= NULL
;
452 finfo
.sym_indices
= NULL
;
453 finfo
.outsyms
= NULL
;
454 finfo
.linenos
= NULL
;
455 finfo
.contents
= NULL
;
456 finfo
.external_relocs
= NULL
;
457 finfo
.internal_relocs
= NULL
;
458 debug_merge_allocated
= false;
460 coff_data (abfd
)->link_info
= info
;
462 finfo
.strtab
= _bfd_stringtab_init ();
463 if (finfo
.strtab
== NULL
)
466 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
468 debug_merge_allocated
= true;
470 /* Compute the file positions for all the sections. */
471 if (! abfd
->output_has_begun
)
472 bfd_coff_compute_section_file_positions (abfd
);
474 /* Count the line numbers and relocation entries required for the
475 output file. Set the file positions for the relocs. */
476 rel_filepos
= obj_relocbase (abfd
);
477 relsz
= bfd_coff_relsz (abfd
);
478 max_contents_size
= 0;
479 max_lineno_count
= 0;
482 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
486 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 if (info
->strip
== strip_none
496 || info
->strip
== strip_some
)
497 o
->lineno_count
+= sec
->lineno_count
;
499 if (info
->relocateable
)
500 o
->reloc_count
+= sec
->reloc_count
;
502 if (sec
->_raw_size
> max_contents_size
)
503 max_contents_size
= sec
->_raw_size
;
504 if (sec
->lineno_count
> max_lineno_count
)
505 max_lineno_count
= sec
->lineno_count
;
506 if (sec
->reloc_count
> max_reloc_count
)
507 max_reloc_count
= sec
->reloc_count
;
509 else if (info
->relocateable
510 && (p
->type
== bfd_section_reloc_link_order
511 || p
->type
== bfd_symbol_reloc_link_order
))
514 if (o
->reloc_count
== 0)
518 o
->flags
|= SEC_RELOC
;
519 o
->rel_filepos
= rel_filepos
;
520 rel_filepos
+= o
->reloc_count
* relsz
;
524 /* If doing a relocateable link, allocate space for the pointers we
526 if (info
->relocateable
)
530 /* We use section_count + 1, rather than section_count, because
531 the target_index fields are 1 based. */
533 ((struct coff_link_section_info
*)
534 bfd_malloc ((abfd
->section_count
+ 1)
535 * sizeof (struct coff_link_section_info
)));
536 if (finfo
.section_info
== NULL
)
538 for (i
= 0; i
<= abfd
->section_count
; i
++)
540 finfo
.section_info
[i
].relocs
= NULL
;
541 finfo
.section_info
[i
].rel_hashes
= NULL
;
545 /* We now know the size of the relocs, so we can determine the file
546 positions of the line numbers. */
547 line_filepos
= rel_filepos
;
548 linesz
= bfd_coff_linesz (abfd
);
549 max_output_reloc_count
= 0;
550 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
552 if (o
->lineno_count
== 0)
556 o
->line_filepos
= line_filepos
;
557 line_filepos
+= o
->lineno_count
* linesz
;
560 if (o
->reloc_count
!= 0)
562 /* We don't know the indices of global symbols until we have
563 written out all the local symbols. For each section in
564 the output file, we keep an array of pointers to hash
565 table entries. Each entry in the array corresponds to a
566 reloc. When we find a reloc against a global symbol, we
567 set the corresponding entry in this array so that we can
568 fix up the symbol index after we have written out all the
571 Because of this problem, we also keep the relocs in
572 memory until the end of the link. This wastes memory,
573 but only when doing a relocateable link, which is not the
575 BFD_ASSERT (info
->relocateable
);
576 finfo
.section_info
[o
->target_index
].relocs
=
577 ((struct internal_reloc
*)
578 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
579 finfo
.section_info
[o
->target_index
].rel_hashes
=
580 ((struct coff_link_hash_entry
**)
581 bfd_malloc (o
->reloc_count
582 * sizeof (struct coff_link_hash_entry
*)));
583 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
584 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
587 if (o
->reloc_count
> max_output_reloc_count
)
588 max_output_reloc_count
= o
->reloc_count
;
591 /* Reset the reloc and lineno counts, so that we can use them to
592 count the number of entries we have output so far. */
597 obj_sym_filepos (abfd
) = line_filepos
;
599 /* Figure out the largest number of symbols in an input BFD. Take
600 the opportunity to clear the output_has_begun fields of all the
603 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
607 sub
->output_has_begun
= false;
608 sz
= obj_raw_syment_count (sub
);
609 if (sz
> max_sym_count
)
613 /* Allocate some buffers used while linking. */
614 finfo
.internal_syms
= ((struct internal_syment
*)
615 bfd_malloc (max_sym_count
616 * sizeof (struct internal_syment
)));
617 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
618 * sizeof (asection
*));
619 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
620 finfo
.outsyms
= ((bfd_byte
*)
621 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
622 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
623 * bfd_coff_linesz (abfd
));
624 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
625 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
626 if (! info
->relocateable
)
627 finfo
.internal_relocs
= ((struct internal_reloc
*)
628 bfd_malloc (max_reloc_count
629 * sizeof (struct internal_reloc
)));
630 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
631 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
632 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
633 || finfo
.outsyms
== NULL
634 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
635 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
636 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
637 || (! info
->relocateable
638 && finfo
.internal_relocs
== NULL
639 && max_reloc_count
> 0))
642 /* We now know the position of everything in the file, except that
643 we don't know the size of the symbol table and therefore we don't
644 know where the string table starts. We just build the string
645 table in memory as we go along. We process all the relocations
646 for a single input file at once. */
647 obj_raw_syment_count (abfd
) = 0;
649 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
651 if (! bfd_coff_start_final_link (abfd
, info
))
655 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
657 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
659 if (p
->type
== bfd_indirect_link_order
660 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
661 == bfd_target_coff_flavour
))
663 sub
= p
->u
.indirect
.section
->owner
;
664 if (! sub
->output_has_begun
)
666 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
668 sub
->output_has_begun
= true;
671 else if (p
->type
== bfd_section_reloc_link_order
672 || p
->type
== bfd_symbol_reloc_link_order
)
674 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
679 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
685 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
687 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
688 debug_merge_allocated
= false;
690 if (finfo
.internal_syms
!= NULL
)
692 free (finfo
.internal_syms
);
693 finfo
.internal_syms
= NULL
;
695 if (finfo
.sec_ptrs
!= NULL
)
697 free (finfo
.sec_ptrs
);
698 finfo
.sec_ptrs
= NULL
;
700 if (finfo
.sym_indices
!= NULL
)
702 free (finfo
.sym_indices
);
703 finfo
.sym_indices
= NULL
;
705 if (finfo
.linenos
!= NULL
)
707 free (finfo
.linenos
);
708 finfo
.linenos
= NULL
;
710 if (finfo
.contents
!= NULL
)
712 free (finfo
.contents
);
713 finfo
.contents
= NULL
;
715 if (finfo
.external_relocs
!= NULL
)
717 free (finfo
.external_relocs
);
718 finfo
.external_relocs
= NULL
;
720 if (finfo
.internal_relocs
!= NULL
)
722 free (finfo
.internal_relocs
);
723 finfo
.internal_relocs
= NULL
;
726 /* The value of the last C_FILE symbol is supposed to be the symbol
727 index of the first external symbol. Write it out again if
729 if (finfo
.last_file_index
!= -1
730 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
732 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
733 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
734 (PTR
) finfo
.outsyms
);
736 (obj_sym_filepos (abfd
)
737 + finfo
.last_file_index
* symesz
),
739 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
743 /* Write out the global symbols. */
744 finfo
.failed
= false;
745 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
750 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
751 if (finfo
.outsyms
!= NULL
)
753 free (finfo
.outsyms
);
754 finfo
.outsyms
= NULL
;
757 if (info
->relocateable
)
759 /* Now that we have written out all the global symbols, we know
760 the symbol indices to use for relocs against them, and we can
761 finally write out the relocs. */
762 external_relocs
= ((bfd_byte
*)
763 bfd_malloc (max_output_reloc_count
* relsz
));
764 if (external_relocs
== NULL
)
767 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
769 struct internal_reloc
*irel
;
770 struct internal_reloc
*irelend
;
771 struct coff_link_hash_entry
**rel_hash
;
774 if (o
->reloc_count
== 0)
777 irel
= finfo
.section_info
[o
->target_index
].relocs
;
778 irelend
= irel
+ o
->reloc_count
;
779 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
780 erel
= external_relocs
;
781 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
783 if (*rel_hash
!= NULL
)
785 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
786 irel
->r_symndx
= (*rel_hash
)->indx
;
788 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
791 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
792 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
793 abfd
) != relsz
* o
->reloc_count
)
797 free (external_relocs
);
798 external_relocs
= NULL
;
801 /* Free up the section information. */
802 if (finfo
.section_info
!= NULL
)
806 for (i
= 0; i
< abfd
->section_count
; i
++)
808 if (finfo
.section_info
[i
].relocs
!= NULL
)
809 free (finfo
.section_info
[i
].relocs
);
810 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
811 free (finfo
.section_info
[i
].rel_hashes
);
813 free (finfo
.section_info
);
814 finfo
.section_info
= NULL
;
817 /* Write out the string table. */
818 if (obj_raw_syment_count (abfd
) != 0)
821 (obj_sym_filepos (abfd
)
822 + obj_raw_syment_count (abfd
) * symesz
),
826 #if STRING_SIZE_SIZE == 4
828 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
829 (bfd_byte
*) strbuf
);
831 #error Change bfd_h_put_32
834 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
837 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
841 _bfd_stringtab_free (finfo
.strtab
);
843 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
844 not try to write out the symbols. */
845 bfd_get_symcount (abfd
) = 0;
850 if (debug_merge_allocated
)
851 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
852 if (finfo
.strtab
!= NULL
)
853 _bfd_stringtab_free (finfo
.strtab
);
854 if (finfo
.section_info
!= NULL
)
858 for (i
= 0; i
< abfd
->section_count
; i
++)
860 if (finfo
.section_info
[i
].relocs
!= NULL
)
861 free (finfo
.section_info
[i
].relocs
);
862 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
863 free (finfo
.section_info
[i
].rel_hashes
);
865 free (finfo
.section_info
);
867 if (finfo
.internal_syms
!= NULL
)
868 free (finfo
.internal_syms
);
869 if (finfo
.sec_ptrs
!= NULL
)
870 free (finfo
.sec_ptrs
);
871 if (finfo
.sym_indices
!= NULL
)
872 free (finfo
.sym_indices
);
873 if (finfo
.outsyms
!= NULL
)
874 free (finfo
.outsyms
);
875 if (finfo
.linenos
!= NULL
)
876 free (finfo
.linenos
);
877 if (finfo
.contents
!= NULL
)
878 free (finfo
.contents
);
879 if (finfo
.external_relocs
!= NULL
)
880 free (finfo
.external_relocs
);
881 if (finfo
.internal_relocs
!= NULL
)
882 free (finfo
.internal_relocs
);
883 if (external_relocs
!= NULL
)
884 free (external_relocs
);
888 /* parse out a -heap <reserved>,<commit> line */
891 dores_com (ptr
, output_bfd
, heap
)
896 if (coff_data(output_bfd
)->pe
)
898 int val
= strtoul (ptr
, &ptr
, 0);
900 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
902 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
906 int val
= strtoul (ptr
+1, &ptr
, 0);
908 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
910 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
916 static char *get_name(ptr
, dst
)
923 while (*ptr
&& *ptr
!= ' ')
929 /* Process any magic embedded commands in a section called .drectve */
932 process_embedded_commands (output_bfd
, info
, abfd
)
934 struct bfd_link_info
*info
;
937 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
944 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
947 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
952 e
= copy
+ sec
->_raw_size
;
953 for (s
= copy
; s
< e
; )
959 if (strncmp (s
,"-attr", 5) == 0)
971 s
= get_name(s
, &name
);
972 s
= get_name(s
, &attribs
);
992 asec
= bfd_get_section_by_name (abfd
, name
);
995 asec
->flags
|= SEC_CODE
;
997 asec
->flags
|= SEC_READONLY
;
1000 else if (strncmp (s
,"-heap", 5) == 0)
1002 s
= dores_com (s
+5, output_bfd
, 1);
1004 else if (strncmp (s
,"-stack", 6) == 0)
1006 s
= dores_com (s
+6, output_bfd
, 0);
1015 /* Link an input file into the linker output file. This function
1016 handles all the sections and relocations of the input file at once. */
1019 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1020 struct coff_final_link_info
*finfo
;
1023 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1024 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1025 asection
*, struct internal_reloc
*,
1028 const char *strings
;
1029 bfd_size_type syment_base
;
1030 unsigned int n_tmask
;
1031 unsigned int n_btshft
;
1033 bfd_size_type isymesz
;
1034 bfd_size_type osymesz
;
1035 bfd_size_type linesz
;
1038 struct internal_syment
*isymp
;
1041 unsigned long output_index
;
1043 struct coff_link_hash_entry
**sym_hash
;
1046 /* Move all the symbols to the output file. */
1048 output_bfd
= finfo
->output_bfd
;
1049 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1051 syment_base
= obj_raw_syment_count (output_bfd
);
1052 isymesz
= bfd_coff_symesz (input_bfd
);
1053 osymesz
= bfd_coff_symesz (output_bfd
);
1054 linesz
= bfd_coff_linesz (input_bfd
);
1055 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1057 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1058 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1060 /* Define macros so that ISFCN, et. al., macros work correctly. */
1061 #define N_TMASK n_tmask
1062 #define N_BTSHFT n_btshft
1065 if (! finfo
->info
->keep_memory
)
1068 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1071 if (! _bfd_coff_get_external_symbols (input_bfd
))
1074 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1075 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1076 isymp
= finfo
->internal_syms
;
1077 secpp
= finfo
->sec_ptrs
;
1078 indexp
= finfo
->sym_indices
;
1079 output_index
= syment_base
;
1080 outsym
= finfo
->outsyms
;
1082 if (coff_data(output_bfd
)->pe
)
1084 if (!process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1088 while (esym
< esym_end
)
1090 struct internal_syment isym
;
1095 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1097 /* Make a copy of *isymp so that the relocate_section function
1098 always sees the original values. This is more reliable than
1099 always recomputing the symbol value even if we are stripping
1103 if (isym
.n_scnum
!= 0)
1104 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1107 if (isym
.n_value
== 0)
1108 *secpp
= bfd_und_section_ptr
;
1110 *secpp
= bfd_com_section_ptr
;
1117 add
= 1 + isym
.n_numaux
;
1119 /* If we are stripping all symbols, we want to skip this one. */
1120 if (finfo
->info
->strip
== strip_all
)
1125 if (isym
.n_sclass
== C_EXT
1126 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1128 /* This is a global symbol. Global symbols come at the
1129 end of the symbol table, so skip them for now.
1130 Function symbols, however, are an exception, and are
1131 not moved to the end. */
1133 if (! ISFCN (isym
.n_type
))
1138 /* This is a local symbol. Skip it if we are discarding
1140 if (finfo
->info
->discard
== discard_all
)
1145 /* If we stripping debugging symbols, and this is a debugging
1146 symbol, then skip it. */
1148 && finfo
->info
->strip
== strip_debugger
1149 && isym
.n_scnum
== N_DEBUG
)
1152 /* If some symbols are stripped based on the name, work out the
1153 name and decide whether to skip this symbol. */
1155 && (finfo
->info
->strip
== strip_some
1156 || finfo
->info
->discard
== discard_l
))
1159 char buf
[SYMNMLEN
+ 1];
1161 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1165 if ((finfo
->info
->strip
== strip_some
1166 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1169 && finfo
->info
->discard
== discard_l
1170 && strncmp (name
, finfo
->info
->lprefix
,
1171 finfo
->info
->lprefix_len
) == 0))
1175 /* If this is an enum, struct, or union tag, see if we have
1176 already output an identical type. */
1178 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1179 && (isym
.n_sclass
== C_ENTAG
1180 || isym
.n_sclass
== C_STRTAG
1181 || isym
.n_sclass
== C_UNTAG
)
1182 && isym
.n_numaux
== 1)
1185 char buf
[SYMNMLEN
+ 1];
1186 struct coff_debug_merge_hash_entry
*mh
;
1187 struct coff_debug_merge_type
*mt
;
1188 union internal_auxent aux
;
1189 struct coff_debug_merge_element
**epp
;
1190 bfd_byte
*esl
, *eslend
;
1191 struct internal_syment
*islp
;
1193 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1197 /* Ignore fake names invented by compiler; treat them all as
1199 if (*name
== '~' || *name
== '.'
1200 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1201 && (name
[1] == '~' || name
[1] == '.')))
1204 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1209 /* Allocate memory to hold type information. If this turns
1210 out to be a duplicate, we pass this address to
1212 mt
= ((struct coff_debug_merge_type
*)
1213 bfd_alloc (input_bfd
,
1214 sizeof (struct coff_debug_merge_type
)));
1217 mt
->class = isym
.n_sclass
;
1219 /* Pick up the aux entry, which points to the end of the tag
1221 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1222 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1225 /* Gather the elements. */
1226 epp
= &mt
->elements
;
1227 mt
->elements
= NULL
;
1229 esl
= esym
+ 2 * isymesz
;
1230 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1231 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1232 while (esl
< eslend
)
1234 const char *elename
;
1235 char elebuf
[SYMNMLEN
+ 1];
1238 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1240 *epp
= ((struct coff_debug_merge_element
*)
1241 bfd_alloc (input_bfd
,
1242 sizeof (struct coff_debug_merge_element
)));
1246 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1248 if (elename
== NULL
)
1251 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1254 strcpy (copy
, elename
);
1256 (*epp
)->name
= copy
;
1257 (*epp
)->type
= islp
->n_type
;
1259 if (islp
->n_numaux
>= 1
1260 && islp
->n_type
!= T_NULL
1261 && islp
->n_sclass
!= C_EOS
)
1263 union internal_auxent eleaux
;
1266 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1267 islp
->n_type
, islp
->n_sclass
, 0,
1268 islp
->n_numaux
, (PTR
) &eleaux
);
1269 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1271 /* FIXME: If this tagndx entry refers to a symbol
1272 defined later in this file, we just ignore it.
1273 Handling this correctly would be tedious, and may
1279 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1282 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1283 if ((*epp
)->tagndx
< 0)
1287 epp
= &(*epp
)->next
;
1290 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1291 islp
+= islp
->n_numaux
+ 1;
1294 /* See if we already have a definition which matches this
1295 type. We always output the type if it has no elements,
1297 if (mt
->elements
== NULL
)
1298 bfd_release (input_bfd
, (PTR
) mt
);
1301 struct coff_debug_merge_type
*mtl
;
1303 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1305 struct coff_debug_merge_element
*me
, *mel
;
1307 if (mtl
->class != mt
->class)
1310 for (me
= mt
->elements
, mel
= mtl
->elements
;
1311 me
!= NULL
&& mel
!= NULL
;
1312 me
= me
->next
, mel
= mel
->next
)
1314 if (strcmp (me
->name
, mel
->name
) != 0
1315 || me
->type
!= mel
->type
1316 || me
->tagndx
!= mel
->tagndx
)
1320 if (me
== NULL
&& mel
== NULL
)
1324 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1326 /* This is the first definition of this type. */
1327 mt
->indx
= output_index
;
1328 mt
->next
= mh
->types
;
1333 /* This is a redefinition which can be merged. */
1334 bfd_release (input_bfd
, (PTR
) mt
);
1335 *indexp
= mtl
->indx
;
1336 add
= (eslend
- esym
) / isymesz
;
1342 /* We now know whether we are to skip this symbol or not. */
1345 /* Adjust the symbol in order to output it. */
1347 if (isym
._n
._n_n
._n_zeroes
== 0
1348 && isym
._n
._n_n
._n_offset
!= 0)
1353 /* This symbol has a long name. Enter it in the string
1354 table we are building. Note that we do not check
1355 bfd_coff_symname_in_debug. That is only true for
1356 XCOFF, and XCOFF requires different linking code
1358 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1362 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1363 if (indx
== (bfd_size_type
) -1)
1365 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1368 if (isym
.n_scnum
> 0)
1370 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1371 isym
.n_value
+= ((*secpp
)->output_section
->vma
1372 + (*secpp
)->output_offset
1376 /* The value of a C_FILE symbol is the symbol index of the
1377 next C_FILE symbol. The value of the last C_FILE symbol
1378 is the symbol index to the first external symbol
1379 (actually, coff_renumber_symbols does not get this
1380 right--it just sets the value of the last C_FILE symbol
1381 to zero--and nobody has ever complained about it). We
1382 try to get this right, below, just before we write the
1383 symbols out, but in the general case we may have to write
1384 the symbol out twice. */
1385 if (isym
.n_sclass
== C_FILE
)
1387 if (finfo
->last_file_index
!= -1
1388 && finfo
->last_file
.n_value
!= (long) output_index
)
1390 /* We must correct the value of the last C_FILE entry. */
1391 finfo
->last_file
.n_value
= output_index
;
1392 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1394 /* The last C_FILE symbol is in this input file. */
1395 bfd_coff_swap_sym_out (output_bfd
,
1396 (PTR
) &finfo
->last_file
,
1397 (PTR
) (finfo
->outsyms
1398 + ((finfo
->last_file_index
1404 /* We have already written out the last C_FILE
1405 symbol. We need to write it out again. We
1406 borrow *outsym temporarily. */
1407 bfd_coff_swap_sym_out (output_bfd
,
1408 (PTR
) &finfo
->last_file
,
1410 if (bfd_seek (output_bfd
,
1411 (obj_sym_filepos (output_bfd
)
1412 + finfo
->last_file_index
* osymesz
),
1414 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1420 finfo
->last_file_index
= output_index
;
1421 finfo
->last_file
= isym
;
1424 /* Output the symbol. */
1426 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1428 *indexp
= output_index
;
1433 struct coff_link_hash_entry
*h
;
1435 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1437 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1438 BFD_ASSERT (h
!= NULL
);
1439 h
->indx
= output_index
;
1442 output_index
+= add
;
1443 outsym
+= add
* osymesz
;
1446 esym
+= add
* isymesz
;
1450 for (--add
; add
> 0; --add
)
1457 /* Fix up the aux entries. This must be done in a separate pass,
1458 because we don't know the correct symbol indices until we have
1459 already decided which symbols we are going to keep. */
1461 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1462 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1463 isymp
= finfo
->internal_syms
;
1464 indexp
= finfo
->sym_indices
;
1465 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1466 outsym
= finfo
->outsyms
;
1467 while (esym
< esym_end
)
1471 add
= 1 + isymp
->n_numaux
;
1474 || (bfd_size_type
) *indexp
< syment_base
)
1475 && (*sym_hash
== NULL
1476 || (*sym_hash
)->auxbfd
!= input_bfd
))
1477 esym
+= add
* isymesz
;
1480 struct coff_link_hash_entry
*h
;
1487 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1495 /* Handle the aux entries. This handling is based on
1496 coff_pointerize_aux. I don't know if it always correct. */
1497 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1499 union internal_auxent aux
;
1500 union internal_auxent
*auxp
;
1506 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1507 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1512 if (isymp
->n_sclass
== C_FILE
)
1514 /* If this is a long filename, we must put it in the
1516 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1517 && auxp
->x_file
.x_n
.x_offset
!= 0)
1519 const char *filename
;
1522 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1523 >= STRING_SIZE_SIZE
);
1524 if (strings
== NULL
)
1526 strings
= _bfd_coff_read_string_table (input_bfd
);
1527 if (strings
== NULL
)
1530 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1531 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1533 if (indx
== (bfd_size_type
) -1)
1535 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1538 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1542 if (ISFCN (isymp
->n_type
)
1543 || ISTAG (isymp
->n_sclass
)
1544 || isymp
->n_sclass
== C_BLOCK
)
1546 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1548 && indx
< obj_raw_syment_count (input_bfd
))
1550 /* We look forward through the symbol for
1551 the index of the next symbol we are going
1552 to include. I don't know if this is
1554 while (finfo
->sym_indices
[indx
] < 0
1555 && indx
< obj_raw_syment_count (input_bfd
))
1557 if (indx
>= obj_raw_syment_count (input_bfd
))
1558 indx
= output_index
;
1560 indx
= finfo
->sym_indices
[indx
];
1561 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1565 indx
= auxp
->x_sym
.x_tagndx
.l
;
1566 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1570 symindx
= finfo
->sym_indices
[indx
];
1572 auxp
->x_sym
.x_tagndx
.l
= 0;
1574 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1580 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1581 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1595 /* Relocate the line numbers, unless we are stripping them. */
1596 if (finfo
->info
->strip
== strip_none
1597 || finfo
->info
->strip
== strip_some
)
1599 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1605 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1606 build_link_order in ldwrite.c will not have created a
1607 link order, which means that we will not have seen this
1608 input section in _bfd_coff_final_link, which means that
1609 we will not have allocated space for the line numbers of
1610 this section. I don't think line numbers can be
1611 meaningful for a section which does not have
1612 SEC_HAS_CONTENTS set, but, if they do, this must be
1614 if (o
->lineno_count
== 0
1615 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1618 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1619 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1620 input_bfd
) != linesz
* o
->lineno_count
)
1623 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1624 eline
= finfo
->linenos
;
1625 elineend
= eline
+ linesz
* o
->lineno_count
;
1626 for (; eline
< elineend
; eline
+= linesz
)
1628 struct internal_lineno iline
;
1630 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1632 if (iline
.l_lnno
!= 0)
1633 iline
.l_addr
.l_paddr
+= offset
;
1634 else if (iline
.l_addr
.l_symndx
>= 0
1635 && ((unsigned long) iline
.l_addr
.l_symndx
1636 < obj_raw_syment_count (input_bfd
)))
1640 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1644 /* These line numbers are attached to a symbol
1645 which we are stripping. We should really
1646 just discard the line numbers, but that would
1647 be a pain because we have already counted
1653 struct internal_syment is
;
1654 union internal_auxent ia
;
1656 /* Fix up the lnnoptr field in the aux entry of
1657 the symbol. It turns out that we can't do
1658 this when we modify the symbol aux entries,
1659 because gas sometimes screws up the lnnoptr
1660 field and makes it an offset from the start
1661 of the line numbers rather than an absolute
1663 bfd_coff_swap_sym_in (output_bfd
,
1664 (PTR
) (finfo
->outsyms
1665 + ((indx
- syment_base
)
1668 if ((ISFCN (is
.n_type
)
1669 || is
.n_sclass
== C_BLOCK
)
1670 && is
.n_numaux
>= 1)
1674 auxptr
= (PTR
) (finfo
->outsyms
1675 + ((indx
- syment_base
+ 1)
1677 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1678 is
.n_type
, is
.n_sclass
,
1679 0, is
.n_numaux
, (PTR
) &ia
);
1680 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1681 (o
->output_section
->line_filepos
1682 + o
->output_section
->lineno_count
* linesz
1683 + eline
- finfo
->linenos
);
1684 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1685 is
.n_type
, is
.n_sclass
, 0,
1686 is
.n_numaux
, auxptr
);
1690 iline
.l_addr
.l_symndx
= indx
;
1693 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1696 if (bfd_seek (output_bfd
,
1697 (o
->output_section
->line_filepos
1698 + o
->output_section
->lineno_count
* linesz
),
1700 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1701 output_bfd
) != linesz
* o
->lineno_count
)
1704 o
->output_section
->lineno_count
+= o
->lineno_count
;
1708 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1709 symbol will be the first symbol in the next input file. In the
1710 normal case, this will save us from writing out the C_FILE symbol
1712 if (finfo
->last_file_index
!= -1
1713 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1715 finfo
->last_file
.n_value
= output_index
;
1716 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1717 (PTR
) (finfo
->outsyms
1718 + ((finfo
->last_file_index
- syment_base
)
1722 /* Write the modified symbols to the output file. */
1723 if (outsym
> finfo
->outsyms
)
1725 if (bfd_seek (output_bfd
,
1726 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1728 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1730 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1733 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1734 + (outsym
- finfo
->outsyms
) / osymesz
)
1737 obj_raw_syment_count (output_bfd
) = output_index
;
1740 /* Relocate the contents of each section. */
1741 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1742 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1746 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1748 if ((o
->flags
& SEC_RELOC
) != 0
1749 && o
->reloc_count
!= 0)
1751 ((*_bfd_error_handler
)
1752 ("%s: relocs in section `%s', but it has no contents",
1753 bfd_get_filename (input_bfd
),
1754 bfd_get_section_name (input_bfd
, o
)));
1755 bfd_set_error (bfd_error_no_contents
);
1762 if (coff_section_data (input_bfd
, o
) != NULL
1763 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1764 contents
= coff_section_data (input_bfd
, o
)->contents
;
1767 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1768 (file_ptr
) 0, o
->_raw_size
))
1770 contents
= finfo
->contents
;
1773 if ((o
->flags
& SEC_RELOC
) != 0)
1776 struct internal_reloc
*internal_relocs
;
1777 struct internal_reloc
*irel
;
1779 /* Read in the relocs. */
1780 target_index
= o
->output_section
->target_index
;
1781 internal_relocs
= (_bfd_coff_read_internal_relocs
1782 (input_bfd
, o
, false, finfo
->external_relocs
,
1783 finfo
->info
->relocateable
,
1784 (finfo
->info
->relocateable
1785 ? (finfo
->section_info
[target_index
].relocs
1786 + o
->output_section
->reloc_count
)
1787 : finfo
->internal_relocs
)));
1788 if (internal_relocs
== NULL
)
1791 /* Call processor specific code to relocate the section
1793 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1797 finfo
->internal_syms
,
1801 if (finfo
->info
->relocateable
)
1804 struct internal_reloc
*irelend
;
1805 struct coff_link_hash_entry
**rel_hash
;
1807 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1808 irel
= internal_relocs
;
1809 irelend
= irel
+ o
->reloc_count
;
1810 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1811 + o
->output_section
->reloc_count
);
1812 for (; irel
< irelend
; irel
++, rel_hash
++)
1814 struct coff_link_hash_entry
*h
;
1819 /* Adjust the reloc address and symbol index. */
1821 irel
->r_vaddr
+= offset
;
1823 if (irel
->r_symndx
== -1)
1828 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1836 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1839 /* This is a global symbol. */
1841 irel
->r_symndx
= h
->indx
;
1844 /* This symbol is being written at the end
1845 of the file, and we do not yet know the
1846 symbol index. We save the pointer to the
1847 hash table entry in the rel_hash list.
1848 We set the indx field to -2 to indicate
1849 that this symbol must not be stripped. */
1858 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1860 irel
->r_symndx
= indx
;
1863 struct internal_syment
*is
;
1865 char buf
[SYMNMLEN
+ 1];
1867 /* This reloc is against a symbol we are
1868 stripping. It would be possible to
1869 handle this case, but I don't think it's
1871 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1873 name
= (_bfd_coff_internal_syment_name
1874 (input_bfd
, is
, buf
));
1878 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1879 (finfo
->info
, name
, input_bfd
, o
,
1886 o
->output_section
->reloc_count
+= o
->reloc_count
;
1890 /* Write out the modified section contents. */
1891 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1892 contents
, o
->output_offset
,
1893 (o
->_cooked_size
!= 0
1899 if (! finfo
->info
->keep_memory
)
1901 if (! _bfd_coff_free_symbols (input_bfd
))
1908 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1911 _bfd_coff_write_global_sym (h
, data
)
1912 struct coff_link_hash_entry
*h
;
1915 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1917 struct internal_syment isym
;
1918 bfd_size_type symesz
;
1921 output_bfd
= finfo
->output_bfd
;
1927 && (finfo
->info
->strip
== strip_all
1928 || (finfo
->info
->strip
== strip_some
1929 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1930 h
->root
.root
.string
, false, false)
1934 switch (h
->root
.type
)
1937 case bfd_link_hash_new
:
1941 case bfd_link_hash_undefined
:
1942 case bfd_link_hash_undefweak
:
1943 isym
.n_scnum
= N_UNDEF
;
1947 case bfd_link_hash_defined
:
1948 case bfd_link_hash_defweak
:
1952 sec
= h
->root
.u
.def
.section
->output_section
;
1953 if (bfd_is_abs_section (sec
))
1954 isym
.n_scnum
= N_ABS
;
1956 isym
.n_scnum
= sec
->target_index
;
1957 isym
.n_value
= (h
->root
.u
.def
.value
1959 + h
->root
.u
.def
.section
->output_offset
);
1963 case bfd_link_hash_common
:
1964 isym
.n_scnum
= N_UNDEF
;
1965 isym
.n_value
= h
->root
.u
.c
.size
;
1968 case bfd_link_hash_indirect
:
1969 case bfd_link_hash_warning
:
1970 /* Just ignore these. They can't be handled anyhow. */
1974 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1975 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1982 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1984 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1986 if (indx
== (bfd_size_type
) -1)
1988 finfo
->failed
= true;
1991 isym
._n
._n_n
._n_zeroes
= 0;
1992 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1995 isym
.n_sclass
= h
->class;
1996 isym
.n_type
= h
->type
;
1998 if (isym
.n_sclass
== C_NULL
)
1999 isym
.n_sclass
= C_EXT
;
2001 isym
.n_numaux
= h
->numaux
;
2003 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2005 symesz
= bfd_coff_symesz (output_bfd
);
2007 if (bfd_seek (output_bfd
,
2008 (obj_sym_filepos (output_bfd
)
2009 + obj_raw_syment_count (output_bfd
) * symesz
),
2011 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2013 finfo
->failed
= true;
2017 h
->indx
= obj_raw_syment_count (output_bfd
);
2019 ++obj_raw_syment_count (output_bfd
);
2021 /* Write out any associated aux entries. There normally will be
2022 none. If there are any, I have no idea how to modify them. */
2023 for (i
= 0; i
< isym
.n_numaux
; i
++)
2025 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2026 isym
.n_sclass
, i
, isym
.n_numaux
,
2027 (PTR
) finfo
->outsyms
);
2028 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2030 finfo
->failed
= true;
2033 ++obj_raw_syment_count (output_bfd
);
2039 /* Handle a link order which is supposed to generate a reloc. */
2042 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2044 struct coff_final_link_info
*finfo
;
2045 asection
*output_section
;
2046 struct bfd_link_order
*link_order
;
2048 reloc_howto_type
*howto
;
2049 struct internal_reloc
*irel
;
2050 struct coff_link_hash_entry
**rel_hash_ptr
;
2052 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2055 bfd_set_error (bfd_error_bad_value
);
2059 if (link_order
->u
.reloc
.p
->addend
!= 0)
2063 bfd_reloc_status_type rstat
;
2066 size
= bfd_get_reloc_size (howto
);
2067 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2071 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2072 link_order
->u
.reloc
.p
->addend
, buf
);
2078 case bfd_reloc_outofrange
:
2080 case bfd_reloc_overflow
:
2081 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2083 (link_order
->type
== bfd_section_reloc_link_order
2084 ? bfd_section_name (output_bfd
,
2085 link_order
->u
.reloc
.p
->u
.section
)
2086 : link_order
->u
.reloc
.p
->u
.name
),
2087 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2088 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2095 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2096 (file_ptr
) link_order
->offset
, size
);
2102 /* Store the reloc information in the right place. It will get
2103 swapped and written out at the end of the final_link routine. */
2105 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2106 + output_section
->reloc_count
);
2107 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2108 + output_section
->reloc_count
);
2110 memset (irel
, 0, sizeof (struct internal_reloc
));
2111 *rel_hash_ptr
= NULL
;
2113 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2115 if (link_order
->type
== bfd_section_reloc_link_order
)
2117 /* We need to somehow locate a symbol in the right section. The
2118 symbol must either have a value of zero, or we must adjust
2119 the addend by the value of the symbol. FIXME: Write this
2120 when we need it. The old linker couldn't handle this anyhow. */
2122 *rel_hash_ptr
= NULL
;
2127 struct coff_link_hash_entry
*h
;
2129 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2130 link_order
->u
.reloc
.p
->u
.name
,
2131 false, false, true);
2135 irel
->r_symndx
= h
->indx
;
2138 /* Set the index to -2 to force this symbol to get
2147 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2148 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2149 (asection
*) NULL
, (bfd_vma
) 0)))
2155 /* FIXME: Is this always right? */
2156 irel
->r_type
= howto
->type
;
2158 /* r_size is only used on the RS/6000, which needs its own linker
2159 routines anyhow. r_extern is only used for ECOFF. */
2161 /* FIXME: What is the right value for r_offset? Is zero OK? */
2163 ++output_section
->reloc_count
;
2168 /* A basic reloc handling routine which may be used by processors with
2172 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2173 input_section
, contents
, relocs
, syms
,
2176 struct bfd_link_info
*info
;
2178 asection
*input_section
;
2180 struct internal_reloc
*relocs
;
2181 struct internal_syment
*syms
;
2182 asection
**sections
;
2184 struct internal_reloc
*rel
;
2185 struct internal_reloc
*relend
;
2188 relend
= rel
+ input_section
->reloc_count
;
2189 for (; rel
< relend
; rel
++)
2192 struct coff_link_hash_entry
*h
;
2193 struct internal_syment
*sym
;
2196 reloc_howto_type
*howto
;
2197 bfd_reloc_status_type rstat
;
2199 symndx
= rel
->r_symndx
;
2208 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2209 sym
= syms
+ symndx
;
2212 /* COFF treats common symbols in one of two ways. Either the
2213 size of the symbol is included in the section contents, or it
2214 is not. We assume that the size is not included, and force
2215 the rtype_to_howto function to adjust the addend as needed. */
2217 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2218 addend
= - sym
->n_value
;
2223 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2236 sec
= bfd_abs_section_ptr
;
2241 sec
= sections
[symndx
];
2242 val
= (sec
->output_section
->vma
2243 + sec
->output_offset
2250 if (h
->root
.type
== bfd_link_hash_defined
2251 || h
->root
.type
== bfd_link_hash_defweak
)
2255 sec
= h
->root
.u
.def
.section
;
2256 val
= (h
->root
.u
.def
.value
2257 + sec
->output_section
->vma
2258 + sec
->output_offset
);
2261 else if (! info
->relocateable
)
2263 if (! ((*info
->callbacks
->undefined_symbol
)
2264 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2265 rel
->r_vaddr
- input_section
->vma
)))
2270 if (info
->base_file
)
2272 /* Emit a reloc if the backend thinks it needs it. */
2273 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2275 /* relocation to a symbol in a section which
2276 isn't absolute - we output the address here
2278 bfd_vma addr
= rel
->r_vaddr
2279 - input_section
->vma
2280 + input_section
->output_offset
2281 + input_section
->output_section
->vma
;
2282 if (coff_data(output_bfd
)->pe
)
2283 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2284 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2288 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2290 rel
->r_vaddr
- input_section
->vma
,
2299 case bfd_reloc_overflow
:
2302 char buf
[SYMNMLEN
+ 1];
2307 name
= h
->root
.root
.string
;
2310 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2315 if (! ((*info
->callbacks
->reloc_overflow
)
2316 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2317 input_section
, rel
->r_vaddr
- input_section
->vma
)))