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
)
488 if (p
->type
== bfd_indirect_link_order
)
492 sec
= p
->u
.indirect
.section
;
494 /* Mark all sections which are to be included in the
495 link. This will normally be every section. We need
496 to do this so that we can identify any sections which
497 the linker has decided to not include. */
498 sec
->linker_mark
= true;
500 if (info
->strip
== strip_none
501 || info
->strip
== strip_some
)
502 o
->lineno_count
+= sec
->lineno_count
;
504 if (info
->relocateable
)
505 o
->reloc_count
+= sec
->reloc_count
;
507 if (sec
->_raw_size
> max_contents_size
)
508 max_contents_size
= sec
->_raw_size
;
509 if (sec
->lineno_count
> max_lineno_count
)
510 max_lineno_count
= sec
->lineno_count
;
511 if (sec
->reloc_count
> max_reloc_count
)
512 max_reloc_count
= sec
->reloc_count
;
514 else if (info
->relocateable
515 && (p
->type
== bfd_section_reloc_link_order
516 || p
->type
== bfd_symbol_reloc_link_order
))
519 if (o
->reloc_count
== 0)
523 o
->flags
|= SEC_RELOC
;
524 o
->rel_filepos
= rel_filepos
;
525 rel_filepos
+= o
->reloc_count
* relsz
;
529 /* If doing a relocateable link, allocate space for the pointers we
531 if (info
->relocateable
)
535 /* We use section_count + 1, rather than section_count, because
536 the target_index fields are 1 based. */
538 ((struct coff_link_section_info
*)
539 bfd_malloc ((abfd
->section_count
+ 1)
540 * sizeof (struct coff_link_section_info
)));
541 if (finfo
.section_info
== NULL
)
543 for (i
= 0; i
<= abfd
->section_count
; i
++)
545 finfo
.section_info
[i
].relocs
= NULL
;
546 finfo
.section_info
[i
].rel_hashes
= NULL
;
550 /* We now know the size of the relocs, so we can determine the file
551 positions of the line numbers. */
552 line_filepos
= rel_filepos
;
553 linesz
= bfd_coff_linesz (abfd
);
554 max_output_reloc_count
= 0;
555 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
557 if (o
->lineno_count
== 0)
561 o
->line_filepos
= line_filepos
;
562 line_filepos
+= o
->lineno_count
* linesz
;
565 if (o
->reloc_count
!= 0)
567 /* We don't know the indices of global symbols until we have
568 written out all the local symbols. For each section in
569 the output file, we keep an array of pointers to hash
570 table entries. Each entry in the array corresponds to a
571 reloc. When we find a reloc against a global symbol, we
572 set the corresponding entry in this array so that we can
573 fix up the symbol index after we have written out all the
576 Because of this problem, we also keep the relocs in
577 memory until the end of the link. This wastes memory,
578 but only when doing a relocateable link, which is not the
580 BFD_ASSERT (info
->relocateable
);
581 finfo
.section_info
[o
->target_index
].relocs
=
582 ((struct internal_reloc
*)
583 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
584 finfo
.section_info
[o
->target_index
].rel_hashes
=
585 ((struct coff_link_hash_entry
**)
586 bfd_malloc (o
->reloc_count
587 * sizeof (struct coff_link_hash_entry
*)));
588 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
589 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
592 if (o
->reloc_count
> max_output_reloc_count
)
593 max_output_reloc_count
= o
->reloc_count
;
596 /* Reset the reloc and lineno counts, so that we can use them to
597 count the number of entries we have output so far. */
602 obj_sym_filepos (abfd
) = line_filepos
;
604 /* Figure out the largest number of symbols in an input BFD. Take
605 the opportunity to clear the output_has_begun fields of all the
608 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
612 sub
->output_has_begun
= false;
613 sz
= obj_raw_syment_count (sub
);
614 if (sz
> max_sym_count
)
618 /* Allocate some buffers used while linking. */
619 finfo
.internal_syms
= ((struct internal_syment
*)
620 bfd_malloc (max_sym_count
621 * sizeof (struct internal_syment
)));
622 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
623 * sizeof (asection
*));
624 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
625 finfo
.outsyms
= ((bfd_byte
*)
626 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
627 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
628 * bfd_coff_linesz (abfd
));
629 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
630 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
631 if (! info
->relocateable
)
632 finfo
.internal_relocs
= ((struct internal_reloc
*)
633 bfd_malloc (max_reloc_count
634 * sizeof (struct internal_reloc
)));
635 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
636 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
637 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
638 || finfo
.outsyms
== NULL
639 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
640 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
641 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
642 || (! info
->relocateable
643 && finfo
.internal_relocs
== NULL
644 && max_reloc_count
> 0))
647 /* We now know the position of everything in the file, except that
648 we don't know the size of the symbol table and therefore we don't
649 know where the string table starts. We just build the string
650 table in memory as we go along. We process all the relocations
651 for a single input file at once. */
652 obj_raw_syment_count (abfd
) = 0;
654 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
656 if (! bfd_coff_start_final_link (abfd
, info
))
660 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
662 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
664 if (p
->type
== bfd_indirect_link_order
665 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
666 == bfd_target_coff_flavour
))
668 sub
= p
->u
.indirect
.section
->owner
;
669 if (! sub
->output_has_begun
)
671 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
673 sub
->output_has_begun
= true;
676 else if (p
->type
== bfd_section_reloc_link_order
677 || p
->type
== bfd_symbol_reloc_link_order
)
679 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
684 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
690 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
692 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
693 debug_merge_allocated
= false;
695 if (finfo
.internal_syms
!= NULL
)
697 free (finfo
.internal_syms
);
698 finfo
.internal_syms
= NULL
;
700 if (finfo
.sec_ptrs
!= NULL
)
702 free (finfo
.sec_ptrs
);
703 finfo
.sec_ptrs
= NULL
;
705 if (finfo
.sym_indices
!= NULL
)
707 free (finfo
.sym_indices
);
708 finfo
.sym_indices
= NULL
;
710 if (finfo
.linenos
!= NULL
)
712 free (finfo
.linenos
);
713 finfo
.linenos
= NULL
;
715 if (finfo
.contents
!= NULL
)
717 free (finfo
.contents
);
718 finfo
.contents
= NULL
;
720 if (finfo
.external_relocs
!= NULL
)
722 free (finfo
.external_relocs
);
723 finfo
.external_relocs
= NULL
;
725 if (finfo
.internal_relocs
!= NULL
)
727 free (finfo
.internal_relocs
);
728 finfo
.internal_relocs
= NULL
;
731 /* The value of the last C_FILE symbol is supposed to be the symbol
732 index of the first external symbol. Write it out again if
734 if (finfo
.last_file_index
!= -1
735 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
737 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
738 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
739 (PTR
) finfo
.outsyms
);
741 (obj_sym_filepos (abfd
)
742 + finfo
.last_file_index
* symesz
),
744 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
748 /* Write out the global symbols. */
749 finfo
.failed
= false;
750 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
755 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
756 if (finfo
.outsyms
!= NULL
)
758 free (finfo
.outsyms
);
759 finfo
.outsyms
= NULL
;
762 if (info
->relocateable
)
764 /* Now that we have written out all the global symbols, we know
765 the symbol indices to use for relocs against them, and we can
766 finally write out the relocs. */
767 external_relocs
= ((bfd_byte
*)
768 bfd_malloc (max_output_reloc_count
* relsz
));
769 if (external_relocs
== NULL
)
772 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
774 struct internal_reloc
*irel
;
775 struct internal_reloc
*irelend
;
776 struct coff_link_hash_entry
**rel_hash
;
779 if (o
->reloc_count
== 0)
782 irel
= finfo
.section_info
[o
->target_index
].relocs
;
783 irelend
= irel
+ o
->reloc_count
;
784 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
785 erel
= external_relocs
;
786 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
788 if (*rel_hash
!= NULL
)
790 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
791 irel
->r_symndx
= (*rel_hash
)->indx
;
793 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
796 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
797 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
798 abfd
) != relsz
* o
->reloc_count
)
802 free (external_relocs
);
803 external_relocs
= NULL
;
806 /* Free up the section information. */
807 if (finfo
.section_info
!= NULL
)
811 for (i
= 0; i
< abfd
->section_count
; i
++)
813 if (finfo
.section_info
[i
].relocs
!= NULL
)
814 free (finfo
.section_info
[i
].relocs
);
815 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
816 free (finfo
.section_info
[i
].rel_hashes
);
818 free (finfo
.section_info
);
819 finfo
.section_info
= NULL
;
822 /* Write out the string table. */
823 if (obj_raw_syment_count (abfd
) != 0)
826 (obj_sym_filepos (abfd
)
827 + obj_raw_syment_count (abfd
) * symesz
),
831 #if STRING_SIZE_SIZE == 4
833 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
834 (bfd_byte
*) strbuf
);
836 #error Change bfd_h_put_32
839 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
842 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
846 _bfd_stringtab_free (finfo
.strtab
);
848 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
849 not try to write out the symbols. */
850 bfd_get_symcount (abfd
) = 0;
855 if (debug_merge_allocated
)
856 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
857 if (finfo
.strtab
!= NULL
)
858 _bfd_stringtab_free (finfo
.strtab
);
859 if (finfo
.section_info
!= NULL
)
863 for (i
= 0; i
< abfd
->section_count
; i
++)
865 if (finfo
.section_info
[i
].relocs
!= NULL
)
866 free (finfo
.section_info
[i
].relocs
);
867 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
868 free (finfo
.section_info
[i
].rel_hashes
);
870 free (finfo
.section_info
);
872 if (finfo
.internal_syms
!= NULL
)
873 free (finfo
.internal_syms
);
874 if (finfo
.sec_ptrs
!= NULL
)
875 free (finfo
.sec_ptrs
);
876 if (finfo
.sym_indices
!= NULL
)
877 free (finfo
.sym_indices
);
878 if (finfo
.outsyms
!= NULL
)
879 free (finfo
.outsyms
);
880 if (finfo
.linenos
!= NULL
)
881 free (finfo
.linenos
);
882 if (finfo
.contents
!= NULL
)
883 free (finfo
.contents
);
884 if (finfo
.external_relocs
!= NULL
)
885 free (finfo
.external_relocs
);
886 if (finfo
.internal_relocs
!= NULL
)
887 free (finfo
.internal_relocs
);
888 if (external_relocs
!= NULL
)
889 free (external_relocs
);
893 /* parse out a -heap <reserved>,<commit> line */
896 dores_com (ptr
, output_bfd
, heap
)
901 if (coff_data(output_bfd
)->pe
)
903 int val
= strtoul (ptr
, &ptr
, 0);
905 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
907 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
911 int val
= strtoul (ptr
+1, &ptr
, 0);
913 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
915 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
921 static char *get_name(ptr
, dst
)
928 while (*ptr
&& *ptr
!= ' ')
934 /* Process any magic embedded commands in a section called .drectve */
937 process_embedded_commands (output_bfd
, info
, abfd
)
939 struct bfd_link_info
*info
;
942 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
949 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
952 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
957 e
= copy
+ sec
->_raw_size
;
958 for (s
= copy
; s
< e
; )
964 if (strncmp (s
,"-attr", 5) == 0)
976 s
= get_name(s
, &name
);
977 s
= get_name(s
, &attribs
);
997 asec
= bfd_get_section_by_name (abfd
, name
);
1000 asec
->flags
|= SEC_CODE
;
1002 asec
->flags
|= SEC_READONLY
;
1005 else if (strncmp (s
,"-heap", 5) == 0)
1007 s
= dores_com (s
+5, output_bfd
, 1);
1009 else if (strncmp (s
,"-stack", 6) == 0)
1011 s
= dores_com (s
+6, output_bfd
, 0);
1020 /* Link an input file into the linker output file. This function
1021 handles all the sections and relocations of the input file at once. */
1024 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1025 struct coff_final_link_info
*finfo
;
1028 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1029 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1030 asection
*, struct internal_reloc
*,
1033 const char *strings
;
1034 bfd_size_type syment_base
;
1035 unsigned int n_tmask
;
1036 unsigned int n_btshft
;
1038 bfd_size_type isymesz
;
1039 bfd_size_type osymesz
;
1040 bfd_size_type linesz
;
1043 struct internal_syment
*isymp
;
1046 unsigned long output_index
;
1048 struct coff_link_hash_entry
**sym_hash
;
1051 /* Move all the symbols to the output file. */
1053 output_bfd
= finfo
->output_bfd
;
1054 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1056 syment_base
= obj_raw_syment_count (output_bfd
);
1057 isymesz
= bfd_coff_symesz (input_bfd
);
1058 osymesz
= bfd_coff_symesz (output_bfd
);
1059 linesz
= bfd_coff_linesz (input_bfd
);
1060 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1062 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1063 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1065 /* Define macros so that ISFCN, et. al., macros work correctly. */
1066 #define N_TMASK n_tmask
1067 #define N_BTSHFT n_btshft
1070 if (! finfo
->info
->keep_memory
)
1073 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1076 if (! _bfd_coff_get_external_symbols (input_bfd
))
1079 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1080 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1081 isymp
= finfo
->internal_syms
;
1082 secpp
= finfo
->sec_ptrs
;
1083 indexp
= finfo
->sym_indices
;
1084 output_index
= syment_base
;
1085 outsym
= finfo
->outsyms
;
1087 if (coff_data (output_bfd
)->pe
)
1089 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1093 while (esym
< esym_end
)
1095 struct internal_syment isym
;
1100 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1102 /* Make a copy of *isymp so that the relocate_section function
1103 always sees the original values. This is more reliable than
1104 always recomputing the symbol value even if we are stripping
1108 if (isym
.n_scnum
!= 0)
1109 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1112 if (isym
.n_value
== 0)
1113 *secpp
= bfd_und_section_ptr
;
1115 *secpp
= bfd_com_section_ptr
;
1122 add
= 1 + isym
.n_numaux
;
1124 /* If we are stripping all symbols, we want to skip this one. */
1125 if (finfo
->info
->strip
== strip_all
)
1130 if (isym
.n_sclass
== C_EXT
1131 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1133 /* This is a global symbol. Global symbols come at the
1134 end of the symbol table, so skip them for now.
1135 Function symbols, however, are an exception, and are
1136 not moved to the end. */
1138 if (! ISFCN (isym
.n_type
))
1143 /* This is a local symbol. Skip it if we are discarding
1145 if (finfo
->info
->discard
== discard_all
)
1150 /* If we stripping debugging symbols, and this is a debugging
1151 symbol, then skip it. */
1153 && finfo
->info
->strip
== strip_debugger
1154 && isym
.n_scnum
== N_DEBUG
)
1157 /* If some symbols are stripped based on the name, work out the
1158 name and decide whether to skip this symbol. */
1160 && (finfo
->info
->strip
== strip_some
1161 || finfo
->info
->discard
== discard_l
))
1164 char buf
[SYMNMLEN
+ 1];
1166 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1170 if ((finfo
->info
->strip
== strip_some
1171 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1174 && finfo
->info
->discard
== discard_l
1175 && strncmp (name
, finfo
->info
->lprefix
,
1176 finfo
->info
->lprefix_len
) == 0))
1180 /* If this is an enum, struct, or union tag, see if we have
1181 already output an identical type. */
1183 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1184 && (isym
.n_sclass
== C_ENTAG
1185 || isym
.n_sclass
== C_STRTAG
1186 || isym
.n_sclass
== C_UNTAG
)
1187 && isym
.n_numaux
== 1)
1190 char buf
[SYMNMLEN
+ 1];
1191 struct coff_debug_merge_hash_entry
*mh
;
1192 struct coff_debug_merge_type
*mt
;
1193 union internal_auxent aux
;
1194 struct coff_debug_merge_element
**epp
;
1195 bfd_byte
*esl
, *eslend
;
1196 struct internal_syment
*islp
;
1198 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1202 /* Ignore fake names invented by compiler; treat them all as
1204 if (*name
== '~' || *name
== '.' || *name
== '$'
1205 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1206 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1209 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1214 /* Allocate memory to hold type information. If this turns
1215 out to be a duplicate, we pass this address to
1217 mt
= ((struct coff_debug_merge_type
*)
1218 bfd_alloc (input_bfd
,
1219 sizeof (struct coff_debug_merge_type
)));
1222 mt
->class = isym
.n_sclass
;
1224 /* Pick up the aux entry, which points to the end of the tag
1226 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1227 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1230 /* Gather the elements. */
1231 epp
= &mt
->elements
;
1232 mt
->elements
= NULL
;
1234 esl
= esym
+ 2 * isymesz
;
1235 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1236 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1237 while (esl
< eslend
)
1239 const char *elename
;
1240 char elebuf
[SYMNMLEN
+ 1];
1243 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1245 *epp
= ((struct coff_debug_merge_element
*)
1246 bfd_alloc (input_bfd
,
1247 sizeof (struct coff_debug_merge_element
)));
1251 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1253 if (elename
== NULL
)
1256 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1259 strcpy (copy
, elename
);
1261 (*epp
)->name
= copy
;
1262 (*epp
)->type
= islp
->n_type
;
1264 if (islp
->n_numaux
>= 1
1265 && islp
->n_type
!= T_NULL
1266 && islp
->n_sclass
!= C_EOS
)
1268 union internal_auxent eleaux
;
1271 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1272 islp
->n_type
, islp
->n_sclass
, 0,
1273 islp
->n_numaux
, (PTR
) &eleaux
);
1274 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1276 /* FIXME: If this tagndx entry refers to a symbol
1277 defined later in this file, we just ignore it.
1278 Handling this correctly would be tedious, and may
1284 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1287 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1288 if ((*epp
)->tagndx
< 0)
1292 epp
= &(*epp
)->next
;
1295 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1296 islp
+= islp
->n_numaux
+ 1;
1299 /* See if we already have a definition which matches this
1300 type. We always output the type if it has no elements,
1302 if (mt
->elements
== NULL
)
1303 bfd_release (input_bfd
, (PTR
) mt
);
1306 struct coff_debug_merge_type
*mtl
;
1308 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1310 struct coff_debug_merge_element
*me
, *mel
;
1312 if (mtl
->class != mt
->class)
1315 for (me
= mt
->elements
, mel
= mtl
->elements
;
1316 me
!= NULL
&& mel
!= NULL
;
1317 me
= me
->next
, mel
= mel
->next
)
1319 if (strcmp (me
->name
, mel
->name
) != 0
1320 || me
->type
!= mel
->type
1321 || me
->tagndx
!= mel
->tagndx
)
1325 if (me
== NULL
&& mel
== NULL
)
1329 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1331 /* This is the first definition of this type. */
1332 mt
->indx
= output_index
;
1333 mt
->next
= mh
->types
;
1338 /* This is a redefinition which can be merged. */
1339 bfd_release (input_bfd
, (PTR
) mt
);
1340 *indexp
= mtl
->indx
;
1341 add
= (eslend
- esym
) / isymesz
;
1347 /* We now know whether we are to skip this symbol or not. */
1350 /* Adjust the symbol in order to output it. */
1352 if (isym
._n
._n_n
._n_zeroes
== 0
1353 && isym
._n
._n_n
._n_offset
!= 0)
1358 /* This symbol has a long name. Enter it in the string
1359 table we are building. Note that we do not check
1360 bfd_coff_symname_in_debug. That is only true for
1361 XCOFF, and XCOFF requires different linking code
1363 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1367 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1368 if (indx
== (bfd_size_type
) -1)
1370 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1373 if (isym
.n_scnum
> 0)
1375 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1376 isym
.n_value
+= ((*secpp
)->output_section
->vma
1377 + (*secpp
)->output_offset
1381 /* The value of a C_FILE symbol is the symbol index of the
1382 next C_FILE symbol. The value of the last C_FILE symbol
1383 is the symbol index to the first external symbol
1384 (actually, coff_renumber_symbols does not get this
1385 right--it just sets the value of the last C_FILE symbol
1386 to zero--and nobody has ever complained about it). We
1387 try to get this right, below, just before we write the
1388 symbols out, but in the general case we may have to write
1389 the symbol out twice. */
1390 if (isym
.n_sclass
== C_FILE
)
1392 if (finfo
->last_file_index
!= -1
1393 && finfo
->last_file
.n_value
!= (long) output_index
)
1395 /* We must correct the value of the last C_FILE entry. */
1396 finfo
->last_file
.n_value
= output_index
;
1397 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1399 /* The last C_FILE symbol is in this input file. */
1400 bfd_coff_swap_sym_out (output_bfd
,
1401 (PTR
) &finfo
->last_file
,
1402 (PTR
) (finfo
->outsyms
1403 + ((finfo
->last_file_index
1409 /* We have already written out the last C_FILE
1410 symbol. We need to write it out again. We
1411 borrow *outsym temporarily. */
1412 bfd_coff_swap_sym_out (output_bfd
,
1413 (PTR
) &finfo
->last_file
,
1415 if (bfd_seek (output_bfd
,
1416 (obj_sym_filepos (output_bfd
)
1417 + finfo
->last_file_index
* osymesz
),
1419 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1425 finfo
->last_file_index
= output_index
;
1426 finfo
->last_file
= isym
;
1429 /* Output the symbol. */
1431 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1433 *indexp
= output_index
;
1438 struct coff_link_hash_entry
*h
;
1440 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1442 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1443 BFD_ASSERT (h
!= NULL
);
1444 h
->indx
= output_index
;
1447 output_index
+= add
;
1448 outsym
+= add
* osymesz
;
1451 esym
+= add
* isymesz
;
1455 for (--add
; add
> 0; --add
)
1462 /* Fix up the aux entries. This must be done in a separate pass,
1463 because we don't know the correct symbol indices until we have
1464 already decided which symbols we are going to keep. */
1466 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1467 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1468 isymp
= finfo
->internal_syms
;
1469 indexp
= finfo
->sym_indices
;
1470 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1471 outsym
= finfo
->outsyms
;
1472 while (esym
< esym_end
)
1476 add
= 1 + isymp
->n_numaux
;
1479 || (bfd_size_type
) *indexp
< syment_base
)
1480 && (*sym_hash
== NULL
1481 || (*sym_hash
)->auxbfd
!= input_bfd
))
1482 esym
+= add
* isymesz
;
1485 struct coff_link_hash_entry
*h
;
1492 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1500 /* Handle the aux entries. This handling is based on
1501 coff_pointerize_aux. I don't know if it always correct. */
1502 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1504 union internal_auxent aux
;
1505 union internal_auxent
*auxp
;
1511 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1512 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1517 if (isymp
->n_sclass
== C_FILE
)
1519 /* If this is a long filename, we must put it in the
1521 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1522 && auxp
->x_file
.x_n
.x_offset
!= 0)
1524 const char *filename
;
1527 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1528 >= STRING_SIZE_SIZE
);
1529 if (strings
== NULL
)
1531 strings
= _bfd_coff_read_string_table (input_bfd
);
1532 if (strings
== NULL
)
1535 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1536 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1538 if (indx
== (bfd_size_type
) -1)
1540 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1543 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1547 if (ISFCN (isymp
->n_type
)
1548 || ISTAG (isymp
->n_sclass
)
1549 || isymp
->n_sclass
== C_BLOCK
)
1551 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1553 && indx
< obj_raw_syment_count (input_bfd
))
1555 /* We look forward through the symbol for
1556 the index of the next symbol we are going
1557 to include. I don't know if this is
1559 while ((finfo
->sym_indices
[indx
] < 0
1560 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1562 && indx
< obj_raw_syment_count (input_bfd
))
1564 if (indx
>= obj_raw_syment_count (input_bfd
))
1565 indx
= output_index
;
1567 indx
= finfo
->sym_indices
[indx
];
1568 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1572 indx
= auxp
->x_sym
.x_tagndx
.l
;
1573 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1577 symindx
= finfo
->sym_indices
[indx
];
1579 auxp
->x_sym
.x_tagndx
.l
= 0;
1581 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1587 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1588 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1602 /* Relocate the line numbers, unless we are stripping them. */
1603 if (finfo
->info
->strip
== strip_none
1604 || finfo
->info
->strip
== strip_some
)
1606 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1612 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1613 build_link_order in ldwrite.c will not have created a
1614 link order, which means that we will not have seen this
1615 input section in _bfd_coff_final_link, which means that
1616 we will not have allocated space for the line numbers of
1617 this section. I don't think line numbers can be
1618 meaningful for a section which does not have
1619 SEC_HAS_CONTENTS set, but, if they do, this must be
1621 if (o
->lineno_count
== 0
1622 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1625 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1626 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1627 input_bfd
) != linesz
* o
->lineno_count
)
1630 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1631 eline
= finfo
->linenos
;
1632 elineend
= eline
+ linesz
* o
->lineno_count
;
1633 for (; eline
< elineend
; eline
+= linesz
)
1635 struct internal_lineno iline
;
1637 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1639 if (iline
.l_lnno
!= 0)
1640 iline
.l_addr
.l_paddr
+= offset
;
1641 else if (iline
.l_addr
.l_symndx
>= 0
1642 && ((unsigned long) iline
.l_addr
.l_symndx
1643 < obj_raw_syment_count (input_bfd
)))
1647 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1651 /* These line numbers are attached to a symbol
1652 which we are stripping. We should really
1653 just discard the line numbers, but that would
1654 be a pain because we have already counted
1660 struct internal_syment is
;
1661 union internal_auxent ia
;
1663 /* Fix up the lnnoptr field in the aux entry of
1664 the symbol. It turns out that we can't do
1665 this when we modify the symbol aux entries,
1666 because gas sometimes screws up the lnnoptr
1667 field and makes it an offset from the start
1668 of the line numbers rather than an absolute
1670 bfd_coff_swap_sym_in (output_bfd
,
1671 (PTR
) (finfo
->outsyms
1672 + ((indx
- syment_base
)
1675 if ((ISFCN (is
.n_type
)
1676 || is
.n_sclass
== C_BLOCK
)
1677 && is
.n_numaux
>= 1)
1681 auxptr
= (PTR
) (finfo
->outsyms
1682 + ((indx
- syment_base
+ 1)
1684 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1685 is
.n_type
, is
.n_sclass
,
1686 0, is
.n_numaux
, (PTR
) &ia
);
1687 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1688 (o
->output_section
->line_filepos
1689 + o
->output_section
->lineno_count
* linesz
1690 + eline
- finfo
->linenos
);
1691 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1692 is
.n_type
, is
.n_sclass
, 0,
1693 is
.n_numaux
, auxptr
);
1697 iline
.l_addr
.l_symndx
= indx
;
1700 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1703 if (bfd_seek (output_bfd
,
1704 (o
->output_section
->line_filepos
1705 + o
->output_section
->lineno_count
* linesz
),
1707 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1708 output_bfd
) != linesz
* o
->lineno_count
)
1711 o
->output_section
->lineno_count
+= o
->lineno_count
;
1715 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1716 symbol will be the first symbol in the next input file. In the
1717 normal case, this will save us from writing out the C_FILE symbol
1719 if (finfo
->last_file_index
!= -1
1720 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1722 finfo
->last_file
.n_value
= output_index
;
1723 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1724 (PTR
) (finfo
->outsyms
1725 + ((finfo
->last_file_index
- syment_base
)
1729 /* Write the modified symbols to the output file. */
1730 if (outsym
> finfo
->outsyms
)
1732 if (bfd_seek (output_bfd
,
1733 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1735 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1737 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1740 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1741 + (outsym
- finfo
->outsyms
) / osymesz
)
1744 obj_raw_syment_count (output_bfd
) = output_index
;
1747 /* Relocate the contents of each section. */
1748 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1749 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1753 if (! o
->linker_mark
)
1755 /* This section was omitted from the link. */
1759 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1761 if ((o
->flags
& SEC_RELOC
) != 0
1762 && o
->reloc_count
!= 0)
1764 ((*_bfd_error_handler
)
1765 ("%s: relocs in section `%s', but it has no contents",
1766 bfd_get_filename (input_bfd
),
1767 bfd_get_section_name (input_bfd
, o
)));
1768 bfd_set_error (bfd_error_no_contents
);
1775 if (coff_section_data (input_bfd
, o
) != NULL
1776 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1777 contents
= coff_section_data (input_bfd
, o
)->contents
;
1780 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1781 (file_ptr
) 0, o
->_raw_size
))
1783 contents
= finfo
->contents
;
1786 if ((o
->flags
& SEC_RELOC
) != 0)
1789 struct internal_reloc
*internal_relocs
;
1790 struct internal_reloc
*irel
;
1792 /* Read in the relocs. */
1793 target_index
= o
->output_section
->target_index
;
1794 internal_relocs
= (_bfd_coff_read_internal_relocs
1795 (input_bfd
, o
, false, finfo
->external_relocs
,
1796 finfo
->info
->relocateable
,
1797 (finfo
->info
->relocateable
1798 ? (finfo
->section_info
[target_index
].relocs
1799 + o
->output_section
->reloc_count
)
1800 : finfo
->internal_relocs
)));
1801 if (internal_relocs
== NULL
)
1804 /* Call processor specific code to relocate the section
1806 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1810 finfo
->internal_syms
,
1814 if (finfo
->info
->relocateable
)
1817 struct internal_reloc
*irelend
;
1818 struct coff_link_hash_entry
**rel_hash
;
1820 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1821 irel
= internal_relocs
;
1822 irelend
= irel
+ o
->reloc_count
;
1823 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1824 + o
->output_section
->reloc_count
);
1825 for (; irel
< irelend
; irel
++, rel_hash
++)
1827 struct coff_link_hash_entry
*h
;
1832 /* Adjust the reloc address and symbol index. */
1834 irel
->r_vaddr
+= offset
;
1836 if (irel
->r_symndx
== -1)
1841 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1849 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1852 /* This is a global symbol. */
1854 irel
->r_symndx
= h
->indx
;
1857 /* This symbol is being written at the end
1858 of the file, and we do not yet know the
1859 symbol index. We save the pointer to the
1860 hash table entry in the rel_hash list.
1861 We set the indx field to -2 to indicate
1862 that this symbol must not be stripped. */
1871 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1873 irel
->r_symndx
= indx
;
1876 struct internal_syment
*is
;
1878 char buf
[SYMNMLEN
+ 1];
1880 /* This reloc is against a symbol we are
1881 stripping. It would be possible to
1882 handle this case, but I don't think it's
1884 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1886 name
= (_bfd_coff_internal_syment_name
1887 (input_bfd
, is
, buf
));
1891 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1892 (finfo
->info
, name
, input_bfd
, o
,
1899 o
->output_section
->reloc_count
+= o
->reloc_count
;
1903 /* Write out the modified section contents. */
1904 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1905 contents
, o
->output_offset
,
1906 (o
->_cooked_size
!= 0
1912 if (! finfo
->info
->keep_memory
)
1914 if (! _bfd_coff_free_symbols (input_bfd
))
1921 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1924 _bfd_coff_write_global_sym (h
, data
)
1925 struct coff_link_hash_entry
*h
;
1928 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1930 struct internal_syment isym
;
1931 bfd_size_type symesz
;
1934 output_bfd
= finfo
->output_bfd
;
1940 && (finfo
->info
->strip
== strip_all
1941 || (finfo
->info
->strip
== strip_some
1942 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1943 h
->root
.root
.string
, false, false)
1947 switch (h
->root
.type
)
1950 case bfd_link_hash_new
:
1954 case bfd_link_hash_undefined
:
1955 case bfd_link_hash_undefweak
:
1956 isym
.n_scnum
= N_UNDEF
;
1960 case bfd_link_hash_defined
:
1961 case bfd_link_hash_defweak
:
1965 sec
= h
->root
.u
.def
.section
->output_section
;
1966 if (bfd_is_abs_section (sec
))
1967 isym
.n_scnum
= N_ABS
;
1969 isym
.n_scnum
= sec
->target_index
;
1970 isym
.n_value
= (h
->root
.u
.def
.value
1972 + h
->root
.u
.def
.section
->output_offset
);
1976 case bfd_link_hash_common
:
1977 isym
.n_scnum
= N_UNDEF
;
1978 isym
.n_value
= h
->root
.u
.c
.size
;
1981 case bfd_link_hash_indirect
:
1982 case bfd_link_hash_warning
:
1983 /* Just ignore these. They can't be handled anyhow. */
1987 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1988 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1995 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1997 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1999 if (indx
== (bfd_size_type
) -1)
2001 finfo
->failed
= true;
2004 isym
._n
._n_n
._n_zeroes
= 0;
2005 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2008 isym
.n_sclass
= h
->class;
2009 isym
.n_type
= h
->type
;
2011 if (isym
.n_sclass
== C_NULL
)
2012 isym
.n_sclass
= C_EXT
;
2014 isym
.n_numaux
= h
->numaux
;
2016 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2018 symesz
= bfd_coff_symesz (output_bfd
);
2020 if (bfd_seek (output_bfd
,
2021 (obj_sym_filepos (output_bfd
)
2022 + obj_raw_syment_count (output_bfd
) * symesz
),
2024 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2026 finfo
->failed
= true;
2030 h
->indx
= obj_raw_syment_count (output_bfd
);
2032 ++obj_raw_syment_count (output_bfd
);
2034 /* Write out any associated aux entries. There normally will be
2035 none. If there are any, I have no idea how to modify them. */
2036 for (i
= 0; i
< isym
.n_numaux
; i
++)
2038 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2039 isym
.n_sclass
, i
, isym
.n_numaux
,
2040 (PTR
) finfo
->outsyms
);
2041 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2043 finfo
->failed
= true;
2046 ++obj_raw_syment_count (output_bfd
);
2052 /* Handle a link order which is supposed to generate a reloc. */
2055 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2057 struct coff_final_link_info
*finfo
;
2058 asection
*output_section
;
2059 struct bfd_link_order
*link_order
;
2061 reloc_howto_type
*howto
;
2062 struct internal_reloc
*irel
;
2063 struct coff_link_hash_entry
**rel_hash_ptr
;
2065 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2068 bfd_set_error (bfd_error_bad_value
);
2072 if (link_order
->u
.reloc
.p
->addend
!= 0)
2076 bfd_reloc_status_type rstat
;
2079 size
= bfd_get_reloc_size (howto
);
2080 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2084 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2085 link_order
->u
.reloc
.p
->addend
, buf
);
2091 case bfd_reloc_outofrange
:
2093 case bfd_reloc_overflow
:
2094 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2096 (link_order
->type
== bfd_section_reloc_link_order
2097 ? bfd_section_name (output_bfd
,
2098 link_order
->u
.reloc
.p
->u
.section
)
2099 : link_order
->u
.reloc
.p
->u
.name
),
2100 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2101 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2108 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2109 (file_ptr
) link_order
->offset
, size
);
2115 /* Store the reloc information in the right place. It will get
2116 swapped and written out at the end of the final_link routine. */
2118 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2119 + output_section
->reloc_count
);
2120 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2121 + output_section
->reloc_count
);
2123 memset (irel
, 0, sizeof (struct internal_reloc
));
2124 *rel_hash_ptr
= NULL
;
2126 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2128 if (link_order
->type
== bfd_section_reloc_link_order
)
2130 /* We need to somehow locate a symbol in the right section. The
2131 symbol must either have a value of zero, or we must adjust
2132 the addend by the value of the symbol. FIXME: Write this
2133 when we need it. The old linker couldn't handle this anyhow. */
2135 *rel_hash_ptr
= NULL
;
2140 struct coff_link_hash_entry
*h
;
2142 h
= ((struct coff_link_hash_entry
*)
2143 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2144 link_order
->u
.reloc
.p
->u
.name
,
2145 false, false, true));
2149 irel
->r_symndx
= h
->indx
;
2152 /* Set the index to -2 to force this symbol to get
2161 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2162 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2163 (asection
*) NULL
, (bfd_vma
) 0)))
2169 /* FIXME: Is this always right? */
2170 irel
->r_type
= howto
->type
;
2172 /* r_size is only used on the RS/6000, which needs its own linker
2173 routines anyhow. r_extern is only used for ECOFF. */
2175 /* FIXME: What is the right value for r_offset? Is zero OK? */
2177 ++output_section
->reloc_count
;
2182 /* A basic reloc handling routine which may be used by processors with
2186 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2187 input_section
, contents
, relocs
, syms
,
2190 struct bfd_link_info
*info
;
2192 asection
*input_section
;
2194 struct internal_reloc
*relocs
;
2195 struct internal_syment
*syms
;
2196 asection
**sections
;
2198 struct internal_reloc
*rel
;
2199 struct internal_reloc
*relend
;
2202 relend
= rel
+ input_section
->reloc_count
;
2203 for (; rel
< relend
; rel
++)
2206 struct coff_link_hash_entry
*h
;
2207 struct internal_syment
*sym
;
2210 reloc_howto_type
*howto
;
2211 bfd_reloc_status_type rstat
;
2213 symndx
= rel
->r_symndx
;
2222 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2223 sym
= syms
+ symndx
;
2226 /* COFF treats common symbols in one of two ways. Either the
2227 size of the symbol is included in the section contents, or it
2228 is not. We assume that the size is not included, and force
2229 the rtype_to_howto function to adjust the addend as needed. */
2231 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2232 addend
= - sym
->n_value
;
2237 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2250 sec
= bfd_abs_section_ptr
;
2255 sec
= sections
[symndx
];
2256 val
= (sec
->output_section
->vma
2257 + sec
->output_offset
2264 if (h
->root
.type
== bfd_link_hash_defined
2265 || h
->root
.type
== bfd_link_hash_defweak
)
2269 sec
= h
->root
.u
.def
.section
;
2270 val
= (h
->root
.u
.def
.value
2271 + sec
->output_section
->vma
2272 + sec
->output_offset
);
2275 else if (! info
->relocateable
)
2277 if (! ((*info
->callbacks
->undefined_symbol
)
2278 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2279 rel
->r_vaddr
- input_section
->vma
)))
2284 if (info
->base_file
)
2286 /* Emit a reloc if the backend thinks it needs it. */
2287 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
2289 /* relocation to a symbol in a section which
2290 isn't absolute - we output the address here
2292 bfd_vma addr
= rel
->r_vaddr
2293 - input_section
->vma
2294 + input_section
->output_offset
2295 + input_section
->output_section
->vma
;
2296 if (coff_data(output_bfd
)->pe
)
2297 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2298 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2302 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2304 rel
->r_vaddr
- input_section
->vma
,
2313 case bfd_reloc_overflow
:
2316 char buf
[SYMNMLEN
+ 1];
2321 name
= h
->root
.root
.string
;
2324 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2329 if (! ((*info
->callbacks
->reloc_overflow
)
2330 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2331 input_section
, rel
->r_vaddr
- input_section
->vma
)))