1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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
*,
100 memset (&table
->stab_info
, 0, sizeof (table
->stab_info
));
101 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
104 /* Create a COFF linker hash table. */
106 struct bfd_link_hash_table
*
107 _bfd_coff_link_hash_table_create (bfd
*abfd
)
109 struct coff_link_hash_table
*ret
;
110 bfd_size_type amt
= sizeof (struct coff_link_hash_table
);
112 ret
= bfd_malloc (amt
);
116 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
117 _bfd_coff_link_hash_newfunc
,
118 sizeof (struct coff_link_hash_entry
)))
121 return (struct bfd_link_hash_table
*) NULL
;
126 /* Create an entry in a COFF debug merge hash table. */
128 struct bfd_hash_entry
*
129 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
130 struct bfd_hash_table
*table
,
133 struct coff_debug_merge_hash_entry
*ret
=
134 (struct coff_debug_merge_hash_entry
*) entry
;
136 /* Allocate the structure if it has not already been allocated by a
138 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
139 ret
= ((struct coff_debug_merge_hash_entry
*)
140 bfd_hash_allocate (table
,
141 sizeof (struct coff_debug_merge_hash_entry
)));
142 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
143 return (struct bfd_hash_entry
*) ret
;
145 /* Call the allocation method of the superclass. */
146 ret
= ((struct coff_debug_merge_hash_entry
*)
147 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
148 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
150 /* Set local fields. */
154 return (struct bfd_hash_entry
*) ret
;
157 /* Given a COFF BFD, add symbols to the global hash table as
161 _bfd_coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
163 switch (bfd_get_format (abfd
))
166 return coff_link_add_object_symbols (abfd
, info
);
168 return _bfd_generic_link_add_archive_symbols
169 (abfd
, info
, coff_link_check_archive_element
);
171 bfd_set_error (bfd_error_wrong_format
);
176 /* Add symbols from a COFF object file. */
179 coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
181 if (! _bfd_coff_get_external_symbols (abfd
))
183 if (! coff_link_add_symbols (abfd
, info
))
186 if (! info
->keep_memory
187 && ! _bfd_coff_free_symbols (abfd
))
193 /* Look through the symbols to see if this object file should be
194 included in the link. */
197 coff_link_check_ar_symbols (bfd
*abfd
,
198 struct bfd_link_info
*info
,
199 bfd_boolean
*pneeded
)
201 bfd_size_type symesz
;
207 symesz
= bfd_coff_symesz (abfd
);
208 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
209 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
210 while (esym
< esym_end
)
212 struct internal_syment sym
;
213 enum coff_symbol_classification classification
;
215 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
217 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
218 if (classification
== COFF_SYMBOL_GLOBAL
219 || classification
== COFF_SYMBOL_COMMON
)
222 char buf
[SYMNMLEN
+ 1];
223 struct bfd_link_hash_entry
*h
;
225 /* This symbol is externally visible, and is defined by this
227 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
230 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
234 && info
->pei386_auto_import
235 && !strncmp (name
,"__imp_", 6))
236 h
= bfd_link_hash_lookup (info
->hash
, name
+ 6, FALSE
, FALSE
, TRUE
);
238 /* We are only interested in symbols that are currently
239 undefined. If a symbol is currently known to be common,
240 COFF linkers do not bring in an object file which defines
242 if (h
!= (struct bfd_link_hash_entry
*) NULL
243 && h
->type
== bfd_link_hash_undefined
)
245 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
252 esym
+= (sym
.n_numaux
+ 1) * symesz
;
255 /* We do not need this object file. */
259 /* Check a single archive element to see if we need to include it in
260 the link. *PNEEDED is set according to whether this element is
261 needed in the link or not. This is called via
262 _bfd_generic_link_add_archive_symbols. */
265 coff_link_check_archive_element (bfd
*abfd
,
266 struct bfd_link_info
*info
,
267 bfd_boolean
*pneeded
)
269 if (! _bfd_coff_get_external_symbols (abfd
))
272 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
276 && ! coff_link_add_symbols (abfd
, info
))
279 if ((! info
->keep_memory
|| ! *pneeded
)
280 && ! _bfd_coff_free_symbols (abfd
))
286 /* Add all the symbols from an object file to the hash table. */
289 coff_link_add_symbols (bfd
*abfd
,
290 struct bfd_link_info
*info
)
292 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
293 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
294 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
295 bfd_boolean keep_syms
;
296 bfd_boolean default_copy
;
297 bfd_size_type symcount
;
298 struct coff_link_hash_entry
**sym_hash
;
299 bfd_size_type symesz
;
304 /* Keep the symbols during this function, in case the linker needs
305 to read the generic symbols in order to report an error message. */
306 keep_syms
= obj_coff_keep_syms (abfd
);
307 obj_coff_keep_syms (abfd
) = TRUE
;
309 if (info
->keep_memory
)
310 default_copy
= FALSE
;
314 symcount
= obj_raw_syment_count (abfd
);
316 /* We keep a list of the linker hash table entries that correspond
317 to particular symbols. */
318 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
319 sym_hash
= bfd_zalloc (abfd
, amt
);
320 if (sym_hash
== NULL
&& symcount
!= 0)
322 obj_coff_sym_hashes (abfd
) = sym_hash
;
324 symesz
= bfd_coff_symesz (abfd
);
325 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
326 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
327 esym_end
= esym
+ symcount
* symesz
;
328 while (esym
< esym_end
)
330 struct internal_syment sym
;
331 enum coff_symbol_classification classification
;
334 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
336 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
337 if (classification
!= COFF_SYMBOL_LOCAL
)
340 char buf
[SYMNMLEN
+ 1];
346 /* This symbol is externally visible. */
348 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
352 /* We must copy the name into memory if we got it from the
353 syment itself, rather than the string table. */
355 if (sym
._n
._n_n
._n_zeroes
!= 0
356 || sym
._n
._n_n
._n_offset
== 0)
361 switch (classification
)
366 case COFF_SYMBOL_GLOBAL
:
367 flags
= BSF_EXPORT
| BSF_GLOBAL
;
368 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
370 value
-= section
->vma
;
373 case COFF_SYMBOL_UNDEFINED
:
375 section
= bfd_und_section_ptr
;
378 case COFF_SYMBOL_COMMON
:
380 section
= bfd_com_section_ptr
;
383 case COFF_SYMBOL_PE_SECTION
:
384 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
385 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
389 if (IS_WEAK_EXTERNAL (abfd
, sym
))
394 /* In the PE format, section symbols actually refer to the
395 start of the output section. We handle them specially
397 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
399 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
400 name
, FALSE
, copy
, FALSE
);
401 if (*sym_hash
!= NULL
)
403 if (((*sym_hash
)->coff_link_hash_flags
404 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
405 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
406 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
407 (*_bfd_error_handler
)
408 ("Warning: symbol `%s' is both section and non-section",
415 /* The Microsoft Visual C compiler does string pooling by
416 hashing the constants to an internal symbol name, and
417 relying on the linker comdat support to discard
418 duplicate names. However, if one string is a literal and
419 one is a data initializer, one will end up in the .data
420 section and one will end up in the .rdata section. The
421 Microsoft linker will combine them into the .data
422 section, which seems to be wrong since it might cause the
425 As long as there are no external references to the
426 symbols, which there shouldn't be, we can treat the .data
427 and .rdata instances as separate symbols. The comdat
428 code in the linker will do the appropriate merging. Here
429 we avoid getting a multiple definition error for one of
430 these special symbols.
432 FIXME: I don't think this will work in the case where
433 there are two object files which use the constants as a
434 literal and two object files which use it as a data
435 initializer. One or the other of the second object files
436 is going to wind up with an inappropriate reference. */
438 && (classification
== COFF_SYMBOL_GLOBAL
439 || classification
== COFF_SYMBOL_PE_SECTION
)
440 && coff_section_data (abfd
, section
) != NULL
441 && coff_section_data (abfd
, section
)->comdat
!= NULL
442 && strncmp (name
, "??_", 3) == 0
443 && strcmp (name
, coff_section_data (abfd
, section
)->comdat
->name
) == 0)
445 if (*sym_hash
== NULL
)
446 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
447 name
, FALSE
, copy
, FALSE
);
448 if (*sym_hash
!= NULL
449 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
450 && coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
!= NULL
451 && strcmp (coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
->name
,
452 coff_section_data (abfd
, section
)->comdat
->name
) == 0)
458 if (! (bfd_coff_link_add_one_symbol
459 (info
, abfd
, name
, flags
, section
, value
,
460 (const char *) NULL
, copy
, FALSE
,
461 (struct bfd_link_hash_entry
**) sym_hash
)))
465 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
466 (*sym_hash
)->coff_link_hash_flags
|=
467 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
469 /* Limit the alignment of a common symbol to the possible
470 alignment of a section. There is no point to permitting
471 a higher alignment for a common symbol: we can not
472 guarantee it, and it may cause us to allocate extra space
473 in the common section. */
474 if (section
== bfd_com_section_ptr
475 && (*sym_hash
)->root
.type
== bfd_link_hash_common
476 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
477 > bfd_coff_default_section_alignment_power (abfd
)))
478 (*sym_hash
)->root
.u
.c
.p
->alignment_power
479 = bfd_coff_default_section_alignment_power (abfd
);
481 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
483 /* If we don't have any symbol information currently in
484 the hash table, or if we are looking at a symbol
485 definition, then update the symbol class and type in
487 if (((*sym_hash
)->class == C_NULL
488 && (*sym_hash
)->type
== T_NULL
)
491 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
492 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
494 (*sym_hash
)->class = sym
.n_sclass
;
495 if (sym
.n_type
!= T_NULL
)
497 /* We want to warn if the type changed, but not
498 if it changed from an unspecified type.
499 Testing the whole type byte may work, but the
500 change from (e.g.) a function of unspecified
501 type to function of known type also wants to
503 if ((*sym_hash
)->type
!= T_NULL
504 && (*sym_hash
)->type
!= sym
.n_type
505 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
506 && (BTYPE ((*sym_hash
)->type
) == T_NULL
507 || BTYPE (sym
.n_type
) == T_NULL
)))
508 (*_bfd_error_handler
)
509 (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
510 abfd
, name
, (*sym_hash
)->type
, sym
.n_type
);
512 /* We don't want to change from a meaningful
513 base type to a null one, but if we know
514 nothing, take what little we might now know. */
515 if (BTYPE (sym
.n_type
) != T_NULL
516 || (*sym_hash
)->type
== T_NULL
)
517 (*sym_hash
)->type
= sym
.n_type
;
519 (*sym_hash
)->auxbfd
= abfd
;
520 if (sym
.n_numaux
!= 0)
522 union internal_auxent
*alloc
;
525 union internal_auxent
*iaux
;
527 (*sym_hash
)->numaux
= sym
.n_numaux
;
528 alloc
= ((union internal_auxent
*)
529 bfd_hash_allocate (&info
->hash
->table
,
531 * sizeof (*alloc
))));
534 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
536 i
++, eaux
+= symesz
, iaux
++)
537 bfd_coff_swap_aux_in (abfd
, eaux
, sym
.n_type
,
538 sym
.n_sclass
, (int) i
,
540 (*sym_hash
)->aux
= alloc
;
545 if (classification
== COFF_SYMBOL_PE_SECTION
546 && (*sym_hash
)->numaux
!= 0)
548 /* Some PE sections (such as .bss) have a zero size in
549 the section header, but a non-zero size in the AUX
550 record. Correct that here.
552 FIXME: This is not at all the right place to do this.
553 For example, it won't help objdump. This needs to be
554 done when we swap in the section header. */
555 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
556 if (section
->size
== 0)
557 section
->size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
559 /* FIXME: We could test whether the section sizes
560 matches the size in the aux entry, but apparently
561 that sometimes fails unexpectedly. */
565 esym
+= (sym
.n_numaux
+ 1) * symesz
;
566 sym_hash
+= sym
.n_numaux
+ 1;
569 /* If this is a non-traditional, non-relocatable link, try to
570 optimize the handling of any .stab/.stabstr sections. */
571 if (! info
->relocatable
572 && ! info
->traditional_format
573 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
574 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
578 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
582 bfd_size_type string_offset
= 0;
585 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
586 if (strncmp (".stab", stab
->name
, 5) == 0
588 || (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6]))))
590 struct coff_link_hash_table
*table
;
591 struct coff_section_tdata
*secdata
592 = coff_section_data (abfd
, stab
);
596 amt
= sizeof (struct coff_section_tdata
);
597 stab
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
598 if (stab
->used_by_bfd
== NULL
)
600 secdata
= coff_section_data (abfd
, stab
);
603 table
= coff_hash_table (info
);
605 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
614 obj_coff_keep_syms (abfd
) = keep_syms
;
619 obj_coff_keep_syms (abfd
) = keep_syms
;
623 /* Do the final link step. */
626 _bfd_coff_final_link (bfd
*abfd
,
627 struct bfd_link_info
*info
)
629 bfd_size_type symesz
;
630 struct coff_final_link_info finfo
;
631 bfd_boolean debug_merge_allocated
;
632 bfd_boolean long_section_names
;
634 struct bfd_link_order
*p
;
635 bfd_size_type max_sym_count
;
636 bfd_size_type max_lineno_count
;
637 bfd_size_type max_reloc_count
;
638 bfd_size_type max_output_reloc_count
;
639 bfd_size_type max_contents_size
;
640 file_ptr rel_filepos
;
642 file_ptr line_filepos
;
645 bfd_byte
*external_relocs
= NULL
;
646 char strbuf
[STRING_SIZE_SIZE
];
649 symesz
= bfd_coff_symesz (abfd
);
652 finfo
.output_bfd
= abfd
;
654 finfo
.section_info
= NULL
;
655 finfo
.last_file_index
= -1;
656 finfo
.last_bf_index
= -1;
657 finfo
.internal_syms
= NULL
;
658 finfo
.sec_ptrs
= NULL
;
659 finfo
.sym_indices
= NULL
;
660 finfo
.outsyms
= NULL
;
661 finfo
.linenos
= NULL
;
662 finfo
.contents
= NULL
;
663 finfo
.external_relocs
= NULL
;
664 finfo
.internal_relocs
= NULL
;
665 finfo
.global_to_static
= FALSE
;
666 debug_merge_allocated
= FALSE
;
668 coff_data (abfd
)->link_info
= info
;
670 finfo
.strtab
= _bfd_stringtab_init ();
671 if (finfo
.strtab
== NULL
)
674 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
676 debug_merge_allocated
= TRUE
;
678 /* Compute the file positions for all the sections. */
679 if (! abfd
->output_has_begun
)
681 if (! bfd_coff_compute_section_file_positions (abfd
))
685 /* Count the line numbers and relocation entries required for the
686 output file. Set the file positions for the relocs. */
687 rel_filepos
= obj_relocbase (abfd
);
688 relsz
= bfd_coff_relsz (abfd
);
689 max_contents_size
= 0;
690 max_lineno_count
= 0;
693 long_section_names
= FALSE
;
694 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
698 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
700 if (p
->type
== bfd_indirect_link_order
)
704 sec
= p
->u
.indirect
.section
;
706 /* Mark all sections which are to be included in the
707 link. This will normally be every section. We need
708 to do this so that we can identify any sections which
709 the linker has decided to not include. */
710 sec
->linker_mark
= TRUE
;
712 if (info
->strip
== strip_none
713 || info
->strip
== strip_some
)
714 o
->lineno_count
+= sec
->lineno_count
;
716 if (info
->relocatable
)
717 o
->reloc_count
+= sec
->reloc_count
;
719 if (sec
->rawsize
> max_contents_size
)
720 max_contents_size
= sec
->rawsize
;
721 if (sec
->size
> max_contents_size
)
722 max_contents_size
= sec
->size
;
723 if (sec
->lineno_count
> max_lineno_count
)
724 max_lineno_count
= sec
->lineno_count
;
725 if (sec
->reloc_count
> max_reloc_count
)
726 max_reloc_count
= sec
->reloc_count
;
728 else if (info
->relocatable
729 && (p
->type
== bfd_section_reloc_link_order
730 || p
->type
== bfd_symbol_reloc_link_order
))
733 if (o
->reloc_count
== 0)
737 o
->flags
|= SEC_RELOC
;
738 o
->rel_filepos
= rel_filepos
;
739 rel_filepos
+= o
->reloc_count
* relsz
;
740 /* In PE COFF, if there are at least 0xffff relocations an
741 extra relocation will be written out to encode the count. */
742 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
743 rel_filepos
+= relsz
;
746 if (bfd_coff_long_section_names (abfd
)
747 && strlen (o
->name
) > SCNNMLEN
)
749 /* This section has a long name which must go in the string
750 table. This must correspond to the code in
751 coff_write_object_contents which puts the string index
752 into the s_name field of the section header. That is why
753 we pass hash as FALSE. */
754 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, FALSE
, FALSE
)
755 == (bfd_size_type
) -1)
757 long_section_names
= TRUE
;
761 /* If doing a relocatable link, allocate space for the pointers we
763 if (info
->relocatable
)
767 /* We use section_count + 1, rather than section_count, because
768 the target_index fields are 1 based. */
769 amt
= abfd
->section_count
+ 1;
770 amt
*= sizeof (struct coff_link_section_info
);
771 finfo
.section_info
= bfd_malloc (amt
);
772 if (finfo
.section_info
== NULL
)
774 for (i
= 0; i
<= abfd
->section_count
; i
++)
776 finfo
.section_info
[i
].relocs
= NULL
;
777 finfo
.section_info
[i
].rel_hashes
= NULL
;
781 /* We now know the size of the relocs, so we can determine the file
782 positions of the line numbers. */
783 line_filepos
= rel_filepos
;
784 linesz
= bfd_coff_linesz (abfd
);
785 max_output_reloc_count
= 0;
786 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
788 if (o
->lineno_count
== 0)
792 o
->line_filepos
= line_filepos
;
793 line_filepos
+= o
->lineno_count
* linesz
;
796 if (o
->reloc_count
!= 0)
798 /* We don't know the indices of global symbols until we have
799 written out all the local symbols. For each section in
800 the output file, we keep an array of pointers to hash
801 table entries. Each entry in the array corresponds to a
802 reloc. When we find a reloc against a global symbol, we
803 set the corresponding entry in this array so that we can
804 fix up the symbol index after we have written out all the
807 Because of this problem, we also keep the relocs in
808 memory until the end of the link. This wastes memory,
809 but only when doing a relocatable link, which is not the
811 BFD_ASSERT (info
->relocatable
);
812 amt
= o
->reloc_count
;
813 amt
*= sizeof (struct internal_reloc
);
814 finfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
815 amt
= o
->reloc_count
;
816 amt
*= sizeof (struct coff_link_hash_entry
*);
817 finfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
818 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
819 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
822 if (o
->reloc_count
> max_output_reloc_count
)
823 max_output_reloc_count
= o
->reloc_count
;
826 /* Reset the reloc and lineno counts, so that we can use them to
827 count the number of entries we have output so far. */
832 obj_sym_filepos (abfd
) = line_filepos
;
834 /* Figure out the largest number of symbols in an input BFD. Take
835 the opportunity to clear the output_has_begun fields of all the
838 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
842 sub
->output_has_begun
= FALSE
;
843 sz
= obj_raw_syment_count (sub
);
844 if (sz
> max_sym_count
)
848 /* Allocate some buffers used while linking. */
849 amt
= max_sym_count
* sizeof (struct internal_syment
);
850 finfo
.internal_syms
= bfd_malloc (amt
);
851 amt
= max_sym_count
* sizeof (asection
*);
852 finfo
.sec_ptrs
= bfd_malloc (amt
);
853 amt
= max_sym_count
* sizeof (long);
854 finfo
.sym_indices
= bfd_malloc (amt
);
855 finfo
.outsyms
= bfd_malloc ((max_sym_count
+ 1) * symesz
);
856 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
857 finfo
.linenos
= bfd_malloc (amt
);
858 finfo
.contents
= bfd_malloc (max_contents_size
);
859 amt
= max_reloc_count
* relsz
;
860 finfo
.external_relocs
= bfd_malloc (amt
);
861 if (! info
->relocatable
)
863 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
864 finfo
.internal_relocs
= bfd_malloc (amt
);
866 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
867 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
868 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
869 || finfo
.outsyms
== NULL
870 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
871 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
872 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
873 || (! info
->relocatable
874 && finfo
.internal_relocs
== NULL
875 && max_reloc_count
> 0))
878 /* We now know the position of everything in the file, except that
879 we don't know the size of the symbol table and therefore we don't
880 know where the string table starts. We just build the string
881 table in memory as we go along. We process all the relocations
882 for a single input file at once. */
883 obj_raw_syment_count (abfd
) = 0;
885 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
887 if (! bfd_coff_start_final_link (abfd
, info
))
891 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
893 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
895 if (p
->type
== bfd_indirect_link_order
896 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
898 sub
= p
->u
.indirect
.section
->owner
;
899 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
901 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
903 sub
->output_has_begun
= TRUE
;
906 else if (p
->type
== bfd_section_reloc_link_order
907 || p
->type
== bfd_symbol_reloc_link_order
)
909 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
914 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
920 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
923 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
925 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
926 debug_merge_allocated
= FALSE
;
928 if (finfo
.internal_syms
!= NULL
)
930 free (finfo
.internal_syms
);
931 finfo
.internal_syms
= NULL
;
933 if (finfo
.sec_ptrs
!= NULL
)
935 free (finfo
.sec_ptrs
);
936 finfo
.sec_ptrs
= NULL
;
938 if (finfo
.sym_indices
!= NULL
)
940 free (finfo
.sym_indices
);
941 finfo
.sym_indices
= NULL
;
943 if (finfo
.linenos
!= NULL
)
945 free (finfo
.linenos
);
946 finfo
.linenos
= NULL
;
948 if (finfo
.contents
!= NULL
)
950 free (finfo
.contents
);
951 finfo
.contents
= NULL
;
953 if (finfo
.external_relocs
!= NULL
)
955 free (finfo
.external_relocs
);
956 finfo
.external_relocs
= NULL
;
958 if (finfo
.internal_relocs
!= NULL
)
960 free (finfo
.internal_relocs
);
961 finfo
.internal_relocs
= NULL
;
964 /* The value of the last C_FILE symbol is supposed to be the symbol
965 index of the first external symbol. Write it out again if
967 if (finfo
.last_file_index
!= -1
968 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
972 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
973 bfd_coff_swap_sym_out (abfd
, &finfo
.last_file
,
976 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
977 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
978 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
982 /* If doing task linking (ld --task-link) then make a pass through the
983 global symbols, writing out any that are defined, and making them
987 finfo
.failed
= FALSE
;
988 coff_link_hash_traverse (coff_hash_table (info
),
989 _bfd_coff_write_task_globals
, &finfo
);
994 /* Write out the global symbols. */
995 finfo
.failed
= FALSE
;
996 coff_link_hash_traverse (coff_hash_table (info
),
997 _bfd_coff_write_global_sym
, &finfo
);
1001 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1002 if (finfo
.outsyms
!= NULL
)
1004 free (finfo
.outsyms
);
1005 finfo
.outsyms
= NULL
;
1008 if (info
->relocatable
&& max_output_reloc_count
> 0)
1010 /* Now that we have written out all the global symbols, we know
1011 the symbol indices to use for relocs against them, and we can
1012 finally write out the relocs. */
1013 amt
= max_output_reloc_count
* relsz
;
1014 external_relocs
= bfd_malloc (amt
);
1015 if (external_relocs
== NULL
)
1018 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1020 struct internal_reloc
*irel
;
1021 struct internal_reloc
*irelend
;
1022 struct coff_link_hash_entry
**rel_hash
;
1025 if (o
->reloc_count
== 0)
1028 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1029 irelend
= irel
+ o
->reloc_count
;
1030 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1031 erel
= external_relocs
;
1032 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1034 if (*rel_hash
!= NULL
)
1036 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1037 irel
->r_symndx
= (*rel_hash
)->indx
;
1039 bfd_coff_swap_reloc_out (abfd
, irel
, erel
);
1042 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0)
1044 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
1046 /* In PE COFF, write the count of relocs as the first
1047 reloc. The header overflow bit will be set
1049 struct internal_reloc incount
;
1050 bfd_byte
*excount
= (bfd_byte
*)bfd_malloc (relsz
);
1052 memset (&incount
, 0, sizeof (incount
));
1053 incount
.r_vaddr
= o
->reloc_count
+ 1;
1054 bfd_coff_swap_reloc_out (abfd
, (PTR
) &incount
, (PTR
) excount
);
1055 if (bfd_bwrite (excount
, relsz
, abfd
) != relsz
)
1056 /* We'll leak, but it's an error anyway. */
1060 if (bfd_bwrite (external_relocs
,
1061 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1062 != (bfd_size_type
) relsz
* o
->reloc_count
)
1066 free (external_relocs
);
1067 external_relocs
= NULL
;
1070 /* Free up the section information. */
1071 if (finfo
.section_info
!= NULL
)
1075 for (i
= 0; i
< abfd
->section_count
; i
++)
1077 if (finfo
.section_info
[i
].relocs
!= NULL
)
1078 free (finfo
.section_info
[i
].relocs
);
1079 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1080 free (finfo
.section_info
[i
].rel_hashes
);
1082 free (finfo
.section_info
);
1083 finfo
.section_info
= NULL
;
1086 /* If we have optimized stabs strings, output them. */
1087 if (coff_hash_table (info
)->stab_info
.stabstr
!= NULL
)
1089 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1093 /* Write out the string table. */
1094 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1098 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1099 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1102 #if STRING_SIZE_SIZE == 4
1104 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1107 #error Change H_PUT_32 above
1110 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1111 != STRING_SIZE_SIZE
)
1114 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1117 obj_coff_strings_written (abfd
) = TRUE
;
1120 _bfd_stringtab_free (finfo
.strtab
);
1122 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1123 not try to write out the symbols. */
1124 bfd_get_symcount (abfd
) = 0;
1129 if (debug_merge_allocated
)
1130 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1131 if (finfo
.strtab
!= NULL
)
1132 _bfd_stringtab_free (finfo
.strtab
);
1133 if (finfo
.section_info
!= NULL
)
1137 for (i
= 0; i
< abfd
->section_count
; i
++)
1139 if (finfo
.section_info
[i
].relocs
!= NULL
)
1140 free (finfo
.section_info
[i
].relocs
);
1141 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1142 free (finfo
.section_info
[i
].rel_hashes
);
1144 free (finfo
.section_info
);
1146 if (finfo
.internal_syms
!= NULL
)
1147 free (finfo
.internal_syms
);
1148 if (finfo
.sec_ptrs
!= NULL
)
1149 free (finfo
.sec_ptrs
);
1150 if (finfo
.sym_indices
!= NULL
)
1151 free (finfo
.sym_indices
);
1152 if (finfo
.outsyms
!= NULL
)
1153 free (finfo
.outsyms
);
1154 if (finfo
.linenos
!= NULL
)
1155 free (finfo
.linenos
);
1156 if (finfo
.contents
!= NULL
)
1157 free (finfo
.contents
);
1158 if (finfo
.external_relocs
!= NULL
)
1159 free (finfo
.external_relocs
);
1160 if (finfo
.internal_relocs
!= NULL
)
1161 free (finfo
.internal_relocs
);
1162 if (external_relocs
!= NULL
)
1163 free (external_relocs
);
1167 /* Parse out a -heap <reserved>,<commit> line. */
1170 dores_com (char *ptr
, bfd
*output_bfd
, int heap
)
1172 if (coff_data(output_bfd
)->pe
)
1174 int val
= strtoul (ptr
, &ptr
, 0);
1177 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1179 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1183 val
= strtoul (ptr
+1, &ptr
, 0);
1185 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1187 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1194 get_name (char *ptr
, char **dst
)
1199 while (*ptr
&& *ptr
!= ' ')
1205 /* Process any magic embedded commands in a section called .drectve. */
1208 process_embedded_commands (bfd
*output_bfd
,
1209 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1212 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1220 if (!bfd_malloc_and_get_section (abfd
, sec
, ©
))
1226 e
= (char *) copy
+ sec
->size
;
1228 for (s
= (char *) copy
; s
< e
; )
1235 if (strncmp (s
, "-attr", 5) == 0)
1245 s
= get_name (s
, &name
);
1246 s
= get_name (s
, &attribs
);
1266 asec
= bfd_get_section_by_name (abfd
, name
);
1270 asec
->flags
|= SEC_CODE
;
1272 asec
->flags
|= SEC_READONLY
;
1275 else if (strncmp (s
,"-heap", 5) == 0)
1276 s
= dores_com (s
+5, output_bfd
, 1);
1278 else if (strncmp (s
,"-stack", 6) == 0)
1279 s
= dores_com (s
+6, output_bfd
, 0);
1288 /* Place a marker against all symbols which are used by relocations.
1289 This marker can be picked up by the 'do we skip this symbol ?'
1290 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1294 mark_relocs (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1298 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1301 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1303 struct internal_reloc
* internal_relocs
;
1304 struct internal_reloc
* irel
;
1305 struct internal_reloc
* irelend
;
1307 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1309 /* Don't mark relocs in excluded sections. */
1310 if (a
->output_section
== bfd_abs_section_ptr
)
1313 /* Read in the relocs. */
1314 internal_relocs
= _bfd_coff_read_internal_relocs
1315 (input_bfd
, a
, FALSE
,
1316 finfo
->external_relocs
,
1317 finfo
->info
->relocatable
,
1318 (finfo
->info
->relocatable
1319 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1320 : finfo
->internal_relocs
)
1323 if (internal_relocs
== NULL
)
1326 irel
= internal_relocs
;
1327 irelend
= irel
+ a
->reloc_count
;
1329 /* Place a mark in the sym_indices array (whose entries have
1330 been initialised to 0) for all of the symbols that are used
1331 in the relocation table. This will then be picked up in the
1332 skip/don't-skip pass. */
1333 for (; irel
< irelend
; irel
++)
1334 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1338 /* Link an input file into the linker output file. This function
1339 handles all the sections and relocations of the input file at once. */
1342 _bfd_coff_link_input_bfd (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1344 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1345 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1346 bfd_boolean (*adjust_symndx
)
1347 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
1348 struct internal_reloc
*, bfd_boolean
*);
1350 const char *strings
;
1351 bfd_size_type syment_base
;
1352 bfd_boolean copy
, hash
;
1353 bfd_size_type isymesz
;
1354 bfd_size_type osymesz
;
1355 bfd_size_type linesz
;
1358 struct internal_syment
*isymp
;
1361 unsigned long output_index
;
1363 struct coff_link_hash_entry
**sym_hash
;
1366 /* Move all the symbols to the output file. */
1368 output_bfd
= finfo
->output_bfd
;
1370 syment_base
= obj_raw_syment_count (output_bfd
);
1371 isymesz
= bfd_coff_symesz (input_bfd
);
1372 osymesz
= bfd_coff_symesz (output_bfd
);
1373 linesz
= bfd_coff_linesz (input_bfd
);
1374 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1377 if (! finfo
->info
->keep_memory
)
1380 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1383 if (! _bfd_coff_get_external_symbols (input_bfd
))
1386 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1387 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1388 isymp
= finfo
->internal_syms
;
1389 secpp
= finfo
->sec_ptrs
;
1390 indexp
= finfo
->sym_indices
;
1391 output_index
= syment_base
;
1392 outsym
= finfo
->outsyms
;
1394 if (coff_data (output_bfd
)->pe
1395 && ! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1398 /* If we are going to perform relocations and also strip/discard some
1399 symbols then we must make sure that we do not strip/discard those
1400 symbols that are going to be involved in the relocations. */
1401 if (( finfo
->info
->strip
!= strip_none
1402 || finfo
->info
->discard
!= discard_none
)
1403 && finfo
->info
->relocatable
)
1405 /* Mark the symbol array as 'not-used'. */
1406 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1408 mark_relocs (finfo
, input_bfd
);
1411 while (esym
< esym_end
)
1413 struct internal_syment isym
;
1414 enum coff_symbol_classification classification
;
1417 bfd_boolean dont_skip_symbol
;
1420 bfd_coff_swap_sym_in (input_bfd
, esym
, isymp
);
1422 /* Make a copy of *isymp so that the relocate_section function
1423 always sees the original values. This is more reliable than
1424 always recomputing the symbol value even if we are stripping
1428 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1429 switch (classification
)
1433 case COFF_SYMBOL_GLOBAL
:
1434 case COFF_SYMBOL_PE_SECTION
:
1435 case COFF_SYMBOL_LOCAL
:
1436 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1438 case COFF_SYMBOL_COMMON
:
1439 *secpp
= bfd_com_section_ptr
;
1441 case COFF_SYMBOL_UNDEFINED
:
1442 *secpp
= bfd_und_section_ptr
;
1446 /* Extract the flag indicating if this symbol is used by a
1448 if ((finfo
->info
->strip
!= strip_none
1449 || finfo
->info
->discard
!= discard_none
)
1450 && finfo
->info
->relocatable
)
1451 dont_skip_symbol
= *indexp
;
1453 dont_skip_symbol
= FALSE
;
1459 add
= 1 + isym
.n_numaux
;
1461 /* If we are stripping all symbols, we want to skip this one. */
1462 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1467 switch (classification
)
1471 case COFF_SYMBOL_GLOBAL
:
1472 case COFF_SYMBOL_COMMON
:
1473 case COFF_SYMBOL_PE_SECTION
:
1474 /* This is a global symbol. Global symbols come at the
1475 end of the symbol table, so skip them for now.
1476 Locally defined function symbols, however, are an
1477 exception, and are not moved to the end. */
1479 if (! ISFCN (isym
.n_type
))
1483 case COFF_SYMBOL_UNDEFINED
:
1484 /* Undefined symbols are left for the end. */
1489 case COFF_SYMBOL_LOCAL
:
1490 /* This is a local symbol. Skip it if we are discarding
1492 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1498 #ifndef COFF_WITH_PE
1499 /* Skip section symbols for sections which are not going to be
1502 && dont_skip_symbol
== 0
1503 && isym
.n_sclass
== C_STAT
1504 && isym
.n_type
== T_NULL
1505 && isym
.n_numaux
> 0
1506 && (*secpp
)->output_section
== bfd_abs_section_ptr
)
1510 /* If we stripping debugging symbols, and this is a debugging
1511 symbol, then skip it. FIXME: gas sets the section to N_ABS
1512 for some types of debugging symbols; I don't know if this is
1513 a bug or not. In any case, we handle it here. */
1515 && finfo
->info
->strip
== strip_debugger
1516 && ! dont_skip_symbol
1517 && (isym
.n_scnum
== N_DEBUG
1518 || (isym
.n_scnum
== N_ABS
1519 && (isym
.n_sclass
== C_AUTO
1520 || isym
.n_sclass
== C_REG
1521 || isym
.n_sclass
== C_MOS
1522 || isym
.n_sclass
== C_MOE
1523 || isym
.n_sclass
== C_MOU
1524 || isym
.n_sclass
== C_ARG
1525 || isym
.n_sclass
== C_REGPARM
1526 || isym
.n_sclass
== C_FIELD
1527 || isym
.n_sclass
== C_EOS
))))
1530 /* If some symbols are stripped based on the name, work out the
1531 name and decide whether to skip this symbol. */
1533 && (finfo
->info
->strip
== strip_some
1534 || finfo
->info
->discard
== discard_l
))
1537 char buf
[SYMNMLEN
+ 1];
1539 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1543 if (! dont_skip_symbol
1544 && ((finfo
->info
->strip
== strip_some
1545 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
,
1548 && finfo
->info
->discard
== discard_l
1549 && bfd_is_local_label_name (input_bfd
, name
))))
1553 /* If this is an enum, struct, or union tag, see if we have
1554 already output an identical type. */
1556 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1557 && (isym
.n_sclass
== C_ENTAG
1558 || isym
.n_sclass
== C_STRTAG
1559 || isym
.n_sclass
== C_UNTAG
)
1560 && isym
.n_numaux
== 1)
1563 char buf
[SYMNMLEN
+ 1];
1564 struct coff_debug_merge_hash_entry
*mh
;
1565 struct coff_debug_merge_type
*mt
;
1566 union internal_auxent aux
;
1567 struct coff_debug_merge_element
**epp
;
1568 bfd_byte
*esl
, *eslend
;
1569 struct internal_syment
*islp
;
1572 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1576 /* Ignore fake names invented by compiler; treat them all as
1578 if (*name
== '~' || *name
== '.' || *name
== '$'
1579 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1580 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1583 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1588 /* Allocate memory to hold type information. If this turns
1589 out to be a duplicate, we pass this address to
1591 amt
= sizeof (struct coff_debug_merge_type
);
1592 mt
= bfd_alloc (input_bfd
, amt
);
1595 mt
->class = isym
.n_sclass
;
1597 /* Pick up the aux entry, which points to the end of the tag
1599 bfd_coff_swap_aux_in (input_bfd
, (esym
+ isymesz
),
1600 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1603 /* Gather the elements. */
1604 epp
= &mt
->elements
;
1605 mt
->elements
= NULL
;
1607 esl
= esym
+ 2 * isymesz
;
1608 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1609 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1610 while (esl
< eslend
)
1612 const char *elename
;
1613 char elebuf
[SYMNMLEN
+ 1];
1616 bfd_coff_swap_sym_in (input_bfd
, esl
, islp
);
1618 amt
= sizeof (struct coff_debug_merge_element
);
1619 *epp
= bfd_alloc (input_bfd
, amt
);
1623 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1625 if (elename
== NULL
)
1628 amt
= strlen (elename
) + 1;
1629 name_copy
= bfd_alloc (input_bfd
, amt
);
1630 if (name_copy
== NULL
)
1632 strcpy (name_copy
, elename
);
1634 (*epp
)->name
= name_copy
;
1635 (*epp
)->type
= islp
->n_type
;
1637 if (islp
->n_numaux
>= 1
1638 && islp
->n_type
!= T_NULL
1639 && islp
->n_sclass
!= C_EOS
)
1641 union internal_auxent eleaux
;
1644 bfd_coff_swap_aux_in (input_bfd
, (esl
+ isymesz
),
1645 islp
->n_type
, islp
->n_sclass
, 0,
1646 islp
->n_numaux
, &eleaux
);
1647 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1649 /* FIXME: If this tagndx entry refers to a symbol
1650 defined later in this file, we just ignore it.
1651 Handling this correctly would be tedious, and may
1656 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1659 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1660 if ((*epp
)->tagndx
< 0)
1664 epp
= &(*epp
)->next
;
1667 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1668 islp
+= islp
->n_numaux
+ 1;
1671 /* See if we already have a definition which matches this
1672 type. We always output the type if it has no elements,
1674 if (mt
->elements
== NULL
)
1675 bfd_release (input_bfd
, mt
);
1678 struct coff_debug_merge_type
*mtl
;
1680 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1682 struct coff_debug_merge_element
*me
, *mel
;
1684 if (mtl
->class != mt
->class)
1687 for (me
= mt
->elements
, mel
= mtl
->elements
;
1688 me
!= NULL
&& mel
!= NULL
;
1689 me
= me
->next
, mel
= mel
->next
)
1691 if (strcmp (me
->name
, mel
->name
) != 0
1692 || me
->type
!= mel
->type
1693 || me
->tagndx
!= mel
->tagndx
)
1697 if (me
== NULL
&& mel
== NULL
)
1701 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1703 /* This is the first definition of this type. */
1704 mt
->indx
= output_index
;
1705 mt
->next
= mh
->types
;
1710 /* This is a redefinition which can be merged. */
1711 bfd_release (input_bfd
, mt
);
1712 *indexp
= mtl
->indx
;
1713 add
= (eslend
- esym
) / isymesz
;
1719 /* We now know whether we are to skip this symbol or not. */
1722 /* Adjust the symbol in order to output it. */
1724 if (isym
._n
._n_n
._n_zeroes
== 0
1725 && isym
._n
._n_n
._n_offset
!= 0)
1730 /* This symbol has a long name. Enter it in the string
1731 table we are building. Note that we do not check
1732 bfd_coff_symname_in_debug. That is only true for
1733 XCOFF, and XCOFF requires different linking code
1735 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
1738 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1739 if (indx
== (bfd_size_type
) -1)
1741 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1744 switch (isym
.n_sclass
)
1760 /* The symbol value should not be modified. */
1764 if (obj_pe (input_bfd
)
1765 && strcmp (isym
.n_name
, ".bf") != 0
1766 && isym
.n_scnum
> 0)
1768 /* For PE, .lf and .ef get their value left alone,
1769 while .bf gets relocated. However, they all have
1770 "real" section numbers, and need to be moved into
1772 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1777 case C_LABEL
: /* Not completely sure about these 2 */
1786 /* Compute new symbol location. */
1787 if (isym
.n_scnum
> 0)
1789 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1790 isym
.n_value
+= (*secpp
)->output_offset
;
1791 if (! obj_pe (input_bfd
))
1792 isym
.n_value
-= (*secpp
)->vma
;
1793 if (! obj_pe (finfo
->output_bfd
))
1794 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1799 /* The value of a C_FILE symbol is the symbol index of
1800 the next C_FILE symbol. The value of the last C_FILE
1801 symbol is the symbol index to the first external
1802 symbol (actually, coff_renumber_symbols does not get
1803 this right--it just sets the value of the last C_FILE
1804 symbol to zero--and nobody has ever complained about
1805 it). We try to get this right, below, just before we
1806 write the symbols out, but in the general case we may
1807 have to write the symbol out twice. */
1808 if (finfo
->last_file_index
!= -1
1809 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1811 /* We must correct the value of the last C_FILE
1813 finfo
->last_file
.n_value
= output_index
;
1814 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1816 /* The last C_FILE symbol is in this input file. */
1817 bfd_coff_swap_sym_out (output_bfd
,
1820 + ((finfo
->last_file_index
1828 /* We have already written out the last C_FILE
1829 symbol. We need to write it out again. We
1830 borrow *outsym temporarily. */
1831 bfd_coff_swap_sym_out (output_bfd
,
1832 &finfo
->last_file
, outsym
);
1833 pos
= obj_sym_filepos (output_bfd
);
1834 pos
+= finfo
->last_file_index
* osymesz
;
1835 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1836 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1841 finfo
->last_file_index
= output_index
;
1842 finfo
->last_file
= isym
;
1846 /* If doing task linking, convert normal global function symbols to
1847 static functions. */
1848 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1849 isym
.n_sclass
= C_STAT
;
1851 /* Output the symbol. */
1852 bfd_coff_swap_sym_out (output_bfd
, &isym
, outsym
);
1854 *indexp
= output_index
;
1859 struct coff_link_hash_entry
*h
;
1861 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1863 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1866 /* This can happen if there were errors earlier in
1868 bfd_set_error (bfd_error_bad_value
);
1871 h
->indx
= output_index
;
1874 output_index
+= add
;
1875 outsym
+= add
* osymesz
;
1878 esym
+= add
* isymesz
;
1882 for (--add
; add
> 0; --add
)
1889 /* Fix up the aux entries. This must be done in a separate pass,
1890 because we don't know the correct symbol indices until we have
1891 already decided which symbols we are going to keep. */
1892 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1893 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1894 isymp
= finfo
->internal_syms
;
1895 indexp
= finfo
->sym_indices
;
1896 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1897 outsym
= finfo
->outsyms
;
1899 while (esym
< esym_end
)
1903 add
= 1 + isymp
->n_numaux
;
1906 || (bfd_size_type
) *indexp
< syment_base
)
1907 && (*sym_hash
== NULL
1908 || (*sym_hash
)->auxbfd
!= input_bfd
))
1909 esym
+= add
* isymesz
;
1912 struct coff_link_hash_entry
*h
;
1920 /* The m68k-motorola-sysv assembler will sometimes
1921 generate two symbols with the same name, but only one
1922 will have aux entries. */
1923 BFD_ASSERT (isymp
->n_numaux
== 0
1925 || h
->numaux
== isymp
->n_numaux
);
1933 /* Handle the aux entries. This handling is based on
1934 coff_pointerize_aux. I don't know if it always correct. */
1935 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1937 union internal_auxent aux
;
1938 union internal_auxent
*auxp
;
1940 if (h
!= NULL
&& h
->aux
!= NULL
&& (h
->numaux
> i
))
1944 bfd_coff_swap_aux_in (input_bfd
, esym
, isymp
->n_type
,
1945 isymp
->n_sclass
, i
, isymp
->n_numaux
, &aux
);
1949 if (isymp
->n_sclass
== C_FILE
)
1951 /* If this is a long filename, we must put it in the
1953 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1954 && auxp
->x_file
.x_n
.x_offset
!= 0)
1956 const char *filename
;
1959 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1960 >= STRING_SIZE_SIZE
);
1961 if (strings
== NULL
)
1963 strings
= _bfd_coff_read_string_table (input_bfd
);
1964 if (strings
== NULL
)
1967 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1968 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1970 if (indx
== (bfd_size_type
) -1)
1972 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1975 else if ((isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1976 && isymp
->n_sclass
!= C_NT_WEAK
)
1980 if (ISFCN (isymp
->n_type
)
1981 || ISTAG (isymp
->n_sclass
)
1982 || isymp
->n_sclass
== C_BLOCK
1983 || isymp
->n_sclass
== C_FCN
)
1985 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1987 && indx
< obj_raw_syment_count (input_bfd
))
1989 /* We look forward through the symbol for
1990 the index of the next symbol we are going
1991 to include. I don't know if this is
1993 while ((finfo
->sym_indices
[indx
] < 0
1994 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1996 && indx
< obj_raw_syment_count (input_bfd
))
1998 if (indx
>= obj_raw_syment_count (input_bfd
))
1999 indx
= output_index
;
2001 indx
= finfo
->sym_indices
[indx
];
2002 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2006 indx
= auxp
->x_sym
.x_tagndx
.l
;
2007 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2011 symindx
= finfo
->sym_indices
[indx
];
2013 auxp
->x_sym
.x_tagndx
.l
= 0;
2015 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2018 /* The .bf symbols are supposed to be linked through
2019 the endndx field. We need to carry this list
2020 across object files. */
2023 && isymp
->n_sclass
== C_FCN
2024 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2025 || isymp
->_n
._n_n
._n_offset
== 0)
2026 && isymp
->_n
._n_name
[0] == '.'
2027 && isymp
->_n
._n_name
[1] == 'b'
2028 && isymp
->_n
._n_name
[2] == 'f'
2029 && isymp
->_n
._n_name
[3] == '\0')
2031 if (finfo
->last_bf_index
!= -1)
2033 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2036 if ((bfd_size_type
) finfo
->last_bf_index
2041 /* The last .bf symbol is in this input
2042 file. This will only happen if the
2043 assembler did not set up the .bf
2044 endndx symbols correctly. */
2045 auxout
= (finfo
->outsyms
2046 + ((finfo
->last_bf_index
2050 bfd_coff_swap_aux_out (output_bfd
,
2061 /* We have already written out the last
2062 .bf aux entry. We need to write it
2063 out again. We borrow *outsym
2064 temporarily. FIXME: This case should
2066 bfd_coff_swap_aux_out (output_bfd
,
2072 pos
= obj_sym_filepos (output_bfd
);
2073 pos
+= finfo
->last_bf_index
* osymesz
;
2074 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2075 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2081 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2082 finfo
->last_bf_index
= -1;
2085 /* The endndx field of this aux entry must
2086 be updated with the symbol number of the
2088 finfo
->last_bf
= *auxp
;
2089 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2098 bfd_coff_swap_aux_out (output_bfd
, auxp
, isymp
->n_type
,
2099 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2113 /* Relocate the line numbers, unless we are stripping them. */
2114 if (finfo
->info
->strip
== strip_none
2115 || finfo
->info
->strip
== strip_some
)
2117 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2123 bfd_boolean skipping
;
2127 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2128 build_link_order in ldwrite.c will not have created a
2129 link order, which means that we will not have seen this
2130 input section in _bfd_coff_final_link, which means that
2131 we will not have allocated space for the line numbers of
2132 this section. I don't think line numbers can be
2133 meaningful for a section which does not have
2134 SEC_HAS_CONTENTS set, but, if they do, this must be
2136 if (o
->lineno_count
== 0
2137 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2140 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2141 || bfd_bread (finfo
->linenos
, linesz
* o
->lineno_count
,
2142 input_bfd
) != linesz
* o
->lineno_count
)
2145 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2146 eline
= finfo
->linenos
;
2147 oeline
= finfo
->linenos
;
2148 elineend
= eline
+ linesz
* o
->lineno_count
;
2150 for (; eline
< elineend
; eline
+= linesz
)
2152 struct internal_lineno iline
;
2154 bfd_coff_swap_lineno_in (input_bfd
, eline
, &iline
);
2156 if (iline
.l_lnno
!= 0)
2157 iline
.l_addr
.l_paddr
+= offset
;
2158 else if (iline
.l_addr
.l_symndx
>= 0
2159 && ((unsigned long) iline
.l_addr
.l_symndx
2160 < obj_raw_syment_count (input_bfd
)))
2164 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2168 /* These line numbers are attached to a symbol
2169 which we are stripping. We must discard the
2170 line numbers because reading them back with
2171 no associated symbol (or associating them all
2172 with symbol #0) will fail. We can't regain
2173 the space in the output file, but at least
2179 struct internal_syment is
;
2180 union internal_auxent ia
;
2182 /* Fix up the lnnoptr field in the aux entry of
2183 the symbol. It turns out that we can't do
2184 this when we modify the symbol aux entries,
2185 because gas sometimes screws up the lnnoptr
2186 field and makes it an offset from the start
2187 of the line numbers rather than an absolute
2189 bfd_coff_swap_sym_in (output_bfd
,
2191 + ((indx
- syment_base
)
2193 if ((ISFCN (is
.n_type
)
2194 || is
.n_sclass
== C_BLOCK
)
2195 && is
.n_numaux
>= 1)
2199 auxptr
= (finfo
->outsyms
2200 + ((indx
- syment_base
+ 1)
2202 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2203 is
.n_type
, is
.n_sclass
,
2204 0, is
.n_numaux
, &ia
);
2205 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2206 (o
->output_section
->line_filepos
2207 + o
->output_section
->lineno_count
* linesz
2208 + eline
- finfo
->linenos
);
2209 bfd_coff_swap_aux_out (output_bfd
, &ia
,
2210 is
.n_type
, is
.n_sclass
, 0,
2211 is
.n_numaux
, auxptr
);
2217 iline
.l_addr
.l_symndx
= indx
;
2222 bfd_coff_swap_lineno_out (output_bfd
, &iline
, oeline
);
2227 pos
= o
->output_section
->line_filepos
;
2228 pos
+= o
->output_section
->lineno_count
* linesz
;
2229 amt
= oeline
- finfo
->linenos
;
2230 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2231 || bfd_bwrite (finfo
->linenos
, amt
, output_bfd
) != amt
)
2234 o
->output_section
->lineno_count
+= amt
/ linesz
;
2238 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2239 symbol will be the first symbol in the next input file. In the
2240 normal case, this will save us from writing out the C_FILE symbol
2242 if (finfo
->last_file_index
!= -1
2243 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2245 finfo
->last_file
.n_value
= output_index
;
2246 bfd_coff_swap_sym_out (output_bfd
, &finfo
->last_file
,
2248 + ((finfo
->last_file_index
- syment_base
)
2252 /* Write the modified symbols to the output file. */
2253 if (outsym
> finfo
->outsyms
)
2258 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2259 amt
= outsym
- finfo
->outsyms
;
2260 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2261 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
2264 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2265 + (outsym
- finfo
->outsyms
) / osymesz
)
2268 obj_raw_syment_count (output_bfd
) = output_index
;
2271 /* Relocate the contents of each section. */
2272 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2273 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2276 struct coff_section_tdata
*secdata
;
2278 if (! o
->linker_mark
)
2279 /* This section was omitted from the link. */
2282 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
2285 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2286 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2288 if ((o
->flags
& SEC_RELOC
) != 0
2289 && o
->reloc_count
!= 0)
2291 (*_bfd_error_handler
)
2292 (_("%B: relocs in section `%A', but it has no contents"),
2294 bfd_set_error (bfd_error_no_contents
);
2301 secdata
= coff_section_data (input_bfd
, o
);
2302 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2303 contents
= secdata
->contents
;
2306 bfd_size_type x
= o
->rawsize
? o
->rawsize
: o
->size
;
2307 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
, 0, x
))
2309 contents
= finfo
->contents
;
2312 if ((o
->flags
& SEC_RELOC
) != 0)
2315 struct internal_reloc
*internal_relocs
;
2316 struct internal_reloc
*irel
;
2318 /* Read in the relocs. */
2319 target_index
= o
->output_section
->target_index
;
2320 internal_relocs
= (_bfd_coff_read_internal_relocs
2321 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
2322 finfo
->info
->relocatable
,
2323 (finfo
->info
->relocatable
2324 ? (finfo
->section_info
[target_index
].relocs
2325 + o
->output_section
->reloc_count
)
2326 : finfo
->internal_relocs
)));
2327 if (internal_relocs
== NULL
)
2330 /* Call processor specific code to relocate the section
2332 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2336 finfo
->internal_syms
,
2340 if (finfo
->info
->relocatable
)
2343 struct internal_reloc
*irelend
;
2344 struct coff_link_hash_entry
**rel_hash
;
2346 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2347 irel
= internal_relocs
;
2348 irelend
= irel
+ o
->reloc_count
;
2349 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2350 + o
->output_section
->reloc_count
);
2351 for (; irel
< irelend
; irel
++, rel_hash
++)
2353 struct coff_link_hash_entry
*h
;
2354 bfd_boolean adjusted
;
2358 /* Adjust the reloc address and symbol index. */
2359 irel
->r_vaddr
+= offset
;
2361 if (irel
->r_symndx
== -1)
2366 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2374 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2377 /* This is a global symbol. */
2379 irel
->r_symndx
= h
->indx
;
2382 /* This symbol is being written at the end
2383 of the file, and we do not yet know the
2384 symbol index. We save the pointer to the
2385 hash table entry in the rel_hash list.
2386 We set the indx field to -2 to indicate
2387 that this symbol must not be stripped. */
2396 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2398 irel
->r_symndx
= indx
;
2401 struct internal_syment
*is
;
2403 char buf
[SYMNMLEN
+ 1];
2405 /* This reloc is against a symbol we are
2406 stripping. This should have been handled
2407 by the 'dont_skip_symbol' code in the while
2408 loop at the top of this function. */
2409 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2411 name
= (_bfd_coff_internal_syment_name
2412 (input_bfd
, is
, buf
));
2416 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2417 (finfo
->info
, name
, input_bfd
, o
,
2424 o
->output_section
->reloc_count
+= o
->reloc_count
;
2428 /* Write out the modified section contents. */
2429 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2431 file_ptr loc
= o
->output_offset
* bfd_octets_per_byte (output_bfd
);
2432 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2433 contents
, loc
, o
->size
))
2438 if (! (_bfd_write_section_stabs
2439 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2440 o
, &secdata
->stab_info
, contents
)))
2445 if (! finfo
->info
->keep_memory
2446 && ! _bfd_coff_free_symbols (input_bfd
))
2452 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2455 _bfd_coff_write_global_sym (struct coff_link_hash_entry
*h
, void *data
)
2457 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2459 struct internal_syment isym
;
2460 bfd_size_type symesz
;
2464 output_bfd
= finfo
->output_bfd
;
2466 if (h
->root
.type
== bfd_link_hash_warning
)
2468 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2469 if (h
->root
.type
== bfd_link_hash_new
)
2477 && (finfo
->info
->strip
== strip_all
2478 || (finfo
->info
->strip
== strip_some
2479 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2480 h
->root
.root
.string
, FALSE
, FALSE
)
2484 switch (h
->root
.type
)
2487 case bfd_link_hash_new
:
2488 case bfd_link_hash_warning
:
2492 case bfd_link_hash_undefined
:
2493 case bfd_link_hash_undefweak
:
2494 isym
.n_scnum
= N_UNDEF
;
2498 case bfd_link_hash_defined
:
2499 case bfd_link_hash_defweak
:
2503 sec
= h
->root
.u
.def
.section
->output_section
;
2504 if (bfd_is_abs_section (sec
))
2505 isym
.n_scnum
= N_ABS
;
2507 isym
.n_scnum
= sec
->target_index
;
2508 isym
.n_value
= (h
->root
.u
.def
.value
2509 + h
->root
.u
.def
.section
->output_offset
);
2510 if (! obj_pe (finfo
->output_bfd
))
2511 isym
.n_value
+= sec
->vma
;
2515 case bfd_link_hash_common
:
2516 isym
.n_scnum
= N_UNDEF
;
2517 isym
.n_value
= h
->root
.u
.c
.size
;
2520 case bfd_link_hash_indirect
:
2521 /* Just ignore these. They can't be handled anyhow. */
2525 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2526 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2533 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2535 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2537 if (indx
== (bfd_size_type
) -1)
2539 finfo
->failed
= TRUE
;
2542 isym
._n
._n_n
._n_zeroes
= 0;
2543 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2546 isym
.n_sclass
= h
->class;
2547 isym
.n_type
= h
->type
;
2549 if (isym
.n_sclass
== C_NULL
)
2550 isym
.n_sclass
= C_EXT
;
2552 /* If doing task linking and this is the pass where we convert
2553 defined globals to statics, then do that conversion now. If the
2554 symbol is not being converted, just ignore it and it will be
2555 output during a later pass. */
2556 if (finfo
->global_to_static
)
2558 if (! IS_EXTERNAL (output_bfd
, isym
))
2561 isym
.n_sclass
= C_STAT
;
2564 /* When a weak symbol is not overridden by a strong one,
2565 turn it into an external symbol when not building a
2566 shared or relocatable object. */
2567 if (! finfo
->info
->shared
2568 && ! finfo
->info
->relocatable
2569 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2570 isym
.n_sclass
= C_EXT
;
2572 isym
.n_numaux
= h
->numaux
;
2574 bfd_coff_swap_sym_out (output_bfd
, &isym
, finfo
->outsyms
);
2576 symesz
= bfd_coff_symesz (output_bfd
);
2578 pos
= obj_sym_filepos (output_bfd
);
2579 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2580 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2581 || bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2583 finfo
->failed
= TRUE
;
2587 h
->indx
= obj_raw_syment_count (output_bfd
);
2589 ++obj_raw_syment_count (output_bfd
);
2591 /* Write out any associated aux entries. Most of the aux entries
2592 will have been modified in _bfd_coff_link_input_bfd. We have to
2593 handle section aux entries here, now that we have the final
2594 relocation and line number counts. */
2595 for (i
= 0; i
< isym
.n_numaux
; i
++)
2597 union internal_auxent
*auxp
;
2601 /* Look for a section aux entry here using the same tests that
2602 coff_swap_aux_out uses. */
2604 && (isym
.n_sclass
== C_STAT
2605 || isym
.n_sclass
== C_HIDDEN
)
2606 && isym
.n_type
== T_NULL
2607 && (h
->root
.type
== bfd_link_hash_defined
2608 || h
->root
.type
== bfd_link_hash_defweak
))
2612 sec
= h
->root
.u
.def
.section
->output_section
;
2615 auxp
->x_scn
.x_scnlen
= sec
->size
;
2617 /* For PE, an overflow on the final link reportedly does
2618 not matter. FIXME: Why not? */
2619 if (sec
->reloc_count
> 0xffff
2620 && (! obj_pe (output_bfd
)
2621 || finfo
->info
->relocatable
))
2622 (*_bfd_error_handler
)
2623 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2624 bfd_get_filename (output_bfd
),
2625 bfd_get_section_name (output_bfd
, sec
),
2628 if (sec
->lineno_count
> 0xffff
2629 && (! obj_pe (output_bfd
)
2630 || finfo
->info
->relocatable
))
2631 (*_bfd_error_handler
)
2632 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2633 bfd_get_filename (output_bfd
),
2634 bfd_get_section_name (output_bfd
, sec
),
2637 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2638 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2639 auxp
->x_scn
.x_checksum
= 0;
2640 auxp
->x_scn
.x_associated
= 0;
2641 auxp
->x_scn
.x_comdat
= 0;
2645 bfd_coff_swap_aux_out (output_bfd
, auxp
, isym
.n_type
,
2646 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2648 if (bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2650 finfo
->failed
= TRUE
;
2653 ++obj_raw_syment_count (output_bfd
);
2659 /* Write out task global symbols, converting them to statics. Called
2660 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2661 the dirty work, if the symbol we are processing needs conversion. */
2664 _bfd_coff_write_task_globals (struct coff_link_hash_entry
*h
, void *data
)
2666 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2667 bfd_boolean rtnval
= TRUE
;
2668 bfd_boolean save_global_to_static
;
2670 if (h
->root
.type
== bfd_link_hash_warning
)
2671 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2675 switch (h
->root
.type
)
2677 case bfd_link_hash_defined
:
2678 case bfd_link_hash_defweak
:
2679 save_global_to_static
= finfo
->global_to_static
;
2680 finfo
->global_to_static
= TRUE
;
2681 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2682 finfo
->global_to_static
= save_global_to_static
;
2691 /* Handle a link order which is supposed to generate a reloc. */
2694 _bfd_coff_reloc_link_order (bfd
*output_bfd
,
2695 struct coff_final_link_info
*finfo
,
2696 asection
*output_section
,
2697 struct bfd_link_order
*link_order
)
2699 reloc_howto_type
*howto
;
2700 struct internal_reloc
*irel
;
2701 struct coff_link_hash_entry
**rel_hash_ptr
;
2703 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2706 bfd_set_error (bfd_error_bad_value
);
2710 if (link_order
->u
.reloc
.p
->addend
!= 0)
2714 bfd_reloc_status_type rstat
;
2718 size
= bfd_get_reloc_size (howto
);
2719 buf
= bfd_zmalloc (size
);
2723 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2724 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,\
2731 case bfd_reloc_outofrange
:
2733 case bfd_reloc_overflow
:
2734 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2736 (link_order
->type
== bfd_section_reloc_link_order
2737 ? bfd_section_name (output_bfd
,
2738 link_order
->u
.reloc
.p
->u
.section
)
2739 : link_order
->u
.reloc
.p
->u
.name
),
2740 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2741 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2748 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2749 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
2756 /* Store the reloc information in the right place. It will get
2757 swapped and written out at the end of the final_link routine. */
2758 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2759 + output_section
->reloc_count
);
2760 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2761 + output_section
->reloc_count
);
2763 memset (irel
, 0, sizeof (struct internal_reloc
));
2764 *rel_hash_ptr
= NULL
;
2766 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2768 if (link_order
->type
== bfd_section_reloc_link_order
)
2770 /* We need to somehow locate a symbol in the right section. The
2771 symbol must either have a value of zero, or we must adjust
2772 the addend by the value of the symbol. FIXME: Write this
2773 when we need it. The old linker couldn't handle this anyhow. */
2775 *rel_hash_ptr
= NULL
;
2780 struct coff_link_hash_entry
*h
;
2782 h
= ((struct coff_link_hash_entry
*)
2783 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2784 link_order
->u
.reloc
.p
->u
.name
,
2785 FALSE
, FALSE
, TRUE
));
2789 irel
->r_symndx
= h
->indx
;
2792 /* Set the index to -2 to force this symbol to get
2801 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2802 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2803 (asection
*) NULL
, (bfd_vma
) 0)))
2809 /* FIXME: Is this always right? */
2810 irel
->r_type
= howto
->type
;
2812 /* r_size is only used on the RS/6000, which needs its own linker
2813 routines anyhow. r_extern is only used for ECOFF. */
2815 /* FIXME: What is the right value for r_offset? Is zero OK? */
2816 ++output_section
->reloc_count
;
2821 /* A basic reloc handling routine which may be used by processors with
2825 _bfd_coff_generic_relocate_section (bfd
*output_bfd
,
2826 struct bfd_link_info
*info
,
2828 asection
*input_section
,
2830 struct internal_reloc
*relocs
,
2831 struct internal_syment
*syms
,
2832 asection
**sections
)
2834 struct internal_reloc
*rel
;
2835 struct internal_reloc
*relend
;
2838 relend
= rel
+ input_section
->reloc_count
;
2839 for (; rel
< relend
; rel
++)
2842 struct coff_link_hash_entry
*h
;
2843 struct internal_syment
*sym
;
2846 reloc_howto_type
*howto
;
2847 bfd_reloc_status_type rstat
;
2849 symndx
= rel
->r_symndx
;
2857 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2859 (*_bfd_error_handler
)
2860 ("%B: illegal symbol index %ld in relocs", input_bfd
, symndx
);
2865 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2866 sym
= syms
+ symndx
;
2869 /* COFF treats common symbols in one of two ways. Either the
2870 size of the symbol is included in the section contents, or it
2871 is not. We assume that the size is not included, and force
2872 the rtype_to_howto function to adjust the addend as needed. */
2873 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2874 addend
= - sym
->n_value
;
2878 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2883 /* If we are doing a relocatable link, then we can just ignore
2884 a PC relative reloc that is pcrel_offset. It will already
2885 have the correct value. If this is not a relocatable link,
2886 then we should ignore the symbol value. */
2887 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2889 if (info
->relocatable
)
2891 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2892 addend
+= sym
->n_value
;
2903 sec
= bfd_abs_section_ptr
;
2908 sec
= sections
[symndx
];
2909 val
= (sec
->output_section
->vma
2910 + sec
->output_offset
2912 if (! obj_pe (input_bfd
))
2918 if (h
->root
.type
== bfd_link_hash_defined
2919 || h
->root
.type
== bfd_link_hash_defweak
)
2921 /* Defined weak symbols are a GNU extension. */
2924 sec
= h
->root
.u
.def
.section
;
2925 val
= (h
->root
.u
.def
.value
2926 + sec
->output_section
->vma
2927 + sec
->output_offset
);
2930 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2932 if (h
->class == C_NT_WEAK
&& h
->numaux
== 1)
2934 /* See _Microsoft Portable Executable and Common Object
2935 File Format Specification_, section 5.5.3.
2936 Note that weak symbols without aux records are a GNU
2938 FIXME: All weak externals are treated as having
2939 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
2940 These behave as per SVR4 ABI: A library member
2941 will resolve a weak external only if a normal
2942 external causes the library member to be linked.
2943 See also linker.c: generic_link_check_archive_element. */
2945 struct coff_link_hash_entry
*h2
=
2946 input_bfd
->tdata
.coff_obj_data
->sym_hashes
[
2947 h
->aux
->x_sym
.x_tagndx
.l
];
2949 if (!h2
|| h2
->root
.type
== bfd_link_hash_undefined
)
2951 sec
= bfd_abs_section_ptr
;
2956 sec
= h2
->root
.u
.def
.section
;
2957 val
= h2
->root
.u
.def
.value
2958 + sec
->output_section
->vma
+ sec
->output_offset
;
2962 /* This is a GNU extension. */
2966 else if (! info
->relocatable
)
2968 if (! ((*info
->callbacks
->undefined_symbol
)
2969 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2970 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
2975 if (info
->base_file
)
2977 /* Emit a reloc if the backend thinks it needs it. */
2978 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2980 /* Relocation to a symbol in a section which isn't
2981 absolute. We output the address here to a file.
2982 This file is then read by dlltool when generating the
2983 reloc section. Note that the base file is not
2984 portable between systems. We write out a long here,
2985 and dlltool reads in a long. */
2986 long addr
= (rel
->r_vaddr
2987 - input_section
->vma
2988 + input_section
->output_offset
2989 + input_section
->output_section
->vma
);
2990 if (coff_data (output_bfd
)->pe
)
2991 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2992 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2995 bfd_set_error (bfd_error_system_call
);
3001 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3003 rel
->r_vaddr
- input_section
->vma
,
3012 case bfd_reloc_outofrange
:
3013 (*_bfd_error_handler
)
3014 (_("%B: bad reloc address 0x%lx in section `%A'"),
3015 input_bfd
, input_section
, (unsigned long) rel
->r_vaddr
);
3017 case bfd_reloc_overflow
:
3020 char buf
[SYMNMLEN
+ 1];
3028 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3033 if (! ((*info
->callbacks
->reloc_overflow
)
3034 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3035 (bfd_vma
) 0, input_bfd
, input_section
,
3036 rel
->r_vaddr
- input_section
->vma
)))