1 /* COFF specific linker code.
2 Copyright 1994, 1995, 1996, 1997 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 /* Create an entry in a COFF linker hash table. */
45 struct bfd_hash_entry
*
46 _bfd_coff_link_hash_newfunc (entry
, table
, string
)
47 struct bfd_hash_entry
*entry
;
48 struct bfd_hash_table
*table
;
51 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
53 /* Allocate the structure if it has not already been allocated by a
55 if (ret
== (struct coff_link_hash_entry
*) NULL
)
56 ret
= ((struct coff_link_hash_entry
*)
57 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
58 if (ret
== (struct coff_link_hash_entry
*) NULL
)
59 return (struct bfd_hash_entry
*) ret
;
61 /* Call the allocation method of the superclass. */
62 ret
= ((struct coff_link_hash_entry
*)
63 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
65 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
67 /* Set local fields. */
76 return (struct bfd_hash_entry
*) ret
;
79 /* Initialize a COFF linker hash table. */
82 _bfd_coff_link_hash_table_init (table
, abfd
, newfunc
)
83 struct coff_link_hash_table
*table
;
85 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
86 struct bfd_hash_table
*,
89 table
->stab_info
= NULL
;
90 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
93 /* Create a COFF linker hash table. */
95 struct bfd_link_hash_table
*
96 _bfd_coff_link_hash_table_create (abfd
)
99 struct coff_link_hash_table
*ret
;
101 ret
= ((struct coff_link_hash_table
*)
102 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
105 if (! _bfd_coff_link_hash_table_init (ret
, abfd
,
106 _bfd_coff_link_hash_newfunc
))
108 bfd_release (abfd
, ret
);
109 return (struct bfd_link_hash_table
*) NULL
;
114 /* Create an entry in a COFF debug merge hash table. */
116 struct bfd_hash_entry
*
117 _bfd_coff_debug_merge_hash_newfunc (entry
, table
, string
)
118 struct bfd_hash_entry
*entry
;
119 struct bfd_hash_table
*table
;
122 struct coff_debug_merge_hash_entry
*ret
=
123 (struct coff_debug_merge_hash_entry
*) entry
;
125 /* Allocate the structure if it has not already been allocated by a
127 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
128 ret
= ((struct coff_debug_merge_hash_entry
*)
129 bfd_hash_allocate (table
,
130 sizeof (struct coff_debug_merge_hash_entry
)));
131 if (ret
== (struct coff_debug_merge_hash_entry
*) NULL
)
132 return (struct bfd_hash_entry
*) ret
;
134 /* Call the allocation method of the superclass. */
135 ret
= ((struct coff_debug_merge_hash_entry
*)
136 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
137 if (ret
!= (struct coff_debug_merge_hash_entry
*) NULL
)
139 /* Set local fields. */
143 return (struct bfd_hash_entry
*) ret
;
146 /* Given a COFF BFD, add symbols to the global hash table as
150 _bfd_coff_link_add_symbols (abfd
, info
)
152 struct bfd_link_info
*info
;
154 switch (bfd_get_format (abfd
))
157 return coff_link_add_object_symbols (abfd
, info
);
159 return (_bfd_generic_link_add_archive_symbols
160 (abfd
, info
, coff_link_check_archive_element
));
162 bfd_set_error (bfd_error_wrong_format
);
167 /* Add symbols from a COFF object file. */
170 coff_link_add_object_symbols (abfd
, info
)
172 struct bfd_link_info
*info
;
174 if (! _bfd_coff_get_external_symbols (abfd
))
176 if (! coff_link_add_symbols (abfd
, info
))
179 if (! info
->keep_memory
)
181 if (! _bfd_coff_free_symbols (abfd
))
187 /* Check a single archive element to see if we need to include it in
188 the link. *PNEEDED is set according to whether this element is
189 needed in the link or not. This is called via
190 _bfd_generic_link_add_archive_symbols. */
193 coff_link_check_archive_element (abfd
, info
, pneeded
)
195 struct bfd_link_info
*info
;
198 if (! _bfd_coff_get_external_symbols (abfd
))
201 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
206 if (! coff_link_add_symbols (abfd
, info
))
210 if (! info
->keep_memory
|| ! *pneeded
)
212 if (! _bfd_coff_free_symbols (abfd
))
219 /* Look through the symbols to see if this object file should be
220 included in the link. */
223 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
225 struct bfd_link_info
*info
;
228 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
229 bfd_size_type symesz
;
235 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
237 symesz
= bfd_coff_symesz (abfd
);
238 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
239 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
240 while (esym
< esym_end
)
242 struct internal_syment sym
;
244 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
246 if ((sym
.n_sclass
== C_EXT
247 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
248 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
251 char buf
[SYMNMLEN
+ 1];
252 struct bfd_link_hash_entry
*h
;
254 /* This symbol is externally visible, and is defined by this
257 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
260 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
262 /* We are only interested in symbols that are currently
263 undefined. If a symbol is currently known to be common,
264 COFF linkers do not bring in an object file which defines
266 if (h
!= (struct bfd_link_hash_entry
*) NULL
267 && h
->type
== bfd_link_hash_undefined
)
269 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
276 esym
+= (sym
.n_numaux
+ 1) * symesz
;
279 /* We do not need this object file. */
283 /* Add all the symbols from an object file to the hash table. */
286 coff_link_add_symbols (abfd
, info
)
288 struct bfd_link_info
*info
;
290 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
292 boolean default_copy
;
293 bfd_size_type symcount
;
294 struct coff_link_hash_entry
**sym_hash
;
295 bfd_size_type symesz
;
299 /* Keep the symbols during this function, in case the linker needs
300 to read the generic symbols in order to report an error message. */
301 keep_syms
= obj_coff_keep_syms (abfd
);
302 obj_coff_keep_syms (abfd
) = true;
304 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
306 if (info
->keep_memory
)
307 default_copy
= false;
311 symcount
= obj_raw_syment_count (abfd
);
313 /* We keep a list of the linker hash table entries that correspond
314 to particular symbols. */
315 sym_hash
= ((struct coff_link_hash_entry
**)
318 * sizeof (struct coff_link_hash_entry
*))));
319 if (sym_hash
== NULL
&& symcount
!= 0)
321 obj_coff_sym_hashes (abfd
) = sym_hash
;
323 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
325 symesz
= bfd_coff_symesz (abfd
);
326 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
327 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
328 esym_end
= esym
+ symcount
* symesz
;
329 while (esym
< esym_end
)
331 struct internal_syment sym
;
334 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
336 if (sym
.n_sclass
== C_EXT
337 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
340 char buf
[SYMNMLEN
+ 1];
345 /* This symbol is externally visible. */
347 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
351 /* We must copy the name into memory if we got it from the
352 syment itself, rather than the string table. */
354 if (sym
._n
._n_n
._n_zeroes
!= 0
355 || sym
._n
._n_n
._n_offset
== 0)
360 if (sym
.n_scnum
== 0)
365 section
= bfd_und_section_ptr
;
370 section
= bfd_com_section_ptr
;
375 flags
= BSF_EXPORT
| BSF_GLOBAL
;
376 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
378 value
-= section
->vma
;
381 if (! (bfd_coff_link_add_one_symbol
382 (info
, abfd
, name
, flags
, section
, value
,
383 (const char *) NULL
, copy
, false,
384 (struct bfd_link_hash_entry
**) sym_hash
)))
387 if (section
== bfd_com_section_ptr
388 && (*sym_hash
)->root
.type
== bfd_link_hash_common
389 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
390 > bfd_coff_default_section_alignment_power (abfd
)))
391 (*sym_hash
)->root
.u
.c
.p
->alignment_power
392 = bfd_coff_default_section_alignment_power (abfd
);
394 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
396 if (((*sym_hash
)->class == C_NULL
397 && (*sym_hash
)->type
== T_NULL
)
400 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
402 (*sym_hash
)->class = sym
.n_sclass
;
403 if (sym
.n_type
!= T_NULL
)
405 if ((*sym_hash
)->type
!= T_NULL
406 && (*sym_hash
)->type
!= sym
.n_type
)
407 (*_bfd_error_handler
)
408 ("Warning: type of symbol `%s' changed from %d to %d in %s",
409 name
, (*sym_hash
)->type
, sym
.n_type
,
410 bfd_get_filename (abfd
));
411 (*sym_hash
)->type
= sym
.n_type
;
413 (*sym_hash
)->auxbfd
= abfd
;
414 if (sym
.n_numaux
!= 0)
416 union internal_auxent
*alloc
;
419 union internal_auxent
*iaux
;
421 (*sym_hash
)->numaux
= sym
.n_numaux
;
422 alloc
= ((union internal_auxent
*)
423 bfd_hash_allocate (&info
->hash
->table
,
425 * sizeof (*alloc
))));
428 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
430 i
++, eaux
+= symesz
, iaux
++)
431 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
432 sym
.n_sclass
, i
, sym
.n_numaux
,
434 (*sym_hash
)->aux
= alloc
;
440 esym
+= (sym
.n_numaux
+ 1) * symesz
;
441 sym_hash
+= sym
.n_numaux
+ 1;
444 /* If this is a non-traditional, non-relocateable link, try to
445 optimize the handling of any .stab/.stabstr sections. */
446 if (! info
->relocateable
447 && ! info
->traditional_format
448 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
449 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
451 asection
*stab
, *stabstr
;
453 stab
= bfd_get_section_by_name (abfd
, ".stab");
456 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
460 struct coff_link_hash_table
*table
;
461 struct coff_section_tdata
*secdata
;
463 secdata
= coff_section_data (abfd
, stab
);
467 (PTR
) bfd_zalloc (abfd
,
468 sizeof (struct coff_section_tdata
));
469 if (stab
->used_by_bfd
== NULL
)
471 secdata
= coff_section_data (abfd
, stab
);
474 table
= coff_hash_table (info
);
476 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
478 &secdata
->stab_info
))
484 obj_coff_keep_syms (abfd
) = keep_syms
;
489 obj_coff_keep_syms (abfd
) = keep_syms
;
493 /* Do the final link step. */
496 _bfd_coff_final_link (abfd
, info
)
498 struct bfd_link_info
*info
;
500 bfd_size_type symesz
;
501 struct coff_final_link_info finfo
;
502 boolean debug_merge_allocated
;
503 boolean long_section_names
;
505 struct bfd_link_order
*p
;
506 size_t max_sym_count
;
507 size_t max_lineno_count
;
508 size_t max_reloc_count
;
509 size_t max_output_reloc_count
;
510 size_t max_contents_size
;
511 file_ptr rel_filepos
;
513 file_ptr line_filepos
;
516 bfd_byte
*external_relocs
= NULL
;
517 char strbuf
[STRING_SIZE_SIZE
];
519 symesz
= bfd_coff_symesz (abfd
);
522 finfo
.output_bfd
= abfd
;
524 finfo
.section_info
= NULL
;
525 finfo
.last_file_index
= -1;
526 finfo
.last_bf_index
= -1;
527 finfo
.internal_syms
= NULL
;
528 finfo
.sec_ptrs
= NULL
;
529 finfo
.sym_indices
= NULL
;
530 finfo
.outsyms
= NULL
;
531 finfo
.linenos
= NULL
;
532 finfo
.contents
= NULL
;
533 finfo
.external_relocs
= NULL
;
534 finfo
.internal_relocs
= NULL
;
535 finfo
.global_to_static
= false;
536 debug_merge_allocated
= false;
538 coff_data (abfd
)->link_info
= info
;
540 finfo
.strtab
= _bfd_stringtab_init ();
541 if (finfo
.strtab
== NULL
)
544 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
546 debug_merge_allocated
= true;
548 /* Compute the file positions for all the sections. */
549 if (! abfd
->output_has_begun
)
551 if (! bfd_coff_compute_section_file_positions (abfd
))
555 /* Count the line numbers and relocation entries required for the
556 output file. Set the file positions for the relocs. */
557 rel_filepos
= obj_relocbase (abfd
);
558 relsz
= bfd_coff_relsz (abfd
);
559 max_contents_size
= 0;
560 max_lineno_count
= 0;
563 long_section_names
= false;
564 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
568 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
570 if (p
->type
== bfd_indirect_link_order
)
574 sec
= p
->u
.indirect
.section
;
576 /* Mark all sections which are to be included in the
577 link. This will normally be every section. We need
578 to do this so that we can identify any sections which
579 the linker has decided to not include. */
580 sec
->linker_mark
= true;
582 if (info
->strip
== strip_none
583 || info
->strip
== strip_some
)
584 o
->lineno_count
+= sec
->lineno_count
;
586 if (info
->relocateable
)
587 o
->reloc_count
+= sec
->reloc_count
;
589 if (sec
->_raw_size
> max_contents_size
)
590 max_contents_size
= sec
->_raw_size
;
591 if (sec
->lineno_count
> max_lineno_count
)
592 max_lineno_count
= sec
->lineno_count
;
593 if (sec
->reloc_count
> max_reloc_count
)
594 max_reloc_count
= sec
->reloc_count
;
596 else if (info
->relocateable
597 && (p
->type
== bfd_section_reloc_link_order
598 || p
->type
== bfd_symbol_reloc_link_order
))
601 if (o
->reloc_count
== 0)
605 o
->flags
|= SEC_RELOC
;
606 o
->rel_filepos
= rel_filepos
;
607 rel_filepos
+= o
->reloc_count
* relsz
;
610 if (bfd_coff_long_section_names (abfd
)
611 && strlen (o
->name
) > SCNNMLEN
)
613 /* This section has a long name which must go in the string
614 table. This must correspond to the code in
615 coff_write_object_contents which puts the string index
616 into the s_name field of the section header. That is why
617 we pass hash as false. */
618 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
619 == (bfd_size_type
) -1)
621 long_section_names
= true;
625 /* If doing a relocateable link, allocate space for the pointers we
627 if (info
->relocateable
)
631 /* We use section_count + 1, rather than section_count, because
632 the target_index fields are 1 based. */
634 ((struct coff_link_section_info
*)
635 bfd_malloc ((abfd
->section_count
+ 1)
636 * sizeof (struct coff_link_section_info
)));
637 if (finfo
.section_info
== NULL
)
639 for (i
= 0; i
<= abfd
->section_count
; i
++)
641 finfo
.section_info
[i
].relocs
= NULL
;
642 finfo
.section_info
[i
].rel_hashes
= NULL
;
646 /* We now know the size of the relocs, so we can determine the file
647 positions of the line numbers. */
648 line_filepos
= rel_filepos
;
649 linesz
= bfd_coff_linesz (abfd
);
650 max_output_reloc_count
= 0;
651 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
653 if (o
->lineno_count
== 0)
657 o
->line_filepos
= line_filepos
;
658 line_filepos
+= o
->lineno_count
* linesz
;
661 if (o
->reloc_count
!= 0)
663 /* We don't know the indices of global symbols until we have
664 written out all the local symbols. For each section in
665 the output file, we keep an array of pointers to hash
666 table entries. Each entry in the array corresponds to a
667 reloc. When we find a reloc against a global symbol, we
668 set the corresponding entry in this array so that we can
669 fix up the symbol index after we have written out all the
672 Because of this problem, we also keep the relocs in
673 memory until the end of the link. This wastes memory,
674 but only when doing a relocateable link, which is not the
676 BFD_ASSERT (info
->relocateable
);
677 finfo
.section_info
[o
->target_index
].relocs
=
678 ((struct internal_reloc
*)
679 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
680 finfo
.section_info
[o
->target_index
].rel_hashes
=
681 ((struct coff_link_hash_entry
**)
682 bfd_malloc (o
->reloc_count
683 * sizeof (struct coff_link_hash_entry
*)));
684 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
685 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
688 if (o
->reloc_count
> max_output_reloc_count
)
689 max_output_reloc_count
= o
->reloc_count
;
692 /* Reset the reloc and lineno counts, so that we can use them to
693 count the number of entries we have output so far. */
698 obj_sym_filepos (abfd
) = line_filepos
;
700 /* Figure out the largest number of symbols in an input BFD. Take
701 the opportunity to clear the output_has_begun fields of all the
704 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
708 sub
->output_has_begun
= false;
709 sz
= obj_raw_syment_count (sub
);
710 if (sz
> max_sym_count
)
714 /* Allocate some buffers used while linking. */
715 finfo
.internal_syms
= ((struct internal_syment
*)
716 bfd_malloc (max_sym_count
717 * sizeof (struct internal_syment
)));
718 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
719 * sizeof (asection
*));
720 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
721 finfo
.outsyms
= ((bfd_byte
*)
722 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
723 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
724 * bfd_coff_linesz (abfd
));
725 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
726 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
727 if (! info
->relocateable
)
728 finfo
.internal_relocs
= ((struct internal_reloc
*)
729 bfd_malloc (max_reloc_count
730 * sizeof (struct internal_reloc
)));
731 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
732 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
733 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
734 || finfo
.outsyms
== NULL
735 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
736 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
737 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
738 || (! info
->relocateable
739 && finfo
.internal_relocs
== NULL
740 && max_reloc_count
> 0))
743 /* We now know the position of everything in the file, except that
744 we don't know the size of the symbol table and therefore we don't
745 know where the string table starts. We just build the string
746 table in memory as we go along. We process all the relocations
747 for a single input file at once. */
748 obj_raw_syment_count (abfd
) = 0;
750 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
752 if (! bfd_coff_start_final_link (abfd
, info
))
756 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
758 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
760 if (p
->type
== bfd_indirect_link_order
761 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
762 == bfd_target_coff_flavour
))
764 sub
= p
->u
.indirect
.section
->owner
;
765 if (! sub
->output_has_begun
)
767 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
769 sub
->output_has_begun
= true;
772 else if (p
->type
== bfd_section_reloc_link_order
773 || p
->type
== bfd_symbol_reloc_link_order
)
775 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
780 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
786 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
788 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
789 debug_merge_allocated
= false;
791 if (finfo
.internal_syms
!= NULL
)
793 free (finfo
.internal_syms
);
794 finfo
.internal_syms
= NULL
;
796 if (finfo
.sec_ptrs
!= NULL
)
798 free (finfo
.sec_ptrs
);
799 finfo
.sec_ptrs
= NULL
;
801 if (finfo
.sym_indices
!= NULL
)
803 free (finfo
.sym_indices
);
804 finfo
.sym_indices
= NULL
;
806 if (finfo
.linenos
!= NULL
)
808 free (finfo
.linenos
);
809 finfo
.linenos
= NULL
;
811 if (finfo
.contents
!= NULL
)
813 free (finfo
.contents
);
814 finfo
.contents
= NULL
;
816 if (finfo
.external_relocs
!= NULL
)
818 free (finfo
.external_relocs
);
819 finfo
.external_relocs
= NULL
;
821 if (finfo
.internal_relocs
!= NULL
)
823 free (finfo
.internal_relocs
);
824 finfo
.internal_relocs
= NULL
;
827 /* The value of the last C_FILE symbol is supposed to be the symbol
828 index of the first external symbol. Write it out again if
830 if (finfo
.last_file_index
!= -1
831 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
833 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
834 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
835 (PTR
) finfo
.outsyms
);
837 (obj_sym_filepos (abfd
)
838 + finfo
.last_file_index
* symesz
),
840 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
844 /* If doing task linking (ld --task-link) then make a pass through the
845 global symbols, writing out any that are defined, and making them
849 finfo
.failed
= false;
850 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
856 /* Write out the global symbols. */
857 finfo
.failed
= false;
858 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
863 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
864 if (finfo
.outsyms
!= NULL
)
866 free (finfo
.outsyms
);
867 finfo
.outsyms
= NULL
;
870 if (info
->relocateable
&& max_output_reloc_count
> 0)
872 /* Now that we have written out all the global symbols, we know
873 the symbol indices to use for relocs against them, and we can
874 finally write out the relocs. */
875 external_relocs
= ((bfd_byte
*)
876 bfd_malloc (max_output_reloc_count
* relsz
));
877 if (external_relocs
== NULL
)
880 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
882 struct internal_reloc
*irel
;
883 struct internal_reloc
*irelend
;
884 struct coff_link_hash_entry
**rel_hash
;
887 if (o
->reloc_count
== 0)
890 irel
= finfo
.section_info
[o
->target_index
].relocs
;
891 irelend
= irel
+ o
->reloc_count
;
892 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
893 erel
= external_relocs
;
894 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
896 if (*rel_hash
!= NULL
)
898 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
899 irel
->r_symndx
= (*rel_hash
)->indx
;
901 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
904 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
905 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
906 abfd
) != relsz
* o
->reloc_count
)
910 free (external_relocs
);
911 external_relocs
= NULL
;
914 /* Free up the section information. */
915 if (finfo
.section_info
!= NULL
)
919 for (i
= 0; i
< abfd
->section_count
; i
++)
921 if (finfo
.section_info
[i
].relocs
!= NULL
)
922 free (finfo
.section_info
[i
].relocs
);
923 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
924 free (finfo
.section_info
[i
].rel_hashes
);
926 free (finfo
.section_info
);
927 finfo
.section_info
= NULL
;
930 /* If we have optimized stabs strings, output them. */
931 if (coff_hash_table (info
)->stab_info
!= NULL
)
933 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
937 /* Write out the string table. */
938 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
941 (obj_sym_filepos (abfd
)
942 + obj_raw_syment_count (abfd
) * symesz
),
946 #if STRING_SIZE_SIZE == 4
948 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
949 (bfd_byte
*) strbuf
);
951 #error Change bfd_h_put_32
954 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
957 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
961 _bfd_stringtab_free (finfo
.strtab
);
963 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
964 not try to write out the symbols. */
965 bfd_get_symcount (abfd
) = 0;
970 if (debug_merge_allocated
)
971 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
972 if (finfo
.strtab
!= NULL
)
973 _bfd_stringtab_free (finfo
.strtab
);
974 if (finfo
.section_info
!= NULL
)
978 for (i
= 0; i
< abfd
->section_count
; i
++)
980 if (finfo
.section_info
[i
].relocs
!= NULL
)
981 free (finfo
.section_info
[i
].relocs
);
982 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
983 free (finfo
.section_info
[i
].rel_hashes
);
985 free (finfo
.section_info
);
987 if (finfo
.internal_syms
!= NULL
)
988 free (finfo
.internal_syms
);
989 if (finfo
.sec_ptrs
!= NULL
)
990 free (finfo
.sec_ptrs
);
991 if (finfo
.sym_indices
!= NULL
)
992 free (finfo
.sym_indices
);
993 if (finfo
.outsyms
!= NULL
)
994 free (finfo
.outsyms
);
995 if (finfo
.linenos
!= NULL
)
996 free (finfo
.linenos
);
997 if (finfo
.contents
!= NULL
)
998 free (finfo
.contents
);
999 if (finfo
.external_relocs
!= NULL
)
1000 free (finfo
.external_relocs
);
1001 if (finfo
.internal_relocs
!= NULL
)
1002 free (finfo
.internal_relocs
);
1003 if (external_relocs
!= NULL
)
1004 free (external_relocs
);
1008 /* parse out a -heap <reserved>,<commit> line */
1011 dores_com (ptr
, output_bfd
, heap
)
1016 if (coff_data(output_bfd
)->pe
)
1018 int val
= strtoul (ptr
, &ptr
, 0);
1020 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1022 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1026 int val
= strtoul (ptr
+1, &ptr
, 0);
1028 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1030 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1036 static char *get_name(ptr
, dst
)
1043 while (*ptr
&& *ptr
!= ' ')
1049 /* Process any magic embedded commands in a section called .drectve */
1052 process_embedded_commands (output_bfd
, info
, abfd
)
1054 struct bfd_link_info
*info
;
1057 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1064 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1067 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1072 e
= copy
+ sec
->_raw_size
;
1073 for (s
= copy
; s
< e
; )
1079 if (strncmp (s
,"-attr", 5) == 0)
1091 s
= get_name(s
, &name
);
1092 s
= get_name(s
, &attribs
);
1112 asec
= bfd_get_section_by_name (abfd
, name
);
1115 asec
->flags
|= SEC_CODE
;
1117 asec
->flags
|= SEC_READONLY
;
1120 else if (strncmp (s
,"-heap", 5) == 0)
1122 s
= dores_com (s
+5, output_bfd
, 1);
1124 else if (strncmp (s
,"-stack", 6) == 0)
1126 s
= dores_com (s
+6, output_bfd
, 0);
1135 /* Place a marker against all symbols which are used by relocations.
1136 This marker can be picked up by the 'do we skip this symbol ?'
1137 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1142 mark_relocs (finfo
, input_bfd
)
1143 struct coff_final_link_info
* finfo
;
1148 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1151 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1153 struct internal_reloc
* internal_relocs
;
1154 struct internal_reloc
* irel
;
1155 struct internal_reloc
* irelend
;
1158 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1161 /* Read in the relocs. */
1162 internal_relocs
= _bfd_coff_read_internal_relocs
1163 (input_bfd
, a
, false,
1164 finfo
->external_relocs
,
1165 finfo
->info
->relocateable
,
1166 (finfo
->info
->relocateable
1167 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1168 : finfo
->internal_relocs
)
1171 if (internal_relocs
== NULL
)
1174 irel
= internal_relocs
;
1175 irelend
= irel
+ a
->reloc_count
;
1177 /* Place a mark in the sym_indices array (whose entries have
1178 been initialised to 0) for all of the symbols that are used
1179 in the relocation table. This will then be picked up in the
1182 for (; irel
< irelend
; irel
++)
1184 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1189 /* Link an input file into the linker output file. This function
1190 handles all the sections and relocations of the input file at once. */
1193 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1194 struct coff_final_link_info
*finfo
;
1197 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1198 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1199 asection
*, struct internal_reloc
*,
1202 const char *strings
;
1203 bfd_size_type syment_base
;
1204 unsigned int n_tmask
;
1205 unsigned int n_btshft
;
1207 bfd_size_type isymesz
;
1208 bfd_size_type osymesz
;
1209 bfd_size_type linesz
;
1212 struct internal_syment
*isymp
;
1215 unsigned long output_index
;
1217 struct coff_link_hash_entry
**sym_hash
;
1220 /* Move all the symbols to the output file. */
1222 output_bfd
= finfo
->output_bfd
;
1223 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1225 syment_base
= obj_raw_syment_count (output_bfd
);
1226 isymesz
= bfd_coff_symesz (input_bfd
);
1227 osymesz
= bfd_coff_symesz (output_bfd
);
1228 linesz
= bfd_coff_linesz (input_bfd
);
1229 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1231 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1232 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1234 /* Define macros so that ISFCN, et. al., macros work correctly. */
1235 #define N_TMASK n_tmask
1236 #define N_BTSHFT n_btshft
1239 if (! finfo
->info
->keep_memory
)
1242 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1245 if (! _bfd_coff_get_external_symbols (input_bfd
))
1248 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1249 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1250 isymp
= finfo
->internal_syms
;
1251 secpp
= finfo
->sec_ptrs
;
1252 indexp
= finfo
->sym_indices
;
1253 output_index
= syment_base
;
1254 outsym
= finfo
->outsyms
;
1256 if (coff_data (output_bfd
)->pe
)
1258 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1262 /* If we are going to perform relocations and also strip/discard some symbols
1263 then we must make sure that we do not strip/discard those symbols that are
1264 going to be involved in the relocations */
1265 if (( finfo
->info
->strip
!= strip_none
1266 || finfo
->info
->discard
!= discard_none
)
1267 && finfo
->info
->relocateable
)
1269 /* mark the symbol array as 'not-used' */
1270 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1272 mark_relocs (finfo
, input_bfd
);
1275 while (esym
< esym_end
)
1277 struct internal_syment isym
;
1280 boolean dont_skip_symbol
;
1283 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1285 /* Make a copy of *isymp so that the relocate_section function
1286 always sees the original values. This is more reliable than
1287 always recomputing the symbol value even if we are stripping
1291 if (isym
.n_scnum
!= 0)
1292 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1295 if (isym
.n_value
== 0)
1296 *secpp
= bfd_und_section_ptr
;
1298 *secpp
= bfd_com_section_ptr
;
1301 /* Extract the flag indicating if this symbol is used by a relocation */
1302 if (( finfo
->info
->strip
!= strip_none
1303 || finfo
->info
->discard
!= discard_none
)
1304 && finfo
->info
->relocateable
)
1305 dont_skip_symbol
= *indexp
;
1307 dont_skip_symbol
= false;
1313 add
= 1 + isym
.n_numaux
;
1315 /* If we are stripping all symbols, we want to skip this one. */
1316 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1321 if (isym
.n_sclass
== C_EXT
1322 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1324 /* This is a global symbol. Global symbols come at the
1325 end of the symbol table, so skip them for now.
1326 Function symbols, however, are an exception, and are
1327 not moved to the end. */
1329 if (! ISFCN (isym
.n_type
))
1334 /* This is a local symbol. Skip it if we are discarding
1336 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1341 /* If we stripping debugging symbols, and this is a debugging
1342 symbol, then skip it. */
1344 && finfo
->info
->strip
== strip_debugger
1345 && ! dont_skip_symbol
1346 && isym
.n_scnum
== N_DEBUG
)
1349 /* If some symbols are stripped based on the name, work out the
1350 name and decide whether to skip this symbol. */
1352 && (finfo
->info
->strip
== strip_some
1353 || finfo
->info
->discard
== discard_l
))
1356 char buf
[SYMNMLEN
+ 1];
1358 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1362 if (! dont_skip_symbol
1363 && ((finfo
->info
->strip
== strip_some
1364 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1367 && finfo
->info
->discard
== discard_l
1368 && bfd_is_local_label_name (input_bfd
, name
))))
1372 /* If this is an enum, struct, or union tag, see if we have
1373 already output an identical type. */
1375 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1376 && (isym
.n_sclass
== C_ENTAG
1377 || isym
.n_sclass
== C_STRTAG
1378 || isym
.n_sclass
== C_UNTAG
)
1379 && isym
.n_numaux
== 1)
1382 char buf
[SYMNMLEN
+ 1];
1383 struct coff_debug_merge_hash_entry
*mh
;
1384 struct coff_debug_merge_type
*mt
;
1385 union internal_auxent aux
;
1386 struct coff_debug_merge_element
**epp
;
1387 bfd_byte
*esl
, *eslend
;
1388 struct internal_syment
*islp
;
1390 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1394 /* Ignore fake names invented by compiler; treat them all as
1396 if (*name
== '~' || *name
== '.' || *name
== '$'
1397 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1398 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1401 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1406 /* Allocate memory to hold type information. If this turns
1407 out to be a duplicate, we pass this address to
1409 mt
= ((struct coff_debug_merge_type
*)
1410 bfd_alloc (input_bfd
,
1411 sizeof (struct coff_debug_merge_type
)));
1414 mt
->class = isym
.n_sclass
;
1416 /* Pick up the aux entry, which points to the end of the tag
1418 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1419 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1422 /* Gather the elements. */
1423 epp
= &mt
->elements
;
1424 mt
->elements
= NULL
;
1426 esl
= esym
+ 2 * isymesz
;
1427 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1428 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1429 while (esl
< eslend
)
1431 const char *elename
;
1432 char elebuf
[SYMNMLEN
+ 1];
1435 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1437 *epp
= ((struct coff_debug_merge_element
*)
1438 bfd_alloc (input_bfd
,
1439 sizeof (struct coff_debug_merge_element
)));
1443 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1445 if (elename
== NULL
)
1448 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1451 strcpy (copy
, elename
);
1453 (*epp
)->name
= copy
;
1454 (*epp
)->type
= islp
->n_type
;
1456 if (islp
->n_numaux
>= 1
1457 && islp
->n_type
!= T_NULL
1458 && islp
->n_sclass
!= C_EOS
)
1460 union internal_auxent eleaux
;
1463 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1464 islp
->n_type
, islp
->n_sclass
, 0,
1465 islp
->n_numaux
, (PTR
) &eleaux
);
1466 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1468 /* FIXME: If this tagndx entry refers to a symbol
1469 defined later in this file, we just ignore it.
1470 Handling this correctly would be tedious, and may
1476 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1479 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1480 if ((*epp
)->tagndx
< 0)
1484 epp
= &(*epp
)->next
;
1487 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1488 islp
+= islp
->n_numaux
+ 1;
1491 /* See if we already have a definition which matches this
1492 type. We always output the type if it has no elements,
1494 if (mt
->elements
== NULL
)
1495 bfd_release (input_bfd
, (PTR
) mt
);
1498 struct coff_debug_merge_type
*mtl
;
1500 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1502 struct coff_debug_merge_element
*me
, *mel
;
1504 if (mtl
->class != mt
->class)
1507 for (me
= mt
->elements
, mel
= mtl
->elements
;
1508 me
!= NULL
&& mel
!= NULL
;
1509 me
= me
->next
, mel
= mel
->next
)
1511 if (strcmp (me
->name
, mel
->name
) != 0
1512 || me
->type
!= mel
->type
1513 || me
->tagndx
!= mel
->tagndx
)
1517 if (me
== NULL
&& mel
== NULL
)
1521 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1523 /* This is the first definition of this type. */
1524 mt
->indx
= output_index
;
1525 mt
->next
= mh
->types
;
1530 /* This is a redefinition which can be merged. */
1531 bfd_release (input_bfd
, (PTR
) mt
);
1532 *indexp
= mtl
->indx
;
1533 add
= (eslend
- esym
) / isymesz
;
1539 /* We now know whether we are to skip this symbol or not. */
1542 /* Adjust the symbol in order to output it. */
1544 if (isym
._n
._n_n
._n_zeroes
== 0
1545 && isym
._n
._n_n
._n_offset
!= 0)
1550 /* This symbol has a long name. Enter it in the string
1551 table we are building. Note that we do not check
1552 bfd_coff_symname_in_debug. That is only true for
1553 XCOFF, and XCOFF requires different linking code
1555 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1559 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1560 if (indx
== (bfd_size_type
) -1)
1562 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1565 if (isym
.n_scnum
> 0)
1567 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1568 isym
.n_value
+= (*secpp
)->output_offset
;
1569 if (! obj_pe (input_bfd
))
1570 isym
.n_value
-= (*secpp
)->vma
;
1571 if (! obj_pe (finfo
->output_bfd
))
1572 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1575 /* The value of a C_FILE symbol is the symbol index of the
1576 next C_FILE symbol. The value of the last C_FILE symbol
1577 is the symbol index to the first external symbol
1578 (actually, coff_renumber_symbols does not get this
1579 right--it just sets the value of the last C_FILE symbol
1580 to zero--and nobody has ever complained about it). We
1581 try to get this right, below, just before we write the
1582 symbols out, but in the general case we may have to write
1583 the symbol out twice. */
1584 if (isym
.n_sclass
== C_FILE
)
1586 if (finfo
->last_file_index
!= -1
1587 && finfo
->last_file
.n_value
!= (long) output_index
)
1589 /* We must correct the value of the last C_FILE entry. */
1590 finfo
->last_file
.n_value
= output_index
;
1591 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1593 /* The last C_FILE symbol is in this input file. */
1594 bfd_coff_swap_sym_out (output_bfd
,
1595 (PTR
) &finfo
->last_file
,
1596 (PTR
) (finfo
->outsyms
1597 + ((finfo
->last_file_index
1603 /* We have already written out the last C_FILE
1604 symbol. We need to write it out again. We
1605 borrow *outsym temporarily. */
1606 bfd_coff_swap_sym_out (output_bfd
,
1607 (PTR
) &finfo
->last_file
,
1609 if (bfd_seek (output_bfd
,
1610 (obj_sym_filepos (output_bfd
)
1611 + finfo
->last_file_index
* osymesz
),
1613 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1619 finfo
->last_file_index
= output_index
;
1620 finfo
->last_file
= isym
;
1623 /* If doing task linking, convert normal global function symbols to
1624 static functions. */
1626 if (finfo
->info
->task_link
&& isym
.n_sclass
== C_EXT
)
1627 isym
.n_sclass
= C_STAT
;
1629 /* Output the symbol. */
1631 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1633 *indexp
= output_index
;
1638 struct coff_link_hash_entry
*h
;
1640 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1642 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1645 /* This can happen if there were errors earlier in
1647 bfd_set_error (bfd_error_bad_value
);
1650 h
->indx
= output_index
;
1653 output_index
+= add
;
1654 outsym
+= add
* osymesz
;
1657 esym
+= add
* isymesz
;
1661 for (--add
; add
> 0; --add
)
1668 /* Fix up the aux entries. This must be done in a separate pass,
1669 because we don't know the correct symbol indices until we have
1670 already decided which symbols we are going to keep. */
1672 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1673 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1674 isymp
= finfo
->internal_syms
;
1675 indexp
= finfo
->sym_indices
;
1676 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1677 outsym
= finfo
->outsyms
;
1678 while (esym
< esym_end
)
1682 add
= 1 + isymp
->n_numaux
;
1685 || (bfd_size_type
) *indexp
< syment_base
)
1686 && (*sym_hash
== NULL
1687 || (*sym_hash
)->auxbfd
!= input_bfd
))
1688 esym
+= add
* isymesz
;
1691 struct coff_link_hash_entry
*h
;
1699 /* The m68k-motorola-sysv assembler will sometimes
1700 generate two symbols with the same name, but only one
1701 will have aux entries. */
1702 BFD_ASSERT (isymp
->n_numaux
== 0
1703 || h
->numaux
== isymp
->n_numaux
);
1711 /* Handle the aux entries. This handling is based on
1712 coff_pointerize_aux. I don't know if it always correct. */
1713 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1715 union internal_auxent aux
;
1716 union internal_auxent
*auxp
;
1722 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1723 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1728 if (isymp
->n_sclass
== C_FILE
)
1730 /* If this is a long filename, we must put it in the
1732 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1733 && auxp
->x_file
.x_n
.x_offset
!= 0)
1735 const char *filename
;
1738 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1739 >= STRING_SIZE_SIZE
);
1740 if (strings
== NULL
)
1742 strings
= _bfd_coff_read_string_table (input_bfd
);
1743 if (strings
== NULL
)
1746 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1747 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1749 if (indx
== (bfd_size_type
) -1)
1751 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1754 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1758 if (ISFCN (isymp
->n_type
)
1759 || ISTAG (isymp
->n_sclass
)
1760 || isymp
->n_sclass
== C_BLOCK
1761 || isymp
->n_sclass
== C_FCN
)
1763 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1765 && indx
< obj_raw_syment_count (input_bfd
))
1767 /* We look forward through the symbol for
1768 the index of the next symbol we are going
1769 to include. I don't know if this is
1771 while ((finfo
->sym_indices
[indx
] < 0
1772 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1774 && indx
< obj_raw_syment_count (input_bfd
))
1776 if (indx
>= obj_raw_syment_count (input_bfd
))
1777 indx
= output_index
;
1779 indx
= finfo
->sym_indices
[indx
];
1780 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1784 indx
= auxp
->x_sym
.x_tagndx
.l
;
1785 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1789 symindx
= finfo
->sym_indices
[indx
];
1791 auxp
->x_sym
.x_tagndx
.l
= 0;
1793 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1796 /* The .bf symbols are supposed to be linked through
1797 the endndx field. We need to carry this list
1798 across object files. */
1801 && isymp
->n_sclass
== C_FCN
1802 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1803 || isymp
->_n
._n_n
._n_offset
== 0)
1804 && isymp
->_n
._n_name
[0] == '.'
1805 && isymp
->_n
._n_name
[1] == 'b'
1806 && isymp
->_n
._n_name
[2] == 'f'
1807 && isymp
->_n
._n_name
[3] == '\0')
1809 if (finfo
->last_bf_index
!= -1)
1811 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1814 if ((bfd_size_type
) finfo
->last_bf_index
1819 /* The last .bf symbol is in this input
1820 file. This will only happen if the
1821 assembler did not set up the .bf
1822 endndx symbols correctly. */
1823 auxout
= (PTR
) (finfo
->outsyms
1824 + ((finfo
->last_bf_index
1827 bfd_coff_swap_aux_out (output_bfd
,
1828 (PTR
) &finfo
->last_bf
,
1836 /* We have already written out the last
1837 .bf aux entry. We need to write it
1838 out again. We borrow *outsym
1839 temporarily. FIXME: This case should
1841 bfd_coff_swap_aux_out (output_bfd
,
1842 (PTR
) &finfo
->last_bf
,
1847 if (bfd_seek (output_bfd
,
1848 (obj_sym_filepos (output_bfd
)
1849 + finfo
->last_bf_index
* osymesz
),
1851 || bfd_write (outsym
, osymesz
, 1,
1852 output_bfd
) != osymesz
)
1857 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1858 finfo
->last_bf_index
= -1;
1861 /* The endndx field of this aux entry must
1862 be updated with the symbol number of the
1864 finfo
->last_bf
= *auxp
;
1865 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1874 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1875 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1889 /* Relocate the line numbers, unless we are stripping them. */
1890 if (finfo
->info
->strip
== strip_none
1891 || finfo
->info
->strip
== strip_some
)
1893 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1899 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1900 build_link_order in ldwrite.c will not have created a
1901 link order, which means that we will not have seen this
1902 input section in _bfd_coff_final_link, which means that
1903 we will not have allocated space for the line numbers of
1904 this section. I don't think line numbers can be
1905 meaningful for a section which does not have
1906 SEC_HAS_CONTENTS set, but, if they do, this must be
1908 if (o
->lineno_count
== 0
1909 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1912 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1913 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1914 input_bfd
) != linesz
* o
->lineno_count
)
1917 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1918 eline
= finfo
->linenos
;
1919 elineend
= eline
+ linesz
* o
->lineno_count
;
1920 for (; eline
< elineend
; eline
+= linesz
)
1922 struct internal_lineno iline
;
1924 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1926 if (iline
.l_lnno
!= 0)
1927 iline
.l_addr
.l_paddr
+= offset
;
1928 else if (iline
.l_addr
.l_symndx
>= 0
1929 && ((unsigned long) iline
.l_addr
.l_symndx
1930 < obj_raw_syment_count (input_bfd
)))
1934 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1938 /* These line numbers are attached to a symbol
1939 which we are stripping. We should really
1940 just discard the line numbers, but that would
1941 be a pain because we have already counted
1947 struct internal_syment is
;
1948 union internal_auxent ia
;
1950 /* Fix up the lnnoptr field in the aux entry of
1951 the symbol. It turns out that we can't do
1952 this when we modify the symbol aux entries,
1953 because gas sometimes screws up the lnnoptr
1954 field and makes it an offset from the start
1955 of the line numbers rather than an absolute
1957 bfd_coff_swap_sym_in (output_bfd
,
1958 (PTR
) (finfo
->outsyms
1959 + ((indx
- syment_base
)
1962 if ((ISFCN (is
.n_type
)
1963 || is
.n_sclass
== C_BLOCK
)
1964 && is
.n_numaux
>= 1)
1968 auxptr
= (PTR
) (finfo
->outsyms
1969 + ((indx
- syment_base
+ 1)
1971 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1972 is
.n_type
, is
.n_sclass
,
1973 0, is
.n_numaux
, (PTR
) &ia
);
1974 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1975 (o
->output_section
->line_filepos
1976 + o
->output_section
->lineno_count
* linesz
1977 + eline
- finfo
->linenos
);
1978 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1979 is
.n_type
, is
.n_sclass
, 0,
1980 is
.n_numaux
, auxptr
);
1984 iline
.l_addr
.l_symndx
= indx
;
1987 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1990 if (bfd_seek (output_bfd
,
1991 (o
->output_section
->line_filepos
1992 + o
->output_section
->lineno_count
* linesz
),
1994 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1995 output_bfd
) != linesz
* o
->lineno_count
)
1998 o
->output_section
->lineno_count
+= o
->lineno_count
;
2002 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2003 symbol will be the first symbol in the next input file. In the
2004 normal case, this will save us from writing out the C_FILE symbol
2006 if (finfo
->last_file_index
!= -1
2007 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2009 finfo
->last_file
.n_value
= output_index
;
2010 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2011 (PTR
) (finfo
->outsyms
2012 + ((finfo
->last_file_index
- syment_base
)
2016 /* Write the modified symbols to the output file. */
2017 if (outsym
> finfo
->outsyms
)
2019 if (bfd_seek (output_bfd
,
2020 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2022 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2024 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2027 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2028 + (outsym
- finfo
->outsyms
) / osymesz
)
2031 obj_raw_syment_count (output_bfd
) = output_index
;
2034 /* Relocate the contents of each section. */
2035 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2036 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2039 struct coff_section_tdata
*secdata
;
2041 if (! o
->linker_mark
)
2043 /* This section was omitted from the link. */
2047 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2048 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2050 if ((o
->flags
& SEC_RELOC
) != 0
2051 && o
->reloc_count
!= 0)
2053 ((*_bfd_error_handler
)
2054 ("%s: relocs in section `%s', but it has no contents",
2055 bfd_get_filename (input_bfd
),
2056 bfd_get_section_name (input_bfd
, o
)));
2057 bfd_set_error (bfd_error_no_contents
);
2064 secdata
= coff_section_data (input_bfd
, o
);
2065 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2066 contents
= secdata
->contents
;
2069 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2070 (file_ptr
) 0, o
->_raw_size
))
2072 contents
= finfo
->contents
;
2075 if ((o
->flags
& SEC_RELOC
) != 0)
2078 struct internal_reloc
*internal_relocs
;
2079 struct internal_reloc
*irel
;
2081 /* Read in the relocs. */
2082 target_index
= o
->output_section
->target_index
;
2083 internal_relocs
= (_bfd_coff_read_internal_relocs
2084 (input_bfd
, o
, false, finfo
->external_relocs
,
2085 finfo
->info
->relocateable
,
2086 (finfo
->info
->relocateable
2087 ? (finfo
->section_info
[target_index
].relocs
2088 + o
->output_section
->reloc_count
)
2089 : finfo
->internal_relocs
)));
2090 if (internal_relocs
== NULL
)
2093 /* Call processor specific code to relocate the section
2095 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2099 finfo
->internal_syms
,
2103 if (finfo
->info
->relocateable
)
2106 struct internal_reloc
*irelend
;
2107 struct coff_link_hash_entry
**rel_hash
;
2109 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2110 irel
= internal_relocs
;
2111 irelend
= irel
+ o
->reloc_count
;
2112 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2113 + o
->output_section
->reloc_count
);
2114 for (; irel
< irelend
; irel
++, rel_hash
++)
2116 struct coff_link_hash_entry
*h
;
2121 /* Adjust the reloc address and symbol index. */
2123 irel
->r_vaddr
+= offset
;
2125 if (irel
->r_symndx
== -1)
2130 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2138 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2141 /* This is a global symbol. */
2143 irel
->r_symndx
= h
->indx
;
2146 /* This symbol is being written at the end
2147 of the file, and we do not yet know the
2148 symbol index. We save the pointer to the
2149 hash table entry in the rel_hash list.
2150 We set the indx field to -2 to indicate
2151 that this symbol must not be stripped. */
2160 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2162 irel
->r_symndx
= indx
;
2165 struct internal_syment
*is
;
2167 char buf
[SYMNMLEN
+ 1];
2169 /* This reloc is against a symbol we are
2170 stripping. This should have been handled
2171 by the 'dont_skip_symbol' code in the while
2172 loop at the top of this function. */
2174 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2176 name
= (_bfd_coff_internal_syment_name
2177 (input_bfd
, is
, buf
));
2181 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2182 (finfo
->info
, name
, input_bfd
, o
,
2189 o
->output_section
->reloc_count
+= o
->reloc_count
;
2193 /* Write out the modified section contents. */
2194 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2196 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2197 contents
, o
->output_offset
,
2198 (o
->_cooked_size
!= 0
2205 if (! (_bfd_write_section_stabs
2206 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2207 o
, &secdata
->stab_info
, contents
)))
2212 if (! finfo
->info
->keep_memory
)
2214 if (! _bfd_coff_free_symbols (input_bfd
))
2221 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2224 _bfd_coff_write_global_sym (h
, data
)
2225 struct coff_link_hash_entry
*h
;
2228 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2230 struct internal_syment isym
;
2231 bfd_size_type symesz
;
2234 output_bfd
= finfo
->output_bfd
;
2240 && (finfo
->info
->strip
== strip_all
2241 || (finfo
->info
->strip
== strip_some
2242 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2243 h
->root
.root
.string
, false, false)
2247 switch (h
->root
.type
)
2250 case bfd_link_hash_new
:
2254 case bfd_link_hash_undefined
:
2255 case bfd_link_hash_undefweak
:
2256 isym
.n_scnum
= N_UNDEF
;
2260 case bfd_link_hash_defined
:
2261 case bfd_link_hash_defweak
:
2265 sec
= h
->root
.u
.def
.section
->output_section
;
2266 if (bfd_is_abs_section (sec
))
2267 isym
.n_scnum
= N_ABS
;
2269 isym
.n_scnum
= sec
->target_index
;
2270 isym
.n_value
= (h
->root
.u
.def
.value
2271 + h
->root
.u
.def
.section
->output_offset
);
2272 if (! obj_pe (finfo
->output_bfd
))
2273 isym
.n_value
+= sec
->vma
;
2277 case bfd_link_hash_common
:
2278 isym
.n_scnum
= N_UNDEF
;
2279 isym
.n_value
= h
->root
.u
.c
.size
;
2282 case bfd_link_hash_indirect
:
2283 case bfd_link_hash_warning
:
2284 /* Just ignore these. They can't be handled anyhow. */
2288 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2289 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2296 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2298 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2300 if (indx
== (bfd_size_type
) -1)
2302 finfo
->failed
= true;
2305 isym
._n
._n_n
._n_zeroes
= 0;
2306 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2309 isym
.n_sclass
= h
->class;
2310 isym
.n_type
= h
->type
;
2312 if (isym
.n_sclass
== C_NULL
)
2313 isym
.n_sclass
= C_EXT
;
2315 /* If doing task linking and this is the pass where we convert defined globals to
2316 statics, then do that conversion now. If the symbol is not being converted,
2317 just ignore it and it will be output during a later pass. */
2318 if (finfo
->global_to_static
)
2320 if (isym
.n_sclass
!= C_EXT
)
2324 isym
.n_sclass
= C_STAT
;
2327 isym
.n_numaux
= h
->numaux
;
2329 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2331 symesz
= bfd_coff_symesz (output_bfd
);
2333 if (bfd_seek (output_bfd
,
2334 (obj_sym_filepos (output_bfd
)
2335 + obj_raw_syment_count (output_bfd
) * symesz
),
2337 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2339 finfo
->failed
= true;
2343 h
->indx
= obj_raw_syment_count (output_bfd
);
2345 ++obj_raw_syment_count (output_bfd
);
2347 /* Write out any associated aux entries. There normally will be
2348 none. If there are any, I have no idea how to modify them. */
2349 for (i
= 0; i
< isym
.n_numaux
; i
++)
2351 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2352 isym
.n_sclass
, i
, isym
.n_numaux
,
2353 (PTR
) finfo
->outsyms
);
2354 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2356 finfo
->failed
= true;
2359 ++obj_raw_syment_count (output_bfd
);
2365 /* Write out task global symbols, converting them to statics. Called
2366 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2367 the dirty work, if the symbol we are processing needs conversion. */
2370 _bfd_coff_write_task_globals (h
, data
)
2371 struct coff_link_hash_entry
*h
;
2374 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2375 boolean rtnval
= true;
2379 switch (h
->root
.type
)
2381 case bfd_link_hash_defined
:
2382 case bfd_link_hash_defweak
:
2383 finfo
->global_to_static
= true;
2384 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2385 finfo
->global_to_static
= false;
2392 /* Handle a link order which is supposed to generate a reloc. */
2395 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2397 struct coff_final_link_info
*finfo
;
2398 asection
*output_section
;
2399 struct bfd_link_order
*link_order
;
2401 reloc_howto_type
*howto
;
2402 struct internal_reloc
*irel
;
2403 struct coff_link_hash_entry
**rel_hash_ptr
;
2405 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2408 bfd_set_error (bfd_error_bad_value
);
2412 if (link_order
->u
.reloc
.p
->addend
!= 0)
2416 bfd_reloc_status_type rstat
;
2419 size
= bfd_get_reloc_size (howto
);
2420 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2424 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2425 link_order
->u
.reloc
.p
->addend
, buf
);
2431 case bfd_reloc_outofrange
:
2433 case bfd_reloc_overflow
:
2434 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2436 (link_order
->type
== bfd_section_reloc_link_order
2437 ? bfd_section_name (output_bfd
,
2438 link_order
->u
.reloc
.p
->u
.section
)
2439 : link_order
->u
.reloc
.p
->u
.name
),
2440 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2441 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2448 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2449 (file_ptr
) link_order
->offset
, size
);
2455 /* Store the reloc information in the right place. It will get
2456 swapped and written out at the end of the final_link routine. */
2458 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2459 + output_section
->reloc_count
);
2460 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2461 + output_section
->reloc_count
);
2463 memset (irel
, 0, sizeof (struct internal_reloc
));
2464 *rel_hash_ptr
= NULL
;
2466 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2468 if (link_order
->type
== bfd_section_reloc_link_order
)
2470 /* We need to somehow locate a symbol in the right section. The
2471 symbol must either have a value of zero, or we must adjust
2472 the addend by the value of the symbol. FIXME: Write this
2473 when we need it. The old linker couldn't handle this anyhow. */
2475 *rel_hash_ptr
= NULL
;
2480 struct coff_link_hash_entry
*h
;
2482 h
= ((struct coff_link_hash_entry
*)
2483 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2484 link_order
->u
.reloc
.p
->u
.name
,
2485 false, false, true));
2489 irel
->r_symndx
= h
->indx
;
2492 /* Set the index to -2 to force this symbol to get
2501 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2502 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2503 (asection
*) NULL
, (bfd_vma
) 0)))
2509 /* FIXME: Is this always right? */
2510 irel
->r_type
= howto
->type
;
2512 /* r_size is only used on the RS/6000, which needs its own linker
2513 routines anyhow. r_extern is only used for ECOFF. */
2515 /* FIXME: What is the right value for r_offset? Is zero OK? */
2517 ++output_section
->reloc_count
;
2522 /* A basic reloc handling routine which may be used by processors with
2526 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2527 input_section
, contents
, relocs
, syms
,
2530 struct bfd_link_info
*info
;
2532 asection
*input_section
;
2534 struct internal_reloc
*relocs
;
2535 struct internal_syment
*syms
;
2536 asection
**sections
;
2538 struct internal_reloc
*rel
;
2539 struct internal_reloc
*relend
;
2542 relend
= rel
+ input_section
->reloc_count
;
2543 for (; rel
< relend
; rel
++)
2546 struct coff_link_hash_entry
*h
;
2547 struct internal_syment
*sym
;
2550 reloc_howto_type
*howto
;
2551 bfd_reloc_status_type rstat
;
2553 symndx
= rel
->r_symndx
;
2562 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2563 sym
= syms
+ symndx
;
2566 /* COFF treats common symbols in one of two ways. Either the
2567 size of the symbol is included in the section contents, or it
2568 is not. We assume that the size is not included, and force
2569 the rtype_to_howto function to adjust the addend as needed. */
2571 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2572 addend
= - sym
->n_value
;
2577 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2582 /* If we are doing a relocateable link, then we can just ignore
2583 a PC relative reloc that is pcrel_offset. It will already
2584 have the correct value. If this is not a relocateable link,
2585 then we should ignore the symbol value. */
2586 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2588 if (info
->relocateable
)
2590 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2591 addend
+= sym
->n_value
;
2602 sec
= bfd_abs_section_ptr
;
2607 sec
= sections
[symndx
];
2608 val
= (sec
->output_section
->vma
2609 + sec
->output_offset
2611 if (! obj_pe (input_bfd
))
2617 if (h
->root
.type
== bfd_link_hash_defined
2618 || h
->root
.type
== bfd_link_hash_defweak
)
2622 sec
= h
->root
.u
.def
.section
;
2623 val
= (h
->root
.u
.def
.value
2624 + sec
->output_section
->vma
2625 + sec
->output_offset
);
2628 else if (! info
->relocateable
)
2630 if (! ((*info
->callbacks
->undefined_symbol
)
2631 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2632 rel
->r_vaddr
- input_section
->vma
)))
2637 if (info
->base_file
)
2639 /* Emit a reloc if the backend thinks it needs it. */
2640 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2642 /* Relocation to a symbol in a section which isn't
2643 absolute. We output the address here to a file.
2644 This file is then read by dlltool when generating the
2645 reloc section. Note that the base file is not
2646 portable between systems. We write out a long here,
2647 and dlltool reads in a long. */
2648 long addr
= (rel
->r_vaddr
2649 - input_section
->vma
2650 + input_section
->output_offset
2651 + input_section
->output_section
->vma
);
2652 if (coff_data (output_bfd
)->pe
)
2653 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2654 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2657 bfd_set_error (bfd_error_system_call
);
2663 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2665 rel
->r_vaddr
- input_section
->vma
,
2674 case bfd_reloc_outofrange
:
2675 (*_bfd_error_handler
)
2676 ("%s: bad reloc address 0x%lx in section `%s'",
2677 bfd_get_filename (input_bfd
),
2678 (unsigned long) rel
->r_vaddr
,
2679 bfd_get_section_name (input_bfd
, input_section
));
2681 case bfd_reloc_overflow
:
2684 char buf
[SYMNMLEN
+ 1];
2689 name
= h
->root
.root
.string
;
2692 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2697 if (! ((*info
->callbacks
->reloc_overflow
)
2698 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2699 input_section
, rel
->r_vaddr
- input_section
->vma
)))