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",
425 /* The Microsoft Visual C compiler does string pooling by
426 hashing the constants to an internal symbol name, and
427 relying on the the linker comdat support to discard
428 duplicate names. However, if one string is a literal and
429 one is a data initializer, one will end up in the .data
430 section and one will end up in the .rdata section. The
431 Microsoft linker will combine them into the .data
432 section, which seems to be wrong since it might cause the
435 As long as there are no external references to the
436 symbols, which there shouldn't be, we can treat the .data
437 and .rdata instances as separate symbols. The comdat
438 code in the linker will do the appropriate merging. Here
439 we avoid getting a multiple definition error for one of
440 these special symbols.
442 FIXME: I don't think this will work in the case where
443 there are two object files which use the constants as a
444 literal and two object files which use it as a data
445 initializer. One or the other of the second object files
446 is going to wind up with an inappropriate reference. */
448 && (classification
== COFF_SYMBOL_GLOBAL
449 || classification
== COFF_SYMBOL_PE_SECTION
)
450 && section
->comdat
!= NULL
451 && strncmp (name
, "??_", 3) == 0
452 && strcmp (name
, section
->comdat
->name
) == 0)
454 if (*sym_hash
== NULL
)
455 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
456 name
, false, copy
, false);
457 if (*sym_hash
!= NULL
458 && (*sym_hash
)->root
.type
== bfd_link_hash_defined
459 && (*sym_hash
)->root
.u
.def
.section
->comdat
!= NULL
460 && strcmp ((*sym_hash
)->root
.u
.def
.section
->comdat
->name
,
461 section
->comdat
->name
) == 0)
467 if (! (bfd_coff_link_add_one_symbol
468 (info
, abfd
, name
, flags
, section
, value
,
469 (const char *) NULL
, copy
, false,
470 (struct bfd_link_hash_entry
**) sym_hash
)))
474 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
475 (*sym_hash
)->coff_link_hash_flags
|=
476 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
478 /* Limit the alignment of a common symbol to the possible
479 alignment of a section. There is no point to permitting
480 a higher alignment for a common symbol: we can not
481 guarantee it, and it may cause us to allocate extra space
482 in the common section. */
483 if (section
== bfd_com_section_ptr
484 && (*sym_hash
)->root
.type
== bfd_link_hash_common
485 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
486 > bfd_coff_default_section_alignment_power (abfd
)))
487 (*sym_hash
)->root
.u
.c
.p
->alignment_power
488 = bfd_coff_default_section_alignment_power (abfd
);
490 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
492 /* If we don't have any symbol information currently in
493 the hash table, or if we are looking at a symbol
494 definition, then update the symbol class and type in
496 if (((*sym_hash
)->class == C_NULL
497 && (*sym_hash
)->type
== T_NULL
)
500 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
501 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
503 (*sym_hash
)->class = sym
.n_sclass
;
504 if (sym
.n_type
!= T_NULL
)
506 /* We want to warn if the type changed, but not
507 if it changed from an unspecified type.
508 Testing the whole type byte may work, but the
509 change from (e.g.) a function of unspecified
510 type to function of known type also wants to
512 if ((*sym_hash
)->type
!= T_NULL
513 && (*sym_hash
)->type
!= sym
.n_type
514 && !(DTYPE ((*sym_hash
)->type
) == DTYPE (sym
.n_type
)
515 && (BTYPE ((*sym_hash
)->type
) == T_NULL
516 || BTYPE (sym
.n_type
) == T_NULL
)))
517 (*_bfd_error_handler
)
518 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
519 name
, (*sym_hash
)->type
, sym
.n_type
,
520 bfd_get_filename (abfd
));
522 /* We don't want to change from a meaningful
523 base type to a null one, but if we know
524 nothing, take what little we might now know. */
525 if (BTYPE (sym
.n_type
) != T_NULL
526 || (*sym_hash
)->type
== T_NULL
)
527 (*sym_hash
)->type
= sym
.n_type
;
529 (*sym_hash
)->auxbfd
= abfd
;
530 if (sym
.n_numaux
!= 0)
532 union internal_auxent
*alloc
;
535 union internal_auxent
*iaux
;
537 (*sym_hash
)->numaux
= sym
.n_numaux
;
538 alloc
= ((union internal_auxent
*)
539 bfd_hash_allocate (&info
->hash
->table
,
541 * sizeof (*alloc
))));
544 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
546 i
++, eaux
+= symesz
, iaux
++)
547 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
548 sym
.n_sclass
, i
, sym
.n_numaux
,
550 (*sym_hash
)->aux
= alloc
;
555 if (classification
== COFF_SYMBOL_PE_SECTION
556 && (*sym_hash
)->numaux
!= 0)
558 /* Some PE sections (such as .bss) have a zero size in
559 the section header, but a non-zero size in the AUX
560 record. Correct that here.
562 FIXME: This is not at all the right place to do this.
563 For example, it won't help objdump. This needs to be
564 done when we swap in the section header. */
566 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
567 if (section
->_raw_size
== 0)
568 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
570 /* FIXME: We could test whether the section sizes
571 matches the size in the aux entry, but apparently
572 that sometimes fails unexpectedly. */
576 esym
+= (sym
.n_numaux
+ 1) * symesz
;
577 sym_hash
+= sym
.n_numaux
+ 1;
580 /* If this is a non-traditional, non-relocateable link, try to
581 optimize the handling of any .stab/.stabstr sections. */
582 if (! info
->relocateable
583 && ! info
->traditional_format
584 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
585 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
587 asection
*stab
, *stabstr
;
589 stab
= bfd_get_section_by_name (abfd
, ".stab");
592 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
596 struct coff_link_hash_table
*table
;
597 struct coff_section_tdata
*secdata
;
599 secdata
= coff_section_data (abfd
, stab
);
603 (PTR
) bfd_zalloc (abfd
,
604 sizeof (struct coff_section_tdata
));
605 if (stab
->used_by_bfd
== NULL
)
607 secdata
= coff_section_data (abfd
, stab
);
610 table
= coff_hash_table (info
);
612 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
614 &secdata
->stab_info
))
620 obj_coff_keep_syms (abfd
) = keep_syms
;
625 obj_coff_keep_syms (abfd
) = keep_syms
;
629 /* Do the final link step. */
632 _bfd_coff_final_link (abfd
, info
)
634 struct bfd_link_info
*info
;
636 bfd_size_type symesz
;
637 struct coff_final_link_info finfo
;
638 boolean debug_merge_allocated
;
639 boolean long_section_names
;
641 struct bfd_link_order
*p
;
642 size_t max_sym_count
;
643 size_t max_lineno_count
;
644 size_t max_reloc_count
;
645 size_t max_output_reloc_count
;
646 size_t max_contents_size
;
647 file_ptr rel_filepos
;
649 file_ptr line_filepos
;
652 bfd_byte
*external_relocs
= NULL
;
653 char strbuf
[STRING_SIZE_SIZE
];
655 symesz
= bfd_coff_symesz (abfd
);
658 finfo
.output_bfd
= abfd
;
660 finfo
.section_info
= NULL
;
661 finfo
.last_file_index
= -1;
662 finfo
.last_bf_index
= -1;
663 finfo
.internal_syms
= NULL
;
664 finfo
.sec_ptrs
= NULL
;
665 finfo
.sym_indices
= NULL
;
666 finfo
.outsyms
= NULL
;
667 finfo
.linenos
= NULL
;
668 finfo
.contents
= NULL
;
669 finfo
.external_relocs
= NULL
;
670 finfo
.internal_relocs
= NULL
;
671 finfo
.global_to_static
= false;
672 debug_merge_allocated
= false;
674 coff_data (abfd
)->link_info
= info
;
676 finfo
.strtab
= _bfd_stringtab_init ();
677 if (finfo
.strtab
== NULL
)
680 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
682 debug_merge_allocated
= true;
684 /* Compute the file positions for all the sections. */
685 if (! abfd
->output_has_begun
)
687 if (! bfd_coff_compute_section_file_positions (abfd
))
691 /* Count the line numbers and relocation entries required for the
692 output file. Set the file positions for the relocs. */
693 rel_filepos
= obj_relocbase (abfd
);
694 relsz
= bfd_coff_relsz (abfd
);
695 max_contents_size
= 0;
696 max_lineno_count
= 0;
699 long_section_names
= false;
700 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
704 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
706 if (p
->type
== bfd_indirect_link_order
)
710 sec
= p
->u
.indirect
.section
;
712 /* Mark all sections which are to be included in the
713 link. This will normally be every section. We need
714 to do this so that we can identify any sections which
715 the linker has decided to not include. */
716 sec
->linker_mark
= true;
718 if (info
->strip
== strip_none
719 || info
->strip
== strip_some
)
720 o
->lineno_count
+= sec
->lineno_count
;
722 if (info
->relocateable
)
723 o
->reloc_count
+= sec
->reloc_count
;
725 if (sec
->_raw_size
> max_contents_size
)
726 max_contents_size
= sec
->_raw_size
;
727 if (sec
->lineno_count
> max_lineno_count
)
728 max_lineno_count
= sec
->lineno_count
;
729 if (sec
->reloc_count
> max_reloc_count
)
730 max_reloc_count
= sec
->reloc_count
;
732 else if (info
->relocateable
733 && (p
->type
== bfd_section_reloc_link_order
734 || p
->type
== bfd_symbol_reloc_link_order
))
737 if (o
->reloc_count
== 0)
741 o
->flags
|= SEC_RELOC
;
742 o
->rel_filepos
= rel_filepos
;
743 rel_filepos
+= o
->reloc_count
* relsz
;
746 if (bfd_coff_long_section_names (abfd
)
747 && strlen (o
->name
) > SCNNMLEN
)
749 /* This section has a long name which must go in the string
750 table. This must correspond to the code in
751 coff_write_object_contents which puts the string index
752 into the s_name field of the section header. That is why
753 we pass hash as false. */
754 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
755 == (bfd_size_type
) -1)
757 long_section_names
= true;
761 /* If doing a relocateable link, allocate space for the pointers we
763 if (info
->relocateable
)
767 /* We use section_count + 1, rather than section_count, because
768 the target_index fields are 1 based. */
770 ((struct coff_link_section_info
*)
771 bfd_malloc ((abfd
->section_count
+ 1)
772 * sizeof (struct coff_link_section_info
)));
773 if (finfo
.section_info
== NULL
)
775 for (i
= 0; i
<= abfd
->section_count
; i
++)
777 finfo
.section_info
[i
].relocs
= NULL
;
778 finfo
.section_info
[i
].rel_hashes
= NULL
;
782 /* We now know the size of the relocs, so we can determine the file
783 positions of the line numbers. */
784 line_filepos
= rel_filepos
;
785 linesz
= bfd_coff_linesz (abfd
);
786 max_output_reloc_count
= 0;
787 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
789 if (o
->lineno_count
== 0)
793 o
->line_filepos
= line_filepos
;
794 line_filepos
+= o
->lineno_count
* linesz
;
797 if (o
->reloc_count
!= 0)
799 /* We don't know the indices of global symbols until we have
800 written out all the local symbols. For each section in
801 the output file, we keep an array of pointers to hash
802 table entries. Each entry in the array corresponds to a
803 reloc. When we find a reloc against a global symbol, we
804 set the corresponding entry in this array so that we can
805 fix up the symbol index after we have written out all the
808 Because of this problem, we also keep the relocs in
809 memory until the end of the link. This wastes memory,
810 but only when doing a relocateable link, which is not the
812 BFD_ASSERT (info
->relocateable
);
813 finfo
.section_info
[o
->target_index
].relocs
=
814 ((struct internal_reloc
*)
815 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
816 finfo
.section_info
[o
->target_index
].rel_hashes
=
817 ((struct coff_link_hash_entry
**)
818 bfd_malloc (o
->reloc_count
819 * sizeof (struct coff_link_hash_entry
*)));
820 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
821 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
824 if (o
->reloc_count
> max_output_reloc_count
)
825 max_output_reloc_count
= o
->reloc_count
;
828 /* Reset the reloc and lineno counts, so that we can use them to
829 count the number of entries we have output so far. */
834 obj_sym_filepos (abfd
) = line_filepos
;
836 /* Figure out the largest number of symbols in an input BFD. Take
837 the opportunity to clear the output_has_begun fields of all the
840 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
844 sub
->output_has_begun
= false;
845 sz
= obj_raw_syment_count (sub
);
846 if (sz
> max_sym_count
)
850 /* Allocate some buffers used while linking. */
851 finfo
.internal_syms
= ((struct internal_syment
*)
852 bfd_malloc (max_sym_count
853 * sizeof (struct internal_syment
)));
854 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
855 * sizeof (asection
*));
856 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
857 finfo
.outsyms
= ((bfd_byte
*)
858 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
859 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
860 * bfd_coff_linesz (abfd
));
861 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
862 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
863 if (! info
->relocateable
)
864 finfo
.internal_relocs
= ((struct internal_reloc
*)
865 bfd_malloc (max_reloc_count
866 * sizeof (struct internal_reloc
)));
867 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
868 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
869 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
870 || finfo
.outsyms
== NULL
871 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
872 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
873 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
874 || (! info
->relocateable
875 && finfo
.internal_relocs
== NULL
876 && max_reloc_count
> 0))
879 /* We now know the position of everything in the file, except that
880 we don't know the size of the symbol table and therefore we don't
881 know where the string table starts. We just build the string
882 table in memory as we go along. We process all the relocations
883 for a single input file at once. */
884 obj_raw_syment_count (abfd
) = 0;
886 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
888 if (! bfd_coff_start_final_link (abfd
, info
))
892 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
894 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
896 if (p
->type
== bfd_indirect_link_order
897 && bfd_family_coff (p
->u
.indirect
.section
->owner
))
899 sub
= p
->u
.indirect
.section
->owner
;
900 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
902 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
904 sub
->output_has_begun
= true;
907 else if (p
->type
== bfd_section_reloc_link_order
908 || p
->type
== bfd_symbol_reloc_link_order
)
910 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
915 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
921 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
924 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
926 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
927 debug_merge_allocated
= false;
929 if (finfo
.internal_syms
!= NULL
)
931 free (finfo
.internal_syms
);
932 finfo
.internal_syms
= NULL
;
934 if (finfo
.sec_ptrs
!= NULL
)
936 free (finfo
.sec_ptrs
);
937 finfo
.sec_ptrs
= NULL
;
939 if (finfo
.sym_indices
!= NULL
)
941 free (finfo
.sym_indices
);
942 finfo
.sym_indices
= NULL
;
944 if (finfo
.linenos
!= NULL
)
946 free (finfo
.linenos
);
947 finfo
.linenos
= NULL
;
949 if (finfo
.contents
!= NULL
)
951 free (finfo
.contents
);
952 finfo
.contents
= NULL
;
954 if (finfo
.external_relocs
!= NULL
)
956 free (finfo
.external_relocs
);
957 finfo
.external_relocs
= NULL
;
959 if (finfo
.internal_relocs
!= NULL
)
961 free (finfo
.internal_relocs
);
962 finfo
.internal_relocs
= NULL
;
965 /* The value of the last C_FILE symbol is supposed to be the symbol
966 index of the first external symbol. Write it out again if
968 if (finfo
.last_file_index
!= -1
969 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
971 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
972 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
973 (PTR
) finfo
.outsyms
);
975 (obj_sym_filepos (abfd
)
976 + finfo
.last_file_index
* symesz
),
978 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
982 /* If doing task linking (ld --task-link) then make a pass through the
983 global symbols, writing out any that are defined, and making them
987 finfo
.failed
= false;
988 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
994 /* Write out the global symbols. */
995 finfo
.failed
= false;
996 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
1001 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
1002 if (finfo
.outsyms
!= NULL
)
1004 free (finfo
.outsyms
);
1005 finfo
.outsyms
= NULL
;
1008 if (info
->relocateable
&& max_output_reloc_count
> 0)
1010 /* Now that we have written out all the global symbols, we know
1011 the symbol indices to use for relocs against them, and we can
1012 finally write out the relocs. */
1013 external_relocs
= ((bfd_byte
*)
1014 bfd_malloc (max_output_reloc_count
* relsz
));
1015 if (external_relocs
== NULL
)
1018 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1020 struct internal_reloc
*irel
;
1021 struct internal_reloc
*irelend
;
1022 struct coff_link_hash_entry
**rel_hash
;
1025 if (o
->reloc_count
== 0)
1028 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1029 irelend
= irel
+ o
->reloc_count
;
1030 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1031 erel
= external_relocs
;
1032 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1034 if (*rel_hash
!= NULL
)
1036 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1037 irel
->r_symndx
= (*rel_hash
)->indx
;
1039 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1042 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1043 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
1044 abfd
) != relsz
* o
->reloc_count
)
1048 free (external_relocs
);
1049 external_relocs
= NULL
;
1052 /* Free up the section information. */
1053 if (finfo
.section_info
!= NULL
)
1057 for (i
= 0; i
< abfd
->section_count
; i
++)
1059 if (finfo
.section_info
[i
].relocs
!= NULL
)
1060 free (finfo
.section_info
[i
].relocs
);
1061 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1062 free (finfo
.section_info
[i
].rel_hashes
);
1064 free (finfo
.section_info
);
1065 finfo
.section_info
= NULL
;
1068 /* If we have optimized stabs strings, output them. */
1069 if (coff_hash_table (info
)->stab_info
!= NULL
)
1071 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1075 /* Write out the string table. */
1076 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1079 (obj_sym_filepos (abfd
)
1080 + obj_raw_syment_count (abfd
) * symesz
),
1084 #if STRING_SIZE_SIZE == 4
1086 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1087 (bfd_byte
*) strbuf
);
1089 #error Change bfd_h_put_32
1092 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1095 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1099 _bfd_stringtab_free (finfo
.strtab
);
1101 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1102 not try to write out the symbols. */
1103 bfd_get_symcount (abfd
) = 0;
1108 if (debug_merge_allocated
)
1109 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1110 if (finfo
.strtab
!= NULL
)
1111 _bfd_stringtab_free (finfo
.strtab
);
1112 if (finfo
.section_info
!= NULL
)
1116 for (i
= 0; i
< abfd
->section_count
; i
++)
1118 if (finfo
.section_info
[i
].relocs
!= NULL
)
1119 free (finfo
.section_info
[i
].relocs
);
1120 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1121 free (finfo
.section_info
[i
].rel_hashes
);
1123 free (finfo
.section_info
);
1125 if (finfo
.internal_syms
!= NULL
)
1126 free (finfo
.internal_syms
);
1127 if (finfo
.sec_ptrs
!= NULL
)
1128 free (finfo
.sec_ptrs
);
1129 if (finfo
.sym_indices
!= NULL
)
1130 free (finfo
.sym_indices
);
1131 if (finfo
.outsyms
!= NULL
)
1132 free (finfo
.outsyms
);
1133 if (finfo
.linenos
!= NULL
)
1134 free (finfo
.linenos
);
1135 if (finfo
.contents
!= NULL
)
1136 free (finfo
.contents
);
1137 if (finfo
.external_relocs
!= NULL
)
1138 free (finfo
.external_relocs
);
1139 if (finfo
.internal_relocs
!= NULL
)
1140 free (finfo
.internal_relocs
);
1141 if (external_relocs
!= NULL
)
1142 free (external_relocs
);
1146 /* parse out a -heap <reserved>,<commit> line */
1149 dores_com (ptr
, output_bfd
, heap
)
1154 if (coff_data(output_bfd
)->pe
)
1156 int val
= strtoul (ptr
, &ptr
, 0);
1158 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1160 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1164 int val
= strtoul (ptr
+1, &ptr
, 0);
1166 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1168 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1174 static char *get_name(ptr
, dst
)
1181 while (*ptr
&& *ptr
!= ' ')
1187 /* Process any magic embedded commands in a section called .drectve */
1190 process_embedded_commands (output_bfd
, info
, abfd
)
1192 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1195 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1202 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1205 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1210 e
= copy
+ sec
->_raw_size
;
1211 for (s
= copy
; s
< e
; )
1217 if (strncmp (s
,"-attr", 5) == 0)
1229 s
= get_name(s
, &name
);
1230 s
= get_name(s
, &attribs
);
1250 asec
= bfd_get_section_by_name (abfd
, name
);
1253 asec
->flags
|= SEC_CODE
;
1255 asec
->flags
|= SEC_READONLY
;
1258 else if (strncmp (s
,"-heap", 5) == 0)
1260 s
= dores_com (s
+5, output_bfd
, 1);
1262 else if (strncmp (s
,"-stack", 6) == 0)
1264 s
= dores_com (s
+6, output_bfd
, 0);
1273 /* Place a marker against all symbols which are used by relocations.
1274 This marker can be picked up by the 'do we skip this symbol ?'
1275 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1280 mark_relocs (finfo
, input_bfd
)
1281 struct coff_final_link_info
* finfo
;
1286 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1289 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1291 struct internal_reloc
* internal_relocs
;
1292 struct internal_reloc
* irel
;
1293 struct internal_reloc
* irelend
;
1296 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1299 /* Read in the relocs. */
1300 internal_relocs
= _bfd_coff_read_internal_relocs
1301 (input_bfd
, a
, false,
1302 finfo
->external_relocs
,
1303 finfo
->info
->relocateable
,
1304 (finfo
->info
->relocateable
1305 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1306 : finfo
->internal_relocs
)
1309 if (internal_relocs
== NULL
)
1312 irel
= internal_relocs
;
1313 irelend
= irel
+ a
->reloc_count
;
1315 /* Place a mark in the sym_indices array (whose entries have
1316 been initialised to 0) for all of the symbols that are used
1317 in the relocation table. This will then be picked up in the
1320 for (; irel
< irelend
; irel
++)
1322 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1327 /* Link an input file into the linker output file. This function
1328 handles all the sections and relocations of the input file at once. */
1331 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1332 struct coff_final_link_info
*finfo
;
1335 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1336 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1338 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1340 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1341 asection
*, struct internal_reloc
*,
1344 const char *strings
;
1345 bfd_size_type syment_base
;
1347 bfd_size_type isymesz
;
1348 bfd_size_type osymesz
;
1349 bfd_size_type linesz
;
1352 struct internal_syment
*isymp
;
1355 unsigned long output_index
;
1357 struct coff_link_hash_entry
**sym_hash
;
1360 /* Move all the symbols to the output file. */
1362 output_bfd
= finfo
->output_bfd
;
1364 syment_base
= obj_raw_syment_count (output_bfd
);
1365 isymesz
= bfd_coff_symesz (input_bfd
);
1366 osymesz
= bfd_coff_symesz (output_bfd
);
1367 linesz
= bfd_coff_linesz (input_bfd
);
1368 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1371 if (! finfo
->info
->keep_memory
)
1374 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1377 if (! _bfd_coff_get_external_symbols (input_bfd
))
1380 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1381 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1382 isymp
= finfo
->internal_syms
;
1383 secpp
= finfo
->sec_ptrs
;
1384 indexp
= finfo
->sym_indices
;
1385 output_index
= syment_base
;
1386 outsym
= finfo
->outsyms
;
1388 if (coff_data (output_bfd
)->pe
)
1390 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1394 /* If we are going to perform relocations and also strip/discard some symbols
1395 then we must make sure that we do not strip/discard those symbols that are
1396 going to be involved in the relocations */
1397 if (( finfo
->info
->strip
!= strip_none
1398 || finfo
->info
->discard
!= discard_none
)
1399 && finfo
->info
->relocateable
)
1401 /* mark the symbol array as 'not-used' */
1402 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1404 mark_relocs (finfo
, input_bfd
);
1407 while (esym
< esym_end
)
1409 struct internal_syment isym
;
1410 enum coff_symbol_classification classification
;
1413 boolean dont_skip_symbol
;
1416 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1418 /* Make a copy of *isymp so that the relocate_section function
1419 always sees the original values. This is more reliable than
1420 always recomputing the symbol value even if we are stripping
1424 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1425 switch (classification
)
1429 case COFF_SYMBOL_GLOBAL
:
1430 case COFF_SYMBOL_PE_SECTION
:
1431 case COFF_SYMBOL_LOCAL
:
1432 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1434 case COFF_SYMBOL_COMMON
:
1435 *secpp
= bfd_com_section_ptr
;
1437 case COFF_SYMBOL_UNDEFINED
:
1438 *secpp
= bfd_und_section_ptr
;
1442 /* Extract the flag indicating if this symbol is used by a
1444 if ((finfo
->info
->strip
!= strip_none
1445 || finfo
->info
->discard
!= discard_none
)
1446 && finfo
->info
->relocateable
)
1447 dont_skip_symbol
= *indexp
;
1449 dont_skip_symbol
= false;
1455 add
= 1 + isym
.n_numaux
;
1457 /* If we are stripping all symbols, we want to skip this one. */
1458 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1463 switch (classification
)
1467 case COFF_SYMBOL_GLOBAL
:
1468 case COFF_SYMBOL_COMMON
:
1469 case COFF_SYMBOL_PE_SECTION
:
1470 /* This is a global symbol. Global symbols come at the
1471 end of the symbol table, so skip them for now.
1472 Locally defined function symbols, however, are an
1473 exception, and are not moved to the end. */
1475 if (! ISFCN (isym
.n_type
))
1479 case COFF_SYMBOL_UNDEFINED
:
1480 /* Undefined symbols are left for the end. */
1485 case COFF_SYMBOL_LOCAL
:
1486 /* This is a local symbol. Skip it if we are discarding
1488 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1494 /* If we stripping debugging symbols, and this is a debugging
1495 symbol, then skip it. FIXME: gas sets the section to N_ABS
1496 for some types of debugging symbols; I don't know if this is
1497 a bug or not. In any case, we handle it here. */
1499 && finfo
->info
->strip
== strip_debugger
1500 && ! dont_skip_symbol
1501 && (isym
.n_scnum
== N_DEBUG
1502 || (isym
.n_scnum
== N_ABS
1503 && (isym
.n_sclass
== C_AUTO
1504 || isym
.n_sclass
== C_REG
1505 || isym
.n_sclass
== C_MOS
1506 || isym
.n_sclass
== C_MOE
1507 || isym
.n_sclass
== C_MOU
1508 || isym
.n_sclass
== C_ARG
1509 || isym
.n_sclass
== C_REGPARM
1510 || isym
.n_sclass
== C_FIELD
1511 || isym
.n_sclass
== C_EOS
))))
1514 /* If some symbols are stripped based on the name, work out the
1515 name and decide whether to skip this symbol. */
1517 && (finfo
->info
->strip
== strip_some
1518 || finfo
->info
->discard
== discard_l
))
1521 char buf
[SYMNMLEN
+ 1];
1523 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1527 if (! dont_skip_symbol
1528 && ((finfo
->info
->strip
== strip_some
1529 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1532 && finfo
->info
->discard
== discard_l
1533 && bfd_is_local_label_name (input_bfd
, name
))))
1537 /* If this is an enum, struct, or union tag, see if we have
1538 already output an identical type. */
1540 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1541 && (isym
.n_sclass
== C_ENTAG
1542 || isym
.n_sclass
== C_STRTAG
1543 || isym
.n_sclass
== C_UNTAG
)
1544 && isym
.n_numaux
== 1)
1547 char buf
[SYMNMLEN
+ 1];
1548 struct coff_debug_merge_hash_entry
*mh
;
1549 struct coff_debug_merge_type
*mt
;
1550 union internal_auxent aux
;
1551 struct coff_debug_merge_element
**epp
;
1552 bfd_byte
*esl
, *eslend
;
1553 struct internal_syment
*islp
;
1555 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1559 /* Ignore fake names invented by compiler; treat them all as
1561 if (*name
== '~' || *name
== '.' || *name
== '$'
1562 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1563 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1566 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1571 /* Allocate memory to hold type information. If this turns
1572 out to be a duplicate, we pass this address to
1574 mt
= ((struct coff_debug_merge_type
*)
1575 bfd_alloc (input_bfd
,
1576 sizeof (struct coff_debug_merge_type
)));
1579 mt
->class = isym
.n_sclass
;
1581 /* Pick up the aux entry, which points to the end of the tag
1583 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1584 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1587 /* Gather the elements. */
1588 epp
= &mt
->elements
;
1589 mt
->elements
= NULL
;
1591 esl
= esym
+ 2 * isymesz
;
1592 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1593 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1594 while (esl
< eslend
)
1596 const char *elename
;
1597 char elebuf
[SYMNMLEN
+ 1];
1600 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1602 *epp
= ((struct coff_debug_merge_element
*)
1603 bfd_alloc (input_bfd
,
1604 sizeof (struct coff_debug_merge_element
)));
1608 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1610 if (elename
== NULL
)
1613 name_copy
= (char *) bfd_alloc (input_bfd
,
1614 strlen (elename
) + 1);
1615 if (name_copy
== NULL
)
1617 strcpy (name_copy
, elename
);
1619 (*epp
)->name
= name_copy
;
1620 (*epp
)->type
= islp
->n_type
;
1622 if (islp
->n_numaux
>= 1
1623 && islp
->n_type
!= T_NULL
1624 && islp
->n_sclass
!= C_EOS
)
1626 union internal_auxent eleaux
;
1629 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1630 islp
->n_type
, islp
->n_sclass
, 0,
1631 islp
->n_numaux
, (PTR
) &eleaux
);
1632 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1634 /* FIXME: If this tagndx entry refers to a symbol
1635 defined later in this file, we just ignore it.
1636 Handling this correctly would be tedious, and may
1642 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1645 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1646 if ((*epp
)->tagndx
< 0)
1650 epp
= &(*epp
)->next
;
1653 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1654 islp
+= islp
->n_numaux
+ 1;
1657 /* See if we already have a definition which matches this
1658 type. We always output the type if it has no elements,
1660 if (mt
->elements
== NULL
)
1661 bfd_release (input_bfd
, (PTR
) mt
);
1664 struct coff_debug_merge_type
*mtl
;
1666 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1668 struct coff_debug_merge_element
*me
, *mel
;
1670 if (mtl
->class != mt
->class)
1673 for (me
= mt
->elements
, mel
= mtl
->elements
;
1674 me
!= NULL
&& mel
!= NULL
;
1675 me
= me
->next
, mel
= mel
->next
)
1677 if (strcmp (me
->name
, mel
->name
) != 0
1678 || me
->type
!= mel
->type
1679 || me
->tagndx
!= mel
->tagndx
)
1683 if (me
== NULL
&& mel
== NULL
)
1687 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1689 /* This is the first definition of this type. */
1690 mt
->indx
= output_index
;
1691 mt
->next
= mh
->types
;
1696 /* This is a redefinition which can be merged. */
1697 bfd_release (input_bfd
, (PTR
) mt
);
1698 *indexp
= mtl
->indx
;
1699 add
= (eslend
- esym
) / isymesz
;
1705 /* We now know whether we are to skip this symbol or not. */
1708 /* Adjust the symbol in order to output it. */
1710 if (isym
._n
._n_n
._n_zeroes
== 0
1711 && isym
._n
._n_n
._n_offset
!= 0)
1716 /* This symbol has a long name. Enter it in the string
1717 table we are building. Note that we do not check
1718 bfd_coff_symname_in_debug. That is only true for
1719 XCOFF, and XCOFF requires different linking code
1721 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1725 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1726 if (indx
== (bfd_size_type
) -1)
1728 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1731 switch (isym
.n_sclass
)
1747 /* The symbol value should not be modified. */
1751 if (obj_pe (input_bfd
)
1752 && strcmp (isym
.n_name
, ".bf") != 0
1753 && isym
.n_scnum
> 0)
1755 /* For PE, .lf and .ef get their value left alone,
1756 while .bf gets relocated. However, they all have
1757 "real" section numbers, and need to be moved into
1759 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1764 case C_LABEL
: /* Not completely sure about these 2 */
1773 /* Compute new symbol location. */
1774 if (isym
.n_scnum
> 0)
1776 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1777 isym
.n_value
+= (*secpp
)->output_offset
;
1778 if (! obj_pe (input_bfd
))
1779 isym
.n_value
-= (*secpp
)->vma
;
1780 if (! obj_pe (finfo
->output_bfd
))
1781 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1786 /* The value of a C_FILE symbol is the symbol index of
1787 the next C_FILE symbol. The value of the last C_FILE
1788 symbol is the symbol index to the first external
1789 symbol (actually, coff_renumber_symbols does not get
1790 this right--it just sets the value of the last C_FILE
1791 symbol to zero--and nobody has ever complained about
1792 it). We try to get this right, below, just before we
1793 write the symbols out, but in the general case we may
1794 have to write the symbol out twice. */
1796 if (finfo
->last_file_index
!= -1
1797 && finfo
->last_file
.n_value
!= (long) output_index
)
1799 /* We must correct the value of the last C_FILE
1801 finfo
->last_file
.n_value
= output_index
;
1802 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1804 /* The last C_FILE symbol is in this input file. */
1805 bfd_coff_swap_sym_out (output_bfd
,
1806 (PTR
) &finfo
->last_file
,
1807 (PTR
) (finfo
->outsyms
1808 + ((finfo
->last_file_index
1814 /* We have already written out the last C_FILE
1815 symbol. We need to write it out again. We
1816 borrow *outsym temporarily. */
1817 bfd_coff_swap_sym_out (output_bfd
,
1818 (PTR
) &finfo
->last_file
,
1820 if (bfd_seek (output_bfd
,
1821 (obj_sym_filepos (output_bfd
)
1822 + finfo
->last_file_index
* osymesz
),
1824 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1830 finfo
->last_file_index
= output_index
;
1831 finfo
->last_file
= isym
;
1835 /* If doing task linking, convert normal global function symbols to
1836 static functions. */
1838 if (finfo
->info
->task_link
1839 && (isym
.n_sclass
== C_EXT
1840 || isym
.n_sclass
== C_WEAKEXT
1841 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)))
1842 isym
.n_sclass
= C_STAT
;
1844 /* Output the symbol. */
1846 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1848 *indexp
= output_index
;
1853 struct coff_link_hash_entry
*h
;
1855 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1857 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1860 /* This can happen if there were errors earlier in
1862 bfd_set_error (bfd_error_bad_value
);
1865 h
->indx
= output_index
;
1868 output_index
+= add
;
1869 outsym
+= add
* osymesz
;
1872 esym
+= add
* isymesz
;
1876 for (--add
; add
> 0; --add
)
1883 /* Fix up the aux entries. This must be done in a separate pass,
1884 because we don't know the correct symbol indices until we have
1885 already decided which symbols we are going to keep. */
1887 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1888 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1889 isymp
= finfo
->internal_syms
;
1890 indexp
= finfo
->sym_indices
;
1891 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1892 outsym
= finfo
->outsyms
;
1893 while (esym
< esym_end
)
1897 add
= 1 + isymp
->n_numaux
;
1900 || (bfd_size_type
) *indexp
< syment_base
)
1901 && (*sym_hash
== NULL
1902 || (*sym_hash
)->auxbfd
!= input_bfd
))
1903 esym
+= add
* isymesz
;
1906 struct coff_link_hash_entry
*h
;
1914 /* The m68k-motorola-sysv assembler will sometimes
1915 generate two symbols with the same name, but only one
1916 will have aux entries. */
1917 BFD_ASSERT (isymp
->n_numaux
== 0
1918 || h
->numaux
== isymp
->n_numaux
);
1926 /* Handle the aux entries. This handling is based on
1927 coff_pointerize_aux. I don't know if it always correct. */
1928 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1930 union internal_auxent aux
;
1931 union internal_auxent
*auxp
;
1937 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1938 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1943 if (isymp
->n_sclass
== C_FILE
)
1945 /* If this is a long filename, we must put it in the
1947 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1948 && auxp
->x_file
.x_n
.x_offset
!= 0)
1950 const char *filename
;
1953 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1954 >= STRING_SIZE_SIZE
);
1955 if (strings
== NULL
)
1957 strings
= _bfd_coff_read_string_table (input_bfd
);
1958 if (strings
== NULL
)
1961 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1962 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1964 if (indx
== (bfd_size_type
) -1)
1966 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1969 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1973 if (ISFCN (isymp
->n_type
)
1974 || ISTAG (isymp
->n_sclass
)
1975 || isymp
->n_sclass
== C_BLOCK
1976 || isymp
->n_sclass
== C_FCN
)
1978 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1980 && indx
< obj_raw_syment_count (input_bfd
))
1982 /* We look forward through the symbol for
1983 the index of the next symbol we are going
1984 to include. I don't know if this is
1986 while ((finfo
->sym_indices
[indx
] < 0
1987 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1989 && indx
< obj_raw_syment_count (input_bfd
))
1991 if (indx
>= obj_raw_syment_count (input_bfd
))
1992 indx
= output_index
;
1994 indx
= finfo
->sym_indices
[indx
];
1995 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1999 indx
= auxp
->x_sym
.x_tagndx
.l
;
2000 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
2004 symindx
= finfo
->sym_indices
[indx
];
2006 auxp
->x_sym
.x_tagndx
.l
= 0;
2008 auxp
->x_sym
.x_tagndx
.l
= symindx
;
2011 /* The .bf symbols are supposed to be linked through
2012 the endndx field. We need to carry this list
2013 across object files. */
2016 && isymp
->n_sclass
== C_FCN
2017 && (isymp
->_n
._n_n
._n_zeroes
!= 0
2018 || isymp
->_n
._n_n
._n_offset
== 0)
2019 && isymp
->_n
._n_name
[0] == '.'
2020 && isymp
->_n
._n_name
[1] == 'b'
2021 && isymp
->_n
._n_name
[2] == 'f'
2022 && isymp
->_n
._n_name
[3] == '\0')
2024 if (finfo
->last_bf_index
!= -1)
2026 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2029 if ((bfd_size_type
) finfo
->last_bf_index
2034 /* The last .bf symbol is in this input
2035 file. This will only happen if the
2036 assembler did not set up the .bf
2037 endndx symbols correctly. */
2038 auxout
= (PTR
) (finfo
->outsyms
2039 + ((finfo
->last_bf_index
2042 bfd_coff_swap_aux_out (output_bfd
,
2043 (PTR
) &finfo
->last_bf
,
2051 /* We have already written out the last
2052 .bf aux entry. We need to write it
2053 out again. We borrow *outsym
2054 temporarily. FIXME: This case should
2056 bfd_coff_swap_aux_out (output_bfd
,
2057 (PTR
) &finfo
->last_bf
,
2062 if (bfd_seek (output_bfd
,
2063 (obj_sym_filepos (output_bfd
)
2064 + finfo
->last_bf_index
* osymesz
),
2066 || bfd_write (outsym
, osymesz
, 1,
2067 output_bfd
) != osymesz
)
2072 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
2073 finfo
->last_bf_index
= -1;
2076 /* The endndx field of this aux entry must
2077 be updated with the symbol number of the
2079 finfo
->last_bf
= *auxp
;
2080 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
2089 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
2090 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2104 /* Relocate the line numbers, unless we are stripping them. */
2105 if (finfo
->info
->strip
== strip_none
2106 || finfo
->info
->strip
== strip_some
)
2108 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2116 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2117 build_link_order in ldwrite.c will not have created a
2118 link order, which means that we will not have seen this
2119 input section in _bfd_coff_final_link, which means that
2120 we will not have allocated space for the line numbers of
2121 this section. I don't think line numbers can be
2122 meaningful for a section which does not have
2123 SEC_HAS_CONTENTS set, but, if they do, this must be
2125 if (o
->lineno_count
== 0
2126 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2129 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2130 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2131 input_bfd
) != linesz
* o
->lineno_count
)
2134 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2135 eline
= finfo
->linenos
;
2136 oeline
= finfo
->linenos
;
2137 elineend
= eline
+ linesz
* o
->lineno_count
;
2139 for (; eline
< elineend
; eline
+= linesz
)
2141 struct internal_lineno iline
;
2143 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2145 if (iline
.l_lnno
!= 0)
2146 iline
.l_addr
.l_paddr
+= offset
;
2147 else if (iline
.l_addr
.l_symndx
>= 0
2148 && ((unsigned long) iline
.l_addr
.l_symndx
2149 < obj_raw_syment_count (input_bfd
)))
2153 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2157 /* These line numbers are attached to a symbol
2158 which we are stripping. We must discard the
2159 line numbers because reading them back with
2160 no associated symbol (or associating them all
2161 with symbol #0) will fail. We can't regain
2162 the space in the output file, but at least
2168 struct internal_syment is
;
2169 union internal_auxent ia
;
2171 /* Fix up the lnnoptr field in the aux entry of
2172 the symbol. It turns out that we can't do
2173 this when we modify the symbol aux entries,
2174 because gas sometimes screws up the lnnoptr
2175 field and makes it an offset from the start
2176 of the line numbers rather than an absolute
2178 bfd_coff_swap_sym_in (output_bfd
,
2179 (PTR
) (finfo
->outsyms
2180 + ((indx
- syment_base
)
2183 if ((ISFCN (is
.n_type
)
2184 || is
.n_sclass
== C_BLOCK
)
2185 && is
.n_numaux
>= 1)
2189 auxptr
= (PTR
) (finfo
->outsyms
2190 + ((indx
- syment_base
+ 1)
2192 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2193 is
.n_type
, is
.n_sclass
,
2194 0, is
.n_numaux
, (PTR
) &ia
);
2195 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2196 (o
->output_section
->line_filepos
2197 + o
->output_section
->lineno_count
* linesz
2198 + eline
- finfo
->linenos
);
2199 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2200 is
.n_type
, is
.n_sclass
, 0,
2201 is
.n_numaux
, auxptr
);
2207 iline
.l_addr
.l_symndx
= indx
;
2212 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
,
2218 if (bfd_seek (output_bfd
,
2219 (o
->output_section
->line_filepos
2220 + o
->output_section
->lineno_count
* linesz
),
2222 || (bfd_write (finfo
->linenos
, 1, oeline
- finfo
->linenos
,
2224 != (bfd_size_type
) (oeline
- finfo
->linenos
)))
2227 o
->output_section
->lineno_count
+=
2228 (oeline
- finfo
->linenos
) / linesz
;
2232 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2233 symbol will be the first symbol in the next input file. In the
2234 normal case, this will save us from writing out the C_FILE symbol
2236 if (finfo
->last_file_index
!= -1
2237 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2239 finfo
->last_file
.n_value
= output_index
;
2240 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2241 (PTR
) (finfo
->outsyms
2242 + ((finfo
->last_file_index
- syment_base
)
2246 /* Write the modified symbols to the output file. */
2247 if (outsym
> finfo
->outsyms
)
2249 if (bfd_seek (output_bfd
,
2250 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2252 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2254 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2257 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2258 + (outsym
- finfo
->outsyms
) / osymesz
)
2261 obj_raw_syment_count (output_bfd
) = output_index
;
2264 /* Relocate the contents of each section. */
2265 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2266 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2269 struct coff_section_tdata
*secdata
;
2271 if (! o
->linker_mark
)
2273 /* This section was omitted from the link. */
2277 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2278 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2280 if ((o
->flags
& SEC_RELOC
) != 0
2281 && o
->reloc_count
!= 0)
2283 ((*_bfd_error_handler
)
2284 (_("%s: relocs in section `%s', but it has no contents"),
2285 bfd_get_filename (input_bfd
),
2286 bfd_get_section_name (input_bfd
, o
)));
2287 bfd_set_error (bfd_error_no_contents
);
2294 secdata
= coff_section_data (input_bfd
, o
);
2295 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2296 contents
= secdata
->contents
;
2299 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2300 (file_ptr
) 0, o
->_raw_size
))
2302 contents
= finfo
->contents
;
2305 if ((o
->flags
& SEC_RELOC
) != 0)
2308 struct internal_reloc
*internal_relocs
;
2309 struct internal_reloc
*irel
;
2311 /* Read in the relocs. */
2312 target_index
= o
->output_section
->target_index
;
2313 internal_relocs
= (_bfd_coff_read_internal_relocs
2314 (input_bfd
, o
, false, finfo
->external_relocs
,
2315 finfo
->info
->relocateable
,
2316 (finfo
->info
->relocateable
2317 ? (finfo
->section_info
[target_index
].relocs
2318 + o
->output_section
->reloc_count
)
2319 : finfo
->internal_relocs
)));
2320 if (internal_relocs
== NULL
)
2323 /* Call processor specific code to relocate the section
2325 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2329 finfo
->internal_syms
,
2333 if (finfo
->info
->relocateable
)
2336 struct internal_reloc
*irelend
;
2337 struct coff_link_hash_entry
**rel_hash
;
2339 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2340 irel
= internal_relocs
;
2341 irelend
= irel
+ o
->reloc_count
;
2342 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2343 + o
->output_section
->reloc_count
);
2344 for (; irel
< irelend
; irel
++, rel_hash
++)
2346 struct coff_link_hash_entry
*h
;
2351 /* Adjust the reloc address and symbol index. */
2353 irel
->r_vaddr
+= offset
;
2355 if (irel
->r_symndx
== -1)
2360 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2368 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2371 /* This is a global symbol. */
2373 irel
->r_symndx
= h
->indx
;
2376 /* This symbol is being written at the end
2377 of the file, and we do not yet know the
2378 symbol index. We save the pointer to the
2379 hash table entry in the rel_hash list.
2380 We set the indx field to -2 to indicate
2381 that this symbol must not be stripped. */
2390 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2392 irel
->r_symndx
= indx
;
2395 struct internal_syment
*is
;
2397 char buf
[SYMNMLEN
+ 1];
2399 /* This reloc is against a symbol we are
2400 stripping. This should have been handled
2401 by the 'dont_skip_symbol' code in the while
2402 loop at the top of this function. */
2404 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2406 name
= (_bfd_coff_internal_syment_name
2407 (input_bfd
, is
, buf
));
2411 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2412 (finfo
->info
, name
, input_bfd
, o
,
2419 o
->output_section
->reloc_count
+= o
->reloc_count
;
2423 /* Write out the modified section contents. */
2424 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2426 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2430 bfd_octets_per_byte (output_bfd
)),
2431 (o
->_cooked_size
!= 0
2438 if (! (_bfd_write_section_stabs
2439 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2440 o
, &secdata
->stab_info
, contents
)))
2445 if (! finfo
->info
->keep_memory
)
2447 if (! _bfd_coff_free_symbols (input_bfd
))
2454 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2457 _bfd_coff_write_global_sym (h
, data
)
2458 struct coff_link_hash_entry
*h
;
2461 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2463 struct internal_syment isym
;
2464 bfd_size_type symesz
;
2467 output_bfd
= finfo
->output_bfd
;
2473 && (finfo
->info
->strip
== strip_all
2474 || (finfo
->info
->strip
== strip_some
2475 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2476 h
->root
.root
.string
, false, false)
2480 switch (h
->root
.type
)
2483 case bfd_link_hash_new
:
2487 case bfd_link_hash_undefined
:
2488 case bfd_link_hash_undefweak
:
2489 isym
.n_scnum
= N_UNDEF
;
2493 case bfd_link_hash_defined
:
2494 case bfd_link_hash_defweak
:
2498 sec
= h
->root
.u
.def
.section
->output_section
;
2499 if (bfd_is_abs_section (sec
))
2500 isym
.n_scnum
= N_ABS
;
2502 isym
.n_scnum
= sec
->target_index
;
2503 isym
.n_value
= (h
->root
.u
.def
.value
2504 + h
->root
.u
.def
.section
->output_offset
);
2505 if (! obj_pe (finfo
->output_bfd
))
2506 isym
.n_value
+= sec
->vma
;
2510 case bfd_link_hash_common
:
2511 isym
.n_scnum
= N_UNDEF
;
2512 isym
.n_value
= h
->root
.u
.c
.size
;
2515 case bfd_link_hash_indirect
:
2516 case bfd_link_hash_warning
:
2517 /* Just ignore these. They can't be handled anyhow. */
2521 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2522 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2529 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2531 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2533 if (indx
== (bfd_size_type
) -1)
2535 finfo
->failed
= true;
2538 isym
._n
._n_n
._n_zeroes
= 0;
2539 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2542 isym
.n_sclass
= h
->class;
2543 isym
.n_type
= h
->type
;
2545 if (isym
.n_sclass
== C_NULL
)
2546 isym
.n_sclass
= C_EXT
;
2548 /* If doing task linking and this is the pass where we convert
2549 defined globals to statics, then do that conversion now. If the
2550 symbol is not being converted, just ignore it and it will be
2551 output during a later pass. */
2552 if (finfo
->global_to_static
)
2554 if (isym
.n_sclass
!= C_EXT
2555 && isym
.n_sclass
!= C_WEAKEXT
2556 && (! obj_pe (output_bfd
) || isym
.n_sclass
!= C_NT_WEAK
))
2560 isym
.n_sclass
= C_STAT
;
2563 isym
.n_numaux
= h
->numaux
;
2565 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2567 symesz
= bfd_coff_symesz (output_bfd
);
2569 if (bfd_seek (output_bfd
,
2570 (obj_sym_filepos (output_bfd
)
2571 + obj_raw_syment_count (output_bfd
) * symesz
),
2573 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2575 finfo
->failed
= true;
2579 h
->indx
= obj_raw_syment_count (output_bfd
);
2581 ++obj_raw_syment_count (output_bfd
);
2583 /* Write out any associated aux entries. Most of the aux entries
2584 will have been modified in _bfd_coff_link_input_bfd. We have to
2585 handle section aux entries here, now that we have the final
2586 relocation and line number counts. */
2587 for (i
= 0; i
< isym
.n_numaux
; i
++)
2589 union internal_auxent
*auxp
;
2593 /* Look for a section aux entry here using the same tests that
2594 coff_swap_aux_out uses. */
2596 && (isym
.n_sclass
== C_STAT
2597 || isym
.n_sclass
== C_HIDDEN
)
2598 && isym
.n_type
== T_NULL
2599 && (h
->root
.type
== bfd_link_hash_defined
2600 || h
->root
.type
== bfd_link_hash_defweak
))
2604 sec
= h
->root
.u
.def
.section
->output_section
;
2607 auxp
->x_scn
.x_scnlen
= (sec
->_cooked_size
!= 0
2611 /* For PE, an overflow on the final link reportedly does
2612 not matter. FIXME: Why not? */
2614 if (sec
->reloc_count
> 0xffff
2615 && (! obj_pe (output_bfd
)
2616 || finfo
->info
->relocateable
))
2617 (*_bfd_error_handler
)
2618 (_("%s: %s: reloc overflow: 0x%lx > 0xffff"),
2619 bfd_get_filename (output_bfd
),
2620 bfd_get_section_name (output_bfd
, sec
),
2623 if (sec
->lineno_count
> 0xffff
2624 && (! obj_pe (output_bfd
)
2625 || finfo
->info
->relocateable
))
2626 (*_bfd_error_handler
)
2627 (_("%s: warning: %s: line number overflow: 0x%lx > 0xffff"),
2628 bfd_get_filename (output_bfd
),
2629 bfd_get_section_name (output_bfd
, sec
),
2632 auxp
->x_scn
.x_nreloc
= sec
->reloc_count
;
2633 auxp
->x_scn
.x_nlinno
= sec
->lineno_count
;
2634 auxp
->x_scn
.x_checksum
= 0;
2635 auxp
->x_scn
.x_associated
= 0;
2636 auxp
->x_scn
.x_comdat
= 0;
2640 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isym
.n_type
,
2641 isym
.n_sclass
, i
, isym
.n_numaux
,
2642 (PTR
) finfo
->outsyms
);
2643 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2645 finfo
->failed
= true;
2648 ++obj_raw_syment_count (output_bfd
);
2654 /* Write out task global symbols, converting them to statics. Called
2655 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2656 the dirty work, if the symbol we are processing needs conversion. */
2659 _bfd_coff_write_task_globals (h
, data
)
2660 struct coff_link_hash_entry
*h
;
2663 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2664 boolean rtnval
= true;
2665 boolean save_global_to_static
;
2669 switch (h
->root
.type
)
2671 case bfd_link_hash_defined
:
2672 case bfd_link_hash_defweak
:
2673 save_global_to_static
= finfo
->global_to_static
;
2674 finfo
->global_to_static
= true;
2675 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2676 finfo
->global_to_static
= save_global_to_static
;
2685 /* Handle a link order which is supposed to generate a reloc. */
2688 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2690 struct coff_final_link_info
*finfo
;
2691 asection
*output_section
;
2692 struct bfd_link_order
*link_order
;
2694 reloc_howto_type
*howto
;
2695 struct internal_reloc
*irel
;
2696 struct coff_link_hash_entry
**rel_hash_ptr
;
2698 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2701 bfd_set_error (bfd_error_bad_value
);
2705 if (link_order
->u
.reloc
.p
->addend
!= 0)
2709 bfd_reloc_status_type rstat
;
2712 size
= bfd_get_reloc_size (howto
);
2713 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2717 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2718 link_order
->u
.reloc
.p
->addend
, buf
);
2724 case bfd_reloc_outofrange
:
2726 case bfd_reloc_overflow
:
2727 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2729 (link_order
->type
== bfd_section_reloc_link_order
2730 ? bfd_section_name (output_bfd
,
2731 link_order
->u
.reloc
.p
->u
.section
)
2732 : link_order
->u
.reloc
.p
->u
.name
),
2733 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2734 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2741 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2743 (link_order
->offset
*
2744 bfd_octets_per_byte (output_bfd
)), size
);
2750 /* Store the reloc information in the right place. It will get
2751 swapped and written out at the end of the final_link routine. */
2753 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2754 + output_section
->reloc_count
);
2755 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2756 + output_section
->reloc_count
);
2758 memset (irel
, 0, sizeof (struct internal_reloc
));
2759 *rel_hash_ptr
= NULL
;
2761 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2763 if (link_order
->type
== bfd_section_reloc_link_order
)
2765 /* We need to somehow locate a symbol in the right section. The
2766 symbol must either have a value of zero, or we must adjust
2767 the addend by the value of the symbol. FIXME: Write this
2768 when we need it. The old linker couldn't handle this anyhow. */
2770 *rel_hash_ptr
= NULL
;
2775 struct coff_link_hash_entry
*h
;
2777 h
= ((struct coff_link_hash_entry
*)
2778 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2779 link_order
->u
.reloc
.p
->u
.name
,
2780 false, false, true));
2784 irel
->r_symndx
= h
->indx
;
2787 /* Set the index to -2 to force this symbol to get
2796 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2797 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2798 (asection
*) NULL
, (bfd_vma
) 0)))
2804 /* FIXME: Is this always right? */
2805 irel
->r_type
= howto
->type
;
2807 /* r_size is only used on the RS/6000, which needs its own linker
2808 routines anyhow. r_extern is only used for ECOFF. */
2810 /* FIXME: What is the right value for r_offset? Is zero OK? */
2812 ++output_section
->reloc_count
;
2817 /* A basic reloc handling routine which may be used by processors with
2821 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2822 input_section
, contents
, relocs
, syms
,
2825 struct bfd_link_info
*info
;
2827 asection
*input_section
;
2829 struct internal_reloc
*relocs
;
2830 struct internal_syment
*syms
;
2831 asection
**sections
;
2833 struct internal_reloc
*rel
;
2834 struct internal_reloc
*relend
;
2837 relend
= rel
+ input_section
->reloc_count
;
2838 for (; rel
< relend
; rel
++)
2841 struct coff_link_hash_entry
*h
;
2842 struct internal_syment
*sym
;
2845 reloc_howto_type
*howto
;
2846 bfd_reloc_status_type rstat
;
2848 symndx
= rel
->r_symndx
;
2856 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2858 (*_bfd_error_handler
)
2859 ("%s: illegal symbol index %ld in relocs",
2860 bfd_get_filename (input_bfd
), symndx
);
2865 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2866 sym
= syms
+ symndx
;
2869 /* COFF treats common symbols in one of two ways. Either the
2870 size of the symbol is included in the section contents, or it
2871 is not. We assume that the size is not included, and force
2872 the rtype_to_howto function to adjust the addend as needed. */
2874 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2875 addend
= - sym
->n_value
;
2880 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2885 /* If we are doing a relocateable link, then we can just ignore
2886 a PC relative reloc that is pcrel_offset. It will already
2887 have the correct value. If this is not a relocateable link,
2888 then we should ignore the symbol value. */
2889 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2891 if (info
->relocateable
)
2893 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2894 addend
+= sym
->n_value
;
2905 sec
= bfd_abs_section_ptr
;
2910 sec
= sections
[symndx
];
2911 val
= (sec
->output_section
->vma
2912 + sec
->output_offset
2914 if (! obj_pe (input_bfd
))
2920 if (h
->root
.type
== bfd_link_hash_defined
2921 || h
->root
.type
== bfd_link_hash_defweak
)
2925 sec
= h
->root
.u
.def
.section
;
2926 val
= (h
->root
.u
.def
.value
2927 + sec
->output_section
->vma
2928 + sec
->output_offset
);
2931 else if (! info
->relocateable
)
2933 if (! ((*info
->callbacks
->undefined_symbol
)
2934 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2935 rel
->r_vaddr
- input_section
->vma
, true)))
2940 if (info
->base_file
)
2942 /* Emit a reloc if the backend thinks it needs it. */
2943 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2945 /* Relocation to a symbol in a section which isn't
2946 absolute. We output the address here to a file.
2947 This file is then read by dlltool when generating the
2948 reloc section. Note that the base file is not
2949 portable between systems. We write out a long here,
2950 and dlltool reads in a long. */
2951 long addr
= (rel
->r_vaddr
2952 - input_section
->vma
2953 + input_section
->output_offset
2954 + input_section
->output_section
->vma
);
2955 if (coff_data (output_bfd
)->pe
)
2956 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2957 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2960 bfd_set_error (bfd_error_system_call
);
2966 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2968 rel
->r_vaddr
- input_section
->vma
,
2977 case bfd_reloc_outofrange
:
2978 (*_bfd_error_handler
)
2979 (_("%s: bad reloc address 0x%lx in section `%s'"),
2980 bfd_get_filename (input_bfd
),
2981 (unsigned long) rel
->r_vaddr
,
2982 bfd_get_section_name (input_bfd
, input_section
));
2984 case bfd_reloc_overflow
:
2987 char buf
[SYMNMLEN
+ 1];
2992 name
= h
->root
.root
.string
;
2995 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
3000 if (! ((*info
->callbacks
->reloc_overflow
)
3001 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
3002 input_section
, rel
->r_vaddr
- input_section
->vma
)))