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
*));
104 /* These new data and data types are used to keep track of the .idata$4 and
105 .idata$5 relocations which are put into the .idata section for all of the
106 *.dll input libraries linked in. This is not a great solution, and may
107 break in the future if MS changes the format of its libraries, but it
108 does work for the collection of mstools libraries we are currently working
109 with. The main problem is that there are some new majic symbols defined
110 in the libraries which are non-standard coff and simply aren't handled
111 completely by ld. What has been included here will help finish up the job.
112 Basically, during the link, .idata$4 and .idata$5 pointers are correctly
113 relocated to the image. At the very end of the link, the .idata$2
114 information is written. This data appears at the beginning of the .idata
115 section and a 'set' of information appears for each *.dll passed in.
116 Each set of information consists of 3 addresses, a pointer to the .idata$4
117 start, a pointer to .idata$6 (which has the name of the dll), and a pointer
118 to .idata$5 start. The idata$4 and 5 information is a list of pointers
119 which appear to point to the name of various functions found within the dll.
120 When invoked, the loader will write over these names with the correct
121 addresses to use for these functions.
122 Without this 'fix', all information appears correctly except for the
123 addresses of the .idata$4 and 5 starts within the .idata$2 portion of the
124 .idata section. What we will do is to keep track of the dll's processed
125 and the number of functions needed from each dll. From this information
126 we can correctly compute the start of the idata$4 and 5 lists for each
127 dll in the idata section */
128 static int num_DLLs_done
= 0;
129 static int num_DLLs
= 0;
130 static int all_entries
= 0;
132 const char * DLL_name
;
135 struct DLL_struct MS_DLL
[10];
136 static bfd_vma idata_4_prev
= 0;
137 static bfd_vma idata_5_prev
= 0;
138 static bfd_vma add_to_val
= 0;
142 /* Create an entry in a COFF linker hash table. */
144 static struct bfd_hash_entry
*
145 coff_link_hash_newfunc (entry
, table
, string
)
146 struct bfd_hash_entry
*entry
;
147 struct bfd_hash_table
*table
;
150 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
152 /* Allocate the structure if it has not already been allocated by a
154 if (ret
== (struct coff_link_hash_entry
*) NULL
)
155 ret
= ((struct coff_link_hash_entry
*)
156 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
157 if (ret
== (struct coff_link_hash_entry
*) NULL
)
159 bfd_set_error (bfd_error_no_memory
);
160 return (struct bfd_hash_entry
*) ret
;
163 /* Call the allocation method of the superclass. */
164 ret
= ((struct coff_link_hash_entry
*)
165 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
167 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
169 /* Set local fields. */
178 return (struct bfd_hash_entry
*) ret
;
181 /* Create a COFF linker hash table. */
183 struct bfd_link_hash_table
*
184 _bfd_coff_link_hash_table_create (abfd
)
187 struct coff_link_hash_table
*ret
;
189 ret
= ((struct coff_link_hash_table
*)
190 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
193 bfd_set_error (bfd_error_no_memory
);
196 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
197 coff_link_hash_newfunc
))
200 return (struct bfd_link_hash_table
*) NULL
;
205 /* Given a COFF BFD, add symbols to the global hash table as
209 _bfd_coff_link_add_symbols (abfd
, info
)
211 struct bfd_link_info
*info
;
213 switch (bfd_get_format (abfd
))
216 return coff_link_add_object_symbols (abfd
, info
);
218 return (_bfd_generic_link_add_archive_symbols
219 (abfd
, info
, coff_link_check_archive_element
));
221 bfd_set_error (bfd_error_wrong_format
);
226 /* Add symbols from a COFF object file. */
229 coff_link_add_object_symbols (abfd
, info
)
231 struct bfd_link_info
*info
;
233 if (! _bfd_coff_get_external_symbols (abfd
))
235 if (! coff_link_add_symbols (abfd
, info
))
237 if (! info
->keep_memory
)
239 if (! _bfd_coff_free_symbols (abfd
))
245 /* Check a single archive element to see if we need to include it in
246 the link. *PNEEDED is set according to whether this element is
247 needed in the link or not. This is called via
248 _bfd_generic_link_add_archive_symbols. */
251 coff_link_check_archive_element (abfd
, info
, pneeded
)
253 struct bfd_link_info
*info
;
256 if (! _bfd_coff_get_external_symbols (abfd
))
259 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
264 if (! coff_link_add_symbols (abfd
, info
))
268 if (! info
->keep_memory
|| ! *pneeded
)
270 if (! _bfd_coff_free_symbols (abfd
))
277 /* Get the name of a symbol. The caller must pass in a buffer of size
280 static INLINE
const char *
281 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
283 const struct internal_syment
*sym
;
286 /* FIXME: It's not clear this will work correctly if sizeof
288 if (sym
->_n
._n_n
._n_zeroes
!= 0
289 || sym
->_n
._n_n
._n_offset
== 0)
291 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
292 buf
[SYMNMLEN
] = '\0';
299 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
300 strings
= obj_coff_strings (abfd
);
303 strings
= _bfd_coff_read_string_table (abfd
);
307 return strings
+ sym
->_n
._n_n
._n_offset
;
311 /* Look through the symbols to see if this object file should be
312 included in the link. */
315 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
317 struct bfd_link_info
*info
;
320 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
321 bfd_size_type symesz
;
327 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
329 symesz
= bfd_coff_symesz (abfd
);
330 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
331 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
332 while (esym
< esym_end
)
334 struct internal_syment sym
;
336 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
338 if ((sym
.n_sclass
== C_EXT
339 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
340 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
343 char buf
[SYMNMLEN
+ 1];
344 struct bfd_link_hash_entry
*h
;
346 /* This symbol is externally visible, and is defined by this
349 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
352 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
354 /* We are only interested in symbols that are currently
355 undefined. If a symbol is currently known to be common,
356 COFF linkers do not bring in an object file which defines
358 if (h
!= (struct bfd_link_hash_entry
*) NULL
359 && h
->type
== bfd_link_hash_undefined
)
361 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
368 esym
+= (sym
.n_numaux
+ 1) * symesz
;
371 /* We do not need this object file. */
375 /* Add all the symbols from an object file to the hash table. */
378 coff_link_add_symbols (abfd
, info
)
380 struct bfd_link_info
*info
;
382 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
383 boolean default_copy
;
384 bfd_size_type symcount
;
385 struct coff_link_hash_entry
**sym_hash
;
386 bfd_size_type symesz
;
390 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
392 if (info
->keep_memory
)
393 default_copy
= false;
397 symcount
= obj_raw_syment_count (abfd
);
399 /* We keep a list of the linker hash table entries that correspond
400 to particular symbols. */
401 sym_hash
= ((struct coff_link_hash_entry
**)
404 * sizeof (struct coff_link_hash_entry
*))));
405 if (sym_hash
== NULL
&& symcount
!= 0)
407 bfd_set_error (bfd_error_no_memory
);
410 obj_coff_sym_hashes (abfd
) = sym_hash
;
412 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
414 symesz
= bfd_coff_symesz (abfd
);
415 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
416 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
417 esym_end
= esym
+ symcount
* symesz
;
418 while (esym
< esym_end
)
420 struct internal_syment sym
;
423 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
425 if (sym
.n_sclass
== C_EXT
426 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
429 char buf
[SYMNMLEN
+ 1];
434 /* This symbol is externally visible. */
436 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
440 /* We must copy the name into memory if we got it from the
441 syment itself, rather than the string table. */
443 if (sym
._n
._n_n
._n_zeroes
!= 0
444 || sym
._n
._n_n
._n_offset
== 0)
449 if (sym
.n_scnum
== 0)
454 section
= bfd_und_section_ptr
;
459 section
= bfd_com_section_ptr
;
464 flags
= BSF_EXPORT
| BSF_GLOBAL
;
465 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
466 value
-= section
->vma
;
469 if (! (_bfd_generic_link_add_one_symbol
470 (info
, abfd
, name
, flags
, section
, value
,
471 (const char *) NULL
, copy
, false,
472 (struct bfd_link_hash_entry
**) sym_hash
)))
475 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
477 if (((*sym_hash
)->class == C_NULL
478 && (*sym_hash
)->type
== T_NULL
)
481 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
483 (*sym_hash
)->class = sym
.n_sclass
;
484 (*sym_hash
)->type
= sym
.n_type
;
485 (*sym_hash
)->numaux
= sym
.n_numaux
;
486 (*sym_hash
)->auxbfd
= abfd
;
487 if (sym
.n_numaux
!= 0)
489 union internal_auxent
*alloc
;
492 union internal_auxent
*iaux
;
494 alloc
= ((union internal_auxent
*)
495 bfd_hash_allocate (&info
->hash
->table
,
497 * sizeof (*alloc
))));
500 bfd_set_error (bfd_error_no_memory
);
503 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
505 i
++, eaux
+= symesz
, iaux
++)
506 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
507 sym
.n_sclass
, i
, sym
.n_numaux
,
509 (*sym_hash
)->aux
= alloc
;
515 esym
+= (sym
.n_numaux
+ 1) * symesz
;
516 sym_hash
+= sym
.n_numaux
+ 1;
522 /* parse out a -heap <reserved>,<commit> line */
525 dores_com (ptr
, res
, com
)
527 bfd_link_pe_info_dval
*res
;
528 bfd_link_pe_info_dval
*com
;
531 res
->value
= strtoul (ptr
, &ptr
, 0);
534 com
->value
= strtoul (ptr
+1, &ptr
, 0);
540 static char *get_name(ptr
, dst
)
547 while (*ptr
&& *ptr
!= ' ')
552 /* Process any magic embedded commands in a section called .drectve */
555 process_embedded_commands (info
, abfd
)
556 struct bfd_link_info
*info
;
559 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
566 copy
= malloc ((size_t) sec
->_raw_size
);
569 bfd_set_error (bfd_error_no_memory
);
572 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
577 e
= copy
+ sec
->_raw_size
;
578 for (s
= copy
; s
< e
; )
584 if (strncmp (s
,"-attr", 5) == 0)
596 s
= get_name(s
, &name
);
597 s
= get_name(s
, &attribs
);
617 asec
= bfd_get_section_by_name (abfd
, name
);
620 asec
->flags
|= SEC_CODE
;
622 asec
->flags
|= SEC_READONLY
;
625 else if (strncmp (s
,"-heap", 5) == 0)
628 &info
->pe_info
->heap_reserve
,
629 &info
->pe_info
->heap_commit
);
631 else if (strncmp (s
,"-stack", 6) == 0)
634 &info
->pe_info
->stack_reserve
,
635 &info
->pe_info
->stack_commit
);
644 /* Do the final link step. */
647 _bfd_coff_final_link (abfd
, info
)
649 struct bfd_link_info
*info
;
651 bfd_size_type symesz
;
652 struct coff_final_link_info finfo
;
654 struct bfd_link_order
*p
;
655 size_t max_contents_size
;
656 size_t max_sym_count
;
657 size_t max_lineno_count
;
658 size_t max_reloc_count
;
659 size_t max_output_reloc_count
;
660 file_ptr rel_filepos
;
662 file_ptr line_filepos
;
665 bfd_byte
*external_relocs
= NULL
;
666 char strbuf
[STRING_SIZE_SIZE
];
668 symesz
= bfd_coff_symesz (abfd
);
671 finfo
.output_bfd
= abfd
;
673 finfo
.section_info
= NULL
;
674 finfo
.last_file_index
= -1;
675 finfo
.internal_syms
= NULL
;
676 finfo
.sec_ptrs
= NULL
;
677 finfo
.sym_indices
= NULL
;
678 finfo
.outsyms
= NULL
;
679 finfo
.linenos
= NULL
;
680 finfo
.contents
= NULL
;
681 finfo
.external_relocs
= NULL
;
682 finfo
.internal_relocs
= NULL
;
684 coff_data (abfd
)->link_info
= info
;
686 finfo
.strtab
= _bfd_stringtab_init ();
687 if (finfo
.strtab
== NULL
)
690 /* Compute the file positions for all the sections. */
691 if (! abfd
->output_has_begun
)
692 bfd_coff_compute_section_file_positions (abfd
);
694 /* Count the line numbers and relocation entries required for the
695 output file. Set the file positions for the relocs. */
696 rel_filepos
= obj_relocbase (abfd
);
697 relsz
= bfd_coff_relsz (abfd
);
698 max_contents_size
= 0;
699 max_lineno_count
= 0;
701 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
705 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
707 if (p
->type
== bfd_indirect_link_order
)
711 sec
= p
->u
.indirect
.section
;
713 if (info
->strip
== strip_none
714 || info
->strip
== strip_some
)
715 o
->lineno_count
+= sec
->lineno_count
;
717 if (info
->relocateable
)
718 o
->reloc_count
+= sec
->reloc_count
;
720 if (sec
->_raw_size
> max_contents_size
)
721 max_contents_size
= sec
->_raw_size
;
722 if (sec
->lineno_count
> max_lineno_count
)
723 max_lineno_count
= sec
->lineno_count
;
724 if (sec
->reloc_count
> max_reloc_count
)
725 max_reloc_count
= sec
->reloc_count
;
727 else if (info
->relocateable
728 && (p
->type
== bfd_section_reloc_link_order
729 || p
->type
== bfd_symbol_reloc_link_order
))
732 if (o
->reloc_count
== 0)
736 o
->flags
|= SEC_RELOC
;
737 o
->rel_filepos
= rel_filepos
;
738 rel_filepos
+= o
->reloc_count
* relsz
;
742 /* If doing a relocateable link, allocate space for the pointers we
744 if (info
->relocateable
)
748 /* We use section_count + 1, rather than section_count, because
749 the target_index fields are 1 based. */
750 finfo
.section_info
= ((struct coff_link_section_info
*)
751 malloc ((abfd
->section_count
+ 1)
752 * sizeof (struct coff_link_section_info
)));
753 if (finfo
.section_info
== NULL
)
755 bfd_set_error (bfd_error_no_memory
);
758 for (i
= 0; i
<= abfd
->section_count
; i
++)
760 finfo
.section_info
[i
].relocs
= NULL
;
761 finfo
.section_info
[i
].rel_hashes
= NULL
;
765 /* We now know the size of the relocs, so we can determine the file
766 positions of the line numbers. */
767 line_filepos
= rel_filepos
;
768 linesz
= bfd_coff_linesz (abfd
);
769 max_output_reloc_count
= 0;
770 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
772 if (o
->lineno_count
== 0)
776 o
->line_filepos
= line_filepos
;
777 line_filepos
+= o
->lineno_count
* linesz
;
780 if (o
->reloc_count
!= 0)
782 /* We don't know the indices of global symbols until we have
783 written out all the local symbols. For each section in
784 the output file, we keep an array of pointers to hash
785 table entries. Each entry in the array corresponds to a
786 reloc. When we find a reloc against a global symbol, we
787 set the corresponding entry in this array so that we can
788 fix up the symbol index after we have written out all the
791 Because of this problem, we also keep the relocs in
792 memory until the end of the link. This wastes memory,
793 but only when doing a relocateable link, which is not the
795 BFD_ASSERT (info
->relocateable
);
796 finfo
.section_info
[o
->target_index
].relocs
=
797 ((struct internal_reloc
*)
798 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
799 finfo
.section_info
[o
->target_index
].rel_hashes
=
800 ((struct coff_link_hash_entry
**)
801 malloc (o
->reloc_count
802 * sizeof (struct coff_link_hash_entry
*)));
803 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
804 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
806 bfd_set_error (bfd_error_no_memory
);
810 if (o
->reloc_count
> max_output_reloc_count
)
811 max_output_reloc_count
= o
->reloc_count
;
814 /* Reset the reloc and lineno counts, so that we can use them to
815 count the number of entries we have output so far. */
820 obj_sym_filepos (abfd
) = line_filepos
;
822 /* Figure out the largest number of symbols in an input BFD. Take
823 the opportunity to clear the output_has_begun fields of all the
826 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
830 sub
->output_has_begun
= false;
831 sz
= obj_raw_syment_count (sub
);
832 if (sz
> max_sym_count
)
836 /* Allocate some buffers used while linking. */
837 finfo
.internal_syms
= ((struct internal_syment
*)
838 malloc (max_sym_count
839 * sizeof (struct internal_syment
)));
840 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
841 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
842 finfo
.outsyms
= ((bfd_byte
*)
843 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
844 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
845 * bfd_coff_linesz (abfd
));
846 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
847 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
848 if (! info
->relocateable
)
849 finfo
.internal_relocs
= ((struct internal_reloc
*)
850 malloc (max_reloc_count
851 * sizeof (struct internal_reloc
)));
852 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
853 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
854 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
855 || finfo
.outsyms
== NULL
856 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
857 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
858 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
859 || (! info
->relocateable
860 && finfo
.internal_relocs
== NULL
861 && max_reloc_count
> 0))
863 bfd_set_error (bfd_error_no_memory
);
867 /* We now know the position of everything in the file, except that
868 we don't know the size of the symbol table and therefore we don't
869 know where the string table starts. We just build the string
870 table in memory as we go along. We process all the relocations
871 for a single input file at once. */
872 obj_raw_syment_count (abfd
) = 0;
873 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
875 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
877 if (p
->type
== bfd_indirect_link_order
878 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
879 == bfd_target_coff_flavour
))
881 sub
= p
->u
.indirect
.section
->owner
;
882 if (! sub
->output_has_begun
)
884 if (! coff_link_input_bfd (&finfo
, sub
))
886 sub
->output_has_begun
= true;
889 else if (p
->type
== bfd_section_reloc_link_order
890 || p
->type
== bfd_symbol_reloc_link_order
)
892 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
897 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
903 /* Free up the buffers used by coff_link_input_bfd. */
904 if (finfo
.internal_syms
!= NULL
)
906 free (finfo
.internal_syms
);
907 finfo
.internal_syms
= NULL
;
909 if (finfo
.sec_ptrs
!= NULL
)
911 free (finfo
.sec_ptrs
);
912 finfo
.sec_ptrs
= NULL
;
914 if (finfo
.sym_indices
!= NULL
)
916 free (finfo
.sym_indices
);
917 finfo
.sym_indices
= NULL
;
919 if (finfo
.linenos
!= NULL
)
921 free (finfo
.linenos
);
922 finfo
.linenos
= NULL
;
924 if (finfo
.contents
!= NULL
)
926 free (finfo
.contents
);
927 finfo
.contents
= NULL
;
929 if (finfo
.external_relocs
!= NULL
)
931 free (finfo
.external_relocs
);
932 finfo
.external_relocs
= NULL
;
934 if (finfo
.internal_relocs
!= NULL
)
936 free (finfo
.internal_relocs
);
937 finfo
.internal_relocs
= NULL
;
940 /* The value of the last C_FILE symbol is supposed to be the symbol
941 index of the first external symbol. Write it out again if
943 if (finfo
.last_file_index
!= -1
944 && finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
946 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
947 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
948 (PTR
) finfo
.outsyms
);
950 (obj_sym_filepos (abfd
)
951 + finfo
.last_file_index
* symesz
),
953 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
957 /* Write out the global symbols. */
958 finfo
.failed
= false;
959 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
964 /* The outsyms buffer is used by coff_write_global_sym. */
965 if (finfo
.outsyms
!= NULL
)
967 free (finfo
.outsyms
);
968 finfo
.outsyms
= NULL
;
971 if (info
->relocateable
)
973 /* Now that we have written out all the global symbols, we know
974 the symbol indices to use for relocs against them, and we can
975 finally write out the relocs. */
976 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
977 if (external_relocs
== NULL
)
979 bfd_set_error (bfd_error_no_memory
);
983 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
985 struct internal_reloc
*irel
;
986 struct internal_reloc
*irelend
;
987 struct coff_link_hash_entry
**rel_hash
;
990 if (o
->reloc_count
== 0)
993 irel
= finfo
.section_info
[o
->target_index
].relocs
;
994 irelend
= irel
+ o
->reloc_count
;
995 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
996 erel
= external_relocs
;
997 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
999 if (*rel_hash
!= NULL
)
1001 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1002 irel
->r_symndx
= (*rel_hash
)->indx
;
1004 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1007 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1008 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
1009 abfd
) != relsz
* o
->reloc_count
)
1013 free (external_relocs
);
1014 external_relocs
= NULL
;
1017 /* Free up the section information. */
1018 if (finfo
.section_info
!= NULL
)
1022 for (i
= 0; i
< abfd
->section_count
; i
++)
1024 if (finfo
.section_info
[i
].relocs
!= NULL
)
1025 free (finfo
.section_info
[i
].relocs
);
1026 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1027 free (finfo
.section_info
[i
].rel_hashes
);
1029 free (finfo
.section_info
);
1030 finfo
.section_info
= NULL
;
1033 /* Write out the string table. */
1035 (obj_sym_filepos (abfd
)
1036 + obj_raw_syment_count (abfd
) * symesz
),
1040 #if STRING_SIZE_SIZE == 4
1042 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1043 (bfd_byte
*) strbuf
);
1045 #error Change bfd_h_put_32
1048 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1051 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1054 _bfd_stringtab_free (finfo
.strtab
);
1056 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1057 not try to write out the symbols. */
1058 bfd_get_symcount (abfd
) = 0;
1063 if (finfo
.strtab
!= NULL
)
1064 _bfd_stringtab_free (finfo
.strtab
);
1065 if (finfo
.section_info
!= NULL
)
1069 for (i
= 0; i
< abfd
->section_count
; i
++)
1071 if (finfo
.section_info
[i
].relocs
!= NULL
)
1072 free (finfo
.section_info
[i
].relocs
);
1073 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1074 free (finfo
.section_info
[i
].rel_hashes
);
1076 free (finfo
.section_info
);
1078 if (finfo
.internal_syms
!= NULL
)
1079 free (finfo
.internal_syms
);
1080 if (finfo
.sec_ptrs
!= NULL
)
1081 free (finfo
.sec_ptrs
);
1082 if (finfo
.sym_indices
!= NULL
)
1083 free (finfo
.sym_indices
);
1084 if (finfo
.outsyms
!= NULL
)
1085 free (finfo
.outsyms
);
1086 if (finfo
.linenos
!= NULL
)
1087 free (finfo
.linenos
);
1088 if (finfo
.contents
!= NULL
)
1089 free (finfo
.contents
);
1090 if (finfo
.external_relocs
!= NULL
)
1091 free (finfo
.external_relocs
);
1092 if (finfo
.internal_relocs
!= NULL
)
1093 free (finfo
.internal_relocs
);
1094 if (external_relocs
!= NULL
)
1095 free (external_relocs
);
1099 /* Read in and swap the relocs. This returns a buffer holding the
1100 relocs for section SEC in file ABFD. If CACHE is true and
1101 INTERNAL_RELOCS is NULL, the relocs read in wil be saved in case
1102 the function is called again. If EXTERNAL_RELOCS is not NULL, it
1103 is a buffer large enough to hold the unswapped relocs. If
1104 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
1105 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
1106 value must be INTERNAL_RELOCS. The function returns NULL on error. */
1108 struct internal_reloc
*
1109 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1110 require_internal
, internal_relocs
)
1114 bfd_byte
*external_relocs
;
1115 boolean require_internal
;
1116 struct internal_reloc
*internal_relocs
;
1118 bfd_size_type relsz
;
1119 bfd_byte
*free_external
= NULL
;
1120 struct internal_reloc
*free_internal
= NULL
;
1123 struct internal_reloc
*irel
;
1125 if (coff_section_data (abfd
, sec
) != NULL
1126 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
1128 if (! require_internal
)
1129 return coff_section_data (abfd
, sec
)->relocs
;
1130 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
1131 sec
->reloc_count
* sizeof (struct internal_reloc
));
1132 return internal_relocs
;
1135 relsz
= bfd_coff_relsz (abfd
);
1137 if (external_relocs
== NULL
)
1139 free_external
= (bfd_byte
*) malloc (sec
->reloc_count
* relsz
);
1140 if (free_external
== NULL
&& sec
->reloc_count
> 0)
1142 bfd_set_error (bfd_error_no_memory
);
1145 external_relocs
= free_external
;
1148 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
1149 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
1150 != relsz
* sec
->reloc_count
))
1153 if (internal_relocs
== NULL
)
1155 free_internal
= ((struct internal_reloc
*)
1156 malloc (sec
->reloc_count
1157 * sizeof (struct internal_reloc
)));
1158 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
1160 bfd_set_error (bfd_error_no_memory
);
1163 internal_relocs
= free_internal
;
1166 /* Swap in the relocs. */
1167 erel
= external_relocs
;
1168 erel_end
= erel
+ relsz
* sec
->reloc_count
;
1169 irel
= internal_relocs
;
1170 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
1171 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
1173 if (free_external
!= NULL
)
1175 free (free_external
);
1176 free_external
= NULL
;
1179 if (cache
&& free_internal
!= NULL
)
1181 if (coff_section_data (abfd
, sec
) == NULL
)
1183 sec
->used_by_bfd
= ((PTR
) bfd_zalloc (abfd
,
1184 sizeof (struct coff_section_tdata
)));
1185 if (sec
->used_by_bfd
== NULL
)
1187 bfd_set_error (bfd_error_no_memory
);
1190 coff_section_data (abfd
, sec
)->contents
= NULL
;
1192 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
1195 return internal_relocs
;
1198 if (free_external
!= NULL
)
1199 free (free_external
);
1200 if (free_internal
!= NULL
)
1201 free (free_internal
);
1205 /* Link an input file into the linker output file. This function
1206 handles all the sections and relocations of the input file at once. */
1209 coff_link_input_bfd (finfo
, input_bfd
)
1210 struct coff_final_link_info
*finfo
;
1213 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1214 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1215 asection
*, struct internal_reloc
*,
1218 const char *strings
;
1219 bfd_size_type syment_base
;
1220 unsigned int n_tmask
;
1221 unsigned int n_btshft
;
1223 bfd_size_type isymesz
;
1224 bfd_size_type osymesz
;
1225 bfd_size_type linesz
;
1228 struct internal_syment
*isymp
;
1233 struct coff_link_hash_entry
**sym_hash
;
1236 /* Move all the symbols to the output file. */
1238 output_bfd
= finfo
->output_bfd
;
1239 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1241 syment_base
= obj_raw_syment_count (output_bfd
);
1242 isymesz
= bfd_coff_symesz (input_bfd
);
1243 osymesz
= bfd_coff_symesz (output_bfd
);
1244 linesz
= bfd_coff_linesz (input_bfd
);
1245 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1247 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1248 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1250 /* Define macros so that ISFCN, et. al., macros work correctly. */
1251 #define N_TMASK n_tmask
1252 #define N_BTSHFT n_btshft
1255 if (! finfo
->info
->keep_memory
)
1258 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1261 if (! _bfd_coff_get_external_symbols (input_bfd
))
1264 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1265 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1266 isymp
= finfo
->internal_syms
;
1267 secpp
= finfo
->sec_ptrs
;
1268 indexp
= finfo
->sym_indices
;
1269 output_index
= syment_base
;
1270 outsym
= finfo
->outsyms
;
1272 if (obj_pe (output_bfd
))
1274 if (!process_embedded_commands (finfo
->info
, input_bfd
))
1278 while (esym
< esym_end
)
1280 struct internal_syment isym
;
1285 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1287 /* Make a copy of *isymp so that the relocate_section function
1288 always sees the original values. This is more reliable than
1289 always recomputing the symbol value even if we are stripping
1293 if (isym
.n_scnum
!= 0)
1294 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1297 if (isym
.n_value
== 0)
1298 *secpp
= bfd_und_section_ptr
;
1300 *secpp
= bfd_com_section_ptr
;
1307 add
= 1 + isym
.n_numaux
;
1309 /* If we are stripping all symbols, we want to skip this one. */
1310 if (finfo
->info
->strip
== strip_all
)
1315 if (isym
.n_sclass
== C_EXT
1316 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1318 /* This is a global symbol. Global symbols come at the
1319 end of the symbol table, so skip them for now.
1320 Function symbols, however, are an exception, and are
1321 not moved to the end. */
1323 if (! ISFCN (isym
.n_type
))
1328 /* This is a local symbol. Skip it if we are discarding
1330 if (finfo
->info
->discard
== discard_all
)
1335 /* If we stripping debugging symbols, and this is a debugging
1336 symbol, then skip it. */
1338 && finfo
->info
->strip
== strip_debugger
1339 && isym
.n_scnum
== N_DEBUG
)
1342 /* If some symbols are stripped based on the name, work out the
1343 name and decide whether to skip this symbol. */
1345 && (finfo
->info
->strip
== strip_some
1346 || finfo
->info
->discard
== discard_l
))
1349 char buf
[SYMNMLEN
+ 1];
1351 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1355 if ((finfo
->info
->strip
== strip_some
1356 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1359 && finfo
->info
->discard
== discard_l
1360 && strncmp (name
, finfo
->info
->lprefix
,
1361 finfo
->info
->lprefix_len
) == 0))
1365 /* We now know whether we are to skip this symbol or not. */
1368 /* Adjust the symbol in order to output it. */
1370 if (isym
._n
._n_n
._n_zeroes
== 0
1371 && isym
._n
._n_n
._n_offset
!= 0)
1376 /* This symbol has a long name. Enter it in the string
1377 table we are building. Note that we do not check
1378 bfd_coff_symname_in_debug. That is only true for
1379 XCOFF, and XCOFF requires different linking code
1381 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1385 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1386 if (indx
== (bfd_size_type
) -1)
1388 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1391 if (isym
.n_scnum
> 0)
1393 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1394 isym
.n_value
+= ((*secpp
)->output_section
->vma
1395 + (*secpp
)->output_offset
1399 /* The value of a C_FILE symbol is the symbol index of the
1400 next C_FILE symbol. The value of the last C_FILE symbol
1401 is the symbol index to the first external symbol
1402 (actually, coff_renumber_symbols does not get this
1403 right--it just sets the value of the last C_FILE symbol
1404 to zero--and nobody has ever complained about it). We
1405 try to get this right, below, just before we write the
1406 symbols out, but in the general case we may have to write
1407 the symbol out twice. */
1408 if (isym
.n_sclass
== C_FILE
)
1410 if (finfo
->last_file_index
!= -1
1411 && finfo
->last_file
.n_value
!= output_index
)
1413 /* We must correct the value of the last C_FILE entry. */
1414 finfo
->last_file
.n_value
= output_index
;
1415 if (finfo
->last_file_index
>= syment_base
)
1417 /* The last C_FILE symbol is in this input file. */
1418 bfd_coff_swap_sym_out (output_bfd
,
1419 (PTR
) &finfo
->last_file
,
1420 (PTR
) (finfo
->outsyms
1421 + ((finfo
->last_file_index
1427 /* We have already written out the last C_FILE
1428 symbol. We need to write it out again. We
1429 borrow *outsym temporarily. */
1430 bfd_coff_swap_sym_out (output_bfd
,
1431 (PTR
) &finfo
->last_file
,
1433 if (bfd_seek (output_bfd
,
1434 (obj_sym_filepos (output_bfd
)
1435 + finfo
->last_file_index
* osymesz
),
1437 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1443 finfo
->last_file_index
= output_index
;
1444 finfo
->last_file
= isym
;
1447 /* Output the symbol. */
1449 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1451 *indexp
= output_index
;
1456 struct coff_link_hash_entry
*h
;
1458 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1460 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1461 BFD_ASSERT (h
!= NULL
);
1462 h
->indx
= output_index
;
1465 output_index
+= add
;
1466 outsym
+= add
* osymesz
;
1469 esym
+= add
* isymesz
;
1473 for (--add
; add
> 0; --add
)
1480 /* Fix up the aux entries. This must be done in a separate pass,
1481 because we don't know the correct symbol indices until we have
1482 already decided which symbols we are going to keep. */
1484 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1485 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1486 isymp
= finfo
->internal_syms
;
1487 indexp
= finfo
->sym_indices
;
1488 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1489 outsym
= finfo
->outsyms
;
1490 while (esym
< esym_end
)
1494 add
= 1 + isymp
->n_numaux
;
1497 && (*sym_hash
== NULL
1498 || (*sym_hash
)->auxbfd
!= input_bfd
))
1499 esym
+= add
* isymesz
;
1502 struct coff_link_hash_entry
*h
;
1509 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1517 /* Handle the aux entries. This handling is based on
1518 coff_pointerize_aux. I don't know if it always correct. */
1519 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1521 union internal_auxent aux
;
1522 union internal_auxent
*auxp
;
1528 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1529 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1534 if (isymp
->n_sclass
== C_FILE
)
1536 /* If this is a long filename, we must put it in the
1538 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1539 && auxp
->x_file
.x_n
.x_offset
!= 0)
1541 const char *filename
;
1544 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1545 >= STRING_SIZE_SIZE
);
1546 if (strings
== NULL
)
1548 strings
= _bfd_coff_read_string_table (input_bfd
);
1549 if (strings
== NULL
)
1552 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1553 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1555 if (indx
== (bfd_size_type
) -1)
1557 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1560 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1564 if (ISFCN (isymp
->n_type
)
1565 || ISTAG (isymp
->n_sclass
)
1566 || isymp
->n_sclass
== C_BLOCK
)
1568 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1570 && indx
< obj_raw_syment_count (input_bfd
))
1572 /* We look forward through the symbol for
1573 the index of the next symbol we are going
1574 to include. I don't know if this is
1576 while (finfo
->sym_indices
[indx
] < 0
1577 && indx
< obj_raw_syment_count (input_bfd
))
1579 if (indx
>= obj_raw_syment_count (input_bfd
))
1580 indx
= output_index
;
1582 indx
= finfo
->sym_indices
[indx
];
1583 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1587 indx
= auxp
->x_sym
.x_tagndx
.l
;
1588 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1590 indx
= finfo
->sym_indices
[indx
];
1592 auxp
->x_sym
.x_tagndx
.l
= 0;
1594 auxp
->x_sym
.x_tagndx
.l
= indx
;
1600 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1601 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1615 /* Relocate the line numbers, unless we are stripping them. */
1616 if (finfo
->info
->strip
== strip_none
1617 || finfo
->info
->strip
== strip_some
)
1619 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1625 /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
1626 build_link_order in ldwrite.c will not have created a
1627 link order, which means that we will not have seen this
1628 input section in _bfd_coff_final_link, which means that
1629 we will not have allocated space for the line numbers of
1630 this section. I don't think line numbers can be
1631 meaningful for a section which does not have
1632 SEC_HAS_CONTENTS set, but, if they do, this must be
1634 if (o
->lineno_count
== 0
1635 || (o
->output_section
->flags
& SEC_HAS_CONTENTS
) == 0)
1638 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1639 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1640 input_bfd
) != linesz
* o
->lineno_count
)
1643 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1644 eline
= finfo
->linenos
;
1645 elineend
= eline
+ linesz
* o
->lineno_count
;
1646 for (; eline
< elineend
; eline
+= linesz
)
1648 struct internal_lineno iline
;
1650 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1652 if (iline
.l_lnno
!= 0)
1653 iline
.l_addr
.l_paddr
+= offset
;
1654 else if (iline
.l_addr
.l_symndx
>= 0
1655 && (iline
.l_addr
.l_symndx
1656 < obj_raw_syment_count (input_bfd
)))
1660 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1664 /* These line numbers are attached to a symbol
1665 which we are stripping. We should really
1666 just discard the line numbers, but that would
1667 be a pain because we have already counted
1673 struct internal_syment is
;
1674 union internal_auxent ia
;
1676 /* Fix up the lnnoptr field in the aux entry of
1677 the symbol. It turns out that we can't do
1678 this when we modify the symbol aux entries,
1679 because gas sometimes screws up the lnnoptr
1680 field and makes it an offset from the start
1681 of the line numbers rather than an absolute
1683 bfd_coff_swap_sym_in (output_bfd
,
1684 (PTR
) (finfo
->outsyms
1685 + ((indx
- syment_base
)
1688 if ((ISFCN (is
.n_type
)
1689 || is
.n_sclass
== C_BLOCK
)
1690 && is
.n_numaux
>= 1)
1694 auxptr
= (PTR
) (finfo
->outsyms
1695 + ((indx
- syment_base
+ 1)
1697 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1698 is
.n_type
, is
.n_sclass
,
1699 0, is
.n_numaux
, (PTR
) &ia
);
1700 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1701 (o
->output_section
->line_filepos
1702 + o
->output_section
->lineno_count
* linesz
1703 + eline
- finfo
->linenos
);
1704 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1705 is
.n_type
, is
.n_sclass
, 0,
1706 is
.n_numaux
, auxptr
);
1710 iline
.l_addr
.l_symndx
= indx
;
1713 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1716 if (bfd_seek (output_bfd
,
1717 (o
->output_section
->line_filepos
1718 + o
->output_section
->lineno_count
* linesz
),
1720 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1721 output_bfd
) != linesz
* o
->lineno_count
)
1724 o
->output_section
->lineno_count
+= o
->lineno_count
;
1728 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1729 symbol will be the first symbol in the next input file. In the
1730 normal case, this will save us from writing out the C_FILE symbol
1732 if (finfo
->last_file_index
!= -1
1733 && finfo
->last_file_index
>= syment_base
)
1735 finfo
->last_file
.n_value
= output_index
;
1736 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1737 (PTR
) (finfo
->outsyms
1738 + ((finfo
->last_file_index
- syment_base
)
1742 /* Write the modified symbols to the output file. */
1743 if (outsym
> finfo
->outsyms
)
1745 if (bfd_seek (output_bfd
,
1746 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1748 || bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1749 output_bfd
) != outsym
- finfo
->outsyms
)
1752 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1753 + (outsym
- finfo
->outsyms
) / osymesz
)
1756 obj_raw_syment_count (output_bfd
) = output_index
;
1759 /* Relocate the contents of each section. */
1760 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1761 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1765 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1768 if (coff_section_data (input_bfd
, o
) != NULL
1769 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1770 contents
= coff_section_data (input_bfd
, o
)->contents
;
1773 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1774 (file_ptr
) 0, o
->_raw_size
))
1776 contents
= finfo
->contents
;
1779 if ((o
->flags
& SEC_RELOC
) != 0)
1782 struct internal_reloc
*internal_relocs
;
1783 struct internal_reloc
*irel
;
1785 /* Read in the relocs. */
1786 target_index
= o
->output_section
->target_index
;
1787 internal_relocs
= (_bfd_coff_read_internal_relocs
1788 (input_bfd
, o
, false, finfo
->external_relocs
,
1789 finfo
->info
->relocateable
,
1790 (finfo
->info
->relocateable
1791 ? (finfo
->section_info
[target_index
].relocs
1792 + o
->output_section
->reloc_count
)
1793 : finfo
->internal_relocs
)));
1794 if (internal_relocs
== NULL
)
1797 /* Call processor specific code to relocate the section
1799 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1803 finfo
->internal_syms
,
1807 if (finfo
->info
->relocateable
)
1810 struct internal_reloc
*irelend
;
1811 struct coff_link_hash_entry
**rel_hash
;
1813 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1814 irel
= internal_relocs
;
1815 irelend
= irel
+ o
->reloc_count
;
1816 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1817 + o
->output_section
->reloc_count
);
1818 for (; irel
< irelend
; irel
++, rel_hash
++)
1820 struct coff_link_hash_entry
*h
;
1825 /* Adjust the reloc address and symbol index. */
1827 irel
->r_vaddr
+= offset
;
1829 if (irel
->r_symndx
== -1)
1834 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1842 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1845 /* This is a global symbol. */
1847 irel
->r_symndx
= h
->indx
;
1850 /* This symbol is being written at the end
1851 of the file, and we do not yet know the
1852 symbol index. We save the pointer to the
1853 hash table entry in the rel_hash list.
1854 We set the indx field to -2 to indicate
1855 that this symbol must not be stripped. */
1864 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1866 irel
->r_symndx
= indx
;
1869 struct internal_syment
*is
;
1871 char buf
[SYMNMLEN
+ 1];
1873 /* This reloc is against a symbol we are
1874 stripping. It would be possible to
1875 handle this case, but I don't think it's
1877 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1879 name
= (_bfd_coff_internal_syment_name
1880 (input_bfd
, is
, buf
));
1884 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1885 (finfo
->info
, name
, input_bfd
, o
,
1892 o
->output_section
->reloc_count
+= o
->reloc_count
;
1896 /* Write out the modified section contents. */
1897 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1898 contents
, o
->output_offset
,
1899 (o
->_cooked_size
!= 0
1905 if (! finfo
->info
->keep_memory
)
1907 if (! _bfd_coff_free_symbols (input_bfd
))
1914 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1917 coff_write_global_sym (h
, data
)
1918 struct coff_link_hash_entry
*h
;
1921 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1923 struct internal_syment isym
;
1924 bfd_size_type symesz
;
1927 output_bfd
= finfo
->output_bfd
;
1933 && (finfo
->info
->strip
== strip_all
1934 || (finfo
->info
->strip
== strip_some
1935 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1936 h
->root
.root
.string
, false, false)
1940 switch (h
->root
.type
)
1943 case bfd_link_hash_new
:
1947 case bfd_link_hash_undefined
:
1948 case bfd_link_hash_undefweak
:
1949 isym
.n_scnum
= N_UNDEF
;
1953 case bfd_link_hash_defined
:
1954 case bfd_link_hash_defweak
:
1958 sec
= h
->root
.u
.def
.section
->output_section
;
1959 if (bfd_is_abs_section (sec
))
1960 isym
.n_scnum
= N_ABS
;
1962 isym
.n_scnum
= sec
->target_index
;
1963 isym
.n_value
= (h
->root
.u
.def
.value
1965 + h
->root
.u
.def
.section
->output_offset
);
1969 case bfd_link_hash_common
:
1970 isym
.n_scnum
= N_UNDEF
;
1971 isym
.n_value
= h
->root
.u
.c
.size
;
1974 case bfd_link_hash_indirect
:
1975 case bfd_link_hash_warning
:
1976 /* Just ignore these. They can't be handled anyhow. */
1980 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1981 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1988 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1990 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1992 if (indx
== (bfd_size_type
) -1)
1994 finfo
->failed
= true;
1997 isym
._n
._n_n
._n_zeroes
= 0;
1998 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
2001 isym
.n_sclass
= h
->class;
2002 isym
.n_type
= h
->type
;
2004 if (isym
.n_sclass
== C_NULL
)
2005 isym
.n_sclass
= C_EXT
;
2007 isym
.n_numaux
= h
->numaux
;
2009 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2011 symesz
= bfd_coff_symesz (output_bfd
);
2013 if (bfd_seek (output_bfd
,
2014 (obj_sym_filepos (output_bfd
)
2015 + obj_raw_syment_count (output_bfd
) * symesz
),
2017 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2019 finfo
->failed
= true;
2023 h
->indx
= obj_raw_syment_count (output_bfd
);
2025 ++obj_raw_syment_count (output_bfd
);
2027 /* Write out any associated aux entries. There normally will be
2028 none. If there are any, I have no idea how to modify them. */
2029 for (i
= 0; i
< isym
.n_numaux
; i
++)
2031 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2032 isym
.n_sclass
, i
, isym
.n_numaux
,
2033 (PTR
) finfo
->outsyms
);
2034 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2036 finfo
->failed
= true;
2039 ++obj_raw_syment_count (output_bfd
);
2045 /* Handle a link order which is supposed to generate a reloc. */
2048 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2050 struct coff_final_link_info
*finfo
;
2051 asection
*output_section
;
2052 struct bfd_link_order
*link_order
;
2054 reloc_howto_type
*howto
;
2055 struct internal_reloc
*irel
;
2056 struct coff_link_hash_entry
**rel_hash_ptr
;
2058 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2061 bfd_set_error (bfd_error_bad_value
);
2065 if (link_order
->u
.reloc
.p
->addend
!= 0)
2069 bfd_reloc_status_type rstat
;
2072 size
= bfd_get_reloc_size (howto
);
2073 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2076 bfd_set_error (bfd_error_no_memory
);
2080 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2081 link_order
->u
.reloc
.p
->addend
, buf
);
2087 case bfd_reloc_outofrange
:
2089 case bfd_reloc_overflow
:
2090 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2092 (link_order
->type
== bfd_section_reloc_link_order
2093 ? bfd_section_name (output_bfd
,
2094 link_order
->u
.reloc
.p
->u
.section
)
2095 : link_order
->u
.reloc
.p
->u
.name
),
2096 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2097 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2104 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2105 (file_ptr
) link_order
->offset
, size
);
2111 /* Store the reloc information in the right place. It will get
2112 swapped and written out at the end of the final_link routine. */
2114 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2115 + output_section
->reloc_count
);
2116 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2117 + output_section
->reloc_count
);
2119 memset (irel
, 0, sizeof (struct internal_reloc
));
2120 *rel_hash_ptr
= NULL
;
2122 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2124 if (link_order
->type
== bfd_section_reloc_link_order
)
2126 /* We need to somehow locate a symbol in the right section. The
2127 symbol must either have a value of zero, or we must adjust
2128 the addend by the value of the symbol. FIXME: Write this
2129 when we need it. The old linker couldn't handle this anyhow. */
2131 *rel_hash_ptr
= NULL
;
2136 struct coff_link_hash_entry
*h
;
2138 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2139 link_order
->u
.reloc
.p
->u
.name
,
2140 false, false, true);
2144 irel
->r_symndx
= h
->indx
;
2147 /* Set the index to -2 to force this symbol to get
2156 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2157 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2158 (asection
*) NULL
, (bfd_vma
) 0)))
2164 /* FIXME: Is this always right? */
2165 irel
->r_type
= howto
->type
;
2167 /* r_size is only used on the RS/6000, which needs its own linker
2168 routines anyhow. r_extern is only used for ECOFF. */
2170 /* FIXME: What is the right value for r_offset? Is zero OK? */
2172 ++output_section
->reloc_count
;
2177 /* A basic reloc handling routine which may be used by processors with
2181 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2182 input_section
, contents
, relocs
, syms
,
2185 struct bfd_link_info
*info
;
2187 asection
*input_section
;
2189 struct internal_reloc
*relocs
;
2190 struct internal_syment
*syms
;
2191 asection
**sections
;
2193 struct internal_reloc
*rel
;
2194 struct internal_reloc
*relend
;
2198 relend
= rel
+ input_section
->reloc_count
;
2199 for (; rel
< relend
; rel
++)
2202 struct coff_link_hash_entry
*h
;
2203 struct internal_syment
*sym
;
2206 reloc_howto_type
*howto
;
2207 bfd_reloc_status_type rstat
;
2209 symndx
= rel
->r_symndx
;
2218 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2219 sym
= syms
+ symndx
;
2222 /* COFF treats common symbols in one of two ways. Either the
2223 size of the symbol is included in the section contents, or it
2224 is not. We assume that the size is not included, and force
2225 the rtype_to_howto function to adjust the addend as needed. */
2227 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2228 addend
= - sym
->n_value
;
2233 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2246 sec
= bfd_abs_section_ptr
;
2251 sec
= sections
[symndx
];
2252 val
= (sec
->output_section
->vma
2253 + sec
->output_offset
2260 if (h
->root
.type
== bfd_link_hash_defined
2261 || h
->root
.type
== bfd_link_hash_defweak
)
2265 sec
= h
->root
.u
.def
.section
;
2266 val
= (h
->root
.u
.def
.value
2267 + sec
->output_section
->vma
2268 + sec
->output_offset
);
2271 else if (! info
->relocateable
)
2273 if (! ((*info
->callbacks
->undefined_symbol
)
2274 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2275 rel
->r_vaddr
- input_section
->vma
)))
2280 if (info
->base_file
)
2282 /* So if this is non pcrelative, and is referenced
2283 to a section or a common symbol, then it needs a reloc */
2284 if (!howto
->pc_relative
2288 /* relocation to a symbol in a section which
2289 isn't absolute - we output the address here
2291 bfd_vma addr
= rel
->r_vaddr
2292 + input_section
->output_offset
2293 + input_section
->output_section
->vma
;
2294 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2298 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2300 rel
->r_vaddr
- input_section
->vma
,
2309 case bfd_reloc_overflow
:
2312 char buf
[SYMNMLEN
+ 1];
2317 name
= h
->root
.root
.string
;
2320 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2325 if (! ((*info
->callbacks
->reloc_overflow
)
2326 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2327 input_section
, rel
->r_vaddr
- input_section
->vma
)))