1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file contains the COFF backend linker code. */
28 #include "coff/internal.h"
31 static bfd_boolean
coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
);
32 static bfd_boolean
coff_link_check_archive_element (bfd
*abfd
, struct bfd_link_info
*info
, bfd_boolean
*pneeded
);
33 static bfd_boolean
coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
);
35 /* Return TRUE if SYM is a weak, external symbol. */
36 #define IS_WEAK_EXTERNAL(abfd, sym) \
37 ((sym).n_sclass == C_WEAKEXT \
38 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
40 /* Return TRUE if SYM is an external symbol. */
41 #define IS_EXTERNAL(abfd, sym) \
42 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
44 /* Define macros so that the ISFCN, et. al., macros work correctly.
45 These macros are defined in include/coff/internal.h in terms of
46 N_TMASK, etc. These definitions require a user to define local
47 variables with the appropriate names, and with values from the
48 coff_data (abfd) structure. */
50 #define N_TMASK n_tmask
51 #define N_BTSHFT n_btshft
52 #define N_BTMASK n_btmask
54 /* Create an entry in a COFF linker hash table. */
56 struct bfd_hash_entry
*
57 _bfd_coff_link_hash_newfunc (struct bfd_hash_entry
*entry
,
58 struct bfd_hash_table
*table
,
61 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
63 /* Allocate the structure if it has not already been allocated by a
65 if (ret
== (struct coff_link_hash_entry
*) NULL
)
66 ret
= ((struct coff_link_hash_entry
*)
67 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
68 if (ret
== (struct coff_link_hash_entry
*) NULL
)
69 return (struct bfd_hash_entry
*) ret
;
71 /* Call the allocation method of the superclass. */
72 ret
= ((struct coff_link_hash_entry
*)
73 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
75 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
77 /* Set local fields. */
86 return (struct bfd_hash_entry
*) ret
;
89 /* Initialize a COFF linker hash table. */
92 _bfd_coff_link_hash_table_init (struct coff_link_hash_table
*table
,
94 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
95 struct bfd_hash_table
*,
98 table
->stab_info
= NULL
;
99 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
102 /* Create a COFF linker hash table. */
104 struct bfd_link_hash_table
*
105 _bfd_coff_link_hash_table_create (bfd
*abfd
)
107 struct coff_link_hash_table
*ret
;
108 bfd_size_type amt
= sizeof (struct coff_link_hash_table
);
110 ret
= bfd_malloc (amt
);
114 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
115 _bfd_coff_link_hash_newfunc
))
118 return (struct bfd_link_hash_table
*) NULL
;
123 /* Create an entry in a COFF debug merge hash table. */
125 struct bfd_hash_entry
*
126 _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry
*entry
,
127 struct bfd_hash_table
*table
,
130 struct coff_debug_merge_hash_entry
*ret
=
131 (struct coff_debug_merge_hash_entry
*) entry
;
133 /* Allocate the structure if it has not already been allocated by a
135 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
136 ret
= ((struct coff_debug_merge_hash_entry
*)
137 bfd_hash_allocate (table
,
138 sizeof (struct coff_debug_merge_hash_entry
)));
139 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
140 return (struct bfd_hash_entry
*) ret
;
142 /* Call the allocation method of the superclass. */
143 ret
= ((struct coff_debug_merge_hash_entry
*)
144 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
145 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
147 /* Set local fields. */
151 return (struct bfd_hash_entry
*) ret
;
154 /* Given a COFF BFD, add symbols to the global hash table as
158 _bfd_coff_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
160 switch (bfd_get_format (abfd
))
163 return coff_link_add_object_symbols (abfd
, info
);
165 return _bfd_generic_link_add_archive_symbols
166 (abfd
, info
, coff_link_check_archive_element
);
168 bfd_set_error (bfd_error_wrong_format
);
173 /* Add symbols from a COFF object file. */
176 coff_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
178 if (! _bfd_coff_get_external_symbols (abfd
))
180 if (! coff_link_add_symbols (abfd
, info
))
183 if (! info
->keep_memory
184 && ! _bfd_coff_free_symbols (abfd
))
190 /* Look through the symbols to see if this object file should be
191 included in the link. */
194 coff_link_check_ar_symbols (bfd
*abfd
,
195 struct bfd_link_info
*info
,
196 bfd_boolean
*pneeded
)
198 bfd_size_type symesz
;
204 symesz
= bfd_coff_symesz (abfd
);
205 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
206 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
207 while (esym
< esym_end
)
209 struct internal_syment sym
;
210 enum coff_symbol_classification classification
;
212 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
214 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
215 if (classification
== COFF_SYMBOL_GLOBAL
216 || classification
== COFF_SYMBOL_COMMON
)
219 char buf
[SYMNMLEN
+ 1];
220 struct bfd_link_hash_entry
*h
;
222 /* This symbol is externally visible, and is defined by this
224 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
227 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
231 && info
->pei386_auto_import
232 && !strncmp (name
,"__imp_", 6))
233 h
= bfd_link_hash_lookup (info
->hash
, name
+ 6, FALSE
, FALSE
, TRUE
);
235 /* We are only interested in symbols that are currently
236 undefined. If a symbol is currently known to be common,
237 COFF linkers do not bring in an object file which defines
239 if (h
!= (struct bfd_link_hash_entry
*) NULL
240 && h
->type
== bfd_link_hash_undefined
)
242 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
249 esym
+= (sym
.n_numaux
+ 1) * symesz
;
252 /* We do not need this object file. */
256 /* Check a single archive element to see if we need to include it in
257 the link. *PNEEDED is set according to whether this element is
258 needed in the link or not. This is called via
259 _bfd_generic_link_add_archive_symbols. */
262 coff_link_check_archive_element (bfd
*abfd
,
263 struct bfd_link_info
*info
,
264 bfd_boolean
*pneeded
)
266 if (! _bfd_coff_get_external_symbols (abfd
))
269 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
273 && ! coff_link_add_symbols (abfd
, info
))
276 if ((! info
->keep_memory
|| ! *pneeded
)
277 && ! _bfd_coff_free_symbols (abfd
))
283 /* Add all the symbols from an object file to the hash table. */
286 coff_link_add_symbols (bfd
*abfd
,
287 struct bfd_link_info
*info
)
289 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
290 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
291 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
292 bfd_boolean keep_syms
;
293 bfd_boolean default_copy
;
294 bfd_size_type symcount
;
295 struct coff_link_hash_entry
**sym_hash
;
296 bfd_size_type symesz
;
301 /* Keep the symbols during this function, in case the linker needs
302 to read the generic symbols in order to report an error message. */
303 keep_syms
= obj_coff_keep_syms (abfd
);
304 obj_coff_keep_syms (abfd
) = TRUE
;
306 if (info
->keep_memory
)
307 default_copy
= FALSE
;
311 symcount
= obj_raw_syment_count (abfd
);
313 /* We keep a list of the linker hash table entries that correspond
314 to particular symbols. */
315 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
316 sym_hash
= bfd_zalloc (abfd
, amt
);
317 if (sym_hash
== NULL
&& symcount
!= 0)
319 obj_coff_sym_hashes (abfd
) = sym_hash
;
321 symesz
= bfd_coff_symesz (abfd
);
322 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
323 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
324 esym_end
= esym
+ symcount
* symesz
;
325 while (esym
< esym_end
)
327 struct internal_syment sym
;
328 enum coff_symbol_classification classification
;
331 bfd_coff_swap_sym_in (abfd
, esym
, &sym
);
333 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
334 if (classification
!= COFF_SYMBOL_LOCAL
)
337 char buf
[SYMNMLEN
+ 1];
343 /* This symbol is externally visible. */
345 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
349 /* We must copy the name into memory if we got it from the
350 syment itself, rather than the string table. */
352 if (sym
._n
._n_n
._n_zeroes
!= 0
353 || sym
._n
._n_n
._n_offset
== 0)
358 switch (classification
)
363 case COFF_SYMBOL_GLOBAL
:
364 flags
= BSF_EXPORT
| BSF_GLOBAL
;
365 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
367 value
-= section
->vma
;
370 case COFF_SYMBOL_UNDEFINED
:
372 section
= bfd_und_section_ptr
;
375 case COFF_SYMBOL_COMMON
:
377 section
= bfd_com_section_ptr
;
380 case COFF_SYMBOL_PE_SECTION
:
381 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
382 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
386 if (IS_WEAK_EXTERNAL (abfd
, sym
))
391 /* In the PE format, section symbols actually refer to the
392 start of the output section. We handle them specially
394 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
396 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
397 name
, FALSE
, copy
, FALSE
);
398 if (*sym_hash
!= NULL
)
400 if (((*sym_hash
)->coff_link_hash_flags
401 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
402 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
403 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
404 (*_bfd_error_handler
)
405 ("Warning: symbol `%s' is both section and non-section",
412 /* The Microsoft Visual C compiler does string pooling by
413 hashing the constants to an internal symbol name, and
414 relying on the linker comdat support to discard
415 duplicate names. However, if one string is a literal and
416 one is a data initializer, one will end up in the .data
417 section and one will end up in the .rdata section. The
418 Microsoft linker will combine them into the .data
419 section, which seems to be wrong since it might cause the
422 As long as there are no external references to the
423 symbols, which there shouldn't be, we can treat the .data
424 and .rdata instances as separate symbols. The comdat
425 code in the linker will do the appropriate merging. Here
426 we avoid getting a multiple definition error for one of
427 these special symbols.
429 FIXME: I don't think this will work in the case where
430 there are two object files which use the constants as a
431 literal and two object files which use it as a data
432 initializer. One or the other of the second object files
433 is going to wind up with an inappropriate reference. */
435 && (classification
== COFF_SYMBOL_GLOBAL
436 || classification
== COFF_SYMBOL_PE_SECTION
)
437 && section
->comdat
!= NULL
438 && strncmp (name
, "??_", 3) == 0
439 && strcmp (name
, section
->comdat
->name
) == 0)
441 if (*sym_hash
== NULL
)
442 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
443 name
, FALSE
, copy
, FALSE
);
444 if (*sym_hash
!= NULL
445 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
446 && (*sym_hash
)->root
.u
.def
.section
->comdat
!= NULL
447 && strcmp ((*sym_hash
)->root
.u
.def
.section
->comdat
->name
,
448 section
->comdat
->name
) == 0)
454 if (! (bfd_coff_link_add_one_symbol
455 (info
, abfd
, name
, flags
, section
, value
,
456 (const char *) NULL
, copy
, FALSE
,
457 (struct bfd_link_hash_entry
**) sym_hash
)))
461 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
462 (*sym_hash
)->coff_link_hash_flags
|=
463 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
465 /* Limit the alignment of a common symbol to the possible
466 alignment of a section. There is no point to permitting
467 a higher alignment for a common symbol: we can not
468 guarantee it, and it may cause us to allocate extra space
469 in the common section. */
470 if (section
== bfd_com_section_ptr
471 && (*sym_hash
)->root
.type
== bfd_link_hash_common
472 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
473 > bfd_coff_default_section_alignment_power (abfd
)))
474 (*sym_hash
)->root
.u
.c
.p
->alignment_power
475 = bfd_coff_default_section_alignment_power (abfd
);
477 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
479 /* If we don't have any symbol information currently in
480 the hash table, or if we are looking at a symbol
481 definition, then update the symbol class and type in
483 if (((*sym_hash
)->class == C_NULL
484 && (*sym_hash
)->type
== T_NULL
)
487 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
488 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
490 (*sym_hash
)->class = sym
.n_sclass
;
491 if (sym
.n_type
!= T_NULL
)
493 /* We want to warn if the type changed, but not
494 if it changed from an unspecified type.
495 Testing the whole type byte may work, but the
496 change from (e.g.) a function of unspecified
497 type to function of known type also wants to
499 if ((*sym_hash
)->type
!= T_NULL
500 && (*sym_hash
)->type
!= sym
.n_type
501 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
502 && (BTYPE ((*sym_hash
)->type
) == T_NULL
503 || BTYPE (sym
.n_type
) == T_NULL
)))
504 (*_bfd_error_handler
)
505 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
506 name
, (*sym_hash
)->type
, sym
.n_type
,
507 bfd_archive_filename (abfd
));
509 /* We don't want to change from a meaningful
510 base type to a null one, but if we know
511 nothing, take what little we might now know. */
512 if (BTYPE (sym
.n_type
) != T_NULL
513 || (*sym_hash
)->type
== T_NULL
)
514 (*sym_hash
)->type
= sym
.n_type
;
516 (*sym_hash
)->auxbfd
= abfd
;
517 if (sym
.n_numaux
!= 0)
519 union internal_auxent
*alloc
;
522 union internal_auxent
*iaux
;
524 (*sym_hash
)->numaux
= sym
.n_numaux
;
525 alloc
= ((union internal_auxent
*)
526 bfd_hash_allocate (&info
->hash
->table
,
528 * sizeof (*alloc
))));
531 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
533 i
++, eaux
+= symesz
, iaux
++)
534 bfd_coff_swap_aux_in (abfd
, eaux
, sym
.n_type
,
535 sym
.n_sclass
, (int) i
,
537 (*sym_hash
)->aux
= alloc
;
542 if (classification
== COFF_SYMBOL_PE_SECTION
543 && (*sym_hash
)->numaux
!= 0)
545 /* Some PE sections (such as .bss) have a zero size in
546 the section header, but a non-zero size in the AUX
547 record. Correct that here.
549 FIXME: This is not at all the right place to do this.
550 For example, it won't help objdump. This needs to be
551 done when we swap in the section header. */
552 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
553 if (section
->_raw_size
== 0)
554 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
556 /* FIXME: We could test whether the section sizes
557 matches the size in the aux entry, but apparently
558 that sometimes fails unexpectedly. */
562 esym
+= (sym
.n_numaux
+ 1) * symesz
;
563 sym_hash
+= sym
.n_numaux
+ 1;
566 /* If this is a non-traditional, non-relocatable link, try to
567 optimize the handling of any .stab/.stabstr sections. */
568 if (! info
->relocatable
569 && ! info
->traditional_format
570 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
571 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
573 asection
*stab
, *stabstr
;
575 stab
= bfd_get_section_by_name (abfd
, ".stab");
578 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
582 struct coff_link_hash_table
*table
;
583 struct coff_section_tdata
*secdata
;
585 secdata
= coff_section_data (abfd
, stab
);
588 amt
= sizeof (struct coff_section_tdata
);
589 stab
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
590 if (stab
->used_by_bfd
== NULL
)
592 secdata
= coff_section_data (abfd
, stab
);
595 table
= coff_hash_table (info
);
597 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
599 &secdata
->stab_info
))
605 obj_coff_keep_syms (abfd
) = keep_syms
;
610 obj_coff_keep_syms (abfd
) = keep_syms
;
614 /* Do the final link step. */
617 _bfd_coff_final_link (bfd
*abfd
,
618 struct bfd_link_info
*info
)
620 bfd_size_type symesz
;
621 struct coff_final_link_info finfo
;
622 bfd_boolean debug_merge_allocated
;
623 bfd_boolean long_section_names
;
625 struct bfd_link_order
*p
;
626 bfd_size_type max_sym_count
;
627 bfd_size_type max_lineno_count
;
628 bfd_size_type max_reloc_count
;
629 bfd_size_type max_output_reloc_count
;
630 bfd_size_type max_contents_size
;
631 file_ptr rel_filepos
;
633 file_ptr line_filepos
;
636 bfd_byte
*external_relocs
= NULL
;
637 char strbuf
[STRING_SIZE_SIZE
];
640 symesz
= bfd_coff_symesz (abfd
);
643 finfo
.output_bfd
= abfd
;
645 finfo
.section_info
= NULL
;
646 finfo
.last_file_index
= -1;
647 finfo
.last_bf_index
= -1;
648 finfo
.internal_syms
= NULL
;
649 finfo
.sec_ptrs
= NULL
;
650 finfo
.sym_indices
= NULL
;
651 finfo
.outsyms
= NULL
;
652 finfo
.linenos
= NULL
;
653 finfo
.contents
= NULL
;
654 finfo
.external_relocs
= NULL
;
655 finfo
.internal_relocs
= NULL
;
656 finfo
.global_to_static
= FALSE
;
657 debug_merge_allocated
= FALSE
;
659 coff_data (abfd
)->link_info
= info
;
661 finfo
.strtab
= _bfd_stringtab_init ();
662 if (finfo
.strtab
== NULL
)
665 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
667 debug_merge_allocated
= TRUE
;
669 /* Compute the file positions for all the sections. */
670 if (! abfd
->output_has_begun
)
672 if (! bfd_coff_compute_section_file_positions (abfd
))
676 /* Count the line numbers and relocation entries required for the
677 output file. Set the file positions for the relocs. */
678 rel_filepos
= obj_relocbase (abfd
);
679 relsz
= bfd_coff_relsz (abfd
);
680 max_contents_size
= 0;
681 max_lineno_count
= 0;
684 long_section_names
= FALSE
;
685 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
689 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
691 if (p
->type
== bfd_indirect_link_order
)
695 sec
= p
->u
.indirect
.section
;
697 /* Mark all sections which are to be included in the
698 link. This will normally be every section. We need
699 to do this so that we can identify any sections which
700 the linker has decided to not include. */
701 sec
->linker_mark
= TRUE
;
703 if (info
->strip
== strip_none
704 || info
->strip
== strip_some
)
705 o
->lineno_count
+= sec
->lineno_count
;
707 if (info
->relocatable
)
708 o
->reloc_count
+= sec
->reloc_count
;
710 if (sec
->_raw_size
> max_contents_size
)
711 max_contents_size
= sec
->_raw_size
;
712 if (sec
->lineno_count
> max_lineno_count
)
713 max_lineno_count
= sec
->lineno_count
;
714 if (sec
->reloc_count
> max_reloc_count
)
715 max_reloc_count
= sec
->reloc_count
;
717 else if (info
->relocatable
718 && (p
->type
== bfd_section_reloc_link_order
719 || p
->type
== bfd_symbol_reloc_link_order
))
722 if (o
->reloc_count
== 0)
726 o
->flags
|= SEC_RELOC
;
727 o
->rel_filepos
= rel_filepos
;
728 rel_filepos
+= o
->reloc_count
* relsz
;
729 /* In PE COFF, if there are at least 0xffff relocations an
730 extra relocation will be written out to encode the count. */
731 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
732 rel_filepos
+= relsz
;
735 if (bfd_coff_long_section_names (abfd
)
736 && strlen (o
->name
) > SCNNMLEN
)
738 /* This section has a long name which must go in the string
739 table. This must correspond to the code in
740 coff_write_object_contents which puts the string index
741 into the s_name field of the section header. That is why
742 we pass hash as FALSE. */
743 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, FALSE
, FALSE
)
744 == (bfd_size_type
) -1)
746 long_section_names
= TRUE
;
750 /* If doing a relocatable link, allocate space for the pointers we
752 if (info
->relocatable
)
756 /* We use section_count + 1, rather than section_count, because
757 the target_index fields are 1 based. */
758 amt
= abfd
->section_count
+ 1;
759 amt
*= sizeof (struct coff_link_section_info
);
760 finfo
.section_info
= bfd_malloc (amt
);
761 if (finfo
.section_info
== NULL
)
763 for (i
= 0; i
<= abfd
->section_count
; i
++)
765 finfo
.section_info
[i
].relocs
= NULL
;
766 finfo
.section_info
[i
].rel_hashes
= NULL
;
770 /* We now know the size of the relocs, so we can determine the file
771 positions of the line numbers. */
772 line_filepos
= rel_filepos
;
773 linesz
= bfd_coff_linesz (abfd
);
774 max_output_reloc_count
= 0;
775 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
777 if (o
->lineno_count
== 0)
781 o
->line_filepos
= line_filepos
;
782 line_filepos
+= o
->lineno_count
* linesz
;
785 if (o
->reloc_count
!= 0)
787 /* We don't know the indices of global symbols until we have
788 written out all the local symbols. For each section in
789 the output file, we keep an array of pointers to hash
790 table entries. Each entry in the array corresponds to a
791 reloc. When we find a reloc against a global symbol, we
792 set the corresponding entry in this array so that we can
793 fix up the symbol index after we have written out all the
796 Because of this problem, we also keep the relocs in
797 memory until the end of the link. This wastes memory,
798 but only when doing a relocatable link, which is not the
800 BFD_ASSERT (info
->relocatable
);
801 amt
= o
->reloc_count
;
802 amt
*= sizeof (struct internal_reloc
);
803 finfo
.section_info
[o
->target_index
].relocs
= bfd_malloc (amt
);
804 amt
= o
->reloc_count
;
805 amt
*= sizeof (struct coff_link_hash_entry
*);
806 finfo
.section_info
[o
->target_index
].rel_hashes
= bfd_malloc (amt
);
807 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
808 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
811 if (o
->reloc_count
> max_output_reloc_count
)
812 max_output_reloc_count
= o
->reloc_count
;
815 /* Reset the reloc and lineno counts, so that we can use them to
816 count the number of entries we have output so far. */
821 obj_sym_filepos (abfd
) = line_filepos
;
823 /* Figure out the largest number of symbols in an input BFD. Take
824 the opportunity to clear the output_has_begun fields of all the
827 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
831 sub
->output_has_begun
= FALSE
;
832 sz
= obj_raw_syment_count (sub
);
833 if (sz
> max_sym_count
)
837 /* Allocate some buffers used while linking. */
838 amt
= max_sym_count
* sizeof (struct internal_syment
);
839 finfo
.internal_syms
= bfd_malloc (amt
);
840 amt
= max_sym_count
* sizeof (asection
*);
841 finfo
.sec_ptrs
= bfd_malloc (amt
);
842 amt
= max_sym_count
* sizeof (long);
843 finfo
.sym_indices
= bfd_malloc (amt
);
844 finfo
.outsyms
= bfd_malloc ((max_sym_count
+ 1) * symesz
);
845 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
846 finfo
.linenos
= bfd_malloc (amt
);
847 finfo
.contents
= bfd_malloc (max_contents_size
);
848 amt
= max_reloc_count
* relsz
;
849 finfo
.external_relocs
= bfd_malloc (amt
);
850 if (! info
->relocatable
)
852 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
853 finfo
.internal_relocs
= bfd_malloc (amt
);
855 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
856 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
857 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
858 || finfo
.outsyms
== NULL
859 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
860 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
861 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
862 || (! info
->relocatable
863 && finfo
.internal_relocs
== NULL
864 && max_reloc_count
> 0))
867 /* We now know the position of everything in the file, except that
868 we don't know the size of the symbol table and therefore we don't
869 know where the string table starts. We just build the string
870 table in memory as we go along. We process all the relocations
871 for a single input file at once. */
872 obj_raw_syment_count (abfd
) = 0;
874 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
876 if (! bfd_coff_start_final_link (abfd
, info
))
880 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
882 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
884 if (p
->type
== bfd_indirect_link_order
885 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
887 sub
= p
->u
.indirect
.section
->owner
;
888 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
890 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
892 sub
->output_has_begun
= TRUE
;
895 else if (p
->type
== bfd_section_reloc_link_order
896 || p
->type
== bfd_symbol_reloc_link_order
)
898 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
903 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
909 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
912 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
914 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
915 debug_merge_allocated
= FALSE
;
917 if (finfo
.internal_syms
!= NULL
)
919 free (finfo
.internal_syms
);
920 finfo
.internal_syms
= NULL
;
922 if (finfo
.sec_ptrs
!= NULL
)
924 free (finfo
.sec_ptrs
);
925 finfo
.sec_ptrs
= NULL
;
927 if (finfo
.sym_indices
!= NULL
)
929 free (finfo
.sym_indices
);
930 finfo
.sym_indices
= NULL
;
932 if (finfo
.linenos
!= NULL
)
934 free (finfo
.linenos
);
935 finfo
.linenos
= NULL
;
937 if (finfo
.contents
!= NULL
)
939 free (finfo
.contents
);
940 finfo
.contents
= NULL
;
942 if (finfo
.external_relocs
!= NULL
)
944 free (finfo
.external_relocs
);
945 finfo
.external_relocs
= NULL
;
947 if (finfo
.internal_relocs
!= NULL
)
949 free (finfo
.internal_relocs
);
950 finfo
.internal_relocs
= NULL
;
953 /* The value of the last C_FILE symbol is supposed to be the symbol
954 index of the first external symbol. Write it out again if
956 if (finfo
.last_file_index
!= -1
957 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
961 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
962 bfd_coff_swap_sym_out (abfd
, &finfo
.last_file
,
965 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
966 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
967 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
971 /* If doing task linking (ld --task-link) then make a pass through the
972 global symbols, writing out any that are defined, and making them
976 finfo
.failed
= FALSE
;
977 coff_link_hash_traverse (coff_hash_table (info
),
978 _bfd_coff_write_task_globals
, &finfo
);
983 /* Write out the global symbols. */
984 finfo
.failed
= FALSE
;
985 coff_link_hash_traverse (coff_hash_table (info
),
986 _bfd_coff_write_global_sym
, &finfo
);
990 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
991 if (finfo
.outsyms
!= NULL
)
993 free (finfo
.outsyms
);
994 finfo
.outsyms
= NULL
;
997 if (info
->relocatable
&& max_output_reloc_count
> 0)
999 /* Now that we have written out all the global symbols, we know
1000 the symbol indices to use for relocs against them, and we can
1001 finally write out the relocs. */
1002 amt
= max_output_reloc_count
* relsz
;
1003 external_relocs
= bfd_malloc (amt
);
1004 if (external_relocs
== NULL
)
1007 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1009 struct internal_reloc
*irel
;
1010 struct internal_reloc
*irelend
;
1011 struct coff_link_hash_entry
**rel_hash
;
1014 if (o
->reloc_count
== 0)
1017 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1018 irelend
= irel
+ o
->reloc_count
;
1019 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1020 erel
= external_relocs
;
1021 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1023 if (*rel_hash
!= NULL
)
1025 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1026 irel
->r_symndx
= (*rel_hash
)->indx
;
1028 bfd_coff_swap_reloc_out (abfd
, irel
, erel
);
1031 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1032 || (bfd_bwrite (external_relocs
,
1033 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1034 != (bfd_size_type
) relsz
* o
->reloc_count
))
1038 free (external_relocs
);
1039 external_relocs
= NULL
;
1042 /* Free up the section information. */
1043 if (finfo
.section_info
!= NULL
)
1047 for (i
= 0; i
< abfd
->section_count
; i
++)
1049 if (finfo
.section_info
[i
].relocs
!= NULL
)
1050 free (finfo
.section_info
[i
].relocs
);
1051 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1052 free (finfo
.section_info
[i
].rel_hashes
);
1054 free (finfo
.section_info
);
1055 finfo
.section_info
= NULL
;
1058 /* If we have optimized stabs strings, output them. */
1059 if (coff_hash_table (info
)->stab_info
!= NULL
)
1061 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1065 /* Write out the string table. */
1066 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1070 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1071 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1074 #if STRING_SIZE_SIZE == 4
1076 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1079 #error Change H_PUT_32 above
1082 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1083 != STRING_SIZE_SIZE
)
1086 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1089 obj_coff_strings_written (abfd
) = TRUE
;
1092 _bfd_stringtab_free (finfo
.strtab
);
1094 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1095 not try to write out the symbols. */
1096 bfd_get_symcount (abfd
) = 0;
1101 if (debug_merge_allocated
)
1102 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1103 if (finfo
.strtab
!= NULL
)
1104 _bfd_stringtab_free (finfo
.strtab
);
1105 if (finfo
.section_info
!= NULL
)
1109 for (i
= 0; i
< abfd
->section_count
; i
++)
1111 if (finfo
.section_info
[i
].relocs
!= NULL
)
1112 free (finfo
.section_info
[i
].relocs
);
1113 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1114 free (finfo
.section_info
[i
].rel_hashes
);
1116 free (finfo
.section_info
);
1118 if (finfo
.internal_syms
!= NULL
)
1119 free (finfo
.internal_syms
);
1120 if (finfo
.sec_ptrs
!= NULL
)
1121 free (finfo
.sec_ptrs
);
1122 if (finfo
.sym_indices
!= NULL
)
1123 free (finfo
.sym_indices
);
1124 if (finfo
.outsyms
!= NULL
)
1125 free (finfo
.outsyms
);
1126 if (finfo
.linenos
!= NULL
)
1127 free (finfo
.linenos
);
1128 if (finfo
.contents
!= NULL
)
1129 free (finfo
.contents
);
1130 if (finfo
.external_relocs
!= NULL
)
1131 free (finfo
.external_relocs
);
1132 if (finfo
.internal_relocs
!= NULL
)
1133 free (finfo
.internal_relocs
);
1134 if (external_relocs
!= NULL
)
1135 free (external_relocs
);
1139 /* Parse out a -heap <reserved>,<commit> line. */
1142 dores_com (char *ptr
, bfd
*output_bfd
, int heap
)
1144 if (coff_data(output_bfd
)->pe
)
1146 int val
= strtoul (ptr
, &ptr
, 0);
1149 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1151 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1155 val
= strtoul (ptr
+1, &ptr
, 0);
1157 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1159 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1166 get_name (char *ptr
, char **dst
)
1171 while (*ptr
&& *ptr
!= ' ')
1177 /* Process any magic embedded commands in a section called .drectve. */
1180 process_embedded_commands (bfd
*output_bfd
,
1181 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1184 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1192 copy
= bfd_malloc (sec
->_raw_size
);
1196 if (! bfd_get_section_contents (abfd
, sec
, copy
, (bfd_vma
) 0, sec
->_raw_size
))
1201 e
= copy
+ sec
->_raw_size
;
1203 for (s
= copy
; s
< e
; )
1210 if (strncmp (s
,"-attr", 5) == 0)
1222 s
= get_name (s
, &name
);
1223 s
= get_name (s
, &attribs
);
1245 asec
= bfd_get_section_by_name (abfd
, name
);
1249 asec
->flags
|= SEC_CODE
;
1251 asec
->flags
|= SEC_READONLY
;
1254 else if (strncmp (s
,"-heap", 5) == 0)
1255 s
= dores_com (s
+5, output_bfd
, 1);
1257 else if (strncmp (s
,"-stack", 6) == 0)
1258 s
= dores_com (s
+6, output_bfd
, 0);
1267 /* Place a marker against all symbols which are used by relocations.
1268 This marker can be picked up by the 'do we skip this symbol ?'
1269 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1273 mark_relocs (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1277 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1280 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1282 struct internal_reloc
* internal_relocs
;
1283 struct internal_reloc
* irel
;
1284 struct internal_reloc
* irelend
;
1286 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1288 /* Don't mark relocs in excluded sections. */
1289 if (a
->output_section
== bfd_abs_section_ptr
)
1292 /* Read in the relocs. */
1293 internal_relocs
= _bfd_coff_read_internal_relocs
1294 (input_bfd
, a
, FALSE
,
1295 finfo
->external_relocs
,
1296 finfo
->info
->relocatable
,
1297 (finfo
->info
->relocatable
1298 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1299 : finfo
->internal_relocs
)
1302 if (internal_relocs
== NULL
)
1305 irel
= internal_relocs
;
1306 irelend
= irel
+ a
->reloc_count
;
1308 /* Place a mark in the sym_indices array (whose entries have
1309 been initialised to 0) for all of the symbols that are used
1310 in the relocation table. This will then be picked up in the
1311 skip/don't-skip pass. */
1312 for (; irel
< irelend
; irel
++)
1313 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1317 /* Link an input file into the linker output file. This function
1318 handles all the sections and relocations of the input file at once. */
1321 _bfd_coff_link_input_bfd (struct coff_final_link_info
*finfo
, bfd
*input_bfd
)
1323 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1324 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1326 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1328 bfd_boolean (*adjust_symndx
)
1329 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
1330 struct internal_reloc
*, bfd_boolean
*);
1332 const char *strings
;
1333 bfd_size_type syment_base
;
1334 bfd_boolean copy
, hash
;
1335 bfd_size_type isymesz
;
1336 bfd_size_type osymesz
;
1337 bfd_size_type linesz
;
1340 struct internal_syment
*isymp
;
1343 unsigned long output_index
;
1345 struct coff_link_hash_entry
**sym_hash
;
1348 /* Move all the symbols to the output file. */
1350 output_bfd
= finfo
->output_bfd
;
1352 syment_base
= obj_raw_syment_count (output_bfd
);
1353 isymesz
= bfd_coff_symesz (input_bfd
);
1354 osymesz
= bfd_coff_symesz (output_bfd
);
1355 linesz
= bfd_coff_linesz (input_bfd
);
1356 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1359 if (! finfo
->info
->keep_memory
)
1362 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1365 if (! _bfd_coff_get_external_symbols (input_bfd
))
1368 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1369 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1370 isymp
= finfo
->internal_syms
;
1371 secpp
= finfo
->sec_ptrs
;
1372 indexp
= finfo
->sym_indices
;
1373 output_index
= syment_base
;
1374 outsym
= finfo
->outsyms
;
1376 if (coff_data (output_bfd
)->pe
1377 && ! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1380 /* If we are going to perform relocations and also strip/discard some
1381 symbols then we must make sure that we do not strip/discard those
1382 symbols that are going to be involved in the relocations. */
1383 if (( finfo
->info
->strip
!= strip_none
1384 || finfo
->info
->discard
!= discard_none
)
1385 && finfo
->info
->relocatable
)
1387 /* Mark the symbol array as 'not-used'. */
1388 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1390 mark_relocs (finfo
, input_bfd
);
1393 while (esym
< esym_end
)
1395 struct internal_syment isym
;
1396 enum coff_symbol_classification classification
;
1399 bfd_boolean dont_skip_symbol
;
1402 bfd_coff_swap_sym_in (input_bfd
, esym
, isymp
);
1404 /* Make a copy of *isymp so that the relocate_section function
1405 always sees the original values. This is more reliable than
1406 always recomputing the symbol value even if we are stripping
1410 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1411 switch (classification
)
1415 case COFF_SYMBOL_GLOBAL
:
1416 case COFF_SYMBOL_PE_SECTION
:
1417 case COFF_SYMBOL_LOCAL
:
1418 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1420 case COFF_SYMBOL_COMMON
:
1421 *secpp
= bfd_com_section_ptr
;
1423 case COFF_SYMBOL_UNDEFINED
:
1424 *secpp
= bfd_und_section_ptr
;
1428 /* Extract the flag indicating if this symbol is used by a
1430 if ((finfo
->info
->strip
!= strip_none
1431 || finfo
->info
->discard
!= discard_none
)
1432 && finfo
->info
->relocatable
)
1433 dont_skip_symbol
= *indexp
;
1435 dont_skip_symbol
= FALSE
;
1441 add
= 1 + isym
.n_numaux
;
1443 /* If we are stripping all symbols, we want to skip this one. */
1444 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1449 switch (classification
)
1453 case COFF_SYMBOL_GLOBAL
:
1454 case COFF_SYMBOL_COMMON
:
1455 case COFF_SYMBOL_PE_SECTION
:
1456 /* This is a global symbol. Global symbols come at the
1457 end of the symbol table, so skip them for now.
1458 Locally defined function symbols, however, are an
1459 exception, and are not moved to the end. */
1461 if (! ISFCN (isym
.n_type
))
1465 case COFF_SYMBOL_UNDEFINED
:
1466 /* Undefined symbols are left for the end. */
1471 case COFF_SYMBOL_LOCAL
:
1472 /* This is a local symbol. Skip it if we are discarding
1474 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1480 #ifndef COFF_WITH_PE
1481 /* Skip section symbols for sections which are not going to be
1484 && dont_skip_symbol
== 0
1485 && isym
.n_sclass
== C_STAT
1486 && isym
.n_type
== T_NULL
1487 && isym
.n_numaux
> 0
1488 && (*secpp
)->output_section
== bfd_abs_section_ptr
)
1492 /* If we stripping debugging symbols, and this is a debugging
1493 symbol, then skip it. FIXME: gas sets the section to N_ABS
1494 for some types of debugging symbols; I don't know if this is
1495 a bug or not. In any case, we handle it here. */
1497 && finfo
->info
->strip
== strip_debugger
1498 && ! dont_skip_symbol
1499 && (isym
.n_scnum
== N_DEBUG
1500 || (isym
.n_scnum
== N_ABS
1501 && (isym
.n_sclass
== C_AUTO
1502 || isym
.n_sclass
== C_REG
1503 || isym
.n_sclass
== C_MOS
1504 || isym
.n_sclass
== C_MOE
1505 || isym
.n_sclass
== C_MOU
1506 || isym
.n_sclass
== C_ARG
1507 || isym
.n_sclass
== C_REGPARM
1508 || isym
.n_sclass
== C_FIELD
1509 || isym
.n_sclass
== C_EOS
))))
1512 /* If some symbols are stripped based on the name, work out the
1513 name and decide whether to skip this symbol. */
1515 && (finfo
->info
->strip
== strip_some
1516 || finfo
->info
->discard
== discard_l
))
1519 char buf
[SYMNMLEN
+ 1];
1521 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1525 if (! dont_skip_symbol
1526 && ((finfo
->info
->strip
== strip_some
1527 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
,
1530 && finfo
->info
->discard
== discard_l
1531 && bfd_is_local_label_name (input_bfd
, name
))))
1535 /* If this is an enum, struct, or union tag, see if we have
1536 already output an identical type. */
1538 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1539 && (isym
.n_sclass
== C_ENTAG
1540 || isym
.n_sclass
== C_STRTAG
1541 || isym
.n_sclass
== C_UNTAG
)
1542 && isym
.n_numaux
== 1)
1545 char buf
[SYMNMLEN
+ 1];
1546 struct coff_debug_merge_hash_entry
*mh
;
1547 struct coff_debug_merge_type
*mt
;
1548 union internal_auxent aux
;
1549 struct coff_debug_merge_element
**epp
;
1550 bfd_byte
*esl
, *eslend
;
1551 struct internal_syment
*islp
;
1554 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1558 /* Ignore fake names invented by compiler; treat them all as
1560 if (*name
== '~' || *name
== '.' || *name
== '$'
1561 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1562 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1565 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1570 /* Allocate memory to hold type information. If this turns
1571 out to be a duplicate, we pass this address to
1573 amt
= sizeof (struct coff_debug_merge_type
);
1574 mt
= bfd_alloc (input_bfd
, amt
);
1577 mt
->class = isym
.n_sclass
;
1579 /* Pick up the aux entry, which points to the end of the tag
1581 bfd_coff_swap_aux_in (input_bfd
, (esym
+ isymesz
),
1582 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1585 /* Gather the elements. */
1586 epp
= &mt
->elements
;
1587 mt
->elements
= NULL
;
1589 esl
= esym
+ 2 * isymesz
;
1590 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1591 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1592 while (esl
< eslend
)
1594 const char *elename
;
1595 char elebuf
[SYMNMLEN
+ 1];
1598 bfd_coff_swap_sym_in (input_bfd
, esl
, islp
);
1600 amt
= sizeof (struct coff_debug_merge_element
);
1601 *epp
= bfd_alloc (input_bfd
, amt
);
1605 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1607 if (elename
== NULL
)
1610 amt
= strlen (elename
) + 1;
1611 name_copy
= bfd_alloc (input_bfd
, amt
);
1612 if (name_copy
== NULL
)
1614 strcpy (name_copy
, elename
);
1616 (*epp
)->name
= name_copy
;
1617 (*epp
)->type
= islp
->n_type
;
1619 if (islp
->n_numaux
>= 1
1620 && islp
->n_type
!= T_NULL
1621 && islp
->n_sclass
!= C_EOS
)
1623 union internal_auxent eleaux
;
1626 bfd_coff_swap_aux_in (input_bfd
, (esl
+ isymesz
),
1627 islp
->n_type
, islp
->n_sclass
, 0,
1628 islp
->n_numaux
, &eleaux
);
1629 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1631 /* FIXME: If this tagndx entry refers to a symbol
1632 defined later in this file, we just ignore it.
1633 Handling this correctly would be tedious, and may
1638 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1641 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1642 if ((*epp
)->tagndx
< 0)
1646 epp
= &(*epp
)->next
;
1649 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1650 islp
+= islp
->n_numaux
+ 1;
1653 /* See if we already have a definition which matches this
1654 type. We always output the type if it has no elements,
1656 if (mt
->elements
== NULL
)
1657 bfd_release (input_bfd
, mt
);
1660 struct coff_debug_merge_type
*mtl
;
1662 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1664 struct coff_debug_merge_element
*me
, *mel
;
1666 if (mtl
->class != mt
->class)
1669 for (me
= mt
->elements
, mel
= mtl
->elements
;
1670 me
!= NULL
&& mel
!= NULL
;
1671 me
= me
->next
, mel
= mel
->next
)
1673 if (strcmp (me
->name
, mel
->name
) != 0
1674 || me
->type
!= mel
->type
1675 || me
->tagndx
!= mel
->tagndx
)
1679 if (me
== NULL
&& mel
== NULL
)
1683 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1685 /* This is the first definition of this type. */
1686 mt
->indx
= output_index
;
1687 mt
->next
= mh
->types
;
1692 /* This is a redefinition which can be merged. */
1693 bfd_release (input_bfd
, mt
);
1694 *indexp
= mtl
->indx
;
1695 add
= (eslend
- esym
) / isymesz
;
1701 /* We now know whether we are to skip this symbol or not. */
1704 /* Adjust the symbol in order to output it. */
1706 if (isym
._n
._n_n
._n_zeroes
== 0
1707 && isym
._n
._n_n
._n_offset
!= 0)
1712 /* This symbol has a long name. Enter it in the string
1713 table we are building. Note that we do not check
1714 bfd_coff_symname_in_debug. That is only true for
1715 XCOFF, and XCOFF requires different linking code
1717 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, NULL
);
1720 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1721 if (indx
== (bfd_size_type
) -1)
1723 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1726 switch (isym
.n_sclass
)
1742 /* The symbol value should not be modified. */
1746 if (obj_pe (input_bfd
)
1747 && strcmp (isym
.n_name
, ".bf") != 0
1748 && isym
.n_scnum
> 0)
1750 /* For PE, .lf and .ef get their value left alone,
1751 while .bf gets relocated. However, they all have
1752 "real" section numbers, and need to be moved into
1754 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1759 case C_LABEL
: /* Not completely sure about these 2 */
1768 /* Compute new symbol location. */
1769 if (isym
.n_scnum
> 0)
1771 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1772 isym
.n_value
+= (*secpp
)->output_offset
;
1773 if (! obj_pe (input_bfd
))
1774 isym
.n_value
-= (*secpp
)->vma
;
1775 if (! obj_pe (finfo
->output_bfd
))
1776 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1781 /* The value of a C_FILE symbol is the symbol index of
1782 the next C_FILE symbol. The value of the last C_FILE
1783 symbol is the symbol index to the first external
1784 symbol (actually, coff_renumber_symbols does not get
1785 this right--it just sets the value of the last C_FILE
1786 symbol to zero--and nobody has ever complained about
1787 it). We try to get this right, below, just before we
1788 write the symbols out, but in the general case we may
1789 have to write the symbol out twice. */
1790 if (finfo
->last_file_index
!= -1
1791 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1793 /* We must correct the value of the last C_FILE
1795 finfo
->last_file
.n_value
= output_index
;
1796 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1798 /* The last C_FILE symbol is in this input file. */
1799 bfd_coff_swap_sym_out (output_bfd
,
1802 + ((finfo
->last_file_index
1810 /* We have already written out the last C_FILE
1811 symbol. We need to write it out again. We
1812 borrow *outsym temporarily. */
1813 bfd_coff_swap_sym_out (output_bfd
,
1814 &finfo
->last_file
, outsym
);
1815 pos
= obj_sym_filepos (output_bfd
);
1816 pos
+= finfo
->last_file_index
* osymesz
;
1817 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1818 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1823 finfo
->last_file_index
= output_index
;
1824 finfo
->last_file
= isym
;
1828 /* If doing task linking, convert normal global function symbols to
1829 static functions. */
1830 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1831 isym
.n_sclass
= C_STAT
;
1833 /* Output the symbol. */
1834 bfd_coff_swap_sym_out (output_bfd
, &isym
, outsym
);
1836 *indexp
= output_index
;
1841 struct coff_link_hash_entry
*h
;
1843 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1845 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1848 /* This can happen if there were errors earlier in
1850 bfd_set_error (bfd_error_bad_value
);
1853 h
->indx
= output_index
;
1856 output_index
+= add
;
1857 outsym
+= add
* osymesz
;
1860 esym
+= add
* isymesz
;
1864 for (--add
; add
> 0; --add
)
1871 /* Fix up the aux entries. This must be done in a separate pass,
1872 because we don't know the correct symbol indices until we have
1873 already decided which symbols we are going to keep. */
1874 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1875 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1876 isymp
= finfo
->internal_syms
;
1877 indexp
= finfo
->sym_indices
;
1878 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1879 outsym
= finfo
->outsyms
;
1881 while (esym
< esym_end
)
1885 add
= 1 + isymp
->n_numaux
;
1888 || (bfd_size_type
) *indexp
< syment_base
)
1889 && (*sym_hash
== NULL
1890 || (*sym_hash
)->auxbfd
!= input_bfd
))
1891 esym
+= add
* isymesz
;
1894 struct coff_link_hash_entry
*h
;
1902 /* The m68k-motorola-sysv assembler will sometimes
1903 generate two symbols with the same name, but only one
1904 will have aux entries. */
1905 BFD_ASSERT (isymp
->n_numaux
== 0
1906 || h
->numaux
== isymp
->n_numaux
);
1914 /* Handle the aux entries. This handling is based on
1915 coff_pointerize_aux. I don't know if it always correct. */
1916 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1918 union internal_auxent aux
;
1919 union internal_auxent
*auxp
;
1925 bfd_coff_swap_aux_in (input_bfd
, esym
, isymp
->n_type
,
1926 isymp
->n_sclass
, i
, isymp
->n_numaux
, &aux
);
1930 if (isymp
->n_sclass
== C_FILE
)
1932 /* If this is a long filename, we must put it in the
1934 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1935 && auxp
->x_file
.x_n
.x_offset
!= 0)
1937 const char *filename
;
1940 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1941 >= STRING_SIZE_SIZE
);
1942 if (strings
== NULL
)
1944 strings
= _bfd_coff_read_string_table (input_bfd
);
1945 if (strings
== NULL
)
1948 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1949 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1951 if (indx
== (bfd_size_type
) -1)
1953 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1956 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1960 if (ISFCN (isymp
->n_type
)
1961 || ISTAG (isymp
->n_sclass
)
1962 || isymp
->n_sclass
== C_BLOCK
1963 || isymp
->n_sclass
== C_FCN
)
1965 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1967 && indx
< obj_raw_syment_count (input_bfd
))
1969 /* We look forward through the symbol for
1970 the index of the next symbol we are going
1971 to include. I don't know if this is
1973 while ((finfo
->sym_indices
[indx
] < 0
1974 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1976 && indx
< obj_raw_syment_count (input_bfd
))
1978 if (indx
>= obj_raw_syment_count (input_bfd
))
1979 indx
= output_index
;
1981 indx
= finfo
->sym_indices
[indx
];
1982 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1986 indx
= auxp
->x_sym
.x_tagndx
.l
;
1987 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1991 symindx
= finfo
->sym_indices
[indx
];
1993 auxp
->x_sym
.x_tagndx
.l
= 0;
1995 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1998 /* The .bf symbols are supposed to be linked through
1999 the endndx field. We need to carry this list
2000 across object files. */
2003 && isymp
->n_sclass
== C_FCN
2004 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2005 || isymp
->_n
._n_n
._n_offset
== 0)
2006 && isymp
->_n
._n_name
[0] == '.'
2007 && isymp
->_n
._n_name
[1] == 'b'
2008 && isymp
->_n
._n_name
[2] == 'f'
2009 && isymp
->_n
._n_name
[3] == '\0')
2011 if (finfo
->last_bf_index
!= -1)
2013 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2016 if ((bfd_size_type
) finfo
->last_bf_index
2021 /* The last .bf symbol is in this input
2022 file. This will only happen if the
2023 assembler did not set up the .bf
2024 endndx symbols correctly. */
2025 auxout
= (finfo
->outsyms
2026 + ((finfo
->last_bf_index
2030 bfd_coff_swap_aux_out (output_bfd
,
2041 /* We have already written out the last
2042 .bf aux entry. We need to write it
2043 out again. We borrow *outsym
2044 temporarily. FIXME: This case should
2046 bfd_coff_swap_aux_out (output_bfd
,
2052 pos
= obj_sym_filepos (output_bfd
);
2053 pos
+= finfo
->last_bf_index
* osymesz
;
2054 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2055 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2061 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2062 finfo
->last_bf_index
= -1;
2065 /* The endndx field of this aux entry must
2066 be updated with the symbol number of the
2068 finfo
->last_bf
= *auxp
;
2069 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2078 bfd_coff_swap_aux_out (output_bfd
, auxp
, isymp
->n_type
,
2079 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2093 /* Relocate the line numbers, unless we are stripping them. */
2094 if (finfo
->info
->strip
== strip_none
2095 || finfo
->info
->strip
== strip_some
)
2097 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2103 bfd_boolean skipping
;
2107 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2108 build_link_order in ldwrite.c will not have created a
2109 link order, which means that we will not have seen this
2110 input section in _bfd_coff_final_link, which means that
2111 we will not have allocated space for the line numbers of
2112 this section. I don't think line numbers can be
2113 meaningful for a section which does not have
2114 SEC_HAS_CONTENTS set, but, if they do, this must be
2116 if (o
->lineno_count
== 0
2117 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2120 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2121 || bfd_bread (finfo
->linenos
, linesz
* o
->lineno_count
,
2122 input_bfd
) != linesz
* o
->lineno_count
)
2125 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2126 eline
= finfo
->linenos
;
2127 oeline
= finfo
->linenos
;
2128 elineend
= eline
+ linesz
* o
->lineno_count
;
2130 for (; eline
< elineend
; eline
+= linesz
)
2132 struct internal_lineno iline
;
2134 bfd_coff_swap_lineno_in (input_bfd
, eline
, &iline
);
2136 if (iline
.l_lnno
!= 0)
2137 iline
.l_addr
.l_paddr
+= offset
;
2138 else if (iline
.l_addr
.l_symndx
>= 0
2139 && ((unsigned long) iline
.l_addr
.l_symndx
2140 < obj_raw_syment_count (input_bfd
)))
2144 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2148 /* These line numbers are attached to a symbol
2149 which we are stripping. We must discard the
2150 line numbers because reading them back with
2151 no associated symbol (or associating them all
2152 with symbol #0) will fail. We can't regain
2153 the space in the output file, but at least
2159 struct internal_syment is
;
2160 union internal_auxent ia
;
2162 /* Fix up the lnnoptr field in the aux entry of
2163 the symbol. It turns out that we can't do
2164 this when we modify the symbol aux entries,
2165 because gas sometimes screws up the lnnoptr
2166 field and makes it an offset from the start
2167 of the line numbers rather than an absolute
2169 bfd_coff_swap_sym_in (output_bfd
,
2171 + ((indx
- syment_base
)
2173 if ((ISFCN (is
.n_type
)
2174 || is
.n_sclass
== C_BLOCK
)
2175 && is
.n_numaux
>= 1)
2179 auxptr
= (finfo
->outsyms
2180 + ((indx
- syment_base
+ 1)
2182 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2183 is
.n_type
, is
.n_sclass
,
2184 0, is
.n_numaux
, &ia
);
2185 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2186 (o
->output_section
->line_filepos
2187 + o
->output_section
->lineno_count
* linesz
2188 + eline
- finfo
->linenos
);
2189 bfd_coff_swap_aux_out (output_bfd
, &ia
,
2190 is
.n_type
, is
.n_sclass
, 0,
2191 is
.n_numaux
, auxptr
);
2197 iline
.l_addr
.l_symndx
= indx
;
2202 bfd_coff_swap_lineno_out (output_bfd
, &iline
, oeline
);
2207 pos
= o
->output_section
->line_filepos
;
2208 pos
+= o
->output_section
->lineno_count
* linesz
;
2209 amt
= oeline
- finfo
->linenos
;
2210 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2211 || bfd_bwrite (finfo
->linenos
, amt
, output_bfd
) != amt
)
2214 o
->output_section
->lineno_count
+= amt
/ linesz
;
2218 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2219 symbol will be the first symbol in the next input file. In the
2220 normal case, this will save us from writing out the C_FILE symbol
2222 if (finfo
->last_file_index
!= -1
2223 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2225 finfo
->last_file
.n_value
= output_index
;
2226 bfd_coff_swap_sym_out (output_bfd
, &finfo
->last_file
,
2228 + ((finfo
->last_file_index
- syment_base
)
2232 /* Write the modified symbols to the output file. */
2233 if (outsym
> finfo
->outsyms
)
2238 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2239 amt
= outsym
- finfo
->outsyms
;
2240 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2241 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
2244 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2245 + (outsym
- finfo
->outsyms
) / osymesz
)
2248 obj_raw_syment_count (output_bfd
) = output_index
;
2251 /* Relocate the contents of each section. */
2252 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2253 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2256 struct coff_section_tdata
*secdata
;
2258 if (! o
->linker_mark
)
2259 /* This section was omitted from the link. */
2262 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2263 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2265 if ((o
->flags
& SEC_RELOC
) != 0
2266 && o
->reloc_count
!= 0)
2268 ((*_bfd_error_handler
)
2269 (_("%s: relocs in section `%s', but it has no contents"),
2270 bfd_archive_filename (input_bfd
),
2271 bfd_get_section_name (input_bfd
, o
)));
2272 bfd_set_error (bfd_error_no_contents
);
2279 secdata
= coff_section_data (input_bfd
, o
);
2280 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2281 contents
= secdata
->contents
;
2284 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2285 (file_ptr
) 0, o
->_raw_size
))
2287 contents
= finfo
->contents
;
2290 if ((o
->flags
& SEC_RELOC
) != 0)
2293 struct internal_reloc
*internal_relocs
;
2294 struct internal_reloc
*irel
;
2296 /* Read in the relocs. */
2297 target_index
= o
->output_section
->target_index
;
2298 internal_relocs
= (_bfd_coff_read_internal_relocs
2299 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
2300 finfo
->info
->relocatable
,
2301 (finfo
->info
->relocatable
2302 ? (finfo
->section_info
[target_index
].relocs
2303 + o
->output_section
->reloc_count
)
2304 : finfo
->internal_relocs
)));
2305 if (internal_relocs
== NULL
)
2308 /* Call processor specific code to relocate the section
2310 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2314 finfo
->internal_syms
,
2318 if (finfo
->info
->relocatable
)
2321 struct internal_reloc
*irelend
;
2322 struct coff_link_hash_entry
**rel_hash
;
2324 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2325 irel
= internal_relocs
;
2326 irelend
= irel
+ o
->reloc_count
;
2327 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2328 + o
->output_section
->reloc_count
);
2329 for (; irel
< irelend
; irel
++, rel_hash
++)
2331 struct coff_link_hash_entry
*h
;
2332 bfd_boolean adjusted
;
2336 /* Adjust the reloc address and symbol index. */
2337 irel
->r_vaddr
+= offset
;
2339 if (irel
->r_symndx
== -1)
2344 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2352 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2355 /* This is a global symbol. */
2357 irel
->r_symndx
= h
->indx
;
2360 /* This symbol is being written at the end
2361 of the file, and we do not yet know the
2362 symbol index. We save the pointer to the
2363 hash table entry in the rel_hash list.
2364 We set the indx field to -2 to indicate
2365 that this symbol must not be stripped. */
2374 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2376 irel
->r_symndx
= indx
;
2379 struct internal_syment
*is
;
2381 char buf
[SYMNMLEN
+ 1];
2383 /* This reloc is against a symbol we are
2384 stripping. This should have been handled
2385 by the 'dont_skip_symbol' code in the while
2386 loop at the top of this function. */
2387 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2389 name
= (_bfd_coff_internal_syment_name
2390 (input_bfd
, is
, buf
));
2394 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2395 (finfo
->info
, name
, input_bfd
, o
,
2402 o
->output_section
->reloc_count
+= o
->reloc_count
;
2406 /* Write out the modified section contents. */
2407 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2409 file_ptr loc
= o
->output_offset
* bfd_octets_per_byte (output_bfd
);
2410 bfd_size_type amt
= (o
->_cooked_size
!= 0
2411 ? o
->_cooked_size
: o
->_raw_size
);
2412 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2413 contents
, loc
, amt
))
2418 if (! (_bfd_write_section_stabs
2419 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2420 o
, &secdata
->stab_info
, contents
)))
2425 if (! finfo
->info
->keep_memory
2426 && ! _bfd_coff_free_symbols (input_bfd
))
2432 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2435 _bfd_coff_write_global_sym (struct coff_link_hash_entry
*h
, void *data
)
2437 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2439 struct internal_syment isym
;
2440 bfd_size_type symesz
;
2444 output_bfd
= finfo
->output_bfd
;
2446 if (h
->root
.type
== bfd_link_hash_warning
)
2448 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2449 if (h
->root
.type
== bfd_link_hash_new
)
2457 && (finfo
->info
->strip
== strip_all
2458 || (finfo
->info
->strip
== strip_some
2459 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2460 h
->root
.root
.string
, FALSE
, FALSE
)
2464 switch (h
->root
.type
)
2467 case bfd_link_hash_new
:
2468 case bfd_link_hash_warning
:
2472 case bfd_link_hash_undefined
:
2473 case bfd_link_hash_undefweak
:
2474 isym
.n_scnum
= N_UNDEF
;
2478 case bfd_link_hash_defined
:
2479 case bfd_link_hash_defweak
:
2483 sec
= h
->root
.u
.def
.section
->output_section
;
2484 if (bfd_is_abs_section (sec
))
2485 isym
.n_scnum
= N_ABS
;
2487 isym
.n_scnum
= sec
->target_index
;
2488 isym
.n_value
= (h
->root
.u
.def
.value
2489 + h
->root
.u
.def
.section
->output_offset
);
2490 if (! obj_pe (finfo
->output_bfd
))
2491 isym
.n_value
+= sec
->vma
;
2495 case bfd_link_hash_common
:
2496 isym
.n_scnum
= N_UNDEF
;
2497 isym
.n_value
= h
->root
.u
.c
.size
;
2500 case bfd_link_hash_indirect
:
2501 /* Just ignore these. They can't be handled anyhow. */
2505 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2506 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2513 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2515 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2517 if (indx
== (bfd_size_type
) -1)
2519 finfo
->failed
= TRUE
;
2522 isym
._n
._n_n
._n_zeroes
= 0;
2523 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2526 isym
.n_sclass
= h
->class;
2527 isym
.n_type
= h
->type
;
2529 if (isym
.n_sclass
== C_NULL
)
2530 isym
.n_sclass
= C_EXT
;
2532 /* If doing task linking and this is the pass where we convert
2533 defined globals to statics, then do that conversion now. If the
2534 symbol is not being converted, just ignore it and it will be
2535 output during a later pass. */
2536 if (finfo
->global_to_static
)
2538 if (! IS_EXTERNAL (output_bfd
, isym
))
2541 isym
.n_sclass
= C_STAT
;
2544 /* When a weak symbol is not overriden by a strong one,
2545 turn it into an external symbol when not building a
2546 shared or relocatable object. */
2547 if (! finfo
->info
->shared
2548 && ! finfo
->info
->relocatable
2549 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2550 isym
.n_sclass
= C_EXT
;
2552 isym
.n_numaux
= h
->numaux
;
2554 bfd_coff_swap_sym_out (output_bfd
, &isym
, finfo
->outsyms
);
2556 symesz
= bfd_coff_symesz (output_bfd
);
2558 pos
= obj_sym_filepos (output_bfd
);
2559 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2560 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2561 || bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2563 finfo
->failed
= TRUE
;
2567 h
->indx
= obj_raw_syment_count (output_bfd
);
2569 ++obj_raw_syment_count (output_bfd
);
2571 /* Write out any associated aux entries. Most of the aux entries
2572 will have been modified in _bfd_coff_link_input_bfd. We have to
2573 handle section aux entries here, now that we have the final
2574 relocation and line number counts. */
2575 for (i
= 0; i
< isym
.n_numaux
; i
++)
2577 union internal_auxent
*auxp
;
2581 /* Look for a section aux entry here using the same tests that
2582 coff_swap_aux_out uses. */
2584 && (isym
.n_sclass
== C_STAT
2585 || isym
.n_sclass
== C_HIDDEN
)
2586 && isym
.n_type
== T_NULL
2587 && (h
->root
.type
== bfd_link_hash_defined
2588 || h
->root
.type
== bfd_link_hash_defweak
))
2592 sec
= h
->root
.u
.def
.section
->output_section
;
2595 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2599 /* For PE, an overflow on the final link reportedly does
2600 not matter. FIXME: Why not? */
2601 if (sec
->reloc_count
> 0xffff
2602 && (! obj_pe (output_bfd
)
2603 || finfo
->info
->relocatable
))
2604 (*_bfd_error_handler
)
2605 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2606 bfd_get_filename (output_bfd
),
2607 bfd_get_section_name (output_bfd
, sec
),
2610 if (sec
->lineno_count
> 0xffff
2611 && (! obj_pe (output_bfd
)
2612 || finfo
->info
->relocatable
))
2613 (*_bfd_error_handler
)
2614 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2615 bfd_get_filename (output_bfd
),
2616 bfd_get_section_name (output_bfd
, sec
),
2619 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2620 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2621 auxp
->x_scn
.x_checksum
= 0;
2622 auxp
->x_scn
.x_associated
= 0;
2623 auxp
->x_scn
.x_comdat
= 0;
2627 bfd_coff_swap_aux_out (output_bfd
, auxp
, isym
.n_type
,
2628 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2630 if (bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2632 finfo
->failed
= TRUE
;
2635 ++obj_raw_syment_count (output_bfd
);
2641 /* Write out task global symbols, converting them to statics. Called
2642 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2643 the dirty work, if the symbol we are processing needs conversion. */
2646 _bfd_coff_write_task_globals (struct coff_link_hash_entry
*h
, void *data
)
2648 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2649 bfd_boolean rtnval
= TRUE
;
2650 bfd_boolean save_global_to_static
;
2652 if (h
->root
.type
== bfd_link_hash_warning
)
2653 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2657 switch (h
->root
.type
)
2659 case bfd_link_hash_defined
:
2660 case bfd_link_hash_defweak
:
2661 save_global_to_static
= finfo
->global_to_static
;
2662 finfo
->global_to_static
= TRUE
;
2663 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2664 finfo
->global_to_static
= save_global_to_static
;
2673 /* Handle a link order which is supposed to generate a reloc. */
2676 _bfd_coff_reloc_link_order (bfd
*output_bfd
,
2677 struct coff_final_link_info
*finfo
,
2678 asection
*output_section
,
2679 struct bfd_link_order
*link_order
)
2681 reloc_howto_type
*howto
;
2682 struct internal_reloc
*irel
;
2683 struct coff_link_hash_entry
**rel_hash_ptr
;
2685 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2688 bfd_set_error (bfd_error_bad_value
);
2692 if (link_order
->u
.reloc
.p
->addend
!= 0)
2696 bfd_reloc_status_type rstat
;
2700 size
= bfd_get_reloc_size (howto
);
2701 buf
= bfd_zmalloc (size
);
2705 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2706 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,\
2713 case bfd_reloc_outofrange
:
2715 case bfd_reloc_overflow
:
2716 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2718 (link_order
->type
== bfd_section_reloc_link_order
2719 ? bfd_section_name (output_bfd
,
2720 link_order
->u
.reloc
.p
->u
.section
)
2721 : link_order
->u
.reloc
.p
->u
.name
),
2722 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2723 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2730 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2731 ok
= bfd_set_section_contents (output_bfd
, output_section
, buf
,
2738 /* Store the reloc information in the right place. It will get
2739 swapped and written out at the end of the final_link routine. */
2740 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2741 + output_section
->reloc_count
);
2742 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2743 + output_section
->reloc_count
);
2745 memset (irel
, 0, sizeof (struct internal_reloc
));
2746 *rel_hash_ptr
= NULL
;
2748 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2750 if (link_order
->type
== bfd_section_reloc_link_order
)
2752 /* We need to somehow locate a symbol in the right section. The
2753 symbol must either have a value of zero, or we must adjust
2754 the addend by the value of the symbol. FIXME: Write this
2755 when we need it. The old linker couldn't handle this anyhow. */
2757 *rel_hash_ptr
= NULL
;
2762 struct coff_link_hash_entry
*h
;
2764 h
= ((struct coff_link_hash_entry
*)
2765 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2766 link_order
->u
.reloc
.p
->u
.name
,
2767 FALSE
, FALSE
, TRUE
));
2771 irel
->r_symndx
= h
->indx
;
2774 /* Set the index to -2 to force this symbol to get
2783 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2784 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2785 (asection
*) NULL
, (bfd_vma
) 0)))
2791 /* FIXME: Is this always right? */
2792 irel
->r_type
= howto
->type
;
2794 /* r_size is only used on the RS/6000, which needs its own linker
2795 routines anyhow. r_extern is only used for ECOFF. */
2797 /* FIXME: What is the right value for r_offset? Is zero OK? */
2798 ++output_section
->reloc_count
;
2803 /* A basic reloc handling routine which may be used by processors with
2807 _bfd_coff_generic_relocate_section (bfd
*output_bfd
,
2808 struct bfd_link_info
*info
,
2810 asection
*input_section
,
2812 struct internal_reloc
*relocs
,
2813 struct internal_syment
*syms
,
2814 asection
**sections
)
2816 struct internal_reloc
*rel
;
2817 struct internal_reloc
*relend
;
2820 relend
= rel
+ input_section
->reloc_count
;
2821 for (; rel
< relend
; rel
++)
2824 struct coff_link_hash_entry
*h
;
2825 struct internal_syment
*sym
;
2828 reloc_howto_type
*howto
;
2829 bfd_reloc_status_type rstat
;
2831 symndx
= rel
->r_symndx
;
2839 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2841 (*_bfd_error_handler
)
2842 ("%s: illegal symbol index %ld in relocs",
2843 bfd_archive_filename (input_bfd
), symndx
);
2848 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2849 sym
= syms
+ symndx
;
2852 /* COFF treats common symbols in one of two ways. Either the
2853 size of the symbol is included in the section contents, or it
2854 is not. We assume that the size is not included, and force
2855 the rtype_to_howto function to adjust the addend as needed. */
2856 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2857 addend
= - sym
->n_value
;
2861 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2866 /* If we are doing a relocatable link, then we can just ignore
2867 a PC relative reloc that is pcrel_offset. It will already
2868 have the correct value. If this is not a relocatable link,
2869 then we should ignore the symbol value. */
2870 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2872 if (info
->relocatable
)
2874 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2875 addend
+= sym
->n_value
;
2886 sec
= bfd_abs_section_ptr
;
2891 sec
= sections
[symndx
];
2892 val
= (sec
->output_section
->vma
2893 + sec
->output_offset
2895 if (! obj_pe (input_bfd
))
2901 if (h
->root
.type
== bfd_link_hash_defined
2902 || h
->root
.type
== bfd_link_hash_defweak
)
2906 sec
= h
->root
.u
.def
.section
;
2907 val
= (h
->root
.u
.def
.value
2908 + sec
->output_section
->vma
2909 + sec
->output_offset
);
2912 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2915 else if (! info
->relocatable
)
2917 if (! ((*info
->callbacks
->undefined_symbol
)
2918 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2919 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
2924 if (info
->base_file
)
2926 /* Emit a reloc if the backend thinks it needs it. */
2927 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2929 /* Relocation to a symbol in a section which isn't
2930 absolute. We output the address here to a file.
2931 This file is then read by dlltool when generating the
2932 reloc section. Note that the base file is not
2933 portable between systems. We write out a long here,
2934 and dlltool reads in a long. */
2935 long addr
= (rel
->r_vaddr
2936 - input_section
->vma
2937 + input_section
->output_offset
2938 + input_section
->output_section
->vma
);
2939 if (coff_data (output_bfd
)->pe
)
2940 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2941 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2944 bfd_set_error (bfd_error_system_call
);
2950 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2952 rel
->r_vaddr
- input_section
->vma
,
2961 case bfd_reloc_outofrange
:
2962 (*_bfd_error_handler
)
2963 (_("%s: bad reloc address 0x%lx in section `%s'"),
2964 bfd_archive_filename (input_bfd
),
2965 (unsigned long) rel
->r_vaddr
,
2966 bfd_get_section_name (input_bfd
, input_section
));
2968 case bfd_reloc_overflow
:
2971 char buf
[SYMNMLEN
+ 1];
2976 name
= h
->root
.root
.string
;
2979 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2984 if (! ((*info
->callbacks
->reloc_overflow
)
2985 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2986 input_section
, rel
->r_vaddr
- input_section
->vma
)))