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 /* Define macros so that the ISFCN, et. al., macros work correctly.
44 These macros are defined in include/coff/internal.h in terms of
45 N_TMASK, etc. These definitions require a user to define local
46 variables with the appropriate names, and with values from the
47 coff_data (abfd) structure. */
49 #define N_TMASK n_tmask
50 #define N_BTSHFT n_btshft
51 #define N_BTMASK n_btmask
53 /* Create an entry in a COFF linker hash table. */
55 struct bfd_hash_entry
*
56 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
57 struct bfd_hash_entry
*entry
;
58 struct bfd_hash_table
*table
;
61 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
63 /* Allocate the structure if it has not already been allocated by a
65 if (ret
== (struct coff_link_hash_entry
*) NULL
)
66 ret
= ((struct coff_link_hash_entry
*)
67 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
68 if (ret
== (struct coff_link_hash_entry
*) NULL
)
69 return (struct bfd_hash_entry
*) ret
;
71 /* Call the allocation method of the superclass. */
72 ret
= ((struct coff_link_hash_entry
*)
73 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
75 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
77 /* Set local fields. */
86 return (struct bfd_hash_entry
*) ret
;
89 /* Initialize a COFF linker hash table. */
92 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
93 struct coff_link_hash_table
*table
;
95 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
96 struct bfd_hash_table
*,
99 table
->stab_info
= NULL
;
100 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
103 /* Create a COFF linker hash table. */
105 struct bfd_link_hash_table
*
106 _bfd_coff_link_hash_table_create (abfd
)
109 struct coff_link_hash_table
*ret
;
111 ret
= ((struct coff_link_hash_table
*)
112 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
115 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
116 _bfd_coff_link_hash_newfunc
))
118 bfd_release (abfd
, ret
);
119 return (struct bfd_link_hash_table
*) NULL
;
124 /* Create an entry in a COFF debug merge hash table. */
126 struct bfd_hash_entry
*
127 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
128 struct bfd_hash_entry
*entry
;
129 struct bfd_hash_table
*table
;
132 struct coff_debug_merge_hash_entry
*ret
=
133 (struct coff_debug_merge_hash_entry
*) entry
;
135 /* Allocate the structure if it has not already been allocated by a
137 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
138 ret
= ((struct coff_debug_merge_hash_entry
*)
139 bfd_hash_allocate (table
,
140 sizeof (struct coff_debug_merge_hash_entry
)));
141 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
142 return (struct bfd_hash_entry
*) ret
;
144 /* Call the allocation method of the superclass. */
145 ret
= ((struct coff_debug_merge_hash_entry
*)
146 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
147 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
149 /* Set local fields. */
153 return (struct bfd_hash_entry
*) ret
;
156 /* Given a COFF BFD, add symbols to the global hash table as
160 _bfd_coff_link_add_symbols (abfd
, info
)
162 struct bfd_link_info
*info
;
164 switch (bfd_get_format (abfd
))
167 return coff_link_add_object_symbols (abfd
, info
);
169 return (_bfd_generic_link_add_archive_symbols
170 (abfd
, info
, coff_link_check_archive_element
));
172 bfd_set_error (bfd_error_wrong_format
);
177 /* Add symbols from a COFF object file. */
180 coff_link_add_object_symbols (abfd
, info
)
182 struct bfd_link_info
*info
;
184 if (! _bfd_coff_get_external_symbols (abfd
))
186 if (! coff_link_add_symbols (abfd
, info
))
189 if (! info
->keep_memory
)
191 if (! _bfd_coff_free_symbols (abfd
))
197 /* Check a single archive element to see if we need to include it in
198 the link. *PNEEDED is set according to whether this element is
199 needed in the link or not. This is called via
200 _bfd_generic_link_add_archive_symbols. */
203 coff_link_check_archive_element (abfd
, info
, pneeded
)
205 struct bfd_link_info
*info
;
208 if (! _bfd_coff_get_external_symbols (abfd
))
211 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
216 if (! coff_link_add_symbols (abfd
, info
))
220 if (! info
->keep_memory
|| ! *pneeded
)
222 if (! _bfd_coff_free_symbols (abfd
))
229 /* Look through the symbols to see if this object file should be
230 included in the link. */
233 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
235 struct bfd_link_info
*info
;
238 bfd_size_type symesz
;
244 symesz
= bfd_coff_symesz (abfd
);
245 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
246 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
247 while (esym
< esym_end
)
249 struct internal_syment sym
;
250 enum coff_symbol_classification classification
;
252 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
254 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
255 if (classification
== COFF_SYMBOL_GLOBAL
256 || classification
== COFF_SYMBOL_COMMON
)
259 char buf
[SYMNMLEN
+ 1];
260 struct bfd_link_hash_entry
*h
;
262 /* This symbol is externally visible, and is defined by this
265 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
268 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
270 /* We are only interested in symbols that are currently
271 undefined. If a symbol is currently known to be common,
272 COFF linkers do not bring in an object file which defines
274 if (h
!= (struct bfd_link_hash_entry
*) NULL
275 && h
->type
== bfd_link_hash_undefined
)
277 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
284 esym
+= (sym
.n_numaux
+ 1) * symesz
;
287 /* We do not need this object file. */
291 /* Add all the symbols from an object file to the hash table. */
294 coff_link_add_symbols (abfd
, info
)
296 struct bfd_link_info
*info
;
298 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
299 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
300 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
302 boolean default_copy
;
303 bfd_size_type symcount
;
304 struct coff_link_hash_entry
**sym_hash
;
305 bfd_size_type symesz
;
309 /* Keep the symbols during this function, in case the linker needs
310 to read the generic symbols in order to report an error message. */
311 keep_syms
= obj_coff_keep_syms (abfd
);
312 obj_coff_keep_syms (abfd
) = true;
314 if (info
->keep_memory
)
315 default_copy
= false;
319 symcount
= obj_raw_syment_count (abfd
);
321 /* We keep a list of the linker hash table entries that correspond
322 to particular symbols. */
323 sym_hash
= ((struct coff_link_hash_entry
**)
326 * sizeof (struct coff_link_hash_entry
*))));
327 if (sym_hash
== NULL
&& symcount
!= 0)
329 obj_coff_sym_hashes (abfd
) = sym_hash
;
331 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
333 symesz
= bfd_coff_symesz (abfd
);
334 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
335 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
336 esym_end
= esym
+ symcount
* symesz
;
337 while (esym
< esym_end
)
339 struct internal_syment sym
;
340 enum coff_symbol_classification classification
;
343 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
345 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
346 if (classification
!= COFF_SYMBOL_LOCAL
)
349 char buf
[SYMNMLEN
+ 1];
355 /* This symbol is externally visible. */
357 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
361 /* We must copy the name into memory if we got it from the
362 syment itself, rather than the string table. */
364 if (sym
._n
._n_n
._n_zeroes
!= 0
365 || sym
._n
._n_n
._n_offset
== 0)
370 switch (classification
)
375 case COFF_SYMBOL_GLOBAL
:
376 flags
= BSF_EXPORT
| BSF_GLOBAL
;
377 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
379 value
-= section
->vma
;
382 case COFF_SYMBOL_UNDEFINED
:
384 section
= bfd_und_section_ptr
;
387 case COFF_SYMBOL_COMMON
:
389 section
= bfd_com_section_ptr
;
392 case COFF_SYMBOL_PE_SECTION
:
393 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
394 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
398 if (sym
.n_sclass
== C_WEAKEXT
399 || (obj_pe (abfd
) && sym
.n_sclass
== C_NT_WEAK
))
404 /* In the PE format, section symbols actually refer to the
405 start of the output section. We handle them specially
407 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
409 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
410 name
, false, copy
, false);
411 if (*sym_hash
!= NULL
)
413 if (((*sym_hash
)->coff_link_hash_flags
414 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
415 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
416 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
417 (*_bfd_error_handler
)
418 ("Warning: symbol `%s' is both section and non-section",
427 if (! (bfd_coff_link_add_one_symbol
428 (info
, abfd
, name
, flags
, section
, value
,
429 (const char *) NULL
, copy
, false,
430 (struct bfd_link_hash_entry
**) sym_hash
)))
434 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
435 (*sym_hash
)->coff_link_hash_flags
|=
436 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
438 /* Limit the alignment of a common symbol to the possible
439 alignment of a section. There is no point to permitting
440 a higher alignment for a common symbol: we can not
441 guarantee it, and it may cause us to allocate extra space
442 in the common section. */
443 if (section
== bfd_com_section_ptr
444 && (*sym_hash
)->root
.type
== bfd_link_hash_common
445 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
446 > bfd_coff_default_section_alignment_power (abfd
)))
447 (*sym_hash
)->root
.u
.c
.p
->alignment_power
448 = bfd_coff_default_section_alignment_power (abfd
);
450 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
452 /* If we don't have any symbol information currently in
453 the hash table, or if we are looking at a symbol
454 definition, then update the symbol class and type in
456 if (((*sym_hash
)->class == C_NULL
457 && (*sym_hash
)->type
== T_NULL
)
460 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
461 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
463 (*sym_hash
)->class = sym
.n_sclass
;
464 if (sym
.n_type
!= T_NULL
)
466 /* We want to warn if the type changed, but not
467 if it changed from an unspecified type.
468 Testing the whole type byte may work, but the
469 change from (e.g.) a function of unspecified
470 type to function of known type also wants to
472 if ((*sym_hash
)->type
!= T_NULL
473 && (*sym_hash
)->type
!= sym
.n_type
474 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
475 && (BTYPE ((*sym_hash
)->type
) == T_NULL
476 || BTYPE (sym
.n_type
) == T_NULL
)))
477 (*_bfd_error_handler
)
478 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
479 name
, (*sym_hash
)->type
, sym
.n_type
,
480 bfd_get_filename (abfd
));
482 /* We don't want to change from a meaningful
483 base type to a null one, but if we know
484 nothing, take what little we might now know. */
485 if (BTYPE (sym
.n_type
) != T_NULL
486 || (*sym_hash
)->type
== T_NULL
)
487 (*sym_hash
)->type
= sym
.n_type
;
489 (*sym_hash
)->auxbfd
= abfd
;
490 if (sym
.n_numaux
!= 0)
492 union internal_auxent
*alloc
;
495 union internal_auxent
*iaux
;
497 (*sym_hash
)->numaux
= sym
.n_numaux
;
498 alloc
= ((union internal_auxent
*)
499 bfd_hash_allocate (&info
->hash
->table
,
501 * sizeof (*alloc
))));
504 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
506 i
++, eaux
+= symesz
, iaux
++)
507 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
508 sym
.n_sclass
, i
, sym
.n_numaux
,
510 (*sym_hash
)->aux
= alloc
;
515 if (classification
== COFF_SYMBOL_PE_SECTION
516 && (*sym_hash
)->numaux
!= 0)
518 /* Some PE sections (such as .bss) have a zero size in
519 the section header, but a non-zero size in the AUX
520 record. Correct that here.
522 FIXME: This is not at all the right place to do this.
523 For example, it won't help objdump. This needs to be
524 done when we swap in the section header. */
526 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
527 if (section
->_raw_size
== 0)
528 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
530 /* FIXME: We could test whether the section sizes
531 matches the size in the aux entry, but apparently
532 that sometimes fails unexpectedly. */
536 esym
+= (sym
.n_numaux
+ 1) * symesz
;
537 sym_hash
+= sym
.n_numaux
+ 1;
540 /* If this is a non-traditional, non-relocateable link, try to
541 optimize the handling of any .stab/.stabstr sections. */
542 if (! info
->relocateable
543 && ! info
->traditional_format
544 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
545 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
547 asection
*stab
, *stabstr
;
549 stab
= bfd_get_section_by_name (abfd
, ".stab");
552 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
556 struct coff_link_hash_table
*table
;
557 struct coff_section_tdata
*secdata
;
559 secdata
= coff_section_data (abfd
, stab
);
563 (PTR
) bfd_zalloc (abfd
,
564 sizeof (struct coff_section_tdata
));
565 if (stab
->used_by_bfd
== NULL
)
567 secdata
= coff_section_data (abfd
, stab
);
570 table
= coff_hash_table (info
);
572 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
574 &secdata
->stab_info
))
580 obj_coff_keep_syms (abfd
) = keep_syms
;
585 obj_coff_keep_syms (abfd
) = keep_syms
;
589 /* Do the final link step. */
592 _bfd_coff_final_link (abfd
, info
)
594 struct bfd_link_info
*info
;
596 bfd_size_type symesz
;
597 struct coff_final_link_info finfo
;
598 boolean debug_merge_allocated
;
599 boolean long_section_names
;
601 struct bfd_link_order
*p
;
602 size_t max_sym_count
;
603 size_t max_lineno_count
;
604 size_t max_reloc_count
;
605 size_t max_output_reloc_count
;
606 size_t max_contents_size
;
607 file_ptr rel_filepos
;
609 file_ptr line_filepos
;
612 bfd_byte
*external_relocs
= NULL
;
613 char strbuf
[STRING_SIZE_SIZE
];
615 symesz
= bfd_coff_symesz (abfd
);
618 finfo
.output_bfd
= abfd
;
620 finfo
.section_info
= NULL
;
621 finfo
.last_file_index
= -1;
622 finfo
.last_bf_index
= -1;
623 finfo
.internal_syms
= NULL
;
624 finfo
.sec_ptrs
= NULL
;
625 finfo
.sym_indices
= NULL
;
626 finfo
.outsyms
= NULL
;
627 finfo
.linenos
= NULL
;
628 finfo
.contents
= NULL
;
629 finfo
.external_relocs
= NULL
;
630 finfo
.internal_relocs
= NULL
;
631 finfo
.global_to_static
= false;
632 debug_merge_allocated
= false;
634 coff_data (abfd
)->link_info
= info
;
636 finfo
.strtab
= _bfd_stringtab_init ();
637 if (finfo
.strtab
== NULL
)
640 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
642 debug_merge_allocated
= true;
644 /* Compute the file positions for all the sections. */
645 if (! abfd
->output_has_begun
)
647 if (! bfd_coff_compute_section_file_positions (abfd
))
651 /* Count the line numbers and relocation entries required for the
652 output file. Set the file positions for the relocs. */
653 rel_filepos
= obj_relocbase (abfd
);
654 relsz
= bfd_coff_relsz (abfd
);
655 max_contents_size
= 0;
656 max_lineno_count
= 0;
659 long_section_names
= false;
660 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
664 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
666 if (p
->type
== bfd_indirect_link_order
)
670 sec
= p
->u
.indirect
.section
;
672 /* Mark all sections which are to be included in the
673 link. This will normally be every section. We need
674 to do this so that we can identify any sections which
675 the linker has decided to not include. */
676 sec
->linker_mark
= true;
678 if (info
->strip
== strip_none
679 || info
->strip
== strip_some
)
680 o
->lineno_count
+= sec
->lineno_count
;
682 if (info
->relocateable
)
683 o
->reloc_count
+= sec
->reloc_count
;
685 if (sec
->_raw_size
> max_contents_size
)
686 max_contents_size
= sec
->_raw_size
;
687 if (sec
->lineno_count
> max_lineno_count
)
688 max_lineno_count
= sec
->lineno_count
;
689 if (sec
->reloc_count
> max_reloc_count
)
690 max_reloc_count
= sec
->reloc_count
;
692 else if (info
->relocateable
693 && (p
->type
== bfd_section_reloc_link_order
694 || p
->type
== bfd_symbol_reloc_link_order
))
697 if (o
->reloc_count
== 0)
701 o
->flags
|= SEC_RELOC
;
702 o
->rel_filepos
= rel_filepos
;
703 rel_filepos
+= o
->reloc_count
* relsz
;
706 if (bfd_coff_long_section_names (abfd
)
707 && strlen (o
->name
) > SCNNMLEN
)
709 /* This section has a long name which must go in the string
710 table. This must correspond to the code in
711 coff_write_object_contents which puts the string index
712 into the s_name field of the section header. That is why
713 we pass hash as false. */
714 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
715 == (bfd_size_type
) -1)
717 long_section_names
= true;
721 /* If doing a relocateable link, allocate space for the pointers we
723 if (info
->relocateable
)
727 /* We use section_count + 1, rather than section_count, because
728 the target_index fields are 1 based. */
730 ((struct coff_link_section_info
*)
731 bfd_malloc ((abfd
->section_count
+ 1)
732 * sizeof (struct coff_link_section_info
)));
733 if (finfo
.section_info
== NULL
)
735 for (i
= 0; i
<= abfd
->section_count
; i
++)
737 finfo
.section_info
[i
].relocs
= NULL
;
738 finfo
.section_info
[i
].rel_hashes
= NULL
;
742 /* We now know the size of the relocs, so we can determine the file
743 positions of the line numbers. */
744 line_filepos
= rel_filepos
;
745 linesz
= bfd_coff_linesz (abfd
);
746 max_output_reloc_count
= 0;
747 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
749 if (o
->lineno_count
== 0)
753 o
->line_filepos
= line_filepos
;
754 line_filepos
+= o
->lineno_count
* linesz
;
757 if (o
->reloc_count
!= 0)
759 /* We don't know the indices of global symbols until we have
760 written out all the local symbols. For each section in
761 the output file, we keep an array of pointers to hash
762 table entries. Each entry in the array corresponds to a
763 reloc. When we find a reloc against a global symbol, we
764 set the corresponding entry in this array so that we can
765 fix up the symbol index after we have written out all the
768 Because of this problem, we also keep the relocs in
769 memory until the end of the link. This wastes memory,
770 but only when doing a relocateable link, which is not the
772 BFD_ASSERT (info
->relocateable
);
773 finfo
.section_info
[o
->target_index
].relocs
=
774 ((struct internal_reloc
*)
775 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
776 finfo
.section_info
[o
->target_index
].rel_hashes
=
777 ((struct coff_link_hash_entry
**)
778 bfd_malloc (o
->reloc_count
779 * sizeof (struct coff_link_hash_entry
*)));
780 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
781 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
784 if (o
->reloc_count
> max_output_reloc_count
)
785 max_output_reloc_count
= o
->reloc_count
;
788 /* Reset the reloc and lineno counts, so that we can use them to
789 count the number of entries we have output so far. */
794 obj_sym_filepos (abfd
) = line_filepos
;
796 /* Figure out the largest number of symbols in an input BFD. Take
797 the opportunity to clear the output_has_begun fields of all the
800 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
804 sub
->output_has_begun
= false;
805 sz
= obj_raw_syment_count (sub
);
806 if (sz
> max_sym_count
)
810 /* Allocate some buffers used while linking. */
811 finfo
.internal_syms
= ((struct internal_syment
*)
812 bfd_malloc (max_sym_count
813 * sizeof (struct internal_syment
)));
814 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
815 * sizeof (asection
*));
816 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
817 finfo
.outsyms
= ((bfd_byte
*)
818 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
819 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
820 * bfd_coff_linesz (abfd
));
821 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
822 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
823 if (! info
->relocateable
)
824 finfo
.internal_relocs
= ((struct internal_reloc
*)
825 bfd_malloc (max_reloc_count
826 * sizeof (struct internal_reloc
)));
827 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
828 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
829 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
830 || finfo
.outsyms
== NULL
831 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
832 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
833 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
834 || (! info
->relocateable
835 && finfo
.internal_relocs
== NULL
836 && max_reloc_count
> 0))
839 /* We now know the position of everything in the file, except that
840 we don't know the size of the symbol table and therefore we don't
841 know where the string table starts. We just build the string
842 table in memory as we go along. We process all the relocations
843 for a single input file at once. */
844 obj_raw_syment_count (abfd
) = 0;
846 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
848 if (! bfd_coff_start_final_link (abfd
, info
))
852 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
854 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
856 if (p
->type
== bfd_indirect_link_order
857 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
858 == bfd_target_coff_flavour
))
860 sub
= p
->u
.indirect
.section
->owner
;
861 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
863 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
865 sub
->output_has_begun
= true;
868 else if (p
->type
== bfd_section_reloc_link_order
869 || p
->type
== bfd_symbol_reloc_link_order
)
871 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
876 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
882 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
885 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
887 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
888 debug_merge_allocated
= false;
890 if (finfo
.internal_syms
!= NULL
)
892 free (finfo
.internal_syms
);
893 finfo
.internal_syms
= NULL
;
895 if (finfo
.sec_ptrs
!= NULL
)
897 free (finfo
.sec_ptrs
);
898 finfo
.sec_ptrs
= NULL
;
900 if (finfo
.sym_indices
!= NULL
)
902 free (finfo
.sym_indices
);
903 finfo
.sym_indices
= NULL
;
905 if (finfo
.linenos
!= NULL
)
907 free (finfo
.linenos
);
908 finfo
.linenos
= NULL
;
910 if (finfo
.contents
!= NULL
)
912 free (finfo
.contents
);
913 finfo
.contents
= NULL
;
915 if (finfo
.external_relocs
!= NULL
)
917 free (finfo
.external_relocs
);
918 finfo
.external_relocs
= NULL
;
920 if (finfo
.internal_relocs
!= NULL
)
922 free (finfo
.internal_relocs
);
923 finfo
.internal_relocs
= NULL
;
926 /* The value of the last C_FILE symbol is supposed to be the symbol
927 index of the first external symbol. Write it out again if
929 if (finfo
.last_file_index
!= -1
930 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
932 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
933 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
934 (PTR
) finfo
.outsyms
);
936 (obj_sym_filepos (abfd
)
937 + finfo
.last_file_index
* symesz
),
939 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
943 /* If doing task linking (ld --task-link) then make a pass through the
944 global symbols, writing out any that are defined, and making them
948 finfo
.failed
= false;
949 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
955 /* Write out the global symbols. */
956 finfo
.failed
= false;
957 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
962 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
963 if (finfo
.outsyms
!= NULL
)
965 free (finfo
.outsyms
);
966 finfo
.outsyms
= NULL
;
969 if (info
->relocateable
&& max_output_reloc_count
> 0)
971 /* Now that we have written out all the global symbols, we know
972 the symbol indices to use for relocs against them, and we can
973 finally write out the relocs. */
974 external_relocs
= ((bfd_byte
*)
975 bfd_malloc (max_output_reloc_count
* relsz
));
976 if (external_relocs
== NULL
)
979 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
981 struct internal_reloc
*irel
;
982 struct internal_reloc
*irelend
;
983 struct coff_link_hash_entry
**rel_hash
;
986 if (o
->reloc_count
== 0)
989 irel
= finfo
.section_info
[o
->target_index
].relocs
;
990 irelend
= irel
+ o
->reloc_count
;
991 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
992 erel
= external_relocs
;
993 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
995 if (*rel_hash
!= NULL
)
997 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
998 irel
->r_symndx
= (*rel_hash
)->indx
;
1000 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1003 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1004 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
1005 abfd
) != relsz
* o
->reloc_count
)
1009 free (external_relocs
);
1010 external_relocs
= NULL
;
1013 /* Free up the section information. */
1014 if (finfo
.section_info
!= NULL
)
1018 for (i
= 0; i
< abfd
->section_count
; i
++)
1020 if (finfo
.section_info
[i
].relocs
!= NULL
)
1021 free (finfo
.section_info
[i
].relocs
);
1022 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1023 free (finfo
.section_info
[i
].rel_hashes
);
1025 free (finfo
.section_info
);
1026 finfo
.section_info
= NULL
;
1029 /* If we have optimized stabs strings, output them. */
1030 if (coff_hash_table (info
)->stab_info
!= NULL
)
1032 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1036 /* Write out the string table. */
1037 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1040 (obj_sym_filepos (abfd
)
1041 + obj_raw_syment_count (abfd
) * symesz
),
1045 #if STRING_SIZE_SIZE == 4
1047 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1048 (bfd_byte
*) strbuf
);
1050 #error Change bfd_h_put_32
1053 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1056 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1060 _bfd_stringtab_free (finfo
.strtab
);
1062 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1063 not try to write out the symbols. */
1064 bfd_get_symcount (abfd
) = 0;
1069 if (debug_merge_allocated
)
1070 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1071 if (finfo
.strtab
!= NULL
)
1072 _bfd_stringtab_free (finfo
.strtab
);
1073 if (finfo
.section_info
!= NULL
)
1077 for (i
= 0; i
< abfd
->section_count
; i
++)
1079 if (finfo
.section_info
[i
].relocs
!= NULL
)
1080 free (finfo
.section_info
[i
].relocs
);
1081 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1082 free (finfo
.section_info
[i
].rel_hashes
);
1084 free (finfo
.section_info
);
1086 if (finfo
.internal_syms
!= NULL
)
1087 free (finfo
.internal_syms
);
1088 if (finfo
.sec_ptrs
!= NULL
)
1089 free (finfo
.sec_ptrs
);
1090 if (finfo
.sym_indices
!= NULL
)
1091 free (finfo
.sym_indices
);
1092 if (finfo
.outsyms
!= NULL
)
1093 free (finfo
.outsyms
);
1094 if (finfo
.linenos
!= NULL
)
1095 free (finfo
.linenos
);
1096 if (finfo
.contents
!= NULL
)
1097 free (finfo
.contents
);
1098 if (finfo
.external_relocs
!= NULL
)
1099 free (finfo
.external_relocs
);
1100 if (finfo
.internal_relocs
!= NULL
)
1101 free (finfo
.internal_relocs
);
1102 if (external_relocs
!= NULL
)
1103 free (external_relocs
);
1107 /* parse out a -heap <reserved>,<commit> line */
1110 dores_com (ptr
, output_bfd
, heap
)
1115 if (coff_data(output_bfd
)->pe
)
1117 int val
= strtoul (ptr
, &ptr
, 0);
1119 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1121 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1125 int val
= strtoul (ptr
+1, &ptr
, 0);
1127 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1129 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1135 static char *get_name(ptr
, dst
)
1142 while (*ptr
&& *ptr
!= ' ')
1148 /* Process any magic embedded commands in a section called .drectve */
1151 process_embedded_commands (output_bfd
, info
, abfd
)
1153 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1156 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1163 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1166 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1171 e
= copy
+ sec
->_raw_size
;
1172 for (s
= copy
; s
< e
; )
1178 if (strncmp (s
,"-attr", 5) == 0)
1190 s
= get_name(s
, &name
);
1191 s
= get_name(s
, &attribs
);
1211 asec
= bfd_get_section_by_name (abfd
, name
);
1214 asec
->flags
|= SEC_CODE
;
1216 asec
->flags
|= SEC_READONLY
;
1219 else if (strncmp (s
,"-heap", 5) == 0)
1221 s
= dores_com (s
+5, output_bfd
, 1);
1223 else if (strncmp (s
,"-stack", 6) == 0)
1225 s
= dores_com (s
+6, output_bfd
, 0);
1234 /* Place a marker against all symbols which are used by relocations.
1235 This marker can be picked up by the 'do we skip this symbol ?'
1236 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1241 mark_relocs (finfo
, input_bfd
)
1242 struct coff_final_link_info
* finfo
;
1247 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1250 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1252 struct internal_reloc
* internal_relocs
;
1253 struct internal_reloc
* irel
;
1254 struct internal_reloc
* irelend
;
1257 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1260 /* Read in the relocs. */
1261 internal_relocs
= _bfd_coff_read_internal_relocs
1262 (input_bfd
, a
, false,
1263 finfo
->external_relocs
,
1264 finfo
->info
->relocateable
,
1265 (finfo
->info
->relocateable
1266 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1267 : finfo
->internal_relocs
)
1270 if (internal_relocs
== NULL
)
1273 irel
= internal_relocs
;
1274 irelend
= irel
+ a
->reloc_count
;
1276 /* Place a mark in the sym_indices array (whose entries have
1277 been initialised to 0) for all of the symbols that are used
1278 in the relocation table. This will then be picked up in the
1281 for (; irel
< irelend
; irel
++)
1283 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1288 /* Link an input file into the linker output file. This function
1289 handles all the sections and relocations of the input file at once. */
1292 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1293 struct coff_final_link_info
*finfo
;
1296 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1297 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1299 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1301 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1302 asection
*, struct internal_reloc
*,
1305 const char *strings
;
1306 bfd_size_type syment_base
;
1308 bfd_size_type isymesz
;
1309 bfd_size_type osymesz
;
1310 bfd_size_type linesz
;
1313 struct internal_syment
*isymp
;
1316 unsigned long output_index
;
1318 struct coff_link_hash_entry
**sym_hash
;
1321 /* Move all the symbols to the output file. */
1323 output_bfd
= finfo
->output_bfd
;
1325 syment_base
= obj_raw_syment_count (output_bfd
);
1326 isymesz
= bfd_coff_symesz (input_bfd
);
1327 osymesz
= bfd_coff_symesz (output_bfd
);
1328 linesz
= bfd_coff_linesz (input_bfd
);
1329 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1332 if (! finfo
->info
->keep_memory
)
1335 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1338 if (! _bfd_coff_get_external_symbols (input_bfd
))
1341 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1342 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1343 isymp
= finfo
->internal_syms
;
1344 secpp
= finfo
->sec_ptrs
;
1345 indexp
= finfo
->sym_indices
;
1346 output_index
= syment_base
;
1347 outsym
= finfo
->outsyms
;
1349 if (coff_data (output_bfd
)->pe
)
1351 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1355 /* If we are going to perform relocations and also strip/discard some symbols
1356 then we must make sure that we do not strip/discard those symbols that are
1357 going to be involved in the relocations */
1358 if (( finfo
->info
->strip
!= strip_none
1359 || finfo
->info
->discard
!= discard_none
)
1360 && finfo
->info
->relocateable
)
1362 /* mark the symbol array as 'not-used' */
1363 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1365 mark_relocs (finfo
, input_bfd
);
1368 while (esym
< esym_end
)
1370 struct internal_syment isym
;
1371 enum coff_symbol_classification classification
;
1374 boolean dont_skip_symbol
;
1377 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1379 /* Make a copy of *isymp so that the relocate_section function
1380 always sees the original values. This is more reliable than
1381 always recomputing the symbol value even if we are stripping
1385 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1386 switch (classification
)
1390 case COFF_SYMBOL_GLOBAL
:
1391 case COFF_SYMBOL_PE_SECTION
:
1392 case COFF_SYMBOL_LOCAL
:
1393 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1395 case COFF_SYMBOL_COMMON
:
1396 *secpp
= bfd_com_section_ptr
;
1398 case COFF_SYMBOL_UNDEFINED
:
1399 *secpp
= bfd_und_section_ptr
;
1403 /* Extract the flag indicating if this symbol is used by a
1405 if ((finfo
->info
->strip
!= strip_none
1406 || finfo
->info
->discard
!= discard_none
)
1407 && finfo
->info
->relocateable
)
1408 dont_skip_symbol
= *indexp
;
1410 dont_skip_symbol
= false;
1416 add
= 1 + isym
.n_numaux
;
1418 /* If we are stripping all symbols, we want to skip this one. */
1419 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1424 switch (classification
)
1428 case COFF_SYMBOL_GLOBAL
:
1429 case COFF_SYMBOL_COMMON
:
1430 case COFF_SYMBOL_PE_SECTION
:
1431 /* This is a global symbol. Global symbols come at the
1432 end of the symbol table, so skip them for now.
1433 Locally defined function symbols, however, are an
1434 exception, and are not moved to the end. */
1436 if (! ISFCN (isym
.n_type
))
1440 case COFF_SYMBOL_UNDEFINED
:
1441 /* Undefined symbols are left for the end. */
1446 case COFF_SYMBOL_LOCAL
:
1447 /* This is a local symbol. Skip it if we are discarding
1449 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1455 /* If we stripping debugging symbols, and this is a debugging
1456 symbol, then skip it. FIXME: gas sets the section to N_ABS
1457 for some types of debugging symbols; I don't know if this is
1458 a bug or not. In any case, we handle it here. */
1460 && finfo
->info
->strip
== strip_debugger
1461 && ! dont_skip_symbol
1462 && (isym
.n_scnum
== N_DEBUG
1463 || (isym
.n_scnum
== N_ABS
1464 && (isym
.n_sclass
== C_AUTO
1465 || isym
.n_sclass
== C_REG
1466 || isym
.n_sclass
== C_MOS
1467 || isym
.n_sclass
== C_MOE
1468 || isym
.n_sclass
== C_MOU
1469 || isym
.n_sclass
== C_ARG
1470 || isym
.n_sclass
== C_REGPARM
1471 || isym
.n_sclass
== C_FIELD
1472 || isym
.n_sclass
== C_EOS
))))
1475 /* If some symbols are stripped based on the name, work out the
1476 name and decide whether to skip this symbol. */
1478 && (finfo
->info
->strip
== strip_some
1479 || finfo
->info
->discard
== discard_l
))
1482 char buf
[SYMNMLEN
+ 1];
1484 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1488 if (! dont_skip_symbol
1489 && ((finfo
->info
->strip
== strip_some
1490 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1493 && finfo
->info
->discard
== discard_l
1494 && bfd_is_local_label_name (input_bfd
, name
))))
1498 /* If this is an enum, struct, or union tag, see if we have
1499 already output an identical type. */
1501 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1502 && (isym
.n_sclass
== C_ENTAG
1503 || isym
.n_sclass
== C_STRTAG
1504 || isym
.n_sclass
== C_UNTAG
)
1505 && isym
.n_numaux
== 1)
1508 char buf
[SYMNMLEN
+ 1];
1509 struct coff_debug_merge_hash_entry
*mh
;
1510 struct coff_debug_merge_type
*mt
;
1511 union internal_auxent aux
;
1512 struct coff_debug_merge_element
**epp
;
1513 bfd_byte
*esl
, *eslend
;
1514 struct internal_syment
*islp
;
1516 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1520 /* Ignore fake names invented by compiler; treat them all as
1522 if (*name
== '~' || *name
== '.' || *name
== '$'
1523 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1524 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1527 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1532 /* Allocate memory to hold type information. If this turns
1533 out to be a duplicate, we pass this address to
1535 mt
= ((struct coff_debug_merge_type
*)
1536 bfd_alloc (input_bfd
,
1537 sizeof (struct coff_debug_merge_type
)));
1540 mt
->class = isym
.n_sclass
;
1542 /* Pick up the aux entry, which points to the end of the tag
1544 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1545 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1548 /* Gather the elements. */
1549 epp
= &mt
->elements
;
1550 mt
->elements
= NULL
;
1552 esl
= esym
+ 2 * isymesz
;
1553 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1554 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1555 while (esl
< eslend
)
1557 const char *elename
;
1558 char elebuf
[SYMNMLEN
+ 1];
1561 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1563 *epp
= ((struct coff_debug_merge_element
*)
1564 bfd_alloc (input_bfd
,
1565 sizeof (struct coff_debug_merge_element
)));
1569 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1571 if (elename
== NULL
)
1574 name_copy
= (char *) bfd_alloc (input_bfd
,
1575 strlen (elename
) + 1);
1576 if (name_copy
== NULL
)
1578 strcpy (name_copy
, elename
);
1580 (*epp
)->name
= name_copy
;
1581 (*epp
)->type
= islp
->n_type
;
1583 if (islp
->n_numaux
>= 1
1584 && islp
->n_type
!= T_NULL
1585 && islp
->n_sclass
!= C_EOS
)
1587 union internal_auxent eleaux
;
1590 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1591 islp
->n_type
, islp
->n_sclass
, 0,
1592 islp
->n_numaux
, (PTR
) &eleaux
);
1593 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1595 /* FIXME: If this tagndx entry refers to a symbol
1596 defined later in this file, we just ignore it.
1597 Handling this correctly would be tedious, and may
1603 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1606 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1607 if ((*epp
)->tagndx
< 0)
1611 epp
= &(*epp
)->next
;
1614 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1615 islp
+= islp
->n_numaux
+ 1;
1618 /* See if we already have a definition which matches this
1619 type. We always output the type if it has no elements,
1621 if (mt
->elements
== NULL
)
1622 bfd_release (input_bfd
, (PTR
) mt
);
1625 struct coff_debug_merge_type
*mtl
;
1627 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1629 struct coff_debug_merge_element
*me
, *mel
;
1631 if (mtl
->class != mt
->class)
1634 for (me
= mt
->elements
, mel
= mtl
->elements
;
1635 me
!= NULL
&& mel
!= NULL
;
1636 me
= me
->next
, mel
= mel
->next
)
1638 if (strcmp (me
->name
, mel
->name
) != 0
1639 || me
->type
!= mel
->type
1640 || me
->tagndx
!= mel
->tagndx
)
1644 if (me
== NULL
&& mel
== NULL
)
1648 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1650 /* This is the first definition of this type. */
1651 mt
->indx
= output_index
;
1652 mt
->next
= mh
->types
;
1657 /* This is a redefinition which can be merged. */
1658 bfd_release (input_bfd
, (PTR
) mt
);
1659 *indexp
= mtl
->indx
;
1660 add
= (eslend
- esym
) / isymesz
;
1666 /* We now know whether we are to skip this symbol or not. */
1669 /* Adjust the symbol in order to output it. */
1671 if (isym
._n
._n_n
._n_zeroes
== 0
1672 && isym
._n
._n_n
._n_offset
!= 0)
1677 /* This symbol has a long name. Enter it in the string
1678 table we are building. Note that we do not check
1679 bfd_coff_symname_in_debug. That is only true for
1680 XCOFF, and XCOFF requires different linking code
1682 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1686 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1687 if (indx
== (bfd_size_type
) -1)
1689 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1692 switch (isym
.n_sclass
)
1708 /* The symbol value should not be modified. */
1712 if (obj_pe (input_bfd
)
1713 && strcmp (isym
.n_name
, ".bf") != 0
1714 && isym
.n_scnum
> 0)
1716 /* For PE, .lf and .ef get their value left alone,
1717 while .bf gets relocated. However, they all have
1718 "real" section numbers, and need to be moved into
1720 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1725 case C_LABEL
: /* Not completely sure about these 2 */
1734 /* Compute new symbol location. */
1735 if (isym
.n_scnum
> 0)
1737 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1738 isym
.n_value
+= (*secpp
)->output_offset
;
1739 if (! obj_pe (input_bfd
))
1740 isym
.n_value
-= (*secpp
)->vma
;
1741 if (! obj_pe (finfo
->output_bfd
))
1742 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1747 /* The value of a C_FILE symbol is the symbol index of
1748 the next C_FILE symbol. The value of the last C_FILE
1749 symbol is the symbol index to the first external
1750 symbol (actually, coff_renumber_symbols does not get
1751 this right--it just sets the value of the last C_FILE
1752 symbol to zero--and nobody has ever complained about
1753 it). We try to get this right, below, just before we
1754 write the symbols out, but in the general case we may
1755 have to write the symbol out twice. */
1757 if (finfo
->last_file_index
!= -1
1758 && finfo
->last_file
.n_value
!= (long) output_index
)
1760 /* We must correct the value of the last C_FILE
1762 finfo
->last_file
.n_value
= output_index
;
1763 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1765 /* The last C_FILE symbol is in this input file. */
1766 bfd_coff_swap_sym_out (output_bfd
,
1767 (PTR
) &finfo
->last_file
,
1768 (PTR
) (finfo
->outsyms
1769 + ((finfo
->last_file_index
1775 /* We have already written out the last C_FILE
1776 symbol. We need to write it out again. We
1777 borrow *outsym temporarily. */
1778 bfd_coff_swap_sym_out (output_bfd
,
1779 (PTR
) &finfo
->last_file
,
1781 if (bfd_seek (output_bfd
,
1782 (obj_sym_filepos (output_bfd
)
1783 + finfo
->last_file_index
* osymesz
),
1785 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1791 finfo
->last_file_index
= output_index
;
1792 finfo
->last_file
= isym
;
1796 /* If doing task linking, convert normal global function symbols to
1797 static functions. */
1799 if (finfo
->info
->task_link
1800 && (isym
.n_sclass
== C_EXT
1801 || isym
.n_sclass
== C_WEAKEXT
1802 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)))
1803 isym
.n_sclass
= C_STAT
;
1805 /* Output the symbol. */
1807 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1809 *indexp
= output_index
;
1814 struct coff_link_hash_entry
*h
;
1816 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1818 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1821 /* This can happen if there were errors earlier in
1823 bfd_set_error (bfd_error_bad_value
);
1826 h
->indx
= output_index
;
1829 output_index
+= add
;
1830 outsym
+= add
* osymesz
;
1833 esym
+= add
* isymesz
;
1837 for (--add
; add
> 0; --add
)
1844 /* Fix up the aux entries. This must be done in a separate pass,
1845 because we don't know the correct symbol indices until we have
1846 already decided which symbols we are going to keep. */
1848 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1849 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1850 isymp
= finfo
->internal_syms
;
1851 indexp
= finfo
->sym_indices
;
1852 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1853 outsym
= finfo
->outsyms
;
1854 while (esym
< esym_end
)
1858 add
= 1 + isymp
->n_numaux
;
1861 || (bfd_size_type
) *indexp
< syment_base
)
1862 && (*sym_hash
== NULL
1863 || (*sym_hash
)->auxbfd
!= input_bfd
))
1864 esym
+= add
* isymesz
;
1867 struct coff_link_hash_entry
*h
;
1875 /* The m68k-motorola-sysv assembler will sometimes
1876 generate two symbols with the same name, but only one
1877 will have aux entries. */
1878 BFD_ASSERT (isymp
->n_numaux
== 0
1879 || h
->numaux
== isymp
->n_numaux
);
1887 /* Handle the aux entries. This handling is based on
1888 coff_pointerize_aux. I don't know if it always correct. */
1889 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1891 union internal_auxent aux
;
1892 union internal_auxent
*auxp
;
1898 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1899 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1904 if (isymp
->n_sclass
== C_FILE
)
1906 /* If this is a long filename, we must put it in the
1908 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1909 && auxp
->x_file
.x_n
.x_offset
!= 0)
1911 const char *filename
;
1914 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1915 >= STRING_SIZE_SIZE
);
1916 if (strings
== NULL
)
1918 strings
= _bfd_coff_read_string_table (input_bfd
);
1919 if (strings
== NULL
)
1922 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1923 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1925 if (indx
== (bfd_size_type
) -1)
1927 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1930 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1934 if (ISFCN (isymp
->n_type
)
1935 || ISTAG (isymp
->n_sclass
)
1936 || isymp
->n_sclass
== C_BLOCK
1937 || isymp
->n_sclass
== C_FCN
)
1939 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1941 && indx
< obj_raw_syment_count (input_bfd
))
1943 /* We look forward through the symbol for
1944 the index of the next symbol we are going
1945 to include. I don't know if this is
1947 while ((finfo
->sym_indices
[indx
] < 0
1948 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1950 && indx
< obj_raw_syment_count (input_bfd
))
1952 if (indx
>= obj_raw_syment_count (input_bfd
))
1953 indx
= output_index
;
1955 indx
= finfo
->sym_indices
[indx
];
1956 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1960 indx
= auxp
->x_sym
.x_tagndx
.l
;
1961 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1965 symindx
= finfo
->sym_indices
[indx
];
1967 auxp
->x_sym
.x_tagndx
.l
= 0;
1969 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1972 /* The .bf symbols are supposed to be linked through
1973 the endndx field. We need to carry this list
1974 across object files. */
1977 && isymp
->n_sclass
== C_FCN
1978 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1979 || isymp
->_n
._n_n
._n_offset
== 0)
1980 && isymp
->_n
._n_name
[0] == '.'
1981 && isymp
->_n
._n_name
[1] == 'b'
1982 && isymp
->_n
._n_name
[2] == 'f'
1983 && isymp
->_n
._n_name
[3] == '\0')
1985 if (finfo
->last_bf_index
!= -1)
1987 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1990 if ((bfd_size_type
) finfo
->last_bf_index
1995 /* The last .bf symbol is in this input
1996 file. This will only happen if the
1997 assembler did not set up the .bf
1998 endndx symbols correctly. */
1999 auxout
= (PTR
) (finfo
->outsyms
2000 + ((finfo
->last_bf_index
2003 bfd_coff_swap_aux_out (output_bfd
,
2004 (PTR
) &finfo
->last_bf
,
2012 /* We have already written out the last
2013 .bf aux entry. We need to write it
2014 out again. We borrow *outsym
2015 temporarily. FIXME: This case should
2017 bfd_coff_swap_aux_out (output_bfd
,
2018 (PTR
) &finfo
->last_bf
,
2023 if (bfd_seek (output_bfd
,
2024 (obj_sym_filepos (output_bfd
)
2025 + finfo
->last_bf_index
* osymesz
),
2027 || bfd_write (outsym
, osymesz
, 1,
2028 output_bfd
) != osymesz
)
2033 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2034 finfo
->last_bf_index
= -1;
2037 /* The endndx field of this aux entry must
2038 be updated with the symbol number of the
2040 finfo
->last_bf
= *auxp
;
2041 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2050 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
2051 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2065 /* Relocate the line numbers, unless we are stripping them. */
2066 if (finfo
->info
->strip
== strip_none
2067 || finfo
->info
->strip
== strip_some
)
2069 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2077 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2078 build_link_order in ldwrite.c will not have created a
2079 link order, which means that we will not have seen this
2080 input section in _bfd_coff_final_link, which means that
2081 we will not have allocated space for the line numbers of
2082 this section. I don't think line numbers can be
2083 meaningful for a section which does not have
2084 SEC_HAS_CONTENTS set, but, if they do, this must be
2086 if (o
->lineno_count
== 0
2087 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2090 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2091 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2092 input_bfd
) != linesz
* o
->lineno_count
)
2095 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2096 eline
= finfo
->linenos
;
2097 oeline
= finfo
->linenos
;
2098 elineend
= eline
+ linesz
* o
->lineno_count
;
2100 for (; eline
< elineend
; eline
+= linesz
)
2102 struct internal_lineno iline
;
2104 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2106 if (iline
.l_lnno
!= 0)
2107 iline
.l_addr
.l_paddr
+= offset
;
2108 else if (iline
.l_addr
.l_symndx
>= 0
2109 && ((unsigned long) iline
.l_addr
.l_symndx
2110 < obj_raw_syment_count (input_bfd
)))
2114 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2118 /* These line numbers are attached to a symbol
2119 which we are stripping. We must discard the
2120 line numbers because reading them back with
2121 no associated symbol (or associating them all
2122 with symbol #0) will fail. We can't regain
2123 the space in the output file, but at least
2129 struct internal_syment is
;
2130 union internal_auxent ia
;
2132 /* Fix up the lnnoptr field in the aux entry of
2133 the symbol. It turns out that we can't do
2134 this when we modify the symbol aux entries,
2135 because gas sometimes screws up the lnnoptr
2136 field and makes it an offset from the start
2137 of the line numbers rather than an absolute
2139 bfd_coff_swap_sym_in (output_bfd
,
2140 (PTR
) (finfo
->outsyms
2141 + ((indx
- syment_base
)
2144 if ((ISFCN (is
.n_type
)
2145 || is
.n_sclass
== C_BLOCK
)
2146 && is
.n_numaux
>= 1)
2150 auxptr
= (PTR
) (finfo
->outsyms
2151 + ((indx
- syment_base
+ 1)
2153 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2154 is
.n_type
, is
.n_sclass
,
2155 0, is
.n_numaux
, (PTR
) &ia
);
2156 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2157 (o
->output_section
->line_filepos
2158 + o
->output_section
->lineno_count
* linesz
2159 + eline
- finfo
->linenos
);
2160 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2161 is
.n_type
, is
.n_sclass
, 0,
2162 is
.n_numaux
, auxptr
);
2168 iline
.l_addr
.l_symndx
= indx
;
2173 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
,
2179 if (bfd_seek (output_bfd
,
2180 (o
->output_section
->line_filepos
2181 + o
->output_section
->lineno_count
* linesz
),
2183 || (bfd_write (finfo
->linenos
, 1, oeline
- finfo
->linenos
,
2185 != (bfd_size_type
) (oeline
- finfo
->linenos
)))
2188 o
->output_section
->lineno_count
+=
2189 (oeline
- finfo
->linenos
) / linesz
;
2193 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2194 symbol will be the first symbol in the next input file. In the
2195 normal case, this will save us from writing out the C_FILE symbol
2197 if (finfo
->last_file_index
!= -1
2198 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2200 finfo
->last_file
.n_value
= output_index
;
2201 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2202 (PTR
) (finfo
->outsyms
2203 + ((finfo
->last_file_index
- syment_base
)
2207 /* Write the modified symbols to the output file. */
2208 if (outsym
> finfo
->outsyms
)
2210 if (bfd_seek (output_bfd
,
2211 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2213 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2215 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2218 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2219 + (outsym
- finfo
->outsyms
) / osymesz
)
2222 obj_raw_syment_count (output_bfd
) = output_index
;
2225 /* Relocate the contents of each section. */
2226 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2227 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2230 struct coff_section_tdata
*secdata
;
2232 if (! o
->linker_mark
)
2234 /* This section was omitted from the link. */
2238 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2239 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2241 if ((o
->flags
& SEC_RELOC
) != 0
2242 && o
->reloc_count
!= 0)
2244 ((*_bfd_error_handler
)
2245 (_("%s: relocs in section `%s', but it has no contents"),
2246 bfd_get_filename (input_bfd
),
2247 bfd_get_section_name (input_bfd
, o
)));
2248 bfd_set_error (bfd_error_no_contents
);
2255 secdata
= coff_section_data (input_bfd
, o
);
2256 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2257 contents
= secdata
->contents
;
2260 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2261 (file_ptr
) 0, o
->_raw_size
))
2263 contents
= finfo
->contents
;
2266 if ((o
->flags
& SEC_RELOC
) != 0)
2269 struct internal_reloc
*internal_relocs
;
2270 struct internal_reloc
*irel
;
2272 /* Read in the relocs. */
2273 target_index
= o
->output_section
->target_index
;
2274 internal_relocs
= (_bfd_coff_read_internal_relocs
2275 (input_bfd
, o
, false, finfo
->external_relocs
,
2276 finfo
->info
->relocateable
,
2277 (finfo
->info
->relocateable
2278 ? (finfo
->section_info
[target_index
].relocs
2279 + o
->output_section
->reloc_count
)
2280 : finfo
->internal_relocs
)));
2281 if (internal_relocs
== NULL
)
2284 /* Call processor specific code to relocate the section
2286 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2290 finfo
->internal_syms
,
2294 if (finfo
->info
->relocateable
)
2297 struct internal_reloc
*irelend
;
2298 struct coff_link_hash_entry
**rel_hash
;
2300 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2301 irel
= internal_relocs
;
2302 irelend
= irel
+ o
->reloc_count
;
2303 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2304 + o
->output_section
->reloc_count
);
2305 for (; irel
< irelend
; irel
++, rel_hash
++)
2307 struct coff_link_hash_entry
*h
;
2312 /* Adjust the reloc address and symbol index. */
2314 irel
->r_vaddr
+= offset
;
2316 if (irel
->r_symndx
== -1)
2321 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2329 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2332 /* This is a global symbol. */
2334 irel
->r_symndx
= h
->indx
;
2337 /* This symbol is being written at the end
2338 of the file, and we do not yet know the
2339 symbol index. We save the pointer to the
2340 hash table entry in the rel_hash list.
2341 We set the indx field to -2 to indicate
2342 that this symbol must not be stripped. */
2351 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2353 irel
->r_symndx
= indx
;
2356 struct internal_syment
*is
;
2358 char buf
[SYMNMLEN
+ 1];
2360 /* This reloc is against a symbol we are
2361 stripping. This should have been handled
2362 by the 'dont_skip_symbol' code in the while
2363 loop at the top of this function. */
2365 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2367 name
= (_bfd_coff_internal_syment_name
2368 (input_bfd
, is
, buf
));
2372 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2373 (finfo
->info
, name
, input_bfd
, o
,
2380 o
->output_section
->reloc_count
+= o
->reloc_count
;
2384 /* Write out the modified section contents. */
2385 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2387 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2388 contents
, o
->output_offset
,
2389 (o
->_cooked_size
!= 0
2396 if (! (_bfd_write_section_stabs
2397 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2398 o
, &secdata
->stab_info
, contents
)))
2403 if (! finfo
->info
->keep_memory
)
2405 if (! _bfd_coff_free_symbols (input_bfd
))
2412 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2415 _bfd_coff_write_global_sym (h
, data
)
2416 struct coff_link_hash_entry
*h
;
2419 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2421 struct internal_syment isym
;
2422 bfd_size_type symesz
;
2425 output_bfd
= finfo
->output_bfd
;
2431 && (finfo
->info
->strip
== strip_all
2432 || (finfo
->info
->strip
== strip_some
2433 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2434 h
->root
.root
.string
, false, false)
2438 switch (h
->root
.type
)
2441 case bfd_link_hash_new
:
2445 case bfd_link_hash_undefined
:
2446 case bfd_link_hash_undefweak
:
2447 isym
.n_scnum
= N_UNDEF
;
2451 case bfd_link_hash_defined
:
2452 case bfd_link_hash_defweak
:
2456 sec
= h
->root
.u
.def
.section
->output_section
;
2457 if (bfd_is_abs_section (sec
))
2458 isym
.n_scnum
= N_ABS
;
2460 isym
.n_scnum
= sec
->target_index
;
2461 isym
.n_value
= (h
->root
.u
.def
.value
2462 + h
->root
.u
.def
.section
->output_offset
);
2463 if (! obj_pe (finfo
->output_bfd
))
2464 isym
.n_value
+= sec
->vma
;
2468 case bfd_link_hash_common
:
2469 isym
.n_scnum
= N_UNDEF
;
2470 isym
.n_value
= h
->root
.u
.c
.size
;
2473 case bfd_link_hash_indirect
:
2474 case bfd_link_hash_warning
:
2475 /* Just ignore these. They can't be handled anyhow. */
2479 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2480 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2487 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2489 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2491 if (indx
== (bfd_size_type
) -1)
2493 finfo
->failed
= true;
2496 isym
._n
._n_n
._n_zeroes
= 0;
2497 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2500 isym
.n_sclass
= h
->class;
2501 isym
.n_type
= h
->type
;
2503 if (isym
.n_sclass
== C_NULL
)
2504 isym
.n_sclass
= C_EXT
;
2506 /* If doing task linking and this is the pass where we convert
2507 defined globals to statics, then do that conversion now. If the
2508 symbol is not being converted, just ignore it and it will be
2509 output during a later pass. */
2510 if (finfo
->global_to_static
)
2512 if (isym
.n_sclass
!= C_EXT
2513 && isym
.n_sclass
!= C_WEAKEXT
2514 && (! obj_pe (output_bfd
) || isym
.n_sclass
!= C_NT_WEAK
))
2518 isym
.n_sclass
= C_STAT
;
2521 isym
.n_numaux
= h
->numaux
;
2523 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2525 symesz
= bfd_coff_symesz (output_bfd
);
2527 if (bfd_seek (output_bfd
,
2528 (obj_sym_filepos (output_bfd
)
2529 + obj_raw_syment_count (output_bfd
) * symesz
),
2531 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2533 finfo
->failed
= true;
2537 h
->indx
= obj_raw_syment_count (output_bfd
);
2539 ++obj_raw_syment_count (output_bfd
);
2541 /* Write out any associated aux entries. Most of the aux entries
2542 will have been modified in _bfd_coff_link_input_bfd. We have to
2543 handle section aux entries here, now that we have the final
2544 relocation and line number counts. */
2545 for (i
= 0; i
< isym
.n_numaux
; i
++)
2547 union internal_auxent
*auxp
;
2551 /* Look for a section aux entry here using the same tests that
2552 coff_swap_aux_out uses. */
2554 && (isym
.n_sclass
== C_STAT
2555 || isym
.n_sclass
== C_HIDDEN
)
2556 && isym
.n_type
== T_NULL
2557 && (h
->root
.type
== bfd_link_hash_defined
2558 || h
->root
.type
== bfd_link_hash_defweak
))
2562 sec
= h
->root
.u
.def
.section
->output_section
;
2565 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2569 /* For PE, an overflow on the final link reportedly does
2570 not matter. FIXME: Why not? */
2572 if (sec
->reloc_count
> 0xffff
2573 && (! obj_pe (output_bfd
)
2574 || finfo
->info
->relocateable
))
2575 (*_bfd_error_handler
)
2576 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2577 bfd_get_filename (output_bfd
),
2578 bfd_get_section_name (output_bfd
, sec
),
2581 if (sec
->lineno_count
> 0xffff
2582 && (! obj_pe (output_bfd
)
2583 || finfo
->info
->relocateable
))
2584 (*_bfd_error_handler
)
2585 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2586 bfd_get_filename (output_bfd
),
2587 bfd_get_section_name (output_bfd
, sec
),
2590 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2591 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2592 auxp
->x_scn
.x_checksum
= 0;
2593 auxp
->x_scn
.x_associated
= 0;
2594 auxp
->x_scn
.x_comdat
= 0;
2598 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isym
.n_type
,
2599 isym
.n_sclass
, i
, isym
.n_numaux
,
2600 (PTR
) finfo
->outsyms
);
2601 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2603 finfo
->failed
= true;
2606 ++obj_raw_syment_count (output_bfd
);
2612 /* Write out task global symbols, converting them to statics. Called
2613 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2614 the dirty work, if the symbol we are processing needs conversion. */
2617 _bfd_coff_write_task_globals (h
, data
)
2618 struct coff_link_hash_entry
*h
;
2621 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2622 boolean rtnval
= true;
2623 boolean save_global_to_static
;
2627 switch (h
->root
.type
)
2629 case bfd_link_hash_defined
:
2630 case bfd_link_hash_defweak
:
2631 save_global_to_static
= finfo
->global_to_static
;
2632 finfo
->global_to_static
= true;
2633 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2634 finfo
->global_to_static
= save_global_to_static
;
2643 /* Handle a link order which is supposed to generate a reloc. */
2646 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2648 struct coff_final_link_info
*finfo
;
2649 asection
*output_section
;
2650 struct bfd_link_order
*link_order
;
2652 reloc_howto_type
*howto
;
2653 struct internal_reloc
*irel
;
2654 struct coff_link_hash_entry
**rel_hash_ptr
;
2656 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2659 bfd_set_error (bfd_error_bad_value
);
2663 if (link_order
->u
.reloc
.p
->addend
!= 0)
2667 bfd_reloc_status_type rstat
;
2670 size
= bfd_get_reloc_size (howto
);
2671 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2675 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2676 link_order
->u
.reloc
.p
->addend
, buf
);
2682 case bfd_reloc_outofrange
:
2684 case bfd_reloc_overflow
:
2685 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2687 (link_order
->type
== bfd_section_reloc_link_order
2688 ? bfd_section_name (output_bfd
,
2689 link_order
->u
.reloc
.p
->u
.section
)
2690 : link_order
->u
.reloc
.p
->u
.name
),
2691 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2692 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2699 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2700 (file_ptr
) link_order
->offset
, size
);
2706 /* Store the reloc information in the right place. It will get
2707 swapped and written out at the end of the final_link routine. */
2709 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2710 + output_section
->reloc_count
);
2711 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2712 + output_section
->reloc_count
);
2714 memset (irel
, 0, sizeof (struct internal_reloc
));
2715 *rel_hash_ptr
= NULL
;
2717 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2719 if (link_order
->type
== bfd_section_reloc_link_order
)
2721 /* We need to somehow locate a symbol in the right section. The
2722 symbol must either have a value of zero, or we must adjust
2723 the addend by the value of the symbol. FIXME: Write this
2724 when we need it. The old linker couldn't handle this anyhow. */
2726 *rel_hash_ptr
= NULL
;
2731 struct coff_link_hash_entry
*h
;
2733 h
= ((struct coff_link_hash_entry
*)
2734 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2735 link_order
->u
.reloc
.p
->u
.name
,
2736 false, false, true));
2740 irel
->r_symndx
= h
->indx
;
2743 /* Set the index to -2 to force this symbol to get
2752 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2753 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2754 (asection
*) NULL
, (bfd_vma
) 0)))
2760 /* FIXME: Is this always right? */
2761 irel
->r_type
= howto
->type
;
2763 /* r_size is only used on the RS/6000, which needs its own linker
2764 routines anyhow. r_extern is only used for ECOFF. */
2766 /* FIXME: What is the right value for r_offset? Is zero OK? */
2768 ++output_section
->reloc_count
;
2773 /* A basic reloc handling routine which may be used by processors with
2777 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2778 input_section
, contents
, relocs
, syms
,
2781 struct bfd_link_info
*info
;
2783 asection
*input_section
;
2785 struct internal_reloc
*relocs
;
2786 struct internal_syment
*syms
;
2787 asection
**sections
;
2789 struct internal_reloc
*rel
;
2790 struct internal_reloc
*relend
;
2793 relend
= rel
+ input_section
->reloc_count
;
2794 for (; rel
< relend
; rel
++)
2797 struct coff_link_hash_entry
*h
;
2798 struct internal_syment
*sym
;
2801 reloc_howto_type
*howto
;
2802 bfd_reloc_status_type rstat
;
2804 symndx
= rel
->r_symndx
;
2812 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2814 (*_bfd_error_handler
)
2815 ("%s: illegal symbol index %ld in relocs",
2816 bfd_get_filename (input_bfd
), symndx
);
2821 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2822 sym
= syms
+ symndx
;
2825 /* COFF treats common symbols in one of two ways. Either the
2826 size of the symbol is included in the section contents, or it
2827 is not. We assume that the size is not included, and force
2828 the rtype_to_howto function to adjust the addend as needed. */
2830 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2831 addend
= - sym
->n_value
;
2836 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2841 /* If we are doing a relocateable link, then we can just ignore
2842 a PC relative reloc that is pcrel_offset. It will already
2843 have the correct value. If this is not a relocateable link,
2844 then we should ignore the symbol value. */
2845 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2847 if (info
->relocateable
)
2849 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2850 addend
+= sym
->n_value
;
2861 sec
= bfd_abs_section_ptr
;
2866 sec
= sections
[symndx
];
2867 val
= (sec
->output_section
->vma
2868 + sec
->output_offset
2870 if (! obj_pe (input_bfd
))
2876 if (h
->root
.type
== bfd_link_hash_defined
2877 || h
->root
.type
== bfd_link_hash_defweak
)
2881 sec
= h
->root
.u
.def
.section
;
2882 val
= (h
->root
.u
.def
.value
2883 + sec
->output_section
->vma
2884 + sec
->output_offset
);
2887 else if (! info
->relocateable
)
2889 if (! ((*info
->callbacks
->undefined_symbol
)
2890 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2891 rel
->r_vaddr
- input_section
->vma
)))
2896 if (info
->base_file
)
2898 /* Emit a reloc if the backend thinks it needs it. */
2899 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2901 /* Relocation to a symbol in a section which isn't
2902 absolute. We output the address here to a file.
2903 This file is then read by dlltool when generating the
2904 reloc section. Note that the base file is not
2905 portable between systems. We write out a long here,
2906 and dlltool reads in a long. */
2907 long addr
= (rel
->r_vaddr
2908 - input_section
->vma
2909 + input_section
->output_offset
2910 + input_section
->output_section
->vma
);
2911 if (coff_data (output_bfd
)->pe
)
2912 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2913 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2916 bfd_set_error (bfd_error_system_call
);
2922 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2924 rel
->r_vaddr
- input_section
->vma
,
2933 case bfd_reloc_outofrange
:
2934 (*_bfd_error_handler
)
2935 (_("%s: bad reloc address 0x%lx in section `%s'"),
2936 bfd_get_filename (input_bfd
),
2937 (unsigned long) rel
->r_vaddr
,
2938 bfd_get_section_name (input_bfd
, input_section
));
2940 case bfd_reloc_overflow
:
2943 char buf
[SYMNMLEN
+ 1];
2948 name
= h
->root
.root
.string
;
2951 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2956 if (! ((*info
->callbacks
->reloc_overflow
)
2957 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2958 input_section
, rel
->r_vaddr
- input_section
->vma
)))