1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file contains the COFF backend linker code. */
27 #include "coff/internal.h"
30 static boolean coff_link_add_object_symbols
31 PARAMS ((bfd
*, struct bfd_link_info
*));
32 static boolean coff_link_check_archive_element
33 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
34 static boolean coff_link_check_ar_symbols
35 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
36 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
37 static char *dores_com
PARAMS ((char *, bfd
*, int));
38 static char *get_name
PARAMS ((char *, char **));
39 static int process_embedded_commands
40 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*));
41 static void mark_relocs
PARAMS ((struct coff_final_link_info
*, bfd
*));
43 /* Return true if SYM is a weak, external symbol. */
44 #define IS_WEAK_EXTERNAL(abfd, sym) \
45 ((sym).n_sclass == C_WEAKEXT \
46 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
48 /* Return true if SYM is an external symbol. */
49 #define IS_EXTERNAL(abfd, sym) \
50 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
52 /* Define macros so that the ISFCN, et. al., macros work correctly.
53 These macros are defined in include/coff/internal.h in terms of
54 N_TMASK, etc. These definitions require a user to define local
55 variables with the appropriate names, and with values from the
56 coff_data (abfd) structure. */
58 #define N_TMASK n_tmask
59 #define N_BTSHFT n_btshft
60 #define N_BTMASK n_btmask
62 /* Create an entry in a COFF linker hash table. */
64 struct bfd_hash_entry
*
65 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
66 struct bfd_hash_entry
*entry
;
67 struct bfd_hash_table
*table
;
70 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
72 /* Allocate the structure if it has not already been allocated by a
74 if (ret
== (struct coff_link_hash_entry
*) NULL
)
75 ret
= ((struct coff_link_hash_entry
*)
76 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
77 if (ret
== (struct coff_link_hash_entry
*) NULL
)
78 return (struct bfd_hash_entry
*) ret
;
80 /* Call the allocation method of the superclass. */
81 ret
= ((struct coff_link_hash_entry
*)
82 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
84 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
86 /* Set local fields. */
95 return (struct bfd_hash_entry
*) ret
;
98 /* Initialize a COFF linker hash table. */
101 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
102 struct coff_link_hash_table
*table
;
104 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
105 struct bfd_hash_table
*,
108 table
->stab_info
= NULL
;
109 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
112 /* Create a COFF linker hash table. */
114 struct bfd_link_hash_table
*
115 _bfd_coff_link_hash_table_create (abfd
)
118 struct coff_link_hash_table
*ret
;
120 ret
= ((struct coff_link_hash_table
*)
121 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
124 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
125 _bfd_coff_link_hash_newfunc
))
127 bfd_release (abfd
, ret
);
128 return (struct bfd_link_hash_table
*) NULL
;
133 /* Create an entry in a COFF debug merge hash table. */
135 struct bfd_hash_entry
*
136 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
137 struct bfd_hash_entry
*entry
;
138 struct bfd_hash_table
*table
;
141 struct coff_debug_merge_hash_entry
*ret
=
142 (struct coff_debug_merge_hash_entry
*) entry
;
144 /* Allocate the structure if it has not already been allocated by a
146 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
147 ret
= ((struct coff_debug_merge_hash_entry
*)
148 bfd_hash_allocate (table
,
149 sizeof (struct coff_debug_merge_hash_entry
)));
150 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
151 return (struct bfd_hash_entry
*) ret
;
153 /* Call the allocation method of the superclass. */
154 ret
= ((struct coff_debug_merge_hash_entry
*)
155 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
156 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
158 /* Set local fields. */
162 return (struct bfd_hash_entry
*) ret
;
165 /* Given a COFF BFD, add symbols to the global hash table as
169 _bfd_coff_link_add_symbols (abfd
, info
)
171 struct bfd_link_info
*info
;
173 switch (bfd_get_format (abfd
))
176 return coff_link_add_object_symbols (abfd
, info
);
178 return (_bfd_generic_link_add_archive_symbols
179 (abfd
, info
, coff_link_check_archive_element
));
181 bfd_set_error (bfd_error_wrong_format
);
186 /* Add symbols from a COFF object file. */
189 coff_link_add_object_symbols (abfd
, info
)
191 struct bfd_link_info
*info
;
193 if (! _bfd_coff_get_external_symbols (abfd
))
195 if (! coff_link_add_symbols (abfd
, info
))
198 if (! info
->keep_memory
)
200 if (! _bfd_coff_free_symbols (abfd
))
206 /* Check a single archive element to see if we need to include it in
207 the link. *PNEEDED is set according to whether this element is
208 needed in the link or not. This is called via
209 _bfd_generic_link_add_archive_symbols. */
212 coff_link_check_archive_element (abfd
, info
, pneeded
)
214 struct bfd_link_info
*info
;
217 if (! _bfd_coff_get_external_symbols (abfd
))
220 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
225 if (! coff_link_add_symbols (abfd
, info
))
229 if (! info
->keep_memory
|| ! *pneeded
)
231 if (! _bfd_coff_free_symbols (abfd
))
238 /* Look through the symbols to see if this object file should be
239 included in the link. */
242 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
244 struct bfd_link_info
*info
;
247 bfd_size_type symesz
;
253 symesz
= bfd_coff_symesz (abfd
);
254 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
255 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
256 while (esym
< esym_end
)
258 struct internal_syment sym
;
259 enum coff_symbol_classification classification
;
261 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
263 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
264 if (classification
== COFF_SYMBOL_GLOBAL
265 || classification
== COFF_SYMBOL_COMMON
)
268 char buf
[SYMNMLEN
+ 1];
269 struct bfd_link_hash_entry
*h
;
271 /* This symbol is externally visible, and is defined by this
274 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
277 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
279 /* We are only interested in symbols that are currently
280 undefined. If a symbol is currently known to be common,
281 COFF linkers do not bring in an object file which defines
283 if (h
!= (struct bfd_link_hash_entry
*) NULL
284 && h
->type
== bfd_link_hash_undefined
)
286 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
293 esym
+= (sym
.n_numaux
+ 1) * symesz
;
296 /* We do not need this object file. */
300 /* Add all the symbols from an object file to the hash table. */
303 coff_link_add_symbols (abfd
, info
)
305 struct bfd_link_info
*info
;
307 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
308 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
309 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
311 boolean default_copy
;
312 bfd_size_type symcount
;
313 struct coff_link_hash_entry
**sym_hash
;
314 bfd_size_type symesz
;
318 /* Keep the symbols during this function, in case the linker needs
319 to read the generic symbols in order to report an error message. */
320 keep_syms
= obj_coff_keep_syms (abfd
);
321 obj_coff_keep_syms (abfd
) = true;
323 if (info
->keep_memory
)
324 default_copy
= false;
328 symcount
= obj_raw_syment_count (abfd
);
330 /* We keep a list of the linker hash table entries that correspond
331 to particular symbols. */
332 sym_hash
= ((struct coff_link_hash_entry
**)
335 * sizeof (struct coff_link_hash_entry
*))));
336 if (sym_hash
== NULL
&& symcount
!= 0)
338 obj_coff_sym_hashes (abfd
) = sym_hash
;
340 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
342 symesz
= bfd_coff_symesz (abfd
);
343 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
344 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
345 esym_end
= esym
+ symcount
* symesz
;
346 while (esym
< esym_end
)
348 struct internal_syment sym
;
349 enum coff_symbol_classification classification
;
352 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
354 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
355 if (classification
!= COFF_SYMBOL_LOCAL
)
358 char buf
[SYMNMLEN
+ 1];
364 /* This symbol is externally visible. */
366 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
370 /* We must copy the name into memory if we got it from the
371 syment itself, rather than the string table. */
373 if (sym
._n
._n_n
._n_zeroes
!= 0
374 || sym
._n
._n_n
._n_offset
== 0)
379 switch (classification
)
384 case COFF_SYMBOL_GLOBAL
:
385 flags
= BSF_EXPORT
| BSF_GLOBAL
;
386 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
388 value
-= section
->vma
;
391 case COFF_SYMBOL_UNDEFINED
:
393 section
= bfd_und_section_ptr
;
396 case COFF_SYMBOL_COMMON
:
398 section
= bfd_com_section_ptr
;
401 case COFF_SYMBOL_PE_SECTION
:
402 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
403 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
407 if (IS_WEAK_EXTERNAL (abfd
, sym
))
412 /* In the PE format, section symbols actually refer to the
413 start of the output section. We handle them specially
415 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
417 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
418 name
, false, copy
, false);
419 if (*sym_hash
!= NULL
)
421 if (((*sym_hash
)->coff_link_hash_flags
422 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
423 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
424 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
425 (*_bfd_error_handler
)
426 ("Warning: symbol `%s' is both section and non-section",
433 /* The Microsoft Visual C compiler does string pooling by
434 hashing the constants to an internal symbol name, and
435 relying on the the linker comdat support to discard
436 duplicate names. However, if one string is a literal and
437 one is a data initializer, one will end up in the .data
438 section and one will end up in the .rdata section. The
439 Microsoft linker will combine them into the .data
440 section, which seems to be wrong since it might cause the
443 As long as there are no external references to the
444 symbols, which there shouldn't be, we can treat the .data
445 and .rdata instances as separate symbols. The comdat
446 code in the linker will do the appropriate merging. Here
447 we avoid getting a multiple definition error for one of
448 these special symbols.
450 FIXME: I don't think this will work in the case where
451 there are two object files which use the constants as a
452 literal and two object files which use it as a data
453 initializer. One or the other of the second object files
454 is going to wind up with an inappropriate reference. */
456 && (classification
== COFF_SYMBOL_GLOBAL
457 || classification
== COFF_SYMBOL_PE_SECTION
)
458 && section
->comdat
!= NULL
459 && strncmp (name
, "??_", 3) == 0
460 && strcmp (name
, section
->comdat
->name
) == 0)
462 if (*sym_hash
== NULL
)
463 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
464 name
, false, copy
, false);
465 if (*sym_hash
!= NULL
466 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
467 && (*sym_hash
)->root
.u
.def
.section
->comdat
!= NULL
468 && strcmp ((*sym_hash
)->root
.u
.def
.section
->comdat
->name
,
469 section
->comdat
->name
) == 0)
475 if (! (bfd_coff_link_add_one_symbol
476 (info
, abfd
, name
, flags
, section
, value
,
477 (const char *) NULL
, copy
, false,
478 (struct bfd_link_hash_entry
**) sym_hash
)))
482 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
483 (*sym_hash
)->coff_link_hash_flags
|=
484 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
486 /* Limit the alignment of a common symbol to the possible
487 alignment of a section. There is no point to permitting
488 a higher alignment for a common symbol: we can not
489 guarantee it, and it may cause us to allocate extra space
490 in the common section. */
491 if (section
== bfd_com_section_ptr
492 && (*sym_hash
)->root
.type
== bfd_link_hash_common
493 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
494 > bfd_coff_default_section_alignment_power (abfd
)))
495 (*sym_hash
)->root
.u
.c
.p
->alignment_power
496 = bfd_coff_default_section_alignment_power (abfd
);
498 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
500 /* If we don't have any symbol information currently in
501 the hash table, or if we are looking at a symbol
502 definition, then update the symbol class and type in
504 if (((*sym_hash
)->class == C_NULL
505 && (*sym_hash
)->type
== T_NULL
)
508 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
509 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
511 (*sym_hash
)->class = sym
.n_sclass
;
512 if (sym
.n_type
!= T_NULL
)
514 /* We want to warn if the type changed, but not
515 if it changed from an unspecified type.
516 Testing the whole type byte may work, but the
517 change from (e.g.) a function of unspecified
518 type to function of known type also wants to
520 if ((*sym_hash
)->type
!= T_NULL
521 && (*sym_hash
)->type
!= sym
.n_type
522 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
523 && (BTYPE ((*sym_hash
)->type
) == T_NULL
524 || BTYPE (sym
.n_type
) == T_NULL
)))
525 (*_bfd_error_handler
)
526 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
527 name
, (*sym_hash
)->type
, sym
.n_type
,
528 bfd_get_filename (abfd
));
530 /* We don't want to change from a meaningful
531 base type to a null one, but if we know
532 nothing, take what little we might now know. */
533 if (BTYPE (sym
.n_type
) != T_NULL
534 || (*sym_hash
)->type
== T_NULL
)
535 (*sym_hash
)->type
= sym
.n_type
;
537 (*sym_hash
)->auxbfd
= abfd
;
538 if (sym
.n_numaux
!= 0)
540 union internal_auxent
*alloc
;
543 union internal_auxent
*iaux
;
545 (*sym_hash
)->numaux
= sym
.n_numaux
;
546 alloc
= ((union internal_auxent
*)
547 bfd_hash_allocate (&info
->hash
->table
,
549 * sizeof (*alloc
))));
552 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
554 i
++, eaux
+= symesz
, iaux
++)
555 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
556 sym
.n_sclass
, i
, sym
.n_numaux
,
558 (*sym_hash
)->aux
= alloc
;
563 if (classification
== COFF_SYMBOL_PE_SECTION
564 && (*sym_hash
)->numaux
!= 0)
566 /* Some PE sections (such as .bss) have a zero size in
567 the section header, but a non-zero size in the AUX
568 record. Correct that here.
570 FIXME: This is not at all the right place to do this.
571 For example, it won't help objdump. This needs to be
572 done when we swap in the section header. */
574 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
575 if (section
->_raw_size
== 0)
576 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
578 /* FIXME: We could test whether the section sizes
579 matches the size in the aux entry, but apparently
580 that sometimes fails unexpectedly. */
584 esym
+= (sym
.n_numaux
+ 1) * symesz
;
585 sym_hash
+= sym
.n_numaux
+ 1;
588 /* If this is a non-traditional, non-relocateable link, try to
589 optimize the handling of any .stab/.stabstr sections. */
590 if (! info
->relocateable
591 && ! info
->traditional_format
592 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
593 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
595 asection
*stab
, *stabstr
;
597 stab
= bfd_get_section_by_name (abfd
, ".stab");
600 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
604 struct coff_link_hash_table
*table
;
605 struct coff_section_tdata
*secdata
;
607 secdata
= coff_section_data (abfd
, stab
);
611 (PTR
) bfd_zalloc (abfd
,
612 sizeof (struct coff_section_tdata
));
613 if (stab
->used_by_bfd
== NULL
)
615 secdata
= coff_section_data (abfd
, stab
);
618 table
= coff_hash_table (info
);
620 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
622 &secdata
->stab_info
))
628 obj_coff_keep_syms (abfd
) = keep_syms
;
633 obj_coff_keep_syms (abfd
) = keep_syms
;
637 /* Do the final link step. */
640 _bfd_coff_final_link (abfd
, info
)
642 struct bfd_link_info
*info
;
644 bfd_size_type symesz
;
645 struct coff_final_link_info finfo
;
646 boolean debug_merge_allocated
;
647 boolean long_section_names
;
649 struct bfd_link_order
*p
;
650 size_t max_sym_count
;
651 size_t max_lineno_count
;
652 size_t max_reloc_count
;
653 size_t max_output_reloc_count
;
654 size_t max_contents_size
;
655 file_ptr rel_filepos
;
657 file_ptr line_filepos
;
660 bfd_byte
*external_relocs
= NULL
;
661 char strbuf
[STRING_SIZE_SIZE
];
663 symesz
= bfd_coff_symesz (abfd
);
666 finfo
.output_bfd
= abfd
;
668 finfo
.section_info
= NULL
;
669 finfo
.last_file_index
= -1;
670 finfo
.last_bf_index
= -1;
671 finfo
.internal_syms
= NULL
;
672 finfo
.sec_ptrs
= NULL
;
673 finfo
.sym_indices
= NULL
;
674 finfo
.outsyms
= NULL
;
675 finfo
.linenos
= NULL
;
676 finfo
.contents
= NULL
;
677 finfo
.external_relocs
= NULL
;
678 finfo
.internal_relocs
= NULL
;
679 finfo
.global_to_static
= false;
680 debug_merge_allocated
= false;
682 coff_data (abfd
)->link_info
= info
;
684 finfo
.strtab
= _bfd_stringtab_init ();
685 if (finfo
.strtab
== NULL
)
688 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
690 debug_merge_allocated
= true;
692 /* Compute the file positions for all the sections. */
693 if (! abfd
->output_has_begun
)
695 if (! bfd_coff_compute_section_file_positions (abfd
))
699 /* Count the line numbers and relocation entries required for the
700 output file. Set the file positions for the relocs. */
701 rel_filepos
= obj_relocbase (abfd
);
702 relsz
= bfd_coff_relsz (abfd
);
703 max_contents_size
= 0;
704 max_lineno_count
= 0;
707 long_section_names
= false;
708 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
712 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
714 if (p
->type
== bfd_indirect_link_order
)
718 sec
= p
->u
.indirect
.section
;
720 /* Mark all sections which are to be included in the
721 link. This will normally be every section. We need
722 to do this so that we can identify any sections which
723 the linker has decided to not include. */
724 sec
->linker_mark
= true;
726 if (info
->strip
== strip_none
727 || info
->strip
== strip_some
)
728 o
->lineno_count
+= sec
->lineno_count
;
730 if (info
->relocateable
)
731 o
->reloc_count
+= sec
->reloc_count
;
733 if (sec
->_raw_size
> max_contents_size
)
734 max_contents_size
= sec
->_raw_size
;
735 if (sec
->lineno_count
> max_lineno_count
)
736 max_lineno_count
= sec
->lineno_count
;
737 if (sec
->reloc_count
> max_reloc_count
)
738 max_reloc_count
= sec
->reloc_count
;
740 else if (info
->relocateable
741 && (p
->type
== bfd_section_reloc_link_order
742 || p
->type
== bfd_symbol_reloc_link_order
))
745 if (o
->reloc_count
== 0)
749 o
->flags
|= SEC_RELOC
;
750 o
->rel_filepos
= rel_filepos
;
751 rel_filepos
+= o
->reloc_count
* relsz
;
754 if (bfd_coff_long_section_names (abfd
)
755 && strlen (o
->name
) > SCNNMLEN
)
757 /* This section has a long name which must go in the string
758 table. This must correspond to the code in
759 coff_write_object_contents which puts the string index
760 into the s_name field of the section header. That is why
761 we pass hash as false. */
762 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
763 == (bfd_size_type
) -1)
765 long_section_names
= true;
769 /* If doing a relocateable link, allocate space for the pointers we
771 if (info
->relocateable
)
775 /* We use section_count + 1, rather than section_count, because
776 the target_index fields are 1 based. */
778 ((struct coff_link_section_info
*)
779 bfd_malloc ((abfd
->section_count
+ 1)
780 * sizeof (struct coff_link_section_info
)));
781 if (finfo
.section_info
== NULL
)
783 for (i
= 0; i
<= abfd
->section_count
; i
++)
785 finfo
.section_info
[i
].relocs
= NULL
;
786 finfo
.section_info
[i
].rel_hashes
= NULL
;
790 /* We now know the size of the relocs, so we can determine the file
791 positions of the line numbers. */
792 line_filepos
= rel_filepos
;
793 linesz
= bfd_coff_linesz (abfd
);
794 max_output_reloc_count
= 0;
795 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
797 if (o
->lineno_count
== 0)
801 o
->line_filepos
= line_filepos
;
802 line_filepos
+= o
->lineno_count
* linesz
;
805 if (o
->reloc_count
!= 0)
807 /* We don't know the indices of global symbols until we have
808 written out all the local symbols. For each section in
809 the output file, we keep an array of pointers to hash
810 table entries. Each entry in the array corresponds to a
811 reloc. When we find a reloc against a global symbol, we
812 set the corresponding entry in this array so that we can
813 fix up the symbol index after we have written out all the
816 Because of this problem, we also keep the relocs in
817 memory until the end of the link. This wastes memory,
818 but only when doing a relocateable link, which is not the
820 BFD_ASSERT (info
->relocateable
);
821 finfo
.section_info
[o
->target_index
].relocs
=
822 ((struct internal_reloc
*)
823 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
824 finfo
.section_info
[o
->target_index
].rel_hashes
=
825 ((struct coff_link_hash_entry
**)
826 bfd_malloc (o
->reloc_count
827 * sizeof (struct coff_link_hash_entry
*)));
828 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
829 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
832 if (o
->reloc_count
> max_output_reloc_count
)
833 max_output_reloc_count
= o
->reloc_count
;
836 /* Reset the reloc and lineno counts, so that we can use them to
837 count the number of entries we have output so far. */
842 obj_sym_filepos (abfd
) = line_filepos
;
844 /* Figure out the largest number of symbols in an input BFD. Take
845 the opportunity to clear the output_has_begun fields of all the
848 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
852 sub
->output_has_begun
= false;
853 sz
= obj_raw_syment_count (sub
);
854 if (sz
> max_sym_count
)
858 /* Allocate some buffers used while linking. */
859 finfo
.internal_syms
= ((struct internal_syment
*)
860 bfd_malloc (max_sym_count
861 * sizeof (struct internal_syment
)));
862 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
863 * sizeof (asection
*));
864 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
865 finfo
.outsyms
= ((bfd_byte
*)
866 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
867 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
868 * bfd_coff_linesz (abfd
));
869 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
870 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
871 if (! info
->relocateable
)
872 finfo
.internal_relocs
= ((struct internal_reloc
*)
873 bfd_malloc (max_reloc_count
874 * sizeof (struct internal_reloc
)));
875 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
876 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
877 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
878 || finfo
.outsyms
== NULL
879 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
880 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
881 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
882 || (! info
->relocateable
883 && finfo
.internal_relocs
== NULL
884 && max_reloc_count
> 0))
887 /* We now know the position of everything in the file, except that
888 we don't know the size of the symbol table and therefore we don't
889 know where the string table starts. We just build the string
890 table in memory as we go along. We process all the relocations
891 for a single input file at once. */
892 obj_raw_syment_count (abfd
) = 0;
894 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
896 if (! bfd_coff_start_final_link (abfd
, info
))
900 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
902 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
904 if (p
->type
== bfd_indirect_link_order
905 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
907 sub
= p
->u
.indirect
.section
->owner
;
908 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
910 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
912 sub
->output_has_begun
= true;
915 else if (p
->type
== bfd_section_reloc_link_order
916 || p
->type
== bfd_symbol_reloc_link_order
)
918 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
923 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
929 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
932 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
934 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
935 debug_merge_allocated
= false;
937 if (finfo
.internal_syms
!= NULL
)
939 free (finfo
.internal_syms
);
940 finfo
.internal_syms
= NULL
;
942 if (finfo
.sec_ptrs
!= NULL
)
944 free (finfo
.sec_ptrs
);
945 finfo
.sec_ptrs
= NULL
;
947 if (finfo
.sym_indices
!= NULL
)
949 free (finfo
.sym_indices
);
950 finfo
.sym_indices
= NULL
;
952 if (finfo
.linenos
!= NULL
)
954 free (finfo
.linenos
);
955 finfo
.linenos
= NULL
;
957 if (finfo
.contents
!= NULL
)
959 free (finfo
.contents
);
960 finfo
.contents
= NULL
;
962 if (finfo
.external_relocs
!= NULL
)
964 free (finfo
.external_relocs
);
965 finfo
.external_relocs
= NULL
;
967 if (finfo
.internal_relocs
!= NULL
)
969 free (finfo
.internal_relocs
);
970 finfo
.internal_relocs
= NULL
;
973 /* The value of the last C_FILE symbol is supposed to be the symbol
974 index of the first external symbol. Write it out again if
976 if (finfo
.last_file_index
!= -1
977 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
979 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
980 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
981 (PTR
) finfo
.outsyms
);
983 (obj_sym_filepos (abfd
)
984 + finfo
.last_file_index
* symesz
),
986 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
990 /* If doing task linking (ld --task-link) then make a pass through the
991 global symbols, writing out any that are defined, and making them
995 finfo
.failed
= false;
996 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
1002 /* Write out the global symbols. */
1003 finfo
.failed
= false;
1004 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
1009 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1010 if (finfo
.outsyms
!= NULL
)
1012 free (finfo
.outsyms
);
1013 finfo
.outsyms
= NULL
;
1016 if (info
->relocateable
&& max_output_reloc_count
> 0)
1018 /* Now that we have written out all the global symbols, we know
1019 the symbol indices to use for relocs against them, and we can
1020 finally write out the relocs. */
1021 external_relocs
= ((bfd_byte
*)
1022 bfd_malloc (max_output_reloc_count
* relsz
));
1023 if (external_relocs
== NULL
)
1026 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1028 struct internal_reloc
*irel
;
1029 struct internal_reloc
*irelend
;
1030 struct coff_link_hash_entry
**rel_hash
;
1033 if (o
->reloc_count
== 0)
1036 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1037 irelend
= irel
+ o
->reloc_count
;
1038 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1039 erel
= external_relocs
;
1040 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1042 if (*rel_hash
!= NULL
)
1044 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1045 irel
->r_symndx
= (*rel_hash
)->indx
;
1047 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1050 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1051 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
1052 abfd
) != relsz
* o
->reloc_count
)
1056 free (external_relocs
);
1057 external_relocs
= NULL
;
1060 /* Free up the section information. */
1061 if (finfo
.section_info
!= NULL
)
1065 for (i
= 0; i
< abfd
->section_count
; i
++)
1067 if (finfo
.section_info
[i
].relocs
!= NULL
)
1068 free (finfo
.section_info
[i
].relocs
);
1069 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1070 free (finfo
.section_info
[i
].rel_hashes
);
1072 free (finfo
.section_info
);
1073 finfo
.section_info
= NULL
;
1076 /* If we have optimized stabs strings, output them. */
1077 if (coff_hash_table (info
)->stab_info
!= NULL
)
1079 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1083 /* Write out the string table. */
1084 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1087 (obj_sym_filepos (abfd
)
1088 + obj_raw_syment_count (abfd
) * symesz
),
1092 #if STRING_SIZE_SIZE == 4
1094 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1095 (bfd_byte
*) strbuf
);
1097 #error Change bfd_h_put_32
1100 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1103 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1106 obj_coff_strings_written (abfd
) = true;
1109 _bfd_stringtab_free (finfo
.strtab
);
1111 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1112 not try to write out the symbols. */
1113 bfd_get_symcount (abfd
) = 0;
1118 if (debug_merge_allocated
)
1119 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1120 if (finfo
.strtab
!= NULL
)
1121 _bfd_stringtab_free (finfo
.strtab
);
1122 if (finfo
.section_info
!= NULL
)
1126 for (i
= 0; i
< abfd
->section_count
; i
++)
1128 if (finfo
.section_info
[i
].relocs
!= NULL
)
1129 free (finfo
.section_info
[i
].relocs
);
1130 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1131 free (finfo
.section_info
[i
].rel_hashes
);
1133 free (finfo
.section_info
);
1135 if (finfo
.internal_syms
!= NULL
)
1136 free (finfo
.internal_syms
);
1137 if (finfo
.sec_ptrs
!= NULL
)
1138 free (finfo
.sec_ptrs
);
1139 if (finfo
.sym_indices
!= NULL
)
1140 free (finfo
.sym_indices
);
1141 if (finfo
.outsyms
!= NULL
)
1142 free (finfo
.outsyms
);
1143 if (finfo
.linenos
!= NULL
)
1144 free (finfo
.linenos
);
1145 if (finfo
.contents
!= NULL
)
1146 free (finfo
.contents
);
1147 if (finfo
.external_relocs
!= NULL
)
1148 free (finfo
.external_relocs
);
1149 if (finfo
.internal_relocs
!= NULL
)
1150 free (finfo
.internal_relocs
);
1151 if (external_relocs
!= NULL
)
1152 free (external_relocs
);
1156 /* parse out a -heap <reserved>,<commit> line */
1159 dores_com (ptr
, output_bfd
, heap
)
1164 if (coff_data(output_bfd
)->pe
)
1166 int val
= strtoul (ptr
, &ptr
, 0);
1168 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1170 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1174 int val
= strtoul (ptr
+1, &ptr
, 0);
1176 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1178 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1184 static char *get_name(ptr
, dst
)
1191 while (*ptr
&& *ptr
!= ' ')
1197 /* Process any magic embedded commands in a section called .drectve */
1200 process_embedded_commands (output_bfd
, info
, abfd
)
1202 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1205 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1212 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1215 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1220 e
= copy
+ sec
->_raw_size
;
1221 for (s
= copy
; s
< e
; )
1227 if (strncmp (s
,"-attr", 5) == 0)
1239 s
= get_name(s
, &name
);
1240 s
= get_name(s
, &attribs
);
1260 asec
= bfd_get_section_by_name (abfd
, name
);
1263 asec
->flags
|= SEC_CODE
;
1265 asec
->flags
|= SEC_READONLY
;
1268 else if (strncmp (s
,"-heap", 5) == 0)
1270 s
= dores_com (s
+5, output_bfd
, 1);
1272 else if (strncmp (s
,"-stack", 6) == 0)
1274 s
= dores_com (s
+6, output_bfd
, 0);
1283 /* Place a marker against all symbols which are used by relocations.
1284 This marker can be picked up by the 'do we skip this symbol ?'
1285 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1290 mark_relocs (finfo
, input_bfd
)
1291 struct coff_final_link_info
* finfo
;
1296 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1299 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1301 struct internal_reloc
* internal_relocs
;
1302 struct internal_reloc
* irel
;
1303 struct internal_reloc
* irelend
;
1305 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1308 /* Read in the relocs. */
1309 internal_relocs
= _bfd_coff_read_internal_relocs
1310 (input_bfd
, a
, false,
1311 finfo
->external_relocs
,
1312 finfo
->info
->relocateable
,
1313 (finfo
->info
->relocateable
1314 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1315 : finfo
->internal_relocs
)
1318 if (internal_relocs
== NULL
)
1321 irel
= internal_relocs
;
1322 irelend
= irel
+ a
->reloc_count
;
1324 /* Place a mark in the sym_indices array (whose entries have
1325 been initialised to 0) for all of the symbols that are used
1326 in the relocation table. This will then be picked up in the
1329 for (; irel
< irelend
; irel
++)
1331 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1336 /* Link an input file into the linker output file. This function
1337 handles all the sections and relocations of the input file at once. */
1340 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1341 struct coff_final_link_info
*finfo
;
1344 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1345 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1347 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1349 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1350 asection
*, struct internal_reloc
*,
1353 const char *strings
;
1354 bfd_size_type syment_base
;
1356 bfd_size_type isymesz
;
1357 bfd_size_type osymesz
;
1358 bfd_size_type linesz
;
1361 struct internal_syment
*isymp
;
1364 unsigned long output_index
;
1366 struct coff_link_hash_entry
**sym_hash
;
1369 /* Move all the symbols to the output file. */
1371 output_bfd
= finfo
->output_bfd
;
1373 syment_base
= obj_raw_syment_count (output_bfd
);
1374 isymesz
= bfd_coff_symesz (input_bfd
);
1375 osymesz
= bfd_coff_symesz (output_bfd
);
1376 linesz
= bfd_coff_linesz (input_bfd
);
1377 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1380 if (! finfo
->info
->keep_memory
)
1383 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1386 if (! _bfd_coff_get_external_symbols (input_bfd
))
1389 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1390 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1391 isymp
= finfo
->internal_syms
;
1392 secpp
= finfo
->sec_ptrs
;
1393 indexp
= finfo
->sym_indices
;
1394 output_index
= syment_base
;
1395 outsym
= finfo
->outsyms
;
1397 if (coff_data (output_bfd
)->pe
)
1399 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1403 /* If we are going to perform relocations and also strip/discard some symbols
1404 then we must make sure that we do not strip/discard those symbols that are
1405 going to be involved in the relocations */
1406 if (( finfo
->info
->strip
!= strip_none
1407 || finfo
->info
->discard
!= discard_none
)
1408 && finfo
->info
->relocateable
)
1410 /* mark the symbol array as 'not-used' */
1411 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1413 mark_relocs (finfo
, input_bfd
);
1416 while (esym
< esym_end
)
1418 struct internal_syment isym
;
1419 enum coff_symbol_classification classification
;
1422 boolean dont_skip_symbol
;
1425 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1427 /* Make a copy of *isymp so that the relocate_section function
1428 always sees the original values. This is more reliable than
1429 always recomputing the symbol value even if we are stripping
1433 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1434 switch (classification
)
1438 case COFF_SYMBOL_GLOBAL
:
1439 case COFF_SYMBOL_PE_SECTION
:
1440 case COFF_SYMBOL_LOCAL
:
1441 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1443 case COFF_SYMBOL_COMMON
:
1444 *secpp
= bfd_com_section_ptr
;
1446 case COFF_SYMBOL_UNDEFINED
:
1447 *secpp
= bfd_und_section_ptr
;
1451 /* Extract the flag indicating if this symbol is used by a
1453 if ((finfo
->info
->strip
!= strip_none
1454 || finfo
->info
->discard
!= discard_none
)
1455 && finfo
->info
->relocateable
)
1456 dont_skip_symbol
= *indexp
;
1458 dont_skip_symbol
= false;
1464 add
= 1 + isym
.n_numaux
;
1466 /* If we are stripping all symbols, we want to skip this one. */
1467 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1472 switch (classification
)
1476 case COFF_SYMBOL_GLOBAL
:
1477 case COFF_SYMBOL_COMMON
:
1478 case COFF_SYMBOL_PE_SECTION
:
1479 /* This is a global symbol. Global symbols come at the
1480 end of the symbol table, so skip them for now.
1481 Locally defined function symbols, however, are an
1482 exception, and are not moved to the end. */
1484 if (! ISFCN (isym
.n_type
))
1488 case COFF_SYMBOL_UNDEFINED
:
1489 /* Undefined symbols are left for the end. */
1494 case COFF_SYMBOL_LOCAL
:
1495 /* This is a local symbol. Skip it if we are discarding
1497 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1503 /* If we stripping debugging symbols, and this is a debugging
1504 symbol, then skip it. FIXME: gas sets the section to N_ABS
1505 for some types of debugging symbols; I don't know if this is
1506 a bug or not. In any case, we handle it here. */
1508 && finfo
->info
->strip
== strip_debugger
1509 && ! dont_skip_symbol
1510 && (isym
.n_scnum
== N_DEBUG
1511 || (isym
.n_scnum
== N_ABS
1512 && (isym
.n_sclass
== C_AUTO
1513 || isym
.n_sclass
== C_REG
1514 || isym
.n_sclass
== C_MOS
1515 || isym
.n_sclass
== C_MOE
1516 || isym
.n_sclass
== C_MOU
1517 || isym
.n_sclass
== C_ARG
1518 || isym
.n_sclass
== C_REGPARM
1519 || isym
.n_sclass
== C_FIELD
1520 || isym
.n_sclass
== C_EOS
))))
1523 /* If some symbols are stripped based on the name, work out the
1524 name and decide whether to skip this symbol. */
1526 && (finfo
->info
->strip
== strip_some
1527 || finfo
->info
->discard
== discard_l
))
1530 char buf
[SYMNMLEN
+ 1];
1532 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1536 if (! dont_skip_symbol
1537 && ((finfo
->info
->strip
== strip_some
1538 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1541 && finfo
->info
->discard
== discard_l
1542 && bfd_is_local_label_name (input_bfd
, name
))))
1546 /* If this is an enum, struct, or union tag, see if we have
1547 already output an identical type. */
1549 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1550 && (isym
.n_sclass
== C_ENTAG
1551 || isym
.n_sclass
== C_STRTAG
1552 || isym
.n_sclass
== C_UNTAG
)
1553 && isym
.n_numaux
== 1)
1556 char buf
[SYMNMLEN
+ 1];
1557 struct coff_debug_merge_hash_entry
*mh
;
1558 struct coff_debug_merge_type
*mt
;
1559 union internal_auxent aux
;
1560 struct coff_debug_merge_element
**epp
;
1561 bfd_byte
*esl
, *eslend
;
1562 struct internal_syment
*islp
;
1564 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1568 /* Ignore fake names invented by compiler; treat them all as
1570 if (*name
== '~' || *name
== '.' || *name
== '$'
1571 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1572 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1575 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1580 /* Allocate memory to hold type information. If this turns
1581 out to be a duplicate, we pass this address to
1583 mt
= ((struct coff_debug_merge_type
*)
1584 bfd_alloc (input_bfd
,
1585 sizeof (struct coff_debug_merge_type
)));
1588 mt
->class = isym
.n_sclass
;
1590 /* Pick up the aux entry, which points to the end of the tag
1592 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1593 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1596 /* Gather the elements. */
1597 epp
= &mt
->elements
;
1598 mt
->elements
= NULL
;
1600 esl
= esym
+ 2 * isymesz
;
1601 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1602 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1603 while (esl
< eslend
)
1605 const char *elename
;
1606 char elebuf
[SYMNMLEN
+ 1];
1609 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1611 *epp
= ((struct coff_debug_merge_element
*)
1612 bfd_alloc (input_bfd
,
1613 sizeof (struct coff_debug_merge_element
)));
1617 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1619 if (elename
== NULL
)
1622 name_copy
= (char *) bfd_alloc (input_bfd
,
1623 strlen (elename
) + 1);
1624 if (name_copy
== NULL
)
1626 strcpy (name_copy
, elename
);
1628 (*epp
)->name
= name_copy
;
1629 (*epp
)->type
= islp
->n_type
;
1631 if (islp
->n_numaux
>= 1
1632 && islp
->n_type
!= T_NULL
1633 && islp
->n_sclass
!= C_EOS
)
1635 union internal_auxent eleaux
;
1638 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1639 islp
->n_type
, islp
->n_sclass
, 0,
1640 islp
->n_numaux
, (PTR
) &eleaux
);
1641 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1643 /* FIXME: If this tagndx entry refers to a symbol
1644 defined later in this file, we just ignore it.
1645 Handling this correctly would be tedious, and may
1651 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1654 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1655 if ((*epp
)->tagndx
< 0)
1659 epp
= &(*epp
)->next
;
1662 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1663 islp
+= islp
->n_numaux
+ 1;
1666 /* See if we already have a definition which matches this
1667 type. We always output the type if it has no elements,
1669 if (mt
->elements
== NULL
)
1670 bfd_release (input_bfd
, (PTR
) mt
);
1673 struct coff_debug_merge_type
*mtl
;
1675 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1677 struct coff_debug_merge_element
*me
, *mel
;
1679 if (mtl
->class != mt
->class)
1682 for (me
= mt
->elements
, mel
= mtl
->elements
;
1683 me
!= NULL
&& mel
!= NULL
;
1684 me
= me
->next
, mel
= mel
->next
)
1686 if (strcmp (me
->name
, mel
->name
) != 0
1687 || me
->type
!= mel
->type
1688 || me
->tagndx
!= mel
->tagndx
)
1692 if (me
== NULL
&& mel
== NULL
)
1696 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1698 /* This is the first definition of this type. */
1699 mt
->indx
= output_index
;
1700 mt
->next
= mh
->types
;
1705 /* This is a redefinition which can be merged. */
1706 bfd_release (input_bfd
, (PTR
) mt
);
1707 *indexp
= mtl
->indx
;
1708 add
= (eslend
- esym
) / isymesz
;
1714 /* We now know whether we are to skip this symbol or not. */
1717 /* Adjust the symbol in order to output it. */
1719 if (isym
._n
._n_n
._n_zeroes
== 0
1720 && isym
._n
._n_n
._n_offset
!= 0)
1725 /* This symbol has a long name. Enter it in the string
1726 table we are building. Note that we do not check
1727 bfd_coff_symname_in_debug. That is only true for
1728 XCOFF, and XCOFF requires different linking code
1730 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1734 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1735 if (indx
== (bfd_size_type
) -1)
1737 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1740 switch (isym
.n_sclass
)
1756 /* The symbol value should not be modified. */
1760 if (obj_pe (input_bfd
)
1761 && strcmp (isym
.n_name
, ".bf") != 0
1762 && isym
.n_scnum
> 0)
1764 /* For PE, .lf and .ef get their value left alone,
1765 while .bf gets relocated. However, they all have
1766 "real" section numbers, and need to be moved into
1768 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1773 case C_LABEL
: /* Not completely sure about these 2 */
1782 /* Compute new symbol location. */
1783 if (isym
.n_scnum
> 0)
1785 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1786 isym
.n_value
+= (*secpp
)->output_offset
;
1787 if (! obj_pe (input_bfd
))
1788 isym
.n_value
-= (*secpp
)->vma
;
1789 if (! obj_pe (finfo
->output_bfd
))
1790 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1795 /* The value of a C_FILE symbol is the symbol index of
1796 the next C_FILE symbol. The value of the last C_FILE
1797 symbol is the symbol index to the first external
1798 symbol (actually, coff_renumber_symbols does not get
1799 this right--it just sets the value of the last C_FILE
1800 symbol to zero--and nobody has ever complained about
1801 it). We try to get this right, below, just before we
1802 write the symbols out, but in the general case we may
1803 have to write the symbol out twice. */
1805 if (finfo
->last_file_index
!= -1
1806 && finfo
->last_file
.n_value
!= (long) output_index
)
1808 /* We must correct the value of the last C_FILE
1810 finfo
->last_file
.n_value
= output_index
;
1811 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1813 /* The last C_FILE symbol is in this input file. */
1814 bfd_coff_swap_sym_out (output_bfd
,
1815 (PTR
) &finfo
->last_file
,
1816 (PTR
) (finfo
->outsyms
1817 + ((finfo
->last_file_index
1823 /* We have already written out the last C_FILE
1824 symbol. We need to write it out again. We
1825 borrow *outsym temporarily. */
1826 bfd_coff_swap_sym_out (output_bfd
,
1827 (PTR
) &finfo
->last_file
,
1829 if (bfd_seek (output_bfd
,
1830 (obj_sym_filepos (output_bfd
)
1831 + finfo
->last_file_index
* osymesz
),
1833 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1839 finfo
->last_file_index
= output_index
;
1840 finfo
->last_file
= isym
;
1844 /* If doing task linking, convert normal global function symbols to
1845 static functions. */
1846 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1847 isym
.n_sclass
= C_STAT
;
1849 /* Output the symbol. */
1851 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1853 *indexp
= output_index
;
1858 struct coff_link_hash_entry
*h
;
1860 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1862 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1865 /* This can happen if there were errors earlier in
1867 bfd_set_error (bfd_error_bad_value
);
1870 h
->indx
= output_index
;
1873 output_index
+= add
;
1874 outsym
+= add
* osymesz
;
1877 esym
+= add
* isymesz
;
1881 for (--add
; add
> 0; --add
)
1888 /* Fix up the aux entries. This must be done in a separate pass,
1889 because we don't know the correct symbol indices until we have
1890 already decided which symbols we are going to keep. */
1892 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1893 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1894 isymp
= finfo
->internal_syms
;
1895 indexp
= finfo
->sym_indices
;
1896 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1897 outsym
= finfo
->outsyms
;
1898 while (esym
< esym_end
)
1902 add
= 1 + isymp
->n_numaux
;
1905 || (bfd_size_type
) *indexp
< syment_base
)
1906 && (*sym_hash
== NULL
1907 || (*sym_hash
)->auxbfd
!= input_bfd
))
1908 esym
+= add
* isymesz
;
1911 struct coff_link_hash_entry
*h
;
1919 /* The m68k-motorola-sysv assembler will sometimes
1920 generate two symbols with the same name, but only one
1921 will have aux entries. */
1922 BFD_ASSERT (isymp
->n_numaux
== 0
1923 || h
->numaux
== isymp
->n_numaux
);
1931 /* Handle the aux entries. This handling is based on
1932 coff_pointerize_aux. I don't know if it always correct. */
1933 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1935 union internal_auxent aux
;
1936 union internal_auxent
*auxp
;
1942 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1943 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1948 if (isymp
->n_sclass
== C_FILE
)
1950 /* If this is a long filename, we must put it in the
1952 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1953 && auxp
->x_file
.x_n
.x_offset
!= 0)
1955 const char *filename
;
1958 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1959 >= STRING_SIZE_SIZE
);
1960 if (strings
== NULL
)
1962 strings
= _bfd_coff_read_string_table (input_bfd
);
1963 if (strings
== NULL
)
1966 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1967 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1969 if (indx
== (bfd_size_type
) -1)
1971 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1974 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1978 if (ISFCN (isymp
->n_type
)
1979 || ISTAG (isymp
->n_sclass
)
1980 || isymp
->n_sclass
== C_BLOCK
1981 || isymp
->n_sclass
== C_FCN
)
1983 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1985 && indx
< obj_raw_syment_count (input_bfd
))
1987 /* We look forward through the symbol for
1988 the index of the next symbol we are going
1989 to include. I don't know if this is
1991 while ((finfo
->sym_indices
[indx
] < 0
1992 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1994 && indx
< obj_raw_syment_count (input_bfd
))
1996 if (indx
>= obj_raw_syment_count (input_bfd
))
1997 indx
= output_index
;
1999 indx
= finfo
->sym_indices
[indx
];
2000 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2004 indx
= auxp
->x_sym
.x_tagndx
.l
;
2005 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2009 symindx
= finfo
->sym_indices
[indx
];
2011 auxp
->x_sym
.x_tagndx
.l
= 0;
2013 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2016 /* The .bf symbols are supposed to be linked through
2017 the endndx field. We need to carry this list
2018 across object files. */
2021 && isymp
->n_sclass
== C_FCN
2022 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2023 || isymp
->_n
._n_n
._n_offset
== 0)
2024 && isymp
->_n
._n_name
[0] == '.'
2025 && isymp
->_n
._n_name
[1] == 'b'
2026 && isymp
->_n
._n_name
[2] == 'f'
2027 && isymp
->_n
._n_name
[3] == '\0')
2029 if (finfo
->last_bf_index
!= -1)
2031 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2034 if ((bfd_size_type
) finfo
->last_bf_index
2039 /* The last .bf symbol is in this input
2040 file. This will only happen if the
2041 assembler did not set up the .bf
2042 endndx symbols correctly. */
2043 auxout
= (PTR
) (finfo
->outsyms
2044 + ((finfo
->last_bf_index
2047 bfd_coff_swap_aux_out (output_bfd
,
2048 (PTR
) &finfo
->last_bf
,
2056 /* We have already written out the last
2057 .bf aux entry. We need to write it
2058 out again. We borrow *outsym
2059 temporarily. FIXME: This case should
2061 bfd_coff_swap_aux_out (output_bfd
,
2062 (PTR
) &finfo
->last_bf
,
2067 if (bfd_seek (output_bfd
,
2068 (obj_sym_filepos (output_bfd
)
2069 + finfo
->last_bf_index
* osymesz
),
2071 || bfd_write (outsym
, osymesz
, 1,
2072 output_bfd
) != osymesz
)
2077 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2078 finfo
->last_bf_index
= -1;
2081 /* The endndx field of this aux entry must
2082 be updated with the symbol number of the
2084 finfo
->last_bf
= *auxp
;
2085 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2094 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
2095 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2109 /* Relocate the line numbers, unless we are stripping them. */
2110 if (finfo
->info
->strip
== strip_none
2111 || finfo
->info
->strip
== strip_some
)
2113 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2121 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2122 build_link_order in ldwrite.c will not have created a
2123 link order, which means that we will not have seen this
2124 input section in _bfd_coff_final_link, which means that
2125 we will not have allocated space for the line numbers of
2126 this section. I don't think line numbers can be
2127 meaningful for a section which does not have
2128 SEC_HAS_CONTENTS set, but, if they do, this must be
2130 if (o
->lineno_count
== 0
2131 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2134 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2135 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2136 input_bfd
) != linesz
* o
->lineno_count
)
2139 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2140 eline
= finfo
->linenos
;
2141 oeline
= finfo
->linenos
;
2142 elineend
= eline
+ linesz
* o
->lineno_count
;
2144 for (; eline
< elineend
; eline
+= linesz
)
2146 struct internal_lineno iline
;
2148 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2150 if (iline
.l_lnno
!= 0)
2151 iline
.l_addr
.l_paddr
+= offset
;
2152 else if (iline
.l_addr
.l_symndx
>= 0
2153 && ((unsigned long) iline
.l_addr
.l_symndx
2154 < obj_raw_syment_count (input_bfd
)))
2158 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2162 /* These line numbers are attached to a symbol
2163 which we are stripping. We must discard the
2164 line numbers because reading them back with
2165 no associated symbol (or associating them all
2166 with symbol #0) will fail. We can't regain
2167 the space in the output file, but at least
2173 struct internal_syment is
;
2174 union internal_auxent ia
;
2176 /* Fix up the lnnoptr field in the aux entry of
2177 the symbol. It turns out that we can't do
2178 this when we modify the symbol aux entries,
2179 because gas sometimes screws up the lnnoptr
2180 field and makes it an offset from the start
2181 of the line numbers rather than an absolute
2183 bfd_coff_swap_sym_in (output_bfd
,
2184 (PTR
) (finfo
->outsyms
2185 + ((indx
- syment_base
)
2188 if ((ISFCN (is
.n_type
)
2189 || is
.n_sclass
== C_BLOCK
)
2190 && is
.n_numaux
>= 1)
2194 auxptr
= (PTR
) (finfo
->outsyms
2195 + ((indx
- syment_base
+ 1)
2197 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2198 is
.n_type
, is
.n_sclass
,
2199 0, is
.n_numaux
, (PTR
) &ia
);
2200 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2201 (o
->output_section
->line_filepos
2202 + o
->output_section
->lineno_count
* linesz
2203 + eline
- finfo
->linenos
);
2204 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2205 is
.n_type
, is
.n_sclass
, 0,
2206 is
.n_numaux
, auxptr
);
2212 iline
.l_addr
.l_symndx
= indx
;
2217 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
,
2223 if (bfd_seek (output_bfd
,
2224 (o
->output_section
->line_filepos
2225 + o
->output_section
->lineno_count
* linesz
),
2227 || (bfd_write (finfo
->linenos
, 1, oeline
- finfo
->linenos
,
2229 != (bfd_size_type
) (oeline
- finfo
->linenos
)))
2232 o
->output_section
->lineno_count
+=
2233 (oeline
- finfo
->linenos
) / linesz
;
2237 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2238 symbol will be the first symbol in the next input file. In the
2239 normal case, this will save us from writing out the C_FILE symbol
2241 if (finfo
->last_file_index
!= -1
2242 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2244 finfo
->last_file
.n_value
= output_index
;
2245 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2246 (PTR
) (finfo
->outsyms
2247 + ((finfo
->last_file_index
- syment_base
)
2251 /* Write the modified symbols to the output file. */
2252 if (outsym
> finfo
->outsyms
)
2254 if (bfd_seek (output_bfd
,
2255 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2257 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2259 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2262 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2263 + (outsym
- finfo
->outsyms
) / osymesz
)
2266 obj_raw_syment_count (output_bfd
) = output_index
;
2269 /* Relocate the contents of each section. */
2270 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2271 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2274 struct coff_section_tdata
*secdata
;
2276 if (! o
->linker_mark
)
2278 /* This section was omitted from the link. */
2282 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2283 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2285 if ((o
->flags
& SEC_RELOC
) != 0
2286 && o
->reloc_count
!= 0)
2288 ((*_bfd_error_handler
)
2289 (_("%s: relocs in section `%s', but it has no contents"),
2290 bfd_get_filename (input_bfd
),
2291 bfd_get_section_name (input_bfd
, o
)));
2292 bfd_set_error (bfd_error_no_contents
);
2299 secdata
= coff_section_data (input_bfd
, o
);
2300 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2301 contents
= secdata
->contents
;
2304 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2305 (file_ptr
) 0, o
->_raw_size
))
2307 contents
= finfo
->contents
;
2310 if ((o
->flags
& SEC_RELOC
) != 0)
2313 struct internal_reloc
*internal_relocs
;
2314 struct internal_reloc
*irel
;
2316 /* Read in the relocs. */
2317 target_index
= o
->output_section
->target_index
;
2318 internal_relocs
= (_bfd_coff_read_internal_relocs
2319 (input_bfd
, o
, false, finfo
->external_relocs
,
2320 finfo
->info
->relocateable
,
2321 (finfo
->info
->relocateable
2322 ? (finfo
->section_info
[target_index
].relocs
2323 + o
->output_section
->reloc_count
)
2324 : finfo
->internal_relocs
)));
2325 if (internal_relocs
== NULL
)
2328 /* Call processor specific code to relocate the section
2330 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2334 finfo
->internal_syms
,
2338 if (finfo
->info
->relocateable
)
2341 struct internal_reloc
*irelend
;
2342 struct coff_link_hash_entry
**rel_hash
;
2344 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2345 irel
= internal_relocs
;
2346 irelend
= irel
+ o
->reloc_count
;
2347 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2348 + o
->output_section
->reloc_count
);
2349 for (; irel
< irelend
; irel
++, rel_hash
++)
2351 struct coff_link_hash_entry
*h
;
2356 /* Adjust the reloc address and symbol index. */
2358 irel
->r_vaddr
+= offset
;
2360 if (irel
->r_symndx
== -1)
2365 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2373 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2376 /* This is a global symbol. */
2378 irel
->r_symndx
= h
->indx
;
2381 /* This symbol is being written at the end
2382 of the file, and we do not yet know the
2383 symbol index. We save the pointer to the
2384 hash table entry in the rel_hash list.
2385 We set the indx field to -2 to indicate
2386 that this symbol must not be stripped. */
2395 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2397 irel
->r_symndx
= indx
;
2400 struct internal_syment
*is
;
2402 char buf
[SYMNMLEN
+ 1];
2404 /* This reloc is against a symbol we are
2405 stripping. This should have been handled
2406 by the 'dont_skip_symbol' code in the while
2407 loop at the top of this function. */
2409 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2411 name
= (_bfd_coff_internal_syment_name
2412 (input_bfd
, is
, buf
));
2416 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2417 (finfo
->info
, name
, input_bfd
, o
,
2424 o
->output_section
->reloc_count
+= o
->reloc_count
;
2428 /* Write out the modified section contents. */
2429 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2431 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2435 bfd_octets_per_byte (output_bfd
)),
2436 (o
->_cooked_size
!= 0
2443 if (! (_bfd_write_section_stabs
2444 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2445 o
, &secdata
->stab_info
, contents
)))
2450 if (! finfo
->info
->keep_memory
)
2452 if (! _bfd_coff_free_symbols (input_bfd
))
2459 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2462 _bfd_coff_write_global_sym (h
, data
)
2463 struct coff_link_hash_entry
*h
;
2466 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2468 struct internal_syment isym
;
2469 bfd_size_type symesz
;
2472 output_bfd
= finfo
->output_bfd
;
2478 && (finfo
->info
->strip
== strip_all
2479 || (finfo
->info
->strip
== strip_some
2480 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2481 h
->root
.root
.string
, false, false)
2485 switch (h
->root
.type
)
2488 case bfd_link_hash_new
:
2492 case bfd_link_hash_undefined
:
2493 case bfd_link_hash_undefweak
:
2494 isym
.n_scnum
= N_UNDEF
;
2498 case bfd_link_hash_defined
:
2499 case bfd_link_hash_defweak
:
2503 sec
= h
->root
.u
.def
.section
->output_section
;
2504 if (bfd_is_abs_section (sec
))
2505 isym
.n_scnum
= N_ABS
;
2507 isym
.n_scnum
= sec
->target_index
;
2508 isym
.n_value
= (h
->root
.u
.def
.value
2509 + h
->root
.u
.def
.section
->output_offset
);
2510 if (! obj_pe (finfo
->output_bfd
))
2511 isym
.n_value
+= sec
->vma
;
2515 case bfd_link_hash_common
:
2516 isym
.n_scnum
= N_UNDEF
;
2517 isym
.n_value
= h
->root
.u
.c
.size
;
2520 case bfd_link_hash_indirect
:
2521 case bfd_link_hash_warning
:
2522 /* Just ignore these. They can't be handled anyhow. */
2526 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2527 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2534 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2536 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2538 if (indx
== (bfd_size_type
) -1)
2540 finfo
->failed
= true;
2543 isym
._n
._n_n
._n_zeroes
= 0;
2544 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2547 isym
.n_sclass
= h
->class;
2548 isym
.n_type
= h
->type
;
2550 if (isym
.n_sclass
== C_NULL
)
2551 isym
.n_sclass
= C_EXT
;
2553 /* If doing task linking and this is the pass where we convert
2554 defined globals to statics, then do that conversion now. If the
2555 symbol is not being converted, just ignore it and it will be
2556 output during a later pass. */
2557 if (finfo
->global_to_static
)
2559 if (! IS_EXTERNAL (output_bfd
, isym
))
2562 isym
.n_sclass
= C_STAT
;
2565 /* When a weak symbol is not overriden by a strong one,
2566 turn it into an external symbol when not building a
2567 shared or relocateable object. */
2568 if (! finfo
->info
->shared
2569 && ! finfo
->info
->relocateable
2570 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2571 isym
.n_sclass
= C_EXT
;
2573 isym
.n_numaux
= h
->numaux
;
2575 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2577 symesz
= bfd_coff_symesz (output_bfd
);
2579 if (bfd_seek (output_bfd
,
2580 (obj_sym_filepos (output_bfd
)
2581 + obj_raw_syment_count (output_bfd
) * symesz
),
2583 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2585 finfo
->failed
= true;
2589 h
->indx
= obj_raw_syment_count (output_bfd
);
2591 ++obj_raw_syment_count (output_bfd
);
2593 /* Write out any associated aux entries. Most of the aux entries
2594 will have been modified in _bfd_coff_link_input_bfd. We have to
2595 handle section aux entries here, now that we have the final
2596 relocation and line number counts. */
2597 for (i
= 0; i
< isym
.n_numaux
; i
++)
2599 union internal_auxent
*auxp
;
2603 /* Look for a section aux entry here using the same tests that
2604 coff_swap_aux_out uses. */
2606 && (isym
.n_sclass
== C_STAT
2607 || isym
.n_sclass
== C_HIDDEN
)
2608 && isym
.n_type
== T_NULL
2609 && (h
->root
.type
== bfd_link_hash_defined
2610 || h
->root
.type
== bfd_link_hash_defweak
))
2614 sec
= h
->root
.u
.def
.section
->output_section
;
2617 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2621 /* For PE, an overflow on the final link reportedly does
2622 not matter. FIXME: Why not? */
2624 if (sec
->reloc_count
> 0xffff
2625 && (! obj_pe (output_bfd
)
2626 || finfo
->info
->relocateable
))
2627 (*_bfd_error_handler
)
2628 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2629 bfd_get_filename (output_bfd
),
2630 bfd_get_section_name (output_bfd
, sec
),
2633 if (sec
->lineno_count
> 0xffff
2634 && (! obj_pe (output_bfd
)
2635 || finfo
->info
->relocateable
))
2636 (*_bfd_error_handler
)
2637 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2638 bfd_get_filename (output_bfd
),
2639 bfd_get_section_name (output_bfd
, sec
),
2642 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2643 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2644 auxp
->x_scn
.x_checksum
= 0;
2645 auxp
->x_scn
.x_associated
= 0;
2646 auxp
->x_scn
.x_comdat
= 0;
2650 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isym
.n_type
,
2651 isym
.n_sclass
, i
, isym
.n_numaux
,
2652 (PTR
) finfo
->outsyms
);
2653 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2655 finfo
->failed
= true;
2658 ++obj_raw_syment_count (output_bfd
);
2664 /* Write out task global symbols, converting them to statics. Called
2665 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2666 the dirty work, if the symbol we are processing needs conversion. */
2669 _bfd_coff_write_task_globals (h
, data
)
2670 struct coff_link_hash_entry
*h
;
2673 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2674 boolean rtnval
= true;
2675 boolean save_global_to_static
;
2679 switch (h
->root
.type
)
2681 case bfd_link_hash_defined
:
2682 case bfd_link_hash_defweak
:
2683 save_global_to_static
= finfo
->global_to_static
;
2684 finfo
->global_to_static
= true;
2685 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2686 finfo
->global_to_static
= save_global_to_static
;
2695 /* Handle a link order which is supposed to generate a reloc. */
2698 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2700 struct coff_final_link_info
*finfo
;
2701 asection
*output_section
;
2702 struct bfd_link_order
*link_order
;
2704 reloc_howto_type
*howto
;
2705 struct internal_reloc
*irel
;
2706 struct coff_link_hash_entry
**rel_hash_ptr
;
2708 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2711 bfd_set_error (bfd_error_bad_value
);
2715 if (link_order
->u
.reloc
.p
->addend
!= 0)
2719 bfd_reloc_status_type rstat
;
2722 size
= bfd_get_reloc_size (howto
);
2723 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2727 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2728 link_order
->u
.reloc
.p
->addend
, buf
);
2734 case bfd_reloc_outofrange
:
2736 case bfd_reloc_overflow
:
2737 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2739 (link_order
->type
== bfd_section_reloc_link_order
2740 ? bfd_section_name (output_bfd
,
2741 link_order
->u
.reloc
.p
->u
.section
)
2742 : link_order
->u
.reloc
.p
->u
.name
),
2743 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2744 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2751 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2753 (link_order
->offset
*
2754 bfd_octets_per_byte (output_bfd
)), size
);
2760 /* Store the reloc information in the right place. It will get
2761 swapped and written out at the end of the final_link routine. */
2763 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2764 + output_section
->reloc_count
);
2765 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2766 + output_section
->reloc_count
);
2768 memset (irel
, 0, sizeof (struct internal_reloc
));
2769 *rel_hash_ptr
= NULL
;
2771 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2773 if (link_order
->type
== bfd_section_reloc_link_order
)
2775 /* We need to somehow locate a symbol in the right section. The
2776 symbol must either have a value of zero, or we must adjust
2777 the addend by the value of the symbol. FIXME: Write this
2778 when we need it. The old linker couldn't handle this anyhow. */
2780 *rel_hash_ptr
= NULL
;
2785 struct coff_link_hash_entry
*h
;
2787 h
= ((struct coff_link_hash_entry
*)
2788 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2789 link_order
->u
.reloc
.p
->u
.name
,
2790 false, false, true));
2794 irel
->r_symndx
= h
->indx
;
2797 /* Set the index to -2 to force this symbol to get
2806 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2807 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2808 (asection
*) NULL
, (bfd_vma
) 0)))
2814 /* FIXME: Is this always right? */
2815 irel
->r_type
= howto
->type
;
2817 /* r_size is only used on the RS/6000, which needs its own linker
2818 routines anyhow. r_extern is only used for ECOFF. */
2820 /* FIXME: What is the right value for r_offset? Is zero OK? */
2822 ++output_section
->reloc_count
;
2827 /* A basic reloc handling routine which may be used by processors with
2831 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2832 input_section
, contents
, relocs
, syms
,
2835 struct bfd_link_info
*info
;
2837 asection
*input_section
;
2839 struct internal_reloc
*relocs
;
2840 struct internal_syment
*syms
;
2841 asection
**sections
;
2843 struct internal_reloc
*rel
;
2844 struct internal_reloc
*relend
;
2847 relend
= rel
+ input_section
->reloc_count
;
2848 for (; rel
< relend
; rel
++)
2851 struct coff_link_hash_entry
*h
;
2852 struct internal_syment
*sym
;
2855 reloc_howto_type
*howto
;
2856 bfd_reloc_status_type rstat
;
2858 symndx
= rel
->r_symndx
;
2866 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2868 (*_bfd_error_handler
)
2869 ("%s: illegal symbol index %ld in relocs",
2870 bfd_get_filename (input_bfd
), symndx
);
2875 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2876 sym
= syms
+ symndx
;
2879 /* COFF treats common symbols in one of two ways. Either the
2880 size of the symbol is included in the section contents, or it
2881 is not. We assume that the size is not included, and force
2882 the rtype_to_howto function to adjust the addend as needed. */
2884 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2885 addend
= - sym
->n_value
;
2889 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2894 /* If we are doing a relocateable link, then we can just ignore
2895 a PC relative reloc that is pcrel_offset. It will already
2896 have the correct value. If this is not a relocateable link,
2897 then we should ignore the symbol value. */
2898 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2900 if (info
->relocateable
)
2902 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2903 addend
+= sym
->n_value
;
2914 sec
= bfd_abs_section_ptr
;
2919 sec
= sections
[symndx
];
2920 val
= (sec
->output_section
->vma
2921 + sec
->output_offset
2923 if (! obj_pe (input_bfd
))
2929 if (h
->root
.type
== bfd_link_hash_defined
2930 || h
->root
.type
== bfd_link_hash_defweak
)
2934 sec
= h
->root
.u
.def
.section
;
2935 val
= (h
->root
.u
.def
.value
2936 + sec
->output_section
->vma
2937 + sec
->output_offset
);
2940 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2943 else if (! info
->relocateable
)
2945 if (! ((*info
->callbacks
->undefined_symbol
)
2946 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2947 rel
->r_vaddr
- input_section
->vma
, true)))
2952 if (info
->base_file
)
2954 /* Emit a reloc if the backend thinks it needs it. */
2955 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2957 /* Relocation to a symbol in a section which isn't
2958 absolute. We output the address here to a file.
2959 This file is then read by dlltool when generating the
2960 reloc section. Note that the base file is not
2961 portable between systems. We write out a long here,
2962 and dlltool reads in a long. */
2963 long addr
= (rel
->r_vaddr
2964 - input_section
->vma
2965 + input_section
->output_offset
2966 + input_section
->output_section
->vma
);
2967 if (coff_data (output_bfd
)->pe
)
2968 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2969 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2972 bfd_set_error (bfd_error_system_call
);
2978 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2980 rel
->r_vaddr
- input_section
->vma
,
2989 case bfd_reloc_outofrange
:
2990 (*_bfd_error_handler
)
2991 (_("%s: bad reloc address 0x%lx in section `%s'"),
2992 bfd_get_filename (input_bfd
),
2993 (unsigned long) rel
->r_vaddr
,
2994 bfd_get_section_name (input_bfd
, input_section
));
2996 case bfd_reloc_overflow
:
2999 char buf
[SYMNMLEN
+ 1];
3004 name
= h
->root
.root
.string
;
3007 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3012 if (! ((*info
->callbacks
->reloc_overflow
)
3013 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
3014 input_section
, rel
->r_vaddr
- input_section
->vma
)))