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
248 || sym
.n_sclass
== C_SYSTEM
250 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
251 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
254 char buf
[SYMNMLEN
+ 1];
255 struct bfd_link_hash_entry
*h
;
257 /* This symbol is externally visible, and is defined by this
260 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
263 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
265 /* We are only interested in symbols that are currently
266 undefined. If a symbol is currently known to be common,
267 COFF linkers do not bring in an object file which defines
269 if (h
!= (struct bfd_link_hash_entry
*) NULL
270 && h
->type
== bfd_link_hash_undefined
)
272 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
279 esym
+= (sym
.n_numaux
+ 1) * symesz
;
282 /* We do not need this object file. */
286 /* Add all the symbols from an object file to the hash table. */
289 coff_link_add_symbols (abfd
, info
)
291 struct bfd_link_info
*info
;
293 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
295 boolean default_copy
;
296 bfd_size_type symcount
;
297 struct coff_link_hash_entry
**sym_hash
;
298 bfd_size_type symesz
;
302 /* Keep the symbols during this function, in case the linker needs
303 to read the generic symbols in order to report an error message. */
304 keep_syms
= obj_coff_keep_syms (abfd
);
305 obj_coff_keep_syms (abfd
) = true;
307 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
309 if (info
->keep_memory
)
310 default_copy
= false;
314 symcount
= obj_raw_syment_count (abfd
);
316 /* We keep a list of the linker hash table entries that correspond
317 to particular symbols. */
318 sym_hash
= ((struct coff_link_hash_entry
**)
321 * sizeof (struct coff_link_hash_entry
*))));
322 if (sym_hash
== NULL
&& symcount
!= 0)
324 obj_coff_sym_hashes (abfd
) = sym_hash
;
326 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
328 symesz
= bfd_coff_symesz (abfd
);
329 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
330 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
331 esym_end
= esym
+ symcount
* symesz
;
332 while (esym
< esym_end
)
334 struct internal_syment sym
;
337 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
339 if (sym
.n_sclass
== C_EXT
341 || sym
.n_sclass
== C_SYSTEM
343 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
346 char buf
[SYMNMLEN
+ 1];
351 /* This symbol is externally visible. */
353 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
357 /* We must copy the name into memory if we got it from the
358 syment itself, rather than the string table. */
360 if (sym
._n
._n_n
._n_zeroes
!= 0
361 || sym
._n
._n_n
._n_offset
== 0)
366 if (sym
.n_scnum
== 0)
371 section
= bfd_und_section_ptr
;
376 section
= bfd_com_section_ptr
;
381 flags
= BSF_EXPORT
| BSF_GLOBAL
;
382 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
384 value
-= section
->vma
;
387 if (! (bfd_coff_link_add_one_symbol
388 (info
, abfd
, name
, flags
, section
, value
,
389 (const char *) NULL
, copy
, false,
390 (struct bfd_link_hash_entry
**) sym_hash
)))
393 if (section
== bfd_com_section_ptr
394 && (*sym_hash
)->root
.type
== bfd_link_hash_common
395 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
396 > bfd_coff_default_section_alignment_power (abfd
)))
397 (*sym_hash
)->root
.u
.c
.p
->alignment_power
398 = bfd_coff_default_section_alignment_power (abfd
);
400 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
402 if (((*sym_hash
)->class == C_NULL
403 && (*sym_hash
)->type
== T_NULL
)
406 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
408 (*sym_hash
)->class = sym
.n_sclass
;
409 if (sym
.n_type
!= T_NULL
)
411 if ((*sym_hash
)->type
!= T_NULL
412 && (*sym_hash
)->type
!= sym
.n_type
)
413 (*_bfd_error_handler
)
414 ("Warning: type of symbol `%s' changed from %d to %d in %s",
415 name
, (*sym_hash
)->type
, sym
.n_type
,
416 bfd_get_filename (abfd
));
417 (*sym_hash
)->type
= sym
.n_type
;
419 (*sym_hash
)->auxbfd
= abfd
;
420 if (sym
.n_numaux
!= 0)
422 union internal_auxent
*alloc
;
425 union internal_auxent
*iaux
;
427 (*sym_hash
)->numaux
= sym
.n_numaux
;
428 alloc
= ((union internal_auxent
*)
429 bfd_hash_allocate (&info
->hash
->table
,
431 * sizeof (*alloc
))));
434 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
436 i
++, eaux
+= symesz
, iaux
++)
437 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
438 sym
.n_sclass
, i
, sym
.n_numaux
,
440 (*sym_hash
)->aux
= alloc
;
446 esym
+= (sym
.n_numaux
+ 1) * symesz
;
447 sym_hash
+= sym
.n_numaux
+ 1;
450 /* If this is a non-traditional, non-relocateable link, try to
451 optimize the handling of any .stab/.stabstr sections. */
452 if (! info
->relocateable
453 && ! info
->traditional_format
454 && info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
)
455 && (info
->strip
!= strip_all
&& info
->strip
!= strip_debugger
))
457 asection
*stab
, *stabstr
;
459 stab
= bfd_get_section_by_name (abfd
, ".stab");
462 stabstr
= bfd_get_section_by_name (abfd
, ".stabstr");
466 struct coff_link_hash_table
*table
;
467 struct coff_section_tdata
*secdata
;
469 secdata
= coff_section_data (abfd
, stab
);
473 (PTR
) bfd_zalloc (abfd
,
474 sizeof (struct coff_section_tdata
));
475 if (stab
->used_by_bfd
== NULL
)
477 secdata
= coff_section_data (abfd
, stab
);
480 table
= coff_hash_table (info
);
482 if (! _bfd_link_section_stabs (abfd
, &table
->stab_info
,
484 &secdata
->stab_info
))
490 obj_coff_keep_syms (abfd
) = keep_syms
;
495 obj_coff_keep_syms (abfd
) = keep_syms
;
499 /* Do the final link step. */
502 _bfd_coff_final_link (abfd
, info
)
504 struct bfd_link_info
*info
;
506 bfd_size_type symesz
;
507 struct coff_final_link_info finfo
;
508 boolean debug_merge_allocated
;
509 boolean long_section_names
;
511 struct bfd_link_order
*p
;
512 size_t max_sym_count
;
513 size_t max_lineno_count
;
514 size_t max_reloc_count
;
515 size_t max_output_reloc_count
;
516 size_t max_contents_size
;
517 file_ptr rel_filepos
;
519 file_ptr line_filepos
;
522 bfd_byte
*external_relocs
= NULL
;
523 char strbuf
[STRING_SIZE_SIZE
];
525 symesz
= bfd_coff_symesz (abfd
);
528 finfo
.output_bfd
= abfd
;
530 finfo
.section_info
= NULL
;
531 finfo
.last_file_index
= -1;
532 finfo
.last_bf_index
= -1;
533 finfo
.internal_syms
= NULL
;
534 finfo
.sec_ptrs
= NULL
;
535 finfo
.sym_indices
= NULL
;
536 finfo
.outsyms
= NULL
;
537 finfo
.linenos
= NULL
;
538 finfo
.contents
= NULL
;
539 finfo
.external_relocs
= NULL
;
540 finfo
.internal_relocs
= NULL
;
541 finfo
.global_to_static
= false;
542 debug_merge_allocated
= false;
544 coff_data (abfd
)->link_info
= info
;
546 finfo
.strtab
= _bfd_stringtab_init ();
547 if (finfo
.strtab
== NULL
)
550 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
552 debug_merge_allocated
= true;
554 /* Compute the file positions for all the sections. */
555 if (! abfd
->output_has_begun
)
557 if (! bfd_coff_compute_section_file_positions (abfd
))
561 /* Count the line numbers and relocation entries required for the
562 output file. Set the file positions for the relocs. */
563 rel_filepos
= obj_relocbase (abfd
);
564 relsz
= bfd_coff_relsz (abfd
);
565 max_contents_size
= 0;
566 max_lineno_count
= 0;
569 long_section_names
= false;
570 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
574 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
576 if (p
->type
== bfd_indirect_link_order
)
580 sec
= p
->u
.indirect
.section
;
582 /* Mark all sections which are to be included in the
583 link. This will normally be every section. We need
584 to do this so that we can identify any sections which
585 the linker has decided to not include. */
586 sec
->linker_mark
= true;
588 if (info
->strip
== strip_none
589 || info
->strip
== strip_some
)
590 o
->lineno_count
+= sec
->lineno_count
;
592 if (info
->relocateable
)
593 o
->reloc_count
+= sec
->reloc_count
;
595 if (sec
->_raw_size
> max_contents_size
)
596 max_contents_size
= sec
->_raw_size
;
597 if (sec
->lineno_count
> max_lineno_count
)
598 max_lineno_count
= sec
->lineno_count
;
599 if (sec
->reloc_count
> max_reloc_count
)
600 max_reloc_count
= sec
->reloc_count
;
602 else if (info
->relocateable
603 && (p
->type
== bfd_section_reloc_link_order
604 || p
->type
== bfd_symbol_reloc_link_order
))
607 if (o
->reloc_count
== 0)
611 o
->flags
|= SEC_RELOC
;
612 o
->rel_filepos
= rel_filepos
;
613 rel_filepos
+= o
->reloc_count
* relsz
;
616 if (bfd_coff_long_section_names (abfd
)
617 && strlen (o
->name
) > SCNNMLEN
)
619 /* This section has a long name which must go in the string
620 table. This must correspond to the code in
621 coff_write_object_contents which puts the string index
622 into the s_name field of the section header. That is why
623 we pass hash as false. */
624 if (_bfd_stringtab_add (finfo
.strtab
, o
->name
, false, false)
625 == (bfd_size_type
) -1)
627 long_section_names
= true;
631 /* If doing a relocateable link, allocate space for the pointers we
633 if (info
->relocateable
)
637 /* We use section_count + 1, rather than section_count, because
638 the target_index fields are 1 based. */
640 ((struct coff_link_section_info
*)
641 bfd_malloc ((abfd
->section_count
+ 1)
642 * sizeof (struct coff_link_section_info
)));
643 if (finfo
.section_info
== NULL
)
645 for (i
= 0; i
<= abfd
->section_count
; i
++)
647 finfo
.section_info
[i
].relocs
= NULL
;
648 finfo
.section_info
[i
].rel_hashes
= NULL
;
652 /* We now know the size of the relocs, so we can determine the file
653 positions of the line numbers. */
654 line_filepos
= rel_filepos
;
655 linesz
= bfd_coff_linesz (abfd
);
656 max_output_reloc_count
= 0;
657 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
659 if (o
->lineno_count
== 0)
663 o
->line_filepos
= line_filepos
;
664 line_filepos
+= o
->lineno_count
* linesz
;
667 if (o
->reloc_count
!= 0)
669 /* We don't know the indices of global symbols until we have
670 written out all the local symbols. For each section in
671 the output file, we keep an array of pointers to hash
672 table entries. Each entry in the array corresponds to a
673 reloc. When we find a reloc against a global symbol, we
674 set the corresponding entry in this array so that we can
675 fix up the symbol index after we have written out all the
678 Because of this problem, we also keep the relocs in
679 memory until the end of the link. This wastes memory,
680 but only when doing a relocateable link, which is not the
682 BFD_ASSERT (info
->relocateable
);
683 finfo
.section_info
[o
->target_index
].relocs
=
684 ((struct internal_reloc
*)
685 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
686 finfo
.section_info
[o
->target_index
].rel_hashes
=
687 ((struct coff_link_hash_entry
**)
688 bfd_malloc (o
->reloc_count
689 * sizeof (struct coff_link_hash_entry
*)));
690 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
691 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
694 if (o
->reloc_count
> max_output_reloc_count
)
695 max_output_reloc_count
= o
->reloc_count
;
698 /* Reset the reloc and lineno counts, so that we can use them to
699 count the number of entries we have output so far. */
704 obj_sym_filepos (abfd
) = line_filepos
;
706 /* Figure out the largest number of symbols in an input BFD. Take
707 the opportunity to clear the output_has_begun fields of all the
710 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
714 sub
->output_has_begun
= false;
715 sz
= obj_raw_syment_count (sub
);
716 if (sz
> max_sym_count
)
720 /* Allocate some buffers used while linking. */
721 finfo
.internal_syms
= ((struct internal_syment
*)
722 bfd_malloc (max_sym_count
723 * sizeof (struct internal_syment
)));
724 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
725 * sizeof (asection
*));
726 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
727 finfo
.outsyms
= ((bfd_byte
*)
728 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
729 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
730 * bfd_coff_linesz (abfd
));
731 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
732 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
733 if (! info
->relocateable
)
734 finfo
.internal_relocs
= ((struct internal_reloc
*)
735 bfd_malloc (max_reloc_count
736 * sizeof (struct internal_reloc
)));
737 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
738 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
739 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
740 || finfo
.outsyms
== NULL
741 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
742 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
743 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
744 || (! info
->relocateable
745 && finfo
.internal_relocs
== NULL
746 && max_reloc_count
> 0))
749 /* We now know the position of everything in the file, except that
750 we don't know the size of the symbol table and therefore we don't
751 know where the string table starts. We just build the string
752 table in memory as we go along. We process all the relocations
753 for a single input file at once. */
754 obj_raw_syment_count (abfd
) = 0;
756 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
758 if (! bfd_coff_start_final_link (abfd
, info
))
762 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
764 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
766 if (p
->type
== bfd_indirect_link_order
767 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
768 == bfd_target_coff_flavour
))
770 sub
= p
->u
.indirect
.section
->owner
;
771 if (! bfd_coff_link_output_has_begun (sub
, & finfo
))
773 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
775 sub
->output_has_begun
= true;
778 else if (p
->type
== bfd_section_reloc_link_order
779 || p
->type
== bfd_symbol_reloc_link_order
)
781 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
786 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
792 if (! bfd_coff_final_link_postscript (abfd
, & finfo
))
795 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
797 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
798 debug_merge_allocated
= false;
800 if (finfo
.internal_syms
!= NULL
)
802 free (finfo
.internal_syms
);
803 finfo
.internal_syms
= NULL
;
805 if (finfo
.sec_ptrs
!= NULL
)
807 free (finfo
.sec_ptrs
);
808 finfo
.sec_ptrs
= NULL
;
810 if (finfo
.sym_indices
!= NULL
)
812 free (finfo
.sym_indices
);
813 finfo
.sym_indices
= NULL
;
815 if (finfo
.linenos
!= NULL
)
817 free (finfo
.linenos
);
818 finfo
.linenos
= NULL
;
820 if (finfo
.contents
!= NULL
)
822 free (finfo
.contents
);
823 finfo
.contents
= NULL
;
825 if (finfo
.external_relocs
!= NULL
)
827 free (finfo
.external_relocs
);
828 finfo
.external_relocs
= NULL
;
830 if (finfo
.internal_relocs
!= NULL
)
832 free (finfo
.internal_relocs
);
833 finfo
.internal_relocs
= NULL
;
836 /* The value of the last C_FILE symbol is supposed to be the symbol
837 index of the first external symbol. Write it out again if
839 if (finfo
.last_file_index
!= -1
840 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
842 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
843 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
844 (PTR
) finfo
.outsyms
);
846 (obj_sym_filepos (abfd
)
847 + finfo
.last_file_index
* symesz
),
849 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
853 /* If doing task linking (ld --task-link) then make a pass through the
854 global symbols, writing out any that are defined, and making them
858 finfo
.failed
= false;
859 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_task_globals
,
865 /* Write out the global symbols. */
866 finfo
.failed
= false;
867 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
872 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
873 if (finfo
.outsyms
!= NULL
)
875 free (finfo
.outsyms
);
876 finfo
.outsyms
= NULL
;
879 if (info
->relocateable
&& max_output_reloc_count
> 0)
881 /* Now that we have written out all the global symbols, we know
882 the symbol indices to use for relocs against them, and we can
883 finally write out the relocs. */
884 external_relocs
= ((bfd_byte
*)
885 bfd_malloc (max_output_reloc_count
* relsz
));
886 if (external_relocs
== NULL
)
889 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
891 struct internal_reloc
*irel
;
892 struct internal_reloc
*irelend
;
893 struct coff_link_hash_entry
**rel_hash
;
896 if (o
->reloc_count
== 0)
899 irel
= finfo
.section_info
[o
->target_index
].relocs
;
900 irelend
= irel
+ o
->reloc_count
;
901 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
902 erel
= external_relocs
;
903 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
905 if (*rel_hash
!= NULL
)
907 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
908 irel
->r_symndx
= (*rel_hash
)->indx
;
910 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
913 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
914 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
915 abfd
) != relsz
* o
->reloc_count
)
919 free (external_relocs
);
920 external_relocs
= NULL
;
923 /* Free up the section information. */
924 if (finfo
.section_info
!= NULL
)
928 for (i
= 0; i
< abfd
->section_count
; i
++)
930 if (finfo
.section_info
[i
].relocs
!= NULL
)
931 free (finfo
.section_info
[i
].relocs
);
932 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
933 free (finfo
.section_info
[i
].rel_hashes
);
935 free (finfo
.section_info
);
936 finfo
.section_info
= NULL
;
939 /* If we have optimized stabs strings, output them. */
940 if (coff_hash_table (info
)->stab_info
!= NULL
)
942 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
946 /* Write out the string table. */
947 if (obj_raw_syment_count (abfd
) != 0 || long_section_names
)
950 (obj_sym_filepos (abfd
)
951 + obj_raw_syment_count (abfd
) * symesz
),
955 #if STRING_SIZE_SIZE == 4
957 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
958 (bfd_byte
*) strbuf
);
960 #error Change bfd_h_put_32
963 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
966 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
970 _bfd_stringtab_free (finfo
.strtab
);
972 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
973 not try to write out the symbols. */
974 bfd_get_symcount (abfd
) = 0;
979 if (debug_merge_allocated
)
980 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
981 if (finfo
.strtab
!= NULL
)
982 _bfd_stringtab_free (finfo
.strtab
);
983 if (finfo
.section_info
!= NULL
)
987 for (i
= 0; i
< abfd
->section_count
; i
++)
989 if (finfo
.section_info
[i
].relocs
!= NULL
)
990 free (finfo
.section_info
[i
].relocs
);
991 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
992 free (finfo
.section_info
[i
].rel_hashes
);
994 free (finfo
.section_info
);
996 if (finfo
.internal_syms
!= NULL
)
997 free (finfo
.internal_syms
);
998 if (finfo
.sec_ptrs
!= NULL
)
999 free (finfo
.sec_ptrs
);
1000 if (finfo
.sym_indices
!= NULL
)
1001 free (finfo
.sym_indices
);
1002 if (finfo
.outsyms
!= NULL
)
1003 free (finfo
.outsyms
);
1004 if (finfo
.linenos
!= NULL
)
1005 free (finfo
.linenos
);
1006 if (finfo
.contents
!= NULL
)
1007 free (finfo
.contents
);
1008 if (finfo
.external_relocs
!= NULL
)
1009 free (finfo
.external_relocs
);
1010 if (finfo
.internal_relocs
!= NULL
)
1011 free (finfo
.internal_relocs
);
1012 if (external_relocs
!= NULL
)
1013 free (external_relocs
);
1017 /* parse out a -heap <reserved>,<commit> line */
1020 dores_com (ptr
, output_bfd
, heap
)
1025 if (coff_data(output_bfd
)->pe
)
1027 int val
= strtoul (ptr
, &ptr
, 0);
1029 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1031 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1035 int val
= strtoul (ptr
+1, &ptr
, 0);
1037 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1039 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1045 static char *get_name(ptr
, dst
)
1052 while (*ptr
&& *ptr
!= ' ')
1058 /* Process any magic embedded commands in a section called .drectve */
1061 process_embedded_commands (output_bfd
, info
, abfd
)
1063 struct bfd_link_info
*info
;
1066 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1073 copy
= bfd_malloc ((size_t) sec
->_raw_size
);
1076 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1081 e
= copy
+ sec
->_raw_size
;
1082 for (s
= copy
; s
< e
; )
1088 if (strncmp (s
,"-attr", 5) == 0)
1100 s
= get_name(s
, &name
);
1101 s
= get_name(s
, &attribs
);
1121 asec
= bfd_get_section_by_name (abfd
, name
);
1124 asec
->flags
|= SEC_CODE
;
1126 asec
->flags
|= SEC_READONLY
;
1129 else if (strncmp (s
,"-heap", 5) == 0)
1131 s
= dores_com (s
+5, output_bfd
, 1);
1133 else if (strncmp (s
,"-stack", 6) == 0)
1135 s
= dores_com (s
+6, output_bfd
, 0);
1144 /* Place a marker against all symbols which are used by relocations.
1145 This marker can be picked up by the 'do we skip this symbol ?'
1146 loop in _bfd_coff_link_input_bfd() and used to prevent skipping
1151 mark_relocs (finfo
, input_bfd
)
1152 struct coff_final_link_info
* finfo
;
1157 if ((bfd_get_file_flags (input_bfd
) & HAS_SYMS
) == 0)
1160 for (a
= input_bfd
->sections
; a
!= (asection
*) NULL
; a
= a
->next
)
1162 struct internal_reloc
* internal_relocs
;
1163 struct internal_reloc
* irel
;
1164 struct internal_reloc
* irelend
;
1167 if ((a
->flags
& SEC_RELOC
) == 0 || a
->reloc_count
< 1)
1170 /* Read in the relocs. */
1171 internal_relocs
= _bfd_coff_read_internal_relocs
1172 (input_bfd
, a
, false,
1173 finfo
->external_relocs
,
1174 finfo
->info
->relocateable
,
1175 (finfo
->info
->relocateable
1176 ? (finfo
->section_info
[ a
->output_section
->target_index
].relocs
+ a
->output_section
->reloc_count
)
1177 : finfo
->internal_relocs
)
1180 if (internal_relocs
== NULL
)
1183 irel
= internal_relocs
;
1184 irelend
= irel
+ a
->reloc_count
;
1186 /* Place a mark in the sym_indices array (whose entries have
1187 been initialised to 0) for all of the symbols that are used
1188 in the relocation table. This will then be picked up in the
1191 for (; irel
< irelend
; irel
++)
1193 finfo
->sym_indices
[ irel
->r_symndx
] = -1;
1198 /* Link an input file into the linker output file. This function
1199 handles all the sections and relocations of the input file at once. */
1202 _bfd_coff_link_input_bfd (finfo
, input_bfd
)
1203 struct coff_final_link_info
*finfo
;
1206 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1207 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1208 asection
*, struct internal_reloc
*,
1211 const char *strings
;
1212 bfd_size_type syment_base
;
1213 unsigned int n_tmask
;
1214 unsigned int n_btshft
;
1216 bfd_size_type isymesz
;
1217 bfd_size_type osymesz
;
1218 bfd_size_type linesz
;
1221 struct internal_syment
*isymp
;
1224 unsigned long output_index
;
1226 struct coff_link_hash_entry
**sym_hash
;
1229 /* Move all the symbols to the output file. */
1231 output_bfd
= finfo
->output_bfd
;
1232 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1234 syment_base
= obj_raw_syment_count (output_bfd
);
1235 isymesz
= bfd_coff_symesz (input_bfd
);
1236 osymesz
= bfd_coff_symesz (output_bfd
);
1237 linesz
= bfd_coff_linesz (input_bfd
);
1238 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1240 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1241 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1243 /* Define macros so that ISFCN, et. al., macros work correctly. */
1244 #define N_TMASK n_tmask
1245 #define N_BTSHFT n_btshft
1248 if (! finfo
->info
->keep_memory
)
1251 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1254 if (! _bfd_coff_get_external_symbols (input_bfd
))
1257 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1258 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1259 isymp
= finfo
->internal_syms
;
1260 secpp
= finfo
->sec_ptrs
;
1261 indexp
= finfo
->sym_indices
;
1262 output_index
= syment_base
;
1263 outsym
= finfo
->outsyms
;
1265 if (coff_data (output_bfd
)->pe
)
1267 if (! process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1271 /* If we are going to perform relocations and also strip/discard some symbols
1272 then we must make sure that we do not strip/discard those symbols that are
1273 going to be involved in the relocations */
1274 if (( finfo
->info
->strip
!= strip_none
1275 || finfo
->info
->discard
!= discard_none
)
1276 && finfo
->info
->relocateable
)
1278 /* mark the symbol array as 'not-used' */
1279 memset (indexp
, 0, obj_raw_syment_count (input_bfd
) * sizeof * indexp
);
1281 mark_relocs (finfo
, input_bfd
);
1284 while (esym
< esym_end
)
1286 struct internal_syment isym
;
1289 boolean dont_skip_symbol
;
1292 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1294 /* Make a copy of *isymp so that the relocate_section function
1295 always sees the original values. This is more reliable than
1296 always recomputing the symbol value even if we are stripping
1300 if (isym
.n_scnum
!= 0)
1301 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1304 if (isym
.n_value
== 0)
1305 *secpp
= bfd_und_section_ptr
;
1307 *secpp
= bfd_com_section_ptr
;
1310 /* Extract the flag indicating if this symbol is used by a
1312 if ((finfo
->info
->strip
!= strip_none
1313 || finfo
->info
->discard
!= discard_none
)
1314 && finfo
->info
->relocateable
)
1315 dont_skip_symbol
= *indexp
;
1317 dont_skip_symbol
= false;
1323 add
= 1 + isym
.n_numaux
;
1325 /* If we are stripping all symbols, we want to skip this one. */
1326 if (finfo
->info
->strip
== strip_all
&& ! dont_skip_symbol
)
1331 if (isym
.n_sclass
== C_EXT
1333 || isym
.n_sclass
== C_SYSTEM
1335 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1337 /* This is a global symbol. Global symbols come at the
1338 end of the symbol table, so skip them for now.
1339 Function symbols, however, are an exception, and are
1340 not moved to the end. */
1342 if (! ISFCN (isym
.n_type
))
1347 /* This is a local symbol. Skip it if we are discarding
1349 if (finfo
->info
->discard
== discard_all
&& ! dont_skip_symbol
)
1354 /* If we stripping debugging symbols, and this is a debugging
1355 symbol, then skip it. FIXME: gas sets the section to N_ABS
1356 for some types of debugging symbols; I don't know if this is
1357 a bug or not. In any case, we handle it here. */
1359 && finfo
->info
->strip
== strip_debugger
1360 && ! dont_skip_symbol
1361 && (isym
.n_scnum
== N_DEBUG
1362 || (isym
.n_scnum
== N_ABS
1363 && (isym
.n_sclass
== C_AUTO
1364 || isym
.n_sclass
== C_REG
1365 || isym
.n_sclass
== C_MOS
1366 || isym
.n_sclass
== C_MOE
1367 || isym
.n_sclass
== C_MOU
1368 || isym
.n_sclass
== C_ARG
1369 || isym
.n_sclass
== C_REGPARM
1370 || isym
.n_sclass
== C_FIELD
1371 || isym
.n_sclass
== C_EOS
))))
1374 /* If some symbols are stripped based on the name, work out the
1375 name and decide whether to skip this symbol. */
1377 && (finfo
->info
->strip
== strip_some
1378 || finfo
->info
->discard
== discard_l
))
1381 char buf
[SYMNMLEN
+ 1];
1383 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1387 if (! dont_skip_symbol
1388 && ((finfo
->info
->strip
== strip_some
1389 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1392 && finfo
->info
->discard
== discard_l
1393 && bfd_is_local_label_name (input_bfd
, name
))))
1397 /* If this is an enum, struct, or union tag, see if we have
1398 already output an identical type. */
1400 && (finfo
->output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) == 0
1401 && (isym
.n_sclass
== C_ENTAG
1402 || isym
.n_sclass
== C_STRTAG
1403 || isym
.n_sclass
== C_UNTAG
)
1404 && isym
.n_numaux
== 1)
1407 char buf
[SYMNMLEN
+ 1];
1408 struct coff_debug_merge_hash_entry
*mh
;
1409 struct coff_debug_merge_type
*mt
;
1410 union internal_auxent aux
;
1411 struct coff_debug_merge_element
**epp
;
1412 bfd_byte
*esl
, *eslend
;
1413 struct internal_syment
*islp
;
1415 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1419 /* Ignore fake names invented by compiler; treat them all as
1421 if (*name
== '~' || *name
== '.' || *name
== '$'
1422 || (*name
== bfd_get_symbol_leading_char (input_bfd
)
1423 && (name
[1] == '~' || name
[1] == '.' || name
[1] == '$')))
1426 mh
= coff_debug_merge_hash_lookup (&finfo
->debug_merge
, name
,
1431 /* Allocate memory to hold type information. If this turns
1432 out to be a duplicate, we pass this address to
1434 mt
= ((struct coff_debug_merge_type
*)
1435 bfd_alloc (input_bfd
,
1436 sizeof (struct coff_debug_merge_type
)));
1439 mt
->class = isym
.n_sclass
;
1441 /* Pick up the aux entry, which points to the end of the tag
1443 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esym
+ isymesz
),
1444 isym
.n_type
, isym
.n_sclass
, 0, isym
.n_numaux
,
1447 /* Gather the elements. */
1448 epp
= &mt
->elements
;
1449 mt
->elements
= NULL
;
1451 esl
= esym
+ 2 * isymesz
;
1452 eslend
= ((bfd_byte
*) obj_coff_external_syms (input_bfd
)
1453 + aux
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
* isymesz
);
1454 while (esl
< eslend
)
1456 const char *elename
;
1457 char elebuf
[SYMNMLEN
+ 1];
1460 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esl
, (PTR
) islp
);
1462 *epp
= ((struct coff_debug_merge_element
*)
1463 bfd_alloc (input_bfd
,
1464 sizeof (struct coff_debug_merge_element
)));
1468 elename
= _bfd_coff_internal_syment_name (input_bfd
, islp
,
1470 if (elename
== NULL
)
1473 copy
= (char *) bfd_alloc (input_bfd
, strlen (elename
) + 1);
1476 strcpy (copy
, elename
);
1478 (*epp
)->name
= copy
;
1479 (*epp
)->type
= islp
->n_type
;
1481 if (islp
->n_numaux
>= 1
1482 && islp
->n_type
!= T_NULL
1483 && islp
->n_sclass
!= C_EOS
)
1485 union internal_auxent eleaux
;
1488 bfd_coff_swap_aux_in (input_bfd
, (PTR
) (esl
+ isymesz
),
1489 islp
->n_type
, islp
->n_sclass
, 0,
1490 islp
->n_numaux
, (PTR
) &eleaux
);
1491 indx
= eleaux
.x_sym
.x_tagndx
.l
;
1493 /* FIXME: If this tagndx entry refers to a symbol
1494 defined later in this file, we just ignore it.
1495 Handling this correctly would be tedious, and may
1501 (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1504 (*epp
)->tagndx
= finfo
->sym_indices
[indx
];
1505 if ((*epp
)->tagndx
< 0)
1509 epp
= &(*epp
)->next
;
1512 esl
+= (islp
->n_numaux
+ 1) * isymesz
;
1513 islp
+= islp
->n_numaux
+ 1;
1516 /* See if we already have a definition which matches this
1517 type. We always output the type if it has no elements,
1519 if (mt
->elements
== NULL
)
1520 bfd_release (input_bfd
, (PTR
) mt
);
1523 struct coff_debug_merge_type
*mtl
;
1525 for (mtl
= mh
->types
; mtl
!= NULL
; mtl
= mtl
->next
)
1527 struct coff_debug_merge_element
*me
, *mel
;
1529 if (mtl
->class != mt
->class)
1532 for (me
= mt
->elements
, mel
= mtl
->elements
;
1533 me
!= NULL
&& mel
!= NULL
;
1534 me
= me
->next
, mel
= mel
->next
)
1536 if (strcmp (me
->name
, mel
->name
) != 0
1537 || me
->type
!= mel
->type
1538 || me
->tagndx
!= mel
->tagndx
)
1542 if (me
== NULL
&& mel
== NULL
)
1546 if (mtl
== NULL
|| (bfd_size_type
) mtl
->indx
>= syment_base
)
1548 /* This is the first definition of this type. */
1549 mt
->indx
= output_index
;
1550 mt
->next
= mh
->types
;
1555 /* This is a redefinition which can be merged. */
1556 bfd_release (input_bfd
, (PTR
) mt
);
1557 *indexp
= mtl
->indx
;
1558 add
= (eslend
- esym
) / isymesz
;
1564 /* We now know whether we are to skip this symbol or not. */
1567 /* Adjust the symbol in order to output it. */
1569 if (isym
._n
._n_n
._n_zeroes
== 0
1570 && isym
._n
._n_n
._n_offset
!= 0)
1575 /* This symbol has a long name. Enter it in the string
1576 table we are building. Note that we do not check
1577 bfd_coff_symname_in_debug. That is only true for
1578 XCOFF, and XCOFF requires different linking code
1580 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1584 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1585 if (indx
== (bfd_size_type
) -1)
1587 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1590 if (isym
.n_scnum
> 0)
1592 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1593 isym
.n_value
+= (*secpp
)->output_offset
;
1594 if (! obj_pe (input_bfd
))
1595 isym
.n_value
-= (*secpp
)->vma
;
1596 if (! obj_pe (finfo
->output_bfd
))
1597 isym
.n_value
+= (*secpp
)->output_section
->vma
;
1600 /* The value of a C_FILE symbol is the symbol index of the
1601 next C_FILE symbol. The value of the last C_FILE symbol
1602 is the symbol index to the first external symbol
1603 (actually, coff_renumber_symbols does not get this
1604 right--it just sets the value of the last C_FILE symbol
1605 to zero--and nobody has ever complained about it). We
1606 try to get this right, below, just before we write the
1607 symbols out, but in the general case we may have to write
1608 the symbol out twice. */
1609 if (isym
.n_sclass
== C_FILE
)
1611 if (finfo
->last_file_index
!= -1
1612 && finfo
->last_file
.n_value
!= (long) output_index
)
1614 /* We must correct the value of the last C_FILE entry. */
1615 finfo
->last_file
.n_value
= output_index
;
1616 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1618 /* The last C_FILE symbol is in this input file. */
1619 bfd_coff_swap_sym_out (output_bfd
,
1620 (PTR
) &finfo
->last_file
,
1621 (PTR
) (finfo
->outsyms
1622 + ((finfo
->last_file_index
1628 /* We have already written out the last C_FILE
1629 symbol. We need to write it out again. We
1630 borrow *outsym temporarily. */
1631 bfd_coff_swap_sym_out (output_bfd
,
1632 (PTR
) &finfo
->last_file
,
1634 if (bfd_seek (output_bfd
,
1635 (obj_sym_filepos (output_bfd
)
1636 + finfo
->last_file_index
* osymesz
),
1638 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1644 finfo
->last_file_index
= output_index
;
1645 finfo
->last_file
= isym
;
1648 /* If doing task linking, convert normal global function symbols to
1649 static functions. */
1651 if (finfo
->info
->task_link
&& isym
.n_sclass
== C_EXT
)
1652 isym
.n_sclass
= C_STAT
;
1654 /* Output the symbol. */
1656 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1658 *indexp
= output_index
;
1663 struct coff_link_hash_entry
*h
;
1665 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1667 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1670 /* This can happen if there were errors earlier in
1672 bfd_set_error (bfd_error_bad_value
);
1675 h
->indx
= output_index
;
1678 output_index
+= add
;
1679 outsym
+= add
* osymesz
;
1682 esym
+= add
* isymesz
;
1686 for (--add
; add
> 0; --add
)
1693 /* Fix up the aux entries. This must be done in a separate pass,
1694 because we don't know the correct symbol indices until we have
1695 already decided which symbols we are going to keep. */
1697 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1698 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1699 isymp
= finfo
->internal_syms
;
1700 indexp
= finfo
->sym_indices
;
1701 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1702 outsym
= finfo
->outsyms
;
1703 while (esym
< esym_end
)
1707 add
= 1 + isymp
->n_numaux
;
1710 || (bfd_size_type
) *indexp
< syment_base
)
1711 && (*sym_hash
== NULL
1712 || (*sym_hash
)->auxbfd
!= input_bfd
))
1713 esym
+= add
* isymesz
;
1716 struct coff_link_hash_entry
*h
;
1724 /* The m68k-motorola-sysv assembler will sometimes
1725 generate two symbols with the same name, but only one
1726 will have aux entries. */
1727 BFD_ASSERT (isymp
->n_numaux
== 0
1728 || h
->numaux
== isymp
->n_numaux
);
1736 /* Handle the aux entries. This handling is based on
1737 coff_pointerize_aux. I don't know if it always correct. */
1738 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1740 union internal_auxent aux
;
1741 union internal_auxent
*auxp
;
1747 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1748 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1753 if (isymp
->n_sclass
== C_FILE
)
1755 /* If this is a long filename, we must put it in the
1757 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1758 && auxp
->x_file
.x_n
.x_offset
!= 0)
1760 const char *filename
;
1763 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1764 >= STRING_SIZE_SIZE
);
1765 if (strings
== NULL
)
1767 strings
= _bfd_coff_read_string_table (input_bfd
);
1768 if (strings
== NULL
)
1771 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1772 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1774 if (indx
== (bfd_size_type
) -1)
1776 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1779 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1783 if (ISFCN (isymp
->n_type
)
1784 || ISTAG (isymp
->n_sclass
)
1785 || isymp
->n_sclass
== C_BLOCK
1786 || isymp
->n_sclass
== C_FCN
)
1788 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1790 && indx
< obj_raw_syment_count (input_bfd
))
1792 /* We look forward through the symbol for
1793 the index of the next symbol we are going
1794 to include. I don't know if this is
1796 while ((finfo
->sym_indices
[indx
] < 0
1797 || ((bfd_size_type
) finfo
->sym_indices
[indx
]
1799 && indx
< obj_raw_syment_count (input_bfd
))
1801 if (indx
>= obj_raw_syment_count (input_bfd
))
1802 indx
= output_index
;
1804 indx
= finfo
->sym_indices
[indx
];
1805 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1809 indx
= auxp
->x_sym
.x_tagndx
.l
;
1810 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1814 symindx
= finfo
->sym_indices
[indx
];
1816 auxp
->x_sym
.x_tagndx
.l
= 0;
1818 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1821 /* The .bf symbols are supposed to be linked through
1822 the endndx field. We need to carry this list
1823 across object files. */
1826 && isymp
->n_sclass
== C_FCN
1827 && (isymp
->_n
._n_n
._n_zeroes
!= 0
1828 || isymp
->_n
._n_n
._n_offset
== 0)
1829 && isymp
->_n
._n_name
[0] == '.'
1830 && isymp
->_n
._n_name
[1] == 'b'
1831 && isymp
->_n
._n_name
[2] == 'f'
1832 && isymp
->_n
._n_name
[3] == '\0')
1834 if (finfo
->last_bf_index
!= -1)
1836 finfo
->last_bf
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1839 if ((bfd_size_type
) finfo
->last_bf_index
1844 /* The last .bf symbol is in this input
1845 file. This will only happen if the
1846 assembler did not set up the .bf
1847 endndx symbols correctly. */
1848 auxout
= (PTR
) (finfo
->outsyms
1849 + ((finfo
->last_bf_index
1852 bfd_coff_swap_aux_out (output_bfd
,
1853 (PTR
) &finfo
->last_bf
,
1861 /* We have already written out the last
1862 .bf aux entry. We need to write it
1863 out again. We borrow *outsym
1864 temporarily. FIXME: This case should
1866 bfd_coff_swap_aux_out (output_bfd
,
1867 (PTR
) &finfo
->last_bf
,
1872 if (bfd_seek (output_bfd
,
1873 (obj_sym_filepos (output_bfd
)
1874 + finfo
->last_bf_index
* osymesz
),
1876 || bfd_write (outsym
, osymesz
, 1,
1877 output_bfd
) != osymesz
)
1882 if (auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
!= 0)
1883 finfo
->last_bf_index
= -1;
1886 /* The endndx field of this aux entry must
1887 be updated with the symbol number of the
1889 finfo
->last_bf
= *auxp
;
1890 finfo
->last_bf_index
= (((outsym
- finfo
->outsyms
)
1899 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1900 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1914 /* Relocate the line numbers, unless we are stripping them. */
1915 if (finfo
->info
->strip
== strip_none
1916 || finfo
->info
->strip
== strip_some
)
1918 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1924 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1925 build_link_order in ldwrite.c will not have created a
1926 link order, which means that we will not have seen this
1927 input section in _bfd_coff_final_link, which means that
1928 we will not have allocated space for the line numbers of
1929 this section. I don't think line numbers can be
1930 meaningful for a section which does not have
1931 SEC_HAS_CONTENTS set, but, if they do, this must be
1933 if (o
->lineno_count
== 0
1934 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1937 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1938 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1939 input_bfd
) != linesz
* o
->lineno_count
)
1942 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1943 eline
= finfo
->linenos
;
1944 elineend
= eline
+ linesz
* o
->lineno_count
;
1945 for (; eline
< elineend
; eline
+= linesz
)
1947 struct internal_lineno iline
;
1949 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1951 if (iline
.l_lnno
!= 0)
1952 iline
.l_addr
.l_paddr
+= offset
;
1953 else if (iline
.l_addr
.l_symndx
>= 0
1954 && ((unsigned long) iline
.l_addr
.l_symndx
1955 < obj_raw_syment_count (input_bfd
)))
1959 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1963 /* These line numbers are attached to a symbol
1964 which we are stripping. We should really
1965 just discard the line numbers, but that would
1966 be a pain because we have already counted
1972 struct internal_syment is
;
1973 union internal_auxent ia
;
1975 /* Fix up the lnnoptr field in the aux entry of
1976 the symbol. It turns out that we can't do
1977 this when we modify the symbol aux entries,
1978 because gas sometimes screws up the lnnoptr
1979 field and makes it an offset from the start
1980 of the line numbers rather than an absolute
1982 bfd_coff_swap_sym_in (output_bfd
,
1983 (PTR
) (finfo
->outsyms
1984 + ((indx
- syment_base
)
1987 if ((ISFCN (is
.n_type
)
1988 || is
.n_sclass
== C_BLOCK
)
1989 && is
.n_numaux
>= 1)
1993 auxptr
= (PTR
) (finfo
->outsyms
1994 + ((indx
- syment_base
+ 1)
1996 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1997 is
.n_type
, is
.n_sclass
,
1998 0, is
.n_numaux
, (PTR
) &ia
);
1999 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
2000 (o
->output_section
->line_filepos
2001 + o
->output_section
->lineno_count
* linesz
2002 + eline
- finfo
->linenos
);
2003 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
2004 is
.n_type
, is
.n_sclass
, 0,
2005 is
.n_numaux
, auxptr
);
2009 iline
.l_addr
.l_symndx
= indx
;
2012 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
2015 if (bfd_seek (output_bfd
,
2016 (o
->output_section
->line_filepos
2017 + o
->output_section
->lineno_count
* linesz
),
2019 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
2020 output_bfd
) != linesz
* o
->lineno_count
)
2023 o
->output_section
->lineno_count
+= o
->lineno_count
;
2027 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
2028 symbol will be the first symbol in the next input file. In the
2029 normal case, this will save us from writing out the C_FILE symbol
2031 if (finfo
->last_file_index
!= -1
2032 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
2034 finfo
->last_file
.n_value
= output_index
;
2035 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
2036 (PTR
) (finfo
->outsyms
2037 + ((finfo
->last_file_index
- syment_base
)
2041 /* Write the modified symbols to the output file. */
2042 if (outsym
> finfo
->outsyms
)
2044 if (bfd_seek (output_bfd
,
2045 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
2047 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
2049 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
2052 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
2053 + (outsym
- finfo
->outsyms
) / osymesz
)
2056 obj_raw_syment_count (output_bfd
) = output_index
;
2059 /* Relocate the contents of each section. */
2060 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
2061 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
2064 struct coff_section_tdata
*secdata
;
2066 if (! o
->linker_mark
)
2068 /* This section was omitted from the link. */
2072 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0
2073 || (o
->_raw_size
== 0 && (o
->flags
& SEC_RELOC
) == 0))
2075 if ((o
->flags
& SEC_RELOC
) != 0
2076 && o
->reloc_count
!= 0)
2078 ((*_bfd_error_handler
)
2079 ("%s: relocs in section `%s', but it has no contents",
2080 bfd_get_filename (input_bfd
),
2081 bfd_get_section_name (input_bfd
, o
)));
2082 bfd_set_error (bfd_error_no_contents
);
2089 secdata
= coff_section_data (input_bfd
, o
);
2090 if (secdata
!= NULL
&& secdata
->contents
!= NULL
)
2091 contents
= secdata
->contents
;
2094 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
2095 (file_ptr
) 0, o
->_raw_size
))
2097 contents
= finfo
->contents
;
2100 if ((o
->flags
& SEC_RELOC
) != 0)
2103 struct internal_reloc
*internal_relocs
;
2104 struct internal_reloc
*irel
;
2106 /* Read in the relocs. */
2107 target_index
= o
->output_section
->target_index
;
2108 internal_relocs
= (_bfd_coff_read_internal_relocs
2109 (input_bfd
, o
, false, finfo
->external_relocs
,
2110 finfo
->info
->relocateable
,
2111 (finfo
->info
->relocateable
2112 ? (finfo
->section_info
[target_index
].relocs
2113 + o
->output_section
->reloc_count
)
2114 : finfo
->internal_relocs
)));
2115 if (internal_relocs
== NULL
)
2118 /* Call processor specific code to relocate the section
2120 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
2124 finfo
->internal_syms
,
2128 if (finfo
->info
->relocateable
)
2131 struct internal_reloc
*irelend
;
2132 struct coff_link_hash_entry
**rel_hash
;
2134 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
2135 irel
= internal_relocs
;
2136 irelend
= irel
+ o
->reloc_count
;
2137 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
2138 + o
->output_section
->reloc_count
);
2139 for (; irel
< irelend
; irel
++, rel_hash
++)
2141 struct coff_link_hash_entry
*h
;
2146 /* Adjust the reloc address and symbol index. */
2148 irel
->r_vaddr
+= offset
;
2150 if (irel
->r_symndx
== -1)
2155 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
2163 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
2166 /* This is a global symbol. */
2168 irel
->r_symndx
= h
->indx
;
2171 /* This symbol is being written at the end
2172 of the file, and we do not yet know the
2173 symbol index. We save the pointer to the
2174 hash table entry in the rel_hash list.
2175 We set the indx field to -2 to indicate
2176 that this symbol must not be stripped. */
2185 indx
= finfo
->sym_indices
[irel
->r_symndx
];
2187 irel
->r_symndx
= indx
;
2190 struct internal_syment
*is
;
2192 char buf
[SYMNMLEN
+ 1];
2194 /* This reloc is against a symbol we are
2195 stripping. This should have been handled
2196 by the 'dont_skip_symbol' code in the while
2197 loop at the top of this function. */
2199 is
= finfo
->internal_syms
+ irel
->r_symndx
;
2201 name
= (_bfd_coff_internal_syment_name
2202 (input_bfd
, is
, buf
));
2206 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2207 (finfo
->info
, name
, input_bfd
, o
,
2214 o
->output_section
->reloc_count
+= o
->reloc_count
;
2218 /* Write out the modified section contents. */
2219 if (secdata
== NULL
|| secdata
->stab_info
== NULL
)
2221 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
2222 contents
, o
->output_offset
,
2223 (o
->_cooked_size
!= 0
2230 if (! (_bfd_write_section_stabs
2231 (output_bfd
, &coff_hash_table (finfo
->info
)->stab_info
,
2232 o
, &secdata
->stab_info
, contents
)))
2237 if (! finfo
->info
->keep_memory
)
2239 if (! _bfd_coff_free_symbols (input_bfd
))
2246 /* Write out a global symbol. Called via coff_link_hash_traverse. */
2249 _bfd_coff_write_global_sym (h
, data
)
2250 struct coff_link_hash_entry
*h
;
2253 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2255 struct internal_syment isym
;
2256 bfd_size_type symesz
;
2259 output_bfd
= finfo
->output_bfd
;
2265 && (finfo
->info
->strip
== strip_all
2266 || (finfo
->info
->strip
== strip_some
2267 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
2268 h
->root
.root
.string
, false, false)
2272 switch (h
->root
.type
)
2275 case bfd_link_hash_new
:
2279 case bfd_link_hash_undefined
:
2280 case bfd_link_hash_undefweak
:
2281 isym
.n_scnum
= N_UNDEF
;
2285 case bfd_link_hash_defined
:
2286 case bfd_link_hash_defweak
:
2290 sec
= h
->root
.u
.def
.section
->output_section
;
2291 if (bfd_is_abs_section (sec
))
2292 isym
.n_scnum
= N_ABS
;
2294 isym
.n_scnum
= sec
->target_index
;
2295 isym
.n_value
= (h
->root
.u
.def
.value
2296 + h
->root
.u
.def
.section
->output_offset
);
2297 if (! obj_pe (finfo
->output_bfd
))
2298 isym
.n_value
+= sec
->vma
;
2302 case bfd_link_hash_common
:
2303 isym
.n_scnum
= N_UNDEF
;
2304 isym
.n_value
= h
->root
.u
.c
.size
;
2307 case bfd_link_hash_indirect
:
2308 case bfd_link_hash_warning
:
2309 /* Just ignore these. They can't be handled anyhow. */
2313 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
2314 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
2321 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
2323 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
2325 if (indx
== (bfd_size_type
) -1)
2327 finfo
->failed
= true;
2330 isym
._n
._n_n
._n_zeroes
= 0;
2331 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2334 isym
.n_sclass
= h
->class;
2335 isym
.n_type
= h
->type
;
2337 if (isym
.n_sclass
== C_NULL
)
2338 isym
.n_sclass
= C_EXT
;
2340 /* If doing task linking and this is the pass where we convert defined globals to
2341 statics, then do that conversion now. If the symbol is not being converted,
2342 just ignore it and it will be output during a later pass. */
2343 if (finfo
->global_to_static
)
2345 if (isym
.n_sclass
!= C_EXT
)
2349 isym
.n_sclass
= C_STAT
;
2352 isym
.n_numaux
= h
->numaux
;
2354 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2356 symesz
= bfd_coff_symesz (output_bfd
);
2358 if (bfd_seek (output_bfd
,
2359 (obj_sym_filepos (output_bfd
)
2360 + obj_raw_syment_count (output_bfd
) * symesz
),
2362 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2364 finfo
->failed
= true;
2368 h
->indx
= obj_raw_syment_count (output_bfd
);
2370 ++obj_raw_syment_count (output_bfd
);
2372 /* Write out any associated aux entries. There normally will be
2373 none. If there are any, I have no idea how to modify them. */
2374 for (i
= 0; i
< isym
.n_numaux
; i
++)
2376 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2377 isym
.n_sclass
, i
, isym
.n_numaux
,
2378 (PTR
) finfo
->outsyms
);
2379 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2381 finfo
->failed
= true;
2384 ++obj_raw_syment_count (output_bfd
);
2390 /* Write out task global symbols, converting them to statics. Called
2391 via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
2392 the dirty work, if the symbol we are processing needs conversion. */
2395 _bfd_coff_write_task_globals (h
, data
)
2396 struct coff_link_hash_entry
*h
;
2399 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
2400 boolean rtnval
= true;
2401 boolean save_global_to_static
;
2405 switch (h
->root
.type
)
2407 case bfd_link_hash_defined
:
2408 case bfd_link_hash_defweak
:
2409 save_global_to_static
= finfo
->global_to_static
;
2410 finfo
->global_to_static
= true;
2411 rtnval
= _bfd_coff_write_global_sym (h
, data
);
2412 finfo
->global_to_static
= save_global_to_static
;
2421 /* Handle a link order which is supposed to generate a reloc. */
2424 _bfd_coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2426 struct coff_final_link_info
*finfo
;
2427 asection
*output_section
;
2428 struct bfd_link_order
*link_order
;
2430 reloc_howto_type
*howto
;
2431 struct internal_reloc
*irel
;
2432 struct coff_link_hash_entry
**rel_hash_ptr
;
2434 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2437 bfd_set_error (bfd_error_bad_value
);
2441 if (link_order
->u
.reloc
.p
->addend
!= 0)
2445 bfd_reloc_status_type rstat
;
2448 size
= bfd_get_reloc_size (howto
);
2449 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2453 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2454 link_order
->u
.reloc
.p
->addend
, buf
);
2460 case bfd_reloc_outofrange
:
2462 case bfd_reloc_overflow
:
2463 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2465 (link_order
->type
== bfd_section_reloc_link_order
2466 ? bfd_section_name (output_bfd
,
2467 link_order
->u
.reloc
.p
->u
.section
)
2468 : link_order
->u
.reloc
.p
->u
.name
),
2469 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2470 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2477 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2478 (file_ptr
) link_order
->offset
, size
);
2484 /* Store the reloc information in the right place. It will get
2485 swapped and written out at the end of the final_link routine. */
2487 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2488 + output_section
->reloc_count
);
2489 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2490 + output_section
->reloc_count
);
2492 memset (irel
, 0, sizeof (struct internal_reloc
));
2493 *rel_hash_ptr
= NULL
;
2495 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2497 if (link_order
->type
== bfd_section_reloc_link_order
)
2499 /* We need to somehow locate a symbol in the right section. The
2500 symbol must either have a value of zero, or we must adjust
2501 the addend by the value of the symbol. FIXME: Write this
2502 when we need it. The old linker couldn't handle this anyhow. */
2504 *rel_hash_ptr
= NULL
;
2509 struct coff_link_hash_entry
*h
;
2511 h
= ((struct coff_link_hash_entry
*)
2512 bfd_wrapped_link_hash_lookup (output_bfd
, finfo
->info
,
2513 link_order
->u
.reloc
.p
->u
.name
,
2514 false, false, true));
2518 irel
->r_symndx
= h
->indx
;
2521 /* Set the index to -2 to force this symbol to get
2530 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2531 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2532 (asection
*) NULL
, (bfd_vma
) 0)))
2538 /* FIXME: Is this always right? */
2539 irel
->r_type
= howto
->type
;
2541 /* r_size is only used on the RS/6000, which needs its own linker
2542 routines anyhow. r_extern is only used for ECOFF. */
2544 /* FIXME: What is the right value for r_offset? Is zero OK? */
2546 ++output_section
->reloc_count
;
2551 /* A basic reloc handling routine which may be used by processors with
2555 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2556 input_section
, contents
, relocs
, syms
,
2559 struct bfd_link_info
*info
;
2561 asection
*input_section
;
2563 struct internal_reloc
*relocs
;
2564 struct internal_syment
*syms
;
2565 asection
**sections
;
2567 struct internal_reloc
*rel
;
2568 struct internal_reloc
*relend
;
2571 relend
= rel
+ input_section
->reloc_count
;
2572 for (; rel
< relend
; rel
++)
2575 struct coff_link_hash_entry
*h
;
2576 struct internal_syment
*sym
;
2579 reloc_howto_type
*howto
;
2580 bfd_reloc_status_type rstat
;
2582 symndx
= rel
->r_symndx
;
2591 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2592 sym
= syms
+ symndx
;
2595 /* COFF treats common symbols in one of two ways. Either the
2596 size of the symbol is included in the section contents, or it
2597 is not. We assume that the size is not included, and force
2598 the rtype_to_howto function to adjust the addend as needed. */
2600 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2601 addend
= - sym
->n_value
;
2606 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2611 /* If we are doing a relocateable link, then we can just ignore
2612 a PC relative reloc that is pcrel_offset. It will already
2613 have the correct value. If this is not a relocateable link,
2614 then we should ignore the symbol value. */
2615 if (howto
->pc_relative
&& howto
->pcrel_offset
)
2617 if (info
->relocateable
)
2619 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2620 addend
+= sym
->n_value
;
2631 sec
= bfd_abs_section_ptr
;
2636 sec
= sections
[symndx
];
2637 val
= (sec
->output_section
->vma
2638 + sec
->output_offset
2640 if (! obj_pe (input_bfd
))
2646 if (h
->root
.type
== bfd_link_hash_defined
2647 || h
->root
.type
== bfd_link_hash_defweak
)
2651 sec
= h
->root
.u
.def
.section
;
2652 val
= (h
->root
.u
.def
.value
2653 + sec
->output_section
->vma
2654 + sec
->output_offset
);
2657 else if (! info
->relocateable
)
2659 if (! ((*info
->callbacks
->undefined_symbol
)
2660 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2661 rel
->r_vaddr
- input_section
->vma
)))
2666 if (info
->base_file
)
2668 /* Emit a reloc if the backend thinks it needs it. */
2669 if (sym
&& pe_data (output_bfd
)->in_reloc_p (output_bfd
, howto
))
2671 /* Relocation to a symbol in a section which isn't
2672 absolute. We output the address here to a file.
2673 This file is then read by dlltool when generating the
2674 reloc section. Note that the base file is not
2675 portable between systems. We write out a long here,
2676 and dlltool reads in a long. */
2677 long addr
= (rel
->r_vaddr
2678 - input_section
->vma
2679 + input_section
->output_offset
2680 + input_section
->output_section
->vma
);
2681 if (coff_data (output_bfd
)->pe
)
2682 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2683 if (fwrite (&addr
, 1, sizeof (long), (FILE *) info
->base_file
)
2686 bfd_set_error (bfd_error_system_call
);
2692 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2694 rel
->r_vaddr
- input_section
->vma
,
2703 case bfd_reloc_outofrange
:
2704 (*_bfd_error_handler
)
2705 ("%s: bad reloc address 0x%lx in section `%s'",
2706 bfd_get_filename (input_bfd
),
2707 (unsigned long) rel
->r_vaddr
,
2708 bfd_get_section_name (input_bfd
, input_section
));
2710 case bfd_reloc_overflow
:
2713 char buf
[SYMNMLEN
+ 1];
2718 name
= h
->root
.root
.string
;
2721 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2726 if (! ((*info
->callbacks
->reloc_overflow
)
2727 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2728 input_section
, rel
->r_vaddr
- input_section
->vma
)))