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
32 PARAMS ((bfd
*, struct bfd_link_info
*));
33 static bfd_boolean coff_link_check_archive_element
34 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean
*));
35 static bfd_boolean coff_link_check_ar_symbols
36 PARAMS ((bfd
*, struct bfd_link_info
*, bfd_boolean
*));
37 static bfd_boolean coff_link_add_symbols
38 PARAMS ((bfd
*, struct bfd_link_info
*));
39 static char *dores_com
40 PARAMS ((char *, bfd
*, int));
42 PARAMS ((char *, char **));
43 static int process_embedded_commands
44 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*));
45 static void mark_relocs
46 PARAMS ((struct coff_final_link_info
*, bfd
*));
48 /* Return TRUE if SYM is a weak, external symbol. */
49 #define IS_WEAK_EXTERNAL(abfd, sym) \
50 ((sym).n_sclass == C_WEAKEXT \
51 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
53 /* Return TRUE if SYM is an external symbol. */
54 #define IS_EXTERNAL(abfd, sym) \
55 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
57 /* Define macros so that the ISFCN, et. al., macros work correctly.
58 These macros are defined in include/coff/internal.h in terms of
59 N_TMASK, etc. These definitions require a user to define local
60 variables with the appropriate names, and with values from the
61 coff_data (abfd) structure. */
63 #define N_TMASK n_tmask
64 #define N_BTSHFT n_btshft
65 #define N_BTMASK n_btmask
67 /* Create an entry in a COFF linker hash table. */
69 struct bfd_hash_entry
*
70 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
71 struct bfd_hash_entry
*entry
;
72 struct bfd_hash_table
*table
;
75 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
77 /* Allocate the structure if it has not already been allocated by a
79 if (ret
== (struct coff_link_hash_entry
*) NULL
)
80 ret
= ((struct coff_link_hash_entry
*)
81 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
82 if (ret
== (struct coff_link_hash_entry
*) NULL
)
83 return (struct bfd_hash_entry
*) ret
;
85 /* Call the allocation method of the superclass. */
86 ret
= ((struct coff_link_hash_entry
*)
87 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
89 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
91 /* Set local fields. */
100 return (struct bfd_hash_entry
*) ret
;
103 /* Initialize a COFF linker hash table. */
106 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
107 struct coff_link_hash_table
*table
;
109 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
110 struct bfd_hash_table
*,
113 table
->stab_info
= NULL
;
114 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
117 /* Create a COFF linker hash table. */
119 struct bfd_link_hash_table
*
120 _bfd_coff_link_hash_table_create (abfd
)
123 struct coff_link_hash_table
*ret
;
124 bfd_size_type amt
= sizeof (struct coff_link_hash_table
);
126 ret
= (struct coff_link_hash_table
*) bfd_malloc (amt
);
129 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
130 _bfd_coff_link_hash_newfunc
))
133 return (struct bfd_link_hash_table
*) NULL
;
138 /* Create an entry in a COFF debug merge hash table. */
140 struct bfd_hash_entry
*
141 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
142 struct bfd_hash_entry
*entry
;
143 struct bfd_hash_table
*table
;
146 struct coff_debug_merge_hash_entry
*ret
=
147 (struct coff_debug_merge_hash_entry
*) entry
;
149 /* Allocate the structure if it has not already been allocated by a
151 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
152 ret
= ((struct coff_debug_merge_hash_entry
*)
153 bfd_hash_allocate (table
,
154 sizeof (struct coff_debug_merge_hash_entry
)));
155 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
156 return (struct bfd_hash_entry
*) ret
;
158 /* Call the allocation method of the superclass. */
159 ret
= ((struct coff_debug_merge_hash_entry
*)
160 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
161 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
163 /* Set local fields. */
167 return (struct bfd_hash_entry
*) ret
;
170 /* Given a COFF BFD, add symbols to the global hash table as
174 _bfd_coff_link_add_symbols (abfd
, info
)
176 struct bfd_link_info
*info
;
178 switch (bfd_get_format (abfd
))
181 return coff_link_add_object_symbols (abfd
, info
);
183 return (_bfd_generic_link_add_archive_symbols
184 (abfd
, info
, coff_link_check_archive_element
));
186 bfd_set_error (bfd_error_wrong_format
);
191 /* Add symbols from a COFF object file. */
194 coff_link_add_object_symbols (abfd
, info
)
196 struct bfd_link_info
*info
;
198 if (! _bfd_coff_get_external_symbols (abfd
))
200 if (! coff_link_add_symbols (abfd
, info
))
203 if (! info
->keep_memory
)
205 if (! _bfd_coff_free_symbols (abfd
))
211 /* Check a single archive element to see if we need to include it in
212 the link. *PNEEDED is set according to whether this element is
213 needed in the link or not. This is called via
214 _bfd_generic_link_add_archive_symbols. */
217 coff_link_check_archive_element (abfd
, info
, pneeded
)
219 struct bfd_link_info
*info
;
220 bfd_boolean
*pneeded
;
222 if (! _bfd_coff_get_external_symbols (abfd
))
225 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
230 if (! coff_link_add_symbols (abfd
, info
))
234 if (! info
->keep_memory
|| ! *pneeded
)
236 if (! _bfd_coff_free_symbols (abfd
))
243 /* Look through the symbols to see if this object file should be
244 included in the link. */
247 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
249 struct bfd_link_info
*info
;
250 bfd_boolean
*pneeded
;
252 bfd_size_type symesz
;
258 symesz
= bfd_coff_symesz (abfd
);
259 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
260 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
261 while (esym
< esym_end
)
263 struct internal_syment sym
;
264 enum coff_symbol_classification classification
;
266 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
268 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
269 if (classification
== COFF_SYMBOL_GLOBAL
270 || classification
== COFF_SYMBOL_COMMON
)
273 char buf
[SYMNMLEN
+ 1];
274 struct bfd_link_hash_entry
*h
;
276 /* This symbol is externally visible, and is defined by this
279 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
282 h
= bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, TRUE
);
285 if (!h
&& info
->pei386_auto_import
)
287 if (!strncmp (name
,"__imp_", 6))
290 bfd_link_hash_lookup (info
->hash
, name
+ 6, FALSE
, FALSE
,
294 /* We are only interested in symbols that are currently
295 undefined. If a symbol is currently known to be common,
296 COFF linkers do not bring in an object file which defines
298 if (h
!= (struct bfd_link_hash_entry
*) NULL
299 && h
->type
== bfd_link_hash_undefined
)
301 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
308 esym
+= (sym
.n_numaux
+ 1) * symesz
;
311 /* We do not need this object file. */
315 /* Add all the symbols from an object file to the hash table. */
318 coff_link_add_symbols (abfd
, info
)
320 struct bfd_link_info
*info
;
322 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
323 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
324 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
325 bfd_boolean keep_syms
;
326 bfd_boolean default_copy
;
327 bfd_size_type symcount
;
328 struct coff_link_hash_entry
**sym_hash
;
329 bfd_size_type symesz
;
334 /* Keep the symbols during this function, in case the linker needs
335 to read the generic symbols in order to report an error message. */
336 keep_syms
= obj_coff_keep_syms (abfd
);
337 obj_coff_keep_syms (abfd
) = TRUE
;
339 if (info
->keep_memory
)
340 default_copy
= FALSE
;
344 symcount
= obj_raw_syment_count (abfd
);
346 /* We keep a list of the linker hash table entries that correspond
347 to particular symbols. */
348 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
349 sym_hash
= (struct coff_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
350 if (sym_hash
== NULL
&& symcount
!= 0)
352 obj_coff_sym_hashes (abfd
) = sym_hash
;
354 symesz
= bfd_coff_symesz (abfd
);
355 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
356 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
357 esym_end
= esym
+ symcount
* symesz
;
358 while (esym
< esym_end
)
360 struct internal_syment sym
;
361 enum coff_symbol_classification classification
;
364 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
366 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
367 if (classification
!= COFF_SYMBOL_LOCAL
)
370 char buf
[SYMNMLEN
+ 1];
376 /* This symbol is externally visible. */
378 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
382 /* We must copy the name into memory if we got it from the
383 syment itself, rather than the string table. */
385 if (sym
._n
._n_n
._n_zeroes
!= 0
386 || sym
._n
._n_n
._n_offset
== 0)
391 switch (classification
)
396 case COFF_SYMBOL_GLOBAL
:
397 flags
= BSF_EXPORT
| BSF_GLOBAL
;
398 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
400 value
-= section
->vma
;
403 case COFF_SYMBOL_UNDEFINED
:
405 section
= bfd_und_section_ptr
;
408 case COFF_SYMBOL_COMMON
:
410 section
= bfd_com_section_ptr
;
413 case COFF_SYMBOL_PE_SECTION
:
414 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
415 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
419 if (IS_WEAK_EXTERNAL (abfd
, sym
))
424 /* In the PE format, section symbols actually refer to the
425 start of the output section. We handle them specially
427 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
429 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
430 name
, FALSE
, copy
, FALSE
);
431 if (*sym_hash
!= NULL
)
433 if (((*sym_hash
)->coff_link_hash_flags
434 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
435 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
436 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
437 (*_bfd_error_handler
)
438 ("Warning: symbol `%s' is both section and non-section",
445 /* The Microsoft Visual C compiler does string pooling by
446 hashing the constants to an internal symbol name, and
447 relying on the linker comdat support to discard
448 duplicate names. However, if one string is a literal and
449 one is a data initializer, one will end up in the .data
450 section and one will end up in the .rdata section. The
451 Microsoft linker will combine them into the .data
452 section, which seems to be wrong since it might cause the
455 As long as there are no external references to the
456 symbols, which there shouldn't be, we can treat the .data
457 and .rdata instances as separate symbols. The comdat
458 code in the linker will do the appropriate merging. Here
459 we avoid getting a multiple definition error for one of
460 these special symbols.
462 FIXME: I don't think this will work in the case where
463 there are two object files which use the constants as a
464 literal and two object files which use it as a data
465 initializer. One or the other of the second object files
466 is going to wind up with an inappropriate reference. */
468 && (classification
== COFF_SYMBOL_GLOBAL
469 || classification
== COFF_SYMBOL_PE_SECTION
)
470 && section
->comdat
!= NULL
471 && strncmp (name
, "??_", 3) == 0
472 && strcmp (name
, section
->comdat
->name
) == 0)
474 if (*sym_hash
== NULL
)
475 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
476 name
, FALSE
, copy
, FALSE
);
477 if (*sym_hash
!= NULL
478 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
479 && (*sym_hash
)->root
.u
.def
.section
->comdat
!= NULL
480 && strcmp ((*sym_hash
)->root
.u
.def
.section
->comdat
->name
,
481 section
->comdat
->name
) == 0)
487 if (! (bfd_coff_link_add_one_symbol
488 (info
, abfd
, name
, flags
, section
, value
,
489 (const char *) NULL
, copy
, FALSE
,
490 (struct bfd_link_hash_entry
**) sym_hash
)))
494 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
495 (*sym_hash
)->coff_link_hash_flags
|=
496 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
498 /* Limit the alignment of a common symbol to the possible
499 alignment of a section. There is no point to permitting
500 a higher alignment for a common symbol: we can not
501 guarantee it, and it may cause us to allocate extra space
502 in the common section. */
503 if (section
== bfd_com_section_ptr
504 && (*sym_hash
)->root
.type
== bfd_link_hash_common
505 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
506 > bfd_coff_default_section_alignment_power (abfd
)))
507 (*sym_hash
)->root
.u
.c
.p
->alignment_power
508 = bfd_coff_default_section_alignment_power (abfd
);
510 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
512 /* If we don't have any symbol information currently in
513 the hash table, or if we are looking at a symbol
514 definition, then update the symbol class and type in
516 if (((*sym_hash
)->class == C_NULL
517 && (*sym_hash
)->type
== T_NULL
)
520 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
521 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
523 (*sym_hash
)->class = sym
.n_sclass
;
524 if (sym
.n_type
!= T_NULL
)
526 /* We want to warn if the type changed, but not
527 if it changed from an unspecified type.
528 Testing the whole type byte may work, but the
529 change from (e.g.) a function of unspecified
530 type to function of known type also wants to
532 if ((*sym_hash
)->type
!= T_NULL
533 && (*sym_hash
)->type
!= sym
.n_type
534 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
535 && (BTYPE ((*sym_hash
)->type
) == T_NULL
536 || BTYPE (sym
.n_type
) == T_NULL
)))
537 (*_bfd_error_handler
)
538 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
539 name
, (*sym_hash
)->type
, sym
.n_type
,
540 bfd_archive_filename (abfd
));
542 /* We don't want to change from a meaningful
543 base type to a null one, but if we know
544 nothing, take what little we might now know. */
545 if (BTYPE (sym
.n_type
) != T_NULL
546 || (*sym_hash
)->type
== T_NULL
)
547 (*sym_hash
)->type
= sym
.n_type
;
549 (*sym_hash
)->auxbfd
= abfd
;
550 if (sym
.n_numaux
!= 0)
552 union internal_auxent
*alloc
;
555 union internal_auxent
*iaux
;
557 (*sym_hash
)->numaux
= sym
.n_numaux
;
558 alloc
= ((union internal_auxent
*)
559 bfd_hash_allocate (&info
->hash
->table
,
561 * sizeof (*alloc
))));
564 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
566 i
++, eaux
+= symesz
, iaux
++)
567 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
568 sym
.n_sclass
, (int) i
,
569 sym
.n_numaux
, (PTR
) iaux
);
570 (*sym_hash
)->aux
= alloc
;
575 if (classification
== COFF_SYMBOL_PE_SECTION
576 && (*sym_hash
)->numaux
!= 0)
578 /* Some PE sections (such as .bss) have a zero size in
579 the section header, but a non-zero size in the AUX
580 record. Correct that here.
582 FIXME: This is not at all the right place to do this.
583 For example, it won't help objdump. This needs to be
584 done when we swap in the section header. */
586 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
587 if (section
->_raw_size
== 0)
588 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
590 /* FIXME: We could test whether the section sizes
591 matches the size in the aux entry, but apparently
592 that sometimes fails unexpectedly. */
596 esym
+= (sym
.n_numaux
+ 1) * symesz
;
597 sym_hash
+= sym
.n_numaux
+ 1;
600 /* If this is a non-traditional, non-relocatable link, try to
601 optimize the handling of any .stab/.stabstr sections. */
602 if (! info
->relocatable
603 && ! info
->traditional_format
604 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
605 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
607 asection
*stab
, *stabstr
;
609 stab
= bfd_get_section_by_name (abfd
, ".stab");
612 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
616 struct coff_link_hash_table
*table
;
617 struct coff_section_tdata
*secdata
;
619 secdata
= coff_section_data (abfd
, stab
);
622 amt
= sizeof (struct coff_section_tdata
);
623 stab
->used_by_bfd
= (PTR
) bfd_zalloc (abfd
, amt
);
624 if (stab
->used_by_bfd
== NULL
)
626 secdata
= coff_section_data (abfd
, stab
);
629 table
= coff_hash_table (info
);
631 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
633 &secdata
->stab_info
))
639 obj_coff_keep_syms (abfd
) = keep_syms
;
644 obj_coff_keep_syms (abfd
) = keep_syms
;
648 /* Do the final link step. */
651 _bfd_coff_final_link (abfd
, info
)
653 struct bfd_link_info
*info
;
655 bfd_size_type symesz
;
656 struct coff_final_link_info finfo
;
657 bfd_boolean debug_merge_allocated
;
658 bfd_boolean long_section_names
;
660 struct bfd_link_order
*p
;
661 bfd_size_type max_sym_count
;
662 bfd_size_type max_lineno_count
;
663 bfd_size_type max_reloc_count
;
664 bfd_size_type max_output_reloc_count
;
665 bfd_size_type max_contents_size
;
666 file_ptr rel_filepos
;
668 file_ptr line_filepos
;
671 bfd_byte
*external_relocs
= NULL
;
672 char strbuf
[STRING_SIZE_SIZE
];
675 symesz
= bfd_coff_symesz (abfd
);
678 finfo
.output_bfd
= abfd
;
680 finfo
.section_info
= NULL
;
681 finfo
.last_file_index
= -1;
682 finfo
.last_bf_index
= -1;
683 finfo
.internal_syms
= NULL
;
684 finfo
.sec_ptrs
= NULL
;
685 finfo
.sym_indices
= NULL
;
686 finfo
.outsyms
= NULL
;
687 finfo
.linenos
= NULL
;
688 finfo
.contents
= NULL
;
689 finfo
.external_relocs
= NULL
;
690 finfo
.internal_relocs
= NULL
;
691 finfo
.global_to_static
= FALSE
;
692 debug_merge_allocated
= FALSE
;
694 coff_data (abfd
)->link_info
= info
;
696 finfo
.strtab
= _bfd_stringtab_init ();
697 if (finfo
.strtab
== NULL
)
700 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
702 debug_merge_allocated
= TRUE
;
704 /* Compute the file positions for all the sections. */
705 if (! abfd
->output_has_begun
)
707 if (! bfd_coff_compute_section_file_positions (abfd
))
711 /* Count the line numbers and relocation entries required for the
712 output file. Set the file positions for the relocs. */
713 rel_filepos
= obj_relocbase (abfd
);
714 relsz
= bfd_coff_relsz (abfd
);
715 max_contents_size
= 0;
716 max_lineno_count
= 0;
719 long_section_names
= FALSE
;
720 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
724 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
726 if (p
->type
== bfd_indirect_link_order
)
730 sec
= p
->u
.indirect
.section
;
732 /* Mark all sections which are to be included in the
733 link. This will normally be every section. We need
734 to do this so that we can identify any sections which
735 the linker has decided to not include. */
736 sec
->linker_mark
= TRUE
;
738 if (info
->strip
== strip_none
739 || info
->strip
== strip_some
)
740 o
->lineno_count
+= sec
->lineno_count
;
742 if (info
->relocatable
)
743 o
->reloc_count
+= sec
->reloc_count
;
745 if (sec
->_raw_size
> max_contents_size
)
746 max_contents_size
= sec
->_raw_size
;
747 if (sec
->lineno_count
> max_lineno_count
)
748 max_lineno_count
= sec
->lineno_count
;
749 if (sec
->reloc_count
> max_reloc_count
)
750 max_reloc_count
= sec
->reloc_count
;
752 else if (info
->relocatable
753 && (p
->type
== bfd_section_reloc_link_order
754 || p
->type
== bfd_symbol_reloc_link_order
))
757 if (o
->reloc_count
== 0)
761 o
->flags
|= SEC_RELOC
;
762 o
->rel_filepos
= rel_filepos
;
763 rel_filepos
+= o
->reloc_count
* relsz
;
764 /* In PE COFF, if there are at least 0xffff relocations an
765 extra relocation will be written out to encode the count. */
766 if (obj_pe (abfd
) && o
->reloc_count
>= 0xffff)
767 rel_filepos
+= relsz
;
770 if (bfd_coff_long_section_names (abfd
)
771 && strlen (o
->name
) > SCNNMLEN
)
773 /* This section has a long name which must go in the string
774 table. This must correspond to the code in
775 coff_write_object_contents which puts the string index
776 into the s_name field of the section header. That is why
777 we pass hash as FALSE. */
778 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, FALSE
, FALSE
)
779 == (bfd_size_type
) -1)
781 long_section_names
= TRUE
;
785 /* If doing a relocatable link, allocate space for the pointers we
787 if (info
->relocatable
)
791 /* We use section_count + 1, rather than section_count, because
792 the target_index fields are 1 based. */
793 amt
= abfd
->section_count
+ 1;
794 amt
*= sizeof (struct coff_link_section_info
);
795 finfo
.section_info
= (struct coff_link_section_info
*) bfd_malloc (amt
);
796 if (finfo
.section_info
== NULL
)
798 for (i
= 0; i
<= abfd
->section_count
; i
++)
800 finfo
.section_info
[i
].relocs
= NULL
;
801 finfo
.section_info
[i
].rel_hashes
= NULL
;
805 /* We now know the size of the relocs, so we can determine the file
806 positions of the line numbers. */
807 line_filepos
= rel_filepos
;
808 linesz
= bfd_coff_linesz (abfd
);
809 max_output_reloc_count
= 0;
810 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
812 if (o
->lineno_count
== 0)
816 o
->line_filepos
= line_filepos
;
817 line_filepos
+= o
->lineno_count
* linesz
;
820 if (o
->reloc_count
!= 0)
822 /* We don't know the indices of global symbols until we have
823 written out all the local symbols. For each section in
824 the output file, we keep an array of pointers to hash
825 table entries. Each entry in the array corresponds to a
826 reloc. When we find a reloc against a global symbol, we
827 set the corresponding entry in this array so that we can
828 fix up the symbol index after we have written out all the
831 Because of this problem, we also keep the relocs in
832 memory until the end of the link. This wastes memory,
833 but only when doing a relocatable link, which is not the
835 BFD_ASSERT (info
->relocatable
);
836 amt
= o
->reloc_count
;
837 amt
*= sizeof (struct internal_reloc
);
838 finfo
.section_info
[o
->target_index
].relocs
=
839 (struct internal_reloc
*) bfd_malloc (amt
);
840 amt
= o
->reloc_count
;
841 amt
*= sizeof (struct coff_link_hash_entry
*);
842 finfo
.section_info
[o
->target_index
].rel_hashes
=
843 (struct coff_link_hash_entry
**) bfd_malloc (amt
);
844 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
845 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
848 if (o
->reloc_count
> max_output_reloc_count
)
849 max_output_reloc_count
= o
->reloc_count
;
852 /* Reset the reloc and lineno counts, so that we can use them to
853 count the number of entries we have output so far. */
858 obj_sym_filepos (abfd
) = line_filepos
;
860 /* Figure out the largest number of symbols in an input BFD. Take
861 the opportunity to clear the output_has_begun fields of all the
864 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
868 sub
->output_has_begun
= FALSE
;
869 sz
= obj_raw_syment_count (sub
);
870 if (sz
> max_sym_count
)
874 /* Allocate some buffers used while linking. */
875 amt
= max_sym_count
* sizeof (struct internal_syment
);
876 finfo
.internal_syms
= (struct internal_syment
*) bfd_malloc (amt
);
877 amt
= max_sym_count
* sizeof (asection
*);
878 finfo
.sec_ptrs
= (asection
**) bfd_malloc (amt
);
879 amt
= max_sym_count
* sizeof (long);
880 finfo
.sym_indices
= (long *) bfd_malloc (amt
);
881 finfo
.outsyms
= (bfd_byte
*) bfd_malloc ((max_sym_count
+ 1) * symesz
);
882 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
883 finfo
.linenos
= (bfd_byte
*) bfd_malloc (amt
);
884 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
885 amt
= max_reloc_count
* relsz
;
886 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
887 if (! info
->relocatable
)
889 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
890 finfo
.internal_relocs
= (struct internal_reloc
*) bfd_malloc (amt
);
892 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
893 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
894 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
895 || finfo
.outsyms
== NULL
896 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
897 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
898 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
899 || (! info
->relocatable
900 && finfo
.internal_relocs
== NULL
901 && max_reloc_count
> 0))
904 /* We now know the position of everything in the file, except that
905 we don't know the size of the symbol table and therefore we don't
906 know where the string table starts. We just build the string
907 table in memory as we go along. We process all the relocations
908 for a single input file at once. */
909 obj_raw_syment_count (abfd
) = 0;
911 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
913 if (! bfd_coff_start_final_link (abfd
, info
))
917 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
919 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
921 if (p
->type
== bfd_indirect_link_order
922 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
924 sub
= p
->u
.indirect
.section
->owner
;
925 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
927 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
929 sub
->output_has_begun
= TRUE
;
932 else if (p
->type
== bfd_section_reloc_link_order
933 || p
->type
== bfd_symbol_reloc_link_order
)
935 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
940 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
946 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
949 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
951 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
952 debug_merge_allocated
= FALSE
;
954 if (finfo
.internal_syms
!= NULL
)
956 free (finfo
.internal_syms
);
957 finfo
.internal_syms
= NULL
;
959 if (finfo
.sec_ptrs
!= NULL
)
961 free (finfo
.sec_ptrs
);
962 finfo
.sec_ptrs
= NULL
;
964 if (finfo
.sym_indices
!= NULL
)
966 free (finfo
.sym_indices
);
967 finfo
.sym_indices
= NULL
;
969 if (finfo
.linenos
!= NULL
)
971 free (finfo
.linenos
);
972 finfo
.linenos
= NULL
;
974 if (finfo
.contents
!= NULL
)
976 free (finfo
.contents
);
977 finfo
.contents
= NULL
;
979 if (finfo
.external_relocs
!= NULL
)
981 free (finfo
.external_relocs
);
982 finfo
.external_relocs
= NULL
;
984 if (finfo
.internal_relocs
!= NULL
)
986 free (finfo
.internal_relocs
);
987 finfo
.internal_relocs
= NULL
;
990 /* The value of the last C_FILE symbol is supposed to be the symbol
991 index of the first external symbol. Write it out again if
993 if (finfo
.last_file_index
!= -1
994 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
998 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
999 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
1000 (PTR
) finfo
.outsyms
);
1002 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
1003 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
1004 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
1008 /* If doing task linking (ld --task-link) then make a pass through the
1009 global symbols, writing out any that are defined, and making them
1011 if (info
->task_link
)
1013 finfo
.failed
= FALSE
;
1014 coff_link_hash_traverse (coff_hash_table (info
),
1015 _bfd_coff_write_task_globals
,
1021 /* Write out the global symbols. */
1022 finfo
.failed
= FALSE
;
1023 coff_link_hash_traverse (coff_hash_table (info
),
1024 _bfd_coff_write_global_sym
,
1029 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1030 if (finfo
.outsyms
!= NULL
)
1032 free (finfo
.outsyms
);
1033 finfo
.outsyms
= NULL
;
1036 if (info
->relocatable
&& max_output_reloc_count
> 0)
1038 /* Now that we have written out all the global symbols, we know
1039 the symbol indices to use for relocs against them, and we can
1040 finally write out the relocs. */
1041 amt
= max_output_reloc_count
* relsz
;
1042 external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
1043 if (external_relocs
== NULL
)
1046 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1048 struct internal_reloc
*irel
;
1049 struct internal_reloc
*irelend
;
1050 struct coff_link_hash_entry
**rel_hash
;
1053 if (o
->reloc_count
== 0)
1056 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1057 irelend
= irel
+ o
->reloc_count
;
1058 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1059 erel
= external_relocs
;
1060 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1062 if (*rel_hash
!= NULL
)
1064 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1065 irel
->r_symndx
= (*rel_hash
)->indx
;
1067 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1070 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1071 || (bfd_bwrite ((PTR
) external_relocs
,
1072 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1073 != (bfd_size_type
) relsz
* o
->reloc_count
))
1077 free (external_relocs
);
1078 external_relocs
= NULL
;
1081 /* Free up the section information. */
1082 if (finfo
.section_info
!= NULL
)
1086 for (i
= 0; i
< abfd
->section_count
; i
++)
1088 if (finfo
.section_info
[i
].relocs
!= NULL
)
1089 free (finfo
.section_info
[i
].relocs
);
1090 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1091 free (finfo
.section_info
[i
].rel_hashes
);
1093 free (finfo
.section_info
);
1094 finfo
.section_info
= NULL
;
1097 /* If we have optimized stabs strings, output them. */
1098 if (coff_hash_table (info
)->stab_info
!= NULL
)
1100 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1104 /* Write out the string table. */
1105 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1109 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1110 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1113 #if STRING_SIZE_SIZE == 4
1115 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1118 #error Change H_PUT_32 above
1121 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1122 != STRING_SIZE_SIZE
)
1125 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1128 obj_coff_strings_written (abfd
) = TRUE
;
1131 _bfd_stringtab_free (finfo
.strtab
);
1133 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1134 not try to write out the symbols. */
1135 bfd_get_symcount (abfd
) = 0;
1140 if (debug_merge_allocated
)
1141 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1142 if (finfo
.strtab
!= NULL
)
1143 _bfd_stringtab_free (finfo
.strtab
);
1144 if (finfo
.section_info
!= NULL
)
1148 for (i
= 0; i
< abfd
->section_count
; i
++)
1150 if (finfo
.section_info
[i
].relocs
!= NULL
)
1151 free (finfo
.section_info
[i
].relocs
);
1152 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1153 free (finfo
.section_info
[i
].rel_hashes
);
1155 free (finfo
.section_info
);
1157 if (finfo
.internal_syms
!= NULL
)
1158 free (finfo
.internal_syms
);
1159 if (finfo
.sec_ptrs
!= NULL
)
1160 free (finfo
.sec_ptrs
);
1161 if (finfo
.sym_indices
!= NULL
)
1162 free (finfo
.sym_indices
);
1163 if (finfo
.outsyms
!= NULL
)
1164 free (finfo
.outsyms
);
1165 if (finfo
.linenos
!= NULL
)
1166 free (finfo
.linenos
);
1167 if (finfo
.contents
!= NULL
)
1168 free (finfo
.contents
);
1169 if (finfo
.external_relocs
!= NULL
)
1170 free (finfo
.external_relocs
);
1171 if (finfo
.internal_relocs
!= NULL
)
1172 free (finfo
.internal_relocs
);
1173 if (external_relocs
!= NULL
)
1174 free (external_relocs
);
1178 /* parse out a -heap <reserved>,<commit> line */
1181 dores_com (ptr
, output_bfd
, heap
)
1186 if (coff_data(output_bfd
)->pe
)
1188 int val
= strtoul (ptr
, &ptr
, 0);
1190 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1192 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1196 val
= strtoul (ptr
+1, &ptr
, 0);
1198 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1200 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1206 static char *get_name(ptr
, dst
)
1213 while (*ptr
&& *ptr
!= ' ')
1219 /* Process any magic embedded commands in a section called .drectve */
1222 process_embedded_commands (output_bfd
, info
, abfd
)
1224 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1227 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1234 copy
= bfd_malloc (sec
->_raw_size
);
1237 if (! bfd_get_section_contents(abfd
, sec
, copy
, (bfd_vma
) 0, sec
->_raw_size
))
1242 e
= copy
+ sec
->_raw_size
;
1243 for (s
= copy
; s
< e
; )
1249 if (strncmp (s
,"-attr", 5) == 0)
1261 s
= get_name(s
, &name
);
1262 s
= get_name(s
, &attribs
);
1282 asec
= bfd_get_section_by_name (abfd
, name
);
1285 asec
->flags
|= SEC_CODE
;
1287 asec
->flags
|= SEC_READONLY
;
1290 else if (strncmp (s
,"-heap", 5) == 0)
1292 s
= dores_com (s
+5, output_bfd
, 1);
1294 else if (strncmp (s
,"-stack", 6) == 0)
1296 s
= dores_com (s
+6, output_bfd
, 0);
1305 /* Place a marker against all symbols which are used by relocations.
1306 This marker can be picked up by the 'do we skip this symbol ?'
1307 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1312 mark_relocs (finfo
, input_bfd
)
1313 struct coff_final_link_info
* finfo
;
1318 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1321 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1323 struct internal_reloc
* internal_relocs
;
1324 struct internal_reloc
* irel
;
1325 struct internal_reloc
* irelend
;
1327 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1329 /* Don't mark relocs in excluded sections. */
1330 if (a
->output_section
== bfd_abs_section_ptr
)
1333 /* Read in the relocs. */
1334 internal_relocs
= _bfd_coff_read_internal_relocs
1335 (input_bfd
, a
, FALSE
,
1336 finfo
->external_relocs
,
1337 finfo
->info
->relocatable
,
1338 (finfo
->info
->relocatable
1339 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1340 : finfo
->internal_relocs
)
1343 if (internal_relocs
== NULL
)
1346 irel
= internal_relocs
;
1347 irelend
= irel
+ a
->reloc_count
;
1349 /* Place a mark in the sym_indices array (whose entries have
1350 been initialised to 0) for all of the symbols that are used
1351 in the relocation table. This will then be picked up in the
1354 for (; irel
< irelend
; irel
++)
1356 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1361 /* Link an input file into the linker output file. This function
1362 handles all the sections and relocations of the input file at once. */
1365 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1366 struct coff_final_link_info
*finfo
;
1369 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1370 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1372 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1374 bfd_boolean (*adjust_symndx
)
1375 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
1376 struct internal_reloc
*, bfd_boolean
*));
1378 const char *strings
;
1379 bfd_size_type syment_base
;
1380 bfd_boolean copy
, hash
;
1381 bfd_size_type isymesz
;
1382 bfd_size_type osymesz
;
1383 bfd_size_type linesz
;
1386 struct internal_syment
*isymp
;
1389 unsigned long output_index
;
1391 struct coff_link_hash_entry
**sym_hash
;
1394 /* Move all the symbols to the output file. */
1396 output_bfd
= finfo
->output_bfd
;
1398 syment_base
= obj_raw_syment_count (output_bfd
);
1399 isymesz
= bfd_coff_symesz (input_bfd
);
1400 osymesz
= bfd_coff_symesz (output_bfd
);
1401 linesz
= bfd_coff_linesz (input_bfd
);
1402 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1405 if (! finfo
->info
->keep_memory
)
1408 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1411 if (! _bfd_coff_get_external_symbols (input_bfd
))
1414 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1415 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1416 isymp
= finfo
->internal_syms
;
1417 secpp
= finfo
->sec_ptrs
;
1418 indexp
= finfo
->sym_indices
;
1419 output_index
= syment_base
;
1420 outsym
= finfo
->outsyms
;
1422 if (coff_data (output_bfd
)->pe
)
1424 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1428 /* If we are going to perform relocations and also strip/discard some symbols
1429 then we must make sure that we do not strip/discard those symbols that are
1430 going to be involved in the relocations */
1431 if (( finfo
->info
->strip
!= strip_none
1432 || finfo
->info
->discard
!= discard_none
)
1433 && finfo
->info
->relocatable
)
1435 /* mark the symbol array as 'not-used' */
1436 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1438 mark_relocs (finfo
, input_bfd
);
1441 while (esym
< esym_end
)
1443 struct internal_syment isym
;
1444 enum coff_symbol_classification classification
;
1447 bfd_boolean dont_skip_symbol
;
1450 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1452 /* Make a copy of *isymp so that the relocate_section function
1453 always sees the original values. This is more reliable than
1454 always recomputing the symbol value even if we are stripping
1458 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1459 switch (classification
)
1463 case COFF_SYMBOL_GLOBAL
:
1464 case COFF_SYMBOL_PE_SECTION
:
1465 case COFF_SYMBOL_LOCAL
:
1466 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1468 case COFF_SYMBOL_COMMON
:
1469 *secpp
= bfd_com_section_ptr
;
1471 case COFF_SYMBOL_UNDEFINED
:
1472 *secpp
= bfd_und_section_ptr
;
1476 /* Extract the flag indicating if this symbol is used by a
1478 if ((finfo
->info
->strip
!= strip_none
1479 || finfo
->info
->discard
!= discard_none
)
1480 && finfo
->info
->relocatable
)
1481 dont_skip_symbol
= *indexp
;
1483 dont_skip_symbol
= FALSE
;
1489 add
= 1 + isym
.n_numaux
;
1491 /* If we are stripping all symbols, we want to skip this one. */
1492 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1497 switch (classification
)
1501 case COFF_SYMBOL_GLOBAL
:
1502 case COFF_SYMBOL_COMMON
:
1503 case COFF_SYMBOL_PE_SECTION
:
1504 /* This is a global symbol. Global symbols come at the
1505 end of the symbol table, so skip them for now.
1506 Locally defined function symbols, however, are an
1507 exception, and are not moved to the end. */
1509 if (! ISFCN (isym
.n_type
))
1513 case COFF_SYMBOL_UNDEFINED
:
1514 /* Undefined symbols are left for the end. */
1519 case COFF_SYMBOL_LOCAL
:
1520 /* This is a local symbol. Skip it if we are discarding
1522 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1528 #ifndef COFF_WITH_PE
1529 /* Skip section symbols for sections which are not going to be
1532 && isym
.n_sclass
== C_STAT
1533 && isym
.n_type
== T_NULL
1534 && isym
.n_numaux
> 0)
1536 if ((*secpp
)->output_section
== bfd_abs_section_ptr
)
1541 /* If we stripping debugging symbols, and this is a debugging
1542 symbol, then skip it. FIXME: gas sets the section to N_ABS
1543 for some types of debugging symbols; I don't know if this is
1544 a bug or not. In any case, we handle it here. */
1546 && finfo
->info
->strip
== strip_debugger
1547 && ! dont_skip_symbol
1548 && (isym
.n_scnum
== N_DEBUG
1549 || (isym
.n_scnum
== N_ABS
1550 && (isym
.n_sclass
== C_AUTO
1551 || isym
.n_sclass
== C_REG
1552 || isym
.n_sclass
== C_MOS
1553 || isym
.n_sclass
== C_MOE
1554 || isym
.n_sclass
== C_MOU
1555 || isym
.n_sclass
== C_ARG
1556 || isym
.n_sclass
== C_REGPARM
1557 || isym
.n_sclass
== C_FIELD
1558 || isym
.n_sclass
== C_EOS
))))
1561 /* If some symbols are stripped based on the name, work out the
1562 name and decide whether to skip this symbol. */
1564 && (finfo
->info
->strip
== strip_some
1565 || finfo
->info
->discard
== discard_l
))
1568 char buf
[SYMNMLEN
+ 1];
1570 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1574 if (! dont_skip_symbol
1575 && ((finfo
->info
->strip
== strip_some
1576 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, FALSE
,
1579 && finfo
->info
->discard
== discard_l
1580 && bfd_is_local_label_name (input_bfd
, name
))))
1584 /* If this is an enum, struct, or union tag, see if we have
1585 already output an identical type. */
1587 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1588 && (isym
.n_sclass
== C_ENTAG
1589 || isym
.n_sclass
== C_STRTAG
1590 || isym
.n_sclass
== C_UNTAG
)
1591 && isym
.n_numaux
== 1)
1594 char buf
[SYMNMLEN
+ 1];
1595 struct coff_debug_merge_hash_entry
*mh
;
1596 struct coff_debug_merge_type
*mt
;
1597 union internal_auxent aux
;
1598 struct coff_debug_merge_element
**epp
;
1599 bfd_byte
*esl
, *eslend
;
1600 struct internal_syment
*islp
;
1603 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1607 /* Ignore fake names invented by compiler; treat them all as
1609 if (*name
== '~' || *name
== '.' || *name
== '$'
1610 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1611 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1614 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1619 /* Allocate memory to hold type information. If this turns
1620 out to be a duplicate, we pass this address to
1622 amt
= sizeof (struct coff_debug_merge_type
);
1623 mt
= (struct coff_debug_merge_type
*) bfd_alloc (input_bfd
, amt
);
1626 mt
->class = isym
.n_sclass
;
1628 /* Pick up the aux entry, which points to the end of the tag
1630 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1631 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1634 /* Gather the elements. */
1635 epp
= &mt
->elements
;
1636 mt
->elements
= NULL
;
1638 esl
= esym
+ 2 * isymesz
;
1639 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1640 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1641 while (esl
< eslend
)
1643 const char *elename
;
1644 char elebuf
[SYMNMLEN
+ 1];
1647 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1649 amt
= sizeof (struct coff_debug_merge_element
);
1650 *epp
= ((struct coff_debug_merge_element
*)
1651 bfd_alloc (input_bfd
, amt
));
1655 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1657 if (elename
== NULL
)
1660 amt
= strlen (elename
) + 1;
1661 name_copy
= (char *) bfd_alloc (input_bfd
, amt
);
1662 if (name_copy
== NULL
)
1664 strcpy (name_copy
, elename
);
1666 (*epp
)->name
= name_copy
;
1667 (*epp
)->type
= islp
->n_type
;
1669 if (islp
->n_numaux
>= 1
1670 && islp
->n_type
!= T_NULL
1671 && islp
->n_sclass
!= C_EOS
)
1673 union internal_auxent eleaux
;
1676 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1677 islp
->n_type
, islp
->n_sclass
, 0,
1678 islp
->n_numaux
, (PTR
) &eleaux
);
1679 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1681 /* FIXME: If this tagndx entry refers to a symbol
1682 defined later in this file, we just ignore it.
1683 Handling this correctly would be tedious, and may
1689 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1692 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1693 if ((*epp
)->tagndx
< 0)
1697 epp
= &(*epp
)->next
;
1700 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1701 islp
+= islp
->n_numaux
+ 1;
1704 /* See if we already have a definition which matches this
1705 type. We always output the type if it has no elements,
1707 if (mt
->elements
== NULL
)
1708 bfd_release (input_bfd
, (PTR
) mt
);
1711 struct coff_debug_merge_type
*mtl
;
1713 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1715 struct coff_debug_merge_element
*me
, *mel
;
1717 if (mtl
->class != mt
->class)
1720 for (me
= mt
->elements
, mel
= mtl
->elements
;
1721 me
!= NULL
&& mel
!= NULL
;
1722 me
= me
->next
, mel
= mel
->next
)
1724 if (strcmp (me
->name
, mel
->name
) != 0
1725 || me
->type
!= mel
->type
1726 || me
->tagndx
!= mel
->tagndx
)
1730 if (me
== NULL
&& mel
== NULL
)
1734 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1736 /* This is the first definition of this type. */
1737 mt
->indx
= output_index
;
1738 mt
->next
= mh
->types
;
1743 /* This is a redefinition which can be merged. */
1744 bfd_release (input_bfd
, (PTR
) mt
);
1745 *indexp
= mtl
->indx
;
1746 add
= (eslend
- esym
) / isymesz
;
1752 /* We now know whether we are to skip this symbol or not. */
1755 /* Adjust the symbol in order to output it. */
1757 if (isym
._n
._n_n
._n_zeroes
== 0
1758 && isym
._n
._n_n
._n_offset
!= 0)
1763 /* This symbol has a long name. Enter it in the string
1764 table we are building. Note that we do not check
1765 bfd_coff_symname_in_debug. That is only true for
1766 XCOFF, and XCOFF requires different linking code
1768 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1772 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1773 if (indx
== (bfd_size_type
) -1)
1775 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1778 switch (isym
.n_sclass
)
1794 /* The symbol value should not be modified. */
1798 if (obj_pe (input_bfd
)
1799 && strcmp (isym
.n_name
, ".bf") != 0
1800 && isym
.n_scnum
> 0)
1802 /* For PE, .lf and .ef get their value left alone,
1803 while .bf gets relocated. However, they all have
1804 "real" section numbers, and need to be moved into
1806 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1811 case C_LABEL
: /* Not completely sure about these 2 */
1820 /* Compute new symbol location. */
1821 if (isym
.n_scnum
> 0)
1823 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1824 isym
.n_value
+= (*secpp
)->output_offset
;
1825 if (! obj_pe (input_bfd
))
1826 isym
.n_value
-= (*secpp
)->vma
;
1827 if (! obj_pe (finfo
->output_bfd
))
1828 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1833 /* The value of a C_FILE symbol is the symbol index of
1834 the next C_FILE symbol. The value of the last C_FILE
1835 symbol is the symbol index to the first external
1836 symbol (actually, coff_renumber_symbols does not get
1837 this right--it just sets the value of the last C_FILE
1838 symbol to zero--and nobody has ever complained about
1839 it). We try to get this right, below, just before we
1840 write the symbols out, but in the general case we may
1841 have to write the symbol out twice. */
1843 if (finfo
->last_file_index
!= -1
1844 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1846 /* We must correct the value of the last C_FILE
1848 finfo
->last_file
.n_value
= output_index
;
1849 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1851 /* The last C_FILE symbol is in this input file. */
1852 bfd_coff_swap_sym_out (output_bfd
,
1853 (PTR
) &finfo
->last_file
,
1854 (PTR
) (finfo
->outsyms
1855 + ((finfo
->last_file_index
1863 /* We have already written out the last C_FILE
1864 symbol. We need to write it out again. We
1865 borrow *outsym temporarily. */
1866 bfd_coff_swap_sym_out (output_bfd
,
1867 (PTR
) &finfo
->last_file
,
1869 pos
= obj_sym_filepos (output_bfd
);
1870 pos
+= finfo
->last_file_index
* osymesz
;
1871 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1872 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1877 finfo
->last_file_index
= output_index
;
1878 finfo
->last_file
= isym
;
1882 /* If doing task linking, convert normal global function symbols to
1883 static functions. */
1884 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1885 isym
.n_sclass
= C_STAT
;
1887 /* Output the symbol. */
1889 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1891 *indexp
= output_index
;
1896 struct coff_link_hash_entry
*h
;
1898 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1900 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1903 /* This can happen if there were errors earlier in
1905 bfd_set_error (bfd_error_bad_value
);
1908 h
->indx
= output_index
;
1911 output_index
+= add
;
1912 outsym
+= add
* osymesz
;
1915 esym
+= add
* isymesz
;
1919 for (--add
; add
> 0; --add
)
1926 /* Fix up the aux entries. This must be done in a separate pass,
1927 because we don't know the correct symbol indices until we have
1928 already decided which symbols we are going to keep. */
1930 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1931 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1932 isymp
= finfo
->internal_syms
;
1933 indexp
= finfo
->sym_indices
;
1934 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1935 outsym
= finfo
->outsyms
;
1936 while (esym
< esym_end
)
1940 add
= 1 + isymp
->n_numaux
;
1943 || (bfd_size_type
) *indexp
< syment_base
)
1944 && (*sym_hash
== NULL
1945 || (*sym_hash
)->auxbfd
!= input_bfd
))
1946 esym
+= add
* isymesz
;
1949 struct coff_link_hash_entry
*h
;
1957 /* The m68k-motorola-sysv assembler will sometimes
1958 generate two symbols with the same name, but only one
1959 will have aux entries. */
1960 BFD_ASSERT (isymp
->n_numaux
== 0
1961 || h
->numaux
== isymp
->n_numaux
);
1969 /* Handle the aux entries. This handling is based on
1970 coff_pointerize_aux. I don't know if it always correct. */
1971 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1973 union internal_auxent aux
;
1974 union internal_auxent
*auxp
;
1980 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1981 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1986 if (isymp
->n_sclass
== C_FILE
)
1988 /* If this is a long filename, we must put it in the
1990 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1991 && auxp
->x_file
.x_n
.x_offset
!= 0)
1993 const char *filename
;
1996 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1997 >= STRING_SIZE_SIZE
);
1998 if (strings
== NULL
)
2000 strings
= _bfd_coff_read_string_table (input_bfd
);
2001 if (strings
== NULL
)
2004 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
2005 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
2007 if (indx
== (bfd_size_type
) -1)
2009 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
2012 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
2016 if (ISFCN (isymp
->n_type
)
2017 || ISTAG (isymp
->n_sclass
)
2018 || isymp
->n_sclass
== C_BLOCK
2019 || isymp
->n_sclass
== C_FCN
)
2021 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2023 && indx
< obj_raw_syment_count (input_bfd
))
2025 /* We look forward through the symbol for
2026 the index of the next symbol we are going
2027 to include. I don't know if this is
2029 while ((finfo
->sym_indices
[indx
] < 0
2030 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
2032 && indx
< obj_raw_syment_count (input_bfd
))
2034 if (indx
>= obj_raw_syment_count (input_bfd
))
2035 indx
= output_index
;
2037 indx
= finfo
->sym_indices
[indx
];
2038 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2042 indx
= auxp
->x_sym
.x_tagndx
.l
;
2043 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2047 symindx
= finfo
->sym_indices
[indx
];
2049 auxp
->x_sym
.x_tagndx
.l
= 0;
2051 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2054 /* The .bf symbols are supposed to be linked through
2055 the endndx field. We need to carry this list
2056 across object files. */
2059 && isymp
->n_sclass
== C_FCN
2060 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2061 || isymp
->_n
._n_n
._n_offset
== 0)
2062 && isymp
->_n
._n_name
[0] == '.'
2063 && isymp
->_n
._n_name
[1] == 'b'
2064 && isymp
->_n
._n_name
[2] == 'f'
2065 && isymp
->_n
._n_name
[3] == '\0')
2067 if (finfo
->last_bf_index
!= -1)
2069 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2072 if ((bfd_size_type
) finfo
->last_bf_index
2077 /* The last .bf symbol is in this input
2078 file. This will only happen if the
2079 assembler did not set up the .bf
2080 endndx symbols correctly. */
2081 auxout
= (PTR
) (finfo
->outsyms
2082 + ((finfo
->last_bf_index
2085 bfd_coff_swap_aux_out (output_bfd
,
2086 (PTR
) &finfo
->last_bf
,
2096 /* We have already written out the last
2097 .bf aux entry. We need to write it
2098 out again. We borrow *outsym
2099 temporarily. FIXME: This case should
2101 bfd_coff_swap_aux_out (output_bfd
,
2102 (PTR
) &finfo
->last_bf
,
2107 pos
= obj_sym_filepos (output_bfd
);
2108 pos
+= finfo
->last_bf_index
* osymesz
;
2109 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2110 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2116 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2117 finfo
->last_bf_index
= -1;
2120 /* The endndx field of this aux entry must
2121 be updated with the symbol number of the
2123 finfo
->last_bf
= *auxp
;
2124 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2133 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
2134 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2148 /* Relocate the line numbers, unless we are stripping them. */
2149 if (finfo
->info
->strip
== strip_none
2150 || finfo
->info
->strip
== strip_some
)
2152 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2158 bfd_boolean skipping
;
2162 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2163 build_link_order in ldwrite.c will not have created a
2164 link order, which means that we will not have seen this
2165 input section in _bfd_coff_final_link, which means that
2166 we will not have allocated space for the line numbers of
2167 this section. I don't think line numbers can be
2168 meaningful for a section which does not have
2169 SEC_HAS_CONTENTS set, but, if they do, this must be
2171 if (o
->lineno_count
== 0
2172 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2175 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2176 || bfd_bread (finfo
->linenos
, linesz
* o
->lineno_count
,
2177 input_bfd
) != linesz
* o
->lineno_count
)
2180 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2181 eline
= finfo
->linenos
;
2182 oeline
= finfo
->linenos
;
2183 elineend
= eline
+ linesz
* o
->lineno_count
;
2185 for (; eline
< elineend
; eline
+= linesz
)
2187 struct internal_lineno iline
;
2189 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2191 if (iline
.l_lnno
!= 0)
2192 iline
.l_addr
.l_paddr
+= offset
;
2193 else if (iline
.l_addr
.l_symndx
>= 0
2194 && ((unsigned long) iline
.l_addr
.l_symndx
2195 < obj_raw_syment_count (input_bfd
)))
2199 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2203 /* These line numbers are attached to a symbol
2204 which we are stripping. We must discard the
2205 line numbers because reading them back with
2206 no associated symbol (or associating them all
2207 with symbol #0) will fail. We can't regain
2208 the space in the output file, but at least
2214 struct internal_syment is
;
2215 union internal_auxent ia
;
2217 /* Fix up the lnnoptr field in the aux entry of
2218 the symbol. It turns out that we can't do
2219 this when we modify the symbol aux entries,
2220 because gas sometimes screws up the lnnoptr
2221 field and makes it an offset from the start
2222 of the line numbers rather than an absolute
2224 bfd_coff_swap_sym_in (output_bfd
,
2225 (PTR
) (finfo
->outsyms
2226 + ((indx
- syment_base
)
2229 if ((ISFCN (is
.n_type
)
2230 || is
.n_sclass
== C_BLOCK
)
2231 && is
.n_numaux
>= 1)
2235 auxptr
= (PTR
) (finfo
->outsyms
2236 + ((indx
- syment_base
+ 1)
2238 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2239 is
.n_type
, is
.n_sclass
,
2240 0, is
.n_numaux
, (PTR
) &ia
);
2241 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2242 (o
->output_section
->line_filepos
2243 + o
->output_section
->lineno_count
* linesz
2244 + eline
- finfo
->linenos
);
2245 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2246 is
.n_type
, is
.n_sclass
, 0,
2247 is
.n_numaux
, auxptr
);
2253 iline
.l_addr
.l_symndx
= indx
;
2258 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
,
2264 pos
= o
->output_section
->line_filepos
;
2265 pos
+= o
->output_section
->lineno_count
* linesz
;
2266 amt
= oeline
- finfo
->linenos
;
2267 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2268 || bfd_bwrite (finfo
->linenos
, amt
, output_bfd
) != amt
)
2271 o
->output_section
->lineno_count
+= amt
/ linesz
;
2275 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2276 symbol will be the first symbol in the next input file. In the
2277 normal case, this will save us from writing out the C_FILE symbol
2279 if (finfo
->last_file_index
!= -1
2280 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2282 finfo
->last_file
.n_value
= output_index
;
2283 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2284 (PTR
) (finfo
->outsyms
2285 + ((finfo
->last_file_index
- syment_base
)
2289 /* Write the modified symbols to the output file. */
2290 if (outsym
> finfo
->outsyms
)
2295 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2296 amt
= outsym
- finfo
->outsyms
;
2297 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2298 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
2301 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2302 + (outsym
- finfo
->outsyms
) / osymesz
)
2305 obj_raw_syment_count (output_bfd
) = output_index
;
2308 /* Relocate the contents of each section. */
2309 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2310 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2313 struct coff_section_tdata
*secdata
;
2315 if (! o
->linker_mark
)
2317 /* This section was omitted from the link. */
2321 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2322 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2324 if ((o
->flags
& SEC_RELOC
) != 0
2325 && o
->reloc_count
!= 0)
2327 ((*_bfd_error_handler
)
2328 (_("%s: relocs in section `%s', but it has no contents"),
2329 bfd_archive_filename (input_bfd
),
2330 bfd_get_section_name (input_bfd
, o
)));
2331 bfd_set_error (bfd_error_no_contents
);
2338 secdata
= coff_section_data (input_bfd
, o
);
2339 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2340 contents
= secdata
->contents
;
2343 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2344 (file_ptr
) 0, o
->_raw_size
))
2346 contents
= finfo
->contents
;
2349 if ((o
->flags
& SEC_RELOC
) != 0)
2352 struct internal_reloc
*internal_relocs
;
2353 struct internal_reloc
*irel
;
2355 /* Read in the relocs. */
2356 target_index
= o
->output_section
->target_index
;
2357 internal_relocs
= (_bfd_coff_read_internal_relocs
2358 (input_bfd
, o
, FALSE
, finfo
->external_relocs
,
2359 finfo
->info
->relocatable
,
2360 (finfo
->info
->relocatable
2361 ? (finfo
->section_info
[target_index
].relocs
2362 + o
->output_section
->reloc_count
)
2363 : finfo
->internal_relocs
)));
2364 if (internal_relocs
== NULL
)
2367 /* Call processor specific code to relocate the section
2369 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2373 finfo
->internal_syms
,
2377 if (finfo
->info
->relocatable
)
2380 struct internal_reloc
*irelend
;
2381 struct coff_link_hash_entry
**rel_hash
;
2383 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2384 irel
= internal_relocs
;
2385 irelend
= irel
+ o
->reloc_count
;
2386 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2387 + o
->output_section
->reloc_count
);
2388 for (; irel
< irelend
; irel
++, rel_hash
++)
2390 struct coff_link_hash_entry
*h
;
2391 bfd_boolean adjusted
;
2395 /* Adjust the reloc address and symbol index. */
2397 irel
->r_vaddr
+= offset
;
2399 if (irel
->r_symndx
== -1)
2404 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2412 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2415 /* This is a global symbol. */
2417 irel
->r_symndx
= h
->indx
;
2420 /* This symbol is being written at the end
2421 of the file, and we do not yet know the
2422 symbol index. We save the pointer to the
2423 hash table entry in the rel_hash list.
2424 We set the indx field to -2 to indicate
2425 that this symbol must not be stripped. */
2434 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2436 irel
->r_symndx
= indx
;
2439 struct internal_syment
*is
;
2441 char buf
[SYMNMLEN
+ 1];
2443 /* This reloc is against a symbol we are
2444 stripping. This should have been handled
2445 by the 'dont_skip_symbol' code in the while
2446 loop at the top of this function. */
2448 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2450 name
= (_bfd_coff_internal_syment_name
2451 (input_bfd
, is
, buf
));
2455 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2456 (finfo
->info
, name
, input_bfd
, o
,
2463 o
->output_section
->reloc_count
+= o
->reloc_count
;
2467 /* Write out the modified section contents. */
2468 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2470 file_ptr loc
= o
->output_offset
* bfd_octets_per_byte (output_bfd
);
2471 bfd_size_type amt
= (o
->_cooked_size
!= 0
2472 ? o
->_cooked_size
: o
->_raw_size
);
2473 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2474 contents
, loc
, amt
))
2479 if (! (_bfd_write_section_stabs
2480 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2481 o
, &secdata
->stab_info
, contents
)))
2486 if (! finfo
->info
->keep_memory
)
2488 if (! _bfd_coff_free_symbols (input_bfd
))
2495 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2498 _bfd_coff_write_global_sym (h
, data
)
2499 struct coff_link_hash_entry
*h
;
2502 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2504 struct internal_syment isym
;
2505 bfd_size_type symesz
;
2509 output_bfd
= finfo
->output_bfd
;
2511 if (h
->root
.type
== bfd_link_hash_warning
)
2513 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2514 if (h
->root
.type
== bfd_link_hash_new
)
2522 && (finfo
->info
->strip
== strip_all
2523 || (finfo
->info
->strip
== strip_some
2524 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2525 h
->root
.root
.string
, FALSE
, FALSE
)
2529 switch (h
->root
.type
)
2532 case bfd_link_hash_new
:
2533 case bfd_link_hash_warning
:
2537 case bfd_link_hash_undefined
:
2538 case bfd_link_hash_undefweak
:
2539 isym
.n_scnum
= N_UNDEF
;
2543 case bfd_link_hash_defined
:
2544 case bfd_link_hash_defweak
:
2548 sec
= h
->root
.u
.def
.section
->output_section
;
2549 if (bfd_is_abs_section (sec
))
2550 isym
.n_scnum
= N_ABS
;
2552 isym
.n_scnum
= sec
->target_index
;
2553 isym
.n_value
= (h
->root
.u
.def
.value
2554 + h
->root
.u
.def
.section
->output_offset
);
2555 if (! obj_pe (finfo
->output_bfd
))
2556 isym
.n_value
+= sec
->vma
;
2560 case bfd_link_hash_common
:
2561 isym
.n_scnum
= N_UNDEF
;
2562 isym
.n_value
= h
->root
.u
.c
.size
;
2565 case bfd_link_hash_indirect
:
2566 /* Just ignore these. They can't be handled anyhow. */
2570 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2571 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2578 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2580 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2582 if (indx
== (bfd_size_type
) -1)
2584 finfo
->failed
= TRUE
;
2587 isym
._n
._n_n
._n_zeroes
= 0;
2588 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2591 isym
.n_sclass
= h
->class;
2592 isym
.n_type
= h
->type
;
2594 if (isym
.n_sclass
== C_NULL
)
2595 isym
.n_sclass
= C_EXT
;
2597 /* If doing task linking and this is the pass where we convert
2598 defined globals to statics, then do that conversion now. If the
2599 symbol is not being converted, just ignore it and it will be
2600 output during a later pass. */
2601 if (finfo
->global_to_static
)
2603 if (! IS_EXTERNAL (output_bfd
, isym
))
2606 isym
.n_sclass
= C_STAT
;
2609 /* When a weak symbol is not overriden by a strong one,
2610 turn it into an external symbol when not building a
2611 shared or relocatable object. */
2612 if (! finfo
->info
->shared
2613 && ! finfo
->info
->relocatable
2614 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2615 isym
.n_sclass
= C_EXT
;
2617 isym
.n_numaux
= h
->numaux
;
2619 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2621 symesz
= bfd_coff_symesz (output_bfd
);
2623 pos
= obj_sym_filepos (output_bfd
);
2624 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2625 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2626 || bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2628 finfo
->failed
= TRUE
;
2632 h
->indx
= obj_raw_syment_count (output_bfd
);
2634 ++obj_raw_syment_count (output_bfd
);
2636 /* Write out any associated aux entries. Most of the aux entries
2637 will have been modified in _bfd_coff_link_input_bfd. We have to
2638 handle section aux entries here, now that we have the final
2639 relocation and line number counts. */
2640 for (i
= 0; i
< isym
.n_numaux
; i
++)
2642 union internal_auxent
*auxp
;
2646 /* Look for a section aux entry here using the same tests that
2647 coff_swap_aux_out uses. */
2649 && (isym
.n_sclass
== C_STAT
2650 || isym
.n_sclass
== C_HIDDEN
)
2651 && isym
.n_type
== T_NULL
2652 && (h
->root
.type
== bfd_link_hash_defined
2653 || h
->root
.type
== bfd_link_hash_defweak
))
2657 sec
= h
->root
.u
.def
.section
->output_section
;
2660 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2664 /* For PE, an overflow on the final link reportedly does
2665 not matter. FIXME: Why not? */
2667 if (sec
->reloc_count
> 0xffff
2668 && (! obj_pe (output_bfd
)
2669 || finfo
->info
->relocatable
))
2670 (*_bfd_error_handler
)
2671 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2672 bfd_get_filename (output_bfd
),
2673 bfd_get_section_name (output_bfd
, sec
),
2676 if (sec
->lineno_count
> 0xffff
2677 && (! obj_pe (output_bfd
)
2678 || finfo
->info
->relocatable
))
2679 (*_bfd_error_handler
)
2680 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2681 bfd_get_filename (output_bfd
),
2682 bfd_get_section_name (output_bfd
, sec
),
2685 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2686 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2687 auxp
->x_scn
.x_checksum
= 0;
2688 auxp
->x_scn
.x_associated
= 0;
2689 auxp
->x_scn
.x_comdat
= 0;
2693 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isym
.n_type
,
2694 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2695 (PTR
) finfo
->outsyms
);
2696 if (bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2698 finfo
->failed
= TRUE
;
2701 ++obj_raw_syment_count (output_bfd
);
2707 /* Write out task global symbols, converting them to statics. Called
2708 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2709 the dirty work, if the symbol we are processing needs conversion. */
2712 _bfd_coff_write_task_globals (h
, data
)
2713 struct coff_link_hash_entry
*h
;
2716 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2717 bfd_boolean rtnval
= TRUE
;
2718 bfd_boolean save_global_to_static
;
2720 if (h
->root
.type
== bfd_link_hash_warning
)
2721 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2725 switch (h
->root
.type
)
2727 case bfd_link_hash_defined
:
2728 case bfd_link_hash_defweak
:
2729 save_global_to_static
= finfo
->global_to_static
;
2730 finfo
->global_to_static
= TRUE
;
2731 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2732 finfo
->global_to_static
= save_global_to_static
;
2741 /* Handle a link order which is supposed to generate a reloc. */
2744 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2746 struct coff_final_link_info
*finfo
;
2747 asection
*output_section
;
2748 struct bfd_link_order
*link_order
;
2750 reloc_howto_type
*howto
;
2751 struct internal_reloc
*irel
;
2752 struct coff_link_hash_entry
**rel_hash_ptr
;
2754 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2757 bfd_set_error (bfd_error_bad_value
);
2761 if (link_order
->u
.reloc
.p
->addend
!= 0)
2765 bfd_reloc_status_type rstat
;
2769 size
= bfd_get_reloc_size (howto
);
2770 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2774 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2775 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,\
2782 case bfd_reloc_outofrange
:
2784 case bfd_reloc_overflow
:
2785 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2787 (link_order
->type
== bfd_section_reloc_link_order
2788 ? bfd_section_name (output_bfd
,
2789 link_order
->u
.reloc
.p
->u
.section
)
2790 : link_order
->u
.reloc
.p
->u
.name
),
2791 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2792 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2799 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2800 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2807 /* Store the reloc information in the right place. It will get
2808 swapped and written out at the end of the final_link routine. */
2810 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2811 + output_section
->reloc_count
);
2812 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2813 + output_section
->reloc_count
);
2815 memset (irel
, 0, sizeof (struct internal_reloc
));
2816 *rel_hash_ptr
= NULL
;
2818 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2820 if (link_order
->type
== bfd_section_reloc_link_order
)
2822 /* We need to somehow locate a symbol in the right section. The
2823 symbol must either have a value of zero, or we must adjust
2824 the addend by the value of the symbol. FIXME: Write this
2825 when we need it. The old linker couldn't handle this anyhow. */
2827 *rel_hash_ptr
= NULL
;
2832 struct coff_link_hash_entry
*h
;
2834 h
= ((struct coff_link_hash_entry
*)
2835 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2836 link_order
->u
.reloc
.p
->u
.name
,
2837 FALSE
, FALSE
, TRUE
));
2841 irel
->r_symndx
= h
->indx
;
2844 /* Set the index to -2 to force this symbol to get
2853 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2854 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2855 (asection
*) NULL
, (bfd_vma
) 0)))
2861 /* FIXME: Is this always right? */
2862 irel
->r_type
= howto
->type
;
2864 /* r_size is only used on the RS/6000, which needs its own linker
2865 routines anyhow. r_extern is only used for ECOFF. */
2867 /* FIXME: What is the right value for r_offset? Is zero OK? */
2869 ++output_section
->reloc_count
;
2874 /* A basic reloc handling routine which may be used by processors with
2878 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2879 input_section
, contents
, relocs
, syms
,
2882 struct bfd_link_info
*info
;
2884 asection
*input_section
;
2886 struct internal_reloc
*relocs
;
2887 struct internal_syment
*syms
;
2888 asection
**sections
;
2890 struct internal_reloc
*rel
;
2891 struct internal_reloc
*relend
;
2894 relend
= rel
+ input_section
->reloc_count
;
2895 for (; rel
< relend
; rel
++)
2898 struct coff_link_hash_entry
*h
;
2899 struct internal_syment
*sym
;
2902 reloc_howto_type
*howto
;
2903 bfd_reloc_status_type rstat
;
2905 symndx
= rel
->r_symndx
;
2913 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2915 (*_bfd_error_handler
)
2916 ("%s: illegal symbol index %ld in relocs",
2917 bfd_archive_filename (input_bfd
), symndx
);
2922 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2923 sym
= syms
+ symndx
;
2926 /* COFF treats common symbols in one of two ways. Either the
2927 size of the symbol is included in the section contents, or it
2928 is not. We assume that the size is not included, and force
2929 the rtype_to_howto function to adjust the addend as needed. */
2931 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2932 addend
= - sym
->n_value
;
2936 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2941 /* If we are doing a relocatable link, then we can just ignore
2942 a PC relative reloc that is pcrel_offset. It will already
2943 have the correct value. If this is not a relocatable link,
2944 then we should ignore the symbol value. */
2945 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2947 if (info
->relocatable
)
2949 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2950 addend
+= sym
->n_value
;
2961 sec
= bfd_abs_section_ptr
;
2966 sec
= sections
[symndx
];
2967 val
= (sec
->output_section
->vma
2968 + sec
->output_offset
2970 if (! obj_pe (input_bfd
))
2976 if (h
->root
.type
== bfd_link_hash_defined
2977 || h
->root
.type
== bfd_link_hash_defweak
)
2981 sec
= h
->root
.u
.def
.section
;
2982 val
= (h
->root
.u
.def
.value
2983 + sec
->output_section
->vma
2984 + sec
->output_offset
);
2987 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2990 else if (! info
->relocatable
)
2992 if (! ((*info
->callbacks
->undefined_symbol
)
2993 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2994 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
2999 if (info
->base_file
)
3001 /* Emit a reloc if the backend thinks it needs it. */
3002 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
3004 /* Relocation to a symbol in a section which isn't
3005 absolute. We output the address here to a file.
3006 This file is then read by dlltool when generating the
3007 reloc section. Note that the base file is not
3008 portable between systems. We write out a long here,
3009 and dlltool reads in a long. */
3010 long addr
= (rel
->r_vaddr
3011 - input_section
->vma
3012 + input_section
->output_offset
3013 + input_section
->output_section
->vma
);
3014 if (coff_data (output_bfd
)->pe
)
3015 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
3016 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
3019 bfd_set_error (bfd_error_system_call
);
3025 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3027 rel
->r_vaddr
- input_section
->vma
,
3036 case bfd_reloc_outofrange
:
3037 (*_bfd_error_handler
)
3038 (_("%s: bad reloc address 0x%lx in section `%s'"),
3039 bfd_archive_filename (input_bfd
),
3040 (unsigned long) rel
->r_vaddr
,
3041 bfd_get_section_name (input_bfd
, input_section
));
3043 case bfd_reloc_overflow
:
3046 char buf
[SYMNMLEN
+ 1];
3051 name
= h
->root
.root
.string
;
3054 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3059 if (! ((*info
->callbacks
->reloc_overflow
)
3060 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
3061 input_section
, rel
->r_vaddr
- input_section
->vma
)))