1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file contains the COFF backend linker code. */
28 #include "coff/internal.h"
30 #include "safe-ctype.h"
32 static bfd_boolean
coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
);
33 static bfd_boolean
coff_link_check_archive_element (bfd
*abfd
, struct bfd_link_info
*info
, bfd_boolean
*pneeded
);
34 static bfd_boolean
coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
);
36 /* Return TRUE if SYM is a weak, external symbol. */
37 #define IS_WEAK_EXTERNAL(abfd, sym) \
38 ((sym).n_sclass == C_WEAKEXT \
39 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
41 /* Return TRUE if SYM is an external symbol. */
42 #define IS_EXTERNAL(abfd, sym) \
43 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
45 /* Define macros so that the ISFCN, et. al., macros work correctly.
46 These macros are defined in include/coff/internal.h in terms of
47 N_TMASK, etc. These definitions require a user to define local
48 variables with the appropriate names, and with values from the
49 coff_data (abfd) structure. */
51 #define N_TMASK n_tmask
52 #define N_BTSHFT n_btshft
53 #define N_BTMASK n_btmask
55 /* Create an entry in a COFF linker hash table. */
57 struct bfd_hash_entry
*
58 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
59 struct bfd_hash_table
*table
,
62 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
64 /* Allocate the structure if it has not already been allocated by a
66 if (ret
== (struct coff_link_hash_entry
*) NULL
)
67 ret
= ((struct coff_link_hash_entry
*)
68 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
69 if (ret
== (struct coff_link_hash_entry
*) NULL
)
70 return (struct bfd_hash_entry
*) ret
;
72 /* Call the allocation method of the superclass. */
73 ret
= ((struct coff_link_hash_entry
*)
74 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
76 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
78 /* Set local fields. */
87 return (struct bfd_hash_entry
*) ret
;
90 /* Initialize a COFF linker hash table. */
93 _bfd_coff_link_hash_table_init (struct coff_link_hash_table
*table
,
95 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
96 struct bfd_hash_table
*,
99 memset (&table
->stab_info
, 0, sizeof (table
->stab_info
));
100 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
103 /* Create a COFF linker hash table. */
105 struct bfd_link_hash_table
*
106 _bfd_coff_link_hash_table_create (bfd
*abfd
)
108 struct coff_link_hash_table
*ret
;
109 bfd_size_type amt
= sizeof (struct coff_link_hash_table
);
111 ret
= bfd_malloc (amt
);
115 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
116 _bfd_coff_link_hash_newfunc
))
119 return (struct bfd_link_hash_table
*) NULL
;
124 /* Create an entry in a COFF debug merge hash table. */
126 struct bfd_hash_entry
*
127 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
128 struct bfd_hash_table
*table
,
131 struct coff_debug_merge_hash_entry
*ret
=
132 (struct coff_debug_merge_hash_entry
*) entry
;
134 /* Allocate the structure if it has not already been allocated by a
136 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
137 ret
= ((struct coff_debug_merge_hash_entry
*)
138 bfd_hash_allocate (table
,
139 sizeof (struct coff_debug_merge_hash_entry
)));
140 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
141 return (struct bfd_hash_entry
*) ret
;
143 /* Call the allocation method of the superclass. */
144 ret
= ((struct coff_debug_merge_hash_entry
*)
145 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
146 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
148 /* Set local fields. */
152 return (struct bfd_hash_entry
*) ret
;
155 /* Given a COFF BFD, add symbols to the global hash table as
159 _bfd_coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
161 switch (bfd_get_format (abfd
))
164 return coff_link_add_object_symbols (abfd
, info
);
166 return _bfd_generic_link_add_archive_symbols
167 (abfd
, info
, coff_link_check_archive_element
);
169 bfd_set_error (bfd_error_wrong_format
);
174 /* Add symbols from a COFF object file. */
177 coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
179 if (! _bfd_coff_get_external_symbols (abfd
))
181 if (! coff_link_add_symbols (abfd
, info
))
184 if (! info
->keep_memory
185 && ! _bfd_coff_free_symbols (abfd
))
191 /* Look through the symbols to see if this object file should be
192 included in the link. */
195 coff_link_check_ar_symbols (bfd
*abfd
,
196 struct bfd_link_info
*info
,
197 bfd_boolean
*pneeded
)
199 bfd_size_type symesz
;
205 symesz
= bfd_coff_symesz (abfd
);
206 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
207 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
208 while (esym
< esym_end
)
210 struct internal_syment sym
;
211 enum coff_symbol_classification classification
;
213 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
215 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
216 if (classification
== COFF_SYMBOL_GLOBAL
217 || classification
== COFF_SYMBOL_COMMON
)
220 char buf
[SYMNMLEN
+ 1];
221 struct bfd_link_hash_entry
*h
;
223 /* This symbol is externally visible, and is defined by this
225 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
228 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
232 && info
->pei386_auto_import
233 && !strncmp (name
,"__imp_", 6))
234 h
= bfd_link_hash_lookup (info
->hash
, name
+ 6, FALSE
, FALSE
, TRUE
);
236 /* We are only interested in symbols that are currently
237 undefined. If a symbol is currently known to be common,
238 COFF linkers do not bring in an object file which defines
240 if (h
!= (struct bfd_link_hash_entry
*) NULL
241 && h
->type
== bfd_link_hash_undefined
)
243 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
250 esym
+= (sym
.n_numaux
+ 1) * symesz
;
253 /* We do not need this object file. */
257 /* Check a single archive element to see if we need to include it in
258 the link. *PNEEDED is set according to whether this element is
259 needed in the link or not. This is called via
260 _bfd_generic_link_add_archive_symbols. */
263 coff_link_check_archive_element (bfd
*abfd
,
264 struct bfd_link_info
*info
,
265 bfd_boolean
*pneeded
)
267 if (! _bfd_coff_get_external_symbols (abfd
))
270 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
274 && ! coff_link_add_symbols (abfd
, info
))
277 if ((! info
->keep_memory
|| ! *pneeded
)
278 && ! _bfd_coff_free_symbols (abfd
))
284 /* Add all the symbols from an object file to the hash table. */
287 coff_link_add_symbols (bfd
*abfd
,
288 struct bfd_link_info
*info
)
290 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
291 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
292 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
293 bfd_boolean keep_syms
;
294 bfd_boolean default_copy
;
295 bfd_size_type symcount
;
296 struct coff_link_hash_entry
**sym_hash
;
297 bfd_size_type symesz
;
302 /* Keep the symbols during this function, in case the linker needs
303 to read the generic symbols in order to report an error message. */
304 keep_syms
= obj_coff_keep_syms (abfd
);
305 obj_coff_keep_syms (abfd
) = TRUE
;
307 if (info
->keep_memory
)
308 default_copy
= FALSE
;
312 symcount
= obj_raw_syment_count (abfd
);
314 /* We keep a list of the linker hash table entries that correspond
315 to particular symbols. */
316 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
317 sym_hash
= bfd_zalloc (abfd
, amt
);
318 if (sym_hash
== NULL
&& symcount
!= 0)
320 obj_coff_sym_hashes (abfd
) = sym_hash
;
322 symesz
= bfd_coff_symesz (abfd
);
323 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
324 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
325 esym_end
= esym
+ symcount
* symesz
;
326 while (esym
< esym_end
)
328 struct internal_syment sym
;
329 enum coff_symbol_classification classification
;
332 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
334 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
335 if (classification
!= COFF_SYMBOL_LOCAL
)
338 char buf
[SYMNMLEN
+ 1];
344 /* This symbol is externally visible. */
346 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
350 /* We must copy the name into memory if we got it from the
351 syment itself, rather than the string table. */
353 if (sym
._n
._n_n
._n_zeroes
!= 0
354 || sym
._n
._n_n
._n_offset
== 0)
359 switch (classification
)
364 case COFF_SYMBOL_GLOBAL
:
365 flags
= BSF_EXPORT
| BSF_GLOBAL
;
366 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
368 value
-= section
->vma
;
371 case COFF_SYMBOL_UNDEFINED
:
373 section
= bfd_und_section_ptr
;
376 case COFF_SYMBOL_COMMON
:
378 section
= bfd_com_section_ptr
;
381 case COFF_SYMBOL_PE_SECTION
:
382 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
383 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
387 if (IS_WEAK_EXTERNAL (abfd
, sym
))
392 /* In the PE format, section symbols actually refer to the
393 start of the output section. We handle them specially
395 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
397 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
398 name
, FALSE
, copy
, FALSE
);
399 if (*sym_hash
!= NULL
)
401 if (((*sym_hash
)->coff_link_hash_flags
402 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
403 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
404 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
405 (*_bfd_error_handler
)
406 ("Warning: symbol `%s' is both section and non-section",
413 /* The Microsoft Visual C compiler does string pooling by
414 hashing the constants to an internal symbol name, and
415 relying on the linker comdat support to discard
416 duplicate names. However, if one string is a literal and
417 one is a data initializer, one will end up in the .data
418 section and one will end up in the .rdata section. The
419 Microsoft linker will combine them into the .data
420 section, which seems to be wrong since it might cause the
423 As long as there are no external references to the
424 symbols, which there shouldn't be, we can treat the .data
425 and .rdata instances as separate symbols. The comdat
426 code in the linker will do the appropriate merging. Here
427 we avoid getting a multiple definition error for one of
428 these special symbols.
430 FIXME: I don't think this will work in the case where
431 there are two object files which use the constants as a
432 literal and two object files which use it as a data
433 initializer. One or the other of the second object files
434 is going to wind up with an inappropriate reference. */
436 && (classification
== COFF_SYMBOL_GLOBAL
437 || classification
== COFF_SYMBOL_PE_SECTION
)
438 && coff_section_data (abfd
, section
) != NULL
439 && coff_section_data (abfd
, section
)->comdat
!= NULL
440 && strncmp (name
, "??_", 3) == 0
441 && strcmp (name
, coff_section_data (abfd
, section
)->comdat
->name
) == 0)
443 if (*sym_hash
== NULL
)
444 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
445 name
, FALSE
, copy
, FALSE
);
446 if (*sym_hash
!= NULL
447 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
448 && coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
!= NULL
449 && strcmp (coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
->name
,
450 coff_section_data (abfd
, section
)->comdat
->name
) == 0)
456 if (! (bfd_coff_link_add_one_symbol
457 (info
, abfd
, name
, flags
, section
, value
,
458 (const char *) NULL
, copy
, FALSE
,
459 (struct bfd_link_hash_entry
**) sym_hash
)))
463 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
464 (*sym_hash
)->coff_link_hash_flags
|=
465 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
467 /* Limit the alignment of a common symbol to the possible
468 alignment of a section. There is no point to permitting
469 a higher alignment for a common symbol: we can not
470 guarantee it, and it may cause us to allocate extra space
471 in the common section. */
472 if (section
== bfd_com_section_ptr
473 && (*sym_hash
)->root
.type
== bfd_link_hash_common
474 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
475 > bfd_coff_default_section_alignment_power (abfd
)))
476 (*sym_hash
)->root
.u
.c
.p
->alignment_power
477 = bfd_coff_default_section_alignment_power (abfd
);
479 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
481 /* If we don't have any symbol information currently in
482 the hash table, or if we are looking at a symbol
483 definition, then update the symbol class and type in
485 if (((*sym_hash
)->class == C_NULL
486 && (*sym_hash
)->type
== T_NULL
)
489 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
490 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
492 (*sym_hash
)->class = sym
.n_sclass
;
493 if (sym
.n_type
!= T_NULL
)
495 /* We want to warn if the type changed, but not
496 if it changed from an unspecified type.
497 Testing the whole type byte may work, but the
498 change from (e.g.) a function of unspecified
499 type to function of known type also wants to
501 if ((*sym_hash
)->type
!= T_NULL
502 && (*sym_hash
)->type
!= sym
.n_type
503 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
504 && (BTYPE ((*sym_hash
)->type
) == T_NULL
505 || BTYPE (sym
.n_type
) == T_NULL
)))
506 (*_bfd_error_handler
)
507 (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
508 abfd
, name
, (*sym_hash
)->type
, sym
.n_type
);
510 /* We don't want to change from a meaningful
511 base type to a null one, but if we know
512 nothing, take what little we might now know. */
513 if (BTYPE (sym
.n_type
) != T_NULL
514 || (*sym_hash
)->type
== T_NULL
)
515 (*sym_hash
)->type
= sym
.n_type
;
517 (*sym_hash
)->auxbfd
= abfd
;
518 if (sym
.n_numaux
!= 0)
520 union internal_auxent
*alloc
;
523 union internal_auxent
*iaux
;
525 (*sym_hash
)->numaux
= sym
.n_numaux
;
526 alloc
= ((union internal_auxent
*)
527 bfd_hash_allocate (&info
->hash
->table
,
529 * sizeof (*alloc
))));
532 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
534 i
++, eaux
+= symesz
, iaux
++)
535 bfd_coff_swap_aux_in (abfd
, eaux
, sym
.n_type
,
536 sym
.n_sclass
, (int) i
,
538 (*sym_hash
)->aux
= alloc
;
543 if (classification
== COFF_SYMBOL_PE_SECTION
544 && (*sym_hash
)->numaux
!= 0)
546 /* Some PE sections (such as .bss) have a zero size in
547 the section header, but a non-zero size in the AUX
548 record. Correct that here.
550 FIXME: This is not at all the right place to do this.
551 For example, it won't help objdump. This needs to be
552 done when we swap in the section header. */
553 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
554 if (section
->size
== 0)
555 section
->size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
557 /* FIXME: We could test whether the section sizes
558 matches the size in the aux entry, but apparently
559 that sometimes fails unexpectedly. */
563 esym
+= (sym
.n_numaux
+ 1) * symesz
;
564 sym_hash
+= sym
.n_numaux
+ 1;
567 /* If this is a non-traditional, non-relocatable link, try to
568 optimize the handling of any .stab/.stabstr sections. */
569 if (! info
->relocatable
570 && ! info
->traditional_format
571 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
572 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
576 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
580 bfd_size_type string_offset
= 0;
583 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
584 if (strncmp (".stab", stab
->name
, 5) == 0
586 || (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6]))))
588 struct coff_link_hash_table
*table
;
589 struct coff_section_tdata
*secdata
590 = coff_section_data (abfd
, stab
);
594 amt
= sizeof (struct coff_section_tdata
);
595 stab
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
596 if (stab
->used_by_bfd
== NULL
)
598 secdata
= coff_section_data (abfd
, stab
);
601 table
= coff_hash_table (info
);
603 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
612 obj_coff_keep_syms (abfd
) = keep_syms
;
617 obj_coff_keep_syms (abfd
) = keep_syms
;
621 /* Do the final link step. */
624 _bfd_coff_final_link (bfd
*abfd
,
625 struct bfd_link_info
*info
)
627 bfd_size_type symesz
;
628 struct coff_final_link_info finfo
;
629 bfd_boolean debug_merge_allocated
;
630 bfd_boolean long_section_names
;
632 struct bfd_link_order
*p
;
633 bfd_size_type max_sym_count
;
634 bfd_size_type max_lineno_count
;
635 bfd_size_type max_reloc_count
;
636 bfd_size_type max_output_reloc_count
;
637 bfd_size_type max_contents_size
;
638 file_ptr rel_filepos
;
640 file_ptr line_filepos
;
643 bfd_byte
*external_relocs
= NULL
;
644 char strbuf
[STRING_SIZE_SIZE
];
647 symesz
= bfd_coff_symesz (abfd
);
650 finfo
.output_bfd
= abfd
;
652 finfo
.section_info
= NULL
;
653 finfo
.last_file_index
= -1;
654 finfo
.last_bf_index
= -1;
655 finfo
.internal_syms
= NULL
;
656 finfo
.sec_ptrs
= NULL
;
657 finfo
.sym_indices
= NULL
;
658 finfo
.outsyms
= NULL
;
659 finfo
.linenos
= NULL
;
660 finfo
.contents
= NULL
;
661 finfo
.external_relocs
= NULL
;
662 finfo
.internal_relocs
= NULL
;
663 finfo
.global_to_static
= FALSE
;
664 debug_merge_allocated
= FALSE
;
666 coff_data (abfd
)->link_info
= info
;
668 finfo
.strtab
= _bfd_stringtab_init ();
669 if (finfo
.strtab
== NULL
)
672 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
674 debug_merge_allocated
= TRUE
;
676 /* Compute the file positions for all the sections. */
677 if (! abfd
->output_has_begun
)
679 if (! bfd_coff_compute_section_file_positions (abfd
))
683 /* Count the line numbers and relocation entries required for the
684 output file. Set the file positions for the relocs. */
685 rel_filepos
= obj_relocbase (abfd
);
686 relsz
= bfd_coff_relsz (abfd
);
687 max_contents_size
= 0;
688 max_lineno_count
= 0;
691 long_section_names
= FALSE
;
692 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
696 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
698 if (p
->type
== bfd_indirect_link_order
)
702 sec
= p
->u
.indirect
.section
;
704 /* Mark all sections which are to be included in the
705 link. This will normally be every section. We need
706 to do this so that we can identify any sections which
707 the linker has decided to not include. */
708 sec
->linker_mark
= TRUE
;
710 if (info
->strip
== strip_none
711 || info
->strip
== strip_some
)
712 o
->lineno_count
+= sec
->lineno_count
;
714 if (info
->relocatable
)
715 o
->reloc_count
+= sec
->reloc_count
;
717 if (sec
->rawsize
> max_contents_size
)
718 max_contents_size
= sec
->rawsize
;
719 if (sec
->size
> max_contents_size
)
720 max_contents_size
= sec
->size
;
721 if (sec
->lineno_count
> max_lineno_count
)
722 max_lineno_count
= sec
->lineno_count
;
723 if (sec
->reloc_count
> max_reloc_count
)
724 max_reloc_count
= sec
->reloc_count
;
726 else if (info
->relocatable
727 && (p
->type
== bfd_section_reloc_link_order
728 || p
->type
== bfd_symbol_reloc_link_order
))
731 if (o
->reloc_count
== 0)
735 o
->flags
|= SEC_RELOC
;
736 o
->rel_filepos
= rel_filepos
;
737 rel_filepos
+= o
->reloc_count
* relsz
;
738 /* In PE COFF, if there are at least 0xffff relocations an
739 extra relocation will be written out to encode the count. */
740 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
741 rel_filepos
+= relsz
;
744 if (bfd_coff_long_section_names (abfd
)
745 && strlen (o
->name
) > SCNNMLEN
)
747 /* This section has a long name which must go in the string
748 table. This must correspond to the code in
749 coff_write_object_contents which puts the string index
750 into the s_name field of the section header. That is why
751 we pass hash as FALSE. */
752 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, FALSE
, FALSE
)
753 == (bfd_size_type
) -1)
755 long_section_names
= TRUE
;
759 /* If doing a relocatable link, allocate space for the pointers we
761 if (info
->relocatable
)
765 /* We use section_count + 1, rather than section_count, because
766 the target_index fields are 1 based. */
767 amt
= abfd
->section_count
+ 1;
768 amt
*= sizeof (struct coff_link_section_info
);
769 finfo
.section_info
= bfd_malloc (amt
);
770 if (finfo
.section_info
== NULL
)
772 for (i
= 0; i
<= abfd
->section_count
; i
++)
774 finfo
.section_info
[i
].relocs
= NULL
;
775 finfo
.section_info
[i
].rel_hashes
= NULL
;
779 /* We now know the size of the relocs, so we can determine the file
780 positions of the line numbers. */
781 line_filepos
= rel_filepos
;
782 linesz
= bfd_coff_linesz (abfd
);
783 max_output_reloc_count
= 0;
784 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
786 if (o
->lineno_count
== 0)
790 o
->line_filepos
= line_filepos
;
791 line_filepos
+= o
->lineno_count
* linesz
;
794 if (o
->reloc_count
!= 0)
796 /* We don't know the indices of global symbols until we have
797 written out all the local symbols. For each section in
798 the output file, we keep an array of pointers to hash
799 table entries. Each entry in the array corresponds to a
800 reloc. When we find a reloc against a global symbol, we
801 set the corresponding entry in this array so that we can
802 fix up the symbol index after we have written out all the
805 Because of this problem, we also keep the relocs in
806 memory until the end of the link. This wastes memory,
807 but only when doing a relocatable link, which is not the
809 BFD_ASSERT (info
->relocatable
);
810 amt
= o
->reloc_count
;
811 amt
*= sizeof (struct internal_reloc
);
812 finfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
813 amt
= o
->reloc_count
;
814 amt
*= sizeof (struct coff_link_hash_entry
*);
815 finfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
816 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
817 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
820 if (o
->reloc_count
> max_output_reloc_count
)
821 max_output_reloc_count
= o
->reloc_count
;
824 /* Reset the reloc and lineno counts, so that we can use them to
825 count the number of entries we have output so far. */
830 obj_sym_filepos (abfd
) = line_filepos
;
832 /* Figure out the largest number of symbols in an input BFD. Take
833 the opportunity to clear the output_has_begun fields of all the
836 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
840 sub
->output_has_begun
= FALSE
;
841 sz
= obj_raw_syment_count (sub
);
842 if (sz
> max_sym_count
)
846 /* Allocate some buffers used while linking. */
847 amt
= max_sym_count
* sizeof (struct internal_syment
);
848 finfo
.internal_syms
= bfd_malloc (amt
);
849 amt
= max_sym_count
* sizeof (asection
*);
850 finfo
.sec_ptrs
= bfd_malloc (amt
);
851 amt
= max_sym_count
* sizeof (long);
852 finfo
.sym_indices
= bfd_malloc (amt
);
853 finfo
.outsyms
= bfd_malloc ((max_sym_count
+ 1) * symesz
);
854 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
855 finfo
.linenos
= bfd_malloc (amt
);
856 finfo
.contents
= bfd_malloc (max_contents_size
);
857 amt
= max_reloc_count
* relsz
;
858 finfo
.external_relocs
= bfd_malloc (amt
);
859 if (! info
->relocatable
)
861 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
862 finfo
.internal_relocs
= bfd_malloc (amt
);
864 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
865 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
866 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
867 || finfo
.outsyms
== NULL
868 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
869 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
870 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
871 || (! info
->relocatable
872 && finfo
.internal_relocs
== NULL
873 && max_reloc_count
> 0))
876 /* We now know the position of everything in the file, except that
877 we don't know the size of the symbol table and therefore we don't
878 know where the string table starts. We just build the string
879 table in memory as we go along. We process all the relocations
880 for a single input file at once. */
881 obj_raw_syment_count (abfd
) = 0;
883 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
885 if (! bfd_coff_start_final_link (abfd
, info
))
889 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
891 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
893 if (p
->type
== bfd_indirect_link_order
894 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
896 sub
= p
->u
.indirect
.section
->owner
;
897 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
899 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
901 sub
->output_has_begun
= TRUE
;
904 else if (p
->type
== bfd_section_reloc_link_order
905 || p
->type
== bfd_symbol_reloc_link_order
)
907 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
912 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
918 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
921 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
923 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
924 debug_merge_allocated
= FALSE
;
926 if (finfo
.internal_syms
!= NULL
)
928 free (finfo
.internal_syms
);
929 finfo
.internal_syms
= NULL
;
931 if (finfo
.sec_ptrs
!= NULL
)
933 free (finfo
.sec_ptrs
);
934 finfo
.sec_ptrs
= NULL
;
936 if (finfo
.sym_indices
!= NULL
)
938 free (finfo
.sym_indices
);
939 finfo
.sym_indices
= NULL
;
941 if (finfo
.linenos
!= NULL
)
943 free (finfo
.linenos
);
944 finfo
.linenos
= NULL
;
946 if (finfo
.contents
!= NULL
)
948 free (finfo
.contents
);
949 finfo
.contents
= NULL
;
951 if (finfo
.external_relocs
!= NULL
)
953 free (finfo
.external_relocs
);
954 finfo
.external_relocs
= NULL
;
956 if (finfo
.internal_relocs
!= NULL
)
958 free (finfo
.internal_relocs
);
959 finfo
.internal_relocs
= NULL
;
962 /* The value of the last C_FILE symbol is supposed to be the symbol
963 index of the first external symbol. Write it out again if
965 if (finfo
.last_file_index
!= -1
966 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
970 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
971 bfd_coff_swap_sym_out (abfd
, &finfo
.last_file
,
974 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
975 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
976 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
980 /* If doing task linking (ld --task-link) then make a pass through the
981 global symbols, writing out any that are defined, and making them
985 finfo
.failed
= FALSE
;
986 coff_link_hash_traverse (coff_hash_table (info
),
987 _bfd_coff_write_task_globals
, &finfo
);
992 /* Write out the global symbols. */
993 finfo
.failed
= FALSE
;
994 coff_link_hash_traverse (coff_hash_table (info
),
995 _bfd_coff_write_global_sym
, &finfo
);
999 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1000 if (finfo
.outsyms
!= NULL
)
1002 free (finfo
.outsyms
);
1003 finfo
.outsyms
= NULL
;
1006 if (info
->relocatable
&& max_output_reloc_count
> 0)
1008 /* Now that we have written out all the global symbols, we know
1009 the symbol indices to use for relocs against them, and we can
1010 finally write out the relocs. */
1011 amt
= max_output_reloc_count
* relsz
;
1012 external_relocs
= bfd_malloc (amt
);
1013 if (external_relocs
== NULL
)
1016 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1018 struct internal_reloc
*irel
;
1019 struct internal_reloc
*irelend
;
1020 struct coff_link_hash_entry
**rel_hash
;
1023 if (o
->reloc_count
== 0)
1026 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1027 irelend
= irel
+ o
->reloc_count
;
1028 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1029 erel
= external_relocs
;
1030 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1032 if (*rel_hash
!= NULL
)
1034 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1035 irel
->r_symndx
= (*rel_hash
)->indx
;
1037 bfd_coff_swap_reloc_out (abfd
, irel
, erel
);
1040 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0)
1042 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
1044 /* In PE COFF, write the count of relocs as the first
1045 reloc. The header overflow bit will be set
1047 struct internal_reloc incount
;
1048 bfd_byte
*excount
= (bfd_byte
*)bfd_malloc (relsz
);
1050 memset (&incount
, 0, sizeof (incount
));
1051 incount
.r_vaddr
= o
->reloc_count
+ 1;
1052 bfd_coff_swap_reloc_out (abfd
, (PTR
) &incount
, (PTR
) excount
);
1053 if (bfd_bwrite (excount
, relsz
, abfd
) != relsz
)
1054 /* We'll leak, but it's an error anyway. */
1058 if (bfd_bwrite (external_relocs
,
1059 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1060 != (bfd_size_type
) relsz
* o
->reloc_count
)
1064 free (external_relocs
);
1065 external_relocs
= NULL
;
1068 /* Free up the section information. */
1069 if (finfo
.section_info
!= NULL
)
1073 for (i
= 0; i
< abfd
->section_count
; i
++)
1075 if (finfo
.section_info
[i
].relocs
!= NULL
)
1076 free (finfo
.section_info
[i
].relocs
);
1077 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1078 free (finfo
.section_info
[i
].rel_hashes
);
1080 free (finfo
.section_info
);
1081 finfo
.section_info
= NULL
;
1084 /* If we have optimized stabs strings, output them. */
1085 if (coff_hash_table (info
)->stab_info
.stabstr
!= NULL
)
1087 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1091 /* Write out the string table. */
1092 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1096 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1097 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1100 #if STRING_SIZE_SIZE == 4
1102 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1105 #error Change H_PUT_32 above
1108 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1109 != STRING_SIZE_SIZE
)
1112 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1115 obj_coff_strings_written (abfd
) = TRUE
;
1118 _bfd_stringtab_free (finfo
.strtab
);
1120 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1121 not try to write out the symbols. */
1122 bfd_get_symcount (abfd
) = 0;
1127 if (debug_merge_allocated
)
1128 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1129 if (finfo
.strtab
!= NULL
)
1130 _bfd_stringtab_free (finfo
.strtab
);
1131 if (finfo
.section_info
!= NULL
)
1135 for (i
= 0; i
< abfd
->section_count
; i
++)
1137 if (finfo
.section_info
[i
].relocs
!= NULL
)
1138 free (finfo
.section_info
[i
].relocs
);
1139 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1140 free (finfo
.section_info
[i
].rel_hashes
);
1142 free (finfo
.section_info
);
1144 if (finfo
.internal_syms
!= NULL
)
1145 free (finfo
.internal_syms
);
1146 if (finfo
.sec_ptrs
!= NULL
)
1147 free (finfo
.sec_ptrs
);
1148 if (finfo
.sym_indices
!= NULL
)
1149 free (finfo
.sym_indices
);
1150 if (finfo
.outsyms
!= NULL
)
1151 free (finfo
.outsyms
);
1152 if (finfo
.linenos
!= NULL
)
1153 free (finfo
.linenos
);
1154 if (finfo
.contents
!= NULL
)
1155 free (finfo
.contents
);
1156 if (finfo
.external_relocs
!= NULL
)
1157 free (finfo
.external_relocs
);
1158 if (finfo
.internal_relocs
!= NULL
)
1159 free (finfo
.internal_relocs
);
1160 if (external_relocs
!= NULL
)
1161 free (external_relocs
);
1165 /* Parse out a -heap <reserved>,<commit> line. */
1168 dores_com (char *ptr
, bfd
*output_bfd
, int heap
)
1170 if (coff_data(output_bfd
)->pe
)
1172 int val
= strtoul (ptr
, &ptr
, 0);
1175 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1177 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1181 val
= strtoul (ptr
+1, &ptr
, 0);
1183 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1185 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1192 get_name (char *ptr
, char **dst
)
1197 while (*ptr
&& *ptr
!= ' ')
1203 /* Process any magic embedded commands in a section called .drectve. */
1206 process_embedded_commands (bfd
*output_bfd
,
1207 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1210 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1218 if (!bfd_malloc_and_get_section (abfd
, sec
, ©
))
1224 e
= copy
+ sec
->size
;
1226 for (s
= copy
; s
< e
; )
1233 if (strncmp (s
,"-attr", 5) == 0)
1245 s
= get_name (s
, &name
);
1246 s
= get_name (s
, &attribs
);
1268 asec
= bfd_get_section_by_name (abfd
, name
);
1272 asec
->flags
|= SEC_CODE
;
1274 asec
->flags
|= SEC_READONLY
;
1277 else if (strncmp (s
,"-heap", 5) == 0)
1278 s
= dores_com (s
+5, output_bfd
, 1);
1280 else if (strncmp (s
,"-stack", 6) == 0)
1281 s
= dores_com (s
+6, output_bfd
, 0);
1290 /* Place a marker against all symbols which are used by relocations.
1291 This marker can be picked up by the 'do we skip this symbol ?'
1292 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1296 mark_relocs (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1300 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1303 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1305 struct internal_reloc
* internal_relocs
;
1306 struct internal_reloc
* irel
;
1307 struct internal_reloc
* irelend
;
1309 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1311 /* Don't mark relocs in excluded sections. */
1312 if (a
->output_section
== bfd_abs_section_ptr
)
1315 /* Read in the relocs. */
1316 internal_relocs
= _bfd_coff_read_internal_relocs
1317 (input_bfd
, a
, FALSE
,
1318 finfo
->external_relocs
,
1319 finfo
->info
->relocatable
,
1320 (finfo
->info
->relocatable
1321 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1322 : finfo
->internal_relocs
)
1325 if (internal_relocs
== NULL
)
1328 irel
= internal_relocs
;
1329 irelend
= irel
+ a
->reloc_count
;
1331 /* Place a mark in the sym_indices array (whose entries have
1332 been initialised to 0) for all of the symbols that are used
1333 in the relocation table. This will then be picked up in the
1334 skip/don't-skip pass. */
1335 for (; irel
< irelend
; irel
++)
1336 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1340 /* Link an input file into the linker output file. This function
1341 handles all the sections and relocations of the input file at once. */
1344 _bfd_coff_link_input_bfd (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1346 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1347 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1349 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1351 bfd_boolean (*adjust_symndx
)
1352 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
1353 struct internal_reloc
*, bfd_boolean
*);
1355 const char *strings
;
1356 bfd_size_type syment_base
;
1357 bfd_boolean copy
, hash
;
1358 bfd_size_type isymesz
;
1359 bfd_size_type osymesz
;
1360 bfd_size_type linesz
;
1363 struct internal_syment
*isymp
;
1366 unsigned long output_index
;
1368 struct coff_link_hash_entry
**sym_hash
;
1371 /* Move all the symbols to the output file. */
1373 output_bfd
= finfo
->output_bfd
;
1375 syment_base
= obj_raw_syment_count (output_bfd
);
1376 isymesz
= bfd_coff_symesz (input_bfd
);
1377 osymesz
= bfd_coff_symesz (output_bfd
);
1378 linesz
= bfd_coff_linesz (input_bfd
);
1379 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1382 if (! finfo
->info
->keep_memory
)
1385 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1388 if (! _bfd_coff_get_external_symbols (input_bfd
))
1391 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1392 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1393 isymp
= finfo
->internal_syms
;
1394 secpp
= finfo
->sec_ptrs
;
1395 indexp
= finfo
->sym_indices
;
1396 output_index
= syment_base
;
1397 outsym
= finfo
->outsyms
;
1399 if (coff_data (output_bfd
)->pe
1400 && ! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1403 /* If we are going to perform relocations and also strip/discard some
1404 symbols then we must make sure that we do not strip/discard those
1405 symbols that are going to be involved in the relocations. */
1406 if (( finfo
->info
->strip
!= strip_none
1407 || finfo
->info
->discard
!= discard_none
)
1408 && finfo
->info
->relocatable
)
1410 /* Mark the symbol array as 'not-used'. */
1411 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1413 mark_relocs (finfo
, input_bfd
);
1416 while (esym
< esym_end
)
1418 struct internal_syment isym
;
1419 enum coff_symbol_classification classification
;
1422 bfd_boolean dont_skip_symbol
;
1425 bfd_coff_swap_sym_in (input_bfd
, esym
, isymp
);
1427 /* Make a copy of *isymp so that the relocate_section function
1428 always sees the original values. This is more reliable than
1429 always recomputing the symbol value even if we are stripping
1433 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1434 switch (classification
)
1438 case COFF_SYMBOL_GLOBAL
:
1439 case COFF_SYMBOL_PE_SECTION
:
1440 case COFF_SYMBOL_LOCAL
:
1441 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1443 case COFF_SYMBOL_COMMON
:
1444 *secpp
= bfd_com_section_ptr
;
1446 case COFF_SYMBOL_UNDEFINED
:
1447 *secpp
= bfd_und_section_ptr
;
1451 /* Extract the flag indicating if this symbol is used by a
1453 if ((finfo
->info
->strip
!= strip_none
1454 || finfo
->info
->discard
!= discard_none
)
1455 && finfo
->info
->relocatable
)
1456 dont_skip_symbol
= *indexp
;
1458 dont_skip_symbol
= FALSE
;
1464 add
= 1 + isym
.n_numaux
;
1466 /* If we are stripping all symbols, we want to skip this one. */
1467 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1472 switch (classification
)
1476 case COFF_SYMBOL_GLOBAL
:
1477 case COFF_SYMBOL_COMMON
:
1478 case COFF_SYMBOL_PE_SECTION
:
1479 /* This is a global symbol. Global symbols come at the
1480 end of the symbol table, so skip them for now.
1481 Locally defined function symbols, however, are an
1482 exception, and are not moved to the end. */
1484 if (! ISFCN (isym
.n_type
))
1488 case COFF_SYMBOL_UNDEFINED
:
1489 /* Undefined symbols are left for the end. */
1494 case COFF_SYMBOL_LOCAL
:
1495 /* This is a local symbol. Skip it if we are discarding
1497 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1503 #ifndef COFF_WITH_PE
1504 /* Skip section symbols for sections which are not going to be
1507 && dont_skip_symbol
== 0
1508 && isym
.n_sclass
== C_STAT
1509 && isym
.n_type
== T_NULL
1510 && isym
.n_numaux
> 0
1511 && (*secpp
)->output_section
== bfd_abs_section_ptr
)
1515 /* If we stripping debugging symbols, and this is a debugging
1516 symbol, then skip it. FIXME: gas sets the section to N_ABS
1517 for some types of debugging symbols; I don't know if this is
1518 a bug or not. In any case, we handle it here. */
1520 && finfo
->info
->strip
== strip_debugger
1521 && ! dont_skip_symbol
1522 && (isym
.n_scnum
== N_DEBUG
1523 || (isym
.n_scnum
== N_ABS
1524 && (isym
.n_sclass
== C_AUTO
1525 || isym
.n_sclass
== C_REG
1526 || isym
.n_sclass
== C_MOS
1527 || isym
.n_sclass
== C_MOE
1528 || isym
.n_sclass
== C_MOU
1529 || isym
.n_sclass
== C_ARG
1530 || isym
.n_sclass
== C_REGPARM
1531 || isym
.n_sclass
== C_FIELD
1532 || isym
.n_sclass
== C_EOS
))))
1535 /* If some symbols are stripped based on the name, work out the
1536 name and decide whether to skip this symbol. */
1538 && (finfo
->info
->strip
== strip_some
1539 || finfo
->info
->discard
== discard_l
))
1542 char buf
[SYMNMLEN
+ 1];
1544 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1548 if (! dont_skip_symbol
1549 && ((finfo
->info
->strip
== strip_some
1550 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
,
1553 && finfo
->info
->discard
== discard_l
1554 && bfd_is_local_label_name (input_bfd
, name
))))
1558 /* If this is an enum, struct, or union tag, see if we have
1559 already output an identical type. */
1561 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1562 && (isym
.n_sclass
== C_ENTAG
1563 || isym
.n_sclass
== C_STRTAG
1564 || isym
.n_sclass
== C_UNTAG
)
1565 && isym
.n_numaux
== 1)
1568 char buf
[SYMNMLEN
+ 1];
1569 struct coff_debug_merge_hash_entry
*mh
;
1570 struct coff_debug_merge_type
*mt
;
1571 union internal_auxent aux
;
1572 struct coff_debug_merge_element
**epp
;
1573 bfd_byte
*esl
, *eslend
;
1574 struct internal_syment
*islp
;
1577 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1581 /* Ignore fake names invented by compiler; treat them all as
1583 if (*name
== '~' || *name
== '.' || *name
== '$'
1584 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1585 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1588 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1593 /* Allocate memory to hold type information. If this turns
1594 out to be a duplicate, we pass this address to
1596 amt
= sizeof (struct coff_debug_merge_type
);
1597 mt
= bfd_alloc (input_bfd
, amt
);
1600 mt
->class = isym
.n_sclass
;
1602 /* Pick up the aux entry, which points to the end of the tag
1604 bfd_coff_swap_aux_in (input_bfd
, (esym
+ isymesz
),
1605 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1608 /* Gather the elements. */
1609 epp
= &mt
->elements
;
1610 mt
->elements
= NULL
;
1612 esl
= esym
+ 2 * isymesz
;
1613 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1614 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1615 while (esl
< eslend
)
1617 const char *elename
;
1618 char elebuf
[SYMNMLEN
+ 1];
1621 bfd_coff_swap_sym_in (input_bfd
, esl
, islp
);
1623 amt
= sizeof (struct coff_debug_merge_element
);
1624 *epp
= bfd_alloc (input_bfd
, amt
);
1628 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1630 if (elename
== NULL
)
1633 amt
= strlen (elename
) + 1;
1634 name_copy
= bfd_alloc (input_bfd
, amt
);
1635 if (name_copy
== NULL
)
1637 strcpy (name_copy
, elename
);
1639 (*epp
)->name
= name_copy
;
1640 (*epp
)->type
= islp
->n_type
;
1642 if (islp
->n_numaux
>= 1
1643 && islp
->n_type
!= T_NULL
1644 && islp
->n_sclass
!= C_EOS
)
1646 union internal_auxent eleaux
;
1649 bfd_coff_swap_aux_in (input_bfd
, (esl
+ isymesz
),
1650 islp
->n_type
, islp
->n_sclass
, 0,
1651 islp
->n_numaux
, &eleaux
);
1652 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1654 /* FIXME: If this tagndx entry refers to a symbol
1655 defined later in this file, we just ignore it.
1656 Handling this correctly would be tedious, and may
1661 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1664 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1665 if ((*epp
)->tagndx
< 0)
1669 epp
= &(*epp
)->next
;
1672 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1673 islp
+= islp
->n_numaux
+ 1;
1676 /* See if we already have a definition which matches this
1677 type. We always output the type if it has no elements,
1679 if (mt
->elements
== NULL
)
1680 bfd_release (input_bfd
, mt
);
1683 struct coff_debug_merge_type
*mtl
;
1685 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1687 struct coff_debug_merge_element
*me
, *mel
;
1689 if (mtl
->class != mt
->class)
1692 for (me
= mt
->elements
, mel
= mtl
->elements
;
1693 me
!= NULL
&& mel
!= NULL
;
1694 me
= me
->next
, mel
= mel
->next
)
1696 if (strcmp (me
->name
, mel
->name
) != 0
1697 || me
->type
!= mel
->type
1698 || me
->tagndx
!= mel
->tagndx
)
1702 if (me
== NULL
&& mel
== NULL
)
1706 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1708 /* This is the first definition of this type. */
1709 mt
->indx
= output_index
;
1710 mt
->next
= mh
->types
;
1715 /* This is a redefinition which can be merged. */
1716 bfd_release (input_bfd
, mt
);
1717 *indexp
= mtl
->indx
;
1718 add
= (eslend
- esym
) / isymesz
;
1724 /* We now know whether we are to skip this symbol or not. */
1727 /* Adjust the symbol in order to output it. */
1729 if (isym
._n
._n_n
._n_zeroes
== 0
1730 && isym
._n
._n_n
._n_offset
!= 0)
1735 /* This symbol has a long name. Enter it in the string
1736 table we are building. Note that we do not check
1737 bfd_coff_symname_in_debug. That is only true for
1738 XCOFF, and XCOFF requires different linking code
1740 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
1743 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1744 if (indx
== (bfd_size_type
) -1)
1746 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1749 switch (isym
.n_sclass
)
1765 /* The symbol value should not be modified. */
1769 if (obj_pe (input_bfd
)
1770 && strcmp (isym
.n_name
, ".bf") != 0
1771 && isym
.n_scnum
> 0)
1773 /* For PE, .lf and .ef get their value left alone,
1774 while .bf gets relocated. However, they all have
1775 "real" section numbers, and need to be moved into
1777 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1782 case C_LABEL
: /* Not completely sure about these 2 */
1791 /* Compute new symbol location. */
1792 if (isym
.n_scnum
> 0)
1794 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1795 isym
.n_value
+= (*secpp
)->output_offset
;
1796 if (! obj_pe (input_bfd
))
1797 isym
.n_value
-= (*secpp
)->vma
;
1798 if (! obj_pe (finfo
->output_bfd
))
1799 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1804 /* The value of a C_FILE symbol is the symbol index of
1805 the next C_FILE symbol. The value of the last C_FILE
1806 symbol is the symbol index to the first external
1807 symbol (actually, coff_renumber_symbols does not get
1808 this right--it just sets the value of the last C_FILE
1809 symbol to zero--and nobody has ever complained about
1810 it). We try to get this right, below, just before we
1811 write the symbols out, but in the general case we may
1812 have to write the symbol out twice. */
1813 if (finfo
->last_file_index
!= -1
1814 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1816 /* We must correct the value of the last C_FILE
1818 finfo
->last_file
.n_value
= output_index
;
1819 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1821 /* The last C_FILE symbol is in this input file. */
1822 bfd_coff_swap_sym_out (output_bfd
,
1825 + ((finfo
->last_file_index
1833 /* We have already written out the last C_FILE
1834 symbol. We need to write it out again. We
1835 borrow *outsym temporarily. */
1836 bfd_coff_swap_sym_out (output_bfd
,
1837 &finfo
->last_file
, outsym
);
1838 pos
= obj_sym_filepos (output_bfd
);
1839 pos
+= finfo
->last_file_index
* osymesz
;
1840 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1841 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1846 finfo
->last_file_index
= output_index
;
1847 finfo
->last_file
= isym
;
1851 /* If doing task linking, convert normal global function symbols to
1852 static functions. */
1853 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1854 isym
.n_sclass
= C_STAT
;
1856 /* Output the symbol. */
1857 bfd_coff_swap_sym_out (output_bfd
, &isym
, outsym
);
1859 *indexp
= output_index
;
1864 struct coff_link_hash_entry
*h
;
1866 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1868 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1871 /* This can happen if there were errors earlier in
1873 bfd_set_error (bfd_error_bad_value
);
1876 h
->indx
= output_index
;
1879 output_index
+= add
;
1880 outsym
+= add
* osymesz
;
1883 esym
+= add
* isymesz
;
1887 for (--add
; add
> 0; --add
)
1894 /* Fix up the aux entries. This must be done in a separate pass,
1895 because we don't know the correct symbol indices until we have
1896 already decided which symbols we are going to keep. */
1897 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1898 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1899 isymp
= finfo
->internal_syms
;
1900 indexp
= finfo
->sym_indices
;
1901 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1902 outsym
= finfo
->outsyms
;
1904 while (esym
< esym_end
)
1908 add
= 1 + isymp
->n_numaux
;
1911 || (bfd_size_type
) *indexp
< syment_base
)
1912 && (*sym_hash
== NULL
1913 || (*sym_hash
)->auxbfd
!= input_bfd
))
1914 esym
+= add
* isymesz
;
1917 struct coff_link_hash_entry
*h
;
1925 /* The m68k-motorola-sysv assembler will sometimes
1926 generate two symbols with the same name, but only one
1927 will have aux entries. */
1928 BFD_ASSERT (isymp
->n_numaux
== 0
1929 || h
->numaux
== isymp
->n_numaux
);
1937 /* Handle the aux entries. This handling is based on
1938 coff_pointerize_aux. I don't know if it always correct. */
1939 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1941 union internal_auxent aux
;
1942 union internal_auxent
*auxp
;
1948 bfd_coff_swap_aux_in (input_bfd
, esym
, isymp
->n_type
,
1949 isymp
->n_sclass
, i
, isymp
->n_numaux
, &aux
);
1953 if (isymp
->n_sclass
== C_FILE
)
1955 /* If this is a long filename, we must put it in the
1957 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1958 && auxp
->x_file
.x_n
.x_offset
!= 0)
1960 const char *filename
;
1963 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1964 >= STRING_SIZE_SIZE
);
1965 if (strings
== NULL
)
1967 strings
= _bfd_coff_read_string_table (input_bfd
);
1968 if (strings
== NULL
)
1971 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1972 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1974 if (indx
== (bfd_size_type
) -1)
1976 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1979 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1983 if (ISFCN (isymp
->n_type
)
1984 || ISTAG (isymp
->n_sclass
)
1985 || isymp
->n_sclass
== C_BLOCK
1986 || isymp
->n_sclass
== C_FCN
)
1988 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1990 && indx
< obj_raw_syment_count (input_bfd
))
1992 /* We look forward through the symbol for
1993 the index of the next symbol we are going
1994 to include. I don't know if this is
1996 while ((finfo
->sym_indices
[indx
] < 0
1997 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1999 && indx
< obj_raw_syment_count (input_bfd
))
2001 if (indx
>= obj_raw_syment_count (input_bfd
))
2002 indx
= output_index
;
2004 indx
= finfo
->sym_indices
[indx
];
2005 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2009 indx
= auxp
->x_sym
.x_tagndx
.l
;
2010 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2014 symindx
= finfo
->sym_indices
[indx
];
2016 auxp
->x_sym
.x_tagndx
.l
= 0;
2018 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2021 /* The .bf symbols are supposed to be linked through
2022 the endndx field. We need to carry this list
2023 across object files. */
2026 && isymp
->n_sclass
== C_FCN
2027 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2028 || isymp
->_n
._n_n
._n_offset
== 0)
2029 && isymp
->_n
._n_name
[0] == '.'
2030 && isymp
->_n
._n_name
[1] == 'b'
2031 && isymp
->_n
._n_name
[2] == 'f'
2032 && isymp
->_n
._n_name
[3] == '\0')
2034 if (finfo
->last_bf_index
!= -1)
2036 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2039 if ((bfd_size_type
) finfo
->last_bf_index
2044 /* The last .bf symbol is in this input
2045 file. This will only happen if the
2046 assembler did not set up the .bf
2047 endndx symbols correctly. */
2048 auxout
= (finfo
->outsyms
2049 + ((finfo
->last_bf_index
2053 bfd_coff_swap_aux_out (output_bfd
,
2064 /* We have already written out the last
2065 .bf aux entry. We need to write it
2066 out again. We borrow *outsym
2067 temporarily. FIXME: This case should
2069 bfd_coff_swap_aux_out (output_bfd
,
2075 pos
= obj_sym_filepos (output_bfd
);
2076 pos
+= finfo
->last_bf_index
* osymesz
;
2077 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2078 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2084 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2085 finfo
->last_bf_index
= -1;
2088 /* The endndx field of this aux entry must
2089 be updated with the symbol number of the
2091 finfo
->last_bf
= *auxp
;
2092 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2101 bfd_coff_swap_aux_out (output_bfd
, auxp
, isymp
->n_type
,
2102 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2116 /* Relocate the line numbers, unless we are stripping them. */
2117 if (finfo
->info
->strip
== strip_none
2118 || finfo
->info
->strip
== strip_some
)
2120 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2126 bfd_boolean skipping
;
2130 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2131 build_link_order in ldwrite.c will not have created a
2132 link order, which means that we will not have seen this
2133 input section in _bfd_coff_final_link, which means that
2134 we will not have allocated space for the line numbers of
2135 this section. I don't think line numbers can be
2136 meaningful for a section which does not have
2137 SEC_HAS_CONTENTS set, but, if they do, this must be
2139 if (o
->lineno_count
== 0
2140 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2143 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2144 || bfd_bread (finfo
->linenos
, linesz
* o
->lineno_count
,
2145 input_bfd
) != linesz
* o
->lineno_count
)
2148 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2149 eline
= finfo
->linenos
;
2150 oeline
= finfo
->linenos
;
2151 elineend
= eline
+ linesz
* o
->lineno_count
;
2153 for (; eline
< elineend
; eline
+= linesz
)
2155 struct internal_lineno iline
;
2157 bfd_coff_swap_lineno_in (input_bfd
, eline
, &iline
);
2159 if (iline
.l_lnno
!= 0)
2160 iline
.l_addr
.l_paddr
+= offset
;
2161 else if (iline
.l_addr
.l_symndx
>= 0
2162 && ((unsigned long) iline
.l_addr
.l_symndx
2163 < obj_raw_syment_count (input_bfd
)))
2167 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2171 /* These line numbers are attached to a symbol
2172 which we are stripping. We must discard the
2173 line numbers because reading them back with
2174 no associated symbol (or associating them all
2175 with symbol #0) will fail. We can't regain
2176 the space in the output file, but at least
2182 struct internal_syment is
;
2183 union internal_auxent ia
;
2185 /* Fix up the lnnoptr field in the aux entry of
2186 the symbol. It turns out that we can't do
2187 this when we modify the symbol aux entries,
2188 because gas sometimes screws up the lnnoptr
2189 field and makes it an offset from the start
2190 of the line numbers rather than an absolute
2192 bfd_coff_swap_sym_in (output_bfd
,
2194 + ((indx
- syment_base
)
2196 if ((ISFCN (is
.n_type
)
2197 || is
.n_sclass
== C_BLOCK
)
2198 && is
.n_numaux
>= 1)
2202 auxptr
= (finfo
->outsyms
2203 + ((indx
- syment_base
+ 1)
2205 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2206 is
.n_type
, is
.n_sclass
,
2207 0, is
.n_numaux
, &ia
);
2208 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2209 (o
->output_section
->line_filepos
2210 + o
->output_section
->lineno_count
* linesz
2211 + eline
- finfo
->linenos
);
2212 bfd_coff_swap_aux_out (output_bfd
, &ia
,
2213 is
.n_type
, is
.n_sclass
, 0,
2214 is
.n_numaux
, auxptr
);
2220 iline
.l_addr
.l_symndx
= indx
;
2225 bfd_coff_swap_lineno_out (output_bfd
, &iline
, oeline
);
2230 pos
= o
->output_section
->line_filepos
;
2231 pos
+= o
->output_section
->lineno_count
* linesz
;
2232 amt
= oeline
- finfo
->linenos
;
2233 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2234 || bfd_bwrite (finfo
->linenos
, amt
, output_bfd
) != amt
)
2237 o
->output_section
->lineno_count
+= amt
/ linesz
;
2241 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2242 symbol will be the first symbol in the next input file. In the
2243 normal case, this will save us from writing out the C_FILE symbol
2245 if (finfo
->last_file_index
!= -1
2246 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2248 finfo
->last_file
.n_value
= output_index
;
2249 bfd_coff_swap_sym_out (output_bfd
, &finfo
->last_file
,
2251 + ((finfo
->last_file_index
- syment_base
)
2255 /* Write the modified symbols to the output file. */
2256 if (outsym
> finfo
->outsyms
)
2261 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2262 amt
= outsym
- finfo
->outsyms
;
2263 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2264 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
2267 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2268 + (outsym
- finfo
->outsyms
) / osymesz
)
2271 obj_raw_syment_count (output_bfd
) = output_index
;
2274 /* Relocate the contents of each section. */
2275 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2276 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2279 struct coff_section_tdata
*secdata
;
2281 if (! o
->linker_mark
)
2282 /* This section was omitted from the link. */
2285 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
2288 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2289 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2291 if ((o
->flags
& SEC_RELOC
) != 0
2292 && o
->reloc_count
!= 0)
2294 (*_bfd_error_handler
)
2295 (_("%B: relocs in section `%A', but it has no contents"),
2297 bfd_set_error (bfd_error_no_contents
);
2304 secdata
= coff_section_data (input_bfd
, o
);
2305 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2306 contents
= secdata
->contents
;
2309 bfd_size_type x
= o
->rawsize
? o
->rawsize
: o
->size
;
2310 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
, 0, x
))
2312 contents
= finfo
->contents
;
2315 if ((o
->flags
& SEC_RELOC
) != 0)
2318 struct internal_reloc
*internal_relocs
;
2319 struct internal_reloc
*irel
;
2321 /* Read in the relocs. */
2322 target_index
= o
->output_section
->target_index
;
2323 internal_relocs
= (_bfd_coff_read_internal_relocs
2324 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
2325 finfo
->info
->relocatable
,
2326 (finfo
->info
->relocatable
2327 ? (finfo
->section_info
[target_index
].relocs
2328 + o
->output_section
->reloc_count
)
2329 : finfo
->internal_relocs
)));
2330 if (internal_relocs
== NULL
)
2333 /* Call processor specific code to relocate the section
2335 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2339 finfo
->internal_syms
,
2343 if (finfo
->info
->relocatable
)
2346 struct internal_reloc
*irelend
;
2347 struct coff_link_hash_entry
**rel_hash
;
2349 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2350 irel
= internal_relocs
;
2351 irelend
= irel
+ o
->reloc_count
;
2352 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2353 + o
->output_section
->reloc_count
);
2354 for (; irel
< irelend
; irel
++, rel_hash
++)
2356 struct coff_link_hash_entry
*h
;
2357 bfd_boolean adjusted
;
2361 /* Adjust the reloc address and symbol index. */
2362 irel
->r_vaddr
+= offset
;
2364 if (irel
->r_symndx
== -1)
2369 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2377 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2380 /* This is a global symbol. */
2382 irel
->r_symndx
= h
->indx
;
2385 /* This symbol is being written at the end
2386 of the file, and we do not yet know the
2387 symbol index. We save the pointer to the
2388 hash table entry in the rel_hash list.
2389 We set the indx field to -2 to indicate
2390 that this symbol must not be stripped. */
2399 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2401 irel
->r_symndx
= indx
;
2404 struct internal_syment
*is
;
2406 char buf
[SYMNMLEN
+ 1];
2408 /* This reloc is against a symbol we are
2409 stripping. This should have been handled
2410 by the 'dont_skip_symbol' code in the while
2411 loop at the top of this function. */
2412 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2414 name
= (_bfd_coff_internal_syment_name
2415 (input_bfd
, is
, buf
));
2419 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2420 (finfo
->info
, name
, input_bfd
, o
,
2427 o
->output_section
->reloc_count
+= o
->reloc_count
;
2431 /* Write out the modified section contents. */
2432 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2434 file_ptr loc
= o
->output_offset
* bfd_octets_per_byte (output_bfd
);
2435 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2436 contents
, loc
, o
->size
))
2441 if (! (_bfd_write_section_stabs
2442 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2443 o
, &secdata
->stab_info
, contents
)))
2448 if (! finfo
->info
->keep_memory
2449 && ! _bfd_coff_free_symbols (input_bfd
))
2455 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2458 _bfd_coff_write_global_sym (struct coff_link_hash_entry
*h
, void *data
)
2460 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2462 struct internal_syment isym
;
2463 bfd_size_type symesz
;
2467 output_bfd
= finfo
->output_bfd
;
2469 if (h
->root
.type
== bfd_link_hash_warning
)
2471 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2472 if (h
->root
.type
== bfd_link_hash_new
)
2480 && (finfo
->info
->strip
== strip_all
2481 || (finfo
->info
->strip
== strip_some
2482 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2483 h
->root
.root
.string
, FALSE
, FALSE
)
2487 switch (h
->root
.type
)
2490 case bfd_link_hash_new
:
2491 case bfd_link_hash_warning
:
2495 case bfd_link_hash_undefined
:
2496 case bfd_link_hash_undefweak
:
2497 isym
.n_scnum
= N_UNDEF
;
2501 case bfd_link_hash_defined
:
2502 case bfd_link_hash_defweak
:
2506 sec
= h
->root
.u
.def
.section
->output_section
;
2507 if (bfd_is_abs_section (sec
))
2508 isym
.n_scnum
= N_ABS
;
2510 isym
.n_scnum
= sec
->target_index
;
2511 isym
.n_value
= (h
->root
.u
.def
.value
2512 + h
->root
.u
.def
.section
->output_offset
);
2513 if (! obj_pe (finfo
->output_bfd
))
2514 isym
.n_value
+= sec
->vma
;
2518 case bfd_link_hash_common
:
2519 isym
.n_scnum
= N_UNDEF
;
2520 isym
.n_value
= h
->root
.u
.c
.size
;
2523 case bfd_link_hash_indirect
:
2524 /* Just ignore these. They can't be handled anyhow. */
2528 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2529 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2536 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2538 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2540 if (indx
== (bfd_size_type
) -1)
2542 finfo
->failed
= TRUE
;
2545 isym
._n
._n_n
._n_zeroes
= 0;
2546 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2549 isym
.n_sclass
= h
->class;
2550 isym
.n_type
= h
->type
;
2552 if (isym
.n_sclass
== C_NULL
)
2553 isym
.n_sclass
= C_EXT
;
2555 /* If doing task linking and this is the pass where we convert
2556 defined globals to statics, then do that conversion now. If the
2557 symbol is not being converted, just ignore it and it will be
2558 output during a later pass. */
2559 if (finfo
->global_to_static
)
2561 if (! IS_EXTERNAL (output_bfd
, isym
))
2564 isym
.n_sclass
= C_STAT
;
2567 /* When a weak symbol is not overridden by a strong one,
2568 turn it into an external symbol when not building a
2569 shared or relocatable object. */
2570 if (! finfo
->info
->shared
2571 && ! finfo
->info
->relocatable
2572 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2573 isym
.n_sclass
= C_EXT
;
2575 isym
.n_numaux
= h
->numaux
;
2577 bfd_coff_swap_sym_out (output_bfd
, &isym
, finfo
->outsyms
);
2579 symesz
= bfd_coff_symesz (output_bfd
);
2581 pos
= obj_sym_filepos (output_bfd
);
2582 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2583 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2584 || bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2586 finfo
->failed
= TRUE
;
2590 h
->indx
= obj_raw_syment_count (output_bfd
);
2592 ++obj_raw_syment_count (output_bfd
);
2594 /* Write out any associated aux entries. Most of the aux entries
2595 will have been modified in _bfd_coff_link_input_bfd. We have to
2596 handle section aux entries here, now that we have the final
2597 relocation and line number counts. */
2598 for (i
= 0; i
< isym
.n_numaux
; i
++)
2600 union internal_auxent
*auxp
;
2604 /* Look for a section aux entry here using the same tests that
2605 coff_swap_aux_out uses. */
2607 && (isym
.n_sclass
== C_STAT
2608 || isym
.n_sclass
== C_HIDDEN
)
2609 && isym
.n_type
== T_NULL
2610 && (h
->root
.type
== bfd_link_hash_defined
2611 || h
->root
.type
== bfd_link_hash_defweak
))
2615 sec
= h
->root
.u
.def
.section
->output_section
;
2618 auxp
->x_scn
.x_scnlen
= sec
->size
;
2620 /* For PE, an overflow on the final link reportedly does
2621 not matter. FIXME: Why not? */
2622 if (sec
->reloc_count
> 0xffff
2623 && (! obj_pe (output_bfd
)
2624 || finfo
->info
->relocatable
))
2625 (*_bfd_error_handler
)
2626 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2627 bfd_get_filename (output_bfd
),
2628 bfd_get_section_name (output_bfd
, sec
),
2631 if (sec
->lineno_count
> 0xffff
2632 && (! obj_pe (output_bfd
)
2633 || finfo
->info
->relocatable
))
2634 (*_bfd_error_handler
)
2635 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2636 bfd_get_filename (output_bfd
),
2637 bfd_get_section_name (output_bfd
, sec
),
2640 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2641 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2642 auxp
->x_scn
.x_checksum
= 0;
2643 auxp
->x_scn
.x_associated
= 0;
2644 auxp
->x_scn
.x_comdat
= 0;
2648 bfd_coff_swap_aux_out (output_bfd
, auxp
, isym
.n_type
,
2649 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2651 if (bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2653 finfo
->failed
= TRUE
;
2656 ++obj_raw_syment_count (output_bfd
);
2662 /* Write out task global symbols, converting them to statics. Called
2663 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2664 the dirty work, if the symbol we are processing needs conversion. */
2667 _bfd_coff_write_task_globals (struct coff_link_hash_entry
*h
, void *data
)
2669 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2670 bfd_boolean rtnval
= TRUE
;
2671 bfd_boolean save_global_to_static
;
2673 if (h
->root
.type
== bfd_link_hash_warning
)
2674 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2678 switch (h
->root
.type
)
2680 case bfd_link_hash_defined
:
2681 case bfd_link_hash_defweak
:
2682 save_global_to_static
= finfo
->global_to_static
;
2683 finfo
->global_to_static
= TRUE
;
2684 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2685 finfo
->global_to_static
= save_global_to_static
;
2694 /* Handle a link order which is supposed to generate a reloc. */
2697 _bfd_coff_reloc_link_order (bfd
*output_bfd
,
2698 struct coff_final_link_info
*finfo
,
2699 asection
*output_section
,
2700 struct bfd_link_order
*link_order
)
2702 reloc_howto_type
*howto
;
2703 struct internal_reloc
*irel
;
2704 struct coff_link_hash_entry
**rel_hash_ptr
;
2706 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2709 bfd_set_error (bfd_error_bad_value
);
2713 if (link_order
->u
.reloc
.p
->addend
!= 0)
2717 bfd_reloc_status_type rstat
;
2721 size
= bfd_get_reloc_size (howto
);
2722 buf
= bfd_zmalloc (size
);
2726 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2727 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,\
2734 case bfd_reloc_outofrange
:
2736 case bfd_reloc_overflow
:
2737 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2739 (link_order
->type
== bfd_section_reloc_link_order
2740 ? bfd_section_name (output_bfd
,
2741 link_order
->u
.reloc
.p
->u
.section
)
2742 : link_order
->u
.reloc
.p
->u
.name
),
2743 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2744 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2751 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2752 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
2759 /* Store the reloc information in the right place. It will get
2760 swapped and written out at the end of the final_link routine. */
2761 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2762 + output_section
->reloc_count
);
2763 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2764 + output_section
->reloc_count
);
2766 memset (irel
, 0, sizeof (struct internal_reloc
));
2767 *rel_hash_ptr
= NULL
;
2769 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2771 if (link_order
->type
== bfd_section_reloc_link_order
)
2773 /* We need to somehow locate a symbol in the right section. The
2774 symbol must either have a value of zero, or we must adjust
2775 the addend by the value of the symbol. FIXME: Write this
2776 when we need it. The old linker couldn't handle this anyhow. */
2778 *rel_hash_ptr
= NULL
;
2783 struct coff_link_hash_entry
*h
;
2785 h
= ((struct coff_link_hash_entry
*)
2786 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2787 link_order
->u
.reloc
.p
->u
.name
,
2788 FALSE
, FALSE
, TRUE
));
2792 irel
->r_symndx
= h
->indx
;
2795 /* Set the index to -2 to force this symbol to get
2804 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2805 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2806 (asection
*) NULL
, (bfd_vma
) 0)))
2812 /* FIXME: Is this always right? */
2813 irel
->r_type
= howto
->type
;
2815 /* r_size is only used on the RS/6000, which needs its own linker
2816 routines anyhow. r_extern is only used for ECOFF. */
2818 /* FIXME: What is the right value for r_offset? Is zero OK? */
2819 ++output_section
->reloc_count
;
2824 /* A basic reloc handling routine which may be used by processors with
2828 _bfd_coff_generic_relocate_section (bfd
*output_bfd
,
2829 struct bfd_link_info
*info
,
2831 asection
*input_section
,
2833 struct internal_reloc
*relocs
,
2834 struct internal_syment
*syms
,
2835 asection
**sections
)
2837 struct internal_reloc
*rel
;
2838 struct internal_reloc
*relend
;
2841 relend
= rel
+ input_section
->reloc_count
;
2842 for (; rel
< relend
; rel
++)
2845 struct coff_link_hash_entry
*h
;
2846 struct internal_syment
*sym
;
2849 reloc_howto_type
*howto
;
2850 bfd_reloc_status_type rstat
;
2852 symndx
= rel
->r_symndx
;
2860 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2862 (*_bfd_error_handler
)
2863 ("%B: illegal symbol index %ld in relocs", input_bfd
, symndx
);
2868 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2869 sym
= syms
+ symndx
;
2872 /* COFF treats common symbols in one of two ways. Either the
2873 size of the symbol is included in the section contents, or it
2874 is not. We assume that the size is not included, and force
2875 the rtype_to_howto function to adjust the addend as needed. */
2876 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2877 addend
= - sym
->n_value
;
2881 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2886 /* If we are doing a relocatable link, then we can just ignore
2887 a PC relative reloc that is pcrel_offset. It will already
2888 have the correct value. If this is not a relocatable link,
2889 then we should ignore the symbol value. */
2890 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2892 if (info
->relocatable
)
2894 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2895 addend
+= sym
->n_value
;
2906 sec
= bfd_abs_section_ptr
;
2911 sec
= sections
[symndx
];
2912 val
= (sec
->output_section
->vma
2913 + sec
->output_offset
2915 if (! obj_pe (input_bfd
))
2921 if (h
->root
.type
== bfd_link_hash_defined
2922 || h
->root
.type
== bfd_link_hash_defweak
)
2924 /* Defined weak symbols are a GNU extension. */
2927 sec
= h
->root
.u
.def
.section
;
2928 val
= (h
->root
.u
.def
.value
2929 + sec
->output_section
->vma
2930 + sec
->output_offset
);
2933 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2935 if (h
->class == C_NT_WEAK
&& h
->numaux
== 1)
2937 /* See _Microsoft Portable Executable and Common Object
2938 * File Format Specification_, section 5.5.3.
2939 * Note that weak symbols without aux records are a GNU
2941 * FIXME: All weak externals are treated as having
2942 * characteristics IMAGE_WEAK_EXTERN_SEARCH_LIBRARY (2).
2943 * There are no known uses of the other two types of
2947 struct coff_link_hash_entry
*h2
=
2948 input_bfd
->tdata
.coff_obj_data
->sym_hashes
[
2949 h
->aux
->x_sym
.x_tagndx
.l
];
2951 sec
= h2
->root
.u
.def
.section
;
2952 val
= h2
->root
.u
.def
.value
+ sec
->output_section
->vma
2953 + sec
->output_offset
;
2956 /* This is a GNU extension. */
2960 else if (! info
->relocatable
)
2962 if (! ((*info
->callbacks
->undefined_symbol
)
2963 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2964 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
2969 if (info
->base_file
)
2971 /* Emit a reloc if the backend thinks it needs it. */
2972 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2974 /* Relocation to a symbol in a section which isn't
2975 absolute. We output the address here to a file.
2976 This file is then read by dlltool when generating the
2977 reloc section. Note that the base file is not
2978 portable between systems. We write out a long here,
2979 and dlltool reads in a long. */
2980 long addr
= (rel
->r_vaddr
2981 - input_section
->vma
2982 + input_section
->output_offset
2983 + input_section
->output_section
->vma
);
2984 if (coff_data (output_bfd
)->pe
)
2985 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2986 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2989 bfd_set_error (bfd_error_system_call
);
2995 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2997 rel
->r_vaddr
- input_section
->vma
,
3006 case bfd_reloc_outofrange
:
3007 (*_bfd_error_handler
)
3008 (_("%B: bad reloc address 0x%lx in section `%A'"),
3009 input_bfd
, input_section
, (unsigned long) rel
->r_vaddr
);
3011 case bfd_reloc_overflow
:
3014 char buf
[SYMNMLEN
+ 1];
3022 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3027 if (! ((*info
->callbacks
->reloc_overflow
)
3028 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3029 (bfd_vma
) 0, input_bfd
, input_section
,
3030 rel
->r_vaddr
- input_section
->vma
)))