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 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1544 strcpy (copy
, elename
);
1546 (*epp
)->name
= copy
;
1547 (*epp
)->type
= islp
->n_type
;
1549 if (islp
->n_numaux
>= 1
1550 && islp
->n_type
!= T_NULL
1551 && islp
->n_sclass
!= C_EOS
)
1553 union internal_auxent eleaux
;
1556 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1557 islp
->n_type
, islp
->n_sclass
, 0,
1558 islp
->n_numaux
, (PTR
) &eleaux
);
1559 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1561 /* FIXME: If this tagndx entry refers to a symbol
1562 defined later in this file, we just ignore it.
1563 Handling this correctly would be tedious, and may
1569 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1572 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1573 if ((*epp
)->tagndx
< 0)
1577 epp
= &(*epp
)->next
;
1580 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1581 islp
+= islp
->n_numaux
+ 1;
1584 /* See if we already have a definition which matches this
1585 type. We always output the type if it has no elements,
1587 if (mt
->elements
== NULL
)
1588 bfd_release (input_bfd
, (PTR
) mt
);
1591 struct coff_debug_merge_type
*mtl
;
1593 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1595 struct coff_debug_merge_element
*me
, *mel
;
1597 if (mtl
->class != mt
->class)
1600 for (me
= mt
->elements
, mel
= mtl
->elements
;
1601 me
!= NULL
&& mel
!= NULL
;
1602 me
= me
->next
, mel
= mel
->next
)
1604 if (strcmp (me
->name
, mel
->name
) != 0
1605 || me
->type
!= mel
->type
1606 || me
->tagndx
!= mel
->tagndx
)
1610 if (me
== NULL
&& mel
== NULL
)
1614 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1616 /* This is the first definition of this type. */
1617 mt
->indx
= output_index
;
1618 mt
->next
= mh
->types
;
1623 /* This is a redefinition which can be merged. */
1624 bfd_release (input_bfd
, (PTR
) mt
);
1625 *indexp
= mtl
->indx
;
1626 add
= (eslend
- esym
) / isymesz
;
1632 /* We now know whether we are to skip this symbol or not. */
1635 /* Adjust the symbol in order to output it. */
1637 if (isym
._n
._n_n
._n_zeroes
== 0
1638 && isym
._n
._n_n
._n_offset
!= 0)
1643 /* This symbol has a long name. Enter it in the string
1644 table we are building. Note that we do not check
1645 bfd_coff_symname_in_debug. That is only true for
1646 XCOFF, and XCOFF requires different linking code
1648 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1652 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1653 if (indx
== (bfd_size_type
) -1)
1655 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1658 if (isym
.n_scnum
> 0)
1660 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1661 isym
.n_value
+= (*secpp
)->output_offset
;
1662 if (! obj_pe (input_bfd
))
1663 isym
.n_value
-= (*secpp
)->vma
;
1664 if (! obj_pe (finfo
->output_bfd
))
1665 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1668 /* The value of a C_FILE symbol is the symbol index of the
1669 next C_FILE symbol. The value of the last C_FILE symbol
1670 is the symbol index to the first external symbol
1671 (actually, coff_renumber_symbols does not get this
1672 right--it just sets the value of the last C_FILE symbol
1673 to zero--and nobody has ever complained about it). We
1674 try to get this right, below, just before we write the
1675 symbols out, but in the general case we may have to write
1676 the symbol out twice. */
1677 if (isym
.n_sclass
== C_FILE
)
1679 if (finfo
->last_file_index
!= -1
1680 && finfo
->last_file
.n_value
!= (long) output_index
)
1682 /* We must correct the value of the last C_FILE entry. */
1683 finfo
->last_file
.n_value
= output_index
;
1684 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1686 /* The last C_FILE symbol is in this input file. */
1687 bfd_coff_swap_sym_out (output_bfd
,
1688 (PTR
) &finfo
->last_file
,
1689 (PTR
) (finfo
->outsyms
1690 + ((finfo
->last_file_index
1696 /* We have already written out the last C_FILE
1697 symbol. We need to write it out again. We
1698 borrow *outsym temporarily. */
1699 bfd_coff_swap_sym_out (output_bfd
,
1700 (PTR
) &finfo
->last_file
,
1702 if (bfd_seek (output_bfd
,
1703 (obj_sym_filepos (output_bfd
)
1704 + finfo
->last_file_index
* osymesz
),
1706 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1712 finfo
->last_file_index
= output_index
;
1713 finfo
->last_file
= isym
;
1716 /* If doing task linking, convert normal global function symbols to
1717 static functions. */
1719 if (finfo
->info
->task_link
1720 && (isym
.n_sclass
== C_EXT
1721 || isym
.n_sclass
== C_WEAKEXT
1722 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)))
1723 isym
.n_sclass
= C_STAT
;
1725 /* Output the symbol. */
1727 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1729 *indexp
= output_index
;
1734 struct coff_link_hash_entry
*h
;
1736 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1738 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1741 /* This can happen if there were errors earlier in
1743 bfd_set_error (bfd_error_bad_value
);
1746 h
->indx
= output_index
;
1749 output_index
+= add
;
1750 outsym
+= add
* osymesz
;
1753 esym
+= add
* isymesz
;
1757 for (--add
; add
> 0; --add
)
1764 /* Fix up the aux entries. This must be done in a separate pass,
1765 because we don't know the correct symbol indices until we have
1766 already decided which symbols we are going to keep. */
1768 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1769 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1770 isymp
= finfo
->internal_syms
;
1771 indexp
= finfo
->sym_indices
;
1772 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1773 outsym
= finfo
->outsyms
;
1774 while (esym
< esym_end
)
1778 add
= 1 + isymp
->n_numaux
;
1781 || (bfd_size_type
) *indexp
< syment_base
)
1782 && (*sym_hash
== NULL
1783 || (*sym_hash
)->auxbfd
!= input_bfd
))
1784 esym
+= add
* isymesz
;
1787 struct coff_link_hash_entry
*h
;
1795 /* The m68k-motorola-sysv assembler will sometimes
1796 generate two symbols with the same name, but only one
1797 will have aux entries. */
1798 BFD_ASSERT (isymp
->n_numaux
== 0
1799 || h
->numaux
== isymp
->n_numaux
);
1807 /* Handle the aux entries. This handling is based on
1808 coff_pointerize_aux. I don't know if it always correct. */
1809 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1811 union internal_auxent aux
;
1812 union internal_auxent
*auxp
;
1818 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1819 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1824 if (isymp
->n_sclass
== C_FILE
)
1826 /* If this is a long filename, we must put it in the
1828 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1829 && auxp
->x_file
.x_n
.x_offset
!= 0)
1831 const char *filename
;
1834 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1835 >= STRING_SIZE_SIZE
);
1836 if (strings
== NULL
)
1838 strings
= _bfd_coff_read_string_table (input_bfd
);
1839 if (strings
== NULL
)
1842 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1843 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1845 if (indx
== (bfd_size_type
) -1)
1847 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1850 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1854 if (ISFCN (isymp
->n_type
)
1855 || ISTAG (isymp
->n_sclass
)
1856 || isymp
->n_sclass
== C_BLOCK
1857 || isymp
->n_sclass
== C_FCN
)
1859 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1861 && indx
< obj_raw_syment_count (input_bfd
))
1863 /* We look forward through the symbol for
1864 the index of the next symbol we are going
1865 to include. I don't know if this is
1867 while ((finfo
->sym_indices
[indx
] < 0
1868 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1870 && indx
< obj_raw_syment_count (input_bfd
))
1872 if (indx
>= obj_raw_syment_count (input_bfd
))
1873 indx
= output_index
;
1875 indx
= finfo
->sym_indices
[indx
];
1876 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1880 indx
= auxp
->x_sym
.x_tagndx
.l
;
1881 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1885 symindx
= finfo
->sym_indices
[indx
];
1887 auxp
->x_sym
.x_tagndx
.l
= 0;
1889 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1892 /* The .bf symbols are supposed to be linked through
1893 the endndx field. We need to carry this list
1894 across object files. */
1897 && isymp
->n_sclass
== C_FCN
1898 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1899 || isymp
->_n
._n_n
._n_offset
== 0)
1900 && isymp
->_n
._n_name
[0] == '.'
1901 && isymp
->_n
._n_name
[1] == 'b'
1902 && isymp
->_n
._n_name
[2] == 'f'
1903 && isymp
->_n
._n_name
[3] == '\0')
1905 if (finfo
->last_bf_index
!= -1)
1907 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1910 if ((bfd_size_type
) finfo
->last_bf_index
1915 /* The last .bf symbol is in this input
1916 file. This will only happen if the
1917 assembler did not set up the .bf
1918 endndx symbols correctly. */
1919 auxout
= (PTR
) (finfo
->outsyms
1920 + ((finfo
->last_bf_index
1923 bfd_coff_swap_aux_out (output_bfd
,
1924 (PTR
) &finfo
->last_bf
,
1932 /* We have already written out the last
1933 .bf aux entry. We need to write it
1934 out again. We borrow *outsym
1935 temporarily. FIXME: This case should
1937 bfd_coff_swap_aux_out (output_bfd
,
1938 (PTR
) &finfo
->last_bf
,
1943 if (bfd_seek (output_bfd
,
1944 (obj_sym_filepos (output_bfd
)
1945 + finfo
->last_bf_index
* osymesz
),
1947 || bfd_write (outsym
, osymesz
, 1,
1948 output_bfd
) != osymesz
)
1953 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1954 finfo
->last_bf_index
= -1;
1957 /* The endndx field of this aux entry must
1958 be updated with the symbol number of the
1960 finfo
->last_bf
= *auxp
;
1961 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1970 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1971 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1985 /* Relocate the line numbers, unless we are stripping them. */
1986 if (finfo
->info
->strip
== strip_none
1987 || finfo
->info
->strip
== strip_some
)
1989 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1995 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1996 build_link_order in ldwrite.c will not have created a
1997 link order, which means that we will not have seen this
1998 input section in _bfd_coff_final_link, which means that
1999 we will not have allocated space for the line numbers of
2000 this section. I don't think line numbers can be
2001 meaningful for a section which does not have
2002 SEC_HAS_CONTENTS set, but, if they do, this must be
2004 if (o
->lineno_count
== 0
2005 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2008 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2009 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2010 input_bfd
) != linesz
* o
->lineno_count
)
2013 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2014 eline
= finfo
->linenos
;
2015 elineend
= eline
+ linesz
* o
->lineno_count
;
2016 for (; eline
< elineend
; eline
+= linesz
)
2018 struct internal_lineno iline
;
2020 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2022 if (iline
.l_lnno
!= 0)
2023 iline
.l_addr
.l_paddr
+= offset
;
2024 else if (iline
.l_addr
.l_symndx
>= 0
2025 && ((unsigned long) iline
.l_addr
.l_symndx
2026 < obj_raw_syment_count (input_bfd
)))
2030 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2034 /* These line numbers are attached to a symbol
2035 which we are stripping. We should really
2036 just discard the line numbers, but that would
2037 be a pain because we have already counted
2043 struct internal_syment is
;
2044 union internal_auxent ia
;
2046 /* Fix up the lnnoptr field in the aux entry of
2047 the symbol. It turns out that we can't do
2048 this when we modify the symbol aux entries,
2049 because gas sometimes screws up the lnnoptr
2050 field and makes it an offset from the start
2051 of the line numbers rather than an absolute
2053 bfd_coff_swap_sym_in (output_bfd
,
2054 (PTR
) (finfo
->outsyms
2055 + ((indx
- syment_base
)
2058 if ((ISFCN (is
.n_type
)
2059 || is
.n_sclass
== C_BLOCK
)
2060 && is
.n_numaux
>= 1)
2064 auxptr
= (PTR
) (finfo
->outsyms
2065 + ((indx
- syment_base
+ 1)
2067 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2068 is
.n_type
, is
.n_sclass
,
2069 0, is
.n_numaux
, (PTR
) &ia
);
2070 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2071 (o
->output_section
->line_filepos
2072 + o
->output_section
->lineno_count
* linesz
2073 + eline
- finfo
->linenos
);
2074 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2075 is
.n_type
, is
.n_sclass
, 0,
2076 is
.n_numaux
, auxptr
);
2080 iline
.l_addr
.l_symndx
= indx
;
2083 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
2086 if (bfd_seek (output_bfd
,
2087 (o
->output_section
->line_filepos
2088 + o
->output_section
->lineno_count
* linesz
),
2090 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
2091 output_bfd
) != linesz
* o
->lineno_count
)
2094 o
->output_section
->lineno_count
+= o
->lineno_count
;
2098 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2099 symbol will be the first symbol in the next input file. In the
2100 normal case, this will save us from writing out the C_FILE symbol
2102 if (finfo
->last_file_index
!= -1
2103 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2105 finfo
->last_file
.n_value
= output_index
;
2106 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2107 (PTR
) (finfo
->outsyms
2108 + ((finfo
->last_file_index
- syment_base
)
2112 /* Write the modified symbols to the output file. */
2113 if (outsym
> finfo
->outsyms
)
2115 if (bfd_seek (output_bfd
,
2116 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2118 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2120 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2123 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2124 + (outsym
- finfo
->outsyms
) / osymesz
)
2127 obj_raw_syment_count (output_bfd
) = output_index
;
2130 /* Relocate the contents of each section. */
2131 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2132 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2135 struct coff_section_tdata
*secdata
;
2137 if (! o
->linker_mark
)
2139 /* This section was omitted from the link. */
2143 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2144 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2146 if ((o
->flags
& SEC_RELOC
) != 0
2147 && o
->reloc_count
!= 0)
2149 ((*_bfd_error_handler
)
2150 (_("%s: relocs in section `%s', but it has no contents"),
2151 bfd_get_filename (input_bfd
),
2152 bfd_get_section_name (input_bfd
, o
)));
2153 bfd_set_error (bfd_error_no_contents
);
2160 secdata
= coff_section_data (input_bfd
, o
);
2161 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2162 contents
= secdata
->contents
;
2165 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2166 (file_ptr
) 0, o
->_raw_size
))
2168 contents
= finfo
->contents
;
2171 if ((o
->flags
& SEC_RELOC
) != 0)
2174 struct internal_reloc
*internal_relocs
;
2175 struct internal_reloc
*irel
;
2177 /* Read in the relocs. */
2178 target_index
= o
->output_section
->target_index
;
2179 internal_relocs
= (_bfd_coff_read_internal_relocs
2180 (input_bfd
, o
, false, finfo
->external_relocs
,
2181 finfo
->info
->relocateable
,
2182 (finfo
->info
->relocateable
2183 ? (finfo
->section_info
[target_index
].relocs
2184 + o
->output_section
->reloc_count
)
2185 : finfo
->internal_relocs
)));
2186 if (internal_relocs
== NULL
)
2189 /* Call processor specific code to relocate the section
2191 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2195 finfo
->internal_syms
,
2199 if (finfo
->info
->relocateable
)
2202 struct internal_reloc
*irelend
;
2203 struct coff_link_hash_entry
**rel_hash
;
2205 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2206 irel
= internal_relocs
;
2207 irelend
= irel
+ o
->reloc_count
;
2208 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2209 + o
->output_section
->reloc_count
);
2210 for (; irel
< irelend
; irel
++, rel_hash
++)
2212 struct coff_link_hash_entry
*h
;
2217 /* Adjust the reloc address and symbol index. */
2219 irel
->r_vaddr
+= offset
;
2221 if (irel
->r_symndx
== -1)
2226 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2234 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2237 /* This is a global symbol. */
2239 irel
->r_symndx
= h
->indx
;
2242 /* This symbol is being written at the end
2243 of the file, and we do not yet know the
2244 symbol index. We save the pointer to the
2245 hash table entry in the rel_hash list.
2246 We set the indx field to -2 to indicate
2247 that this symbol must not be stripped. */
2256 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2258 irel
->r_symndx
= indx
;
2261 struct internal_syment
*is
;
2263 char buf
[SYMNMLEN
+ 1];
2265 /* This reloc is against a symbol we are
2266 stripping. This should have been handled
2267 by the 'dont_skip_symbol' code in the while
2268 loop at the top of this function. */
2270 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2272 name
= (_bfd_coff_internal_syment_name
2273 (input_bfd
, is
, buf
));
2277 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2278 (finfo
->info
, name
, input_bfd
, o
,
2285 o
->output_section
->reloc_count
+= o
->reloc_count
;
2289 /* Write out the modified section contents. */
2290 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2292 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2293 contents
, o
->output_offset
,
2294 (o
->_cooked_size
!= 0
2301 if (! (_bfd_write_section_stabs
2302 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2303 o
, &secdata
->stab_info
, contents
)))
2308 if (! finfo
->info
->keep_memory
)
2310 if (! _bfd_coff_free_symbols (input_bfd
))
2317 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2320 _bfd_coff_write_global_sym (h
, data
)
2321 struct coff_link_hash_entry
*h
;
2324 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2326 struct internal_syment isym
;
2327 bfd_size_type symesz
;
2330 output_bfd
= finfo
->output_bfd
;
2336 && (finfo
->info
->strip
== strip_all
2337 || (finfo
->info
->strip
== strip_some
2338 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2339 h
->root
.root
.string
, false, false)
2343 switch (h
->root
.type
)
2346 case bfd_link_hash_new
:
2350 case bfd_link_hash_undefined
:
2351 case bfd_link_hash_undefweak
:
2352 isym
.n_scnum
= N_UNDEF
;
2356 case bfd_link_hash_defined
:
2357 case bfd_link_hash_defweak
:
2361 sec
= h
->root
.u
.def
.section
->output_section
;
2362 if (bfd_is_abs_section (sec
))
2363 isym
.n_scnum
= N_ABS
;
2365 isym
.n_scnum
= sec
->target_index
;
2366 isym
.n_value
= (h
->root
.u
.def
.value
2367 + h
->root
.u
.def
.section
->output_offset
);
2368 if (! obj_pe (finfo
->output_bfd
))
2369 isym
.n_value
+= sec
->vma
;
2373 case bfd_link_hash_common
:
2374 isym
.n_scnum
= N_UNDEF
;
2375 isym
.n_value
= h
->root
.u
.c
.size
;
2378 case bfd_link_hash_indirect
:
2379 case bfd_link_hash_warning
:
2380 /* Just ignore these. They can't be handled anyhow. */
2384 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2385 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2392 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2394 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2396 if (indx
== (bfd_size_type
) -1)
2398 finfo
->failed
= true;
2401 isym
._n
._n_n
._n_zeroes
= 0;
2402 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2405 isym
.n_sclass
= h
->class;
2406 isym
.n_type
= h
->type
;
2408 if (isym
.n_sclass
== C_NULL
)
2409 isym
.n_sclass
= C_EXT
;
2411 /* If doing task linking and this is the pass where we convert
2412 defined globals to statics, then do that conversion now. If the
2413 symbol is not being converted, just ignore it and it will be
2414 output during a later pass. */
2415 if (finfo
->global_to_static
)
2417 if (isym
.n_sclass
!= C_EXT
2418 && isym
.n_sclass
!= C_WEAKEXT
2419 && (! obj_pe (output_bfd
) || isym
.n_sclass
!= C_NT_WEAK
))
2423 isym
.n_sclass
= C_STAT
;
2426 isym
.n_numaux
= h
->numaux
;
2428 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2430 symesz
= bfd_coff_symesz (output_bfd
);
2432 if (bfd_seek (output_bfd
,
2433 (obj_sym_filepos (output_bfd
)
2434 + obj_raw_syment_count (output_bfd
) * symesz
),
2436 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2438 finfo
->failed
= true;
2442 h
->indx
= obj_raw_syment_count (output_bfd
);
2444 ++obj_raw_syment_count (output_bfd
);
2446 /* Write out any associated aux entries. There normally will be
2447 none. If there are any, I have no idea how to modify them. */
2448 for (i
= 0; i
< isym
.n_numaux
; i
++)
2450 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2451 isym
.n_sclass
, i
, isym
.n_numaux
,
2452 (PTR
) finfo
->outsyms
);
2453 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2455 finfo
->failed
= true;
2458 ++obj_raw_syment_count (output_bfd
);
2464 /* Write out task global symbols, converting them to statics. Called
2465 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2466 the dirty work, if the symbol we are processing needs conversion. */
2469 _bfd_coff_write_task_globals (h
, data
)
2470 struct coff_link_hash_entry
*h
;
2473 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2474 boolean rtnval
= true;
2475 boolean save_global_to_static
;
2479 switch (h
->root
.type
)
2481 case bfd_link_hash_defined
:
2482 case bfd_link_hash_defweak
:
2483 save_global_to_static
= finfo
->global_to_static
;
2484 finfo
->global_to_static
= true;
2485 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2486 finfo
->global_to_static
= save_global_to_static
;
2495 /* Handle a link order which is supposed to generate a reloc. */
2498 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2500 struct coff_final_link_info
*finfo
;
2501 asection
*output_section
;
2502 struct bfd_link_order
*link_order
;
2504 reloc_howto_type
*howto
;
2505 struct internal_reloc
*irel
;
2506 struct coff_link_hash_entry
**rel_hash_ptr
;
2508 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2511 bfd_set_error (bfd_error_bad_value
);
2515 if (link_order
->u
.reloc
.p
->addend
!= 0)
2519 bfd_reloc_status_type rstat
;
2522 size
= bfd_get_reloc_size (howto
);
2523 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2527 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2528 link_order
->u
.reloc
.p
->addend
, buf
);
2534 case bfd_reloc_outofrange
:
2536 case bfd_reloc_overflow
:
2537 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2539 (link_order
->type
== bfd_section_reloc_link_order
2540 ? bfd_section_name (output_bfd
,
2541 link_order
->u
.reloc
.p
->u
.section
)
2542 : link_order
->u
.reloc
.p
->u
.name
),
2543 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2544 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2551 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2552 (file_ptr
) link_order
->offset
, size
);
2558 /* Store the reloc information in the right place. It will get
2559 swapped and written out at the end of the final_link routine. */
2561 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2562 + output_section
->reloc_count
);
2563 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2564 + output_section
->reloc_count
);
2566 memset (irel
, 0, sizeof (struct internal_reloc
));
2567 *rel_hash_ptr
= NULL
;
2569 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2571 if (link_order
->type
== bfd_section_reloc_link_order
)
2573 /* We need to somehow locate a symbol in the right section. The
2574 symbol must either have a value of zero, or we must adjust
2575 the addend by the value of the symbol. FIXME: Write this
2576 when we need it. The old linker couldn't handle this anyhow. */
2578 *rel_hash_ptr
= NULL
;
2583 struct coff_link_hash_entry
*h
;
2585 h
= ((struct coff_link_hash_entry
*)
2586 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2587 link_order
->u
.reloc
.p
->u
.name
,
2588 false, false, true));
2592 irel
->r_symndx
= h
->indx
;
2595 /* Set the index to -2 to force this symbol to get
2604 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2605 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2606 (asection
*) NULL
, (bfd_vma
) 0)))
2612 /* FIXME: Is this always right? */
2613 irel
->r_type
= howto
->type
;
2615 /* r_size is only used on the RS/6000, which needs its own linker
2616 routines anyhow. r_extern is only used for ECOFF. */
2618 /* FIXME: What is the right value for r_offset? Is zero OK? */
2620 ++output_section
->reloc_count
;
2625 /* A basic reloc handling routine which may be used by processors with
2629 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2630 input_section
, contents
, relocs
, syms
,
2633 struct bfd_link_info
*info
;
2635 asection
*input_section
;
2637 struct internal_reloc
*relocs
;
2638 struct internal_syment
*syms
;
2639 asection
**sections
;
2641 struct internal_reloc
*rel
;
2642 struct internal_reloc
*relend
;
2645 relend
= rel
+ input_section
->reloc_count
;
2646 for (; rel
< relend
; rel
++)
2649 struct coff_link_hash_entry
*h
;
2650 struct internal_syment
*sym
;
2653 reloc_howto_type
*howto
;
2654 bfd_reloc_status_type rstat
;
2656 symndx
= rel
->r_symndx
;
2664 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2666 (*_bfd_error_handler
)
2667 ("%s: illegal symbol index %ld in relocs",
2668 bfd_get_filename (input_bfd
), symndx
);
2673 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2674 sym
= syms
+ symndx
;
2677 /* COFF treats common symbols in one of two ways. Either the
2678 size of the symbol is included in the section contents, or it
2679 is not. We assume that the size is not included, and force
2680 the rtype_to_howto function to adjust the addend as needed. */
2682 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2683 addend
= - sym
->n_value
;
2688 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2693 /* If we are doing a relocateable link, then we can just ignore
2694 a PC relative reloc that is pcrel_offset. It will already
2695 have the correct value. If this is not a relocateable link,
2696 then we should ignore the symbol value. */
2697 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2699 if (info
->relocateable
)
2701 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2702 addend
+= sym
->n_value
;
2713 sec
= bfd_abs_section_ptr
;
2718 sec
= sections
[symndx
];
2719 val
= (sec
->output_section
->vma
2720 + sec
->output_offset
2722 if (! obj_pe (input_bfd
))
2728 if (h
->root
.type
== bfd_link_hash_defined
2729 || h
->root
.type
== bfd_link_hash_defweak
)
2733 sec
= h
->root
.u
.def
.section
;
2734 val
= (h
->root
.u
.def
.value
2735 + sec
->output_section
->vma
2736 + sec
->output_offset
);
2739 else if (! info
->relocateable
)
2741 if (! ((*info
->callbacks
->undefined_symbol
)
2742 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2743 rel
->r_vaddr
- input_section
->vma
)))
2748 if (info
->base_file
)
2750 /* Emit a reloc if the backend thinks it needs it. */
2751 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2753 /* Relocation to a symbol in a section which isn't
2754 absolute. We output the address here to a file.
2755 This file is then read by dlltool when generating the
2756 reloc section. Note that the base file is not
2757 portable between systems. We write out a long here,
2758 and dlltool reads in a long. */
2759 long addr
= (rel
->r_vaddr
2760 - input_section
->vma
2761 + input_section
->output_offset
2762 + input_section
->output_section
->vma
);
2763 if (coff_data (output_bfd
)->pe
)
2764 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2765 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2768 bfd_set_error (bfd_error_system_call
);
2774 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2776 rel
->r_vaddr
- input_section
->vma
,
2785 case bfd_reloc_outofrange
:
2786 (*_bfd_error_handler
)
2787 (_("%s: bad reloc address 0x%lx in section `%s'"),
2788 bfd_get_filename (input_bfd
),
2789 (unsigned long) rel
->r_vaddr
,
2790 bfd_get_section_name (input_bfd
, input_section
));
2792 case bfd_reloc_overflow
:
2795 char buf
[SYMNMLEN
+ 1];
2800 name
= h
->root
.root
.string
;
2803 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2808 if (! ((*info
->callbacks
->reloc_overflow
)
2809 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2810 input_section
, rel
->r_vaddr
- input_section
->vma
)))