1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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
;
121 ret
= ((struct coff_link_hash_table
*)
122 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
125 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
126 _bfd_coff_link_hash_newfunc
))
128 bfd_release (abfd
, ret
);
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
;
329 /* Keep the symbols during this function, in case the linker needs
330 to read the generic symbols in order to report an error message. */
331 keep_syms
= obj_coff_keep_syms (abfd
);
332 obj_coff_keep_syms (abfd
) = true;
334 if (info
->keep_memory
)
335 default_copy
= false;
339 symcount
= obj_raw_syment_count (abfd
);
341 /* We keep a list of the linker hash table entries that correspond
342 to particular symbols. */
343 sym_hash
= ((struct coff_link_hash_entry
**)
346 * sizeof (struct coff_link_hash_entry
*))));
347 if (sym_hash
== NULL
&& symcount
!= 0)
349 obj_coff_sym_hashes (abfd
) = sym_hash
;
351 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
353 symesz
= bfd_coff_symesz (abfd
);
354 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
355 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
356 esym_end
= esym
+ symcount
* symesz
;
357 while (esym
< esym_end
)
359 struct internal_syment sym
;
360 enum coff_symbol_classification classification
;
363 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
365 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
366 if (classification
!= COFF_SYMBOL_LOCAL
)
369 char buf
[SYMNMLEN
+ 1];
375 /* This symbol is externally visible. */
377 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
381 /* We must copy the name into memory if we got it from the
382 syment itself, rather than the string table. */
384 if (sym
._n
._n_n
._n_zeroes
!= 0
385 || sym
._n
._n_n
._n_offset
== 0)
390 switch (classification
)
395 case COFF_SYMBOL_GLOBAL
:
396 flags
= BSF_EXPORT
| BSF_GLOBAL
;
397 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
399 value
-= section
->vma
;
402 case COFF_SYMBOL_UNDEFINED
:
404 section
= bfd_und_section_ptr
;
407 case COFF_SYMBOL_COMMON
:
409 section
= bfd_com_section_ptr
;
412 case COFF_SYMBOL_PE_SECTION
:
413 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
414 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
418 if (IS_WEAK_EXTERNAL (abfd
, sym
))
423 /* In the PE format, section symbols actually refer to the
424 start of the output section. We handle them specially
426 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
428 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
429 name
, false, copy
, false);
430 if (*sym_hash
!= NULL
)
432 if (((*sym_hash
)->coff_link_hash_flags
433 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
434 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
435 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
436 (*_bfd_error_handler
)
437 ("Warning: symbol `%s' is both section and non-section",
444 /* The Microsoft Visual C compiler does string pooling by
445 hashing the constants to an internal symbol name, and
446 relying on the the linker comdat support to discard
447 duplicate names. However, if one string is a literal and
448 one is a data initializer, one will end up in the .data
449 section and one will end up in the .rdata section. The
450 Microsoft linker will combine them into the .data
451 section, which seems to be wrong since it might cause the
454 As long as there are no external references to the
455 symbols, which there shouldn't be, we can treat the .data
456 and .rdata instances as separate symbols. The comdat
457 code in the linker will do the appropriate merging. Here
458 we avoid getting a multiple definition error for one of
459 these special symbols.
461 FIXME: I don't think this will work in the case where
462 there are two object files which use the constants as a
463 literal and two object files which use it as a data
464 initializer. One or the other of the second object files
465 is going to wind up with an inappropriate reference. */
467 && (classification
== COFF_SYMBOL_GLOBAL
468 || classification
== COFF_SYMBOL_PE_SECTION
)
469 && section
->comdat
!= NULL
470 && strncmp (name
, "??_", 3) == 0
471 && strcmp (name
, section
->comdat
->name
) == 0)
473 if (*sym_hash
== NULL
)
474 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
475 name
, false, copy
, false);
476 if (*sym_hash
!= NULL
477 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
478 && (*sym_hash
)->root
.u
.def
.section
->comdat
!= NULL
479 && strcmp ((*sym_hash
)->root
.u
.def
.section
->comdat
->name
,
480 section
->comdat
->name
) == 0)
486 if (! (bfd_coff_link_add_one_symbol
487 (info
, abfd
, name
, flags
, section
, value
,
488 (const char *) NULL
, copy
, false,
489 (struct bfd_link_hash_entry
**) sym_hash
)))
493 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
494 (*sym_hash
)->coff_link_hash_flags
|=
495 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
497 /* Limit the alignment of a common symbol to the possible
498 alignment of a section. There is no point to permitting
499 a higher alignment for a common symbol: we can not
500 guarantee it, and it may cause us to allocate extra space
501 in the common section. */
502 if (section
== bfd_com_section_ptr
503 && (*sym_hash
)->root
.type
== bfd_link_hash_common
504 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
505 > bfd_coff_default_section_alignment_power (abfd
)))
506 (*sym_hash
)->root
.u
.c
.p
->alignment_power
507 = bfd_coff_default_section_alignment_power (abfd
);
509 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
511 /* If we don't have any symbol information currently in
512 the hash table, or if we are looking at a symbol
513 definition, then update the symbol class and type in
515 if (((*sym_hash
)->class == C_NULL
516 && (*sym_hash
)->type
== T_NULL
)
519 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
520 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
522 (*sym_hash
)->class = sym
.n_sclass
;
523 if (sym
.n_type
!= T_NULL
)
525 /* We want to warn if the type changed, but not
526 if it changed from an unspecified type.
527 Testing the whole type byte may work, but the
528 change from (e.g.) a function of unspecified
529 type to function of known type also wants to
531 if ((*sym_hash
)->type
!= T_NULL
532 && (*sym_hash
)->type
!= sym
.n_type
533 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
534 && (BTYPE ((*sym_hash
)->type
) == T_NULL
535 || BTYPE (sym
.n_type
) == T_NULL
)))
536 (*_bfd_error_handler
)
537 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
538 name
, (*sym_hash
)->type
, sym
.n_type
,
539 bfd_get_filename (abfd
));
541 /* We don't want to change from a meaningful
542 base type to a null one, but if we know
543 nothing, take what little we might now know. */
544 if (BTYPE (sym
.n_type
) != T_NULL
545 || (*sym_hash
)->type
== T_NULL
)
546 (*sym_hash
)->type
= sym
.n_type
;
548 (*sym_hash
)->auxbfd
= abfd
;
549 if (sym
.n_numaux
!= 0)
551 union internal_auxent
*alloc
;
554 union internal_auxent
*iaux
;
556 (*sym_hash
)->numaux
= sym
.n_numaux
;
557 alloc
= ((union internal_auxent
*)
558 bfd_hash_allocate (&info
->hash
->table
,
560 * sizeof (*alloc
))));
563 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
565 i
++, eaux
+= symesz
, iaux
++)
566 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
567 sym
.n_sclass
, i
, sym
.n_numaux
,
569 (*sym_hash
)->aux
= alloc
;
574 if (classification
== COFF_SYMBOL_PE_SECTION
575 && (*sym_hash
)->numaux
!= 0)
577 /* Some PE sections (such as .bss) have a zero size in
578 the section header, but a non-zero size in the AUX
579 record. Correct that here.
581 FIXME: This is not at all the right place to do this.
582 For example, it won't help objdump. This needs to be
583 done when we swap in the section header. */
585 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
586 if (section
->_raw_size
== 0)
587 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
589 /* FIXME: We could test whether the section sizes
590 matches the size in the aux entry, but apparently
591 that sometimes fails unexpectedly. */
595 esym
+= (sym
.n_numaux
+ 1) * symesz
;
596 sym_hash
+= sym
.n_numaux
+ 1;
599 /* If this is a non-traditional, non-relocateable link, try to
600 optimize the handling of any .stab/.stabstr sections. */
601 if (! info
->relocateable
602 && ! info
->traditional_format
603 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
604 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
606 asection
*stab
, *stabstr
;
608 stab
= bfd_get_section_by_name (abfd
, ".stab");
611 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
615 struct coff_link_hash_table
*table
;
616 struct coff_section_tdata
*secdata
;
618 secdata
= coff_section_data (abfd
, stab
);
622 (PTR
) bfd_zalloc (abfd
,
623 sizeof (struct coff_section_tdata
));
624 if (stab
->used_by_bfd
== NULL
)
626 secdata
= coff_section_data (abfd
, stab
);
629 table
= coff_hash_table (info
);
631 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
633 &secdata
->stab_info
))
639 obj_coff_keep_syms (abfd
) = keep_syms
;
644 obj_coff_keep_syms (abfd
) = keep_syms
;
648 /* Do the final link step. */
651 _bfd_coff_final_link (abfd
, info
)
653 struct bfd_link_info
*info
;
655 bfd_size_type symesz
;
656 struct coff_final_link_info finfo
;
657 boolean debug_merge_allocated
;
658 boolean long_section_names
;
660 struct bfd_link_order
*p
;
661 size_t max_sym_count
;
662 size_t max_lineno_count
;
663 size_t max_reloc_count
;
664 size_t max_output_reloc_count
;
665 size_t max_contents_size
;
666 file_ptr rel_filepos
;
668 file_ptr line_filepos
;
671 bfd_byte
*external_relocs
= NULL
;
672 char strbuf
[STRING_SIZE_SIZE
];
674 symesz
= bfd_coff_symesz (abfd
);
677 finfo
.output_bfd
= abfd
;
679 finfo
.section_info
= NULL
;
680 finfo
.last_file_index
= -1;
681 finfo
.last_bf_index
= -1;
682 finfo
.internal_syms
= NULL
;
683 finfo
.sec_ptrs
= NULL
;
684 finfo
.sym_indices
= NULL
;
685 finfo
.outsyms
= NULL
;
686 finfo
.linenos
= NULL
;
687 finfo
.contents
= NULL
;
688 finfo
.external_relocs
= NULL
;
689 finfo
.internal_relocs
= NULL
;
690 finfo
.global_to_static
= false;
691 debug_merge_allocated
= false;
693 coff_data (abfd
)->link_info
= info
;
695 finfo
.strtab
= _bfd_stringtab_init ();
696 if (finfo
.strtab
== NULL
)
699 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
701 debug_merge_allocated
= true;
703 /* Compute the file positions for all the sections. */
704 if (! abfd
->output_has_begun
)
706 if (! bfd_coff_compute_section_file_positions (abfd
))
710 /* Count the line numbers and relocation entries required for the
711 output file. Set the file positions for the relocs. */
712 rel_filepos
= obj_relocbase (abfd
);
713 relsz
= bfd_coff_relsz (abfd
);
714 max_contents_size
= 0;
715 max_lineno_count
= 0;
718 long_section_names
= false;
719 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
723 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
725 if (p
->type
== bfd_indirect_link_order
)
729 sec
= p
->u
.indirect
.section
;
731 /* Mark all sections which are to be included in the
732 link. This will normally be every section. We need
733 to do this so that we can identify any sections which
734 the linker has decided to not include. */
735 sec
->linker_mark
= true;
737 if (info
->strip
== strip_none
738 || info
->strip
== strip_some
)
739 o
->lineno_count
+= sec
->lineno_count
;
741 if (info
->relocateable
)
742 o
->reloc_count
+= sec
->reloc_count
;
744 if (sec
->_raw_size
> max_contents_size
)
745 max_contents_size
= sec
->_raw_size
;
746 if (sec
->lineno_count
> max_lineno_count
)
747 max_lineno_count
= sec
->lineno_count
;
748 if (sec
->reloc_count
> max_reloc_count
)
749 max_reloc_count
= sec
->reloc_count
;
751 else if (info
->relocateable
752 && (p
->type
== bfd_section_reloc_link_order
753 || p
->type
== bfd_symbol_reloc_link_order
))
756 if (o
->reloc_count
== 0)
760 o
->flags
|= SEC_RELOC
;
761 o
->rel_filepos
= rel_filepos
;
762 rel_filepos
+= o
->reloc_count
* relsz
;
765 if (bfd_coff_long_section_names (abfd
)
766 && strlen (o
->name
) > SCNNMLEN
)
768 /* This section has a long name which must go in the string
769 table. This must correspond to the code in
770 coff_write_object_contents which puts the string index
771 into the s_name field of the section header. That is why
772 we pass hash as false. */
773 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
774 == (bfd_size_type
) -1)
776 long_section_names
= true;
780 /* If doing a relocateable link, allocate space for the pointers we
782 if (info
->relocateable
)
786 /* We use section_count + 1, rather than section_count, because
787 the target_index fields are 1 based. */
789 ((struct coff_link_section_info
*)
790 bfd_malloc ((abfd
->section_count
+ 1)
791 * sizeof (struct coff_link_section_info
)));
792 if (finfo
.section_info
== NULL
)
794 for (i
= 0; i
<= abfd
->section_count
; i
++)
796 finfo
.section_info
[i
].relocs
= NULL
;
797 finfo
.section_info
[i
].rel_hashes
= NULL
;
801 /* We now know the size of the relocs, so we can determine the file
802 positions of the line numbers. */
803 line_filepos
= rel_filepos
;
804 linesz
= bfd_coff_linesz (abfd
);
805 max_output_reloc_count
= 0;
806 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
808 if (o
->lineno_count
== 0)
812 o
->line_filepos
= line_filepos
;
813 line_filepos
+= o
->lineno_count
* linesz
;
816 if (o
->reloc_count
!= 0)
818 /* We don't know the indices of global symbols until we have
819 written out all the local symbols. For each section in
820 the output file, we keep an array of pointers to hash
821 table entries. Each entry in the array corresponds to a
822 reloc. When we find a reloc against a global symbol, we
823 set the corresponding entry in this array so that we can
824 fix up the symbol index after we have written out all the
827 Because of this problem, we also keep the relocs in
828 memory until the end of the link. This wastes memory,
829 but only when doing a relocateable link, which is not the
831 BFD_ASSERT (info
->relocateable
);
832 finfo
.section_info
[o
->target_index
].relocs
=
833 ((struct internal_reloc
*)
834 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
835 finfo
.section_info
[o
->target_index
].rel_hashes
=
836 ((struct coff_link_hash_entry
**)
837 bfd_malloc (o
->reloc_count
838 * sizeof (struct coff_link_hash_entry
*)));
839 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
840 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
843 if (o
->reloc_count
> max_output_reloc_count
)
844 max_output_reloc_count
= o
->reloc_count
;
847 /* Reset the reloc and lineno counts, so that we can use them to
848 count the number of entries we have output so far. */
853 obj_sym_filepos (abfd
) = line_filepos
;
855 /* Figure out the largest number of symbols in an input BFD. Take
856 the opportunity to clear the output_has_begun fields of all the
859 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
863 sub
->output_has_begun
= false;
864 sz
= obj_raw_syment_count (sub
);
865 if (sz
> max_sym_count
)
869 /* Allocate some buffers used while linking. */
870 finfo
.internal_syms
= ((struct internal_syment
*)
871 bfd_malloc (max_sym_count
872 * sizeof (struct internal_syment
)));
873 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
874 * sizeof (asection
*));
875 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
876 finfo
.outsyms
= ((bfd_byte
*)
877 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
878 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
879 * bfd_coff_linesz (abfd
));
880 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
881 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
882 if (! info
->relocateable
)
883 finfo
.internal_relocs
= ((struct internal_reloc
*)
884 bfd_malloc (max_reloc_count
885 * sizeof (struct internal_reloc
)));
886 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
887 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
888 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
889 || finfo
.outsyms
== NULL
890 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
891 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
892 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
893 || (! info
->relocateable
894 && finfo
.internal_relocs
== NULL
895 && max_reloc_count
> 0))
898 /* We now know the position of everything in the file, except that
899 we don't know the size of the symbol table and therefore we don't
900 know where the string table starts. We just build the string
901 table in memory as we go along. We process all the relocations
902 for a single input file at once. */
903 obj_raw_syment_count (abfd
) = 0;
905 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
907 if (! bfd_coff_start_final_link (abfd
, info
))
911 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
913 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
915 if (p
->type
== bfd_indirect_link_order
916 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
918 sub
= p
->u
.indirect
.section
->owner
;
919 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
921 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
923 sub
->output_has_begun
= true;
926 else if (p
->type
== bfd_section_reloc_link_order
927 || p
->type
== bfd_symbol_reloc_link_order
)
929 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
934 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
940 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
943 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
945 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
946 debug_merge_allocated
= false;
948 if (finfo
.internal_syms
!= NULL
)
950 free (finfo
.internal_syms
);
951 finfo
.internal_syms
= NULL
;
953 if (finfo
.sec_ptrs
!= NULL
)
955 free (finfo
.sec_ptrs
);
956 finfo
.sec_ptrs
= NULL
;
958 if (finfo
.sym_indices
!= NULL
)
960 free (finfo
.sym_indices
);
961 finfo
.sym_indices
= NULL
;
963 if (finfo
.linenos
!= NULL
)
965 free (finfo
.linenos
);
966 finfo
.linenos
= NULL
;
968 if (finfo
.contents
!= NULL
)
970 free (finfo
.contents
);
971 finfo
.contents
= NULL
;
973 if (finfo
.external_relocs
!= NULL
)
975 free (finfo
.external_relocs
);
976 finfo
.external_relocs
= NULL
;
978 if (finfo
.internal_relocs
!= NULL
)
980 free (finfo
.internal_relocs
);
981 finfo
.internal_relocs
= NULL
;
984 /* The value of the last C_FILE symbol is supposed to be the symbol
985 index of the first external symbol. Write it out again if
987 if (finfo
.last_file_index
!= -1
988 && (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 (obj_sym_filepos (abfd
)
995 + finfo
.last_file_index
* symesz
),
997 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
1001 /* If doing task linking (ld --task-link) then make a pass through the
1002 global symbols, writing out any that are defined, and making them
1004 if (info
->task_link
)
1006 finfo
.failed
= false;
1007 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
1013 /* Write out the global symbols. */
1014 finfo
.failed
= false;
1015 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
1020 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1021 if (finfo
.outsyms
!= NULL
)
1023 free (finfo
.outsyms
);
1024 finfo
.outsyms
= NULL
;
1027 if (info
->relocateable
&& max_output_reloc_count
> 0)
1029 /* Now that we have written out all the global symbols, we know
1030 the symbol indices to use for relocs against them, and we can
1031 finally write out the relocs. */
1032 external_relocs
= ((bfd_byte
*)
1033 bfd_malloc (max_output_reloc_count
* relsz
));
1034 if (external_relocs
== NULL
)
1037 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1039 struct internal_reloc
*irel
;
1040 struct internal_reloc
*irelend
;
1041 struct coff_link_hash_entry
**rel_hash
;
1044 if (o
->reloc_count
== 0)
1047 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1048 irelend
= irel
+ o
->reloc_count
;
1049 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1050 erel
= external_relocs
;
1051 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1053 if (*rel_hash
!= NULL
)
1055 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1056 irel
->r_symndx
= (*rel_hash
)->indx
;
1058 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1061 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1062 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
1063 abfd
) != relsz
* o
->reloc_count
)
1067 free (external_relocs
);
1068 external_relocs
= NULL
;
1071 /* Free up the section information. */
1072 if (finfo
.section_info
!= NULL
)
1076 for (i
= 0; i
< abfd
->section_count
; i
++)
1078 if (finfo
.section_info
[i
].relocs
!= NULL
)
1079 free (finfo
.section_info
[i
].relocs
);
1080 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1081 free (finfo
.section_info
[i
].rel_hashes
);
1083 free (finfo
.section_info
);
1084 finfo
.section_info
= NULL
;
1087 /* If we have optimized stabs strings, output them. */
1088 if (coff_hash_table (info
)->stab_info
!= NULL
)
1090 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1094 /* Write out the string table. */
1095 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1098 (obj_sym_filepos (abfd
)
1099 + obj_raw_syment_count (abfd
) * symesz
),
1103 #if STRING_SIZE_SIZE == 4
1105 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1106 (bfd_byte
*) strbuf
);
1108 #error Change bfd_h_put_32
1111 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1114 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1117 obj_coff_strings_written (abfd
) = true;
1120 _bfd_stringtab_free (finfo
.strtab
);
1122 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1123 not try to write out the symbols. */
1124 bfd_get_symcount (abfd
) = 0;
1129 if (debug_merge_allocated
)
1130 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1131 if (finfo
.strtab
!= NULL
)
1132 _bfd_stringtab_free (finfo
.strtab
);
1133 if (finfo
.section_info
!= NULL
)
1137 for (i
= 0; i
< abfd
->section_count
; i
++)
1139 if (finfo
.section_info
[i
].relocs
!= NULL
)
1140 free (finfo
.section_info
[i
].relocs
);
1141 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1142 free (finfo
.section_info
[i
].rel_hashes
);
1144 free (finfo
.section_info
);
1146 if (finfo
.internal_syms
!= NULL
)
1147 free (finfo
.internal_syms
);
1148 if (finfo
.sec_ptrs
!= NULL
)
1149 free (finfo
.sec_ptrs
);
1150 if (finfo
.sym_indices
!= NULL
)
1151 free (finfo
.sym_indices
);
1152 if (finfo
.outsyms
!= NULL
)
1153 free (finfo
.outsyms
);
1154 if (finfo
.linenos
!= NULL
)
1155 free (finfo
.linenos
);
1156 if (finfo
.contents
!= NULL
)
1157 free (finfo
.contents
);
1158 if (finfo
.external_relocs
!= NULL
)
1159 free (finfo
.external_relocs
);
1160 if (finfo
.internal_relocs
!= NULL
)
1161 free (finfo
.internal_relocs
);
1162 if (external_relocs
!= NULL
)
1163 free (external_relocs
);
1167 /* parse out a -heap <reserved>,<commit> line */
1170 dores_com (ptr
, output_bfd
, heap
)
1175 if (coff_data(output_bfd
)->pe
)
1177 int val
= strtoul (ptr
, &ptr
, 0);
1179 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1181 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1185 int val
= strtoul (ptr
+1, &ptr
, 0);
1187 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1189 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1195 static char *get_name(ptr
, dst
)
1202 while (*ptr
&& *ptr
!= ' ')
1208 /* Process any magic embedded commands in a section called .drectve */
1211 process_embedded_commands (output_bfd
, info
, abfd
)
1213 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1216 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1223 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1226 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1231 e
= copy
+ sec
->_raw_size
;
1232 for (s
= copy
; s
< e
; )
1238 if (strncmp (s
,"-attr", 5) == 0)
1250 s
= get_name(s
, &name
);
1251 s
= get_name(s
, &attribs
);
1271 asec
= bfd_get_section_by_name (abfd
, name
);
1274 asec
->flags
|= SEC_CODE
;
1276 asec
->flags
|= SEC_READONLY
;
1279 else if (strncmp (s
,"-heap", 5) == 0)
1281 s
= dores_com (s
+5, output_bfd
, 1);
1283 else if (strncmp (s
,"-stack", 6) == 0)
1285 s
= dores_com (s
+6, output_bfd
, 0);
1294 /* Place a marker against all symbols which are used by relocations.
1295 This marker can be picked up by the 'do we skip this symbol ?'
1296 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1301 mark_relocs (finfo
, input_bfd
)
1302 struct coff_final_link_info
* finfo
;
1307 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1310 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1312 struct internal_reloc
* internal_relocs
;
1313 struct internal_reloc
* irel
;
1314 struct internal_reloc
* irelend
;
1316 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1319 /* Read in the relocs. */
1320 internal_relocs
= _bfd_coff_read_internal_relocs
1321 (input_bfd
, a
, false,
1322 finfo
->external_relocs
,
1323 finfo
->info
->relocateable
,
1324 (finfo
->info
->relocateable
1325 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1326 : finfo
->internal_relocs
)
1329 if (internal_relocs
== NULL
)
1332 irel
= internal_relocs
;
1333 irelend
= irel
+ a
->reloc_count
;
1335 /* Place a mark in the sym_indices array (whose entries have
1336 been initialised to 0) for all of the symbols that are used
1337 in the relocation table. This will then be picked up in the
1340 for (; irel
< irelend
; irel
++)
1342 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1347 /* Link an input file into the linker output file. This function
1348 handles all the sections and relocations of the input file at once. */
1351 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1352 struct coff_final_link_info
*finfo
;
1355 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1356 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1358 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1360 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1361 asection
*, struct internal_reloc
*,
1364 const char *strings
;
1365 bfd_size_type syment_base
;
1367 bfd_size_type isymesz
;
1368 bfd_size_type osymesz
;
1369 bfd_size_type linesz
;
1372 struct internal_syment
*isymp
;
1375 unsigned long output_index
;
1377 struct coff_link_hash_entry
**sym_hash
;
1380 /* Move all the symbols to the output file. */
1382 output_bfd
= finfo
->output_bfd
;
1384 syment_base
= obj_raw_syment_count (output_bfd
);
1385 isymesz
= bfd_coff_symesz (input_bfd
);
1386 osymesz
= bfd_coff_symesz (output_bfd
);
1387 linesz
= bfd_coff_linesz (input_bfd
);
1388 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1391 if (! finfo
->info
->keep_memory
)
1394 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1397 if (! _bfd_coff_get_external_symbols (input_bfd
))
1400 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1401 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1402 isymp
= finfo
->internal_syms
;
1403 secpp
= finfo
->sec_ptrs
;
1404 indexp
= finfo
->sym_indices
;
1405 output_index
= syment_base
;
1406 outsym
= finfo
->outsyms
;
1408 if (coff_data (output_bfd
)->pe
)
1410 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1414 /* If we are going to perform relocations and also strip/discard some symbols
1415 then we must make sure that we do not strip/discard those symbols that are
1416 going to be involved in the relocations */
1417 if (( finfo
->info
->strip
!= strip_none
1418 || finfo
->info
->discard
!= discard_none
)
1419 && finfo
->info
->relocateable
)
1421 /* mark the symbol array as 'not-used' */
1422 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1424 mark_relocs (finfo
, input_bfd
);
1427 while (esym
< esym_end
)
1429 struct internal_syment isym
;
1430 enum coff_symbol_classification classification
;
1433 boolean dont_skip_symbol
;
1436 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1438 /* Make a copy of *isymp so that the relocate_section function
1439 always sees the original values. This is more reliable than
1440 always recomputing the symbol value even if we are stripping
1444 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1445 switch (classification
)
1449 case COFF_SYMBOL_GLOBAL
:
1450 case COFF_SYMBOL_PE_SECTION
:
1451 case COFF_SYMBOL_LOCAL
:
1452 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1454 case COFF_SYMBOL_COMMON
:
1455 *secpp
= bfd_com_section_ptr
;
1457 case COFF_SYMBOL_UNDEFINED
:
1458 *secpp
= bfd_und_section_ptr
;
1462 /* Extract the flag indicating if this symbol is used by a
1464 if ((finfo
->info
->strip
!= strip_none
1465 || finfo
->info
->discard
!= discard_none
)
1466 && finfo
->info
->relocateable
)
1467 dont_skip_symbol
= *indexp
;
1469 dont_skip_symbol
= false;
1475 add
= 1 + isym
.n_numaux
;
1477 /* If we are stripping all symbols, we want to skip this one. */
1478 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1483 switch (classification
)
1487 case COFF_SYMBOL_GLOBAL
:
1488 case COFF_SYMBOL_COMMON
:
1489 case COFF_SYMBOL_PE_SECTION
:
1490 /* This is a global symbol. Global symbols come at the
1491 end of the symbol table, so skip them for now.
1492 Locally defined function symbols, however, are an
1493 exception, and are not moved to the end. */
1495 if (! ISFCN (isym
.n_type
))
1499 case COFF_SYMBOL_UNDEFINED
:
1500 /* Undefined symbols are left for the end. */
1505 case COFF_SYMBOL_LOCAL
:
1506 /* This is a local symbol. Skip it if we are discarding
1508 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1514 #ifndef COFF_WITH_PE
1515 /* Skip section symbols for sections which are not going to be
1516 emitted, or which belong to linkonce sections that are going
1519 && isym
.n_sclass
== C_STAT
1520 && isym
.n_type
== T_NULL
1521 && isym
.n_numaux
> 0)
1523 if ((*secpp
)->output_section
== bfd_abs_section_ptr
1524 || (*secpp
)->kept_section
)
1529 /* If we stripping debugging symbols, and this is a debugging
1530 symbol, then skip it. FIXME: gas sets the section to N_ABS
1531 for some types of debugging symbols; I don't know if this is
1532 a bug or not. In any case, we handle it here. */
1534 && finfo
->info
->strip
== strip_debugger
1535 && ! dont_skip_symbol
1536 && (isym
.n_scnum
== N_DEBUG
1537 || (isym
.n_scnum
== N_ABS
1538 && (isym
.n_sclass
== C_AUTO
1539 || isym
.n_sclass
== C_REG
1540 || isym
.n_sclass
== C_MOS
1541 || isym
.n_sclass
== C_MOE
1542 || isym
.n_sclass
== C_MOU
1543 || isym
.n_sclass
== C_ARG
1544 || isym
.n_sclass
== C_REGPARM
1545 || isym
.n_sclass
== C_FIELD
1546 || isym
.n_sclass
== C_EOS
))))
1549 /* If some symbols are stripped based on the name, work out the
1550 name and decide whether to skip this symbol. */
1552 && (finfo
->info
->strip
== strip_some
1553 || finfo
->info
->discard
== discard_l
))
1556 char buf
[SYMNMLEN
+ 1];
1558 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1562 if (! dont_skip_symbol
1563 && ((finfo
->info
->strip
== strip_some
1564 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1567 && finfo
->info
->discard
== discard_l
1568 && bfd_is_local_label_name (input_bfd
, name
))))
1572 /* If this is an enum, struct, or union tag, see if we have
1573 already output an identical type. */
1575 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1576 && (isym
.n_sclass
== C_ENTAG
1577 || isym
.n_sclass
== C_STRTAG
1578 || isym
.n_sclass
== C_UNTAG
)
1579 && isym
.n_numaux
== 1)
1582 char buf
[SYMNMLEN
+ 1];
1583 struct coff_debug_merge_hash_entry
*mh
;
1584 struct coff_debug_merge_type
*mt
;
1585 union internal_auxent aux
;
1586 struct coff_debug_merge_element
**epp
;
1587 bfd_byte
*esl
, *eslend
;
1588 struct internal_syment
*islp
;
1590 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1594 /* Ignore fake names invented by compiler; treat them all as
1596 if (*name
== '~' || *name
== '.' || *name
== '$'
1597 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1598 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1601 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1606 /* Allocate memory to hold type information. If this turns
1607 out to be a duplicate, we pass this address to
1609 mt
= ((struct coff_debug_merge_type
*)
1610 bfd_alloc (input_bfd
,
1611 sizeof (struct coff_debug_merge_type
)));
1614 mt
->class = isym
.n_sclass
;
1616 /* Pick up the aux entry, which points to the end of the tag
1618 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1619 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1622 /* Gather the elements. */
1623 epp
= &mt
->elements
;
1624 mt
->elements
= NULL
;
1626 esl
= esym
+ 2 * isymesz
;
1627 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1628 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1629 while (esl
< eslend
)
1631 const char *elename
;
1632 char elebuf
[SYMNMLEN
+ 1];
1635 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1637 *epp
= ((struct coff_debug_merge_element
*)
1638 bfd_alloc (input_bfd
,
1639 sizeof (struct coff_debug_merge_element
)));
1643 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1645 if (elename
== NULL
)
1648 name_copy
= (char *) bfd_alloc (input_bfd
,
1649 strlen (elename
) + 1);
1650 if (name_copy
== NULL
)
1652 strcpy (name_copy
, elename
);
1654 (*epp
)->name
= name_copy
;
1655 (*epp
)->type
= islp
->n_type
;
1657 if (islp
->n_numaux
>= 1
1658 && islp
->n_type
!= T_NULL
1659 && islp
->n_sclass
!= C_EOS
)
1661 union internal_auxent eleaux
;
1664 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1665 islp
->n_type
, islp
->n_sclass
, 0,
1666 islp
->n_numaux
, (PTR
) &eleaux
);
1667 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1669 /* FIXME: If this tagndx entry refers to a symbol
1670 defined later in this file, we just ignore it.
1671 Handling this correctly would be tedious, and may
1677 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1680 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1681 if ((*epp
)->tagndx
< 0)
1685 epp
= &(*epp
)->next
;
1688 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1689 islp
+= islp
->n_numaux
+ 1;
1692 /* See if we already have a definition which matches this
1693 type. We always output the type if it has no elements,
1695 if (mt
->elements
== NULL
)
1696 bfd_release (input_bfd
, (PTR
) mt
);
1699 struct coff_debug_merge_type
*mtl
;
1701 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1703 struct coff_debug_merge_element
*me
, *mel
;
1705 if (mtl
->class != mt
->class)
1708 for (me
= mt
->elements
, mel
= mtl
->elements
;
1709 me
!= NULL
&& mel
!= NULL
;
1710 me
= me
->next
, mel
= mel
->next
)
1712 if (strcmp (me
->name
, mel
->name
) != 0
1713 || me
->type
!= mel
->type
1714 || me
->tagndx
!= mel
->tagndx
)
1718 if (me
== NULL
&& mel
== NULL
)
1722 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1724 /* This is the first definition of this type. */
1725 mt
->indx
= output_index
;
1726 mt
->next
= mh
->types
;
1731 /* This is a redefinition which can be merged. */
1732 bfd_release (input_bfd
, (PTR
) mt
);
1733 *indexp
= mtl
->indx
;
1734 add
= (eslend
- esym
) / isymesz
;
1740 /* We now know whether we are to skip this symbol or not. */
1743 /* Adjust the symbol in order to output it. */
1745 if (isym
._n
._n_n
._n_zeroes
== 0
1746 && isym
._n
._n_n
._n_offset
!= 0)
1751 /* This symbol has a long name. Enter it in the string
1752 table we are building. Note that we do not check
1753 bfd_coff_symname_in_debug. That is only true for
1754 XCOFF, and XCOFF requires different linking code
1756 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1760 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1761 if (indx
== (bfd_size_type
) -1)
1763 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1766 switch (isym
.n_sclass
)
1782 /* The symbol value should not be modified. */
1786 if (obj_pe (input_bfd
)
1787 && strcmp (isym
.n_name
, ".bf") != 0
1788 && isym
.n_scnum
> 0)
1790 /* For PE, .lf and .ef get their value left alone,
1791 while .bf gets relocated. However, they all have
1792 "real" section numbers, and need to be moved into
1794 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1799 case C_LABEL
: /* Not completely sure about these 2 */
1808 /* Compute new symbol location. */
1809 if (isym
.n_scnum
> 0)
1811 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1812 isym
.n_value
+= (*secpp
)->output_offset
;
1813 if (! obj_pe (input_bfd
))
1814 isym
.n_value
-= (*secpp
)->vma
;
1815 if (! obj_pe (finfo
->output_bfd
))
1816 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1821 /* The value of a C_FILE symbol is the symbol index of
1822 the next C_FILE symbol. The value of the last C_FILE
1823 symbol is the symbol index to the first external
1824 symbol (actually, coff_renumber_symbols does not get
1825 this right--it just sets the value of the last C_FILE
1826 symbol to zero--and nobody has ever complained about
1827 it). We try to get this right, below, just before we
1828 write the symbols out, but in the general case we may
1829 have to write the symbol out twice. */
1831 if (finfo
->last_file_index
!= -1
1832 && finfo
->last_file
.n_value
!= (bfd_vma
) output_index
)
1834 /* We must correct the value of the last C_FILE
1836 finfo
->last_file
.n_value
= output_index
;
1837 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1839 /* The last C_FILE symbol is in this input file. */
1840 bfd_coff_swap_sym_out (output_bfd
,
1841 (PTR
) &finfo
->last_file
,
1842 (PTR
) (finfo
->outsyms
1843 + ((finfo
->last_file_index
1849 /* We have already written out the last C_FILE
1850 symbol. We need to write it out again. We
1851 borrow *outsym temporarily. */
1852 bfd_coff_swap_sym_out (output_bfd
,
1853 (PTR
) &finfo
->last_file
,
1855 if (bfd_seek (output_bfd
,
1856 (obj_sym_filepos (output_bfd
)
1857 + finfo
->last_file_index
* osymesz
),
1859 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1865 finfo
->last_file_index
= output_index
;
1866 finfo
->last_file
= isym
;
1870 /* If doing task linking, convert normal global function symbols to
1871 static functions. */
1872 if (finfo
->info
->task_link
&& IS_EXTERNAL (input_bfd
, isym
))
1873 isym
.n_sclass
= C_STAT
;
1875 /* Output the symbol. */
1877 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1879 *indexp
= output_index
;
1884 struct coff_link_hash_entry
*h
;
1886 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1888 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1891 /* This can happen if there were errors earlier in
1893 bfd_set_error (bfd_error_bad_value
);
1896 h
->indx
= output_index
;
1899 output_index
+= add
;
1900 outsym
+= add
* osymesz
;
1903 esym
+= add
* isymesz
;
1907 for (--add
; add
> 0; --add
)
1914 /* Fix up the aux entries. This must be done in a separate pass,
1915 because we don't know the correct symbol indices until we have
1916 already decided which symbols we are going to keep. */
1918 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1919 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1920 isymp
= finfo
->internal_syms
;
1921 indexp
= finfo
->sym_indices
;
1922 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1923 outsym
= finfo
->outsyms
;
1924 while (esym
< esym_end
)
1928 add
= 1 + isymp
->n_numaux
;
1931 || (bfd_size_type
) *indexp
< syment_base
)
1932 && (*sym_hash
== NULL
1933 || (*sym_hash
)->auxbfd
!= input_bfd
))
1934 esym
+= add
* isymesz
;
1937 struct coff_link_hash_entry
*h
;
1945 /* The m68k-motorola-sysv assembler will sometimes
1946 generate two symbols with the same name, but only one
1947 will have aux entries. */
1948 BFD_ASSERT (isymp
->n_numaux
== 0
1949 || h
->numaux
== isymp
->n_numaux
);
1957 /* Handle the aux entries. This handling is based on
1958 coff_pointerize_aux. I don't know if it always correct. */
1959 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1961 union internal_auxent aux
;
1962 union internal_auxent
*auxp
;
1968 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1969 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1974 if (isymp
->n_sclass
== C_FILE
)
1976 /* If this is a long filename, we must put it in the
1978 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1979 && auxp
->x_file
.x_n
.x_offset
!= 0)
1981 const char *filename
;
1984 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1985 >= STRING_SIZE_SIZE
);
1986 if (strings
== NULL
)
1988 strings
= _bfd_coff_read_string_table (input_bfd
);
1989 if (strings
== NULL
)
1992 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1993 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1995 if (indx
== (bfd_size_type
) -1)
1997 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
2000 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
2004 if (ISFCN (isymp
->n_type
)
2005 || ISTAG (isymp
->n_sclass
)
2006 || isymp
->n_sclass
== C_BLOCK
2007 || isymp
->n_sclass
== C_FCN
)
2009 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2011 && indx
< obj_raw_syment_count (input_bfd
))
2013 /* We look forward through the symbol for
2014 the index of the next symbol we are going
2015 to include. I don't know if this is
2017 while ((finfo
->sym_indices
[indx
] < 0
2018 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
2020 && indx
< obj_raw_syment_count (input_bfd
))
2022 if (indx
>= obj_raw_syment_count (input_bfd
))
2023 indx
= output_index
;
2025 indx
= finfo
->sym_indices
[indx
];
2026 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
2030 indx
= auxp
->x_sym
.x_tagndx
.l
;
2031 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2035 symindx
= finfo
->sym_indices
[indx
];
2037 auxp
->x_sym
.x_tagndx
.l
= 0;
2039 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2042 /* The .bf symbols are supposed to be linked through
2043 the endndx field. We need to carry this list
2044 across object files. */
2047 && isymp
->n_sclass
== C_FCN
2048 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2049 || isymp
->_n
._n_n
._n_offset
== 0)
2050 && isymp
->_n
._n_name
[0] == '.'
2051 && isymp
->_n
._n_name
[1] == 'b'
2052 && isymp
->_n
._n_name
[2] == 'f'
2053 && isymp
->_n
._n_name
[3] == '\0')
2055 if (finfo
->last_bf_index
!= -1)
2057 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2060 if ((bfd_size_type
) finfo
->last_bf_index
2065 /* The last .bf symbol is in this input
2066 file. This will only happen if the
2067 assembler did not set up the .bf
2068 endndx symbols correctly. */
2069 auxout
= (PTR
) (finfo
->outsyms
2070 + ((finfo
->last_bf_index
2073 bfd_coff_swap_aux_out (output_bfd
,
2074 (PTR
) &finfo
->last_bf
,
2082 /* We have already written out the last
2083 .bf aux entry. We need to write it
2084 out again. We borrow *outsym
2085 temporarily. FIXME: This case should
2087 bfd_coff_swap_aux_out (output_bfd
,
2088 (PTR
) &finfo
->last_bf
,
2093 if (bfd_seek (output_bfd
,
2094 (obj_sym_filepos (output_bfd
)
2095 + finfo
->last_bf_index
* osymesz
),
2097 || bfd_write (outsym
, osymesz
, 1,
2098 output_bfd
) != osymesz
)
2103 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2104 finfo
->last_bf_index
= -1;
2107 /* The endndx field of this aux entry must
2108 be updated with the symbol number of the
2110 finfo
->last_bf
= *auxp
;
2111 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2120 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
2121 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2135 /* Relocate the line numbers, unless we are stripping them. */
2136 if (finfo
->info
->strip
== strip_none
2137 || finfo
->info
->strip
== strip_some
)
2139 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2147 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2148 build_link_order in ldwrite.c will not have created a
2149 link order, which means that we will not have seen this
2150 input section in _bfd_coff_final_link, which means that
2151 we will not have allocated space for the line numbers of
2152 this section. I don't think line numbers can be
2153 meaningful for a section which does not have
2154 SEC_HAS_CONTENTS set, but, if they do, this must be
2156 if (o
->lineno_count
== 0
2157 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2160 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2161 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2162 input_bfd
) != linesz
* o
->lineno_count
)
2165 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2166 eline
= finfo
->linenos
;
2167 oeline
= finfo
->linenos
;
2168 elineend
= eline
+ linesz
* o
->lineno_count
;
2170 for (; eline
< elineend
; eline
+= linesz
)
2172 struct internal_lineno iline
;
2174 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2176 if (iline
.l_lnno
!= 0)
2177 iline
.l_addr
.l_paddr
+= offset
;
2178 else if (iline
.l_addr
.l_symndx
>= 0
2179 && ((unsigned long) iline
.l_addr
.l_symndx
2180 < obj_raw_syment_count (input_bfd
)))
2184 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2188 /* These line numbers are attached to a symbol
2189 which we are stripping. We must discard the
2190 line numbers because reading them back with
2191 no associated symbol (or associating them all
2192 with symbol #0) will fail. We can't regain
2193 the space in the output file, but at least
2199 struct internal_syment is
;
2200 union internal_auxent ia
;
2202 /* Fix up the lnnoptr field in the aux entry of
2203 the symbol. It turns out that we can't do
2204 this when we modify the symbol aux entries,
2205 because gas sometimes screws up the lnnoptr
2206 field and makes it an offset from the start
2207 of the line numbers rather than an absolute
2209 bfd_coff_swap_sym_in (output_bfd
,
2210 (PTR
) (finfo
->outsyms
2211 + ((indx
- syment_base
)
2214 if ((ISFCN (is
.n_type
)
2215 || is
.n_sclass
== C_BLOCK
)
2216 && is
.n_numaux
>= 1)
2220 auxptr
= (PTR
) (finfo
->outsyms
2221 + ((indx
- syment_base
+ 1)
2223 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2224 is
.n_type
, is
.n_sclass
,
2225 0, is
.n_numaux
, (PTR
) &ia
);
2226 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2227 (o
->output_section
->line_filepos
2228 + o
->output_section
->lineno_count
* linesz
2229 + eline
- finfo
->linenos
);
2230 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2231 is
.n_type
, is
.n_sclass
, 0,
2232 is
.n_numaux
, auxptr
);
2238 iline
.l_addr
.l_symndx
= indx
;
2243 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
,
2249 if (bfd_seek (output_bfd
,
2250 (o
->output_section
->line_filepos
2251 + o
->output_section
->lineno_count
* linesz
),
2253 || (bfd_write (finfo
->linenos
, 1, oeline
- finfo
->linenos
,
2255 != (bfd_size_type
) (oeline
- finfo
->linenos
)))
2258 o
->output_section
->lineno_count
+=
2259 (oeline
- finfo
->linenos
) / linesz
;
2263 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2264 symbol will be the first symbol in the next input file. In the
2265 normal case, this will save us from writing out the C_FILE symbol
2267 if (finfo
->last_file_index
!= -1
2268 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2270 finfo
->last_file
.n_value
= output_index
;
2271 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2272 (PTR
) (finfo
->outsyms
2273 + ((finfo
->last_file_index
- syment_base
)
2277 /* Write the modified symbols to the output file. */
2278 if (outsym
> finfo
->outsyms
)
2280 if (bfd_seek (output_bfd
,
2281 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2283 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2285 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2288 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2289 + (outsym
- finfo
->outsyms
) / osymesz
)
2292 obj_raw_syment_count (output_bfd
) = output_index
;
2295 /* Relocate the contents of each section. */
2296 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2297 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2300 struct coff_section_tdata
*secdata
;
2302 if (! o
->linker_mark
)
2304 /* This section was omitted from the link. */
2308 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2309 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2311 if ((o
->flags
& SEC_RELOC
) != 0
2312 && o
->reloc_count
!= 0)
2314 ((*_bfd_error_handler
)
2315 (_("%s: relocs in section `%s', but it has no contents"),
2316 bfd_get_filename (input_bfd
),
2317 bfd_get_section_name (input_bfd
, o
)));
2318 bfd_set_error (bfd_error_no_contents
);
2325 secdata
= coff_section_data (input_bfd
, o
);
2326 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2327 contents
= secdata
->contents
;
2330 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2331 (file_ptr
) 0, o
->_raw_size
))
2333 contents
= finfo
->contents
;
2336 if ((o
->flags
& SEC_RELOC
) != 0)
2339 struct internal_reloc
*internal_relocs
;
2340 struct internal_reloc
*irel
;
2342 /* Read in the relocs. */
2343 target_index
= o
->output_section
->target_index
;
2344 internal_relocs
= (_bfd_coff_read_internal_relocs
2345 (input_bfd
, o
, false, finfo
->external_relocs
,
2346 finfo
->info
->relocateable
,
2347 (finfo
->info
->relocateable
2348 ? (finfo
->section_info
[target_index
].relocs
2349 + o
->output_section
->reloc_count
)
2350 : finfo
->internal_relocs
)));
2351 if (internal_relocs
== NULL
)
2354 /* Call processor specific code to relocate the section
2356 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2360 finfo
->internal_syms
,
2364 if (finfo
->info
->relocateable
)
2367 struct internal_reloc
*irelend
;
2368 struct coff_link_hash_entry
**rel_hash
;
2370 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2371 irel
= internal_relocs
;
2372 irelend
= irel
+ o
->reloc_count
;
2373 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2374 + o
->output_section
->reloc_count
);
2375 for (; irel
< irelend
; irel
++, rel_hash
++)
2377 struct coff_link_hash_entry
*h
;
2382 /* Adjust the reloc address and symbol index. */
2384 irel
->r_vaddr
+= offset
;
2386 if (irel
->r_symndx
== -1)
2391 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2399 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2402 /* This is a global symbol. */
2404 irel
->r_symndx
= h
->indx
;
2407 /* This symbol is being written at the end
2408 of the file, and we do not yet know the
2409 symbol index. We save the pointer to the
2410 hash table entry in the rel_hash list.
2411 We set the indx field to -2 to indicate
2412 that this symbol must not be stripped. */
2421 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2423 irel
->r_symndx
= indx
;
2426 struct internal_syment
*is
;
2428 char buf
[SYMNMLEN
+ 1];
2430 /* This reloc is against a symbol we are
2431 stripping. This should have been handled
2432 by the 'dont_skip_symbol' code in the while
2433 loop at the top of this function. */
2435 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2437 name
= (_bfd_coff_internal_syment_name
2438 (input_bfd
, is
, buf
));
2442 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2443 (finfo
->info
, name
, input_bfd
, o
,
2450 o
->output_section
->reloc_count
+= o
->reloc_count
;
2454 /* Write out the modified section contents. */
2455 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2457 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2461 bfd_octets_per_byte (output_bfd
)),
2462 (o
->_cooked_size
!= 0
2469 if (! (_bfd_write_section_stabs
2470 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2471 o
, &secdata
->stab_info
, contents
)))
2476 if (! finfo
->info
->keep_memory
)
2478 if (! _bfd_coff_free_symbols (input_bfd
))
2485 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2488 _bfd_coff_write_global_sym (h
, data
)
2489 struct coff_link_hash_entry
*h
;
2492 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2494 struct internal_syment isym
;
2495 bfd_size_type symesz
;
2498 output_bfd
= finfo
->output_bfd
;
2504 && (finfo
->info
->strip
== strip_all
2505 || (finfo
->info
->strip
== strip_some
2506 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2507 h
->root
.root
.string
, false, false)
2511 switch (h
->root
.type
)
2514 case bfd_link_hash_new
:
2518 case bfd_link_hash_undefined
:
2519 case bfd_link_hash_undefweak
:
2520 isym
.n_scnum
= N_UNDEF
;
2524 case bfd_link_hash_defined
:
2525 case bfd_link_hash_defweak
:
2529 sec
= h
->root
.u
.def
.section
->output_section
;
2530 if (bfd_is_abs_section (sec
))
2531 isym
.n_scnum
= N_ABS
;
2533 isym
.n_scnum
= sec
->target_index
;
2534 isym
.n_value
= (h
->root
.u
.def
.value
2535 + h
->root
.u
.def
.section
->output_offset
);
2536 if (! obj_pe (finfo
->output_bfd
))
2537 isym
.n_value
+= sec
->vma
;
2541 case bfd_link_hash_common
:
2542 isym
.n_scnum
= N_UNDEF
;
2543 isym
.n_value
= h
->root
.u
.c
.size
;
2546 case bfd_link_hash_indirect
:
2547 case bfd_link_hash_warning
:
2548 /* Just ignore these. They can't be handled anyhow. */
2552 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2553 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2560 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2562 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2564 if (indx
== (bfd_size_type
) -1)
2566 finfo
->failed
= true;
2569 isym
._n
._n_n
._n_zeroes
= 0;
2570 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2573 isym
.n_sclass
= h
->class;
2574 isym
.n_type
= h
->type
;
2576 if (isym
.n_sclass
== C_NULL
)
2577 isym
.n_sclass
= C_EXT
;
2579 /* If doing task linking and this is the pass where we convert
2580 defined globals to statics, then do that conversion now. If the
2581 symbol is not being converted, just ignore it and it will be
2582 output during a later pass. */
2583 if (finfo
->global_to_static
)
2585 if (! IS_EXTERNAL (output_bfd
, isym
))
2588 isym
.n_sclass
= C_STAT
;
2591 /* When a weak symbol is not overriden by a strong one,
2592 turn it into an external symbol when not building a
2593 shared or relocateable object. */
2594 if (! finfo
->info
->shared
2595 && ! finfo
->info
->relocateable
2596 && IS_WEAK_EXTERNAL (finfo
->output_bfd
, isym
))
2597 isym
.n_sclass
= C_EXT
;
2599 isym
.n_numaux
= h
->numaux
;
2601 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2603 symesz
= bfd_coff_symesz (output_bfd
);
2605 if (bfd_seek (output_bfd
,
2606 (obj_sym_filepos (output_bfd
)
2607 + obj_raw_syment_count (output_bfd
) * symesz
),
2609 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2611 finfo
->failed
= true;
2615 h
->indx
= obj_raw_syment_count (output_bfd
);
2617 ++obj_raw_syment_count (output_bfd
);
2619 /* Write out any associated aux entries. Most of the aux entries
2620 will have been modified in _bfd_coff_link_input_bfd. We have to
2621 handle section aux entries here, now that we have the final
2622 relocation and line number counts. */
2623 for (i
= 0; i
< isym
.n_numaux
; i
++)
2625 union internal_auxent
*auxp
;
2629 /* Look for a section aux entry here using the same tests that
2630 coff_swap_aux_out uses. */
2632 && (isym
.n_sclass
== C_STAT
2633 || isym
.n_sclass
== C_HIDDEN
)
2634 && isym
.n_type
== T_NULL
2635 && (h
->root
.type
== bfd_link_hash_defined
2636 || h
->root
.type
== bfd_link_hash_defweak
))
2640 sec
= h
->root
.u
.def
.section
->output_section
;
2643 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2647 /* For PE, an overflow on the final link reportedly does
2648 not matter. FIXME: Why not? */
2650 if (sec
->reloc_count
> 0xffff
2651 && (! obj_pe (output_bfd
)
2652 || finfo
->info
->relocateable
))
2653 (*_bfd_error_handler
)
2654 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2655 bfd_get_filename (output_bfd
),
2656 bfd_get_section_name (output_bfd
, sec
),
2659 if (sec
->lineno_count
> 0xffff
2660 && (! obj_pe (output_bfd
)
2661 || finfo
->info
->relocateable
))
2662 (*_bfd_error_handler
)
2663 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2664 bfd_get_filename (output_bfd
),
2665 bfd_get_section_name (output_bfd
, sec
),
2668 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2669 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2670 auxp
->x_scn
.x_checksum
= 0;
2671 auxp
->x_scn
.x_associated
= 0;
2672 auxp
->x_scn
.x_comdat
= 0;
2676 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isym
.n_type
,
2677 isym
.n_sclass
, i
, isym
.n_numaux
,
2678 (PTR
) finfo
->outsyms
);
2679 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2681 finfo
->failed
= true;
2684 ++obj_raw_syment_count (output_bfd
);
2690 /* Write out task global symbols, converting them to statics. Called
2691 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2692 the dirty work, if the symbol we are processing needs conversion. */
2695 _bfd_coff_write_task_globals (h
, data
)
2696 struct coff_link_hash_entry
*h
;
2699 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2700 boolean rtnval
= true;
2701 boolean save_global_to_static
;
2705 switch (h
->root
.type
)
2707 case bfd_link_hash_defined
:
2708 case bfd_link_hash_defweak
:
2709 save_global_to_static
= finfo
->global_to_static
;
2710 finfo
->global_to_static
= true;
2711 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2712 finfo
->global_to_static
= save_global_to_static
;
2721 /* Handle a link order which is supposed to generate a reloc. */
2724 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2726 struct coff_final_link_info
*finfo
;
2727 asection
*output_section
;
2728 struct bfd_link_order
*link_order
;
2730 reloc_howto_type
*howto
;
2731 struct internal_reloc
*irel
;
2732 struct coff_link_hash_entry
**rel_hash_ptr
;
2734 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2737 bfd_set_error (bfd_error_bad_value
);
2741 if (link_order
->u
.reloc
.p
->addend
!= 0)
2745 bfd_reloc_status_type rstat
;
2748 size
= bfd_get_reloc_size (howto
);
2749 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2753 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2754 link_order
->u
.reloc
.p
->addend
, buf
);
2760 case bfd_reloc_outofrange
:
2762 case bfd_reloc_overflow
:
2763 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2765 (link_order
->type
== bfd_section_reloc_link_order
2766 ? bfd_section_name (output_bfd
,
2767 link_order
->u
.reloc
.p
->u
.section
)
2768 : link_order
->u
.reloc
.p
->u
.name
),
2769 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2770 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2777 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2779 (link_order
->offset
*
2780 bfd_octets_per_byte (output_bfd
)), size
);
2786 /* Store the reloc information in the right place. It will get
2787 swapped and written out at the end of the final_link routine. */
2789 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2790 + output_section
->reloc_count
);
2791 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2792 + output_section
->reloc_count
);
2794 memset (irel
, 0, sizeof (struct internal_reloc
));
2795 *rel_hash_ptr
= NULL
;
2797 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2799 if (link_order
->type
== bfd_section_reloc_link_order
)
2801 /* We need to somehow locate a symbol in the right section. The
2802 symbol must either have a value of zero, or we must adjust
2803 the addend by the value of the symbol. FIXME: Write this
2804 when we need it. The old linker couldn't handle this anyhow. */
2806 *rel_hash_ptr
= NULL
;
2811 struct coff_link_hash_entry
*h
;
2813 h
= ((struct coff_link_hash_entry
*)
2814 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2815 link_order
->u
.reloc
.p
->u
.name
,
2816 false, false, true));
2820 irel
->r_symndx
= h
->indx
;
2823 /* Set the index to -2 to force this symbol to get
2832 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2833 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2834 (asection
*) NULL
, (bfd_vma
) 0)))
2840 /* FIXME: Is this always right? */
2841 irel
->r_type
= howto
->type
;
2843 /* r_size is only used on the RS/6000, which needs its own linker
2844 routines anyhow. r_extern is only used for ECOFF. */
2846 /* FIXME: What is the right value for r_offset? Is zero OK? */
2848 ++output_section
->reloc_count
;
2853 /* A basic reloc handling routine which may be used by processors with
2857 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2858 input_section
, contents
, relocs
, syms
,
2861 struct bfd_link_info
*info
;
2863 asection
*input_section
;
2865 struct internal_reloc
*relocs
;
2866 struct internal_syment
*syms
;
2867 asection
**sections
;
2869 struct internal_reloc
*rel
;
2870 struct internal_reloc
*relend
;
2873 relend
= rel
+ input_section
->reloc_count
;
2874 for (; rel
< relend
; rel
++)
2877 struct coff_link_hash_entry
*h
;
2878 struct internal_syment
*sym
;
2881 reloc_howto_type
*howto
;
2882 bfd_reloc_status_type rstat
;
2884 symndx
= rel
->r_symndx
;
2892 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2894 (*_bfd_error_handler
)
2895 ("%s: illegal symbol index %ld in relocs",
2896 bfd_get_filename (input_bfd
), symndx
);
2901 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2902 sym
= syms
+ symndx
;
2905 /* COFF treats common symbols in one of two ways. Either the
2906 size of the symbol is included in the section contents, or it
2907 is not. We assume that the size is not included, and force
2908 the rtype_to_howto function to adjust the addend as needed. */
2910 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2911 addend
= - sym
->n_value
;
2915 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2920 /* If we are doing a relocateable link, then we can just ignore
2921 a PC relative reloc that is pcrel_offset. It will already
2922 have the correct value. If this is not a relocateable link,
2923 then we should ignore the symbol value. */
2924 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2926 if (info
->relocateable
)
2928 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2929 addend
+= sym
->n_value
;
2940 sec
= bfd_abs_section_ptr
;
2945 sec
= sections
[symndx
];
2946 val
= (sec
->output_section
->vma
2947 + sec
->output_offset
2949 if (! obj_pe (input_bfd
))
2955 if (h
->root
.type
== bfd_link_hash_defined
2956 || h
->root
.type
== bfd_link_hash_defweak
)
2960 sec
= h
->root
.u
.def
.section
;
2961 val
= (h
->root
.u
.def
.value
2962 + sec
->output_section
->vma
2963 + sec
->output_offset
);
2966 else if (h
->root
.type
== bfd_link_hash_undefweak
)
2969 else if (! info
->relocateable
)
2971 if (! ((*info
->callbacks
->undefined_symbol
)
2972 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2973 rel
->r_vaddr
- input_section
->vma
, true)))
2978 if (info
->base_file
)
2980 /* Emit a reloc if the backend thinks it needs it. */
2981 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2983 /* Relocation to a symbol in a section which isn't
2984 absolute. We output the address here to a file.
2985 This file is then read by dlltool when generating the
2986 reloc section. Note that the base file is not
2987 portable between systems. We write out a long here,
2988 and dlltool reads in a long. */
2989 long addr
= (rel
->r_vaddr
2990 - input_section
->vma
2991 + input_section
->output_offset
2992 + input_section
->output_section
->vma
);
2993 if (coff_data (output_bfd
)->pe
)
2994 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2995 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2998 bfd_set_error (bfd_error_system_call
);
3004 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
3006 rel
->r_vaddr
- input_section
->vma
,
3015 case bfd_reloc_outofrange
:
3016 (*_bfd_error_handler
)
3017 (_("%s: bad reloc address 0x%lx in section `%s'"),
3018 bfd_get_filename (input_bfd
),
3019 (unsigned long) rel
->r_vaddr
,
3020 bfd_get_section_name (input_bfd
, input_section
));
3022 case bfd_reloc_overflow
:
3025 char buf
[SYMNMLEN
+ 1];
3030 name
= h
->root
.root
.string
;
3033 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3038 if (! ((*info
->callbacks
->reloc_overflow
)
3039 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
3040 input_section
, rel
->r_vaddr
- input_section
->vma
)))