1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 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 3 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,
21 MA 02110-1301, USA. */
23 /* This file contains the COFF backend linker code. */
29 #include "coff/internal.h"
31 #include "safe-ctype.h"
33 static bfd_boolean
coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
);
34 static bfd_boolean
coff_link_check_archive_element (bfd
*abfd
, struct bfd_link_info
*info
, bfd_boolean
*pneeded
);
35 static bfd_boolean
coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
);
37 /* Return TRUE if SYM is a weak, external symbol. */
38 #define IS_WEAK_EXTERNAL(abfd, sym) \
39 ((sym).n_sclass == C_WEAKEXT \
40 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
42 /* Return TRUE if SYM is an external symbol. */
43 #define IS_EXTERNAL(abfd, sym) \
44 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
46 /* Define macros so that the ISFCN, et. al., macros work correctly.
47 These macros are defined in include/coff/internal.h in terms of
48 N_TMASK, etc. These definitions require a user to define local
49 variables with the appropriate names, and with values from the
50 coff_data (abfd) structure. */
52 #define N_TMASK n_tmask
53 #define N_BTSHFT n_btshft
54 #define N_BTMASK n_btmask
56 /* Create an entry in a COFF linker hash table. */
58 struct bfd_hash_entry
*
59 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
60 struct bfd_hash_table
*table
,
63 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
65 /* Allocate the structure if it has not already been allocated by a
67 if (ret
== (struct coff_link_hash_entry
*) NULL
)
68 ret
= ((struct coff_link_hash_entry
*)
69 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
70 if (ret
== (struct coff_link_hash_entry
*) NULL
)
71 return (struct bfd_hash_entry
*) ret
;
73 /* Call the allocation method of the superclass. */
74 ret
= ((struct coff_link_hash_entry
*)
75 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
77 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
79 /* Set local fields. */
82 ret
->symbol_class
= C_NULL
;
88 return (struct bfd_hash_entry
*) ret
;
91 /* Initialize a COFF linker hash table. */
94 _bfd_coff_link_hash_table_init (struct coff_link_hash_table
*table
,
96 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
97 struct bfd_hash_table
*,
101 memset (&table
->stab_info
, 0, sizeof (table
->stab_info
));
102 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
105 /* Create a COFF linker hash table. */
107 struct bfd_link_hash_table
*
108 _bfd_coff_link_hash_table_create (bfd
*abfd
)
110 struct coff_link_hash_table
*ret
;
111 bfd_size_type amt
= sizeof (struct coff_link_hash_table
);
113 ret
= (struct coff_link_hash_table
*) bfd_malloc (amt
);
117 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
118 _bfd_coff_link_hash_newfunc
,
119 sizeof (struct coff_link_hash_entry
)))
122 return (struct bfd_link_hash_table
*) NULL
;
127 /* Create an entry in a COFF debug merge hash table. */
129 struct bfd_hash_entry
*
130 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
131 struct bfd_hash_table
*table
,
134 struct coff_debug_merge_hash_entry
*ret
=
135 (struct coff_debug_merge_hash_entry
*) entry
;
137 /* Allocate the structure if it has not already been allocated by a
139 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
140 ret
= ((struct coff_debug_merge_hash_entry
*)
141 bfd_hash_allocate (table
,
142 sizeof (struct coff_debug_merge_hash_entry
)));
143 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
144 return (struct bfd_hash_entry
*) ret
;
146 /* Call the allocation method of the superclass. */
147 ret
= ((struct coff_debug_merge_hash_entry
*)
148 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
149 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
151 /* Set local fields. */
155 return (struct bfd_hash_entry
*) ret
;
158 /* Given a COFF BFD, add symbols to the global hash table as
162 _bfd_coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
164 switch (bfd_get_format (abfd
))
167 return coff_link_add_object_symbols (abfd
, info
);
169 return _bfd_generic_link_add_archive_symbols
170 (abfd
, info
, coff_link_check_archive_element
);
172 bfd_set_error (bfd_error_wrong_format
);
177 /* Add symbols from a COFF object file. */
180 coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
182 if (! _bfd_coff_get_external_symbols (abfd
))
184 if (! coff_link_add_symbols (abfd
, info
))
187 if (! info
->keep_memory
188 && ! _bfd_coff_free_symbols (abfd
))
194 /* Look through the symbols to see if this object file should be
195 included in the link. */
198 coff_link_check_ar_symbols (bfd
*abfd
,
199 struct bfd_link_info
*info
,
200 bfd_boolean
*pneeded
,
203 bfd_size_type symesz
;
209 symesz
= bfd_coff_symesz (abfd
);
210 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
211 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
212 while (esym
< esym_end
)
214 struct internal_syment sym
;
215 enum coff_symbol_classification classification
;
217 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
219 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
220 if (classification
== COFF_SYMBOL_GLOBAL
221 || classification
== COFF_SYMBOL_COMMON
)
224 char buf
[SYMNMLEN
+ 1];
225 struct bfd_link_hash_entry
*h
;
227 /* This symbol is externally visible, and is defined by this
229 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
232 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
236 && info
->pei386_auto_import
237 && CONST_STRNEQ (name
, "__imp_"))
238 h
= bfd_link_hash_lookup (info
->hash
, name
+ 6, FALSE
, FALSE
, TRUE
);
240 /* We are only interested in symbols that are currently
241 undefined. If a symbol is currently known to be common,
242 COFF linkers do not bring in an object file which defines
244 if (h
!= (struct bfd_link_hash_entry
*) NULL
245 && h
->type
== bfd_link_hash_undefined
)
247 if (! (*info
->callbacks
->add_archive_element
)
248 (info
, abfd
, name
, subsbfd
))
255 esym
+= (sym
.n_numaux
+ 1) * symesz
;
258 /* We do not need this object file. */
262 /* Check a single archive element to see if we need to include it in
263 the link. *PNEEDED is set according to whether this element is
264 needed in the link or not. This is called via
265 _bfd_generic_link_add_archive_symbols. */
268 coff_link_check_archive_element (bfd
*abfd
,
269 struct bfd_link_info
*info
,
270 bfd_boolean
*pneeded
)
274 if (! _bfd_coff_get_external_symbols (abfd
))
277 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
, &subsbfd
))
280 /* Potentially, the add_archive_element hook may have set a
281 substitute BFD for us. */
284 && ! _bfd_coff_get_external_symbols (subsbfd
))
288 && ! coff_link_add_symbols (subsbfd
? subsbfd
: abfd
, info
))
291 if ((! info
->keep_memory
|| ! *pneeded
)
292 && ! _bfd_coff_free_symbols (abfd
))
298 /* Add all the symbols from an object file to the hash table. */
301 coff_link_add_symbols (bfd
*abfd
,
302 struct bfd_link_info
*info
)
304 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
305 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
306 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
307 bfd_boolean keep_syms
;
308 bfd_boolean default_copy
;
309 bfd_size_type symcount
;
310 struct coff_link_hash_entry
**sym_hash
;
311 bfd_size_type symesz
;
316 symcount
= obj_raw_syment_count (abfd
);
319 return TRUE
; /* Nothing to do. */
321 /* Keep the symbols during this function, in case the linker needs
322 to read the generic symbols in order to report an error message. */
323 keep_syms
= obj_coff_keep_syms (abfd
);
324 obj_coff_keep_syms (abfd
) = TRUE
;
326 if (info
->keep_memory
)
327 default_copy
= FALSE
;
331 /* We keep a list of the linker hash table entries that correspond
332 to particular symbols. */
333 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
334 sym_hash
= (struct coff_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
335 if (sym_hash
== NULL
)
337 obj_coff_sym_hashes (abfd
) = sym_hash
;
339 symesz
= bfd_coff_symesz (abfd
);
340 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
341 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
342 esym_end
= esym
+ symcount
* symesz
;
343 while (esym
< esym_end
)
345 struct internal_syment sym
;
346 enum coff_symbol_classification classification
;
349 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
351 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
352 if (classification
!= COFF_SYMBOL_LOCAL
)
355 char buf
[SYMNMLEN
+ 1];
361 /* This symbol is externally visible. */
363 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
367 /* We must copy the name into memory if we got it from the
368 syment itself, rather than the string table. */
370 if (sym
._n
._n_n
._n_zeroes
!= 0
371 || sym
._n
._n_n
._n_offset
== 0)
376 switch (classification
)
381 case COFF_SYMBOL_GLOBAL
:
382 flags
= BSF_EXPORT
| BSF_GLOBAL
;
383 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
385 value
-= section
->vma
;
388 case COFF_SYMBOL_UNDEFINED
:
390 section
= bfd_und_section_ptr
;
393 case COFF_SYMBOL_COMMON
:
395 section
= bfd_com_section_ptr
;
398 case COFF_SYMBOL_PE_SECTION
:
399 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
400 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
404 if (IS_WEAK_EXTERNAL (abfd
, sym
))
409 /* In the PE format, section symbols actually refer to the
410 start of the output section. We handle them specially
412 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
414 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
415 name
, FALSE
, copy
, FALSE
);
416 if (*sym_hash
!= NULL
)
418 if (((*sym_hash
)->coff_link_hash_flags
419 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
420 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
421 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
422 (*_bfd_error_handler
)
423 ("Warning: symbol `%s' is both section and non-section",
430 /* The Microsoft Visual C compiler does string pooling by
431 hashing the constants to an internal symbol name, and
432 relying on the linker comdat support to discard
433 duplicate names. However, if one string is a literal and
434 one is a data initializer, one will end up in the .data
435 section and one will end up in the .rdata section. The
436 Microsoft linker will combine them into the .data
437 section, which seems to be wrong since it might cause the
440 As long as there are no external references to the
441 symbols, which there shouldn't be, we can treat the .data
442 and .rdata instances as separate symbols. The comdat
443 code in the linker will do the appropriate merging. Here
444 we avoid getting a multiple definition error for one of
445 these special symbols.
447 FIXME: I don't think this will work in the case where
448 there are two object files which use the constants as a
449 literal and two object files which use it as a data
450 initializer. One or the other of the second object files
451 is going to wind up with an inappropriate reference. */
453 && (classification
== COFF_SYMBOL_GLOBAL
454 || classification
== COFF_SYMBOL_PE_SECTION
)
455 && coff_section_data (abfd
, section
) != NULL
456 && coff_section_data (abfd
, section
)->comdat
!= NULL
457 && CONST_STRNEQ (name
, "??_")
458 && strcmp (name
, coff_section_data (abfd
, section
)->comdat
->name
) == 0)
460 if (*sym_hash
== NULL
)
461 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
462 name
, FALSE
, copy
, FALSE
);
463 if (*sym_hash
!= NULL
464 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
465 && coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
!= NULL
466 && strcmp (coff_section_data (abfd
, (*sym_hash
)->root
.u
.def
.section
)->comdat
->name
,
467 coff_section_data (abfd
, section
)->comdat
->name
) == 0)
473 if (! (bfd_coff_link_add_one_symbol
474 (info
, abfd
, name
, flags
, section
, value
,
475 (const char *) NULL
, copy
, FALSE
,
476 (struct bfd_link_hash_entry
**) sym_hash
)))
480 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
481 (*sym_hash
)->coff_link_hash_flags
|=
482 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
484 /* Limit the alignment of a common symbol to the possible
485 alignment of a section. There is no point to permitting
486 a higher alignment for a common symbol: we can not
487 guarantee it, and it may cause us to allocate extra space
488 in the common section. */
489 if (section
== bfd_com_section_ptr
490 && (*sym_hash
)->root
.type
== bfd_link_hash_common
491 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
492 > bfd_coff_default_section_alignment_power (abfd
)))
493 (*sym_hash
)->root
.u
.c
.p
->alignment_power
494 = bfd_coff_default_section_alignment_power (abfd
);
496 if (bfd_get_flavour (info
->output_bfd
) == bfd_get_flavour (abfd
))
498 /* If we don't have any symbol information currently in
499 the hash table, or if we are looking at a symbol
500 definition, then update the symbol class and type in
502 if (((*sym_hash
)->symbol_class
== C_NULL
503 && (*sym_hash
)->type
== T_NULL
)
506 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
507 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
509 (*sym_hash
)->symbol_class
= sym
.n_sclass
;
510 if (sym
.n_type
!= T_NULL
)
512 /* We want to warn if the type changed, but not
513 if it changed from an unspecified type.
514 Testing the whole type byte may work, but the
515 change from (e.g.) a function of unspecified
516 type to function of known type also wants to
518 if ((*sym_hash
)->type
!= T_NULL
519 && (*sym_hash
)->type
!= sym
.n_type
520 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
521 && (BTYPE ((*sym_hash
)->type
) == T_NULL
522 || BTYPE (sym
.n_type
) == T_NULL
)))
523 (*_bfd_error_handler
)
524 (_("Warning: type of symbol `%s' changed from %d to %d in %B"),
525 abfd
, name
, (*sym_hash
)->type
, sym
.n_type
);
527 /* We don't want to change from a meaningful
528 base type to a null one, but if we know
529 nothing, take what little we might now know. */
530 if (BTYPE (sym
.n_type
) != T_NULL
531 || (*sym_hash
)->type
== T_NULL
)
532 (*sym_hash
)->type
= sym
.n_type
;
534 (*sym_hash
)->auxbfd
= abfd
;
535 if (sym
.n_numaux
!= 0)
537 union internal_auxent
*alloc
;
540 union internal_auxent
*iaux
;
542 (*sym_hash
)->numaux
= sym
.n_numaux
;
543 alloc
= ((union internal_auxent
*)
544 bfd_hash_allocate (&info
->hash
->table
,
546 * sizeof (*alloc
))));
549 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
551 i
++, eaux
+= symesz
, iaux
++)
552 bfd_coff_swap_aux_in (abfd
, eaux
, sym
.n_type
,
553 sym
.n_sclass
, (int) i
,
555 (*sym_hash
)->aux
= alloc
;
560 if (classification
== COFF_SYMBOL_PE_SECTION
561 && (*sym_hash
)->numaux
!= 0)
563 /* Some PE sections (such as .bss) have a zero size in
564 the section header, but a non-zero size in the AUX
565 record. Correct that here.
567 FIXME: This is not at all the right place to do this.
568 For example, it won't help objdump. This needs to be
569 done when we swap in the section header. */
570 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
571 if (section
->size
== 0)
572 section
->size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
574 /* FIXME: We could test whether the section sizes
575 matches the size in the aux entry, but apparently
576 that sometimes fails unexpectedly. */
580 esym
+= (sym
.n_numaux
+ 1) * symesz
;
581 sym_hash
+= sym
.n_numaux
+ 1;
584 /* If this is a non-traditional, non-relocatable link, try to
585 optimize the handling of any .stab/.stabstr sections. */
586 if (! info
->relocatable
587 && ! info
->traditional_format
588 && bfd_get_flavour (info
->output_bfd
) == bfd_get_flavour (abfd
)
589 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
593 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
597 bfd_size_type string_offset
= 0;
600 for (stab
= abfd
->sections
; stab
; stab
= stab
->next
)
601 if (CONST_STRNEQ (stab
->name
, ".stab")
603 || (stab
->name
[5] == '.' && ISDIGIT (stab
->name
[6]))))
605 struct coff_link_hash_table
*table
;
606 struct coff_section_tdata
*secdata
607 = coff_section_data (abfd
, stab
);
611 amt
= sizeof (struct coff_section_tdata
);
612 stab
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
613 if (stab
->used_by_bfd
== NULL
)
615 secdata
= coff_section_data (abfd
, stab
);
618 table
= coff_hash_table (info
);
620 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
629 obj_coff_keep_syms (abfd
) = keep_syms
;
634 obj_coff_keep_syms (abfd
) = keep_syms
;
638 /* Do the final link step. */
641 _bfd_coff_final_link (bfd
*abfd
,
642 struct bfd_link_info
*info
)
644 bfd_size_type symesz
;
645 struct coff_final_link_info finfo
;
646 bfd_boolean debug_merge_allocated
;
647 bfd_boolean long_section_names
;
649 struct bfd_link_order
*p
;
650 bfd_size_type max_sym_count
;
651 bfd_size_type max_lineno_count
;
652 bfd_size_type max_reloc_count
;
653 bfd_size_type max_output_reloc_count
;
654 bfd_size_type max_contents_size
;
655 file_ptr rel_filepos
;
657 file_ptr line_filepos
;
660 bfd_byte
*external_relocs
= NULL
;
661 char strbuf
[STRING_SIZE_SIZE
];
664 symesz
= bfd_coff_symesz (abfd
);
667 finfo
.output_bfd
= abfd
;
669 finfo
.section_info
= NULL
;
670 finfo
.last_file_index
= -1;
671 finfo
.last_bf_index
= -1;
672 finfo
.internal_syms
= NULL
;
673 finfo
.sec_ptrs
= NULL
;
674 finfo
.sym_indices
= NULL
;
675 finfo
.outsyms
= NULL
;
676 finfo
.linenos
= NULL
;
677 finfo
.contents
= NULL
;
678 finfo
.external_relocs
= NULL
;
679 finfo
.internal_relocs
= NULL
;
680 finfo
.global_to_static
= FALSE
;
681 debug_merge_allocated
= FALSE
;
683 coff_data (abfd
)->link_info
= info
;
685 finfo
.strtab
= _bfd_stringtab_init ();
686 if (finfo
.strtab
== NULL
)
689 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
691 debug_merge_allocated
= TRUE
;
693 /* Compute the file positions for all the sections. */
694 if (! abfd
->output_has_begun
)
696 if (! bfd_coff_compute_section_file_positions (abfd
))
700 /* Count the line numbers and relocation entries required for the
701 output file. Set the file positions for the relocs. */
702 rel_filepos
= obj_relocbase (abfd
);
703 relsz
= bfd_coff_relsz (abfd
);
704 max_contents_size
= 0;
705 max_lineno_count
= 0;
708 long_section_names
= FALSE
;
709 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
713 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
715 if (p
->type
== bfd_indirect_link_order
)
719 sec
= p
->u
.indirect
.section
;
721 /* Mark all sections which are to be included in the
722 link. This will normally be every section. We need
723 to do this so that we can identify any sections which
724 the linker has decided to not include. */
725 sec
->linker_mark
= TRUE
;
727 if (info
->strip
== strip_none
728 || info
->strip
== strip_some
)
729 o
->lineno_count
+= sec
->lineno_count
;
731 if (info
->relocatable
)
732 o
->reloc_count
+= sec
->reloc_count
;
734 if (sec
->rawsize
> max_contents_size
)
735 max_contents_size
= sec
->rawsize
;
736 if (sec
->size
> max_contents_size
)
737 max_contents_size
= sec
->size
;
738 if (sec
->lineno_count
> max_lineno_count
)
739 max_lineno_count
= sec
->lineno_count
;
740 if (sec
->reloc_count
> max_reloc_count
)
741 max_reloc_count
= sec
->reloc_count
;
743 else if (info
->relocatable
744 && (p
->type
== bfd_section_reloc_link_order
745 || p
->type
== bfd_symbol_reloc_link_order
))
748 if (o
->reloc_count
== 0)
752 o
->flags
|= SEC_RELOC
;
753 o
->rel_filepos
= rel_filepos
;
754 rel_filepos
+= o
->reloc_count
* relsz
;
755 /* In PE COFF, if there are at least 0xffff relocations an
756 extra relocation will be written out to encode the count. */
757 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
758 rel_filepos
+= relsz
;
761 if (bfd_coff_long_section_names (abfd
)
762 && strlen (o
->name
) > SCNNMLEN
)
764 /* This section has a long name which must go in the string
765 table. This must correspond to the code in
766 coff_write_object_contents which puts the string index
767 into the s_name field of the section header. That is why
768 we pass hash as FALSE. */
769 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, FALSE
, FALSE
)
770 == (bfd_size_type
) -1)
772 long_section_names
= TRUE
;
776 /* If doing a relocatable link, allocate space for the pointers we
778 if (info
->relocatable
)
782 /* We use section_count + 1, rather than section_count, because
783 the target_index fields are 1 based. */
784 amt
= abfd
->section_count
+ 1;
785 amt
*= sizeof (struct coff_link_section_info
);
786 finfo
.section_info
= (struct coff_link_section_info
*) bfd_malloc (amt
);
787 if (finfo
.section_info
== NULL
)
789 for (i
= 0; i
<= abfd
->section_count
; i
++)
791 finfo
.section_info
[i
].relocs
= NULL
;
792 finfo
.section_info
[i
].rel_hashes
= NULL
;
796 /* We now know the size of the relocs, so we can determine the file
797 positions of the line numbers. */
798 line_filepos
= rel_filepos
;
799 linesz
= bfd_coff_linesz (abfd
);
800 max_output_reloc_count
= 0;
801 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
803 if (o
->lineno_count
== 0)
807 o
->line_filepos
= line_filepos
;
808 line_filepos
+= o
->lineno_count
* linesz
;
811 if (o
->reloc_count
!= 0)
813 /* We don't know the indices of global symbols until we have
814 written out all the local symbols. For each section in
815 the output file, we keep an array of pointers to hash
816 table entries. Each entry in the array corresponds to a
817 reloc. When we find a reloc against a global symbol, we
818 set the corresponding entry in this array so that we can
819 fix up the symbol index after we have written out all the
822 Because of this problem, we also keep the relocs in
823 memory until the end of the link. This wastes memory,
824 but only when doing a relocatable link, which is not the
826 BFD_ASSERT (info
->relocatable
);
827 amt
= o
->reloc_count
;
828 amt
*= sizeof (struct internal_reloc
);
829 finfo
.section_info
[o
->target_index
].relocs
=
830 (struct internal_reloc
*) bfd_malloc (amt
);
831 amt
= o
->reloc_count
;
832 amt
*= sizeof (struct coff_link_hash_entry
*);
833 finfo
.section_info
[o
->target_index
].rel_hashes
=
834 (struct coff_link_hash_entry
**) bfd_malloc (amt
);
835 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
836 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
839 if (o
->reloc_count
> max_output_reloc_count
)
840 max_output_reloc_count
= o
->reloc_count
;
843 /* Reset the reloc and lineno counts, so that we can use them to
844 count the number of entries we have output so far. */
849 obj_sym_filepos (abfd
) = line_filepos
;
851 /* Figure out the largest number of symbols in an input BFD. Take
852 the opportunity to clear the output_has_begun fields of all the
855 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
859 sub
->output_has_begun
= FALSE
;
860 sz
= obj_raw_syment_count (sub
);
861 if (sz
> max_sym_count
)
865 /* Allocate some buffers used while linking. */
866 amt
= max_sym_count
* sizeof (struct internal_syment
);
867 finfo
.internal_syms
= (struct internal_syment
*) bfd_malloc (amt
);
868 amt
= max_sym_count
* sizeof (asection
*);
869 finfo
.sec_ptrs
= (asection
**) bfd_malloc (amt
);
870 amt
= max_sym_count
* sizeof (long);
871 finfo
.sym_indices
= (long int *) bfd_malloc (amt
);
872 finfo
.outsyms
= (bfd_byte
*) bfd_malloc ((max_sym_count
+ 1) * symesz
);
873 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
874 finfo
.linenos
= (bfd_byte
*) bfd_malloc (amt
);
875 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
876 amt
= max_reloc_count
* relsz
;
877 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
878 if (! info
->relocatable
)
880 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
881 finfo
.internal_relocs
= (struct internal_reloc
*) bfd_malloc (amt
);
883 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
884 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
885 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
886 || finfo
.outsyms
== NULL
887 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
888 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
889 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
890 || (! info
->relocatable
891 && finfo
.internal_relocs
== NULL
892 && max_reloc_count
> 0))
895 /* We now know the position of everything in the file, except that
896 we don't know the size of the symbol table and therefore we don't
897 know where the string table starts. We just build the string
898 table in memory as we go along. We process all the relocations
899 for a single input file at once. */
900 obj_raw_syment_count (abfd
) = 0;
902 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
904 if (! bfd_coff_start_final_link (abfd
, info
))
908 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
910 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
912 if (p
->type
== bfd_indirect_link_order
913 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
915 sub
= p
->u
.indirect
.section
->owner
;
916 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
918 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
920 sub
->output_has_begun
= TRUE
;
923 else if (p
->type
== bfd_section_reloc_link_order
924 || p
->type
== bfd_symbol_reloc_link_order
)
926 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
931 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
937 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
940 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
942 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
943 debug_merge_allocated
= FALSE
;
945 if (finfo
.internal_syms
!= NULL
)
947 free (finfo
.internal_syms
);
948 finfo
.internal_syms
= NULL
;
950 if (finfo
.sec_ptrs
!= NULL
)
952 free (finfo
.sec_ptrs
);
953 finfo
.sec_ptrs
= NULL
;
955 if (finfo
.sym_indices
!= NULL
)
957 free (finfo
.sym_indices
);
958 finfo
.sym_indices
= NULL
;
960 if (finfo
.linenos
!= NULL
)
962 free (finfo
.linenos
);
963 finfo
.linenos
= NULL
;
965 if (finfo
.contents
!= NULL
)
967 free (finfo
.contents
);
968 finfo
.contents
= NULL
;
970 if (finfo
.external_relocs
!= NULL
)
972 free (finfo
.external_relocs
);
973 finfo
.external_relocs
= NULL
;
975 if (finfo
.internal_relocs
!= NULL
)
977 free (finfo
.internal_relocs
);
978 finfo
.internal_relocs
= NULL
;
981 /* The value of the last C_FILE symbol is supposed to be the symbol
982 index of the first external symbol. Write it out again if
984 if (finfo
.last_file_index
!= -1
985 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
989 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
990 bfd_coff_swap_sym_out (abfd
, &finfo
.last_file
,
993 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
994 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
995 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
999 /* If doing task linking (ld --task-link) then make a pass through the
1000 global symbols, writing out any that are defined, and making them
1002 if (info
->task_link
)
1004 finfo
.failed
= FALSE
;
1005 coff_link_hash_traverse (coff_hash_table (info
),
1006 _bfd_coff_write_task_globals
, &finfo
);
1011 /* Write out the global symbols. */
1012 finfo
.failed
= FALSE
;
1013 coff_link_hash_traverse (coff_hash_table (info
),
1014 _bfd_coff_write_global_sym
, &finfo
);
1018 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1019 if (finfo
.outsyms
!= NULL
)
1021 free (finfo
.outsyms
);
1022 finfo
.outsyms
= NULL
;
1025 if (info
->relocatable
&& max_output_reloc_count
> 0)
1027 /* Now that we have written out all the global symbols, we know
1028 the symbol indices to use for relocs against them, and we can
1029 finally write out the relocs. */
1030 amt
= max_output_reloc_count
* relsz
;
1031 external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
1032 if (external_relocs
== NULL
)
1035 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1037 struct internal_reloc
*irel
;
1038 struct internal_reloc
*irelend
;
1039 struct coff_link_hash_entry
**rel_hash
;
1042 if (o
->reloc_count
== 0)
1045 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1046 irelend
= irel
+ o
->reloc_count
;
1047 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1048 erel
= external_relocs
;
1049 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1051 if (*rel_hash
!= NULL
)
1053 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1054 irel
->r_symndx
= (*rel_hash
)->indx
;
1056 bfd_coff_swap_reloc_out (abfd
, irel
, erel
);
1059 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0)
1061 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
1063 /* In PE COFF, write the count of relocs as the first
1064 reloc. The header overflow bit will be set
1066 struct internal_reloc incount
;
1067 bfd_byte
*excount
= (bfd_byte
*)bfd_malloc (relsz
);
1069 memset (&incount
, 0, sizeof (incount
));
1070 incount
.r_vaddr
= o
->reloc_count
+ 1;
1071 bfd_coff_swap_reloc_out (abfd
, (PTR
) &incount
, (PTR
) excount
);
1072 if (bfd_bwrite (excount
, relsz
, abfd
) != relsz
)
1073 /* We'll leak, but it's an error anyway. */
1077 if (bfd_bwrite (external_relocs
,
1078 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1079 != (bfd_size_type
) relsz
* o
->reloc_count
)
1083 free (external_relocs
);
1084 external_relocs
= NULL
;
1087 /* Free up the section information. */
1088 if (finfo
.section_info
!= NULL
)
1092 for (i
= 0; i
< abfd
->section_count
; i
++)
1094 if (finfo
.section_info
[i
].relocs
!= NULL
)
1095 free (finfo
.section_info
[i
].relocs
);
1096 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1097 free (finfo
.section_info
[i
].rel_hashes
);
1099 free (finfo
.section_info
);
1100 finfo
.section_info
= NULL
;
1103 /* If we have optimized stabs strings, output them. */
1104 if (coff_hash_table (info
)->stab_info
.stabstr
!= NULL
)
1106 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1110 /* Write out the string table. */
1111 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1115 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1116 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1119 #if STRING_SIZE_SIZE == 4
1121 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1124 #error Change H_PUT_32 above
1127 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1128 != STRING_SIZE_SIZE
)
1131 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1134 obj_coff_strings_written (abfd
) = TRUE
;
1137 _bfd_stringtab_free (finfo
.strtab
);
1139 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1140 not try to write out the symbols. */
1141 bfd_get_symcount (abfd
) = 0;
1146 if (debug_merge_allocated
)
1147 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1148 if (finfo
.strtab
!= NULL
)
1149 _bfd_stringtab_free (finfo
.strtab
);
1150 if (finfo
.section_info
!= NULL
)
1154 for (i
= 0; i
< abfd
->section_count
; i
++)
1156 if (finfo
.section_info
[i
].relocs
!= NULL
)
1157 free (finfo
.section_info
[i
].relocs
);
1158 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1159 free (finfo
.section_info
[i
].rel_hashes
);
1161 free (finfo
.section_info
);
1163 if (finfo
.internal_syms
!= NULL
)
1164 free (finfo
.internal_syms
);
1165 if (finfo
.sec_ptrs
!= NULL
)
1166 free (finfo
.sec_ptrs
);
1167 if (finfo
.sym_indices
!= NULL
)
1168 free (finfo
.sym_indices
);
1169 if (finfo
.outsyms
!= NULL
)
1170 free (finfo
.outsyms
);
1171 if (finfo
.linenos
!= NULL
)
1172 free (finfo
.linenos
);
1173 if (finfo
.contents
!= NULL
)
1174 free (finfo
.contents
);
1175 if (finfo
.external_relocs
!= NULL
)
1176 free (finfo
.external_relocs
);
1177 if (finfo
.internal_relocs
!= NULL
)
1178 free (finfo
.internal_relocs
);
1179 if (external_relocs
!= NULL
)
1180 free (external_relocs
);
1184 /* Parse out a -heap <reserved>,<commit> line. */
1187 dores_com (char *ptr
, bfd
*output_bfd
, int heap
)
1189 if (coff_data(output_bfd
)->pe
)
1191 int val
= strtoul (ptr
, &ptr
, 0);
1194 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1196 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1200 val
= strtoul (ptr
+1, &ptr
, 0);
1202 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1204 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1211 get_name (char *ptr
, char **dst
)
1216 while (*ptr
&& *ptr
!= ' ')
1222 /* Process any magic embedded commands in a section called .drectve. */
1225 process_embedded_commands (bfd
*output_bfd
,
1226 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1229 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1237 if (!bfd_malloc_and_get_section (abfd
, sec
, ©
))
1243 e
= (char *) copy
+ sec
->size
;
1245 for (s
= (char *) copy
; s
< e
; )
1252 if (CONST_STRNEQ (s
, "-attr"))
1262 s
= get_name (s
, &name
);
1263 s
= get_name (s
, &attribs
);
1283 asec
= bfd_get_section_by_name (abfd
, name
);
1287 asec
->flags
|= SEC_CODE
;
1289 asec
->flags
|= SEC_READONLY
;
1292 else if (CONST_STRNEQ (s
, "-heap"))
1293 s
= dores_com (s
+ 5, output_bfd
, 1);
1295 else if (CONST_STRNEQ (s
, "-stack"))
1296 s
= dores_com (s
+ 6, output_bfd
, 0);
1298 /* GNU extension for aligned commons. */
1299 else if (CONST_STRNEQ (s
, "-aligncomm:"))
1301 /* Common symbols must be aligned on reading, as it
1302 is too late to do anything here, after they have
1303 already been allocated, so just skip the directive. */
1314 /* Place a marker against all symbols which are used by relocations.
1315 This marker can be picked up by the 'do we skip this symbol ?'
1316 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1320 mark_relocs (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1324 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1327 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1329 struct internal_reloc
* internal_relocs
;
1330 struct internal_reloc
* irel
;
1331 struct internal_reloc
* irelend
;
1333 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1335 /* Don't mark relocs in excluded sections. */
1336 if (a
->output_section
== bfd_abs_section_ptr
)
1339 /* Read in the relocs. */
1340 internal_relocs
= _bfd_coff_read_internal_relocs
1341 (input_bfd
, a
, FALSE
,
1342 finfo
->external_relocs
,
1343 finfo
->info
->relocatable
,
1344 (finfo
->info
->relocatable
1345 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1346 : finfo
->internal_relocs
)
1349 if (internal_relocs
== NULL
)
1352 irel
= internal_relocs
;
1353 irelend
= irel
+ a
->reloc_count
;
1355 /* Place a mark in the sym_indices array (whose entries have
1356 been initialised to 0) for all of the symbols that are used
1357 in the relocation table. This will then be picked up in the
1358 skip/don't-skip pass. */
1359 for (; irel
< irelend
; irel
++)
1360 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1364 /* Link an input file into the linker output file. This function
1365 handles all the sections and relocations of the input file at once. */
1368 _bfd_coff_link_input_bfd (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1370 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1371 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1372 bfd_boolean (*adjust_symndx
)
1373 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
1374 struct internal_reloc
*, bfd_boolean
*);
1376 const char *strings
;
1377 bfd_size_type syment_base
;
1378 bfd_boolean copy
, hash
;
1379 bfd_size_type isymesz
;
1380 bfd_size_type osymesz
;
1381 bfd_size_type linesz
;
1384 struct internal_syment
*isymp
;
1387 unsigned long output_index
;
1389 struct coff_link_hash_entry
**sym_hash
;
1392 /* Move all the symbols to the output file. */
1394 output_bfd
= finfo
->output_bfd
;
1396 syment_base
= obj_raw_syment_count (output_bfd
);
1397 isymesz
= bfd_coff_symesz (input_bfd
);
1398 osymesz
= bfd_coff_symesz (output_bfd
);
1399 linesz
= bfd_coff_linesz (input_bfd
);
1400 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1403 if (! finfo
->info
->keep_memory
)
1406 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1409 if (! _bfd_coff_get_external_symbols (input_bfd
))
1412 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1413 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1414 isymp
= finfo
->internal_syms
;
1415 secpp
= finfo
->sec_ptrs
;
1416 indexp
= finfo
->sym_indices
;
1417 output_index
= syment_base
;
1418 outsym
= finfo
->outsyms
;
1420 if (coff_data (output_bfd
)->pe
1421 && ! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1424 /* If we are going to perform relocations and also strip/discard some
1425 symbols then we must make sure that we do not strip/discard those
1426 symbols that are going to be involved in the relocations. */
1427 if (( finfo
->info
->strip
!= strip_none
1428 || finfo
->info
->discard
!= discard_none
)
1429 && finfo
->info
->relocatable
)
1431 /* Mark the symbol array as 'not-used'. */
1432 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1434 mark_relocs (finfo
, input_bfd
);
1437 while (esym
< esym_end
)
1439 struct internal_syment isym
;
1440 enum coff_symbol_classification classification
;
1443 bfd_boolean dont_skip_symbol
;
1446 bfd_coff_swap_sym_in (input_bfd
, esym
, isymp
);
1448 /* Make a copy of *isymp so that the relocate_section function
1449 always sees the original values. This is more reliable than
1450 always recomputing the symbol value even if we are stripping
1454 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1455 switch (classification
)
1459 case COFF_SYMBOL_GLOBAL
:
1460 case COFF_SYMBOL_PE_SECTION
:
1461 case COFF_SYMBOL_LOCAL
:
1462 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1464 case COFF_SYMBOL_COMMON
:
1465 *secpp
= bfd_com_section_ptr
;
1467 case COFF_SYMBOL_UNDEFINED
:
1468 *secpp
= bfd_und_section_ptr
;
1472 /* Extract the flag indicating if this symbol is used by a
1474 if ((finfo
->info
->strip
!= strip_none
1475 || finfo
->info
->discard
!= discard_none
)
1476 && finfo
->info
->relocatable
)
1477 dont_skip_symbol
= *indexp
;
1479 dont_skip_symbol
= FALSE
;
1485 add
= 1 + isym
.n_numaux
;
1487 /* If we are stripping all symbols, we want to skip this one. */
1488 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1493 switch (classification
)
1497 case COFF_SYMBOL_GLOBAL
:
1498 case COFF_SYMBOL_COMMON
:
1499 case COFF_SYMBOL_PE_SECTION
:
1500 /* This is a global symbol. Global symbols come at the
1501 end of the symbol table, so skip them for now.
1502 Locally defined function symbols, however, are an
1503 exception, and are not moved to the end. */
1505 if (! ISFCN (isym
.n_type
))
1509 case COFF_SYMBOL_UNDEFINED
:
1510 /* Undefined symbols are left for the end. */
1515 case COFF_SYMBOL_LOCAL
:
1516 /* This is a local symbol. Skip it if we are discarding
1518 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1524 #ifndef COFF_WITH_PE
1525 /* Skip section symbols for sections which are not going to be
1528 && !dont_skip_symbol
1529 && isym
.n_sclass
== C_STAT
1530 && isym
.n_type
== T_NULL
1531 && isym
.n_numaux
> 0
1532 && ((*secpp
)->output_section
== bfd_abs_section_ptr
1533 || bfd_section_removed_from_list (output_bfd
,
1534 (*secpp
)->output_section
)))
1538 /* If we stripping debugging symbols, and this is a debugging
1539 symbol, then skip it. FIXME: gas sets the section to N_ABS
1540 for some types of debugging symbols; I don't know if this is
1541 a bug or not. In any case, we handle it here. */
1543 && finfo
->info
->strip
== strip_debugger
1544 && ! dont_skip_symbol
1545 && (isym
.n_scnum
== N_DEBUG
1546 || (isym
.n_scnum
== N_ABS
1547 && (isym
.n_sclass
== C_AUTO
1548 || isym
.n_sclass
== C_REG
1549 || isym
.n_sclass
== C_MOS
1550 || isym
.n_sclass
== C_MOE
1551 || isym
.n_sclass
== C_MOU
1552 || isym
.n_sclass
== C_ARG
1553 || isym
.n_sclass
== C_REGPARM
1554 || isym
.n_sclass
== C_FIELD
1555 || isym
.n_sclass
== C_EOS
))))
1558 /* If some symbols are stripped based on the name, work out the
1559 name and decide whether to skip this symbol. */
1561 && (finfo
->info
->strip
== strip_some
1562 || finfo
->info
->discard
== discard_l
))
1565 char buf
[SYMNMLEN
+ 1];
1567 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1571 if (! dont_skip_symbol
1572 && ((finfo
->info
->strip
== strip_some
1573 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
,
1576 && finfo
->info
->discard
== discard_l
1577 && bfd_is_local_label_name (input_bfd
, name
))))
1581 /* If this is an enum, struct, or union tag, see if we have
1582 already output an identical type. */
1584 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1585 && (isym
.n_sclass
== C_ENTAG
1586 || isym
.n_sclass
== C_STRTAG
1587 || isym
.n_sclass
== C_UNTAG
)
1588 && isym
.n_numaux
== 1)
1591 char buf
[SYMNMLEN
+ 1];
1592 struct coff_debug_merge_hash_entry
*mh
;
1593 struct coff_debug_merge_type
*mt
;
1594 union internal_auxent aux
;
1595 struct coff_debug_merge_element
**epp
;
1596 bfd_byte
*esl
, *eslend
;
1597 struct internal_syment
*islp
;
1600 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1604 /* Ignore fake names invented by compiler; treat them all as
1606 if (*name
== '~' || *name
== '.' || *name
== '$'
1607 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1608 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1611 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1616 /* Allocate memory to hold type information. If this turns
1617 out to be a duplicate, we pass this address to
1619 amt
= sizeof (struct coff_debug_merge_type
);
1620 mt
= (struct coff_debug_merge_type
*) bfd_alloc (input_bfd
, amt
);
1623 mt
->type_class
= isym
.n_sclass
;
1625 /* Pick up the aux entry, which points to the end of the tag
1627 bfd_coff_swap_aux_in (input_bfd
, (esym
+ isymesz
),
1628 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1631 /* Gather the elements. */
1632 epp
= &mt
->elements
;
1633 mt
->elements
= NULL
;
1635 esl
= esym
+ 2 * isymesz
;
1636 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1637 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1638 while (esl
< eslend
)
1640 const char *elename
;
1641 char elebuf
[SYMNMLEN
+ 1];
1644 bfd_coff_swap_sym_in (input_bfd
, esl
, islp
);
1646 amt
= sizeof (struct coff_debug_merge_element
);
1647 *epp
= (struct coff_debug_merge_element
*)
1648 bfd_alloc (input_bfd
, amt
);
1652 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1654 if (elename
== NULL
)
1657 amt
= strlen (elename
) + 1;
1658 name_copy
= (char *) bfd_alloc (input_bfd
, amt
);
1659 if (name_copy
== NULL
)
1661 strcpy (name_copy
, elename
);
1663 (*epp
)->name
= name_copy
;
1664 (*epp
)->type
= islp
->n_type
;
1666 if (islp
->n_numaux
>= 1
1667 && islp
->n_type
!= T_NULL
1668 && islp
->n_sclass
!= C_EOS
)
1670 union internal_auxent eleaux
;
1673 bfd_coff_swap_aux_in (input_bfd
, (esl
+ isymesz
),
1674 islp
->n_type
, islp
->n_sclass
, 0,
1675 islp
->n_numaux
, &eleaux
);
1676 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1678 /* FIXME: If this tagndx entry refers to a symbol
1679 defined later in this file, we just ignore it.
1680 Handling this correctly would be tedious, and may
1685 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1688 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1689 if ((*epp
)->tagndx
< 0)
1693 epp
= &(*epp
)->next
;
1696 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1697 islp
+= islp
->n_numaux
+ 1;
1700 /* See if we already have a definition which matches this
1701 type. We always output the type if it has no elements,
1703 if (mt
->elements
== NULL
)
1704 bfd_release (input_bfd
, mt
);
1707 struct coff_debug_merge_type
*mtl
;
1709 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1711 struct coff_debug_merge_element
*me
, *mel
;
1713 if (mtl
->type_class
!= mt
->type_class
)
1716 for (me
= mt
->elements
, mel
= mtl
->elements
;
1717 me
!= NULL
&& mel
!= NULL
;
1718 me
= me
->next
, mel
= mel
->next
)
1720 if (strcmp (me
->name
, mel
->name
) != 0
1721 || me
->type
!= mel
->type
1722 || me
->tagndx
!= mel
->tagndx
)
1726 if (me
== NULL
&& mel
== NULL
)
1730 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1732 /* This is the first definition of this type. */
1733 mt
->indx
= output_index
;
1734 mt
->next
= mh
->types
;
1739 /* This is a redefinition which can be merged. */
1740 bfd_release (input_bfd
, mt
);
1741 *indexp
= mtl
->indx
;
1742 add
= (eslend
- esym
) / isymesz
;
1748 /* We now know whether we are to skip this symbol or not. */
1751 /* Adjust the symbol in order to output it. */
1753 if (isym
._n
._n_n
._n_zeroes
== 0
1754 && isym
._n
._n_n
._n_offset
!= 0)
1759 /* This symbol has a long name. Enter it in the string
1760 table we are building. Note that we do not check
1761 bfd_coff_symname_in_debug. That is only true for
1762 XCOFF, and XCOFF requires different linking code
1764 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
1767 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1768 if (indx
== (bfd_size_type
) -1)
1770 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1773 switch (isym
.n_sclass
)
1789 /* The symbol value should not be modified. */
1793 if (obj_pe (input_bfd
)
1794 && strcmp (isym
.n_name
, ".bf") != 0
1795 && isym
.n_scnum
> 0)
1797 /* For PE, .lf and .ef get their value left alone,
1798 while .bf gets relocated. However, they all have
1799 "real" section numbers, and need to be moved into
1801 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1806 case C_LABEL
: /* Not completely sure about these 2 */
1815 /* Compute new symbol location. */
1816 if (isym
.n_scnum
> 0)
1818 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1819 isym
.n_value
+= (*secpp
)->output_offset
;
1820 if (! obj_pe (input_bfd
))
1821 isym
.n_value
-= (*secpp
)->vma
;
1822 if (! obj_pe (finfo
->output_bfd
))
1823 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1828 /* The value of a C_FILE symbol is the symbol index of
1829 the next C_FILE symbol. The value of the last C_FILE
1830 symbol is the symbol index to the first external
1831 symbol (actually, coff_renumber_symbols does not get
1832 this right--it just sets the value of the last C_FILE
1833 symbol to zero--and nobody has ever complained about
1834 it). We try to get this right, below, just before we
1835 write the symbols out, but in the general case we may
1836 have to write the symbol out twice. */
1837 if (finfo
->last_file_index
!= -1
1838 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1840 /* We must correct the value of the last C_FILE
1842 finfo
->last_file
.n_value
= output_index
;
1843 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1845 /* The last C_FILE symbol is in this input file. */
1846 bfd_coff_swap_sym_out (output_bfd
,
1849 + ((finfo
->last_file_index
1857 /* We have already written out the last C_FILE
1858 symbol. We need to write it out again. We
1859 borrow *outsym temporarily. */
1860 bfd_coff_swap_sym_out (output_bfd
,
1861 &finfo
->last_file
, outsym
);
1862 pos
= obj_sym_filepos (output_bfd
);
1863 pos
+= finfo
->last_file_index
* osymesz
;
1864 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1865 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1870 finfo
->last_file_index
= output_index
;
1871 finfo
->last_file
= isym
;
1875 /* If doing task linking, convert normal global function symbols to
1876 static functions. */
1877 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1878 isym
.n_sclass
= C_STAT
;
1880 /* Output the symbol. */
1881 bfd_coff_swap_sym_out (output_bfd
, &isym
, outsym
);
1883 *indexp
= output_index
;
1888 struct coff_link_hash_entry
*h
;
1890 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1892 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1895 /* This can happen if there were errors earlier in
1897 bfd_set_error (bfd_error_bad_value
);
1900 h
->indx
= output_index
;
1903 output_index
+= add
;
1904 outsym
+= add
* osymesz
;
1907 esym
+= add
* isymesz
;
1911 for (--add
; add
> 0; --add
)
1918 /* Fix up the aux entries. This must be done in a separate pass,
1919 because we don't know the correct symbol indices until we have
1920 already decided which symbols we are going to keep. */
1921 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1922 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1923 isymp
= finfo
->internal_syms
;
1924 indexp
= finfo
->sym_indices
;
1925 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1926 outsym
= finfo
->outsyms
;
1928 while (esym
< esym_end
)
1932 add
= 1 + isymp
->n_numaux
;
1935 || (bfd_size_type
) *indexp
< syment_base
)
1936 && (*sym_hash
== NULL
1937 || (*sym_hash
)->auxbfd
!= input_bfd
))
1938 esym
+= add
* isymesz
;
1941 struct coff_link_hash_entry
*h
;
1949 /* The m68k-motorola-sysv assembler will sometimes
1950 generate two symbols with the same name, but only one
1951 will have aux entries. */
1952 BFD_ASSERT (isymp
->n_numaux
== 0
1954 || h
->numaux
== isymp
->n_numaux
);
1962 /* Handle the aux entries. This handling is based on
1963 coff_pointerize_aux. I don't know if it always correct. */
1964 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1966 union internal_auxent aux
;
1967 union internal_auxent
*auxp
;
1969 if (h
!= NULL
&& h
->aux
!= NULL
&& (h
->numaux
> i
))
1973 bfd_coff_swap_aux_in (input_bfd
, esym
, isymp
->n_type
,
1974 isymp
->n_sclass
, i
, isymp
->n_numaux
, &aux
);
1978 if (isymp
->n_sclass
== C_FILE
)
1980 /* If this is a long filename, we must put it in the
1982 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1983 && auxp
->x_file
.x_n
.x_offset
!= 0)
1985 const char *filename
;
1988 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1989 >= STRING_SIZE_SIZE
);
1990 if (strings
== NULL
)
1992 strings
= _bfd_coff_read_string_table (input_bfd
);
1993 if (strings
== NULL
)
1996 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1997 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1999 if (indx
== (bfd_size_type
) -1)
2001 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
2004 else if ((isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
2005 && isymp
->n_sclass
!= C_NT_WEAK
)
2009 if (ISFCN (isymp
->n_type
)
2010 || ISTAG (isymp
->n_sclass
)
2011 || isymp
->n_sclass
== C_BLOCK
2012 || isymp
->n_sclass
== C_FCN
)
2014 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2016 && indx
< obj_raw_syment_count (input_bfd
))
2018 /* We look forward through the symbol for
2019 the index of the next symbol we are going
2020 to include. I don't know if this is
2022 while ((finfo
->sym_indices
[indx
] < 0
2023 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
2025 && indx
< obj_raw_syment_count (input_bfd
))
2027 if (indx
>= obj_raw_syment_count (input_bfd
))
2028 indx
= output_index
;
2030 indx
= finfo
->sym_indices
[indx
];
2031 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2035 indx
= auxp
->x_sym
.x_tagndx
.l
;
2036 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2040 symindx
= finfo
->sym_indices
[indx
];
2042 auxp
->x_sym
.x_tagndx
.l
= 0;
2044 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2047 /* The .bf symbols are supposed to be linked through
2048 the endndx field. We need to carry this list
2049 across object files. */
2052 && isymp
->n_sclass
== C_FCN
2053 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2054 || isymp
->_n
._n_n
._n_offset
== 0)
2055 && isymp
->_n
._n_name
[0] == '.'
2056 && isymp
->_n
._n_name
[1] == 'b'
2057 && isymp
->_n
._n_name
[2] == 'f'
2058 && isymp
->_n
._n_name
[3] == '\0')
2060 if (finfo
->last_bf_index
!= -1)
2062 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2065 if ((bfd_size_type
) finfo
->last_bf_index
2070 /* The last .bf symbol is in this input
2071 file. This will only happen if the
2072 assembler did not set up the .bf
2073 endndx symbols correctly. */
2074 auxout
= (finfo
->outsyms
2075 + ((finfo
->last_bf_index
2079 bfd_coff_swap_aux_out (output_bfd
,
2090 /* We have already written out the last
2091 .bf aux entry. We need to write it
2092 out again. We borrow *outsym
2093 temporarily. FIXME: This case should
2095 bfd_coff_swap_aux_out (output_bfd
,
2101 pos
= obj_sym_filepos (output_bfd
);
2102 pos
+= finfo
->last_bf_index
* osymesz
;
2103 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2104 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2110 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2111 finfo
->last_bf_index
= -1;
2114 /* The endndx field of this aux entry must
2115 be updated with the symbol number of the
2117 finfo
->last_bf
= *auxp
;
2118 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2127 bfd_coff_swap_aux_out (output_bfd
, auxp
, isymp
->n_type
,
2128 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2142 /* Relocate the line numbers, unless we are stripping them. */
2143 if (finfo
->info
->strip
== strip_none
2144 || finfo
->info
->strip
== strip_some
)
2146 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2152 bfd_boolean skipping
;
2156 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2157 build_link_order in ldwrite.c will not have created a
2158 link order, which means that we will not have seen this
2159 input section in _bfd_coff_final_link, which means that
2160 we will not have allocated space for the line numbers of
2161 this section. I don't think line numbers can be
2162 meaningful for a section which does not have
2163 SEC_HAS_CONTENTS set, but, if they do, this must be
2165 if (o
->lineno_count
== 0
2166 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2169 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2170 || bfd_bread (finfo
->linenos
, linesz
* o
->lineno_count
,
2171 input_bfd
) != linesz
* o
->lineno_count
)
2174 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2175 eline
= finfo
->linenos
;
2176 oeline
= finfo
->linenos
;
2177 elineend
= eline
+ linesz
* o
->lineno_count
;
2179 for (; eline
< elineend
; eline
+= linesz
)
2181 struct internal_lineno iline
;
2183 bfd_coff_swap_lineno_in (input_bfd
, eline
, &iline
);
2185 if (iline
.l_lnno
!= 0)
2186 iline
.l_addr
.l_paddr
+= offset
;
2187 else if (iline
.l_addr
.l_symndx
>= 0
2188 && ((unsigned long) iline
.l_addr
.l_symndx
2189 < obj_raw_syment_count (input_bfd
)))
2193 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2197 /* These line numbers are attached to a symbol
2198 which we are stripping. We must discard the
2199 line numbers because reading them back with
2200 no associated symbol (or associating them all
2201 with symbol #0) will fail. We can't regain
2202 the space in the output file, but at least
2208 struct internal_syment is
;
2209 union internal_auxent ia
;
2211 /* Fix up the lnnoptr field in the aux entry of
2212 the symbol. It turns out that we can't do
2213 this when we modify the symbol aux entries,
2214 because gas sometimes screws up the lnnoptr
2215 field and makes it an offset from the start
2216 of the line numbers rather than an absolute
2218 bfd_coff_swap_sym_in (output_bfd
,
2220 + ((indx
- syment_base
)
2222 if ((ISFCN (is
.n_type
)
2223 || is
.n_sclass
== C_BLOCK
)
2224 && is
.n_numaux
>= 1)
2228 auxptr
= (finfo
->outsyms
2229 + ((indx
- syment_base
+ 1)
2231 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2232 is
.n_type
, is
.n_sclass
,
2233 0, is
.n_numaux
, &ia
);
2234 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2235 (o
->output_section
->line_filepos
2236 + o
->output_section
->lineno_count
* linesz
2237 + eline
- finfo
->linenos
);
2238 bfd_coff_swap_aux_out (output_bfd
, &ia
,
2239 is
.n_type
, is
.n_sclass
, 0,
2240 is
.n_numaux
, auxptr
);
2246 iline
.l_addr
.l_symndx
= indx
;
2251 bfd_coff_swap_lineno_out (output_bfd
, &iline
, oeline
);
2256 pos
= o
->output_section
->line_filepos
;
2257 pos
+= o
->output_section
->lineno_count
* linesz
;
2258 amt
= oeline
- finfo
->linenos
;
2259 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2260 || bfd_bwrite (finfo
->linenos
, amt
, output_bfd
) != amt
)
2263 o
->output_section
->lineno_count
+= amt
/ linesz
;
2267 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2268 symbol will be the first symbol in the next input file. In the
2269 normal case, this will save us from writing out the C_FILE symbol
2271 if (finfo
->last_file_index
!= -1
2272 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2274 finfo
->last_file
.n_value
= output_index
;
2275 bfd_coff_swap_sym_out (output_bfd
, &finfo
->last_file
,
2277 + ((finfo
->last_file_index
- syment_base
)
2281 /* Write the modified symbols to the output file. */
2282 if (outsym
> finfo
->outsyms
)
2287 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2288 amt
= outsym
- finfo
->outsyms
;
2289 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2290 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
2293 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2294 + (outsym
- finfo
->outsyms
) / osymesz
)
2297 obj_raw_syment_count (output_bfd
) = output_index
;
2300 /* Relocate the contents of each section. */
2301 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2302 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2305 struct coff_section_tdata
*secdata
;
2307 if (! o
->linker_mark
)
2308 /* This section was omitted from the link. */
2311 if ((o
->flags
& SEC_LINKER_CREATED
) != 0)
2314 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2315 || (o
->size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2317 if ((o
->flags
& SEC_RELOC
) != 0
2318 && o
->reloc_count
!= 0)
2320 (*_bfd_error_handler
)
2321 (_("%B: relocs in section `%A', but it has no contents"),
2323 bfd_set_error (bfd_error_no_contents
);
2330 secdata
= coff_section_data (input_bfd
, o
);
2331 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2332 contents
= secdata
->contents
;
2335 bfd_size_type x
= o
->rawsize
? o
->rawsize
: o
->size
;
2336 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
, 0, x
))
2338 contents
= finfo
->contents
;
2341 if ((o
->flags
& SEC_RELOC
) != 0)
2344 struct internal_reloc
*internal_relocs
;
2345 struct internal_reloc
*irel
;
2347 /* Read in the relocs. */
2348 target_index
= o
->output_section
->target_index
;
2349 internal_relocs
= (_bfd_coff_read_internal_relocs
2350 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
2351 finfo
->info
->relocatable
,
2352 (finfo
->info
->relocatable
2353 ? (finfo
->section_info
[target_index
].relocs
2354 + o
->output_section
->reloc_count
)
2355 : finfo
->internal_relocs
)));
2356 if (internal_relocs
== NULL
)
2359 /* Call processor specific code to relocate the section
2361 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2365 finfo
->internal_syms
,
2369 if (finfo
->info
->relocatable
)
2372 struct internal_reloc
*irelend
;
2373 struct coff_link_hash_entry
**rel_hash
;
2375 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2376 irel
= internal_relocs
;
2377 irelend
= irel
+ o
->reloc_count
;
2378 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2379 + o
->output_section
->reloc_count
);
2380 for (; irel
< irelend
; irel
++, rel_hash
++)
2382 struct coff_link_hash_entry
*h
;
2383 bfd_boolean adjusted
;
2387 /* Adjust the reloc address and symbol index. */
2388 irel
->r_vaddr
+= offset
;
2390 if (irel
->r_symndx
== -1)
2395 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2403 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2406 /* This is a global symbol. */
2408 irel
->r_symndx
= h
->indx
;
2411 /* This symbol is being written at the end
2412 of the file, and we do not yet know the
2413 symbol index. We save the pointer to the
2414 hash table entry in the rel_hash list.
2415 We set the indx field to -2 to indicate
2416 that this symbol must not be stripped. */
2425 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2427 irel
->r_symndx
= indx
;
2430 struct internal_syment
*is
;
2432 char buf
[SYMNMLEN
+ 1];
2434 /* This reloc is against a symbol we are
2435 stripping. This should have been handled
2436 by the 'dont_skip_symbol' code in the while
2437 loop at the top of this function. */
2438 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2440 name
= (_bfd_coff_internal_syment_name
2441 (input_bfd
, is
, buf
));
2445 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2446 (finfo
->info
, name
, input_bfd
, o
,
2453 o
->output_section
->reloc_count
+= o
->reloc_count
;
2457 /* Write out the modified section contents. */
2458 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2460 file_ptr loc
= o
->output_offset
* bfd_octets_per_byte (output_bfd
);
2461 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2462 contents
, loc
, o
->size
))
2467 if (! (_bfd_write_section_stabs
2468 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2469 o
, &secdata
->stab_info
, contents
)))
2474 if (! finfo
->info
->keep_memory
2475 && ! _bfd_coff_free_symbols (input_bfd
))
2481 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2484 _bfd_coff_write_global_sym (struct coff_link_hash_entry
*h
, void *data
)
2486 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2488 struct internal_syment isym
;
2489 bfd_size_type symesz
;
2493 output_bfd
= finfo
->output_bfd
;
2495 if (h
->root
.type
== bfd_link_hash_warning
)
2497 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2498 if (h
->root
.type
== bfd_link_hash_new
)
2506 && (finfo
->info
->strip
== strip_all
2507 || (finfo
->info
->strip
== strip_some
2508 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2509 h
->root
.root
.string
, FALSE
, FALSE
)
2513 switch (h
->root
.type
)
2516 case bfd_link_hash_new
:
2517 case bfd_link_hash_warning
:
2521 case bfd_link_hash_undefined
:
2522 case bfd_link_hash_undefweak
:
2523 isym
.n_scnum
= N_UNDEF
;
2527 case bfd_link_hash_defined
:
2528 case bfd_link_hash_defweak
:
2532 sec
= h
->root
.u
.def
.section
->output_section
;
2533 if (bfd_is_abs_section (sec
))
2534 isym
.n_scnum
= N_ABS
;
2536 isym
.n_scnum
= sec
->target_index
;
2537 isym
.n_value
= (h
->root
.u
.def
.value
2538 + h
->root
.u
.def
.section
->output_offset
);
2539 if (! obj_pe (finfo
->output_bfd
))
2540 isym
.n_value
+= sec
->vma
;
2544 case bfd_link_hash_common
:
2545 isym
.n_scnum
= N_UNDEF
;
2546 isym
.n_value
= h
->root
.u
.c
.size
;
2549 case bfd_link_hash_indirect
:
2550 /* Just ignore these. They can't be handled anyhow. */
2554 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2555 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2562 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2564 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2566 if (indx
== (bfd_size_type
) -1)
2568 finfo
->failed
= TRUE
;
2571 isym
._n
._n_n
._n_zeroes
= 0;
2572 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2575 isym
.n_sclass
= h
->symbol_class
;
2576 isym
.n_type
= h
->type
;
2578 if (isym
.n_sclass
== C_NULL
)
2579 isym
.n_sclass
= C_EXT
;
2581 /* If doing task linking and this is the pass where we convert
2582 defined globals to statics, then do that conversion now. If the
2583 symbol is not being converted, just ignore it and it will be
2584 output during a later pass. */
2585 if (finfo
->global_to_static
)
2587 if (! IS_EXTERNAL (output_bfd
, isym
))
2590 isym
.n_sclass
= C_STAT
;
2593 /* When a weak symbol is not overridden by a strong one,
2594 turn it into an external symbol when not building a
2595 shared or relocatable object. */
2596 if (! finfo
->info
->shared
2597 && ! finfo
->info
->relocatable
2598 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2599 isym
.n_sclass
= C_EXT
;
2601 isym
.n_numaux
= h
->numaux
;
2603 bfd_coff_swap_sym_out (output_bfd
, &isym
, finfo
->outsyms
);
2605 symesz
= bfd_coff_symesz (output_bfd
);
2607 pos
= obj_sym_filepos (output_bfd
);
2608 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2609 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2610 || bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2612 finfo
->failed
= TRUE
;
2616 h
->indx
= obj_raw_syment_count (output_bfd
);
2618 ++obj_raw_syment_count (output_bfd
);
2620 /* Write out any associated aux entries. Most of the aux entries
2621 will have been modified in _bfd_coff_link_input_bfd. We have to
2622 handle section aux entries here, now that we have the final
2623 relocation and line number counts. */
2624 for (i
= 0; i
< isym
.n_numaux
; i
++)
2626 union internal_auxent
*auxp
;
2630 /* Look for a section aux entry here using the same tests that
2631 coff_swap_aux_out uses. */
2633 && (isym
.n_sclass
== C_STAT
2634 || isym
.n_sclass
== C_HIDDEN
)
2635 && isym
.n_type
== T_NULL
2636 && (h
->root
.type
== bfd_link_hash_defined
2637 || h
->root
.type
== bfd_link_hash_defweak
))
2641 sec
= h
->root
.u
.def
.section
->output_section
;
2644 auxp
->x_scn
.x_scnlen
= sec
->size
;
2646 /* For PE, an overflow on the final link reportedly does
2647 not matter. FIXME: Why not? */
2648 if (sec
->reloc_count
> 0xffff
2649 && (! obj_pe (output_bfd
)
2650 || finfo
->info
->relocatable
))
2651 (*_bfd_error_handler
)
2652 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2653 bfd_get_filename (output_bfd
),
2654 bfd_get_section_name (output_bfd
, sec
),
2657 if (sec
->lineno_count
> 0xffff
2658 && (! obj_pe (output_bfd
)
2659 || finfo
->info
->relocatable
))
2660 (*_bfd_error_handler
)
2661 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2662 bfd_get_filename (output_bfd
),
2663 bfd_get_section_name (output_bfd
, sec
),
2666 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2667 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2668 auxp
->x_scn
.x_checksum
= 0;
2669 auxp
->x_scn
.x_associated
= 0;
2670 auxp
->x_scn
.x_comdat
= 0;
2674 bfd_coff_swap_aux_out (output_bfd
, auxp
, isym
.n_type
,
2675 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2677 if (bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2679 finfo
->failed
= TRUE
;
2682 ++obj_raw_syment_count (output_bfd
);
2688 /* Write out task global symbols, converting them to statics. Called
2689 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2690 the dirty work, if the symbol we are processing needs conversion. */
2693 _bfd_coff_write_task_globals (struct coff_link_hash_entry
*h
, void *data
)
2695 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2696 bfd_boolean rtnval
= TRUE
;
2697 bfd_boolean save_global_to_static
;
2699 if (h
->root
.type
== bfd_link_hash_warning
)
2700 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2704 switch (h
->root
.type
)
2706 case bfd_link_hash_defined
:
2707 case bfd_link_hash_defweak
:
2708 save_global_to_static
= finfo
->global_to_static
;
2709 finfo
->global_to_static
= TRUE
;
2710 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2711 finfo
->global_to_static
= save_global_to_static
;
2720 /* Handle a link order which is supposed to generate a reloc. */
2723 _bfd_coff_reloc_link_order (bfd
*output_bfd
,
2724 struct coff_final_link_info
*finfo
,
2725 asection
*output_section
,
2726 struct bfd_link_order
*link_order
)
2728 reloc_howto_type
*howto
;
2729 struct internal_reloc
*irel
;
2730 struct coff_link_hash_entry
**rel_hash_ptr
;
2732 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2735 bfd_set_error (bfd_error_bad_value
);
2739 if (link_order
->u
.reloc
.p
->addend
!= 0)
2743 bfd_reloc_status_type rstat
;
2747 size
= bfd_get_reloc_size (howto
);
2748 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2752 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2753 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,\
2760 case bfd_reloc_outofrange
:
2762 case bfd_reloc_overflow
:
2763 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2765 (link_order
->type
== bfd_section_reloc_link_order
2766 ? bfd_section_name (output_bfd
,
2767 link_order
->u
.reloc
.p
->u
.section
)
2768 : link_order
->u
.reloc
.p
->u
.name
),
2769 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2770 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2777 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2778 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
2785 /* Store the reloc information in the right place. It will get
2786 swapped and written out at the end of the final_link routine. */
2787 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2788 + output_section
->reloc_count
);
2789 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2790 + output_section
->reloc_count
);
2792 memset (irel
, 0, sizeof (struct internal_reloc
));
2793 *rel_hash_ptr
= NULL
;
2795 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2797 if (link_order
->type
== bfd_section_reloc_link_order
)
2799 /* We need to somehow locate a symbol in the right section. The
2800 symbol must either have a value of zero, or we must adjust
2801 the addend by the value of the symbol. FIXME: Write this
2802 when we need it. The old linker couldn't handle this anyhow. */
2804 *rel_hash_ptr
= NULL
;
2809 struct coff_link_hash_entry
*h
;
2811 h
= ((struct coff_link_hash_entry
*)
2812 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2813 link_order
->u
.reloc
.p
->u
.name
,
2814 FALSE
, FALSE
, TRUE
));
2818 irel
->r_symndx
= h
->indx
;
2821 /* Set the index to -2 to force this symbol to get
2830 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2831 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2832 (asection
*) NULL
, (bfd_vma
) 0)))
2838 /* FIXME: Is this always right? */
2839 irel
->r_type
= howto
->type
;
2841 /* r_size is only used on the RS/6000, which needs its own linker
2842 routines anyhow. r_extern is only used for ECOFF. */
2844 /* FIXME: What is the right value for r_offset? Is zero OK? */
2845 ++output_section
->reloc_count
;
2850 /* A basic reloc handling routine which may be used by processors with
2854 _bfd_coff_generic_relocate_section (bfd
*output_bfd
,
2855 struct bfd_link_info
*info
,
2857 asection
*input_section
,
2859 struct internal_reloc
*relocs
,
2860 struct internal_syment
*syms
,
2861 asection
**sections
)
2863 struct internal_reloc
*rel
;
2864 struct internal_reloc
*relend
;
2867 relend
= rel
+ input_section
->reloc_count
;
2868 for (; rel
< relend
; rel
++)
2871 struct coff_link_hash_entry
*h
;
2872 struct internal_syment
*sym
;
2875 reloc_howto_type
*howto
;
2876 bfd_reloc_status_type rstat
;
2878 symndx
= rel
->r_symndx
;
2886 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2888 (*_bfd_error_handler
)
2889 ("%B: illegal symbol index %ld in relocs", input_bfd
, symndx
);
2894 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2895 sym
= syms
+ symndx
;
2898 /* COFF treats common symbols in one of two ways. Either the
2899 size of the symbol is included in the section contents, or it
2900 is not. We assume that the size is not included, and force
2901 the rtype_to_howto function to adjust the addend as needed. */
2902 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2903 addend
= - sym
->n_value
;
2907 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2912 /* If we are doing a relocatable link, then we can just ignore
2913 a PC relative reloc that is pcrel_offset. It will already
2914 have the correct value. If this is not a relocatable link,
2915 then we should ignore the symbol value. */
2916 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2918 if (info
->relocatable
)
2920 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2921 addend
+= sym
->n_value
;
2932 sec
= bfd_abs_section_ptr
;
2937 sec
= sections
[symndx
];
2938 val
= (sec
->output_section
->vma
2939 + sec
->output_offset
2941 if (! obj_pe (input_bfd
))
2947 if (h
->root
.type
== bfd_link_hash_defined
2948 || h
->root
.type
== bfd_link_hash_defweak
)
2950 /* Defined weak symbols are a GNU extension. */
2953 sec
= h
->root
.u
.def
.section
;
2954 val
= (h
->root
.u
.def
.value
2955 + sec
->output_section
->vma
2956 + sec
->output_offset
);
2959 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2961 if (h
->symbol_class
== C_NT_WEAK
&& h
->numaux
== 1)
2963 /* See _Microsoft Portable Executable and Common Object
2964 File Format Specification_, section 5.5.3.
2965 Note that weak symbols without aux records are a GNU
2967 FIXME: All weak externals are treated as having
2968 characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
2969 These behave as per SVR4 ABI: A library member
2970 will resolve a weak external only if a normal
2971 external causes the library member to be linked.
2972 See also linker.c: generic_link_check_archive_element. */
2974 struct coff_link_hash_entry
*h2
=
2975 h
->auxbfd
->tdata
.coff_obj_data
->sym_hashes
[
2976 h
->aux
->x_sym
.x_tagndx
.l
];
2978 if (!h2
|| h2
->root
.type
== bfd_link_hash_undefined
)
2980 sec
= bfd_abs_section_ptr
;
2985 sec
= h2
->root
.u
.def
.section
;
2986 val
= h2
->root
.u
.def
.value
2987 + sec
->output_section
->vma
+ sec
->output_offset
;
2991 /* This is a GNU extension. */
2995 else if (! info
->relocatable
)
2997 if (! ((*info
->callbacks
->undefined_symbol
)
2998 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2999 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
3004 if (info
->base_file
)
3006 /* Emit a reloc if the backend thinks it needs it. */
3007 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
3009 /* Relocation to a symbol in a section which isn't
3010 absolute. We output the address here to a file.
3011 This file is then read by dlltool when generating the
3012 reloc section. Note that the base file is not
3013 portable between systems. We write out a bfd_vma here,
3014 and dlltool reads in a bfd_vma. */
3015 bfd_vma addr
= (rel
->r_vaddr
3016 - input_section
->vma
3017 + input_section
->output_offset
3018 + input_section
->output_section
->vma
);
3019 if (coff_data (output_bfd
)->pe
)
3020 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
3021 if (fwrite (&addr
, 1, sizeof (bfd_vma
), (FILE *) info
->base_file
)
3022 != sizeof (bfd_vma
))
3024 bfd_set_error (bfd_error_system_call
);
3030 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3032 rel
->r_vaddr
- input_section
->vma
,
3041 case bfd_reloc_outofrange
:
3042 (*_bfd_error_handler
)
3043 (_("%B: bad reloc address 0x%lx in section `%A'"),
3044 input_bfd
, input_section
, (unsigned long) rel
->r_vaddr
);
3046 case bfd_reloc_overflow
:
3049 char buf
[SYMNMLEN
+ 1];
3057 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3062 if (! ((*info
->callbacks
->reloc_overflow
)
3063 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3064 (bfd_vma
) 0, input_bfd
, input_section
,
3065 rel
->r_vaddr
- input_section
->vma
)))