1 /* COFF specific linker code.
2 Copyright 1994, 1995 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 #define STRING_SIZE_SIZE (4)
32 /* Information we keep for each section in the output file when doing
33 a relocateable link. */
35 struct coff_link_section_info
37 /* The relocs to be output. */
38 struct internal_reloc
*relocs
;
39 /* For each reloc against a global symbol whose index was not known
40 when the reloc was handled, the global hash table entry. */
41 struct coff_link_hash_entry
**rel_hashes
;
44 /* Information that we pass around while doing the final link step. */
46 struct coff_final_link_info
48 /* General link information. */
49 struct bfd_link_info
*info
;
52 /* Used to indicate failure in traversal routine. */
54 /* Hash table for long symbol name. */
55 struct bfd_strtab_hash
*strtab
;
56 /* When doing a relocateable link, an array of information kept for
57 each output section, indexed by the target_index field. */
58 struct coff_link_section_info
*section_info
;
59 /* Symbol index of last C_FILE symbol (-1 if none). */
61 /* Contents of last C_FILE symbol. */
62 struct internal_syment last_file
;
63 /* Buffer large enough to hold swapped symbols of any input file. */
64 struct internal_syment
*internal_syms
;
65 /* Buffer large enough to hold sections of symbols of any input file. */
67 /* Buffer large enough to hold output indices of symbols of any
70 /* Buffer large enough to hold output symbols for any input file. */
72 /* Buffer large enough to hold external line numbers for any input
75 /* Buffer large enough to hold any input section. */
77 /* Buffer large enough to hold external relocs of any input section. */
78 bfd_byte
*external_relocs
;
79 /* Buffer large enough to hold swapped relocs of any input section. */
80 struct internal_reloc
*internal_relocs
;
84 static struct bfd_hash_entry
*coff_link_hash_newfunc
85 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
86 static boolean coff_link_add_object_symbols
87 PARAMS ((bfd
*, struct bfd_link_info
*));
88 static boolean coff_link_check_archive_element
89 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
90 static INLINE
const char *_bfd_coff_internal_syment_name
91 PARAMS ((bfd
*, const struct internal_syment
*, char *));
92 static boolean coff_link_check_ar_symbols
93 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
94 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
95 static boolean coff_link_input_bfd
96 PARAMS ((struct coff_final_link_info
*, bfd
*));
97 static boolean coff_write_global_sym
98 PARAMS ((struct coff_link_hash_entry
*, PTR
));
99 static boolean coff_reloc_link_order
100 PARAMS ((bfd
*, struct coff_final_link_info
*, asection
*,
101 struct bfd_link_order
*));
103 /* Create an entry in a COFF linker hash table. */
105 static struct bfd_hash_entry
*
106 coff_link_hash_newfunc (entry
, table
, string
)
107 struct bfd_hash_entry
*entry
;
108 struct bfd_hash_table
*table
;
111 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
113 /* Allocate the structure if it has not already been allocated by a
115 if (ret
== (struct coff_link_hash_entry
*) NULL
)
116 ret
= ((struct coff_link_hash_entry
*)
117 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
118 if (ret
== (struct coff_link_hash_entry
*) NULL
)
120 bfd_set_error (bfd_error_no_memory
);
121 return (struct bfd_hash_entry
*) ret
;
124 /* Call the allocation method of the superclass. */
125 ret
= ((struct coff_link_hash_entry
*)
126 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
128 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
130 /* Set local fields. */
139 return (struct bfd_hash_entry
*) ret
;
142 /* Create a COFF linker hash table. */
144 struct bfd_link_hash_table
*
145 _bfd_coff_link_hash_table_create (abfd
)
148 struct coff_link_hash_table
*ret
;
150 ret
= ((struct coff_link_hash_table
*)
151 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
154 bfd_set_error (bfd_error_no_memory
);
157 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
158 coff_link_hash_newfunc
))
161 return (struct bfd_link_hash_table
*) NULL
;
166 /* Given a COFF BFD, add symbols to the global hash table as
170 _bfd_coff_link_add_symbols (abfd
, info
)
172 struct bfd_link_info
*info
;
174 switch (bfd_get_format (abfd
))
177 return coff_link_add_object_symbols (abfd
, info
);
179 return (_bfd_generic_link_add_archive_symbols
180 (abfd
, info
, coff_link_check_archive_element
));
182 bfd_set_error (bfd_error_wrong_format
);
187 /* Add symbols from a COFF object file. */
190 coff_link_add_object_symbols (abfd
, info
)
192 struct bfd_link_info
*info
;
194 if (! _bfd_coff_get_external_symbols (abfd
))
196 if (! coff_link_add_symbols (abfd
, info
))
198 if (! info
->keep_memory
)
200 if (! _bfd_coff_free_symbols (abfd
))
206 /* Check a single archive element to see if we need to include it in
207 the link. *PNEEDED is set according to whether this element is
208 needed in the link or not. This is called via
209 _bfd_generic_link_add_archive_symbols. */
212 coff_link_check_archive_element (abfd
, info
, pneeded
)
214 struct bfd_link_info
*info
;
217 if (! _bfd_coff_get_external_symbols (abfd
))
220 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
225 if (! coff_link_add_symbols (abfd
, info
))
229 if (! info
->keep_memory
|| ! *pneeded
)
231 if (! _bfd_coff_free_symbols (abfd
))
238 /* Get the name of a symbol. The caller must pass in a buffer of size
241 static INLINE
const char *
242 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
244 const struct internal_syment
*sym
;
247 /* FIXME: It's not clear this will work correctly if sizeof
249 if (sym
->_n
._n_n
._n_zeroes
!= 0
250 || sym
->_n
._n_n
._n_offset
== 0)
252 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
253 buf
[SYMNMLEN
] = '\0';
260 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
261 strings
= obj_coff_strings (abfd
);
264 strings
= _bfd_coff_read_string_table (abfd
);
268 return strings
+ sym
->_n
._n_n
._n_offset
;
272 /* Look through the symbols to see if this object file should be
273 included in the link. */
276 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
278 struct bfd_link_info
*info
;
281 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
282 bfd_size_type symesz
;
288 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
290 symesz
= bfd_coff_symesz (abfd
);
291 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
292 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
293 while (esym
< esym_end
)
295 struct internal_syment sym
;
297 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
299 if ((sym
.n_sclass
== C_EXT
300 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
301 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
304 char buf
[SYMNMLEN
+ 1];
305 struct bfd_link_hash_entry
*h
;
307 /* This symbol is externally visible, and is defined by this
310 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
313 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
315 /* We are only interested in symbols that are currently
316 undefined. If a symbol is currently known to be common,
317 COFF linkers do not bring in an object file which defines
319 if (h
!= (struct bfd_link_hash_entry
*) NULL
320 && h
->type
== bfd_link_hash_undefined
)
322 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
329 esym
+= (sym
.n_numaux
+ 1) * symesz
;
332 /* We do not need this object file. */
336 /* Add all the symbols from an object file to the hash table. */
339 coff_link_add_symbols (abfd
, info
)
341 struct bfd_link_info
*info
;
343 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
344 boolean default_copy
;
345 bfd_size_type symcount
;
346 struct coff_link_hash_entry
**sym_hash
;
347 bfd_size_type symesz
;
351 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
353 if (info
->keep_memory
)
354 default_copy
= false;
358 symcount
= obj_raw_syment_count (abfd
);
360 /* We keep a list of the linker hash table entries that correspond
361 to particular symbols. */
362 sym_hash
= ((struct coff_link_hash_entry
**)
365 * sizeof (struct coff_link_hash_entry
*))));
366 if (sym_hash
== NULL
&& symcount
!= 0)
368 bfd_set_error (bfd_error_no_memory
);
371 obj_coff_sym_hashes (abfd
) = sym_hash
;
373 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
375 symesz
= bfd_coff_symesz (abfd
);
376 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
377 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
378 esym_end
= esym
+ symcount
* symesz
;
379 while (esym
< esym_end
)
381 struct internal_syment sym
;
384 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
386 if (sym
.n_sclass
== C_EXT
387 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
390 char buf
[SYMNMLEN
+ 1];
395 /* This symbol is externally visible. */
397 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
401 /* We must copy the name into memory if we got it from the
402 syment itself, rather than the string table. */
404 if (sym
._n
._n_n
._n_zeroes
!= 0
405 || sym
._n
._n_n
._n_offset
== 0)
410 if (sym
.n_scnum
== 0)
415 section
= bfd_und_section_ptr
;
420 section
= bfd_com_section_ptr
;
425 flags
= BSF_EXPORT
| BSF_GLOBAL
;
426 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
427 value
-= section
->vma
;
430 if (! (_bfd_generic_link_add_one_symbol
431 (info
, abfd
, name
, flags
, section
, value
,
432 (const char *) NULL
, copy
, false,
433 (struct bfd_link_hash_entry
**) sym_hash
)))
436 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
438 if (((*sym_hash
)->class == C_NULL
439 && (*sym_hash
)->type
== T_NULL
)
442 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
444 (*sym_hash
)->class = sym
.n_sclass
;
445 (*sym_hash
)->type
= sym
.n_type
;
446 (*sym_hash
)->numaux
= sym
.n_numaux
;
447 (*sym_hash
)->auxbfd
= abfd
;
448 if (sym
.n_numaux
!= 0)
450 union internal_auxent
*alloc
;
453 union internal_auxent
*iaux
;
455 alloc
= ((union internal_auxent
*)
456 bfd_hash_allocate (&info
->hash
->table
,
458 * sizeof (*alloc
))));
461 bfd_set_error (bfd_error_no_memory
);
464 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
466 i
++, eaux
+= symesz
, iaux
++)
467 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
468 sym
.n_sclass
, i
, sym
.n_numaux
,
470 (*sym_hash
)->aux
= alloc
;
476 esym
+= (sym
.n_numaux
+ 1) * symesz
;
477 sym_hash
+= sym
.n_numaux
+ 1;
484 /* Do the final link step. */
487 _bfd_coff_final_link (abfd
, info
)
489 struct bfd_link_info
*info
;
491 bfd_size_type symesz
;
492 struct coff_final_link_info finfo
;
494 struct bfd_link_order
*p
;
495 size_t max_contents_size
;
496 size_t max_sym_count
;
497 size_t max_lineno_count
;
498 size_t max_reloc_count
;
499 size_t max_output_reloc_count
;
500 file_ptr rel_filepos
;
502 file_ptr line_filepos
;
505 bfd_byte
*external_relocs
= NULL
;
506 char strbuf
[STRING_SIZE_SIZE
];
508 symesz
= bfd_coff_symesz (abfd
);
511 finfo
.output_bfd
= abfd
;
513 finfo
.section_info
= NULL
;
514 finfo
.last_file_index
= -1;
515 finfo
.internal_syms
= NULL
;
516 finfo
.sec_ptrs
= NULL
;
517 finfo
.sym_indices
= NULL
;
518 finfo
.outsyms
= NULL
;
519 finfo
.linenos
= NULL
;
520 finfo
.contents
= NULL
;
521 finfo
.external_relocs
= NULL
;
522 finfo
.internal_relocs
= NULL
;
524 coff_data (abfd
)->link_info
= info
;
526 finfo
.strtab
= _bfd_stringtab_init ();
527 if (finfo
.strtab
== NULL
)
530 /* Compute the file positions for all the sections. */
531 if (! abfd
->output_has_begun
)
532 bfd_coff_compute_section_file_positions (abfd
);
534 /* Count the line numbers and relocation entries required for the
535 output file. Set the file positions for the relocs. */
536 rel_filepos
= obj_relocbase (abfd
);
537 relsz
= bfd_coff_relsz (abfd
);
538 max_contents_size
= 0;
539 max_lineno_count
= 0;
541 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
545 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
547 if (p
->type
== bfd_indirect_link_order
)
551 sec
= p
->u
.indirect
.section
;
553 if (info
->strip
== strip_none
554 || info
->strip
== strip_some
)
555 o
->lineno_count
+= sec
->lineno_count
;
557 if (info
->relocateable
)
558 o
->reloc_count
+= sec
->reloc_count
;
560 if (sec
->_raw_size
> max_contents_size
)
561 max_contents_size
= sec
->_raw_size
;
562 if (sec
->lineno_count
> max_lineno_count
)
563 max_lineno_count
= sec
->lineno_count
;
564 if (sec
->reloc_count
> max_reloc_count
)
565 max_reloc_count
= sec
->reloc_count
;
567 else if (info
->relocateable
568 && (p
->type
== bfd_section_reloc_link_order
569 || p
->type
== bfd_symbol_reloc_link_order
))
572 if (o
->reloc_count
== 0)
576 o
->flags
|= SEC_RELOC
;
577 o
->rel_filepos
= rel_filepos
;
578 rel_filepos
+= o
->reloc_count
* relsz
;
582 /* If doing a relocateable link, allocate space for the pointers we
584 if (info
->relocateable
)
588 /* We use section_count + 1, rather than section_count, because
589 the target_index fields are 1 based. */
590 finfo
.section_info
= ((struct coff_link_section_info
*)
591 malloc ((abfd
->section_count
+ 1)
592 * sizeof (struct coff_link_section_info
)));
593 if (finfo
.section_info
== NULL
)
595 bfd_set_error (bfd_error_no_memory
);
598 for (i
= 0; i
<= abfd
->section_count
; i
++)
600 finfo
.section_info
[i
].relocs
= NULL
;
601 finfo
.section_info
[i
].rel_hashes
= NULL
;
605 /* We now know the size of the relocs, so we can determine the file
606 positions of the line numbers. */
607 line_filepos
= rel_filepos
;
608 linesz
= bfd_coff_linesz (abfd
);
609 max_output_reloc_count
= 0;
610 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
612 if (o
->lineno_count
== 0)
616 o
->line_filepos
= line_filepos
;
617 line_filepos
+= o
->lineno_count
* linesz
;
620 if (o
->reloc_count
!= 0)
622 /* We don't know the indices of global symbols until we have
623 written out all the local symbols. For each section in
624 the output file, we keep an array of pointers to hash
625 table entries. Each entry in the array corresponds to a
626 reloc. When we find a reloc against a global symbol, we
627 set the corresponding entry in this array so that we can
628 fix up the symbol index after we have written out all the
631 Because of this problem, we also keep the relocs in
632 memory until the end of the link. This wastes memory,
633 but only when doing a relocateable link, which is not the
635 BFD_ASSERT (info
->relocateable
);
636 finfo
.section_info
[o
->target_index
].relocs
=
637 ((struct internal_reloc
*)
638 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
639 finfo
.section_info
[o
->target_index
].rel_hashes
=
640 ((struct coff_link_hash_entry
**)
641 malloc (o
->reloc_count
642 * sizeof (struct coff_link_hash_entry
*)));
643 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
644 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
646 bfd_set_error (bfd_error_no_memory
);
650 if (o
->reloc_count
> max_output_reloc_count
)
651 max_output_reloc_count
= o
->reloc_count
;
654 /* Reset the reloc and lineno counts, so that we can use them to
655 count the number of entries we have output so far. */
660 obj_sym_filepos (abfd
) = line_filepos
;
662 /* Figure out the largest number of symbols in an input BFD. Take
663 the opportunity to clear the output_has_begun fields of all the
666 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
670 sub
->output_has_begun
= false;
671 sz
= obj_raw_syment_count (sub
);
672 if (sz
> max_sym_count
)
676 /* Allocate some buffers used while linking. */
677 finfo
.internal_syms
= ((struct internal_syment
*)
678 malloc (max_sym_count
679 * sizeof (struct internal_syment
)));
680 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
681 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
682 finfo
.outsyms
= ((bfd_byte
*)
683 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
684 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
685 * bfd_coff_linesz (abfd
));
686 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
687 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
688 if (! info
->relocateable
)
689 finfo
.internal_relocs
= ((struct internal_reloc
*)
690 malloc (max_reloc_count
691 * sizeof (struct internal_reloc
)));
692 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
693 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
694 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
695 || finfo
.outsyms
== NULL
696 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
697 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
698 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
699 || (! info
->relocateable
700 && finfo
.internal_relocs
== NULL
701 && max_reloc_count
> 0))
703 bfd_set_error (bfd_error_no_memory
);
707 /* We now know the position of everything in the file, except that
708 we don't know the size of the symbol table and therefore we don't
709 know where the string table starts. We just build the string
710 table in memory as we go along. We process all the relocations
711 for a single input file at once. */
712 obj_raw_syment_count (abfd
) = 0;
713 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
715 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
717 if (p
->type
== bfd_indirect_link_order
718 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
719 == bfd_target_coff_flavour
))
721 sub
= p
->u
.indirect
.section
->owner
;
722 if (! sub
->output_has_begun
)
724 if (! coff_link_input_bfd (&finfo
, sub
))
726 sub
->output_has_begun
= true;
729 else if (p
->type
== bfd_section_reloc_link_order
730 || p
->type
== bfd_symbol_reloc_link_order
)
732 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
737 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
743 /* Free up the buffers used by coff_link_input_bfd. */
744 if (finfo
.internal_syms
!= NULL
)
746 free (finfo
.internal_syms
);
747 finfo
.internal_syms
= NULL
;
749 if (finfo
.sec_ptrs
!= NULL
)
751 free (finfo
.sec_ptrs
);
752 finfo
.sec_ptrs
= NULL
;
754 if (finfo
.sym_indices
!= NULL
)
756 free (finfo
.sym_indices
);
757 finfo
.sym_indices
= NULL
;
759 if (finfo
.linenos
!= NULL
)
761 free (finfo
.linenos
);
762 finfo
.linenos
= NULL
;
764 if (finfo
.contents
!= NULL
)
766 free (finfo
.contents
);
767 finfo
.contents
= NULL
;
769 if (finfo
.external_relocs
!= NULL
)
771 free (finfo
.external_relocs
);
772 finfo
.external_relocs
= NULL
;
774 if (finfo
.internal_relocs
!= NULL
)
776 free (finfo
.internal_relocs
);
777 finfo
.internal_relocs
= NULL
;
780 /* The value of the last C_FILE symbol is supposed to be the symbol
781 index of the first external symbol. Write it out again if
783 if (finfo
.last_file_index
!= -1
784 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
786 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
787 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
788 (PTR
) finfo
.outsyms
);
790 (obj_sym_filepos (abfd
)
791 + finfo
.last_file_index
* symesz
),
793 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
797 /* Write out the global symbols. */
798 finfo
.failed
= false;
799 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
804 /* The outsyms buffer is used by coff_write_global_sym. */
805 if (finfo
.outsyms
!= NULL
)
807 free (finfo
.outsyms
);
808 finfo
.outsyms
= NULL
;
811 if (info
->relocateable
)
813 /* Now that we have written out all the global symbols, we know
814 the symbol indices to use for relocs against them, and we can
815 finally write out the relocs. */
816 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
817 if (external_relocs
== NULL
)
819 bfd_set_error (bfd_error_no_memory
);
823 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
825 struct internal_reloc
*irel
;
826 struct internal_reloc
*irelend
;
827 struct coff_link_hash_entry
**rel_hash
;
830 if (o
->reloc_count
== 0)
833 irel
= finfo
.section_info
[o
->target_index
].relocs
;
834 irelend
= irel
+ o
->reloc_count
;
835 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
836 erel
= external_relocs
;
837 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
839 if (*rel_hash
!= NULL
)
841 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
842 irel
->r_symndx
= (*rel_hash
)->indx
;
844 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
847 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
848 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
849 abfd
) != relsz
* o
->reloc_count
)
853 free (external_relocs
);
854 external_relocs
= NULL
;
857 /* Free up the section information. */
858 if (finfo
.section_info
!= NULL
)
862 for (i
= 0; i
< abfd
->section_count
; i
++)
864 if (finfo
.section_info
[i
].relocs
!= NULL
)
865 free (finfo
.section_info
[i
].relocs
);
866 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
867 free (finfo
.section_info
[i
].rel_hashes
);
869 free (finfo
.section_info
);
870 finfo
.section_info
= NULL
;
873 /* Write out the string table. */
875 (obj_sym_filepos (abfd
)
876 + obj_raw_syment_count (abfd
) * symesz
),
880 #if STRING_SIZE_SIZE == 4
882 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
883 (bfd_byte
*) strbuf
);
885 #error Change bfd_h_put_32
888 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
891 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
894 _bfd_stringtab_free (finfo
.strtab
);
896 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
897 not try to write out the symbols. */
898 bfd_get_symcount (abfd
) = 0;
903 if (finfo
.strtab
!= NULL
)
904 _bfd_stringtab_free (finfo
.strtab
);
905 if (finfo
.section_info
!= NULL
)
909 for (i
= 0; i
< abfd
->section_count
; i
++)
911 if (finfo
.section_info
[i
].relocs
!= NULL
)
912 free (finfo
.section_info
[i
].relocs
);
913 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
914 free (finfo
.section_info
[i
].rel_hashes
);
916 free (finfo
.section_info
);
918 if (finfo
.internal_syms
!= NULL
)
919 free (finfo
.internal_syms
);
920 if (finfo
.sec_ptrs
!= NULL
)
921 free (finfo
.sec_ptrs
);
922 if (finfo
.sym_indices
!= NULL
)
923 free (finfo
.sym_indices
);
924 if (finfo
.outsyms
!= NULL
)
925 free (finfo
.outsyms
);
926 if (finfo
.linenos
!= NULL
)
927 free (finfo
.linenos
);
928 if (finfo
.contents
!= NULL
)
929 free (finfo
.contents
);
930 if (finfo
.external_relocs
!= NULL
)
931 free (finfo
.external_relocs
);
932 if (finfo
.internal_relocs
!= NULL
)
933 free (finfo
.internal_relocs
);
934 if (external_relocs
!= NULL
)
935 free (external_relocs
);
939 /* Read in and swap the relocs. This returns a buffer holding the
940 relocs for section SEC in file ABFD. If CACHE is true and
941 INTERNAL_RELOCS is NULL, the relocs read in wil be saved in case
942 the function is called again. If EXTERNAL_RELOCS is not NULL, it
943 is a buffer large enough to hold the unswapped relocs. If
944 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
945 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
946 value must be INTERNAL_RELOCS. The function returns NULL on error. */
948 struct internal_reloc
*
949 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
950 require_internal
, internal_relocs
)
954 bfd_byte
*external_relocs
;
955 boolean require_internal
;
956 struct internal_reloc
*internal_relocs
;
959 bfd_byte
*free_external
= NULL
;
960 struct internal_reloc
*free_internal
= NULL
;
963 struct internal_reloc
*irel
;
965 if (coff_section_data (abfd
, sec
) != NULL
966 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
968 if (! require_internal
)
969 return coff_section_data (abfd
, sec
)->relocs
;
970 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
971 sec
->reloc_count
* sizeof (struct internal_reloc
));
972 return internal_relocs
;
975 relsz
= bfd_coff_relsz (abfd
);
977 if (external_relocs
== NULL
)
979 free_external
= (bfd_byte
*) malloc (sec
->reloc_count
* relsz
);
980 if (free_external
== NULL
&& sec
->reloc_count
> 0)
982 bfd_set_error (bfd_error_no_memory
);
985 external_relocs
= free_external
;
988 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
989 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
990 != relsz
* sec
->reloc_count
))
993 if (internal_relocs
== NULL
)
995 free_internal
= ((struct internal_reloc
*)
996 malloc (sec
->reloc_count
997 * sizeof (struct internal_reloc
)));
998 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
1000 bfd_set_error (bfd_error_no_memory
);
1003 internal_relocs
= free_internal
;
1006 /* Swap in the relocs. */
1007 erel
= external_relocs
;
1008 erel_end
= erel
+ relsz
* sec
->reloc_count
;
1009 irel
= internal_relocs
;
1010 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
1011 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
1013 if (free_external
!= NULL
)
1015 free (free_external
);
1016 free_external
= NULL
;
1019 if (cache
&& free_internal
!= NULL
)
1021 if (coff_section_data (abfd
, sec
) == NULL
)
1023 sec
->used_by_bfd
= ((PTR
) bfd_zalloc (abfd
,
1024 sizeof (struct coff_section_tdata
)));
1025 if (sec
->used_by_bfd
== NULL
)
1027 bfd_set_error (bfd_error_no_memory
);
1030 coff_section_data (abfd
, sec
)->contents
= NULL
;
1032 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
1035 return internal_relocs
;
1038 if (free_external
!= NULL
)
1039 free (free_external
);
1040 if (free_internal
!= NULL
)
1041 free (free_internal
);
1047 /* parse out a -heap <reserved>,<commit> line */
1050 dores_com (ptr
, output_bfd
, heap
)
1055 if (coff_data(output_bfd
)->pe
)
1057 int val
= strtoul (ptr
, &ptr
, 0);
1059 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapReserve
=val
;
1061 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackReserve
=val
;
1065 int val
= strtoul (ptr
+1, &ptr
, 0);
1067 pe_data(output_bfd
)->pe_opthdr
.SizeOfHeapCommit
=val
;
1069 pe_data(output_bfd
)->pe_opthdr
.SizeOfStackCommit
=val
;
1075 static char *get_name(ptr
, dst
)
1082 while (*ptr
&& *ptr
!= ' ')
1087 /* Process any magic embedded commands in a section called .drectve */
1090 process_embedded_commands (output_bfd
, info
, abfd
)
1092 struct bfd_link_info
*info
;
1095 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
1102 copy
= malloc ((size_t) sec
->_raw_size
);
1105 bfd_set_error (bfd_error_no_memory
);
1108 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
1113 e
= copy
+ sec
->_raw_size
;
1114 for (s
= copy
; s
< e
; )
1120 if (strncmp (s
,"-attr", 5) == 0)
1132 s
= get_name(s
, &name
);
1133 s
= get_name(s
, &attribs
);
1153 asec
= bfd_get_section_by_name (abfd
, name
);
1156 asec
->flags
|= SEC_CODE
;
1158 asec
->flags
|= SEC_READONLY
;
1161 else if (strncmp (s
,"-heap", 5) == 0)
1163 s
= dores_com (s
+5, output_bfd
, 1);
1165 else if (strncmp (s
,"-stack", 6) == 0)
1167 s
= dores_com (s
+6, output_bfd
, 0);
1176 /* Link an input file into the linker output file. This function
1177 handles all the sections and relocations of the input file at once. */
1180 coff_link_input_bfd (finfo
, input_bfd
)
1181 struct coff_final_link_info
*finfo
;
1184 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1185 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1186 asection
*, struct internal_reloc
*,
1189 const char *strings
;
1190 bfd_size_type syment_base
;
1191 unsigned int n_tmask
;
1192 unsigned int n_btshft
;
1194 bfd_size_type isymesz
;
1195 bfd_size_type osymesz
;
1196 bfd_size_type linesz
;
1199 struct internal_syment
*isymp
;
1202 unsigned long output_index
;
1204 struct coff_link_hash_entry
**sym_hash
;
1207 /* Move all the symbols to the output file. */
1209 output_bfd
= finfo
->output_bfd
;
1210 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1212 syment_base
= obj_raw_syment_count (output_bfd
);
1213 isymesz
= bfd_coff_symesz (input_bfd
);
1214 osymesz
= bfd_coff_symesz (output_bfd
);
1215 linesz
= bfd_coff_linesz (input_bfd
);
1216 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1218 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1219 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1221 /* Define macros so that ISFCN, et. al., macros work correctly. */
1222 #define N_TMASK n_tmask
1223 #define N_BTSHFT n_btshft
1226 if (! finfo
->info
->keep_memory
)
1229 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1232 if (! _bfd_coff_get_external_symbols (input_bfd
))
1235 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1236 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1237 isymp
= finfo
->internal_syms
;
1238 secpp
= finfo
->sec_ptrs
;
1239 indexp
= finfo
->sym_indices
;
1240 output_index
= syment_base
;
1241 outsym
= finfo
->outsyms
;
1243 if (coff_data(output_bfd
)->pe
)
1245 if (!process_embedded_commands (output_bfd
, finfo
->info
, input_bfd
))
1249 while (esym
< esym_end
)
1251 struct internal_syment isym
;
1256 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1258 /* Make a copy of *isymp so that the relocate_section function
1259 always sees the original values. This is more reliable than
1260 always recomputing the symbol value even if we are stripping
1264 if (isym
.n_scnum
!= 0)
1265 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1268 if (isym
.n_value
== 0)
1269 *secpp
= bfd_und_section_ptr
;
1271 *secpp
= bfd_com_section_ptr
;
1278 add
= 1 + isym
.n_numaux
;
1280 /* If we are stripping all symbols, we want to skip this one. */
1281 if (finfo
->info
->strip
== strip_all
)
1286 if (isym
.n_sclass
== C_EXT
1287 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1289 /* This is a global symbol. Global symbols come at the
1290 end of the symbol table, so skip them for now.
1291 Function symbols, however, are an exception, and are
1292 not moved to the end. */
1294 if (! ISFCN (isym
.n_type
))
1299 /* This is a local symbol. Skip it if we are discarding
1301 if (finfo
->info
->discard
== discard_all
)
1306 /* If we stripping debugging symbols, and this is a debugging
1307 symbol, then skip it. */
1309 && finfo
->info
->strip
== strip_debugger
1310 && isym
.n_scnum
== N_DEBUG
)
1313 /* If some symbols are stripped based on the name, work out the
1314 name and decide whether to skip this symbol. */
1316 && (finfo
->info
->strip
== strip_some
1317 || finfo
->info
->discard
== discard_l
))
1320 char buf
[SYMNMLEN
+ 1];
1322 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1326 if ((finfo
->info
->strip
== strip_some
1327 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1330 && finfo
->info
->discard
== discard_l
1331 && strncmp (name
, finfo
->info
->lprefix
,
1332 finfo
->info
->lprefix_len
) == 0))
1336 /* We now know whether we are to skip this symbol or not. */
1339 /* Adjust the symbol in order to output it. */
1341 if (isym
._n
._n_n
._n_zeroes
== 0
1342 && isym
._n
._n_n
._n_offset
!= 0)
1347 /* This symbol has a long name. Enter it in the string
1348 table we are building. Note that we do not check
1349 bfd_coff_symname_in_debug. That is only true for
1350 XCOFF, and XCOFF requires different linking code
1352 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1356 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1357 if (indx
== (bfd_size_type
) -1)
1359 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1362 if (isym
.n_scnum
> 0)
1364 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1365 isym
.n_value
+= ((*secpp
)->output_section
->vma
1366 + (*secpp
)->output_offset
1370 /* The value of a C_FILE symbol is the symbol index of the
1371 next C_FILE symbol. The value of the last C_FILE symbol
1372 is the symbol index to the first external symbol
1373 (actually, coff_renumber_symbols does not get this
1374 right--it just sets the value of the last C_FILE symbol
1375 to zero--and nobody has ever complained about it). We
1376 try to get this right, below, just before we write the
1377 symbols out, but in the general case we may have to write
1378 the symbol out twice. */
1379 if (isym
.n_sclass
== C_FILE
)
1381 if (finfo
->last_file_index
!= -1
1382 && finfo
->last_file
.n_value
!= (long) output_index
)
1384 /* We must correct the value of the last C_FILE entry. */
1385 finfo
->last_file
.n_value
= output_index
;
1386 if ((bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1388 /* The last C_FILE symbol is in this input file. */
1389 bfd_coff_swap_sym_out (output_bfd
,
1390 (PTR
) &finfo
->last_file
,
1391 (PTR
) (finfo
->outsyms
1392 + ((finfo
->last_file_index
1398 /* We have already written out the last C_FILE
1399 symbol. We need to write it out again. We
1400 borrow *outsym temporarily. */
1401 bfd_coff_swap_sym_out (output_bfd
,
1402 (PTR
) &finfo
->last_file
,
1404 if (bfd_seek (output_bfd
,
1405 (obj_sym_filepos (output_bfd
)
1406 + finfo
->last_file_index
* osymesz
),
1408 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1414 finfo
->last_file_index
= output_index
;
1415 finfo
->last_file
= isym
;
1418 /* Output the symbol. */
1420 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1422 *indexp
= output_index
;
1427 struct coff_link_hash_entry
*h
;
1429 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1431 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1432 BFD_ASSERT (h
!= NULL
);
1433 h
->indx
= output_index
;
1436 output_index
+= add
;
1437 outsym
+= add
* osymesz
;
1440 esym
+= add
* isymesz
;
1444 for (--add
; add
> 0; --add
)
1451 /* Fix up the aux entries. This must be done in a separate pass,
1452 because we don't know the correct symbol indices until we have
1453 already decided which symbols we are going to keep. */
1455 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1456 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1457 isymp
= finfo
->internal_syms
;
1458 indexp
= finfo
->sym_indices
;
1459 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1460 outsym
= finfo
->outsyms
;
1461 while (esym
< esym_end
)
1465 add
= 1 + isymp
->n_numaux
;
1468 && (*sym_hash
== NULL
1469 || (*sym_hash
)->auxbfd
!= input_bfd
))
1470 esym
+= add
* isymesz
;
1473 struct coff_link_hash_entry
*h
;
1480 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1488 /* Handle the aux entries. This handling is based on
1489 coff_pointerize_aux. I don't know if it always correct. */
1490 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1492 union internal_auxent aux
;
1493 union internal_auxent
*auxp
;
1499 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1500 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1505 if (isymp
->n_sclass
== C_FILE
)
1507 /* If this is a long filename, we must put it in the
1509 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1510 && auxp
->x_file
.x_n
.x_offset
!= 0)
1512 const char *filename
;
1515 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1516 >= STRING_SIZE_SIZE
);
1517 if (strings
== NULL
)
1519 strings
= _bfd_coff_read_string_table (input_bfd
);
1520 if (strings
== NULL
)
1523 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1524 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1526 if (indx
== (bfd_size_type
) -1)
1528 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1531 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1535 if (ISFCN (isymp
->n_type
)
1536 || ISTAG (isymp
->n_sclass
)
1537 || isymp
->n_sclass
== C_BLOCK
)
1539 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1541 && indx
< obj_raw_syment_count (input_bfd
))
1543 /* We look forward through the symbol for
1544 the index of the next symbol we are going
1545 to include. I don't know if this is
1547 while (finfo
->sym_indices
[indx
] < 0
1548 && indx
< obj_raw_syment_count (input_bfd
))
1550 if (indx
>= obj_raw_syment_count (input_bfd
))
1551 indx
= output_index
;
1553 indx
= finfo
->sym_indices
[indx
];
1554 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1558 indx
= auxp
->x_sym
.x_tagndx
.l
;
1559 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1563 symindx
= finfo
->sym_indices
[indx
];
1565 auxp
->x_sym
.x_tagndx
.l
= 0;
1567 auxp
->x_sym
.x_tagndx
.l
= symindx
;
1573 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1574 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1588 /* Relocate the line numbers, unless we are stripping them. */
1589 if (finfo
->info
->strip
== strip_none
1590 || finfo
->info
->strip
== strip_some
)
1592 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1598 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1599 build_link_order in ldwrite.c will not have created a
1600 link order, which means that we will not have seen this
1601 input section in _bfd_coff_final_link, which means that
1602 we will not have allocated space for the line numbers of
1603 this section. I don't think line numbers can be
1604 meaningful for a section which does not have
1605 SEC_HAS_CONTENTS set, but, if they do, this must be
1607 if (o
->lineno_count
== 0
1608 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1611 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1612 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1613 input_bfd
) != linesz
* o
->lineno_count
)
1616 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1617 eline
= finfo
->linenos
;
1618 elineend
= eline
+ linesz
* o
->lineno_count
;
1619 for (; eline
< elineend
; eline
+= linesz
)
1621 struct internal_lineno iline
;
1623 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1625 if (iline
.l_lnno
!= 0)
1626 iline
.l_addr
.l_paddr
+= offset
;
1627 else if (iline
.l_addr
.l_symndx
>= 0
1628 && ((unsigned long) iline
.l_addr
.l_symndx
1629 < obj_raw_syment_count (input_bfd
)))
1633 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1637 /* These line numbers are attached to a symbol
1638 which we are stripping. We should really
1639 just discard the line numbers, but that would
1640 be a pain because we have already counted
1646 struct internal_syment is
;
1647 union internal_auxent ia
;
1649 /* Fix up the lnnoptr field in the aux entry of
1650 the symbol. It turns out that we can't do
1651 this when we modify the symbol aux entries,
1652 because gas sometimes screws up the lnnoptr
1653 field and makes it an offset from the start
1654 of the line numbers rather than an absolute
1656 bfd_coff_swap_sym_in (output_bfd
,
1657 (PTR
) (finfo
->outsyms
1658 + ((indx
- syment_base
)
1661 if ((ISFCN (is
.n_type
)
1662 || is
.n_sclass
== C_BLOCK
)
1663 && is
.n_numaux
>= 1)
1667 auxptr
= (PTR
) (finfo
->outsyms
1668 + ((indx
- syment_base
+ 1)
1670 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1671 is
.n_type
, is
.n_sclass
,
1672 0, is
.n_numaux
, (PTR
) &ia
);
1673 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1674 (o
->output_section
->line_filepos
1675 + o
->output_section
->lineno_count
* linesz
1676 + eline
- finfo
->linenos
);
1677 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1678 is
.n_type
, is
.n_sclass
, 0,
1679 is
.n_numaux
, auxptr
);
1683 iline
.l_addr
.l_symndx
= indx
;
1686 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1689 if (bfd_seek (output_bfd
,
1690 (o
->output_section
->line_filepos
1691 + o
->output_section
->lineno_count
* linesz
),
1693 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1694 output_bfd
) != linesz
* o
->lineno_count
)
1697 o
->output_section
->lineno_count
+= o
->lineno_count
;
1701 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1702 symbol will be the first symbol in the next input file. In the
1703 normal case, this will save us from writing out the C_FILE symbol
1705 if (finfo
->last_file_index
!= -1
1706 && (bfd_size_type
) finfo
->last_file_index
>= syment_base
)
1708 finfo
->last_file
.n_value
= output_index
;
1709 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1710 (PTR
) (finfo
->outsyms
1711 + ((finfo
->last_file_index
- syment_base
)
1715 /* Write the modified symbols to the output file. */
1716 if (outsym
> finfo
->outsyms
)
1718 if (bfd_seek (output_bfd
,
1719 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1721 || (bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1723 != (bfd_size_type
) (outsym
- finfo
->outsyms
)))
1726 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1727 + (outsym
- finfo
->outsyms
) / osymesz
)
1730 obj_raw_syment_count (output_bfd
) = output_index
;
1733 /* Relocate the contents of each section. */
1734 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1735 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1739 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1741 if ((o
->flags
& SEC_RELOC
) != 0
1742 && o
->reloc_count
!= 0)
1744 ((*_bfd_error_handler
)
1745 ("%s: relocs in section `%s', but it has no contents",
1746 bfd_get_filename (input_bfd
),
1747 bfd_get_section_name (input_bfd
, o
)));
1748 bfd_set_error (bfd_error_no_contents
);
1755 if (coff_section_data (input_bfd
, o
) != NULL
1756 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1757 contents
= coff_section_data (input_bfd
, o
)->contents
;
1760 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1761 (file_ptr
) 0, o
->_raw_size
))
1763 contents
= finfo
->contents
;
1766 if ((o
->flags
& SEC_RELOC
) != 0)
1769 struct internal_reloc
*internal_relocs
;
1770 struct internal_reloc
*irel
;
1772 /* Read in the relocs. */
1773 target_index
= o
->output_section
->target_index
;
1774 internal_relocs
= (_bfd_coff_read_internal_relocs
1775 (input_bfd
, o
, false, finfo
->external_relocs
,
1776 finfo
->info
->relocateable
,
1777 (finfo
->info
->relocateable
1778 ? (finfo
->section_info
[target_index
].relocs
1779 + o
->output_section
->reloc_count
)
1780 : finfo
->internal_relocs
)));
1781 if (internal_relocs
== NULL
)
1784 /* Call processor specific code to relocate the section
1786 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1790 finfo
->internal_syms
,
1794 if (finfo
->info
->relocateable
)
1797 struct internal_reloc
*irelend
;
1798 struct coff_link_hash_entry
**rel_hash
;
1800 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1801 irel
= internal_relocs
;
1802 irelend
= irel
+ o
->reloc_count
;
1803 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1804 + o
->output_section
->reloc_count
);
1805 for (; irel
< irelend
; irel
++, rel_hash
++)
1807 struct coff_link_hash_entry
*h
;
1812 /* Adjust the reloc address and symbol index. */
1814 irel
->r_vaddr
+= offset
;
1816 if (irel
->r_symndx
== -1)
1821 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1829 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1832 /* This is a global symbol. */
1834 irel
->r_symndx
= h
->indx
;
1837 /* This symbol is being written at the end
1838 of the file, and we do not yet know the
1839 symbol index. We save the pointer to the
1840 hash table entry in the rel_hash list.
1841 We set the indx field to -2 to indicate
1842 that this symbol must not be stripped. */
1851 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1853 irel
->r_symndx
= indx
;
1856 struct internal_syment
*is
;
1858 char buf
[SYMNMLEN
+ 1];
1860 /* This reloc is against a symbol we are
1861 stripping. It would be possible to
1862 handle this case, but I don't think it's
1864 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1866 name
= (_bfd_coff_internal_syment_name
1867 (input_bfd
, is
, buf
));
1871 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1872 (finfo
->info
, name
, input_bfd
, o
,
1879 o
->output_section
->reloc_count
+= o
->reloc_count
;
1883 /* Write out the modified section contents. */
1884 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1885 contents
, o
->output_offset
,
1886 (o
->_cooked_size
!= 0
1892 if (! finfo
->info
->keep_memory
)
1894 if (! _bfd_coff_free_symbols (input_bfd
))
1901 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1904 coff_write_global_sym (h
, data
)
1905 struct coff_link_hash_entry
*h
;
1908 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1910 struct internal_syment isym
;
1911 bfd_size_type symesz
;
1914 output_bfd
= finfo
->output_bfd
;
1920 && (finfo
->info
->strip
== strip_all
1921 || (finfo
->info
->strip
== strip_some
1922 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1923 h
->root
.root
.string
, false, false)
1927 switch (h
->root
.type
)
1930 case bfd_link_hash_new
:
1934 case bfd_link_hash_undefined
:
1935 case bfd_link_hash_undefweak
:
1936 isym
.n_scnum
= N_UNDEF
;
1940 case bfd_link_hash_defined
:
1941 case bfd_link_hash_defweak
:
1945 sec
= h
->root
.u
.def
.section
->output_section
;
1946 if (bfd_is_abs_section (sec
))
1947 isym
.n_scnum
= N_ABS
;
1949 isym
.n_scnum
= sec
->target_index
;
1950 isym
.n_value
= (h
->root
.u
.def
.value
1952 + h
->root
.u
.def
.section
->output_offset
);
1956 case bfd_link_hash_common
:
1957 isym
.n_scnum
= N_UNDEF
;
1958 isym
.n_value
= h
->root
.u
.c
.size
;
1961 case bfd_link_hash_indirect
:
1962 case bfd_link_hash_warning
:
1963 /* Just ignore these. They can't be handled anyhow. */
1967 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1968 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1975 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1977 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1979 if (indx
== (bfd_size_type
) -1)
1981 finfo
->failed
= true;
1984 isym
._n
._n_n
._n_zeroes
= 0;
1985 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1988 isym
.n_sclass
= h
->class;
1989 isym
.n_type
= h
->type
;
1991 if (isym
.n_sclass
== C_NULL
)
1992 isym
.n_sclass
= C_EXT
;
1994 isym
.n_numaux
= h
->numaux
;
1996 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
1998 symesz
= bfd_coff_symesz (output_bfd
);
2000 if (bfd_seek (output_bfd
,
2001 (obj_sym_filepos (output_bfd
)
2002 + obj_raw_syment_count (output_bfd
) * symesz
),
2004 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2006 finfo
->failed
= true;
2010 h
->indx
= obj_raw_syment_count (output_bfd
);
2012 ++obj_raw_syment_count (output_bfd
);
2014 /* Write out any associated aux entries. There normally will be
2015 none. If there are any, I have no idea how to modify them. */
2016 for (i
= 0; i
< isym
.n_numaux
; i
++)
2018 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2019 isym
.n_sclass
, i
, isym
.n_numaux
,
2020 (PTR
) finfo
->outsyms
);
2021 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2023 finfo
->failed
= true;
2026 ++obj_raw_syment_count (output_bfd
);
2032 /* Handle a link order which is supposed to generate a reloc. */
2035 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2037 struct coff_final_link_info
*finfo
;
2038 asection
*output_section
;
2039 struct bfd_link_order
*link_order
;
2041 reloc_howto_type
*howto
;
2042 struct internal_reloc
*irel
;
2043 struct coff_link_hash_entry
**rel_hash_ptr
;
2045 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2048 bfd_set_error (bfd_error_bad_value
);
2052 if (link_order
->u
.reloc
.p
->addend
!= 0)
2056 bfd_reloc_status_type rstat
;
2059 size
= bfd_get_reloc_size (howto
);
2060 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2063 bfd_set_error (bfd_error_no_memory
);
2067 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2068 link_order
->u
.reloc
.p
->addend
, buf
);
2074 case bfd_reloc_outofrange
:
2076 case bfd_reloc_overflow
:
2077 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2079 (link_order
->type
== bfd_section_reloc_link_order
2080 ? bfd_section_name (output_bfd
,
2081 link_order
->u
.reloc
.p
->u
.section
)
2082 : link_order
->u
.reloc
.p
->u
.name
),
2083 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2084 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2091 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2092 (file_ptr
) link_order
->offset
, size
);
2098 /* Store the reloc information in the right place. It will get
2099 swapped and written out at the end of the final_link routine. */
2101 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2102 + output_section
->reloc_count
);
2103 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2104 + output_section
->reloc_count
);
2106 memset (irel
, 0, sizeof (struct internal_reloc
));
2107 *rel_hash_ptr
= NULL
;
2109 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2111 if (link_order
->type
== bfd_section_reloc_link_order
)
2113 /* We need to somehow locate a symbol in the right section. The
2114 symbol must either have a value of zero, or we must adjust
2115 the addend by the value of the symbol. FIXME: Write this
2116 when we need it. The old linker couldn't handle this anyhow. */
2118 *rel_hash_ptr
= NULL
;
2123 struct coff_link_hash_entry
*h
;
2125 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2126 link_order
->u
.reloc
.p
->u
.name
,
2127 false, false, true);
2131 irel
->r_symndx
= h
->indx
;
2134 /* Set the index to -2 to force this symbol to get
2143 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2144 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2145 (asection
*) NULL
, (bfd_vma
) 0)))
2151 /* FIXME: Is this always right? */
2152 irel
->r_type
= howto
->type
;
2154 /* r_size is only used on the RS/6000, which needs its own linker
2155 routines anyhow. r_extern is only used for ECOFF. */
2157 /* FIXME: What is the right value for r_offset? Is zero OK? */
2159 ++output_section
->reloc_count
;
2164 /* A basic reloc handling routine which may be used by processors with
2168 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2169 input_section
, contents
, relocs
, syms
,
2172 struct bfd_link_info
*info
;
2174 asection
*input_section
;
2176 struct internal_reloc
*relocs
;
2177 struct internal_syment
*syms
;
2178 asection
**sections
;
2180 struct internal_reloc
*rel
;
2181 struct internal_reloc
*relend
;
2185 relend
= rel
+ input_section
->reloc_count
;
2186 for (; rel
< relend
; rel
++)
2189 struct coff_link_hash_entry
*h
;
2190 struct internal_syment
*sym
;
2193 reloc_howto_type
*howto
;
2194 bfd_reloc_status_type rstat
;
2196 symndx
= rel
->r_symndx
;
2205 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2206 sym
= syms
+ symndx
;
2209 /* COFF treats common symbols in one of two ways. Either the
2210 size of the symbol is included in the section contents, or it
2211 is not. We assume that the size is not included, and force
2212 the rtype_to_howto function to adjust the addend as needed. */
2214 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2215 addend
= - sym
->n_value
;
2220 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2233 sec
= bfd_abs_section_ptr
;
2238 sec
= sections
[symndx
];
2239 val
= (sec
->output_section
->vma
2240 + sec
->output_offset
2247 if (h
->root
.type
== bfd_link_hash_defined
2248 || h
->root
.type
== bfd_link_hash_defweak
)
2252 sec
= h
->root
.u
.def
.section
;
2253 val
= (h
->root
.u
.def
.value
2254 + sec
->output_section
->vma
2255 + sec
->output_offset
);
2258 else if (! info
->relocateable
)
2260 if (! ((*info
->callbacks
->undefined_symbol
)
2261 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2262 rel
->r_vaddr
- input_section
->vma
)))
2267 if (info
->base_file
)
2269 /* So if this is non pcrelative, and is referenced
2270 to a section or a common symbol, then it needs a reloc */
2271 if (!howto
->pc_relative
2272 && sym
&& (sym
->n_scnum
|| sym
->n_value
))
2274 /* relocation to a symbol in a section which
2275 isn't absolute - we output the address here
2277 bfd_vma addr
= rel
->r_vaddr
2278 + input_section
->output_offset
2279 + input_section
->output_section
->vma
;
2280 if (coff_data(output_bfd
)->pe
)
2281 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
2282 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2286 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2288 rel
->r_vaddr
- input_section
->vma
,
2297 case bfd_reloc_overflow
:
2300 char buf
[SYMNMLEN
+ 1];
2305 name
= h
->root
.root
.string
;
2308 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2313 if (! ((*info
->callbacks
->reloc_overflow
)
2314 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2315 input_section
, rel
->r_vaddr
- input_section
->vma
)))