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
;
299 /* These aren't needed yet. */
300 unsigned int n_tmask
= coff_data (abfd
)->local_n_tmask
;
301 unsigned int n_btshft
= coff_data (abfd
)->local_n_btshft
;
302 unsigned int n_btmask
= coff_data (abfd
)->local_n_btmask
;
305 boolean default_copy
;
306 bfd_size_type symcount
;
307 struct coff_link_hash_entry
**sym_hash
;
308 bfd_size_type symesz
;
312 /* Keep the symbols during this function, in case the linker needs
313 to read the generic symbols in order to report an error message. */
314 keep_syms
= obj_coff_keep_syms (abfd
);
315 obj_coff_keep_syms (abfd
) = true;
317 if (info
->keep_memory
)
318 default_copy
= false;
322 symcount
= obj_raw_syment_count (abfd
);
324 /* We keep a list of the linker hash table entries that correspond
325 to particular symbols. */
326 sym_hash
= ((struct coff_link_hash_entry
**)
329 * sizeof (struct coff_link_hash_entry
*))));
330 if (sym_hash
== NULL
&& symcount
!= 0)
332 obj_coff_sym_hashes (abfd
) = sym_hash
;
334 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
336 symesz
= bfd_coff_symesz (abfd
);
337 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
338 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
339 esym_end
= esym
+ symcount
* symesz
;
340 while (esym
< esym_end
)
342 struct internal_syment sym
;
343 enum coff_symbol_classification classification
;
346 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
348 classification
= bfd_coff_classify_symbol (abfd
, &sym
);
349 if (classification
!= COFF_SYMBOL_LOCAL
)
352 char buf
[SYMNMLEN
+ 1];
358 /* This symbol is externally visible. */
360 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
364 /* We must copy the name into memory if we got it from the
365 syment itself, rather than the string table. */
367 if (sym
._n
._n_n
._n_zeroes
!= 0
368 || sym
._n
._n_n
._n_offset
== 0)
373 switch (classification
)
378 case COFF_SYMBOL_GLOBAL
:
379 flags
= BSF_EXPORT
| BSF_GLOBAL
;
380 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
382 value
-= section
->vma
;
385 case COFF_SYMBOL_UNDEFINED
:
387 section
= bfd_und_section_ptr
;
390 case COFF_SYMBOL_COMMON
:
392 section
= bfd_com_section_ptr
;
395 case COFF_SYMBOL_PE_SECTION
:
396 flags
= BSF_SECTION_SYM
| BSF_GLOBAL
;
397 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
401 if (sym
.n_sclass
== C_WEAKEXT
402 || (obj_pe (abfd
) && sym
.n_sclass
== C_NT_WEAK
))
407 /* In the PE format, section symbols actually refer to the
408 start of the output section. We handle them specially
410 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
412 *sym_hash
= coff_link_hash_lookup (coff_hash_table (info
),
413 name
, false, copy
, false);
414 if (*sym_hash
!= NULL
)
416 if (((*sym_hash
)->coff_link_hash_flags
417 & COFF_LINK_HASH_PE_SECTION_SYMBOL
) == 0
418 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefined
419 && (*sym_hash
)->root
.type
!= bfd_link_hash_undefweak
)
420 (*_bfd_error_handler
)
421 ("Warning: symbol `%s' is both section and non-section",
430 if (! (bfd_coff_link_add_one_symbol
431 (info
, abfd
, name
, flags
, section
, value
,
432 (const char *) NULL
, copy
, false,
433 (struct bfd_link_hash_entry
**) sym_hash
)))
437 if (obj_pe (abfd
) && (flags
& BSF_SECTION_SYM
) != 0)
438 (*sym_hash
)->coff_link_hash_flags
|=
439 COFF_LINK_HASH_PE_SECTION_SYMBOL
;
441 /* Limit the alignment of a common symbol to the possible
442 alignment of a section. There is no point to permitting
443 a higher alignment for a common symbol: we can not
444 guarantee it, and it may cause us to allocate extra space
445 in the common section. */
446 if (section
== bfd_com_section_ptr
447 && (*sym_hash
)->root
.type
== bfd_link_hash_common
448 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
449 > bfd_coff_default_section_alignment_power (abfd
)))
450 (*sym_hash
)->root
.u
.c
.p
->alignment_power
451 = bfd_coff_default_section_alignment_power (abfd
);
453 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
455 if (((*sym_hash
)->class == C_NULL
456 && (*sym_hash
)->type
== T_NULL
)
459 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
460 && (*sym_hash
)->root
.type
!= bfd_link_hash_defweak
))
462 (*sym_hash
)->class = sym
.n_sclass
;
463 if (sym
.n_type
!= T_NULL
)
465 if ((*sym_hash
)->type
!= T_NULL
466 && (*sym_hash
)->type
!= sym
.n_type
)
467 (*_bfd_error_handler
)
468 (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
469 name
, (*sym_hash
)->type
, sym
.n_type
,
470 bfd_get_filename (abfd
));
471 (*sym_hash
)->type
= sym
.n_type
;
473 (*sym_hash
)->auxbfd
= abfd
;
474 if (sym
.n_numaux
!= 0)
476 union internal_auxent
*alloc
;
479 union internal_auxent
*iaux
;
481 (*sym_hash
)->numaux
= sym
.n_numaux
;
482 alloc
= ((union internal_auxent
*)
483 bfd_hash_allocate (&info
->hash
->table
,
485 * sizeof (*alloc
))));
488 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
490 i
++, eaux
+= symesz
, iaux
++)
491 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
492 sym
.n_sclass
, i
, sym
.n_numaux
,
494 (*sym_hash
)->aux
= alloc
;
499 if (classification
== COFF_SYMBOL_PE_SECTION
500 && (*sym_hash
)->numaux
!= 0)
502 /* Some PE sections (such as .bss) have a zero size in
503 the section header, but a non-zero size in the AUX
504 record. Correct that here.
506 FIXME: This is not at all the right place to do this.
507 For example, it won't help objdump. This needs to be
508 done when we swap in the section header. */
510 BFD_ASSERT ((*sym_hash
)->numaux
== 1);
511 if (section
->_raw_size
== 0)
512 section
->_raw_size
= (*sym_hash
)->aux
[0].x_scn
.x_scnlen
;
514 /* FIXME: We could test whether the section sizes
515 matches the size in the aux entry, but apparently
516 that sometimes fails unexpectedly. */
520 esym
+= (sym
.n_numaux
+ 1) * symesz
;
521 sym_hash
+= sym
.n_numaux
+ 1;
524 /* If this is a non-traditional, non-relocateable link, try to
525 optimize the handling of any .stab/.stabstr sections. */
526 if (! info
->relocateable
527 && ! info
->traditional_format
528 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
529 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
531 asection
*stab
, *stabstr
;
533 stab
= bfd_get_section_by_name (abfd
, ".stab");
536 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
540 struct coff_link_hash_table
*table
;
541 struct coff_section_tdata
*secdata
;
543 secdata
= coff_section_data (abfd
, stab
);
547 (PTR
) bfd_zalloc (abfd
,
548 sizeof (struct coff_section_tdata
));
549 if (stab
->used_by_bfd
== NULL
)
551 secdata
= coff_section_data (abfd
, stab
);
554 table
= coff_hash_table (info
);
556 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
558 &secdata
->stab_info
))
564 obj_coff_keep_syms (abfd
) = keep_syms
;
569 obj_coff_keep_syms (abfd
) = keep_syms
;
573 /* Do the final link step. */
576 _bfd_coff_final_link (abfd
, info
)
578 struct bfd_link_info
*info
;
580 bfd_size_type symesz
;
581 struct coff_final_link_info finfo
;
582 boolean debug_merge_allocated
;
583 boolean long_section_names
;
585 struct bfd_link_order
*p
;
586 size_t max_sym_count
;
587 size_t max_lineno_count
;
588 size_t max_reloc_count
;
589 size_t max_output_reloc_count
;
590 size_t max_contents_size
;
591 file_ptr rel_filepos
;
593 file_ptr line_filepos
;
596 bfd_byte
*external_relocs
= NULL
;
597 char strbuf
[STRING_SIZE_SIZE
];
599 symesz
= bfd_coff_symesz (abfd
);
602 finfo
.output_bfd
= abfd
;
604 finfo
.section_info
= NULL
;
605 finfo
.last_file_index
= -1;
606 finfo
.last_bf_index
= -1;
607 finfo
.internal_syms
= NULL
;
608 finfo
.sec_ptrs
= NULL
;
609 finfo
.sym_indices
= NULL
;
610 finfo
.outsyms
= NULL
;
611 finfo
.linenos
= NULL
;
612 finfo
.contents
= NULL
;
613 finfo
.external_relocs
= NULL
;
614 finfo
.internal_relocs
= NULL
;
615 finfo
.global_to_static
= false;
616 debug_merge_allocated
= false;
618 coff_data (abfd
)->link_info
= info
;
620 finfo
.strtab
= _bfd_stringtab_init ();
621 if (finfo
.strtab
== NULL
)
624 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
626 debug_merge_allocated
= true;
628 /* Compute the file positions for all the sections. */
629 if (! abfd
->output_has_begun
)
631 if (! bfd_coff_compute_section_file_positions (abfd
))
635 /* Count the line numbers and relocation entries required for the
636 output file. Set the file positions for the relocs. */
637 rel_filepos
= obj_relocbase (abfd
);
638 relsz
= bfd_coff_relsz (abfd
);
639 max_contents_size
= 0;
640 max_lineno_count
= 0;
643 long_section_names
= false;
644 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
648 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
650 if (p
->type
== bfd_indirect_link_order
)
654 sec
= p
->u
.indirect
.section
;
656 /* Mark all sections which are to be included in the
657 link. This will normally be every section. We need
658 to do this so that we can identify any sections which
659 the linker has decided to not include. */
660 sec
->linker_mark
= true;
662 if (info
->strip
== strip_none
663 || info
->strip
== strip_some
)
664 o
->lineno_count
+= sec
->lineno_count
;
666 if (info
->relocateable
)
667 o
->reloc_count
+= sec
->reloc_count
;
669 if (sec
->_raw_size
> max_contents_size
)
670 max_contents_size
= sec
->_raw_size
;
671 if (sec
->lineno_count
> max_lineno_count
)
672 max_lineno_count
= sec
->lineno_count
;
673 if (sec
->reloc_count
> max_reloc_count
)
674 max_reloc_count
= sec
->reloc_count
;
676 else if (info
->relocateable
677 && (p
->type
== bfd_section_reloc_link_order
678 || p
->type
== bfd_symbol_reloc_link_order
))
681 if (o
->reloc_count
== 0)
685 o
->flags
|= SEC_RELOC
;
686 o
->rel_filepos
= rel_filepos
;
687 rel_filepos
+= o
->reloc_count
* relsz
;
690 if (bfd_coff_long_section_names (abfd
)
691 && strlen (o
->name
) > SCNNMLEN
)
693 /* This section has a long name which must go in the string
694 table. This must correspond to the code in
695 coff_write_object_contents which puts the string index
696 into the s_name field of the section header. That is why
697 we pass hash as false. */
698 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
699 == (bfd_size_type
) -1)
701 long_section_names
= true;
705 /* If doing a relocateable link, allocate space for the pointers we
707 if (info
->relocateable
)
711 /* We use section_count + 1, rather than section_count, because
712 the target_index fields are 1 based. */
714 ((struct coff_link_section_info
*)
715 bfd_malloc ((abfd
->section_count
+ 1)
716 * sizeof (struct coff_link_section_info
)));
717 if (finfo
.section_info
== NULL
)
719 for (i
= 0; i
<= abfd
->section_count
; i
++)
721 finfo
.section_info
[i
].relocs
= NULL
;
722 finfo
.section_info
[i
].rel_hashes
= NULL
;
726 /* We now know the size of the relocs, so we can determine the file
727 positions of the line numbers. */
728 line_filepos
= rel_filepos
;
729 linesz
= bfd_coff_linesz (abfd
);
730 max_output_reloc_count
= 0;
731 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
733 if (o
->lineno_count
== 0)
737 o
->line_filepos
= line_filepos
;
738 line_filepos
+= o
->lineno_count
* linesz
;
741 if (o
->reloc_count
!= 0)
743 /* We don't know the indices of global symbols until we have
744 written out all the local symbols. For each section in
745 the output file, we keep an array of pointers to hash
746 table entries. Each entry in the array corresponds to a
747 reloc. When we find a reloc against a global symbol, we
748 set the corresponding entry in this array so that we can
749 fix up the symbol index after we have written out all the
752 Because of this problem, we also keep the relocs in
753 memory until the end of the link. This wastes memory,
754 but only when doing a relocateable link, which is not the
756 BFD_ASSERT (info
->relocateable
);
757 finfo
.section_info
[o
->target_index
].relocs
=
758 ((struct internal_reloc
*)
759 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
760 finfo
.section_info
[o
->target_index
].rel_hashes
=
761 ((struct coff_link_hash_entry
**)
762 bfd_malloc (o
->reloc_count
763 * sizeof (struct coff_link_hash_entry
*)));
764 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
765 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
768 if (o
->reloc_count
> max_output_reloc_count
)
769 max_output_reloc_count
= o
->reloc_count
;
772 /* Reset the reloc and lineno counts, so that we can use them to
773 count the number of entries we have output so far. */
778 obj_sym_filepos (abfd
) = line_filepos
;
780 /* Figure out the largest number of symbols in an input BFD. Take
781 the opportunity to clear the output_has_begun fields of all the
784 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
788 sub
->output_has_begun
= false;
789 sz
= obj_raw_syment_count (sub
);
790 if (sz
> max_sym_count
)
794 /* Allocate some buffers used while linking. */
795 finfo
.internal_syms
= ((struct internal_syment
*)
796 bfd_malloc (max_sym_count
797 * sizeof (struct internal_syment
)));
798 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
799 * sizeof (asection
*));
800 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
801 finfo
.outsyms
= ((bfd_byte
*)
802 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
803 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
804 * bfd_coff_linesz (abfd
));
805 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
806 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
807 if (! info
->relocateable
)
808 finfo
.internal_relocs
= ((struct internal_reloc
*)
809 bfd_malloc (max_reloc_count
810 * sizeof (struct internal_reloc
)));
811 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
812 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
813 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
814 || finfo
.outsyms
== NULL
815 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
816 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
817 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
818 || (! info
->relocateable
819 && finfo
.internal_relocs
== NULL
820 && max_reloc_count
> 0))
823 /* We now know the position of everything in the file, except that
824 we don't know the size of the symbol table and therefore we don't
825 know where the string table starts. We just build the string
826 table in memory as we go along. We process all the relocations
827 for a single input file at once. */
828 obj_raw_syment_count (abfd
) = 0;
830 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
832 if (! bfd_coff_start_final_link (abfd
, info
))
836 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
838 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
840 if (p
->type
== bfd_indirect_link_order
841 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
842 == bfd_target_coff_flavour
))
844 sub
= p
->u
.indirect
.section
->owner
;
845 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
847 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
849 sub
->output_has_begun
= true;
852 else if (p
->type
== bfd_section_reloc_link_order
853 || p
->type
== bfd_symbol_reloc_link_order
)
855 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
860 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
866 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
869 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
871 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
872 debug_merge_allocated
= false;
874 if (finfo
.internal_syms
!= NULL
)
876 free (finfo
.internal_syms
);
877 finfo
.internal_syms
= NULL
;
879 if (finfo
.sec_ptrs
!= NULL
)
881 free (finfo
.sec_ptrs
);
882 finfo
.sec_ptrs
= NULL
;
884 if (finfo
.sym_indices
!= NULL
)
886 free (finfo
.sym_indices
);
887 finfo
.sym_indices
= NULL
;
889 if (finfo
.linenos
!= NULL
)
891 free (finfo
.linenos
);
892 finfo
.linenos
= NULL
;
894 if (finfo
.contents
!= NULL
)
896 free (finfo
.contents
);
897 finfo
.contents
= NULL
;
899 if (finfo
.external_relocs
!= NULL
)
901 free (finfo
.external_relocs
);
902 finfo
.external_relocs
= NULL
;
904 if (finfo
.internal_relocs
!= NULL
)
906 free (finfo
.internal_relocs
);
907 finfo
.internal_relocs
= NULL
;
910 /* The value of the last C_FILE symbol is supposed to be the symbol
911 index of the first external symbol. Write it out again if
913 if (finfo
.last_file_index
!= -1
914 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
916 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
917 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
918 (PTR
) finfo
.outsyms
);
920 (obj_sym_filepos (abfd
)
921 + finfo
.last_file_index
* symesz
),
923 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
927 /* If doing task linking (ld --task-link) then make a pass through the
928 global symbols, writing out any that are defined, and making them
932 finfo
.failed
= false;
933 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
939 /* Write out the global symbols. */
940 finfo
.failed
= false;
941 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
946 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
947 if (finfo
.outsyms
!= NULL
)
949 free (finfo
.outsyms
);
950 finfo
.outsyms
= NULL
;
953 if (info
->relocateable
&& max_output_reloc_count
> 0)
955 /* Now that we have written out all the global symbols, we know
956 the symbol indices to use for relocs against them, and we can
957 finally write out the relocs. */
958 external_relocs
= ((bfd_byte
*)
959 bfd_malloc (max_output_reloc_count
* relsz
));
960 if (external_relocs
== NULL
)
963 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
965 struct internal_reloc
*irel
;
966 struct internal_reloc
*irelend
;
967 struct coff_link_hash_entry
**rel_hash
;
970 if (o
->reloc_count
== 0)
973 irel
= finfo
.section_info
[o
->target_index
].relocs
;
974 irelend
= irel
+ o
->reloc_count
;
975 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
976 erel
= external_relocs
;
977 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
979 if (*rel_hash
!= NULL
)
981 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
982 irel
->r_symndx
= (*rel_hash
)->indx
;
984 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
987 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
988 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
989 abfd
) != relsz
* o
->reloc_count
)
993 free (external_relocs
);
994 external_relocs
= NULL
;
997 /* Free up the section information. */
998 if (finfo
.section_info
!= NULL
)
1002 for (i
= 0; i
< abfd
->section_count
; i
++)
1004 if (finfo
.section_info
[i
].relocs
!= NULL
)
1005 free (finfo
.section_info
[i
].relocs
);
1006 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1007 free (finfo
.section_info
[i
].rel_hashes
);
1009 free (finfo
.section_info
);
1010 finfo
.section_info
= NULL
;
1013 /* If we have optimized stabs strings, output them. */
1014 if (coff_hash_table (info
)->stab_info
!= NULL
)
1016 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
1020 /* Write out the string table. */
1021 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
1024 (obj_sym_filepos (abfd
)
1025 + obj_raw_syment_count (abfd
) * symesz
),
1029 #if STRING_SIZE_SIZE == 4
1031 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1032 (bfd_byte
*) strbuf
);
1034 #error Change bfd_h_put_32
1037 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1040 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1044 _bfd_stringtab_free (finfo
.strtab
);
1046 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1047 not try to write out the symbols. */
1048 bfd_get_symcount (abfd
) = 0;
1053 if (debug_merge_allocated
)
1054 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
1055 if (finfo
.strtab
!= NULL
)
1056 _bfd_stringtab_free (finfo
.strtab
);
1057 if (finfo
.section_info
!= NULL
)
1061 for (i
= 0; i
< abfd
->section_count
; i
++)
1063 if (finfo
.section_info
[i
].relocs
!= NULL
)
1064 free (finfo
.section_info
[i
].relocs
);
1065 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1066 free (finfo
.section_info
[i
].rel_hashes
);
1068 free (finfo
.section_info
);
1070 if (finfo
.internal_syms
!= NULL
)
1071 free (finfo
.internal_syms
);
1072 if (finfo
.sec_ptrs
!= NULL
)
1073 free (finfo
.sec_ptrs
);
1074 if (finfo
.sym_indices
!= NULL
)
1075 free (finfo
.sym_indices
);
1076 if (finfo
.outsyms
!= NULL
)
1077 free (finfo
.outsyms
);
1078 if (finfo
.linenos
!= NULL
)
1079 free (finfo
.linenos
);
1080 if (finfo
.contents
!= NULL
)
1081 free (finfo
.contents
);
1082 if (finfo
.external_relocs
!= NULL
)
1083 free (finfo
.external_relocs
);
1084 if (finfo
.internal_relocs
!= NULL
)
1085 free (finfo
.internal_relocs
);
1086 if (external_relocs
!= NULL
)
1087 free (external_relocs
);
1091 /* parse out a -heap <reserved>,<commit> line */
1094 dores_com (ptr
, output_bfd
, heap
)
1099 if (coff_data(output_bfd
)->pe
)
1101 int val
= strtoul (ptr
, &ptr
, 0);
1103 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1105 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1109 int val
= strtoul (ptr
+1, &ptr
, 0);
1111 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1113 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1119 static char *get_name(ptr
, dst
)
1126 while (*ptr
&& *ptr
!= ' ')
1132 /* Process any magic embedded commands in a section called .drectve */
1135 process_embedded_commands (output_bfd
, info
, abfd
)
1137 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1140 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1147 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1150 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1155 e
= copy
+ sec
->_raw_size
;
1156 for (s
= copy
; s
< e
; )
1162 if (strncmp (s
,"-attr", 5) == 0)
1174 s
= get_name(s
, &name
);
1175 s
= get_name(s
, &attribs
);
1195 asec
= bfd_get_section_by_name (abfd
, name
);
1198 asec
->flags
|= SEC_CODE
;
1200 asec
->flags
|= SEC_READONLY
;
1203 else if (strncmp (s
,"-heap", 5) == 0)
1205 s
= dores_com (s
+5, output_bfd
, 1);
1207 else if (strncmp (s
,"-stack", 6) == 0)
1209 s
= dores_com (s
+6, output_bfd
, 0);
1218 /* Place a marker against all symbols which are used by relocations.
1219 This marker can be picked up by the 'do we skip this symbol ?'
1220 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1225 mark_relocs (finfo
, input_bfd
)
1226 struct coff_final_link_info
* finfo
;
1231 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1234 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1236 struct internal_reloc
* internal_relocs
;
1237 struct internal_reloc
* irel
;
1238 struct internal_reloc
* irelend
;
1241 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1244 /* Read in the relocs. */
1245 internal_relocs
= _bfd_coff_read_internal_relocs
1246 (input_bfd
, a
, false,
1247 finfo
->external_relocs
,
1248 finfo
->info
->relocateable
,
1249 (finfo
->info
->relocateable
1250 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1251 : finfo
->internal_relocs
)
1254 if (internal_relocs
== NULL
)
1257 irel
= internal_relocs
;
1258 irelend
= irel
+ a
->reloc_count
;
1260 /* Place a mark in the sym_indices array (whose entries have
1261 been initialised to 0) for all of the symbols that are used
1262 in the relocation table. This will then be picked up in the
1265 for (; irel
< irelend
; irel
++)
1267 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1272 /* Link an input file into the linker output file. This function
1273 handles all the sections and relocations of the input file at once. */
1276 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1277 struct coff_final_link_info
*finfo
;
1280 unsigned int n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1281 unsigned int n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1283 unsigned int n_btmask
= coff_data (input_bfd
)->local_n_btmask
;
1285 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1286 asection
*, struct internal_reloc
*,
1289 const char *strings
;
1290 bfd_size_type syment_base
;
1292 bfd_size_type isymesz
;
1293 bfd_size_type osymesz
;
1294 bfd_size_type linesz
;
1297 struct internal_syment
*isymp
;
1300 unsigned long output_index
;
1302 struct coff_link_hash_entry
**sym_hash
;
1305 /* Move all the symbols to the output file. */
1307 output_bfd
= finfo
->output_bfd
;
1309 syment_base
= obj_raw_syment_count (output_bfd
);
1310 isymesz
= bfd_coff_symesz (input_bfd
);
1311 osymesz
= bfd_coff_symesz (output_bfd
);
1312 linesz
= bfd_coff_linesz (input_bfd
);
1313 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1316 if (! finfo
->info
->keep_memory
)
1319 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1322 if (! _bfd_coff_get_external_symbols (input_bfd
))
1325 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1326 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1327 isymp
= finfo
->internal_syms
;
1328 secpp
= finfo
->sec_ptrs
;
1329 indexp
= finfo
->sym_indices
;
1330 output_index
= syment_base
;
1331 outsym
= finfo
->outsyms
;
1333 if (coff_data (output_bfd
)->pe
)
1335 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1339 /* If we are going to perform relocations and also strip/discard some symbols
1340 then we must make sure that we do not strip/discard those symbols that are
1341 going to be involved in the relocations */
1342 if (( finfo
->info
->strip
!= strip_none
1343 || finfo
->info
->discard
!= discard_none
)
1344 && finfo
->info
->relocateable
)
1346 /* mark the symbol array as 'not-used' */
1347 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1349 mark_relocs (finfo
, input_bfd
);
1352 while (esym
< esym_end
)
1354 struct internal_syment isym
;
1355 enum coff_symbol_classification classification
;
1358 boolean dont_skip_symbol
;
1361 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1363 /* Make a copy of *isymp so that the relocate_section function
1364 always sees the original values. This is more reliable than
1365 always recomputing the symbol value even if we are stripping
1369 classification
= bfd_coff_classify_symbol (input_bfd
, &isym
);
1370 switch (classification
)
1374 case COFF_SYMBOL_GLOBAL
:
1375 case COFF_SYMBOL_PE_SECTION
:
1376 case COFF_SYMBOL_LOCAL
:
1377 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1379 case COFF_SYMBOL_COMMON
:
1380 *secpp
= bfd_com_section_ptr
;
1382 case COFF_SYMBOL_UNDEFINED
:
1383 *secpp
= bfd_und_section_ptr
;
1387 /* Extract the flag indicating if this symbol is used by a
1389 if ((finfo
->info
->strip
!= strip_none
1390 || finfo
->info
->discard
!= discard_none
)
1391 && finfo
->info
->relocateable
)
1392 dont_skip_symbol
= *indexp
;
1394 dont_skip_symbol
= false;
1400 add
= 1 + isym
.n_numaux
;
1402 /* If we are stripping all symbols, we want to skip this one. */
1403 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1408 switch (classification
)
1412 case COFF_SYMBOL_GLOBAL
:
1413 case COFF_SYMBOL_COMMON
:
1414 case COFF_SYMBOL_PE_SECTION
:
1415 /* This is a global symbol. Global symbols come at the
1416 end of the symbol table, so skip them for now.
1417 Locally defined function symbols, however, are an
1418 exception, and are not moved to the end. */
1420 if (! ISFCN (isym
.n_type
))
1424 case COFF_SYMBOL_UNDEFINED
:
1425 /* Undefined symbols are left for the end. */
1430 case COFF_SYMBOL_LOCAL
:
1431 /* This is a local symbol. Skip it if we are discarding
1433 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1439 /* If we stripping debugging symbols, and this is a debugging
1440 symbol, then skip it. FIXME: gas sets the section to N_ABS
1441 for some types of debugging symbols; I don't know if this is
1442 a bug or not. In any case, we handle it here. */
1444 && finfo
->info
->strip
== strip_debugger
1445 && ! dont_skip_symbol
1446 && (isym
.n_scnum
== N_DEBUG
1447 || (isym
.n_scnum
== N_ABS
1448 && (isym
.n_sclass
== C_AUTO
1449 || isym
.n_sclass
== C_REG
1450 || isym
.n_sclass
== C_MOS
1451 || isym
.n_sclass
== C_MOE
1452 || isym
.n_sclass
== C_MOU
1453 || isym
.n_sclass
== C_ARG
1454 || isym
.n_sclass
== C_REGPARM
1455 || isym
.n_sclass
== C_FIELD
1456 || isym
.n_sclass
== C_EOS
))))
1459 /* If some symbols are stripped based on the name, work out the
1460 name and decide whether to skip this symbol. */
1462 && (finfo
->info
->strip
== strip_some
1463 || finfo
->info
->discard
== discard_l
))
1466 char buf
[SYMNMLEN
+ 1];
1468 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1472 if (! dont_skip_symbol
1473 && ((finfo
->info
->strip
== strip_some
1474 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1477 && finfo
->info
->discard
== discard_l
1478 && bfd_is_local_label_name (input_bfd
, name
))))
1482 /* If this is an enum, struct, or union tag, see if we have
1483 already output an identical type. */
1485 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1486 && (isym
.n_sclass
== C_ENTAG
1487 || isym
.n_sclass
== C_STRTAG
1488 || isym
.n_sclass
== C_UNTAG
)
1489 && isym
.n_numaux
== 1)
1492 char buf
[SYMNMLEN
+ 1];
1493 struct coff_debug_merge_hash_entry
*mh
;
1494 struct coff_debug_merge_type
*mt
;
1495 union internal_auxent aux
;
1496 struct coff_debug_merge_element
**epp
;
1497 bfd_byte
*esl
, *eslend
;
1498 struct internal_syment
*islp
;
1500 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1504 /* Ignore fake names invented by compiler; treat them all as
1506 if (*name
== '~' || *name
== '.' || *name
== '$'
1507 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1508 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1511 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1516 /* Allocate memory to hold type information. If this turns
1517 out to be a duplicate, we pass this address to
1519 mt
= ((struct coff_debug_merge_type
*)
1520 bfd_alloc (input_bfd
,
1521 sizeof (struct coff_debug_merge_type
)));
1524 mt
->class = isym
.n_sclass
;
1526 /* Pick up the aux entry, which points to the end of the tag
1528 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1529 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1532 /* Gather the elements. */
1533 epp
= &mt
->elements
;
1534 mt
->elements
= NULL
;
1536 esl
= esym
+ 2 * isymesz
;
1537 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1538 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1539 while (esl
< eslend
)
1541 const char *elename
;
1542 char elebuf
[SYMNMLEN
+ 1];
1545 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1547 *epp
= ((struct coff_debug_merge_element
*)
1548 bfd_alloc (input_bfd
,
1549 sizeof (struct coff_debug_merge_element
)));
1553 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1555 if (elename
== NULL
)
1558 name_copy
= (char *) bfd_alloc (input_bfd
,
1559 strlen (elename
) + 1);
1560 if (name_copy
== NULL
)
1562 strcpy (name_copy
, elename
);
1564 (*epp
)->name
= name_copy
;
1565 (*epp
)->type
= islp
->n_type
;
1567 if (islp
->n_numaux
>= 1
1568 && islp
->n_type
!= T_NULL
1569 && islp
->n_sclass
!= C_EOS
)
1571 union internal_auxent eleaux
;
1574 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1575 islp
->n_type
, islp
->n_sclass
, 0,
1576 islp
->n_numaux
, (PTR
) &eleaux
);
1577 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1579 /* FIXME: If this tagndx entry refers to a symbol
1580 defined later in this file, we just ignore it.
1581 Handling this correctly would be tedious, and may
1587 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1590 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1591 if ((*epp
)->tagndx
< 0)
1595 epp
= &(*epp
)->next
;
1598 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1599 islp
+= islp
->n_numaux
+ 1;
1602 /* See if we already have a definition which matches this
1603 type. We always output the type if it has no elements,
1605 if (mt
->elements
== NULL
)
1606 bfd_release (input_bfd
, (PTR
) mt
);
1609 struct coff_debug_merge_type
*mtl
;
1611 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1613 struct coff_debug_merge_element
*me
, *mel
;
1615 if (mtl
->class != mt
->class)
1618 for (me
= mt
->elements
, mel
= mtl
->elements
;
1619 me
!= NULL
&& mel
!= NULL
;
1620 me
= me
->next
, mel
= mel
->next
)
1622 if (strcmp (me
->name
, mel
->name
) != 0
1623 || me
->type
!= mel
->type
1624 || me
->tagndx
!= mel
->tagndx
)
1628 if (me
== NULL
&& mel
== NULL
)
1632 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1634 /* This is the first definition of this type. */
1635 mt
->indx
= output_index
;
1636 mt
->next
= mh
->types
;
1641 /* This is a redefinition which can be merged. */
1642 bfd_release (input_bfd
, (PTR
) mt
);
1643 *indexp
= mtl
->indx
;
1644 add
= (eslend
- esym
) / isymesz
;
1650 /* We now know whether we are to skip this symbol or not. */
1653 /* Adjust the symbol in order to output it. */
1655 if (isym
._n
._n_n
._n_zeroes
== 0
1656 && isym
._n
._n_n
._n_offset
!= 0)
1661 /* This symbol has a long name. Enter it in the string
1662 table we are building. Note that we do not check
1663 bfd_coff_symname_in_debug. That is only true for
1664 XCOFF, and XCOFF requires different linking code
1666 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1670 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1671 if (indx
== (bfd_size_type
) -1)
1673 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1676 if (isym
.n_scnum
> 0)
1678 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1679 isym
.n_value
+= (*secpp
)->output_offset
;
1680 if (! obj_pe (input_bfd
))
1681 isym
.n_value
-= (*secpp
)->vma
;
1682 if (! obj_pe (finfo
->output_bfd
))
1683 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1686 /* The value of a C_FILE symbol is the symbol index of the
1687 next C_FILE symbol. The value of the last C_FILE symbol
1688 is the symbol index to the first external symbol
1689 (actually, coff_renumber_symbols does not get this
1690 right--it just sets the value of the last C_FILE symbol
1691 to zero--and nobody has ever complained about it). We
1692 try to get this right, below, just before we write the
1693 symbols out, but in the general case we may have to write
1694 the symbol out twice. */
1695 if (isym
.n_sclass
== C_FILE
)
1697 if (finfo
->last_file_index
!= -1
1698 && finfo
->last_file
.n_value
!= (long) output_index
)
1700 /* We must correct the value of the last C_FILE entry. */
1701 finfo
->last_file
.n_value
= output_index
;
1702 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1704 /* The last C_FILE symbol is in this input file. */
1705 bfd_coff_swap_sym_out (output_bfd
,
1706 (PTR
) &finfo
->last_file
,
1707 (PTR
) (finfo
->outsyms
1708 + ((finfo
->last_file_index
1714 /* We have already written out the last C_FILE
1715 symbol. We need to write it out again. We
1716 borrow *outsym temporarily. */
1717 bfd_coff_swap_sym_out (output_bfd
,
1718 (PTR
) &finfo
->last_file
,
1720 if (bfd_seek (output_bfd
,
1721 (obj_sym_filepos (output_bfd
)
1722 + finfo
->last_file_index
* osymesz
),
1724 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1730 finfo
->last_file_index
= output_index
;
1731 finfo
->last_file
= isym
;
1734 /* If doing task linking, convert normal global function symbols to
1735 static functions. */
1737 if (finfo
->info
->task_link
1738 && (isym
.n_sclass
== C_EXT
1739 || isym
.n_sclass
== C_WEAKEXT
1740 || (obj_pe (input_bfd
) && isym
.n_sclass
== C_NT_WEAK
)))
1741 isym
.n_sclass
= C_STAT
;
1743 /* Output the symbol. */
1745 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1747 *indexp
= output_index
;
1752 struct coff_link_hash_entry
*h
;
1754 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1756 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1759 /* This can happen if there were errors earlier in
1761 bfd_set_error (bfd_error_bad_value
);
1764 h
->indx
= output_index
;
1767 output_index
+= add
;
1768 outsym
+= add
* osymesz
;
1771 esym
+= add
* isymesz
;
1775 for (--add
; add
> 0; --add
)
1782 /* Fix up the aux entries. This must be done in a separate pass,
1783 because we don't know the correct symbol indices until we have
1784 already decided which symbols we are going to keep. */
1786 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1787 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1788 isymp
= finfo
->internal_syms
;
1789 indexp
= finfo
->sym_indices
;
1790 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1791 outsym
= finfo
->outsyms
;
1792 while (esym
< esym_end
)
1796 add
= 1 + isymp
->n_numaux
;
1799 || (bfd_size_type
) *indexp
< syment_base
)
1800 && (*sym_hash
== NULL
1801 || (*sym_hash
)->auxbfd
!= input_bfd
))
1802 esym
+= add
* isymesz
;
1805 struct coff_link_hash_entry
*h
;
1813 /* The m68k-motorola-sysv assembler will sometimes
1814 generate two symbols with the same name, but only one
1815 will have aux entries. */
1816 BFD_ASSERT (isymp
->n_numaux
== 0
1817 || h
->numaux
== isymp
->n_numaux
);
1825 /* Handle the aux entries. This handling is based on
1826 coff_pointerize_aux. I don't know if it always correct. */
1827 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1829 union internal_auxent aux
;
1830 union internal_auxent
*auxp
;
1836 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1837 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1842 if (isymp
->n_sclass
== C_FILE
)
1844 /* If this is a long filename, we must put it in the
1846 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1847 && auxp
->x_file
.x_n
.x_offset
!= 0)
1849 const char *filename
;
1852 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1853 >= STRING_SIZE_SIZE
);
1854 if (strings
== NULL
)
1856 strings
= _bfd_coff_read_string_table (input_bfd
);
1857 if (strings
== NULL
)
1860 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1861 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1863 if (indx
== (bfd_size_type
) -1)
1865 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1868 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1872 if (ISFCN (isymp
->n_type
)
1873 || ISTAG (isymp
->n_sclass
)
1874 || isymp
->n_sclass
== C_BLOCK
1875 || isymp
->n_sclass
== C_FCN
)
1877 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1879 && indx
< obj_raw_syment_count (input_bfd
))
1881 /* We look forward through the symbol for
1882 the index of the next symbol we are going
1883 to include. I don't know if this is
1885 while ((finfo
->sym_indices
[indx
] < 0
1886 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1888 && indx
< obj_raw_syment_count (input_bfd
))
1890 if (indx
>= obj_raw_syment_count (input_bfd
))
1891 indx
= output_index
;
1893 indx
= finfo
->sym_indices
[indx
];
1894 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1898 indx
= auxp
->x_sym
.x_tagndx
.l
;
1899 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1903 symindx
= finfo
->sym_indices
[indx
];
1905 auxp
->x_sym
.x_tagndx
.l
= 0;
1907 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1910 /* The .bf symbols are supposed to be linked through
1911 the endndx field. We need to carry this list
1912 across object files. */
1915 && isymp
->n_sclass
== C_FCN
1916 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1917 || isymp
->_n
._n_n
._n_offset
== 0)
1918 && isymp
->_n
._n_name
[0] == '.'
1919 && isymp
->_n
._n_name
[1] == 'b'
1920 && isymp
->_n
._n_name
[2] == 'f'
1921 && isymp
->_n
._n_name
[3] == '\0')
1923 if (finfo
->last_bf_index
!= -1)
1925 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1928 if ((bfd_size_type
) finfo
->last_bf_index
1933 /* The last .bf symbol is in this input
1934 file. This will only happen if the
1935 assembler did not set up the .bf
1936 endndx symbols correctly. */
1937 auxout
= (PTR
) (finfo
->outsyms
1938 + ((finfo
->last_bf_index
1941 bfd_coff_swap_aux_out (output_bfd
,
1942 (PTR
) &finfo
->last_bf
,
1950 /* We have already written out the last
1951 .bf aux entry. We need to write it
1952 out again. We borrow *outsym
1953 temporarily. FIXME: This case should
1955 bfd_coff_swap_aux_out (output_bfd
,
1956 (PTR
) &finfo
->last_bf
,
1961 if (bfd_seek (output_bfd
,
1962 (obj_sym_filepos (output_bfd
)
1963 + finfo
->last_bf_index
* osymesz
),
1965 || bfd_write (outsym
, osymesz
, 1,
1966 output_bfd
) != osymesz
)
1971 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1972 finfo
->last_bf_index
= -1;
1975 /* The endndx field of this aux entry must
1976 be updated with the symbol number of the
1978 finfo
->last_bf
= *auxp
;
1979 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1988 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1989 isymp
->n_sclass
, i
, isymp
->n_numaux
,
2003 /* Relocate the line numbers, unless we are stripping them. */
2004 if (finfo
->info
->strip
== strip_none
2005 || finfo
->info
->strip
== strip_some
)
2007 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2013 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
2014 build_link_order in ldwrite.c will not have created a
2015 link order, which means that we will not have seen this
2016 input section in _bfd_coff_final_link, which means that
2017 we will not have allocated space for the line numbers of
2018 this section. I don't think line numbers can be
2019 meaningful for a section which does not have
2020 SEC_HAS_CONTENTS set, but, if they do, this must be
2022 if (o
->lineno_count
== 0
2023 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
2026 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
2027 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
2028 input_bfd
) != linesz
* o
->lineno_count
)
2031 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2032 eline
= finfo
->linenos
;
2033 elineend
= eline
+ linesz
* o
->lineno_count
;
2034 for (; eline
< elineend
; eline
+= linesz
)
2036 struct internal_lineno iline
;
2038 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
2040 if (iline
.l_lnno
!= 0)
2041 iline
.l_addr
.l_paddr
+= offset
;
2042 else if (iline
.l_addr
.l_symndx
>= 0
2043 && ((unsigned long) iline
.l_addr
.l_symndx
2044 < obj_raw_syment_count (input_bfd
)))
2048 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
2052 /* These line numbers are attached to a symbol
2053 which we are stripping. We should really
2054 just discard the line numbers, but that would
2055 be a pain because we have already counted
2061 struct internal_syment is
;
2062 union internal_auxent ia
;
2064 /* Fix up the lnnoptr field in the aux entry of
2065 the symbol. It turns out that we can't do
2066 this when we modify the symbol aux entries,
2067 because gas sometimes screws up the lnnoptr
2068 field and makes it an offset from the start
2069 of the line numbers rather than an absolute
2071 bfd_coff_swap_sym_in (output_bfd
,
2072 (PTR
) (finfo
->outsyms
2073 + ((indx
- syment_base
)
2076 if ((ISFCN (is
.n_type
)
2077 || is
.n_sclass
== C_BLOCK
)
2078 && is
.n_numaux
>= 1)
2082 auxptr
= (PTR
) (finfo
->outsyms
2083 + ((indx
- syment_base
+ 1)
2085 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
2086 is
.n_type
, is
.n_sclass
,
2087 0, is
.n_numaux
, (PTR
) &ia
);
2088 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2089 (o
->output_section
->line_filepos
2090 + o
->output_section
->lineno_count
* linesz
2091 + eline
- finfo
->linenos
);
2092 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2093 is
.n_type
, is
.n_sclass
, 0,
2094 is
.n_numaux
, auxptr
);
2098 iline
.l_addr
.l_symndx
= indx
;
2101 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
2104 if (bfd_seek (output_bfd
,
2105 (o
->output_section
->line_filepos
2106 + o
->output_section
->lineno_count
* linesz
),
2108 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
2109 output_bfd
) != linesz
* o
->lineno_count
)
2112 o
->output_section
->lineno_count
+= o
->lineno_count
;
2116 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2117 symbol will be the first symbol in the next input file. In the
2118 normal case, this will save us from writing out the C_FILE symbol
2120 if (finfo
->last_file_index
!= -1
2121 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2123 finfo
->last_file
.n_value
= output_index
;
2124 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2125 (PTR
) (finfo
->outsyms
2126 + ((finfo
->last_file_index
- syment_base
)
2130 /* Write the modified symbols to the output file. */
2131 if (outsym
> finfo
->outsyms
)
2133 if (bfd_seek (output_bfd
,
2134 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2136 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2138 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2141 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2142 + (outsym
- finfo
->outsyms
) / osymesz
)
2145 obj_raw_syment_count (output_bfd
) = output_index
;
2148 /* Relocate the contents of each section. */
2149 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2150 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2153 struct coff_section_tdata
*secdata
;
2155 if (! o
->linker_mark
)
2157 /* This section was omitted from the link. */
2161 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2162 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2164 if ((o
->flags
& SEC_RELOC
) != 0
2165 && o
->reloc_count
!= 0)
2167 ((*_bfd_error_handler
)
2168 (_("%s: relocs in section `%s', but it has no contents"),
2169 bfd_get_filename (input_bfd
),
2170 bfd_get_section_name (input_bfd
, o
)));
2171 bfd_set_error (bfd_error_no_contents
);
2178 secdata
= coff_section_data (input_bfd
, o
);
2179 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2180 contents
= secdata
->contents
;
2183 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2184 (file_ptr
) 0, o
->_raw_size
))
2186 contents
= finfo
->contents
;
2189 if ((o
->flags
& SEC_RELOC
) != 0)
2192 struct internal_reloc
*internal_relocs
;
2193 struct internal_reloc
*irel
;
2195 /* Read in the relocs. */
2196 target_index
= o
->output_section
->target_index
;
2197 internal_relocs
= (_bfd_coff_read_internal_relocs
2198 (input_bfd
, o
, false, finfo
->external_relocs
,
2199 finfo
->info
->relocateable
,
2200 (finfo
->info
->relocateable
2201 ? (finfo
->section_info
[target_index
].relocs
2202 + o
->output_section
->reloc_count
)
2203 : finfo
->internal_relocs
)));
2204 if (internal_relocs
== NULL
)
2207 /* Call processor specific code to relocate the section
2209 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2213 finfo
->internal_syms
,
2217 if (finfo
->info
->relocateable
)
2220 struct internal_reloc
*irelend
;
2221 struct coff_link_hash_entry
**rel_hash
;
2223 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2224 irel
= internal_relocs
;
2225 irelend
= irel
+ o
->reloc_count
;
2226 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2227 + o
->output_section
->reloc_count
);
2228 for (; irel
< irelend
; irel
++, rel_hash
++)
2230 struct coff_link_hash_entry
*h
;
2235 /* Adjust the reloc address and symbol index. */
2237 irel
->r_vaddr
+= offset
;
2239 if (irel
->r_symndx
== -1)
2244 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2252 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2255 /* This is a global symbol. */
2257 irel
->r_symndx
= h
->indx
;
2260 /* This symbol is being written at the end
2261 of the file, and we do not yet know the
2262 symbol index. We save the pointer to the
2263 hash table entry in the rel_hash list.
2264 We set the indx field to -2 to indicate
2265 that this symbol must not be stripped. */
2274 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2276 irel
->r_symndx
= indx
;
2279 struct internal_syment
*is
;
2281 char buf
[SYMNMLEN
+ 1];
2283 /* This reloc is against a symbol we are
2284 stripping. This should have been handled
2285 by the 'dont_skip_symbol' code in the while
2286 loop at the top of this function. */
2288 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2290 name
= (_bfd_coff_internal_syment_name
2291 (input_bfd
, is
, buf
));
2295 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2296 (finfo
->info
, name
, input_bfd
, o
,
2303 o
->output_section
->reloc_count
+= o
->reloc_count
;
2307 /* Write out the modified section contents. */
2308 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2310 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2311 contents
, o
->output_offset
,
2312 (o
->_cooked_size
!= 0
2319 if (! (_bfd_write_section_stabs
2320 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2321 o
, &secdata
->stab_info
, contents
)))
2326 if (! finfo
->info
->keep_memory
)
2328 if (! _bfd_coff_free_symbols (input_bfd
))
2335 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2338 _bfd_coff_write_global_sym (h
, data
)
2339 struct coff_link_hash_entry
*h
;
2342 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2344 struct internal_syment isym
;
2345 bfd_size_type symesz
;
2348 output_bfd
= finfo
->output_bfd
;
2354 && (finfo
->info
->strip
== strip_all
2355 || (finfo
->info
->strip
== strip_some
2356 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2357 h
->root
.root
.string
, false, false)
2361 switch (h
->root
.type
)
2364 case bfd_link_hash_new
:
2368 case bfd_link_hash_undefined
:
2369 case bfd_link_hash_undefweak
:
2370 isym
.n_scnum
= N_UNDEF
;
2374 case bfd_link_hash_defined
:
2375 case bfd_link_hash_defweak
:
2379 sec
= h
->root
.u
.def
.section
->output_section
;
2380 if (bfd_is_abs_section (sec
))
2381 isym
.n_scnum
= N_ABS
;
2383 isym
.n_scnum
= sec
->target_index
;
2384 isym
.n_value
= (h
->root
.u
.def
.value
2385 + h
->root
.u
.def
.section
->output_offset
);
2386 if (! obj_pe (finfo
->output_bfd
))
2387 isym
.n_value
+= sec
->vma
;
2391 case bfd_link_hash_common
:
2392 isym
.n_scnum
= N_UNDEF
;
2393 isym
.n_value
= h
->root
.u
.c
.size
;
2396 case bfd_link_hash_indirect
:
2397 case bfd_link_hash_warning
:
2398 /* Just ignore these. They can't be handled anyhow. */
2402 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2403 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2410 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2412 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2414 if (indx
== (bfd_size_type
) -1)
2416 finfo
->failed
= true;
2419 isym
._n
._n_n
._n_zeroes
= 0;
2420 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2423 isym
.n_sclass
= h
->class;
2424 isym
.n_type
= h
->type
;
2426 if (isym
.n_sclass
== C_NULL
)
2427 isym
.n_sclass
= C_EXT
;
2429 /* If doing task linking and this is the pass where we convert
2430 defined globals to statics, then do that conversion now. If the
2431 symbol is not being converted, just ignore it and it will be
2432 output during a later pass. */
2433 if (finfo
->global_to_static
)
2435 if (isym
.n_sclass
!= C_EXT
2436 && isym
.n_sclass
!= C_WEAKEXT
2437 && (! obj_pe (output_bfd
) || isym
.n_sclass
!= C_NT_WEAK
))
2441 isym
.n_sclass
= C_STAT
;
2444 isym
.n_numaux
= h
->numaux
;
2446 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2448 symesz
= bfd_coff_symesz (output_bfd
);
2450 if (bfd_seek (output_bfd
,
2451 (obj_sym_filepos (output_bfd
)
2452 + obj_raw_syment_count (output_bfd
) * symesz
),
2454 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2456 finfo
->failed
= true;
2460 h
->indx
= obj_raw_syment_count (output_bfd
);
2462 ++obj_raw_syment_count (output_bfd
);
2464 /* Write out any associated aux entries. There normally will be
2465 none. If there are any, I have no idea how to modify them. */
2466 for (i
= 0; i
< isym
.n_numaux
; i
++)
2468 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2469 isym
.n_sclass
, i
, isym
.n_numaux
,
2470 (PTR
) finfo
->outsyms
);
2471 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2473 finfo
->failed
= true;
2476 ++obj_raw_syment_count (output_bfd
);
2482 /* Write out task global symbols, converting them to statics. Called
2483 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2484 the dirty work, if the symbol we are processing needs conversion. */
2487 _bfd_coff_write_task_globals (h
, data
)
2488 struct coff_link_hash_entry
*h
;
2491 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2492 boolean rtnval
= true;
2493 boolean save_global_to_static
;
2497 switch (h
->root
.type
)
2499 case bfd_link_hash_defined
:
2500 case bfd_link_hash_defweak
:
2501 save_global_to_static
= finfo
->global_to_static
;
2502 finfo
->global_to_static
= true;
2503 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2504 finfo
->global_to_static
= save_global_to_static
;
2513 /* Handle a link order which is supposed to generate a reloc. */
2516 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2518 struct coff_final_link_info
*finfo
;
2519 asection
*output_section
;
2520 struct bfd_link_order
*link_order
;
2522 reloc_howto_type
*howto
;
2523 struct internal_reloc
*irel
;
2524 struct coff_link_hash_entry
**rel_hash_ptr
;
2526 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2529 bfd_set_error (bfd_error_bad_value
);
2533 if (link_order
->u
.reloc
.p
->addend
!= 0)
2537 bfd_reloc_status_type rstat
;
2540 size
= bfd_get_reloc_size (howto
);
2541 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2545 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2546 link_order
->u
.reloc
.p
->addend
, buf
);
2552 case bfd_reloc_outofrange
:
2554 case bfd_reloc_overflow
:
2555 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2557 (link_order
->type
== bfd_section_reloc_link_order
2558 ? bfd_section_name (output_bfd
,
2559 link_order
->u
.reloc
.p
->u
.section
)
2560 : link_order
->u
.reloc
.p
->u
.name
),
2561 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2562 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2569 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2570 (file_ptr
) link_order
->offset
, size
);
2576 /* Store the reloc information in the right place. It will get
2577 swapped and written out at the end of the final_link routine. */
2579 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2580 + output_section
->reloc_count
);
2581 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2582 + output_section
->reloc_count
);
2584 memset (irel
, 0, sizeof (struct internal_reloc
));
2585 *rel_hash_ptr
= NULL
;
2587 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2589 if (link_order
->type
== bfd_section_reloc_link_order
)
2591 /* We need to somehow locate a symbol in the right section. The
2592 symbol must either have a value of zero, or we must adjust
2593 the addend by the value of the symbol. FIXME: Write this
2594 when we need it. The old linker couldn't handle this anyhow. */
2596 *rel_hash_ptr
= NULL
;
2601 struct coff_link_hash_entry
*h
;
2603 h
= ((struct coff_link_hash_entry
*)
2604 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2605 link_order
->u
.reloc
.p
->u
.name
,
2606 false, false, true));
2610 irel
->r_symndx
= h
->indx
;
2613 /* Set the index to -2 to force this symbol to get
2622 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2623 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2624 (asection
*) NULL
, (bfd_vma
) 0)))
2630 /* FIXME: Is this always right? */
2631 irel
->r_type
= howto
->type
;
2633 /* r_size is only used on the RS/6000, which needs its own linker
2634 routines anyhow. r_extern is only used for ECOFF. */
2636 /* FIXME: What is the right value for r_offset? Is zero OK? */
2638 ++output_section
->reloc_count
;
2643 /* A basic reloc handling routine which may be used by processors with
2647 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2648 input_section
, contents
, relocs
, syms
,
2651 struct bfd_link_info
*info
;
2653 asection
*input_section
;
2655 struct internal_reloc
*relocs
;
2656 struct internal_syment
*syms
;
2657 asection
**sections
;
2659 struct internal_reloc
*rel
;
2660 struct internal_reloc
*relend
;
2663 relend
= rel
+ input_section
->reloc_count
;
2664 for (; rel
< relend
; rel
++)
2667 struct coff_link_hash_entry
*h
;
2668 struct internal_syment
*sym
;
2671 reloc_howto_type
*howto
;
2672 bfd_reloc_status_type rstat
;
2674 symndx
= rel
->r_symndx
;
2682 || (unsigned long) symndx
>= obj_raw_syment_count (input_bfd
))
2684 (*_bfd_error_handler
)
2685 ("%s: illegal symbol index %ld in relocs",
2686 bfd_get_filename (input_bfd
), symndx
);
2691 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2692 sym
= syms
+ symndx
;
2695 /* COFF treats common symbols in one of two ways. Either the
2696 size of the symbol is included in the section contents, or it
2697 is not. We assume that the size is not included, and force
2698 the rtype_to_howto function to adjust the addend as needed. */
2700 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2701 addend
= - sym
->n_value
;
2706 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2711 /* If we are doing a relocateable link, then we can just ignore
2712 a PC relative reloc that is pcrel_offset. It will already
2713 have the correct value. If this is not a relocateable link,
2714 then we should ignore the symbol value. */
2715 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2717 if (info
->relocateable
)
2719 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2720 addend
+= sym
->n_value
;
2731 sec
= bfd_abs_section_ptr
;
2736 sec
= sections
[symndx
];
2737 val
= (sec
->output_section
->vma
2738 + sec
->output_offset
2740 if (! obj_pe (input_bfd
))
2746 if (h
->root
.type
== bfd_link_hash_defined
2747 || h
->root
.type
== bfd_link_hash_defweak
)
2751 sec
= h
->root
.u
.def
.section
;
2752 val
= (h
->root
.u
.def
.value
2753 + sec
->output_section
->vma
2754 + sec
->output_offset
);
2757 else if (! info
->relocateable
)
2759 if (! ((*info
->callbacks
->undefined_symbol
)
2760 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2761 rel
->r_vaddr
- input_section
->vma
)))
2766 if (info
->base_file
)
2768 /* Emit a reloc if the backend thinks it needs it. */
2769 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2771 /* Relocation to a symbol in a section which isn't
2772 absolute. We output the address here to a file.
2773 This file is then read by dlltool when generating the
2774 reloc section. Note that the base file is not
2775 portable between systems. We write out a long here,
2776 and dlltool reads in a long. */
2777 long addr
= (rel
->r_vaddr
2778 - input_section
->vma
2779 + input_section
->output_offset
2780 + input_section
->output_section
->vma
);
2781 if (coff_data (output_bfd
)->pe
)
2782 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2783 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2786 bfd_set_error (bfd_error_system_call
);
2792 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2794 rel
->r_vaddr
- input_section
->vma
,
2803 case bfd_reloc_outofrange
:
2804 (*_bfd_error_handler
)
2805 (_("%s: bad reloc address 0x%lx in section `%s'"),
2806 bfd_get_filename (input_bfd
),
2807 (unsigned long) rel
->r_vaddr
,
2808 bfd_get_section_name (input_bfd
, input_section
));
2810 case bfd_reloc_overflow
:
2813 char buf
[SYMNMLEN
+ 1];
2818 name
= h
->root
.root
.string
;
2821 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2826 if (! ((*info
->callbacks
->reloc_overflow
)
2827 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2828 input_section
, rel
->r_vaddr
- input_section
->vma
)))