1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 boolean coff_link_add_object_symbols
32 PARAMS ((bfd
*, struct bfd_link_info
*));
33 static boolean coff_link_check_archive_element
34 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
35 static boolean coff_link_check_ar_symbols
36 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
37 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
38 static char *dores_com
PARAMS ((char *, bfd
*, int));
39 static char *get_name
PARAMS ((char *, char **));
40 static int process_embedded_commands
41 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*));
42 static void mark_relocs
PARAMS ((struct coff_final_link_info
*, bfd
*));
44 /* Return true if SYM is a weak, external symbol. */
45 #define IS_WEAK_EXTERNAL(abfd, sym) \
46 ((sym).n_sclass == C_WEAKEXT \
47 || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
49 /* Return true if SYM is an external symbol. */
50 #define IS_EXTERNAL(abfd, sym) \
51 ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
53 /* Define macros so that the ISFCN, et. al., macros work correctly.
54 These macros are defined in include/coff/internal.h in terms of
55 N_TMASK, etc. These definitions require a user to define local
56 variables with the appropriate names, and with values from the
57 coff_data (abfd) structure. */
59 #define N_TMASK n_tmask
60 #define N_BTSHFT n_btshft
61 #define N_BTMASK n_btmask
63 /* Create an entry in a COFF linker hash table. */
65 struct bfd_hash_entry
*
66 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
67 struct bfd_hash_entry
*entry
;
68 struct bfd_hash_table
*table
;
71 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
73 /* Allocate the structure if it has not already been allocated by a
75 if (ret
== (struct coff_link_hash_entry
*) NULL
)
76 ret
= ((struct coff_link_hash_entry
*)
77 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
78 if (ret
== (struct coff_link_hash_entry
*) NULL
)
79 return (struct bfd_hash_entry
*) ret
;
81 /* Call the allocation method of the superclass. */
82 ret
= ((struct coff_link_hash_entry
*)
83 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
85 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
87 /* Set local fields. */
96 return (struct bfd_hash_entry
*) ret
;
99 /* Initialize a COFF linker hash table. */
102 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
103 struct coff_link_hash_table
*table
;
105 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
106 struct bfd_hash_table
*,
109 table
->stab_info
= NULL
;
110 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
113 /* Create a COFF linker hash table. */
115 struct bfd_link_hash_table
*
116 _bfd_coff_link_hash_table_create (abfd
)
119 struct coff_link_hash_table
*ret
;
120 bfd_size_type amt
= sizeof (struct coff_link_hash_table
);
122 ret
= (struct coff_link_hash_table
*) bfd_malloc (amt
);
125 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
126 _bfd_coff_link_hash_newfunc
))
129 return (struct bfd_link_hash_table
*) NULL
;
134 /* Create an entry in a COFF debug merge hash table. */
136 struct bfd_hash_entry
*
137 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
138 struct bfd_hash_entry
*entry
;
139 struct bfd_hash_table
*table
;
142 struct coff_debug_merge_hash_entry
*ret
=
143 (struct coff_debug_merge_hash_entry
*) entry
;
145 /* Allocate the structure if it has not already been allocated by a
147 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
148 ret
= ((struct coff_debug_merge_hash_entry
*)
149 bfd_hash_allocate (table
,
150 sizeof (struct coff_debug_merge_hash_entry
)));
151 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
152 return (struct bfd_hash_entry
*) ret
;
154 /* Call the allocation method of the superclass. */
155 ret
= ((struct coff_debug_merge_hash_entry
*)
156 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
157 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
159 /* Set local fields. */
163 return (struct bfd_hash_entry
*) ret
;
166 /* Given a COFF BFD, add symbols to the global hash table as
170 _bfd_coff_link_add_symbols (abfd
, info
)
172 struct bfd_link_info
*info
;
174 switch (bfd_get_format (abfd
))
177 return coff_link_add_object_symbols (abfd
, info
);
179 return (_bfd_generic_link_add_archive_symbols
180 (abfd
, info
, coff_link_check_archive_element
));
182 bfd_set_error (bfd_error_wrong_format
);
187 /* Add symbols from a COFF object file. */
190 coff_link_add_object_symbols (abfd
, info
)
192 struct bfd_link_info
*info
;
194 if (! _bfd_coff_get_external_symbols (abfd
))
196 if (! coff_link_add_symbols (abfd
, info
))
199 if (! info
->keep_memory
)
201 if (! _bfd_coff_free_symbols (abfd
))
207 /* Check a single archive element to see if we need to include it in
208 the link. *PNEEDED is set according to whether this element is
209 needed in the link or not. This is called via
210 _bfd_generic_link_add_archive_symbols. */
213 coff_link_check_archive_element (abfd
, info
, pneeded
)
215 struct bfd_link_info
*info
;
218 if (! _bfd_coff_get_external_symbols (abfd
))
221 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
226 if (! coff_link_add_symbols (abfd
, info
))
230 if (! info
->keep_memory
|| ! *pneeded
)
232 if (! _bfd_coff_free_symbols (abfd
))
239 /* Look through the symbols to see if this object file should be
240 included in the link. */
243 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
245 struct bfd_link_info
*info
;
248 bfd_size_type symesz
;
254 symesz
= bfd_coff_symesz (abfd
);
255 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
256 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
257 while (esym
< esym_end
)
259 struct internal_syment sym
;
260 enum coff_symbol_classification classification
;
262 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
264 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
265 if (classification
== COFF_SYMBOL_GLOBAL
266 || classification
== COFF_SYMBOL_COMMON
)
269 char buf
[SYMNMLEN
+ 1];
270 struct bfd_link_hash_entry
*h
;
272 /* This symbol is externally visible, and is defined by this
275 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
278 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
281 if (!h
&& info
->pei386_auto_import
)
283 if (!strncmp (name
,"__imp_", 6))
286 bfd_link_hash_lookup (info
->hash
, name
+ 6, false, false,
290 /* We are only interested in symbols that are currently
291 undefined. If a symbol is currently known to be common,
292 COFF linkers do not bring in an object file which defines
294 if (h
!= (struct bfd_link_hash_entry
*) NULL
295 && h
->type
== bfd_link_hash_undefined
)
297 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
304 esym
+= (sym
.n_numaux
+ 1) * symesz
;
307 /* We do not need this object file. */
311 /* Add all the symbols from an object file to the hash table. */
314 coff_link_add_symbols (abfd
, info
)
316 struct bfd_link_info
*info
;
318 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
319 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
320 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
322 boolean default_copy
;
323 bfd_size_type symcount
;
324 struct coff_link_hash_entry
**sym_hash
;
325 bfd_size_type symesz
;
330 /* Keep the symbols during this function, in case the linker needs
331 to read the generic symbols in order to report an error message. */
332 keep_syms
= obj_coff_keep_syms (abfd
);
333 obj_coff_keep_syms (abfd
) = true;
335 if (info
->keep_memory
)
336 default_copy
= false;
340 symcount
= obj_raw_syment_count (abfd
);
342 /* We keep a list of the linker hash table entries that correspond
343 to particular symbols. */
344 amt
= symcount
* sizeof (struct coff_link_hash_entry
*);
345 sym_hash
= (struct coff_link_hash_entry
**) bfd_zalloc (abfd
, amt
);
346 if (sym_hash
== NULL
&& symcount
!= 0)
348 obj_coff_sym_hashes (abfd
) = sym_hash
;
350 symesz
= bfd_coff_symesz (abfd
);
351 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
352 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
353 esym_end
= esym
+ symcount
* symesz
;
354 while (esym
< esym_end
)
356 struct internal_syment sym
;
357 enum coff_symbol_classification classification
;
360 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
362 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
363 if (classification
!= COFF_SYMBOL_LOCAL
)
366 char buf
[SYMNMLEN
+ 1];
372 /* This symbol is externally visible. */
374 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
378 /* We must copy the name into memory if we got it from the
379 syment itself, rather than the string table. */
381 if (sym
._n
._n_n
._n_zeroes
!= 0
382 || sym
._n
._n_n
._n_offset
== 0)
387 switch (classification
)
392 case COFF_SYMBOL_GLOBAL
:
393 flags
= BSF_EXPORT
| BSF_GLOBAL
;
394 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
396 value
-= section
->vma
;
399 case COFF_SYMBOL_UNDEFINED
:
401 section
= bfd_und_section_ptr
;
404 case COFF_SYMBOL_COMMON
:
406 section
= bfd_com_section_ptr
;
409 case COFF_SYMBOL_PE_SECTION
:
410 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
411 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
415 if (IS_WEAK_EXTERNAL (abfd
, sym
))
420 /* In the PE format, section symbols actually refer to the
421 start of the output section. We handle them specially
423 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
425 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
426 name
, false, copy
, false);
427 if (*sym_hash
!= NULL
)
429 if (((*sym_hash
)->coff_link_hash_flags
430 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
431 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
432 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
433 (*_bfd_error_handler
)
434 ("Warning: symbol `%s' is both section and non-section",
441 /* The Microsoft Visual C compiler does string pooling by
442 hashing the constants to an internal symbol name, and
443 relying on the linker comdat support to discard
444 duplicate names. However, if one string is a literal and
445 one is a data initializer, one will end up in the .data
446 section and one will end up in the .rdata section. The
447 Microsoft linker will combine them into the .data
448 section, which seems to be wrong since it might cause the
451 As long as there are no external references to the
452 symbols, which there shouldn't be, we can treat the .data
453 and .rdata instances as separate symbols. The comdat
454 code in the linker will do the appropriate merging. Here
455 we avoid getting a multiple definition error for one of
456 these special symbols.
458 FIXME: I don't think this will work in the case where
459 there are two object files which use the constants as a
460 literal and two object files which use it as a data
461 initializer. One or the other of the second object files
462 is going to wind up with an inappropriate reference. */
464 && (classification
== COFF_SYMBOL_GLOBAL
465 || classification
== COFF_SYMBOL_PE_SECTION
)
466 && section
->comdat
!= NULL
467 && strncmp (name
, "??_", 3) == 0
468 && strcmp (name
, section
->comdat
->name
) == 0)
470 if (*sym_hash
== NULL
)
471 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
472 name
, false, copy
, false);
473 if (*sym_hash
!= NULL
474 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
475 && (*sym_hash
)->root
.u
.def
.section
->comdat
!= NULL
476 && strcmp ((*sym_hash
)->root
.u
.def
.section
->comdat
->name
,
477 section
->comdat
->name
) == 0)
483 if (! (bfd_coff_link_add_one_symbol
484 (info
, abfd
, name
, flags
, section
, value
,
485 (const char *) NULL
, copy
, false,
486 (struct bfd_link_hash_entry
**) sym_hash
)))
490 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
491 (*sym_hash
)->coff_link_hash_flags
|=
492 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
494 /* Limit the alignment of a common symbol to the possible
495 alignment of a section. There is no point to permitting
496 a higher alignment for a common symbol: we can not
497 guarantee it, and it may cause us to allocate extra space
498 in the common section. */
499 if (section
== bfd_com_section_ptr
500 && (*sym_hash
)->root
.type
== bfd_link_hash_common
501 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
502 > bfd_coff_default_section_alignment_power (abfd
)))
503 (*sym_hash
)->root
.u
.c
.p
->alignment_power
504 = bfd_coff_default_section_alignment_power (abfd
);
506 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
508 /* If we don't have any symbol information currently in
509 the hash table, or if we are looking at a symbol
510 definition, then update the symbol class and type in
512 if (((*sym_hash
)->class == C_NULL
513 && (*sym_hash
)->type
== T_NULL
)
516 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
517 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
519 (*sym_hash
)->class = sym
.n_sclass
;
520 if (sym
.n_type
!= T_NULL
)
522 /* We want to warn if the type changed, but not
523 if it changed from an unspecified type.
524 Testing the whole type byte may work, but the
525 change from (e.g.) a function of unspecified
526 type to function of known type also wants to
528 if ((*sym_hash
)->type
!= T_NULL
529 && (*sym_hash
)->type
!= sym
.n_type
530 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
531 && (BTYPE ((*sym_hash
)->type
) == T_NULL
532 || BTYPE (sym
.n_type
) == T_NULL
)))
533 (*_bfd_error_handler
)
534 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
535 name
, (*sym_hash
)->type
, sym
.n_type
,
536 bfd_archive_filename (abfd
));
538 /* We don't want to change from a meaningful
539 base type to a null one, but if we know
540 nothing, take what little we might now know. */
541 if (BTYPE (sym
.n_type
) != T_NULL
542 || (*sym_hash
)->type
== T_NULL
)
543 (*sym_hash
)->type
= sym
.n_type
;
545 (*sym_hash
)->auxbfd
= abfd
;
546 if (sym
.n_numaux
!= 0)
548 union internal_auxent
*alloc
;
551 union internal_auxent
*iaux
;
553 (*sym_hash
)->numaux
= sym
.n_numaux
;
554 alloc
= ((union internal_auxent
*)
555 bfd_hash_allocate (&info
->hash
->table
,
557 * sizeof (*alloc
))));
560 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
562 i
++, eaux
+= symesz
, iaux
++)
563 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
564 sym
.n_sclass
, (int) i
,
565 sym
.n_numaux
, (PTR
) iaux
);
566 (*sym_hash
)->aux
= alloc
;
571 if (classification
== COFF_SYMBOL_PE_SECTION
572 && (*sym_hash
)->numaux
!= 0)
574 /* Some PE sections (such as .bss) have a zero size in
575 the section header, but a non-zero size in the AUX
576 record. Correct that here.
578 FIXME: This is not at all the right place to do this.
579 For example, it won't help objdump. This needs to be
580 done when we swap in the section header. */
582 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
583 if (section
->_raw_size
== 0)
584 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
586 /* FIXME: We could test whether the section sizes
587 matches the size in the aux entry, but apparently
588 that sometimes fails unexpectedly. */
592 esym
+= (sym
.n_numaux
+ 1) * symesz
;
593 sym_hash
+= sym
.n_numaux
+ 1;
596 /* If this is a non-traditional, non-relocateable link, try to
597 optimize the handling of any .stab/.stabstr sections. */
598 if (! info
->relocateable
599 && ! info
->traditional_format
600 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
601 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
603 asection
*stab
, *stabstr
;
605 stab
= bfd_get_section_by_name (abfd
, ".stab");
608 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
612 struct coff_link_hash_table
*table
;
613 struct coff_section_tdata
*secdata
;
615 secdata
= coff_section_data (abfd
, stab
);
618 amt
= sizeof (struct coff_section_tdata
);
619 stab
->used_by_bfd
= (PTR
) bfd_zalloc (abfd
, amt
);
620 if (stab
->used_by_bfd
== NULL
)
622 secdata
= coff_section_data (abfd
, stab
);
625 table
= coff_hash_table (info
);
627 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
629 &secdata
->stab_info
))
635 obj_coff_keep_syms (abfd
) = keep_syms
;
640 obj_coff_keep_syms (abfd
) = keep_syms
;
644 /* Do the final link step. */
647 _bfd_coff_final_link (abfd
, info
)
649 struct bfd_link_info
*info
;
651 bfd_size_type symesz
;
652 struct coff_final_link_info finfo
;
653 boolean debug_merge_allocated
;
654 boolean long_section_names
;
656 struct bfd_link_order
*p
;
657 bfd_size_type max_sym_count
;
658 bfd_size_type max_lineno_count
;
659 bfd_size_type max_reloc_count
;
660 bfd_size_type max_output_reloc_count
;
661 bfd_size_type max_contents_size
;
662 file_ptr rel_filepos
;
664 file_ptr line_filepos
;
667 bfd_byte
*external_relocs
= NULL
;
668 char strbuf
[STRING_SIZE_SIZE
];
671 symesz
= bfd_coff_symesz (abfd
);
674 finfo
.output_bfd
= abfd
;
676 finfo
.section_info
= NULL
;
677 finfo
.last_file_index
= -1;
678 finfo
.last_bf_index
= -1;
679 finfo
.internal_syms
= NULL
;
680 finfo
.sec_ptrs
= NULL
;
681 finfo
.sym_indices
= NULL
;
682 finfo
.outsyms
= NULL
;
683 finfo
.linenos
= NULL
;
684 finfo
.contents
= NULL
;
685 finfo
.external_relocs
= NULL
;
686 finfo
.internal_relocs
= NULL
;
687 finfo
.global_to_static
= false;
688 debug_merge_allocated
= false;
690 coff_data (abfd
)->link_info
= info
;
692 finfo
.strtab
= _bfd_stringtab_init ();
693 if (finfo
.strtab
== NULL
)
696 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
698 debug_merge_allocated
= true;
700 /* Compute the file positions for all the sections. */
701 if (! abfd
->output_has_begun
)
703 if (! bfd_coff_compute_section_file_positions (abfd
))
707 /* Count the line numbers and relocation entries required for the
708 output file. Set the file positions for the relocs. */
709 rel_filepos
= obj_relocbase (abfd
);
710 relsz
= bfd_coff_relsz (abfd
);
711 max_contents_size
= 0;
712 max_lineno_count
= 0;
715 long_section_names
= false;
716 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
720 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
722 if (p
->type
== bfd_indirect_link_order
)
726 sec
= p
->u
.indirect
.section
;
728 /* Mark all sections which are to be included in the
729 link. This will normally be every section. We need
730 to do this so that we can identify any sections which
731 the linker has decided to not include. */
732 sec
->linker_mark
= true;
734 if (info
->strip
== strip_none
735 || info
->strip
== strip_some
)
736 o
->lineno_count
+= sec
->lineno_count
;
738 if (info
->relocateable
)
739 o
->reloc_count
+= sec
->reloc_count
;
741 if (sec
->_raw_size
> max_contents_size
)
742 max_contents_size
= sec
->_raw_size
;
743 if (sec
->lineno_count
> max_lineno_count
)
744 max_lineno_count
= sec
->lineno_count
;
745 if (sec
->reloc_count
> max_reloc_count
)
746 max_reloc_count
= sec
->reloc_count
;
748 else if (info
->relocateable
749 && (p
->type
== bfd_section_reloc_link_order
750 || p
->type
== bfd_symbol_reloc_link_order
))
753 if (o
->reloc_count
== 0)
757 o
->flags
|= SEC_RELOC
;
758 o
->rel_filepos
= rel_filepos
;
759 rel_filepos
+= o
->reloc_count
* relsz
;
762 if (bfd_coff_long_section_names (abfd
)
763 && strlen (o
->name
) > SCNNMLEN
)
765 /* This section has a long name which must go in the string
766 table. This must correspond to the code in
767 coff_write_object_contents which puts the string index
768 into the s_name field of the section header. That is why
769 we pass hash as false. */
770 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
771 == (bfd_size_type
) -1)
773 long_section_names
= true;
777 /* If doing a relocateable link, allocate space for the pointers we
779 if (info
->relocateable
)
783 /* We use section_count + 1, rather than section_count, because
784 the target_index fields are 1 based. */
785 amt
= abfd
->section_count
+ 1;
786 amt
*= sizeof (struct coff_link_section_info
);
787 finfo
.section_info
= (struct coff_link_section_info
*) bfd_malloc (amt
);
788 if (finfo
.section_info
== NULL
)
790 for (i
= 0; i
<= abfd
->section_count
; i
++)
792 finfo
.section_info
[i
].relocs
= NULL
;
793 finfo
.section_info
[i
].rel_hashes
= NULL
;
797 /* We now know the size of the relocs, so we can determine the file
798 positions of the line numbers. */
799 line_filepos
= rel_filepos
;
800 linesz
= bfd_coff_linesz (abfd
);
801 max_output_reloc_count
= 0;
802 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
804 if (o
->lineno_count
== 0)
808 o
->line_filepos
= line_filepos
;
809 line_filepos
+= o
->lineno_count
* linesz
;
812 if (o
->reloc_count
!= 0)
814 /* We don't know the indices of global symbols until we have
815 written out all the local symbols. For each section in
816 the output file, we keep an array of pointers to hash
817 table entries. Each entry in the array corresponds to a
818 reloc. When we find a reloc against a global symbol, we
819 set the corresponding entry in this array so that we can
820 fix up the symbol index after we have written out all the
823 Because of this problem, we also keep the relocs in
824 memory until the end of the link. This wastes memory,
825 but only when doing a relocateable link, which is not the
827 BFD_ASSERT (info
->relocateable
);
828 amt
= o
->reloc_count
;
829 amt
*= sizeof (struct internal_reloc
);
830 finfo
.section_info
[o
->target_index
].relocs
=
831 (struct internal_reloc
*) bfd_malloc (amt
);
832 amt
= o
->reloc_count
;
833 amt
*= sizeof (struct coff_link_hash_entry
*);
834 finfo
.section_info
[o
->target_index
].rel_hashes
=
835 (struct coff_link_hash_entry
**) bfd_malloc (amt
);
836 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
837 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
840 if (o
->reloc_count
> max_output_reloc_count
)
841 max_output_reloc_count
= o
->reloc_count
;
844 /* Reset the reloc and lineno counts, so that we can use them to
845 count the number of entries we have output so far. */
850 obj_sym_filepos (abfd
) = line_filepos
;
852 /* Figure out the largest number of symbols in an input BFD. Take
853 the opportunity to clear the output_has_begun fields of all the
856 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
860 sub
->output_has_begun
= false;
861 sz
= obj_raw_syment_count (sub
);
862 if (sz
> max_sym_count
)
866 /* Allocate some buffers used while linking. */
867 amt
= max_sym_count
* sizeof (struct internal_syment
);
868 finfo
.internal_syms
= (struct internal_syment
*) bfd_malloc (amt
);
869 amt
= max_sym_count
* sizeof (asection
*);
870 finfo
.sec_ptrs
= (asection
**) bfd_malloc (amt
);
871 amt
= max_sym_count
* sizeof (long);
872 finfo
.sym_indices
= (long *) bfd_malloc (amt
);
873 finfo
.outsyms
= (bfd_byte
*) bfd_malloc ((max_sym_count
+ 1) * symesz
);
874 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
875 finfo
.linenos
= (bfd_byte
*) bfd_malloc (amt
);
876 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
877 amt
= max_reloc_count
* relsz
;
878 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
879 if (! info
->relocateable
)
881 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
882 finfo
.internal_relocs
= (struct internal_reloc
*) bfd_malloc (amt
);
884 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
885 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
886 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
887 || finfo
.outsyms
== NULL
888 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
889 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
890 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
891 || (! info
->relocateable
892 && finfo
.internal_relocs
== NULL
893 && max_reloc_count
> 0))
896 /* We now know the position of everything in the file, except that
897 we don't know the size of the symbol table and therefore we don't
898 know where the string table starts. We just build the string
899 table in memory as we go along. We process all the relocations
900 for a single input file at once. */
901 obj_raw_syment_count (abfd
) = 0;
903 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
905 if (! bfd_coff_start_final_link (abfd
, info
))
909 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
911 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
913 if (p
->type
== bfd_indirect_link_order
914 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
916 sub
= p
->u
.indirect
.section
->owner
;
917 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
919 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
921 sub
->output_has_begun
= true;
924 else if (p
->type
== bfd_section_reloc_link_order
925 || p
->type
== bfd_symbol_reloc_link_order
)
927 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
932 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
938 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
941 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
943 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
944 debug_merge_allocated
= false;
946 if (finfo
.internal_syms
!= NULL
)
948 free (finfo
.internal_syms
);
949 finfo
.internal_syms
= NULL
;
951 if (finfo
.sec_ptrs
!= NULL
)
953 free (finfo
.sec_ptrs
);
954 finfo
.sec_ptrs
= NULL
;
956 if (finfo
.sym_indices
!= NULL
)
958 free (finfo
.sym_indices
);
959 finfo
.sym_indices
= NULL
;
961 if (finfo
.linenos
!= NULL
)
963 free (finfo
.linenos
);
964 finfo
.linenos
= NULL
;
966 if (finfo
.contents
!= NULL
)
968 free (finfo
.contents
);
969 finfo
.contents
= NULL
;
971 if (finfo
.external_relocs
!= NULL
)
973 free (finfo
.external_relocs
);
974 finfo
.external_relocs
= NULL
;
976 if (finfo
.internal_relocs
!= NULL
)
978 free (finfo
.internal_relocs
);
979 finfo
.internal_relocs
= NULL
;
982 /* The value of the last C_FILE symbol is supposed to be the symbol
983 index of the first external symbol. Write it out again if
985 if (finfo
.last_file_index
!= -1
986 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
990 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
991 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
992 (PTR
) finfo
.outsyms
);
994 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
995 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
996 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
1000 /* If doing task linking (ld --task-link) then make a pass through the
1001 global symbols, writing out any that are defined, and making them
1003 if (info
->task_link
)
1005 finfo
.failed
= false;
1006 coff_link_hash_traverse (coff_hash_table (info
),
1007 _bfd_coff_write_task_globals
,
1013 /* Write out the global symbols. */
1014 finfo
.failed
= false;
1015 coff_link_hash_traverse (coff_hash_table (info
),
1016 _bfd_coff_write_global_sym
,
1021 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1022 if (finfo
.outsyms
!= NULL
)
1024 free (finfo
.outsyms
);
1025 finfo
.outsyms
= NULL
;
1028 if (info
->relocateable
&& max_output_reloc_count
> 0)
1030 /* Now that we have written out all the global symbols, we know
1031 the symbol indices to use for relocs against them, and we can
1032 finally write out the relocs. */
1033 amt
= max_output_reloc_count
* relsz
;
1034 external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
1035 if (external_relocs
== NULL
)
1038 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1040 struct internal_reloc
*irel
;
1041 struct internal_reloc
*irelend
;
1042 struct coff_link_hash_entry
**rel_hash
;
1045 if (o
->reloc_count
== 0)
1048 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1049 irelend
= irel
+ o
->reloc_count
;
1050 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1051 erel
= external_relocs
;
1052 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1054 if (*rel_hash
!= NULL
)
1056 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1057 irel
->r_symndx
= (*rel_hash
)->indx
;
1059 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1062 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1063 || (bfd_bwrite ((PTR
) external_relocs
,
1064 (bfd_size_type
) relsz
* o
->reloc_count
, abfd
)
1065 != (bfd_size_type
) relsz
* o
->reloc_count
))
1069 free (external_relocs
);
1070 external_relocs
= NULL
;
1073 /* Free up the section information. */
1074 if (finfo
.section_info
!= NULL
)
1078 for (i
= 0; i
< abfd
->section_count
; i
++)
1080 if (finfo
.section_info
[i
].relocs
!= NULL
)
1081 free (finfo
.section_info
[i
].relocs
);
1082 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1083 free (finfo
.section_info
[i
].rel_hashes
);
1085 free (finfo
.section_info
);
1086 finfo
.section_info
= NULL
;
1089 /* If we have optimized stabs strings, output them. */
1090 if (coff_hash_table (info
)->stab_info
!= NULL
)
1092 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1096 /* Write out the string table. */
1097 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1101 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
1102 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1105 #if STRING_SIZE_SIZE == 4
1107 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1110 #error Change H_PUT_32 above
1113 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1114 != STRING_SIZE_SIZE
)
1117 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1120 obj_coff_strings_written (abfd
) = true;
1123 _bfd_stringtab_free (finfo
.strtab
);
1125 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1126 not try to write out the symbols. */
1127 bfd_get_symcount (abfd
) = 0;
1132 if (debug_merge_allocated
)
1133 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1134 if (finfo
.strtab
!= NULL
)
1135 _bfd_stringtab_free (finfo
.strtab
);
1136 if (finfo
.section_info
!= NULL
)
1140 for (i
= 0; i
< abfd
->section_count
; i
++)
1142 if (finfo
.section_info
[i
].relocs
!= NULL
)
1143 free (finfo
.section_info
[i
].relocs
);
1144 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1145 free (finfo
.section_info
[i
].rel_hashes
);
1147 free (finfo
.section_info
);
1149 if (finfo
.internal_syms
!= NULL
)
1150 free (finfo
.internal_syms
);
1151 if (finfo
.sec_ptrs
!= NULL
)
1152 free (finfo
.sec_ptrs
);
1153 if (finfo
.sym_indices
!= NULL
)
1154 free (finfo
.sym_indices
);
1155 if (finfo
.outsyms
!= NULL
)
1156 free (finfo
.outsyms
);
1157 if (finfo
.linenos
!= NULL
)
1158 free (finfo
.linenos
);
1159 if (finfo
.contents
!= NULL
)
1160 free (finfo
.contents
);
1161 if (finfo
.external_relocs
!= NULL
)
1162 free (finfo
.external_relocs
);
1163 if (finfo
.internal_relocs
!= NULL
)
1164 free (finfo
.internal_relocs
);
1165 if (external_relocs
!= NULL
)
1166 free (external_relocs
);
1170 /* parse out a -heap <reserved>,<commit> line */
1173 dores_com (ptr
, output_bfd
, heap
)
1178 if (coff_data(output_bfd
)->pe
)
1180 int val
= strtoul (ptr
, &ptr
, 0);
1182 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
= val
;
1184 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
= val
;
1188 val
= strtoul (ptr
+1, &ptr
, 0);
1190 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
= val
;
1192 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
= val
;
1198 static char *get_name(ptr
, dst
)
1205 while (*ptr
&& *ptr
!= ' ')
1211 /* Process any magic embedded commands in a section called .drectve */
1214 process_embedded_commands (output_bfd
, info
, abfd
)
1216 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1219 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1226 copy
= bfd_malloc (sec
->_raw_size
);
1229 if (! bfd_get_section_contents(abfd
, sec
, copy
, (bfd_vma
) 0, sec
->_raw_size
))
1234 e
= copy
+ sec
->_raw_size
;
1235 for (s
= copy
; s
< e
; )
1241 if (strncmp (s
,"-attr", 5) == 0)
1253 s
= get_name(s
, &name
);
1254 s
= get_name(s
, &attribs
);
1274 asec
= bfd_get_section_by_name (abfd
, name
);
1277 asec
->flags
|= SEC_CODE
;
1279 asec
->flags
|= SEC_READONLY
;
1282 else if (strncmp (s
,"-heap", 5) == 0)
1284 s
= dores_com (s
+5, output_bfd
, 1);
1286 else if (strncmp (s
,"-stack", 6) == 0)
1288 s
= dores_com (s
+6, output_bfd
, 0);
1297 /* Place a marker against all symbols which are used by relocations.
1298 This marker can be picked up by the 'do we skip this symbol ?'
1299 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1304 mark_relocs (finfo
, input_bfd
)
1305 struct coff_final_link_info
* finfo
;
1310 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1313 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1315 struct internal_reloc
* internal_relocs
;
1316 struct internal_reloc
* irel
;
1317 struct internal_reloc
* irelend
;
1319 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1322 /* Read in the relocs. */
1323 internal_relocs
= _bfd_coff_read_internal_relocs
1324 (input_bfd
, a
, false,
1325 finfo
->external_relocs
,
1326 finfo
->info
->relocateable
,
1327 (finfo
->info
->relocateable
1328 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1329 : finfo
->internal_relocs
)
1332 if (internal_relocs
== NULL
)
1335 irel
= internal_relocs
;
1336 irelend
= irel
+ a
->reloc_count
;
1338 /* Place a mark in the sym_indices array (whose entries have
1339 been initialised to 0) for all of the symbols that are used
1340 in the relocation table. This will then be picked up in the
1343 for (; irel
< irelend
; irel
++)
1345 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1350 /* Link an input file into the linker output file. This function
1351 handles all the sections and relocations of the input file at once. */
1354 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1355 struct coff_final_link_info
*finfo
;
1358 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1359 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1361 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1363 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1364 asection
*, struct internal_reloc
*,
1367 const char *strings
;
1368 bfd_size_type syment_base
;
1370 bfd_size_type isymesz
;
1371 bfd_size_type osymesz
;
1372 bfd_size_type linesz
;
1375 struct internal_syment
*isymp
;
1378 unsigned long output_index
;
1380 struct coff_link_hash_entry
**sym_hash
;
1383 /* Move all the symbols to the output file. */
1385 output_bfd
= finfo
->output_bfd
;
1387 syment_base
= obj_raw_syment_count (output_bfd
);
1388 isymesz
= bfd_coff_symesz (input_bfd
);
1389 osymesz
= bfd_coff_symesz (output_bfd
);
1390 linesz
= bfd_coff_linesz (input_bfd
);
1391 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1394 if (! finfo
->info
->keep_memory
)
1397 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1400 if (! _bfd_coff_get_external_symbols (input_bfd
))
1403 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1404 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1405 isymp
= finfo
->internal_syms
;
1406 secpp
= finfo
->sec_ptrs
;
1407 indexp
= finfo
->sym_indices
;
1408 output_index
= syment_base
;
1409 outsym
= finfo
->outsyms
;
1411 if (coff_data (output_bfd
)->pe
)
1413 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1417 /* If we are going to perform relocations and also strip/discard some symbols
1418 then we must make sure that we do not strip/discard those symbols that are
1419 going to be involved in the relocations */
1420 if (( finfo
->info
->strip
!= strip_none
1421 || finfo
->info
->discard
!= discard_none
)
1422 && finfo
->info
->relocateable
)
1424 /* mark the symbol array as 'not-used' */
1425 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1427 mark_relocs (finfo
, input_bfd
);
1430 while (esym
< esym_end
)
1432 struct internal_syment isym
;
1433 enum coff_symbol_classification classification
;
1436 boolean dont_skip_symbol
;
1439 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1441 /* Make a copy of *isymp so that the relocate_section function
1442 always sees the original values. This is more reliable than
1443 always recomputing the symbol value even if we are stripping
1447 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1448 switch (classification
)
1452 case COFF_SYMBOL_GLOBAL
:
1453 case COFF_SYMBOL_PE_SECTION
:
1454 case COFF_SYMBOL_LOCAL
:
1455 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1457 case COFF_SYMBOL_COMMON
:
1458 *secpp
= bfd_com_section_ptr
;
1460 case COFF_SYMBOL_UNDEFINED
:
1461 *secpp
= bfd_und_section_ptr
;
1465 /* Extract the flag indicating if this symbol is used by a
1467 if ((finfo
->info
->strip
!= strip_none
1468 || finfo
->info
->discard
!= discard_none
)
1469 && finfo
->info
->relocateable
)
1470 dont_skip_symbol
= *indexp
;
1472 dont_skip_symbol
= false;
1478 add
= 1 + isym
.n_numaux
;
1480 /* If we are stripping all symbols, we want to skip this one. */
1481 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1486 switch (classification
)
1490 case COFF_SYMBOL_GLOBAL
:
1491 case COFF_SYMBOL_COMMON
:
1492 case COFF_SYMBOL_PE_SECTION
:
1493 /* This is a global symbol. Global symbols come at the
1494 end of the symbol table, so skip them for now.
1495 Locally defined function symbols, however, are an
1496 exception, and are not moved to the end. */
1498 if (! ISFCN (isym
.n_type
))
1502 case COFF_SYMBOL_UNDEFINED
:
1503 /* Undefined symbols are left for the end. */
1508 case COFF_SYMBOL_LOCAL
:
1509 /* This is a local symbol. Skip it if we are discarding
1511 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1517 #ifndef COFF_WITH_PE
1518 /* Skip section symbols for sections which are not going to be
1521 && isym
.n_sclass
== C_STAT
1522 && isym
.n_type
== T_NULL
1523 && isym
.n_numaux
> 0)
1525 if ((*secpp
)->output_section
== bfd_abs_section_ptr
)
1530 /* If we stripping debugging symbols, and this is a debugging
1531 symbol, then skip it. FIXME: gas sets the section to N_ABS
1532 for some types of debugging symbols; I don't know if this is
1533 a bug or not. In any case, we handle it here. */
1535 && finfo
->info
->strip
== strip_debugger
1536 && ! dont_skip_symbol
1537 && (isym
.n_scnum
== N_DEBUG
1538 || (isym
.n_scnum
== N_ABS
1539 && (isym
.n_sclass
== C_AUTO
1540 || isym
.n_sclass
== C_REG
1541 || isym
.n_sclass
== C_MOS
1542 || isym
.n_sclass
== C_MOE
1543 || isym
.n_sclass
== C_MOU
1544 || isym
.n_sclass
== C_ARG
1545 || isym
.n_sclass
== C_REGPARM
1546 || isym
.n_sclass
== C_FIELD
1547 || isym
.n_sclass
== C_EOS
))))
1550 /* If some symbols are stripped based on the name, work out the
1551 name and decide whether to skip this symbol. */
1553 && (finfo
->info
->strip
== strip_some
1554 || finfo
->info
->discard
== discard_l
))
1557 char buf
[SYMNMLEN
+ 1];
1559 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1563 if (! dont_skip_symbol
1564 && ((finfo
->info
->strip
== strip_some
1565 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1568 && finfo
->info
->discard
== discard_l
1569 && bfd_is_local_label_name (input_bfd
, name
))))
1573 /* If this is an enum, struct, or union tag, see if we have
1574 already output an identical type. */
1576 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1577 && (isym
.n_sclass
== C_ENTAG
1578 || isym
.n_sclass
== C_STRTAG
1579 || isym
.n_sclass
== C_UNTAG
)
1580 && isym
.n_numaux
== 1)
1583 char buf
[SYMNMLEN
+ 1];
1584 struct coff_debug_merge_hash_entry
*mh
;
1585 struct coff_debug_merge_type
*mt
;
1586 union internal_auxent aux
;
1587 struct coff_debug_merge_element
**epp
;
1588 bfd_byte
*esl
, *eslend
;
1589 struct internal_syment
*islp
;
1592 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1596 /* Ignore fake names invented by compiler; treat them all as
1598 if (*name
== '~' || *name
== '.' || *name
== '$'
1599 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1600 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1603 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1608 /* Allocate memory to hold type information. If this turns
1609 out to be a duplicate, we pass this address to
1611 amt
= sizeof (struct coff_debug_merge_type
);
1612 mt
= (struct coff_debug_merge_type
*) bfd_alloc (input_bfd
, amt
);
1615 mt
->class = isym
.n_sclass
;
1617 /* Pick up the aux entry, which points to the end of the tag
1619 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1620 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1623 /* Gather the elements. */
1624 epp
= &mt
->elements
;
1625 mt
->elements
= NULL
;
1627 esl
= esym
+ 2 * isymesz
;
1628 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1629 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1630 while (esl
< eslend
)
1632 const char *elename
;
1633 char elebuf
[SYMNMLEN
+ 1];
1636 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1638 amt
= sizeof (struct coff_debug_merge_element
);
1639 *epp
= ((struct coff_debug_merge_element
*)
1640 bfd_alloc (input_bfd
, amt
));
1644 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1646 if (elename
== NULL
)
1649 amt
= strlen (elename
) + 1;
1650 name_copy
= (char *) bfd_alloc (input_bfd
, amt
);
1651 if (name_copy
== NULL
)
1653 strcpy (name_copy
, elename
);
1655 (*epp
)->name
= name_copy
;
1656 (*epp
)->type
= islp
->n_type
;
1658 if (islp
->n_numaux
>= 1
1659 && islp
->n_type
!= T_NULL
1660 && islp
->n_sclass
!= C_EOS
)
1662 union internal_auxent eleaux
;
1665 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1666 islp
->n_type
, islp
->n_sclass
, 0,
1667 islp
->n_numaux
, (PTR
) &eleaux
);
1668 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1670 /* FIXME: If this tagndx entry refers to a symbol
1671 defined later in this file, we just ignore it.
1672 Handling this correctly would be tedious, and may
1678 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1681 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1682 if ((*epp
)->tagndx
< 0)
1686 epp
= &(*epp
)->next
;
1689 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1690 islp
+= islp
->n_numaux
+ 1;
1693 /* See if we already have a definition which matches this
1694 type. We always output the type if it has no elements,
1696 if (mt
->elements
== NULL
)
1697 bfd_release (input_bfd
, (PTR
) mt
);
1700 struct coff_debug_merge_type
*mtl
;
1702 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1704 struct coff_debug_merge_element
*me
, *mel
;
1706 if (mtl
->class != mt
->class)
1709 for (me
= mt
->elements
, mel
= mtl
->elements
;
1710 me
!= NULL
&& mel
!= NULL
;
1711 me
= me
->next
, mel
= mel
->next
)
1713 if (strcmp (me
->name
, mel
->name
) != 0
1714 || me
->type
!= mel
->type
1715 || me
->tagndx
!= mel
->tagndx
)
1719 if (me
== NULL
&& mel
== NULL
)
1723 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1725 /* This is the first definition of this type. */
1726 mt
->indx
= output_index
;
1727 mt
->next
= mh
->types
;
1732 /* This is a redefinition which can be merged. */
1733 bfd_release (input_bfd
, (PTR
) mt
);
1734 *indexp
= mtl
->indx
;
1735 add
= (eslend
- esym
) / isymesz
;
1741 /* We now know whether we are to skip this symbol or not. */
1744 /* Adjust the symbol in order to output it. */
1746 if (isym
._n
._n_n
._n_zeroes
== 0
1747 && isym
._n
._n_n
._n_offset
!= 0)
1752 /* This symbol has a long name. Enter it in the string
1753 table we are building. Note that we do not check
1754 bfd_coff_symname_in_debug. That is only true for
1755 XCOFF, and XCOFF requires different linking code
1757 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1761 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1762 if (indx
== (bfd_size_type
) -1)
1764 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1767 switch (isym
.n_sclass
)
1783 /* The symbol value should not be modified. */
1787 if (obj_pe (input_bfd
)
1788 && strcmp (isym
.n_name
, ".bf") != 0
1789 && isym
.n_scnum
> 0)
1791 /* For PE, .lf and .ef get their value left alone,
1792 while .bf gets relocated. However, they all have
1793 "real" section numbers, and need to be moved into
1795 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1800 case C_LABEL
: /* Not completely sure about these 2 */
1809 /* Compute new symbol location. */
1810 if (isym
.n_scnum
> 0)
1812 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1813 isym
.n_value
+= (*secpp
)->output_offset
;
1814 if (! obj_pe (input_bfd
))
1815 isym
.n_value
-= (*secpp
)->vma
;
1816 if (! obj_pe (finfo
->output_bfd
))
1817 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1822 /* The value of a C_FILE symbol is the symbol index of
1823 the next C_FILE symbol. The value of the last C_FILE
1824 symbol is the symbol index to the first external
1825 symbol (actually, coff_renumber_symbols does not get
1826 this right--it just sets the value of the last C_FILE
1827 symbol to zero--and nobody has ever complained about
1828 it). We try to get this right, below, just before we
1829 write the symbols out, but in the general case we may
1830 have to write the symbol out twice. */
1832 if (finfo
->last_file_index
!= -1
1833 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1835 /* We must correct the value of the last C_FILE
1837 finfo
->last_file
.n_value
= output_index
;
1838 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1840 /* The last C_FILE symbol is in this input file. */
1841 bfd_coff_swap_sym_out (output_bfd
,
1842 (PTR
) &finfo
->last_file
,
1843 (PTR
) (finfo
->outsyms
1844 + ((finfo
->last_file_index
1852 /* We have already written out the last C_FILE
1853 symbol. We need to write it out again. We
1854 borrow *outsym temporarily. */
1855 bfd_coff_swap_sym_out (output_bfd
,
1856 (PTR
) &finfo
->last_file
,
1858 pos
= obj_sym_filepos (output_bfd
);
1859 pos
+= finfo
->last_file_index
* osymesz
;
1860 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
1861 || bfd_bwrite (outsym
, osymesz
, output_bfd
) != osymesz
)
1866 finfo
->last_file_index
= output_index
;
1867 finfo
->last_file
= isym
;
1871 /* If doing task linking, convert normal global function symbols to
1872 static functions. */
1873 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1874 isym
.n_sclass
= C_STAT
;
1876 /* Output the symbol. */
1878 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1880 *indexp
= output_index
;
1885 struct coff_link_hash_entry
*h
;
1887 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1889 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1892 /* This can happen if there were errors earlier in
1894 bfd_set_error (bfd_error_bad_value
);
1897 h
->indx
= output_index
;
1900 output_index
+= add
;
1901 outsym
+= add
* osymesz
;
1904 esym
+= add
* isymesz
;
1908 for (--add
; add
> 0; --add
)
1915 /* Fix up the aux entries. This must be done in a separate pass,
1916 because we don't know the correct symbol indices until we have
1917 already decided which symbols we are going to keep. */
1919 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1920 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1921 isymp
= finfo
->internal_syms
;
1922 indexp
= finfo
->sym_indices
;
1923 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1924 outsym
= finfo
->outsyms
;
1925 while (esym
< esym_end
)
1929 add
= 1 + isymp
->n_numaux
;
1932 || (bfd_size_type
) *indexp
< syment_base
)
1933 && (*sym_hash
== NULL
1934 || (*sym_hash
)->auxbfd
!= input_bfd
))
1935 esym
+= add
* isymesz
;
1938 struct coff_link_hash_entry
*h
;
1946 /* The m68k-motorola-sysv assembler will sometimes
1947 generate two symbols with the same name, but only one
1948 will have aux entries. */
1949 BFD_ASSERT (isymp
->n_numaux
== 0
1950 || h
->numaux
== isymp
->n_numaux
);
1958 /* Handle the aux entries. This handling is based on
1959 coff_pointerize_aux. I don't know if it always correct. */
1960 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1962 union internal_auxent aux
;
1963 union internal_auxent
*auxp
;
1969 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1970 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1975 if (isymp
->n_sclass
== C_FILE
)
1977 /* If this is a long filename, we must put it in the
1979 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1980 && auxp
->x_file
.x_n
.x_offset
!= 0)
1982 const char *filename
;
1985 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1986 >= STRING_SIZE_SIZE
);
1987 if (strings
== NULL
)
1989 strings
= _bfd_coff_read_string_table (input_bfd
);
1990 if (strings
== NULL
)
1993 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1994 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1996 if (indx
== (bfd_size_type
) -1)
1998 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
2001 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
2005 if (ISFCN (isymp
->n_type
)
2006 || ISTAG (isymp
->n_sclass
)
2007 || isymp
->n_sclass
== C_BLOCK
2008 || isymp
->n_sclass
== C_FCN
)
2010 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2012 && indx
< obj_raw_syment_count (input_bfd
))
2014 /* We look forward through the symbol for
2015 the index of the next symbol we are going
2016 to include. I don't know if this is
2018 while ((finfo
->sym_indices
[indx
] < 0
2019 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
2021 && indx
< obj_raw_syment_count (input_bfd
))
2023 if (indx
>= obj_raw_syment_count (input_bfd
))
2024 indx
= output_index
;
2026 indx
= finfo
->sym_indices
[indx
];
2027 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2031 indx
= auxp
->x_sym
.x_tagndx
.l
;
2032 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2036 symindx
= finfo
->sym_indices
[indx
];
2038 auxp
->x_sym
.x_tagndx
.l
= 0;
2040 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2043 /* The .bf symbols are supposed to be linked through
2044 the endndx field. We need to carry this list
2045 across object files. */
2048 && isymp
->n_sclass
== C_FCN
2049 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2050 || isymp
->_n
._n_n
._n_offset
== 0)
2051 && isymp
->_n
._n_name
[0] == '.'
2052 && isymp
->_n
._n_name
[1] == 'b'
2053 && isymp
->_n
._n_name
[2] == 'f'
2054 && isymp
->_n
._n_name
[3] == '\0')
2056 if (finfo
->last_bf_index
!= -1)
2058 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2061 if ((bfd_size_type
) finfo
->last_bf_index
2066 /* The last .bf symbol is in this input
2067 file. This will only happen if the
2068 assembler did not set up the .bf
2069 endndx symbols correctly. */
2070 auxout
= (PTR
) (finfo
->outsyms
2071 + ((finfo
->last_bf_index
2074 bfd_coff_swap_aux_out (output_bfd
,
2075 (PTR
) &finfo
->last_bf
,
2085 /* We have already written out the last
2086 .bf aux entry. We need to write it
2087 out again. We borrow *outsym
2088 temporarily. FIXME: This case should
2090 bfd_coff_swap_aux_out (output_bfd
,
2091 (PTR
) &finfo
->last_bf
,
2096 pos
= obj_sym_filepos (output_bfd
);
2097 pos
+= finfo
->last_bf_index
* osymesz
;
2098 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2099 || (bfd_bwrite (outsym
, osymesz
, output_bfd
)
2105 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2106 finfo
->last_bf_index
= -1;
2109 /* The endndx field of this aux entry must
2110 be updated with the symbol number of the
2112 finfo
->last_bf
= *auxp
;
2113 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2122 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
2123 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2137 /* Relocate the line numbers, unless we are stripping them. */
2138 if (finfo
->info
->strip
== strip_none
2139 || finfo
->info
->strip
== strip_some
)
2141 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2151 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2152 build_link_order in ldwrite.c will not have created a
2153 link order, which means that we will not have seen this
2154 input section in _bfd_coff_final_link, which means that
2155 we will not have allocated space for the line numbers of
2156 this section. I don't think line numbers can be
2157 meaningful for a section which does not have
2158 SEC_HAS_CONTENTS set, but, if they do, this must be
2160 if (o
->lineno_count
== 0
2161 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2164 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2165 || bfd_bread (finfo
->linenos
, linesz
* o
->lineno_count
,
2166 input_bfd
) != linesz
* o
->lineno_count
)
2169 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2170 eline
= finfo
->linenos
;
2171 oeline
= finfo
->linenos
;
2172 elineend
= eline
+ linesz
* o
->lineno_count
;
2174 for (; eline
< elineend
; eline
+= linesz
)
2176 struct internal_lineno iline
;
2178 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2180 if (iline
.l_lnno
!= 0)
2181 iline
.l_addr
.l_paddr
+= offset
;
2182 else if (iline
.l_addr
.l_symndx
>= 0
2183 && ((unsigned long) iline
.l_addr
.l_symndx
2184 < obj_raw_syment_count (input_bfd
)))
2188 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2192 /* These line numbers are attached to a symbol
2193 which we are stripping. We must discard the
2194 line numbers because reading them back with
2195 no associated symbol (or associating them all
2196 with symbol #0) will fail. We can't regain
2197 the space in the output file, but at least
2203 struct internal_syment is
;
2204 union internal_auxent ia
;
2206 /* Fix up the lnnoptr field in the aux entry of
2207 the symbol. It turns out that we can't do
2208 this when we modify the symbol aux entries,
2209 because gas sometimes screws up the lnnoptr
2210 field and makes it an offset from the start
2211 of the line numbers rather than an absolute
2213 bfd_coff_swap_sym_in (output_bfd
,
2214 (PTR
) (finfo
->outsyms
2215 + ((indx
- syment_base
)
2218 if ((ISFCN (is
.n_type
)
2219 || is
.n_sclass
== C_BLOCK
)
2220 && is
.n_numaux
>= 1)
2224 auxptr
= (PTR
) (finfo
->outsyms
2225 + ((indx
- syment_base
+ 1)
2227 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2228 is
.n_type
, is
.n_sclass
,
2229 0, is
.n_numaux
, (PTR
) &ia
);
2230 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2231 (o
->output_section
->line_filepos
2232 + o
->output_section
->lineno_count
* linesz
2233 + eline
- finfo
->linenos
);
2234 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2235 is
.n_type
, is
.n_sclass
, 0,
2236 is
.n_numaux
, auxptr
);
2242 iline
.l_addr
.l_symndx
= indx
;
2247 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
,
2253 pos
= o
->output_section
->line_filepos
;
2254 pos
+= o
->output_section
->lineno_count
* linesz
;
2255 amt
= oeline
- finfo
->linenos
;
2256 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2257 || bfd_bwrite (finfo
->linenos
, amt
, output_bfd
) != amt
)
2260 o
->output_section
->lineno_count
+= amt
/ linesz
;
2264 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2265 symbol will be the first symbol in the next input file. In the
2266 normal case, this will save us from writing out the C_FILE symbol
2268 if (finfo
->last_file_index
!= -1
2269 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2271 finfo
->last_file
.n_value
= output_index
;
2272 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2273 (PTR
) (finfo
->outsyms
2274 + ((finfo
->last_file_index
- syment_base
)
2278 /* Write the modified symbols to the output file. */
2279 if (outsym
> finfo
->outsyms
)
2284 pos
= obj_sym_filepos (output_bfd
) + syment_base
* osymesz
;
2285 amt
= outsym
- finfo
->outsyms
;
2286 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2287 || bfd_bwrite (finfo
->outsyms
, amt
, output_bfd
) != amt
)
2290 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2291 + (outsym
- finfo
->outsyms
) / osymesz
)
2294 obj_raw_syment_count (output_bfd
) = output_index
;
2297 /* Relocate the contents of each section. */
2298 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2299 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2302 struct coff_section_tdata
*secdata
;
2304 if (! o
->linker_mark
)
2306 /* This section was omitted from the link. */
2310 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2311 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2313 if ((o
->flags
& SEC_RELOC
) != 0
2314 && o
->reloc_count
!= 0)
2316 ((*_bfd_error_handler
)
2317 (_("%s: relocs in section `%s', but it has no contents"),
2318 bfd_archive_filename (input_bfd
),
2319 bfd_get_section_name (input_bfd
, o
)));
2320 bfd_set_error (bfd_error_no_contents
);
2327 secdata
= coff_section_data (input_bfd
, o
);
2328 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2329 contents
= secdata
->contents
;
2332 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2333 (file_ptr
) 0, o
->_raw_size
))
2335 contents
= finfo
->contents
;
2338 if ((o
->flags
& SEC_RELOC
) != 0)
2341 struct internal_reloc
*internal_relocs
;
2342 struct internal_reloc
*irel
;
2344 /* Read in the relocs. */
2345 target_index
= o
->output_section
->target_index
;
2346 internal_relocs
= (_bfd_coff_read_internal_relocs
2347 (input_bfd
, o
, false, finfo
->external_relocs
,
2348 finfo
->info
->relocateable
,
2349 (finfo
->info
->relocateable
2350 ? (finfo
->section_info
[target_index
].relocs
2351 + o
->output_section
->reloc_count
)
2352 : finfo
->internal_relocs
)));
2353 if (internal_relocs
== NULL
)
2356 /* Call processor specific code to relocate the section
2358 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2362 finfo
->internal_syms
,
2366 if (finfo
->info
->relocateable
)
2369 struct internal_reloc
*irelend
;
2370 struct coff_link_hash_entry
**rel_hash
;
2372 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2373 irel
= internal_relocs
;
2374 irelend
= irel
+ o
->reloc_count
;
2375 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2376 + o
->output_section
->reloc_count
);
2377 for (; irel
< irelend
; irel
++, rel_hash
++)
2379 struct coff_link_hash_entry
*h
;
2384 /* Adjust the reloc address and symbol index. */
2386 irel
->r_vaddr
+= offset
;
2388 if (irel
->r_symndx
== -1)
2393 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2401 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2404 /* This is a global symbol. */
2406 irel
->r_symndx
= h
->indx
;
2409 /* This symbol is being written at the end
2410 of the file, and we do not yet know the
2411 symbol index. We save the pointer to the
2412 hash table entry in the rel_hash list.
2413 We set the indx field to -2 to indicate
2414 that this symbol must not be stripped. */
2423 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2425 irel
->r_symndx
= indx
;
2428 struct internal_syment
*is
;
2430 char buf
[SYMNMLEN
+ 1];
2432 /* This reloc is against a symbol we are
2433 stripping. This should have been handled
2434 by the 'dont_skip_symbol' code in the while
2435 loop at the top of this function. */
2437 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2439 name
= (_bfd_coff_internal_syment_name
2440 (input_bfd
, is
, buf
));
2444 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2445 (finfo
->info
, name
, input_bfd
, o
,
2452 o
->output_section
->reloc_count
+= o
->reloc_count
;
2456 /* Write out the modified section contents. */
2457 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2459 file_ptr loc
= o
->output_offset
* bfd_octets_per_byte (output_bfd
);
2460 bfd_size_type amt
= (o
->_cooked_size
!= 0
2461 ? o
->_cooked_size
: o
->_raw_size
);
2462 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2463 contents
, loc
, amt
))
2468 if (! (_bfd_write_section_stabs
2469 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2470 o
, &secdata
->stab_info
, contents
)))
2475 if (! finfo
->info
->keep_memory
)
2477 if (! _bfd_coff_free_symbols (input_bfd
))
2484 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2487 _bfd_coff_write_global_sym (h
, data
)
2488 struct coff_link_hash_entry
*h
;
2491 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2493 struct internal_syment isym
;
2494 bfd_size_type symesz
;
2498 output_bfd
= finfo
->output_bfd
;
2500 if (h
->root
.type
== bfd_link_hash_warning
)
2502 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2503 if (h
->root
.type
== bfd_link_hash_new
)
2511 && (finfo
->info
->strip
== strip_all
2512 || (finfo
->info
->strip
== strip_some
2513 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2514 h
->root
.root
.string
, false, false)
2518 switch (h
->root
.type
)
2521 case bfd_link_hash_new
:
2522 case bfd_link_hash_warning
:
2526 case bfd_link_hash_undefined
:
2527 case bfd_link_hash_undefweak
:
2528 isym
.n_scnum
= N_UNDEF
;
2532 case bfd_link_hash_defined
:
2533 case bfd_link_hash_defweak
:
2537 sec
= h
->root
.u
.def
.section
->output_section
;
2538 if (bfd_is_abs_section (sec
))
2539 isym
.n_scnum
= N_ABS
;
2541 isym
.n_scnum
= sec
->target_index
;
2542 isym
.n_value
= (h
->root
.u
.def
.value
2543 + h
->root
.u
.def
.section
->output_offset
);
2544 if (! obj_pe (finfo
->output_bfd
))
2545 isym
.n_value
+= sec
->vma
;
2549 case bfd_link_hash_common
:
2550 isym
.n_scnum
= N_UNDEF
;
2551 isym
.n_value
= h
->root
.u
.c
.size
;
2554 case bfd_link_hash_indirect
:
2555 /* Just ignore these. They can't be handled anyhow. */
2559 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2560 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2567 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2569 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2571 if (indx
== (bfd_size_type
) -1)
2573 finfo
->failed
= true;
2576 isym
._n
._n_n
._n_zeroes
= 0;
2577 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2580 isym
.n_sclass
= h
->class;
2581 isym
.n_type
= h
->type
;
2583 if (isym
.n_sclass
== C_NULL
)
2584 isym
.n_sclass
= C_EXT
;
2586 /* If doing task linking and this is the pass where we convert
2587 defined globals to statics, then do that conversion now. If the
2588 symbol is not being converted, just ignore it and it will be
2589 output during a later pass. */
2590 if (finfo
->global_to_static
)
2592 if (! IS_EXTERNAL (output_bfd
, isym
))
2595 isym
.n_sclass
= C_STAT
;
2598 /* When a weak symbol is not overriden by a strong one,
2599 turn it into an external symbol when not building a
2600 shared or relocateable object. */
2601 if (! finfo
->info
->shared
2602 && ! finfo
->info
->relocateable
2603 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2604 isym
.n_sclass
= C_EXT
;
2606 isym
.n_numaux
= h
->numaux
;
2608 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2610 symesz
= bfd_coff_symesz (output_bfd
);
2612 pos
= obj_sym_filepos (output_bfd
);
2613 pos
+= obj_raw_syment_count (output_bfd
) * symesz
;
2614 if (bfd_seek (output_bfd
, pos
, SEEK_SET
) != 0
2615 || bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2617 finfo
->failed
= true;
2621 h
->indx
= obj_raw_syment_count (output_bfd
);
2623 ++obj_raw_syment_count (output_bfd
);
2625 /* Write out any associated aux entries. Most of the aux entries
2626 will have been modified in _bfd_coff_link_input_bfd. We have to
2627 handle section aux entries here, now that we have the final
2628 relocation and line number counts. */
2629 for (i
= 0; i
< isym
.n_numaux
; i
++)
2631 union internal_auxent
*auxp
;
2635 /* Look for a section aux entry here using the same tests that
2636 coff_swap_aux_out uses. */
2638 && (isym
.n_sclass
== C_STAT
2639 || isym
.n_sclass
== C_HIDDEN
)
2640 && isym
.n_type
== T_NULL
2641 && (h
->root
.type
== bfd_link_hash_defined
2642 || h
->root
.type
== bfd_link_hash_defweak
))
2646 sec
= h
->root
.u
.def
.section
->output_section
;
2649 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2653 /* For PE, an overflow on the final link reportedly does
2654 not matter. FIXME: Why not? */
2656 if (sec
->reloc_count
> 0xffff
2657 && (! obj_pe (output_bfd
)
2658 || finfo
->info
->relocateable
))
2659 (*_bfd_error_handler
)
2660 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2661 bfd_get_filename (output_bfd
),
2662 bfd_get_section_name (output_bfd
, sec
),
2665 if (sec
->lineno_count
> 0xffff
2666 && (! obj_pe (output_bfd
)
2667 || finfo
->info
->relocateable
))
2668 (*_bfd_error_handler
)
2669 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2670 bfd_get_filename (output_bfd
),
2671 bfd_get_section_name (output_bfd
, sec
),
2674 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2675 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2676 auxp
->x_scn
.x_checksum
= 0;
2677 auxp
->x_scn
.x_associated
= 0;
2678 auxp
->x_scn
.x_comdat
= 0;
2682 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isym
.n_type
,
2683 isym
.n_sclass
, (int) i
, isym
.n_numaux
,
2684 (PTR
) finfo
->outsyms
);
2685 if (bfd_bwrite (finfo
->outsyms
, symesz
, output_bfd
) != symesz
)
2687 finfo
->failed
= true;
2690 ++obj_raw_syment_count (output_bfd
);
2696 /* Write out task global symbols, converting them to statics. Called
2697 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2698 the dirty work, if the symbol we are processing needs conversion. */
2701 _bfd_coff_write_task_globals (h
, data
)
2702 struct coff_link_hash_entry
*h
;
2705 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2706 boolean rtnval
= true;
2707 boolean save_global_to_static
;
2709 if (h
->root
.type
== bfd_link_hash_warning
)
2710 h
= (struct coff_link_hash_entry
*) h
->root
.u
.i
.link
;
2714 switch (h
->root
.type
)
2716 case bfd_link_hash_defined
:
2717 case bfd_link_hash_defweak
:
2718 save_global_to_static
= finfo
->global_to_static
;
2719 finfo
->global_to_static
= true;
2720 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2721 finfo
->global_to_static
= save_global_to_static
;
2730 /* Handle a link order which is supposed to generate a reloc. */
2733 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2735 struct coff_final_link_info
*finfo
;
2736 asection
*output_section
;
2737 struct bfd_link_order
*link_order
;
2739 reloc_howto_type
*howto
;
2740 struct internal_reloc
*irel
;
2741 struct coff_link_hash_entry
**rel_hash_ptr
;
2743 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2746 bfd_set_error (bfd_error_bad_value
);
2750 if (link_order
->u
.reloc
.p
->addend
!= 0)
2754 bfd_reloc_status_type rstat
;
2758 size
= bfd_get_reloc_size (howto
);
2759 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2763 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2764 (bfd_vma
) link_order
->u
.reloc
.p
->addend
,\
2771 case bfd_reloc_outofrange
:
2773 case bfd_reloc_overflow
:
2774 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2776 (link_order
->type
== bfd_section_reloc_link_order
2777 ? bfd_section_name (output_bfd
,
2778 link_order
->u
.reloc
.p
->u
.section
)
2779 : link_order
->u
.reloc
.p
->u
.name
),
2780 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2781 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2788 loc
= link_order
->offset
* bfd_octets_per_byte (output_bfd
);
2789 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2796 /* Store the reloc information in the right place. It will get
2797 swapped and written out at the end of the final_link routine. */
2799 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2800 + output_section
->reloc_count
);
2801 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2802 + output_section
->reloc_count
);
2804 memset (irel
, 0, sizeof (struct internal_reloc
));
2805 *rel_hash_ptr
= NULL
;
2807 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2809 if (link_order
->type
== bfd_section_reloc_link_order
)
2811 /* We need to somehow locate a symbol in the right section. The
2812 symbol must either have a value of zero, or we must adjust
2813 the addend by the value of the symbol. FIXME: Write this
2814 when we need it. The old linker couldn't handle this anyhow. */
2816 *rel_hash_ptr
= NULL
;
2821 struct coff_link_hash_entry
*h
;
2823 h
= ((struct coff_link_hash_entry
*)
2824 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2825 link_order
->u
.reloc
.p
->u
.name
,
2826 false, false, true));
2830 irel
->r_symndx
= h
->indx
;
2833 /* Set the index to -2 to force this symbol to get
2842 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2843 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2844 (asection
*) NULL
, (bfd_vma
) 0)))
2850 /* FIXME: Is this always right? */
2851 irel
->r_type
= howto
->type
;
2853 /* r_size is only used on the RS/6000, which needs its own linker
2854 routines anyhow. r_extern is only used for ECOFF. */
2856 /* FIXME: What is the right value for r_offset? Is zero OK? */
2858 ++output_section
->reloc_count
;
2863 /* A basic reloc handling routine which may be used by processors with
2867 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2868 input_section
, contents
, relocs
, syms
,
2871 struct bfd_link_info
*info
;
2873 asection
*input_section
;
2875 struct internal_reloc
*relocs
;
2876 struct internal_syment
*syms
;
2877 asection
**sections
;
2879 struct internal_reloc
*rel
;
2880 struct internal_reloc
*relend
;
2883 relend
= rel
+ input_section
->reloc_count
;
2884 for (; rel
< relend
; rel
++)
2887 struct coff_link_hash_entry
*h
;
2888 struct internal_syment
*sym
;
2891 reloc_howto_type
*howto
;
2892 bfd_reloc_status_type rstat
;
2894 symndx
= rel
->r_symndx
;
2902 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2904 (*_bfd_error_handler
)
2905 ("%s: illegal symbol index %ld in relocs",
2906 bfd_archive_filename (input_bfd
), symndx
);
2911 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2912 sym
= syms
+ symndx
;
2915 /* COFF treats common symbols in one of two ways. Either the
2916 size of the symbol is included in the section contents, or it
2917 is not. We assume that the size is not included, and force
2918 the rtype_to_howto function to adjust the addend as needed. */
2920 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2921 addend
= - sym
->n_value
;
2925 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2930 /* If we are doing a relocateable link, then we can just ignore
2931 a PC relative reloc that is pcrel_offset. It will already
2932 have the correct value. If this is not a relocateable link,
2933 then we should ignore the symbol value. */
2934 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2936 if (info
->relocateable
)
2938 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2939 addend
+= sym
->n_value
;
2950 sec
= bfd_abs_section_ptr
;
2955 sec
= sections
[symndx
];
2956 val
= (sec
->output_section
->vma
2957 + sec
->output_offset
2959 if (! obj_pe (input_bfd
))
2965 if (h
->root
.type
== bfd_link_hash_defined
2966 || h
->root
.type
== bfd_link_hash_defweak
)
2970 sec
= h
->root
.u
.def
.section
;
2971 val
= (h
->root
.u
.def
.value
2972 + sec
->output_section
->vma
2973 + sec
->output_offset
);
2976 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2979 else if (! info
->relocateable
)
2981 if (! ((*info
->callbacks
->undefined_symbol
)
2982 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2983 rel
->r_vaddr
- input_section
->vma
, true)))
2988 if (info
->base_file
)
2990 /* Emit a reloc if the backend thinks it needs it. */
2991 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2993 /* Relocation to a symbol in a section which isn't
2994 absolute. We output the address here to a file.
2995 This file is then read by dlltool when generating the
2996 reloc section. Note that the base file is not
2997 portable between systems. We write out a long here,
2998 and dlltool reads in a long. */
2999 long addr
= (rel
->r_vaddr
3000 - input_section
->vma
3001 + input_section
->output_offset
3002 + input_section
->output_section
->vma
);
3003 if (coff_data (output_bfd
)->pe
)
3004 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
3005 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
3008 bfd_set_error (bfd_error_system_call
);
3014 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3016 rel
->r_vaddr
- input_section
->vma
,
3025 case bfd_reloc_outofrange
:
3026 (*_bfd_error_handler
)
3027 (_("%s: bad reloc address 0x%lx in section `%s'"),
3028 bfd_archive_filename (input_bfd
),
3029 (unsigned long) rel
->r_vaddr
,
3030 bfd_get_section_name (input_bfd
, input_section
));
3032 case bfd_reloc_overflow
:
3035 char buf
[SYMNMLEN
+ 1];
3040 name
= h
->root
.root
.string
;
3043 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3048 if (! ((*info
->callbacks
->reloc_overflow
)
3049 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
3050 input_section
, rel
->r_vaddr
- input_section
->vma
)))