1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999 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
*));
37 static char *dores_com
PARAMS ((char *, bfd
*, int));
38 static char *get_name
PARAMS ((char *, char **));
39 static int process_embedded_commands
40 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*));
41 static void mark_relocs
PARAMS ((struct coff_final_link_info
*, bfd
*));
43 /* Create an entry in a COFF linker hash table. */
45 struct bfd_hash_entry
*
46 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
47 struct bfd_hash_entry
*entry
;
48 struct bfd_hash_table
*table
;
51 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
53 /* Allocate the structure if it has not already been allocated by a
55 if (ret
== (struct coff_link_hash_entry
*) NULL
)
56 ret
= ((struct coff_link_hash_entry
*)
57 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
58 if (ret
== (struct coff_link_hash_entry
*) NULL
)
59 return (struct bfd_hash_entry
*) ret
;
61 /* Call the allocation method of the superclass. */
62 ret
= ((struct coff_link_hash_entry
*)
63 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
65 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
67 /* Set local fields. */
76 return (struct bfd_hash_entry
*) ret
;
79 /* Initialize a COFF linker hash table. */
82 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
83 struct coff_link_hash_table
*table
;
85 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
86 struct bfd_hash_table
*,
89 table
->stab_info
= NULL
;
90 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
93 /* Create a COFF linker hash table. */
95 struct bfd_link_hash_table
*
96 _bfd_coff_link_hash_table_create (abfd
)
99 struct coff_link_hash_table
*ret
;
101 ret
= ((struct coff_link_hash_table
*)
102 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
105 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
106 _bfd_coff_link_hash_newfunc
))
108 bfd_release (abfd
, ret
);
109 return (struct bfd_link_hash_table
*) NULL
;
114 /* Create an entry in a COFF debug merge hash table. */
116 struct bfd_hash_entry
*
117 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
118 struct bfd_hash_entry
*entry
;
119 struct bfd_hash_table
*table
;
122 struct coff_debug_merge_hash_entry
*ret
=
123 (struct coff_debug_merge_hash_entry
*) entry
;
125 /* Allocate the structure if it has not already been allocated by a
127 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
128 ret
= ((struct coff_debug_merge_hash_entry
*)
129 bfd_hash_allocate (table
,
130 sizeof (struct coff_debug_merge_hash_entry
)));
131 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
132 return (struct bfd_hash_entry
*) ret
;
134 /* Call the allocation method of the superclass. */
135 ret
= ((struct coff_debug_merge_hash_entry
*)
136 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
137 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
139 /* Set local fields. */
143 return (struct bfd_hash_entry
*) ret
;
146 /* Given a COFF BFD, add symbols to the global hash table as
150 _bfd_coff_link_add_symbols (abfd
, info
)
152 struct bfd_link_info
*info
;
154 switch (bfd_get_format (abfd
))
157 return coff_link_add_object_symbols (abfd
, info
);
159 return (_bfd_generic_link_add_archive_symbols
160 (abfd
, info
, coff_link_check_archive_element
));
162 bfd_set_error (bfd_error_wrong_format
);
167 /* Add symbols from a COFF object file. */
170 coff_link_add_object_symbols (abfd
, info
)
172 struct bfd_link_info
*info
;
174 if (! _bfd_coff_get_external_symbols (abfd
))
176 if (! coff_link_add_symbols (abfd
, info
))
179 if (! info
->keep_memory
)
181 if (! _bfd_coff_free_symbols (abfd
))
187 /* Check a single archive element to see if we need to include it in
188 the link. *PNEEDED is set according to whether this element is
189 needed in the link or not. This is called via
190 _bfd_generic_link_add_archive_symbols. */
193 coff_link_check_archive_element (abfd
, info
, pneeded
)
195 struct bfd_link_info
*info
;
198 if (! _bfd_coff_get_external_symbols (abfd
))
201 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
206 if (! coff_link_add_symbols (abfd
, info
))
210 if (! info
->keep_memory
|| ! *pneeded
)
212 if (! _bfd_coff_free_symbols (abfd
))
219 /* Look through the symbols to see if this object file should be
220 included in the link. */
223 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
225 struct bfd_link_info
*info
;
228 bfd_size_type symesz
;
234 symesz
= bfd_coff_symesz (abfd
);
235 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
236 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
237 while (esym
< esym_end
)
239 struct internal_syment sym
;
240 enum coff_symbol_classification classification
;
242 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
244 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
245 if (classification
== COFF_SYMBOL_GLOBAL
246 || classification
== COFF_SYMBOL_COMMON
)
249 char buf
[SYMNMLEN
+ 1];
250 struct bfd_link_hash_entry
*h
;
252 /* This symbol is externally visible, and is defined by this
255 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
258 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
260 /* We are only interested in symbols that are currently
261 undefined. If a symbol is currently known to be common,
262 COFF linkers do not bring in an object file which defines
264 if (h
!= (struct bfd_link_hash_entry
*) NULL
265 && h
->type
== bfd_link_hash_undefined
)
267 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
274 esym
+= (sym
.n_numaux
+ 1) * symesz
;
277 /* We do not need this object file. */
281 /* Add all the symbols from an object file to the hash table. */
284 coff_link_add_symbols (abfd
, info
)
286 struct bfd_link_info
*info
;
289 boolean default_copy
;
290 bfd_size_type symcount
;
291 struct coff_link_hash_entry
**sym_hash
;
292 bfd_size_type symesz
;
296 /* Keep the symbols during this function, in case the linker needs
297 to read the generic symbols in order to report an error message. */
298 keep_syms
= obj_coff_keep_syms (abfd
);
299 obj_coff_keep_syms (abfd
) = true;
301 if (info
->keep_memory
)
302 default_copy
= false;
306 symcount
= obj_raw_syment_count (abfd
);
308 /* We keep a list of the linker hash table entries that correspond
309 to particular symbols. */
310 sym_hash
= ((struct coff_link_hash_entry
**)
313 * sizeof (struct coff_link_hash_entry
*))));
314 if (sym_hash
== NULL
&& symcount
!= 0)
316 obj_coff_sym_hashes (abfd
) = sym_hash
;
318 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
320 symesz
= bfd_coff_symesz (abfd
);
321 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
322 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
323 esym_end
= esym
+ symcount
* symesz
;
324 while (esym
< esym_end
)
326 struct internal_syment sym
;
327 enum coff_symbol_classification classification
;
330 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
332 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
333 if (classification
!= COFF_SYMBOL_LOCAL
)
336 char buf
[SYMNMLEN
+ 1];
342 /* This symbol is externally visible. */
344 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
348 /* We must copy the name into memory if we got it from the
349 syment itself, rather than the string table. */
351 if (sym
._n
._n_n
._n_zeroes
!= 0
352 || sym
._n
._n_n
._n_offset
== 0)
357 switch (classification
)
362 case COFF_SYMBOL_GLOBAL
:
363 flags
= BSF_EXPORT
| BSF_GLOBAL
;
364 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
366 value
-= section
->vma
;
369 case COFF_SYMBOL_UNDEFINED
:
371 section
= bfd_und_section_ptr
;
374 case COFF_SYMBOL_COMMON
:
376 section
= bfd_com_section_ptr
;
379 case COFF_SYMBOL_PE_SECTION
:
380 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
381 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
385 if (sym
.n_sclass
== C_WEAKEXT
386 || (obj_pe (abfd
) && sym
.n_sclass
== C_NT_WEAK
))
391 /* In the PE format, section symbols actually refer to the
392 start of the output section. We handle them specially
394 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
396 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
397 name
, false, copy
, false);
398 if (*sym_hash
!= NULL
)
400 if (((*sym_hash
)->coff_link_hash_flags
401 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
402 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
403 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
404 (*_bfd_error_handler
)
405 ("Warning: symbol `%s' is both section and non-section",
414 if (! (bfd_coff_link_add_one_symbol
415 (info
, abfd
, name
, flags
, section
, value
,
416 (const char *) NULL
, copy
, false,
417 (struct bfd_link_hash_entry
**) sym_hash
)))
421 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
422 (*sym_hash
)->coff_link_hash_flags
|=
423 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
425 if (section
== bfd_com_section_ptr
426 && (*sym_hash
)->root
.type
== bfd_link_hash_common
427 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
428 > bfd_coff_default_section_alignment_power (abfd
)))
429 (*sym_hash
)->root
.u
.c
.p
->alignment_power
430 = bfd_coff_default_section_alignment_power (abfd
);
432 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
434 if (((*sym_hash
)->class == C_NULL
435 && (*sym_hash
)->type
== T_NULL
)
438 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
439 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
441 (*sym_hash
)->class = sym
.n_sclass
;
442 if (sym
.n_type
!= T_NULL
)
444 if ((*sym_hash
)->type
!= T_NULL
445 && (*sym_hash
)->type
!= sym
.n_type
)
446 (*_bfd_error_handler
)
447 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
448 name
, (*sym_hash
)->type
, sym
.n_type
,
449 bfd_get_filename (abfd
));
450 (*sym_hash
)->type
= sym
.n_type
;
452 (*sym_hash
)->auxbfd
= abfd
;
453 if (sym
.n_numaux
!= 0)
455 union internal_auxent
*alloc
;
458 union internal_auxent
*iaux
;
460 (*sym_hash
)->numaux
= sym
.n_numaux
;
461 alloc
= ((union internal_auxent
*)
462 bfd_hash_allocate (&info
->hash
->table
,
464 * sizeof (*alloc
))));
467 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
469 i
++, eaux
+= symesz
, iaux
++)
470 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
471 sym
.n_sclass
, i
, sym
.n_numaux
,
473 (*sym_hash
)->aux
= alloc
;
478 if (classification
== COFF_SYMBOL_PE_SECTION
479 && (*sym_hash
)->numaux
!= 0)
481 /* Some PE sections (such as .bss) have a zero size in
482 the section header, but a non-zero size in the AUX
483 record. Correct that here.
485 FIXME: This is not at all the right place to do this.
486 For example, it won't help objdump. This needs to be
487 done when we swap in the section header. */
489 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
490 if (section
->_raw_size
== 0)
491 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
493 /* FIXME: We could test whether the section sizes
494 matches the size in the aux entry, but apparently
495 that sometimes fails unexpectedly. */
499 esym
+= (sym
.n_numaux
+ 1) * symesz
;
500 sym_hash
+= sym
.n_numaux
+ 1;
503 /* If this is a non-traditional, non-relocateable link, try to
504 optimize the handling of any .stab/.stabstr sections. */
505 if (! info
->relocateable
506 && ! info
->traditional_format
507 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
508 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
510 asection
*stab
, *stabstr
;
512 stab
= bfd_get_section_by_name (abfd
, ".stab");
515 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
519 struct coff_link_hash_table
*table
;
520 struct coff_section_tdata
*secdata
;
522 secdata
= coff_section_data (abfd
, stab
);
526 (PTR
) bfd_zalloc (abfd
,
527 sizeof (struct coff_section_tdata
));
528 if (stab
->used_by_bfd
== NULL
)
530 secdata
= coff_section_data (abfd
, stab
);
533 table
= coff_hash_table (info
);
535 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
537 &secdata
->stab_info
))
543 obj_coff_keep_syms (abfd
) = keep_syms
;
548 obj_coff_keep_syms (abfd
) = keep_syms
;
552 /* Do the final link step. */
555 _bfd_coff_final_link (abfd
, info
)
557 struct bfd_link_info
*info
;
559 bfd_size_type symesz
;
560 struct coff_final_link_info finfo
;
561 boolean debug_merge_allocated
;
562 boolean long_section_names
;
564 struct bfd_link_order
*p
;
565 size_t max_sym_count
;
566 size_t max_lineno_count
;
567 size_t max_reloc_count
;
568 size_t max_output_reloc_count
;
569 size_t max_contents_size
;
570 file_ptr rel_filepos
;
572 file_ptr line_filepos
;
575 bfd_byte
*external_relocs
= NULL
;
576 char strbuf
[STRING_SIZE_SIZE
];
578 symesz
= bfd_coff_symesz (abfd
);
581 finfo
.output_bfd
= abfd
;
583 finfo
.section_info
= NULL
;
584 finfo
.last_file_index
= -1;
585 finfo
.last_bf_index
= -1;
586 finfo
.internal_syms
= NULL
;
587 finfo
.sec_ptrs
= NULL
;
588 finfo
.sym_indices
= NULL
;
589 finfo
.outsyms
= NULL
;
590 finfo
.linenos
= NULL
;
591 finfo
.contents
= NULL
;
592 finfo
.external_relocs
= NULL
;
593 finfo
.internal_relocs
= NULL
;
594 finfo
.global_to_static
= false;
595 debug_merge_allocated
= false;
597 coff_data (abfd
)->link_info
= info
;
599 finfo
.strtab
= _bfd_stringtab_init ();
600 if (finfo
.strtab
== NULL
)
603 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
605 debug_merge_allocated
= true;
607 /* Compute the file positions for all the sections. */
608 if (! abfd
->output_has_begun
)
610 if (! bfd_coff_compute_section_file_positions (abfd
))
614 /* Count the line numbers and relocation entries required for the
615 output file. Set the file positions for the relocs. */
616 rel_filepos
= obj_relocbase (abfd
);
617 relsz
= bfd_coff_relsz (abfd
);
618 max_contents_size
= 0;
619 max_lineno_count
= 0;
622 long_section_names
= false;
623 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
627 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
629 if (p
->type
== bfd_indirect_link_order
)
633 sec
= p
->u
.indirect
.section
;
635 /* Mark all sections which are to be included in the
636 link. This will normally be every section. We need
637 to do this so that we can identify any sections which
638 the linker has decided to not include. */
639 sec
->linker_mark
= true;
641 if (info
->strip
== strip_none
642 || info
->strip
== strip_some
)
643 o
->lineno_count
+= sec
->lineno_count
;
645 if (info
->relocateable
)
646 o
->reloc_count
+= sec
->reloc_count
;
648 if (sec
->_raw_size
> max_contents_size
)
649 max_contents_size
= sec
->_raw_size
;
650 if (sec
->lineno_count
> max_lineno_count
)
651 max_lineno_count
= sec
->lineno_count
;
652 if (sec
->reloc_count
> max_reloc_count
)
653 max_reloc_count
= sec
->reloc_count
;
655 else if (info
->relocateable
656 && (p
->type
== bfd_section_reloc_link_order
657 || p
->type
== bfd_symbol_reloc_link_order
))
660 if (o
->reloc_count
== 0)
664 o
->flags
|= SEC_RELOC
;
665 o
->rel_filepos
= rel_filepos
;
666 rel_filepos
+= o
->reloc_count
* relsz
;
669 if (bfd_coff_long_section_names (abfd
)
670 && strlen (o
->name
) > SCNNMLEN
)
672 /* This section has a long name which must go in the string
673 table. This must correspond to the code in
674 coff_write_object_contents which puts the string index
675 into the s_name field of the section header. That is why
676 we pass hash as false. */
677 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
678 == (bfd_size_type
) -1)
680 long_section_names
= true;
684 /* If doing a relocateable link, allocate space for the pointers we
686 if (info
->relocateable
)
690 /* We use section_count + 1, rather than section_count, because
691 the target_index fields are 1 based. */
693 ((struct coff_link_section_info
*)
694 bfd_malloc ((abfd
->section_count
+ 1)
695 * sizeof (struct coff_link_section_info
)));
696 if (finfo
.section_info
== NULL
)
698 for (i
= 0; i
<= abfd
->section_count
; i
++)
700 finfo
.section_info
[i
].relocs
= NULL
;
701 finfo
.section_info
[i
].rel_hashes
= NULL
;
705 /* We now know the size of the relocs, so we can determine the file
706 positions of the line numbers. */
707 line_filepos
= rel_filepos
;
708 linesz
= bfd_coff_linesz (abfd
);
709 max_output_reloc_count
= 0;
710 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
712 if (o
->lineno_count
== 0)
716 o
->line_filepos
= line_filepos
;
717 line_filepos
+= o
->lineno_count
* linesz
;
720 if (o
->reloc_count
!= 0)
722 /* We don't know the indices of global symbols until we have
723 written out all the local symbols. For each section in
724 the output file, we keep an array of pointers to hash
725 table entries. Each entry in the array corresponds to a
726 reloc. When we find a reloc against a global symbol, we
727 set the corresponding entry in this array so that we can
728 fix up the symbol index after we have written out all the
731 Because of this problem, we also keep the relocs in
732 memory until the end of the link. This wastes memory,
733 but only when doing a relocateable link, which is not the
735 BFD_ASSERT (info
->relocateable
);
736 finfo
.section_info
[o
->target_index
].relocs
=
737 ((struct internal_reloc
*)
738 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
739 finfo
.section_info
[o
->target_index
].rel_hashes
=
740 ((struct coff_link_hash_entry
**)
741 bfd_malloc (o
->reloc_count
742 * sizeof (struct coff_link_hash_entry
*)));
743 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
744 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
747 if (o
->reloc_count
> max_output_reloc_count
)
748 max_output_reloc_count
= o
->reloc_count
;
751 /* Reset the reloc and lineno counts, so that we can use them to
752 count the number of entries we have output so far. */
757 obj_sym_filepos (abfd
) = line_filepos
;
759 /* Figure out the largest number of symbols in an input BFD. Take
760 the opportunity to clear the output_has_begun fields of all the
763 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
767 sub
->output_has_begun
= false;
768 sz
= obj_raw_syment_count (sub
);
769 if (sz
> max_sym_count
)
773 /* Allocate some buffers used while linking. */
774 finfo
.internal_syms
= ((struct internal_syment
*)
775 bfd_malloc (max_sym_count
776 * sizeof (struct internal_syment
)));
777 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
778 * sizeof (asection
*));
779 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
780 finfo
.outsyms
= ((bfd_byte
*)
781 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
782 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
783 * bfd_coff_linesz (abfd
));
784 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
785 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
786 if (! info
->relocateable
)
787 finfo
.internal_relocs
= ((struct internal_reloc
*)
788 bfd_malloc (max_reloc_count
789 * sizeof (struct internal_reloc
)));
790 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
791 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
792 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
793 || finfo
.outsyms
== NULL
794 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
795 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
796 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
797 || (! info
->relocateable
798 && finfo
.internal_relocs
== NULL
799 && max_reloc_count
> 0))
802 /* We now know the position of everything in the file, except that
803 we don't know the size of the symbol table and therefore we don't
804 know where the string table starts. We just build the string
805 table in memory as we go along. We process all the relocations
806 for a single input file at once. */
807 obj_raw_syment_count (abfd
) = 0;
809 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
811 if (! bfd_coff_start_final_link (abfd
, info
))
815 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
817 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
819 if (p
->type
== bfd_indirect_link_order
820 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
821 == bfd_target_coff_flavour
))
823 sub
= p
->u
.indirect
.section
->owner
;
824 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
826 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
828 sub
->output_has_begun
= true;
831 else if (p
->type
== bfd_section_reloc_link_order
832 || p
->type
== bfd_symbol_reloc_link_order
)
834 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
839 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
845 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
848 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
850 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
851 debug_merge_allocated
= false;
853 if (finfo
.internal_syms
!= NULL
)
855 free (finfo
.internal_syms
);
856 finfo
.internal_syms
= NULL
;
858 if (finfo
.sec_ptrs
!= NULL
)
860 free (finfo
.sec_ptrs
);
861 finfo
.sec_ptrs
= NULL
;
863 if (finfo
.sym_indices
!= NULL
)
865 free (finfo
.sym_indices
);
866 finfo
.sym_indices
= NULL
;
868 if (finfo
.linenos
!= NULL
)
870 free (finfo
.linenos
);
871 finfo
.linenos
= NULL
;
873 if (finfo
.contents
!= NULL
)
875 free (finfo
.contents
);
876 finfo
.contents
= NULL
;
878 if (finfo
.external_relocs
!= NULL
)
880 free (finfo
.external_relocs
);
881 finfo
.external_relocs
= NULL
;
883 if (finfo
.internal_relocs
!= NULL
)
885 free (finfo
.internal_relocs
);
886 finfo
.internal_relocs
= NULL
;
889 /* The value of the last C_FILE symbol is supposed to be the symbol
890 index of the first external symbol. Write it out again if
892 if (finfo
.last_file_index
!= -1
893 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
895 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
896 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
897 (PTR
) finfo
.outsyms
);
899 (obj_sym_filepos (abfd
)
900 + finfo
.last_file_index
* symesz
),
902 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
906 /* If doing task linking (ld --task-link) then make a pass through the
907 global symbols, writing out any that are defined, and making them
911 finfo
.failed
= false;
912 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
918 /* Write out the global symbols. */
919 finfo
.failed
= false;
920 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
925 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
926 if (finfo
.outsyms
!= NULL
)
928 free (finfo
.outsyms
);
929 finfo
.outsyms
= NULL
;
932 if (info
->relocateable
&& max_output_reloc_count
> 0)
934 /* Now that we have written out all the global symbols, we know
935 the symbol indices to use for relocs against them, and we can
936 finally write out the relocs. */
937 external_relocs
= ((bfd_byte
*)
938 bfd_malloc (max_output_reloc_count
* relsz
));
939 if (external_relocs
== NULL
)
942 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
944 struct internal_reloc
*irel
;
945 struct internal_reloc
*irelend
;
946 struct coff_link_hash_entry
**rel_hash
;
949 if (o
->reloc_count
== 0)
952 irel
= finfo
.section_info
[o
->target_index
].relocs
;
953 irelend
= irel
+ o
->reloc_count
;
954 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
955 erel
= external_relocs
;
956 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
958 if (*rel_hash
!= NULL
)
960 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
961 irel
->r_symndx
= (*rel_hash
)->indx
;
963 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
966 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
967 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
968 abfd
) != relsz
* o
->reloc_count
)
972 free (external_relocs
);
973 external_relocs
= NULL
;
976 /* Free up the section information. */
977 if (finfo
.section_info
!= NULL
)
981 for (i
= 0; i
< abfd
->section_count
; i
++)
983 if (finfo
.section_info
[i
].relocs
!= NULL
)
984 free (finfo
.section_info
[i
].relocs
);
985 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
986 free (finfo
.section_info
[i
].rel_hashes
);
988 free (finfo
.section_info
);
989 finfo
.section_info
= NULL
;
992 /* If we have optimized stabs strings, output them. */
993 if (coff_hash_table (info
)->stab_info
!= NULL
)
995 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
999 /* Write out the string table. */
1000 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1003 (obj_sym_filepos (abfd
)
1004 + obj_raw_syment_count (abfd
) * symesz
),
1008 #if STRING_SIZE_SIZE == 4
1010 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1011 (bfd_byte
*) strbuf
);
1013 #error Change bfd_h_put_32
1016 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1019 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1023 _bfd_stringtab_free (finfo
.strtab
);
1025 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1026 not try to write out the symbols. */
1027 bfd_get_symcount (abfd
) = 0;
1032 if (debug_merge_allocated
)
1033 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1034 if (finfo
.strtab
!= NULL
)
1035 _bfd_stringtab_free (finfo
.strtab
);
1036 if (finfo
.section_info
!= NULL
)
1040 for (i
= 0; i
< abfd
->section_count
; i
++)
1042 if (finfo
.section_info
[i
].relocs
!= NULL
)
1043 free (finfo
.section_info
[i
].relocs
);
1044 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1045 free (finfo
.section_info
[i
].rel_hashes
);
1047 free (finfo
.section_info
);
1049 if (finfo
.internal_syms
!= NULL
)
1050 free (finfo
.internal_syms
);
1051 if (finfo
.sec_ptrs
!= NULL
)
1052 free (finfo
.sec_ptrs
);
1053 if (finfo
.sym_indices
!= NULL
)
1054 free (finfo
.sym_indices
);
1055 if (finfo
.outsyms
!= NULL
)
1056 free (finfo
.outsyms
);
1057 if (finfo
.linenos
!= NULL
)
1058 free (finfo
.linenos
);
1059 if (finfo
.contents
!= NULL
)
1060 free (finfo
.contents
);
1061 if (finfo
.external_relocs
!= NULL
)
1062 free (finfo
.external_relocs
);
1063 if (finfo
.internal_relocs
!= NULL
)
1064 free (finfo
.internal_relocs
);
1065 if (external_relocs
!= NULL
)
1066 free (external_relocs
);
1070 /* parse out a -heap <reserved>,<commit> line */
1073 dores_com (ptr
, output_bfd
, heap
)
1078 if (coff_data(output_bfd
)->pe
)
1080 int val
= strtoul (ptr
, &ptr
, 0);
1082 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1084 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1088 int val
= strtoul (ptr
+1, &ptr
, 0);
1090 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1092 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1098 static char *get_name(ptr
, dst
)
1105 while (*ptr
&& *ptr
!= ' ')
1111 /* Process any magic embedded commands in a section called .drectve */
1114 process_embedded_commands (output_bfd
, info
, abfd
)
1116 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1119 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1126 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1129 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1134 e
= copy
+ sec
->_raw_size
;
1135 for (s
= copy
; s
< e
; )
1141 if (strncmp (s
,"-attr", 5) == 0)
1153 s
= get_name(s
, &name
);
1154 s
= get_name(s
, &attribs
);
1174 asec
= bfd_get_section_by_name (abfd
, name
);
1177 asec
->flags
|= SEC_CODE
;
1179 asec
->flags
|= SEC_READONLY
;
1182 else if (strncmp (s
,"-heap", 5) == 0)
1184 s
= dores_com (s
+5, output_bfd
, 1);
1186 else if (strncmp (s
,"-stack", 6) == 0)
1188 s
= dores_com (s
+6, output_bfd
, 0);
1197 /* Place a marker against all symbols which are used by relocations.
1198 This marker can be picked up by the 'do we skip this symbol ?'
1199 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1204 mark_relocs (finfo
, input_bfd
)
1205 struct coff_final_link_info
* finfo
;
1210 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1213 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1215 struct internal_reloc
* internal_relocs
;
1216 struct internal_reloc
* irel
;
1217 struct internal_reloc
* irelend
;
1220 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1223 /* Read in the relocs. */
1224 internal_relocs
= _bfd_coff_read_internal_relocs
1225 (input_bfd
, a
, false,
1226 finfo
->external_relocs
,
1227 finfo
->info
->relocateable
,
1228 (finfo
->info
->relocateable
1229 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1230 : finfo
->internal_relocs
)
1233 if (internal_relocs
== NULL
)
1236 irel
= internal_relocs
;
1237 irelend
= irel
+ a
->reloc_count
;
1239 /* Place a mark in the sym_indices array (whose entries have
1240 been initialised to 0) for all of the symbols that are used
1241 in the relocation table. This will then be picked up in the
1244 for (; irel
< irelend
; irel
++)
1246 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1251 /* Link an input file into the linker output file. This function
1252 handles all the sections and relocations of the input file at once. */
1255 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1256 struct coff_final_link_info
*finfo
;
1259 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1260 asection
*, struct internal_reloc
*,
1263 const char *strings
;
1264 bfd_size_type syment_base
;
1265 unsigned int n_tmask
;
1266 unsigned int n_btshft
;
1268 bfd_size_type isymesz
;
1269 bfd_size_type osymesz
;
1270 bfd_size_type linesz
;
1273 struct internal_syment
*isymp
;
1276 unsigned long output_index
;
1278 struct coff_link_hash_entry
**sym_hash
;
1281 /* Move all the symbols to the output file. */
1283 output_bfd
= finfo
->output_bfd
;
1285 syment_base
= obj_raw_syment_count (output_bfd
);
1286 isymesz
= bfd_coff_symesz (input_bfd
);
1287 osymesz
= bfd_coff_symesz (output_bfd
);
1288 linesz
= bfd_coff_linesz (input_bfd
);
1289 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1291 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1292 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1294 /* Define macros so that ISFCN, et. al., macros work correctly. */
1295 #define N_TMASK n_tmask
1296 #define N_BTSHFT n_btshft
1299 if (! finfo
->info
->keep_memory
)
1302 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1305 if (! _bfd_coff_get_external_symbols (input_bfd
))
1308 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1309 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1310 isymp
= finfo
->internal_syms
;
1311 secpp
= finfo
->sec_ptrs
;
1312 indexp
= finfo
->sym_indices
;
1313 output_index
= syment_base
;
1314 outsym
= finfo
->outsyms
;
1316 if (coff_data (output_bfd
)->pe
)
1318 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1322 /* If we are going to perform relocations and also strip/discard some symbols
1323 then we must make sure that we do not strip/discard those symbols that are
1324 going to be involved in the relocations */
1325 if (( finfo
->info
->strip
!= strip_none
1326 || finfo
->info
->discard
!= discard_none
)
1327 && finfo
->info
->relocateable
)
1329 /* mark the symbol array as 'not-used' */
1330 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1332 mark_relocs (finfo
, input_bfd
);
1335 while (esym
< esym_end
)
1337 struct internal_syment isym
;
1338 enum coff_symbol_classification classification
;
1341 boolean dont_skip_symbol
;
1344 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1346 /* Make a copy of *isymp so that the relocate_section function
1347 always sees the original values. This is more reliable than
1348 always recomputing the symbol value even if we are stripping
1352 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1353 switch (classification
)
1357 case COFF_SYMBOL_GLOBAL
:
1358 case COFF_SYMBOL_PE_SECTION
:
1359 case COFF_SYMBOL_LOCAL
:
1360 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1362 case COFF_SYMBOL_COMMON
:
1363 *secpp
= bfd_com_section_ptr
;
1365 case COFF_SYMBOL_UNDEFINED
:
1366 *secpp
= bfd_und_section_ptr
;
1370 /* Extract the flag indicating if this symbol is used by a
1372 if ((finfo
->info
->strip
!= strip_none
1373 || finfo
->info
->discard
!= discard_none
)
1374 && finfo
->info
->relocateable
)
1375 dont_skip_symbol
= *indexp
;
1377 dont_skip_symbol
= false;
1383 add
= 1 + isym
.n_numaux
;
1385 /* If we are stripping all symbols, we want to skip this one. */
1386 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1391 switch (classification
)
1395 case COFF_SYMBOL_GLOBAL
:
1396 case COFF_SYMBOL_COMMON
:
1397 case COFF_SYMBOL_PE_SECTION
:
1398 /* This is a global symbol. Global symbols come at the
1399 end of the symbol table, so skip them for now.
1400 Locally defined function symbols, however, are an
1401 exception, and are not moved to the end. */
1403 if (! ISFCN (isym
.n_type
))
1407 case COFF_SYMBOL_UNDEFINED
:
1408 /* Undefined symbols are left for the end. */
1413 case COFF_SYMBOL_LOCAL
:
1414 /* This is a local symbol. Skip it if we are discarding
1416 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1422 /* If we stripping debugging symbols, and this is a debugging
1423 symbol, then skip it. FIXME: gas sets the section to N_ABS
1424 for some types of debugging symbols; I don't know if this is
1425 a bug or not. In any case, we handle it here. */
1427 && finfo
->info
->strip
== strip_debugger
1428 && ! dont_skip_symbol
1429 && (isym
.n_scnum
== N_DEBUG
1430 || (isym
.n_scnum
== N_ABS
1431 && (isym
.n_sclass
== C_AUTO
1432 || isym
.n_sclass
== C_REG
1433 || isym
.n_sclass
== C_MOS
1434 || isym
.n_sclass
== C_MOE
1435 || isym
.n_sclass
== C_MOU
1436 || isym
.n_sclass
== C_ARG
1437 || isym
.n_sclass
== C_REGPARM
1438 || isym
.n_sclass
== C_FIELD
1439 || isym
.n_sclass
== C_EOS
))))
1442 /* If some symbols are stripped based on the name, work out the
1443 name and decide whether to skip this symbol. */
1445 && (finfo
->info
->strip
== strip_some
1446 || finfo
->info
->discard
== discard_l
))
1449 char buf
[SYMNMLEN
+ 1];
1451 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1455 if (! dont_skip_symbol
1456 && ((finfo
->info
->strip
== strip_some
1457 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1460 && finfo
->info
->discard
== discard_l
1461 && bfd_is_local_label_name (input_bfd
, name
))))
1465 /* If this is an enum, struct, or union tag, see if we have
1466 already output an identical type. */
1468 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1469 && (isym
.n_sclass
== C_ENTAG
1470 || isym
.n_sclass
== C_STRTAG
1471 || isym
.n_sclass
== C_UNTAG
)
1472 && isym
.n_numaux
== 1)
1475 char buf
[SYMNMLEN
+ 1];
1476 struct coff_debug_merge_hash_entry
*mh
;
1477 struct coff_debug_merge_type
*mt
;
1478 union internal_auxent aux
;
1479 struct coff_debug_merge_element
**epp
;
1480 bfd_byte
*esl
, *eslend
;
1481 struct internal_syment
*islp
;
1483 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1487 /* Ignore fake names invented by compiler; treat them all as
1489 if (*name
== '~' || *name
== '.' || *name
== '$'
1490 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1491 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1494 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1499 /* Allocate memory to hold type information. If this turns
1500 out to be a duplicate, we pass this address to
1502 mt
= ((struct coff_debug_merge_type
*)
1503 bfd_alloc (input_bfd
,
1504 sizeof (struct coff_debug_merge_type
)));
1507 mt
->class = isym
.n_sclass
;
1509 /* Pick up the aux entry, which points to the end of the tag
1511 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1512 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1515 /* Gather the elements. */
1516 epp
= &mt
->elements
;
1517 mt
->elements
= NULL
;
1519 esl
= esym
+ 2 * isymesz
;
1520 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1521 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1522 while (esl
< eslend
)
1524 const char *elename
;
1525 char elebuf
[SYMNMLEN
+ 1];
1528 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1530 *epp
= ((struct coff_debug_merge_element
*)
1531 bfd_alloc (input_bfd
,
1532 sizeof (struct coff_debug_merge_element
)));
1536 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1538 if (elename
== NULL
)
1541 name_copy
= (char *) bfd_alloc (input_bfd
,
1542 strlen (elename
) + 1);
1543 if (name_copy
== NULL
)
1545 strcpy (name_copy
, elename
);
1547 (*epp
)->name
= name_copy
;
1548 (*epp
)->type
= islp
->n_type
;
1550 if (islp
->n_numaux
>= 1
1551 && islp
->n_type
!= T_NULL
1552 && islp
->n_sclass
!= C_EOS
)
1554 union internal_auxent eleaux
;
1557 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1558 islp
->n_type
, islp
->n_sclass
, 0,
1559 islp
->n_numaux
, (PTR
) &eleaux
);
1560 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1562 /* FIXME: If this tagndx entry refers to a symbol
1563 defined later in this file, we just ignore it.
1564 Handling this correctly would be tedious, and may
1570 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1573 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1574 if ((*epp
)->tagndx
< 0)
1578 epp
= &(*epp
)->next
;
1581 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1582 islp
+= islp
->n_numaux
+ 1;
1585 /* See if we already have a definition which matches this
1586 type. We always output the type if it has no elements,
1588 if (mt
->elements
== NULL
)
1589 bfd_release (input_bfd
, (PTR
) mt
);
1592 struct coff_debug_merge_type
*mtl
;
1594 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1596 struct coff_debug_merge_element
*me
, *mel
;
1598 if (mtl
->class != mt
->class)
1601 for (me
= mt
->elements
, mel
= mtl
->elements
;
1602 me
!= NULL
&& mel
!= NULL
;
1603 me
= me
->next
, mel
= mel
->next
)
1605 if (strcmp (me
->name
, mel
->name
) != 0
1606 || me
->type
!= mel
->type
1607 || me
->tagndx
!= mel
->tagndx
)
1611 if (me
== NULL
&& mel
== NULL
)
1615 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1617 /* This is the first definition of this type. */
1618 mt
->indx
= output_index
;
1619 mt
->next
= mh
->types
;
1624 /* This is a redefinition which can be merged. */
1625 bfd_release (input_bfd
, (PTR
) mt
);
1626 *indexp
= mtl
->indx
;
1627 add
= (eslend
- esym
) / isymesz
;
1633 /* We now know whether we are to skip this symbol or not. */
1636 /* Adjust the symbol in order to output it. */
1638 if (isym
._n
._n_n
._n_zeroes
== 0
1639 && isym
._n
._n_n
._n_offset
!= 0)
1644 /* This symbol has a long name. Enter it in the string
1645 table we are building. Note that we do not check
1646 bfd_coff_symname_in_debug. That is only true for
1647 XCOFF, and XCOFF requires different linking code
1649 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1653 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1654 if (indx
== (bfd_size_type
) -1)
1656 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1659 if (isym
.n_scnum
> 0)
1661 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1662 isym
.n_value
+= (*secpp
)->output_offset
;
1663 if (! obj_pe (input_bfd
))
1664 isym
.n_value
-= (*secpp
)->vma
;
1665 if (! obj_pe (finfo
->output_bfd
))
1666 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1669 /* The value of a C_FILE symbol is the symbol index of the
1670 next C_FILE symbol. The value of the last C_FILE symbol
1671 is the symbol index to the first external symbol
1672 (actually, coff_renumber_symbols does not get this
1673 right--it just sets the value of the last C_FILE symbol
1674 to zero--and nobody has ever complained about it). We
1675 try to get this right, below, just before we write the
1676 symbols out, but in the general case we may have to write
1677 the symbol out twice. */
1678 if (isym
.n_sclass
== C_FILE
)
1680 if (finfo
->last_file_index
!= -1
1681 && finfo
->last_file
.n_value
!= (long) output_index
)
1683 /* We must correct the value of the last C_FILE entry. */
1684 finfo
->last_file
.n_value
= output_index
;
1685 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1687 /* The last C_FILE symbol is in this input file. */
1688 bfd_coff_swap_sym_out (output_bfd
,
1689 (PTR
) &finfo
->last_file
,
1690 (PTR
) (finfo
->outsyms
1691 + ((finfo
->last_file_index
1697 /* We have already written out the last C_FILE
1698 symbol. We need to write it out again. We
1699 borrow *outsym temporarily. */
1700 bfd_coff_swap_sym_out (output_bfd
,
1701 (PTR
) &finfo
->last_file
,
1703 if (bfd_seek (output_bfd
,
1704 (obj_sym_filepos (output_bfd
)
1705 + finfo
->last_file_index
* osymesz
),
1707 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1713 finfo
->last_file_index
= output_index
;
1714 finfo
->last_file
= isym
;
1717 /* If doing task linking, convert normal global function symbols to
1718 static functions. */
1720 if (finfo
->info
->task_link
1721 && (isym
.n_sclass
== C_EXT
1722 || isym
.n_sclass
== C_WEAKEXT
1723 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)))
1724 isym
.n_sclass
= C_STAT
;
1726 /* Output the symbol. */
1728 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1730 *indexp
= output_index
;
1735 struct coff_link_hash_entry
*h
;
1737 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1739 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1742 /* This can happen if there were errors earlier in
1744 bfd_set_error (bfd_error_bad_value
);
1747 h
->indx
= output_index
;
1750 output_index
+= add
;
1751 outsym
+= add
* osymesz
;
1754 esym
+= add
* isymesz
;
1758 for (--add
; add
> 0; --add
)
1765 /* Fix up the aux entries. This must be done in a separate pass,
1766 because we don't know the correct symbol indices until we have
1767 already decided which symbols we are going to keep. */
1769 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1770 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1771 isymp
= finfo
->internal_syms
;
1772 indexp
= finfo
->sym_indices
;
1773 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1774 outsym
= finfo
->outsyms
;
1775 while (esym
< esym_end
)
1779 add
= 1 + isymp
->n_numaux
;
1782 || (bfd_size_type
) *indexp
< syment_base
)
1783 && (*sym_hash
== NULL
1784 || (*sym_hash
)->auxbfd
!= input_bfd
))
1785 esym
+= add
* isymesz
;
1788 struct coff_link_hash_entry
*h
;
1796 /* The m68k-motorola-sysv assembler will sometimes
1797 generate two symbols with the same name, but only one
1798 will have aux entries. */
1799 BFD_ASSERT (isymp
->n_numaux
== 0
1800 || h
->numaux
== isymp
->n_numaux
);
1808 /* Handle the aux entries. This handling is based on
1809 coff_pointerize_aux. I don't know if it always correct. */
1810 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1812 union internal_auxent aux
;
1813 union internal_auxent
*auxp
;
1819 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1820 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1825 if (isymp
->n_sclass
== C_FILE
)
1827 /* If this is a long filename, we must put it in the
1829 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1830 && auxp
->x_file
.x_n
.x_offset
!= 0)
1832 const char *filename
;
1835 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1836 >= STRING_SIZE_SIZE
);
1837 if (strings
== NULL
)
1839 strings
= _bfd_coff_read_string_table (input_bfd
);
1840 if (strings
== NULL
)
1843 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1844 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1846 if (indx
== (bfd_size_type
) -1)
1848 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1851 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1855 if (ISFCN (isymp
->n_type
)
1856 || ISTAG (isymp
->n_sclass
)
1857 || isymp
->n_sclass
== C_BLOCK
1858 || isymp
->n_sclass
== C_FCN
)
1860 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1862 && indx
< obj_raw_syment_count (input_bfd
))
1864 /* We look forward through the symbol for
1865 the index of the next symbol we are going
1866 to include. I don't know if this is
1868 while ((finfo
->sym_indices
[indx
] < 0
1869 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1871 && indx
< obj_raw_syment_count (input_bfd
))
1873 if (indx
>= obj_raw_syment_count (input_bfd
))
1874 indx
= output_index
;
1876 indx
= finfo
->sym_indices
[indx
];
1877 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1881 indx
= auxp
->x_sym
.x_tagndx
.l
;
1882 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1886 symindx
= finfo
->sym_indices
[indx
];
1888 auxp
->x_sym
.x_tagndx
.l
= 0;
1890 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1893 /* The .bf symbols are supposed to be linked through
1894 the endndx field. We need to carry this list
1895 across object files. */
1898 && isymp
->n_sclass
== C_FCN
1899 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1900 || isymp
->_n
._n_n
._n_offset
== 0)
1901 && isymp
->_n
._n_name
[0] == '.'
1902 && isymp
->_n
._n_name
[1] == 'b'
1903 && isymp
->_n
._n_name
[2] == 'f'
1904 && isymp
->_n
._n_name
[3] == '\0')
1906 if (finfo
->last_bf_index
!= -1)
1908 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1911 if ((bfd_size_type
) finfo
->last_bf_index
1916 /* The last .bf symbol is in this input
1917 file. This will only happen if the
1918 assembler did not set up the .bf
1919 endndx symbols correctly. */
1920 auxout
= (PTR
) (finfo
->outsyms
1921 + ((finfo
->last_bf_index
1924 bfd_coff_swap_aux_out (output_bfd
,
1925 (PTR
) &finfo
->last_bf
,
1933 /* We have already written out the last
1934 .bf aux entry. We need to write it
1935 out again. We borrow *outsym
1936 temporarily. FIXME: This case should
1938 bfd_coff_swap_aux_out (output_bfd
,
1939 (PTR
) &finfo
->last_bf
,
1944 if (bfd_seek (output_bfd
,
1945 (obj_sym_filepos (output_bfd
)
1946 + finfo
->last_bf_index
* osymesz
),
1948 || bfd_write (outsym
, osymesz
, 1,
1949 output_bfd
) != osymesz
)
1954 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1955 finfo
->last_bf_index
= -1;
1958 /* The endndx field of this aux entry must
1959 be updated with the symbol number of the
1961 finfo
->last_bf
= *auxp
;
1962 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1971 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1972 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1986 /* Relocate the line numbers, unless we are stripping them. */
1987 if (finfo
->info
->strip
== strip_none
1988 || finfo
->info
->strip
== strip_some
)
1990 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1996 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1997 build_link_order in ldwrite.c will not have created a
1998 link order, which means that we will not have seen this
1999 input section in _bfd_coff_final_link, which means that
2000 we will not have allocated space for the line numbers of
2001 this section. I don't think line numbers can be
2002 meaningful for a section which does not have
2003 SEC_HAS_CONTENTS set, but, if they do, this must be
2005 if (o
->lineno_count
== 0
2006 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2009 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2010 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2011 input_bfd
) != linesz
* o
->lineno_count
)
2014 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2015 eline
= finfo
->linenos
;
2016 elineend
= eline
+ linesz
* o
->lineno_count
;
2017 for (; eline
< elineend
; eline
+= linesz
)
2019 struct internal_lineno iline
;
2021 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2023 if (iline
.l_lnno
!= 0)
2024 iline
.l_addr
.l_paddr
+= offset
;
2025 else if (iline
.l_addr
.l_symndx
>= 0
2026 && ((unsigned long) iline
.l_addr
.l_symndx
2027 < obj_raw_syment_count (input_bfd
)))
2031 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2035 /* These line numbers are attached to a symbol
2036 which we are stripping. We should really
2037 just discard the line numbers, but that would
2038 be a pain because we have already counted
2044 struct internal_syment is
;
2045 union internal_auxent ia
;
2047 /* Fix up the lnnoptr field in the aux entry of
2048 the symbol. It turns out that we can't do
2049 this when we modify the symbol aux entries,
2050 because gas sometimes screws up the lnnoptr
2051 field and makes it an offset from the start
2052 of the line numbers rather than an absolute
2054 bfd_coff_swap_sym_in (output_bfd
,
2055 (PTR
) (finfo
->outsyms
2056 + ((indx
- syment_base
)
2059 if ((ISFCN (is
.n_type
)
2060 || is
.n_sclass
== C_BLOCK
)
2061 && is
.n_numaux
>= 1)
2065 auxptr
= (PTR
) (finfo
->outsyms
2066 + ((indx
- syment_base
+ 1)
2068 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2069 is
.n_type
, is
.n_sclass
,
2070 0, is
.n_numaux
, (PTR
) &ia
);
2071 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2072 (o
->output_section
->line_filepos
2073 + o
->output_section
->lineno_count
* linesz
2074 + eline
- finfo
->linenos
);
2075 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2076 is
.n_type
, is
.n_sclass
, 0,
2077 is
.n_numaux
, auxptr
);
2081 iline
.l_addr
.l_symndx
= indx
;
2084 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
2087 if (bfd_seek (output_bfd
,
2088 (o
->output_section
->line_filepos
2089 + o
->output_section
->lineno_count
* linesz
),
2091 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
2092 output_bfd
) != linesz
* o
->lineno_count
)
2095 o
->output_section
->lineno_count
+= o
->lineno_count
;
2099 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2100 symbol will be the first symbol in the next input file. In the
2101 normal case, this will save us from writing out the C_FILE symbol
2103 if (finfo
->last_file_index
!= -1
2104 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2106 finfo
->last_file
.n_value
= output_index
;
2107 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2108 (PTR
) (finfo
->outsyms
2109 + ((finfo
->last_file_index
- syment_base
)
2113 /* Write the modified symbols to the output file. */
2114 if (outsym
> finfo
->outsyms
)
2116 if (bfd_seek (output_bfd
,
2117 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2119 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2121 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2124 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2125 + (outsym
- finfo
->outsyms
) / osymesz
)
2128 obj_raw_syment_count (output_bfd
) = output_index
;
2131 /* Relocate the contents of each section. */
2132 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2133 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2136 struct coff_section_tdata
*secdata
;
2138 if (! o
->linker_mark
)
2140 /* This section was omitted from the link. */
2144 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2145 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2147 if ((o
->flags
& SEC_RELOC
) != 0
2148 && o
->reloc_count
!= 0)
2150 ((*_bfd_error_handler
)
2151 (_("%s: relocs in section `%s', but it has no contents"),
2152 bfd_get_filename (input_bfd
),
2153 bfd_get_section_name (input_bfd
, o
)));
2154 bfd_set_error (bfd_error_no_contents
);
2161 secdata
= coff_section_data (input_bfd
, o
);
2162 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2163 contents
= secdata
->contents
;
2166 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2167 (file_ptr
) 0, o
->_raw_size
))
2169 contents
= finfo
->contents
;
2172 if ((o
->flags
& SEC_RELOC
) != 0)
2175 struct internal_reloc
*internal_relocs
;
2176 struct internal_reloc
*irel
;
2178 /* Read in the relocs. */
2179 target_index
= o
->output_section
->target_index
;
2180 internal_relocs
= (_bfd_coff_read_internal_relocs
2181 (input_bfd
, o
, false, finfo
->external_relocs
,
2182 finfo
->info
->relocateable
,
2183 (finfo
->info
->relocateable
2184 ? (finfo
->section_info
[target_index
].relocs
2185 + o
->output_section
->reloc_count
)
2186 : finfo
->internal_relocs
)));
2187 if (internal_relocs
== NULL
)
2190 /* Call processor specific code to relocate the section
2192 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2196 finfo
->internal_syms
,
2200 if (finfo
->info
->relocateable
)
2203 struct internal_reloc
*irelend
;
2204 struct coff_link_hash_entry
**rel_hash
;
2206 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2207 irel
= internal_relocs
;
2208 irelend
= irel
+ o
->reloc_count
;
2209 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2210 + o
->output_section
->reloc_count
);
2211 for (; irel
< irelend
; irel
++, rel_hash
++)
2213 struct coff_link_hash_entry
*h
;
2218 /* Adjust the reloc address and symbol index. */
2220 irel
->r_vaddr
+= offset
;
2222 if (irel
->r_symndx
== -1)
2227 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2235 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2238 /* This is a global symbol. */
2240 irel
->r_symndx
= h
->indx
;
2243 /* This symbol is being written at the end
2244 of the file, and we do not yet know the
2245 symbol index. We save the pointer to the
2246 hash table entry in the rel_hash list.
2247 We set the indx field to -2 to indicate
2248 that this symbol must not be stripped. */
2257 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2259 irel
->r_symndx
= indx
;
2262 struct internal_syment
*is
;
2264 char buf
[SYMNMLEN
+ 1];
2266 /* This reloc is against a symbol we are
2267 stripping. This should have been handled
2268 by the 'dont_skip_symbol' code in the while
2269 loop at the top of this function. */
2271 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2273 name
= (_bfd_coff_internal_syment_name
2274 (input_bfd
, is
, buf
));
2278 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2279 (finfo
->info
, name
, input_bfd
, o
,
2286 o
->output_section
->reloc_count
+= o
->reloc_count
;
2290 /* Write out the modified section contents. */
2291 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2293 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2294 contents
, o
->output_offset
,
2295 (o
->_cooked_size
!= 0
2302 if (! (_bfd_write_section_stabs
2303 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2304 o
, &secdata
->stab_info
, contents
)))
2309 if (! finfo
->info
->keep_memory
)
2311 if (! _bfd_coff_free_symbols (input_bfd
))
2318 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2321 _bfd_coff_write_global_sym (h
, data
)
2322 struct coff_link_hash_entry
*h
;
2325 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2327 struct internal_syment isym
;
2328 bfd_size_type symesz
;
2331 output_bfd
= finfo
->output_bfd
;
2337 && (finfo
->info
->strip
== strip_all
2338 || (finfo
->info
->strip
== strip_some
2339 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2340 h
->root
.root
.string
, false, false)
2344 switch (h
->root
.type
)
2347 case bfd_link_hash_new
:
2351 case bfd_link_hash_undefined
:
2352 case bfd_link_hash_undefweak
:
2353 isym
.n_scnum
= N_UNDEF
;
2357 case bfd_link_hash_defined
:
2358 case bfd_link_hash_defweak
:
2362 sec
= h
->root
.u
.def
.section
->output_section
;
2363 if (bfd_is_abs_section (sec
))
2364 isym
.n_scnum
= N_ABS
;
2366 isym
.n_scnum
= sec
->target_index
;
2367 isym
.n_value
= (h
->root
.u
.def
.value
2368 + h
->root
.u
.def
.section
->output_offset
);
2369 if (! obj_pe (finfo
->output_bfd
))
2370 isym
.n_value
+= sec
->vma
;
2374 case bfd_link_hash_common
:
2375 isym
.n_scnum
= N_UNDEF
;
2376 isym
.n_value
= h
->root
.u
.c
.size
;
2379 case bfd_link_hash_indirect
:
2380 case bfd_link_hash_warning
:
2381 /* Just ignore these. They can't be handled anyhow. */
2385 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2386 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2393 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2395 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2397 if (indx
== (bfd_size_type
) -1)
2399 finfo
->failed
= true;
2402 isym
._n
._n_n
._n_zeroes
= 0;
2403 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2406 isym
.n_sclass
= h
->class;
2407 isym
.n_type
= h
->type
;
2409 if (isym
.n_sclass
== C_NULL
)
2410 isym
.n_sclass
= C_EXT
;
2412 /* If doing task linking and this is the pass where we convert
2413 defined globals to statics, then do that conversion now. If the
2414 symbol is not being converted, just ignore it and it will be
2415 output during a later pass. */
2416 if (finfo
->global_to_static
)
2418 if (isym
.n_sclass
!= C_EXT
2419 && isym
.n_sclass
!= C_WEAKEXT
2420 && (! obj_pe (output_bfd
) || isym
.n_sclass
!= C_NT_WEAK
))
2424 isym
.n_sclass
= C_STAT
;
2427 isym
.n_numaux
= h
->numaux
;
2429 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2431 symesz
= bfd_coff_symesz (output_bfd
);
2433 if (bfd_seek (output_bfd
,
2434 (obj_sym_filepos (output_bfd
)
2435 + obj_raw_syment_count (output_bfd
) * symesz
),
2437 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2439 finfo
->failed
= true;
2443 h
->indx
= obj_raw_syment_count (output_bfd
);
2445 ++obj_raw_syment_count (output_bfd
);
2447 /* Write out any associated aux entries. There normally will be
2448 none. If there are any, I have no idea how to modify them. */
2449 for (i
= 0; i
< isym
.n_numaux
; i
++)
2451 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2452 isym
.n_sclass
, i
, isym
.n_numaux
,
2453 (PTR
) finfo
->outsyms
);
2454 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2456 finfo
->failed
= true;
2459 ++obj_raw_syment_count (output_bfd
);
2465 /* Write out task global symbols, converting them to statics. Called
2466 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2467 the dirty work, if the symbol we are processing needs conversion. */
2470 _bfd_coff_write_task_globals (h
, data
)
2471 struct coff_link_hash_entry
*h
;
2474 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2475 boolean rtnval
= true;
2476 boolean save_global_to_static
;
2480 switch (h
->root
.type
)
2482 case bfd_link_hash_defined
:
2483 case bfd_link_hash_defweak
:
2484 save_global_to_static
= finfo
->global_to_static
;
2485 finfo
->global_to_static
= true;
2486 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2487 finfo
->global_to_static
= save_global_to_static
;
2496 /* Handle a link order which is supposed to generate a reloc. */
2499 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2501 struct coff_final_link_info
*finfo
;
2502 asection
*output_section
;
2503 struct bfd_link_order
*link_order
;
2505 reloc_howto_type
*howto
;
2506 struct internal_reloc
*irel
;
2507 struct coff_link_hash_entry
**rel_hash_ptr
;
2509 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2512 bfd_set_error (bfd_error_bad_value
);
2516 if (link_order
->u
.reloc
.p
->addend
!= 0)
2520 bfd_reloc_status_type rstat
;
2523 size
= bfd_get_reloc_size (howto
);
2524 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2528 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2529 link_order
->u
.reloc
.p
->addend
, buf
);
2535 case bfd_reloc_outofrange
:
2537 case bfd_reloc_overflow
:
2538 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2540 (link_order
->type
== bfd_section_reloc_link_order
2541 ? bfd_section_name (output_bfd
,
2542 link_order
->u
.reloc
.p
->u
.section
)
2543 : link_order
->u
.reloc
.p
->u
.name
),
2544 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2545 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2552 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2553 (file_ptr
) link_order
->offset
, size
);
2559 /* Store the reloc information in the right place. It will get
2560 swapped and written out at the end of the final_link routine. */
2562 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2563 + output_section
->reloc_count
);
2564 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2565 + output_section
->reloc_count
);
2567 memset (irel
, 0, sizeof (struct internal_reloc
));
2568 *rel_hash_ptr
= NULL
;
2570 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2572 if (link_order
->type
== bfd_section_reloc_link_order
)
2574 /* We need to somehow locate a symbol in the right section. The
2575 symbol must either have a value of zero, or we must adjust
2576 the addend by the value of the symbol. FIXME: Write this
2577 when we need it. The old linker couldn't handle this anyhow. */
2579 *rel_hash_ptr
= NULL
;
2584 struct coff_link_hash_entry
*h
;
2586 h
= ((struct coff_link_hash_entry
*)
2587 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2588 link_order
->u
.reloc
.p
->u
.name
,
2589 false, false, true));
2593 irel
->r_symndx
= h
->indx
;
2596 /* Set the index to -2 to force this symbol to get
2605 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2606 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2607 (asection
*) NULL
, (bfd_vma
) 0)))
2613 /* FIXME: Is this always right? */
2614 irel
->r_type
= howto
->type
;
2616 /* r_size is only used on the RS/6000, which needs its own linker
2617 routines anyhow. r_extern is only used for ECOFF. */
2619 /* FIXME: What is the right value for r_offset? Is zero OK? */
2621 ++output_section
->reloc_count
;
2626 /* A basic reloc handling routine which may be used by processors with
2630 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2631 input_section
, contents
, relocs
, syms
,
2634 struct bfd_link_info
*info
;
2636 asection
*input_section
;
2638 struct internal_reloc
*relocs
;
2639 struct internal_syment
*syms
;
2640 asection
**sections
;
2642 struct internal_reloc
*rel
;
2643 struct internal_reloc
*relend
;
2646 relend
= rel
+ input_section
->reloc_count
;
2647 for (; rel
< relend
; rel
++)
2650 struct coff_link_hash_entry
*h
;
2651 struct internal_syment
*sym
;
2654 reloc_howto_type
*howto
;
2655 bfd_reloc_status_type rstat
;
2657 symndx
= rel
->r_symndx
;
2665 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2667 (*_bfd_error_handler
)
2668 ("%s: illegal symbol index %ld in relocs",
2669 bfd_get_filename (input_bfd
), symndx
);
2674 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2675 sym
= syms
+ symndx
;
2678 /* COFF treats common symbols in one of two ways. Either the
2679 size of the symbol is included in the section contents, or it
2680 is not. We assume that the size is not included, and force
2681 the rtype_to_howto function to adjust the addend as needed. */
2683 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2684 addend
= - sym
->n_value
;
2689 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2694 /* If we are doing a relocateable link, then we can just ignore
2695 a PC relative reloc that is pcrel_offset. It will already
2696 have the correct value. If this is not a relocateable link,
2697 then we should ignore the symbol value. */
2698 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2700 if (info
->relocateable
)
2702 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2703 addend
+= sym
->n_value
;
2714 sec
= bfd_abs_section_ptr
;
2719 sec
= sections
[symndx
];
2720 val
= (sec
->output_section
->vma
2721 + sec
->output_offset
2723 if (! obj_pe (input_bfd
))
2729 if (h
->root
.type
== bfd_link_hash_defined
2730 || h
->root
.type
== bfd_link_hash_defweak
)
2734 sec
= h
->root
.u
.def
.section
;
2735 val
= (h
->root
.u
.def
.value
2736 + sec
->output_section
->vma
2737 + sec
->output_offset
);
2740 else if (! info
->relocateable
)
2742 if (! ((*info
->callbacks
->undefined_symbol
)
2743 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2744 rel
->r_vaddr
- input_section
->vma
)))
2749 if (info
->base_file
)
2751 /* Emit a reloc if the backend thinks it needs it. */
2752 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2754 /* Relocation to a symbol in a section which isn't
2755 absolute. We output the address here to a file.
2756 This file is then read by dlltool when generating the
2757 reloc section. Note that the base file is not
2758 portable between systems. We write out a long here,
2759 and dlltool reads in a long. */
2760 long addr
= (rel
->r_vaddr
2761 - input_section
->vma
2762 + input_section
->output_offset
2763 + input_section
->output_section
->vma
);
2764 if (coff_data (output_bfd
)->pe
)
2765 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2766 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2769 bfd_set_error (bfd_error_system_call
);
2775 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2777 rel
->r_vaddr
- input_section
->vma
,
2786 case bfd_reloc_outofrange
:
2787 (*_bfd_error_handler
)
2788 (_("%s: bad reloc address 0x%lx in section `%s'"),
2789 bfd_get_filename (input_bfd
),
2790 (unsigned long) rel
->r_vaddr
,
2791 bfd_get_section_name (input_bfd
, input_section
));
2793 case bfd_reloc_overflow
:
2796 char buf
[SYMNMLEN
+ 1];
2801 name
= h
->root
.root
.string
;
2804 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2809 if (! ((*info
->callbacks
->reloc_overflow
)
2810 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2811 input_section
, rel
->r_vaddr
- input_section
->vma
)))