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
;
82 enum bfd_link_subsystem subsystem
;
83 bfd_link_stack_heap stack_heap_parameters
;
86 static struct bfd_hash_entry
*coff_link_hash_newfunc
87 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
88 static boolean coff_link_add_object_symbols
89 PARAMS ((bfd
*, struct bfd_link_info
*));
90 static boolean coff_link_check_archive_element
91 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
92 static INLINE
const char *_bfd_coff_internal_syment_name
93 PARAMS ((bfd
*, const struct internal_syment
*, char *));
94 static boolean coff_link_check_ar_symbols
95 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
96 static boolean coff_link_add_symbols
PARAMS ((bfd
*, struct bfd_link_info
*));
97 static boolean coff_link_input_bfd
98 PARAMS ((struct coff_final_link_info
*, bfd
*));
99 static boolean coff_write_global_sym
100 PARAMS ((struct coff_link_hash_entry
*, PTR
));
101 static boolean coff_reloc_link_order
102 PARAMS ((bfd
*, struct coff_final_link_info
*, asection
*,
103 struct bfd_link_order
*));
106 /* These new data and data types are used to keep track of the .idata$4 and
107 .idata$5 relocations which are put into the .idata section for all of the
108 *.dll input libraries linked in. This is not a great solution, and may
109 break in the future if MS changes the format of its libraries, but it
110 does work for the collection of mstools libraries we are currently working
111 with. The main problem is that there are some new majic symbols defined
112 in the libraries which are non-standard coff and simply aren't handled
113 completely by ld. What has been included here will help finish up the job.
114 Basically, during the link, .idata$4 and .idata$5 pointers are correctly
115 relocated to the image. At the very end of the link, the .idata$2
116 information is written. This data appears at the beginning of the .idata
117 section and a 'set' of information appears for each *.dll passed in.
118 Each set of information consists of 3 addresses, a pointer to the .idata$4
119 start, a pointer to .idata$6 (which has the name of the dll), and a pointer
120 to .idata$5 start. The idata$4 and 5 information is a list of pointers
121 which appear to point to the name of various functions found within the dll.
122 When invoked, the loader will write over these names with the correct
123 addresses to use for these functions.
124 Without this 'fix', all information appears correctly except for the
125 addresses of the .idata$4 and 5 starts within the .idata$2 portion of the
126 .idata section. What we will do is to keep track of the dll's processed
127 and the number of functions needed from each dll. From this information
128 we can correctly compute the start of the idata$4 and 5 lists for each
129 dll in the idata section */
130 static int num_DLLs_done
= 0;
131 static int num_DLLs
= 0;
132 static int all_entries
= 0;
134 const char * DLL_name
;
137 struct DLL_struct MS_DLL
[10];
138 static bfd_vma idata_4_prev
= 0;
139 static bfd_vma idata_5_prev
= 0;
140 static bfd_vma add_to_val
= 0;
144 /* Create an entry in a COFF linker hash table. */
146 static struct bfd_hash_entry
*
147 coff_link_hash_newfunc (entry
, table
, string
)
148 struct bfd_hash_entry
*entry
;
149 struct bfd_hash_table
*table
;
152 struct coff_link_hash_entry
*ret
= (struct coff_link_hash_entry
*) entry
;
154 /* Allocate the structure if it has not already been allocated by a
156 if (ret
== (struct coff_link_hash_entry
*) NULL
)
157 ret
= ((struct coff_link_hash_entry
*)
158 bfd_hash_allocate (table
, sizeof (struct coff_link_hash_entry
)));
159 if (ret
== (struct coff_link_hash_entry
*) NULL
)
161 bfd_set_error (bfd_error_no_memory
);
162 return (struct bfd_hash_entry
*) ret
;
165 /* Call the allocation method of the superclass. */
166 ret
= ((struct coff_link_hash_entry
*)
167 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
169 if (ret
!= (struct coff_link_hash_entry
*) NULL
)
171 /* Set local fields. */
180 return (struct bfd_hash_entry
*) ret
;
183 /* Create a COFF linker hash table. */
185 struct bfd_link_hash_table
*
186 _bfd_coff_link_hash_table_create (abfd
)
189 struct coff_link_hash_table
*ret
;
191 ret
= ((struct coff_link_hash_table
*)
192 bfd_alloc (abfd
, sizeof (struct coff_link_hash_table
)));
195 bfd_set_error (bfd_error_no_memory
);
198 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
199 coff_link_hash_newfunc
))
202 return (struct bfd_link_hash_table
*) NULL
;
207 /* Given a COFF BFD, add symbols to the global hash table as
211 _bfd_coff_link_add_symbols (abfd
, info
)
213 struct bfd_link_info
*info
;
215 switch (bfd_get_format (abfd
))
218 return coff_link_add_object_symbols (abfd
, info
);
220 return (_bfd_generic_link_add_archive_symbols
221 (abfd
, info
, coff_link_check_archive_element
));
223 bfd_set_error (bfd_error_wrong_format
);
228 /* Add symbols from a COFF object file. */
231 coff_link_add_object_symbols (abfd
, info
)
233 struct bfd_link_info
*info
;
235 if (! _bfd_coff_get_external_symbols (abfd
))
237 if (! coff_link_add_symbols (abfd
, info
))
239 if (! info
->keep_memory
)
241 if (! _bfd_coff_free_symbols (abfd
))
247 /* Check a single archive element to see if we need to include it in
248 the link. *PNEEDED is set according to whether this element is
249 needed in the link or not. This is called via
250 _bfd_generic_link_add_archive_symbols. */
253 coff_link_check_archive_element (abfd
, info
, pneeded
)
255 struct bfd_link_info
*info
;
258 if (! _bfd_coff_get_external_symbols (abfd
))
261 if (! coff_link_check_ar_symbols (abfd
, info
, pneeded
))
266 if (! coff_link_add_symbols (abfd
, info
))
270 if (! info
->keep_memory
|| ! *pneeded
)
272 if (! _bfd_coff_free_symbols (abfd
))
279 /* Get the name of a symbol. The caller must pass in a buffer of size
282 static INLINE
const char *
283 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
285 const struct internal_syment
*sym
;
288 /* FIXME: It's not clear this will work correctly if sizeof
290 if (sym
->_n
._n_n
._n_zeroes
!= 0
291 || sym
->_n
._n_n
._n_offset
== 0)
293 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
294 buf
[SYMNMLEN
] = '\0';
301 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
302 strings
= obj_coff_strings (abfd
);
305 strings
= _bfd_coff_read_string_table (abfd
);
309 return strings
+ sym
->_n
._n_n
._n_offset
;
313 /* Look through the symbols to see if this object file should be
314 included in the link. */
317 coff_link_check_ar_symbols (abfd
, info
, pneeded
)
319 struct bfd_link_info
*info
;
322 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
323 bfd_size_type symesz
;
329 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
331 symesz
= bfd_coff_symesz (abfd
);
332 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
333 esym_end
= esym
+ obj_raw_syment_count (abfd
) * symesz
;
334 while (esym
< esym_end
)
336 struct internal_syment sym
;
338 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
340 if ((sym
.n_sclass
== C_EXT
341 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
342 && (sym
.n_scnum
!= 0 || sym
.n_value
!= 0))
345 char buf
[SYMNMLEN
+ 1];
346 struct bfd_link_hash_entry
*h
;
348 /* This symbol is externally visible, and is defined by this
351 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
354 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
356 /* We are only interested in symbols that are currently
357 undefined. If a symbol is currently known to be common,
358 COFF linkers do not bring in an object file which defines
360 if (h
!= (struct bfd_link_hash_entry
*) NULL
361 && h
->type
== bfd_link_hash_undefined
)
363 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
370 esym
+= (sym
.n_numaux
+ 1) * symesz
;
373 /* We do not need this object file. */
377 /* Add all the symbols from an object file to the hash table. */
380 coff_link_add_symbols (abfd
, info
)
382 struct bfd_link_info
*info
;
384 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
385 boolean default_copy
;
386 bfd_size_type symcount
;
387 struct coff_link_hash_entry
**sym_hash
;
388 bfd_size_type symesz
;
392 sym_is_global
= coff_backend_info (abfd
)->_bfd_coff_sym_is_global
;
394 if (info
->keep_memory
)
395 default_copy
= false;
399 symcount
= obj_raw_syment_count (abfd
);
401 /* We keep a list of the linker hash table entries that correspond
402 to particular symbols. */
403 sym_hash
= ((struct coff_link_hash_entry
**)
406 * sizeof (struct coff_link_hash_entry
*))));
407 if (sym_hash
== NULL
&& symcount
!= 0)
409 bfd_set_error (bfd_error_no_memory
);
412 obj_coff_sym_hashes (abfd
) = sym_hash
;
414 (size_t) symcount
* sizeof (struct coff_link_hash_entry
*));
416 symesz
= bfd_coff_symesz (abfd
);
417 BFD_ASSERT (symesz
== bfd_coff_auxesz (abfd
));
418 esym
= (bfd_byte
*) obj_coff_external_syms (abfd
);
419 esym_end
= esym
+ symcount
* symesz
;
420 while (esym
< esym_end
)
422 struct internal_syment sym
;
425 bfd_coff_swap_sym_in (abfd
, (PTR
) esym
, (PTR
) &sym
);
427 if (sym
.n_sclass
== C_EXT
428 || (sym_is_global
&& (*sym_is_global
) (abfd
, &sym
)))
431 char buf
[SYMNMLEN
+ 1];
436 /* This symbol is externally visible. */
438 name
= _bfd_coff_internal_syment_name (abfd
, &sym
, buf
);
442 /* We must copy the name into memory if we got it from the
443 syment itself, rather than the string table. */
445 if (sym
._n
._n_n
._n_zeroes
!= 0
446 || sym
._n
._n_n
._n_offset
== 0)
451 if (sym
.n_scnum
== 0)
456 section
= bfd_und_section_ptr
;
461 section
= bfd_com_section_ptr
;
466 flags
= BSF_EXPORT
| BSF_GLOBAL
;
467 section
= coff_section_from_bfd_index (abfd
, sym
.n_scnum
);
468 value
-= section
->vma
;
471 if (! (_bfd_generic_link_add_one_symbol
472 (info
, abfd
, name
, flags
, section
, value
,
473 (const char *) NULL
, copy
, false,
474 (struct bfd_link_hash_entry
**) sym_hash
)))
477 if (info
->hash
->creator
->flavour
== bfd_get_flavour (abfd
))
479 if (((*sym_hash
)->class == C_NULL
480 && (*sym_hash
)->type
== T_NULL
)
483 && (*sym_hash
)->root
.type
!= bfd_link_hash_defined
))
485 (*sym_hash
)->class = sym
.n_sclass
;
486 (*sym_hash
)->type
= sym
.n_type
;
487 (*sym_hash
)->numaux
= sym
.n_numaux
;
488 (*sym_hash
)->auxbfd
= abfd
;
489 if (sym
.n_numaux
!= 0)
491 union internal_auxent
*alloc
;
494 union internal_auxent
*iaux
;
496 alloc
= ((union internal_auxent
*)
497 bfd_hash_allocate (&info
->hash
->table
,
499 * sizeof (*alloc
))));
502 bfd_set_error (bfd_error_no_memory
);
505 for (i
= 0, eaux
= esym
+ symesz
, iaux
= alloc
;
507 i
++, eaux
+= symesz
, iaux
++)
508 bfd_coff_swap_aux_in (abfd
, (PTR
) eaux
, sym
.n_type
,
509 sym
.n_sclass
, i
, sym
.n_numaux
,
511 (*sym_hash
)->aux
= alloc
;
517 esym
+= (sym
.n_numaux
+ 1) * symesz
;
518 sym_hash
+= sym
.n_numaux
+ 1;
524 /* parse out a -heap <reserved>,<commit> line */
527 dores_com (ptr
, def
,res
, com
)
534 *res
= strtoul (ptr
, &ptr
, 0);
536 *com
= 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 (abfd
)
558 asection
*sec
= bfd_get_section_by_name (abfd
, ".drectve");
565 copy
= malloc ((size_t) sec
->_raw_size
);
568 bfd_set_error (bfd_error_no_memory
);
571 if (! bfd_get_section_contents(abfd
, sec
, copy
, 0, sec
->_raw_size
))
576 e
= copy
+ sec
->_raw_size
;
577 for (s
= copy
; s
< e
; )
583 if (strncmp (s
,"-attr", 5) == 0)
595 s
= get_name(s
, &name
);
596 s
= get_name(s
, &attribs
);
616 asec
= bfd_get_section_by_name (abfd
, name
);
619 asec
->flags
|= SEC_CODE
;
621 asec
->flags
|= SEC_READONLY
;
624 else if (strncmp (s
,"-heap", 5) == 0)
627 &NT_stack_heap
.heap_defined
,
628 &NT_stack_heap
.heap_reserve
,
629 &NT_stack_heap
.heap_commit
);
631 else if (strncmp (s
,"-stack", 6) == 0)
634 &NT_stack_heap
.heap_defined
,
635 &NT_stack_heap
.heap_reserve
,
636 &NT_stack_heap
.heap_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
;
686 /* store the subsystem, stack and heap parameters in variables defined
687 in internal.h so that when they are needed to write the NT optional
688 file header (coffcode.h), they will be available */
689 NT_subsystem
= info
->subsystem
;
690 NT_stack_heap
= info
->stack_heap_parameters
;
693 finfo
.strtab
= _bfd_stringtab_init ();
694 if (finfo
.strtab
== NULL
)
697 /* Compute the file positions for all the sections. */
698 if (! abfd
->output_has_begun
)
699 bfd_coff_compute_section_file_positions (abfd
);
701 /* Count the line numbers and relocation entries required for the
702 output file. Set the file positions for the relocs. */
703 rel_filepos
= obj_relocbase (abfd
);
704 relsz
= bfd_coff_relsz (abfd
);
705 max_contents_size
= 0;
706 max_lineno_count
= 0;
708 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
712 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
714 if (p
->type
== bfd_indirect_link_order
)
718 sec
= p
->u
.indirect
.section
;
720 if (info
->strip
== strip_none
721 || info
->strip
== strip_some
)
722 o
->lineno_count
+= sec
->lineno_count
;
724 if (info
->relocateable
)
725 o
->reloc_count
+= sec
->reloc_count
;
727 if (sec
->_raw_size
> max_contents_size
)
728 max_contents_size
= sec
->_raw_size
;
729 if (sec
->lineno_count
> max_lineno_count
)
730 max_lineno_count
= sec
->lineno_count
;
731 if (sec
->reloc_count
> max_reloc_count
)
732 max_reloc_count
= sec
->reloc_count
;
734 else if (info
->relocateable
735 && (p
->type
== bfd_section_reloc_link_order
736 || p
->type
== bfd_symbol_reloc_link_order
))
739 if (o
->reloc_count
== 0)
743 o
->flags
|= SEC_RELOC
;
744 o
->rel_filepos
= rel_filepos
;
745 rel_filepos
+= o
->reloc_count
* relsz
;
749 /* If doing a relocateable link, allocate space for the pointers we
751 if (info
->relocateable
)
755 /* We use section_count + 1, rather than section_count, because
756 the target_index fields are 1 based. */
757 finfo
.section_info
= ((struct coff_link_section_info
*)
758 malloc ((abfd
->section_count
+ 1)
759 * sizeof (struct coff_link_section_info
)));
760 if (finfo
.section_info
== NULL
)
762 bfd_set_error (bfd_error_no_memory
);
765 for (i
= 0; i
<= abfd
->section_count
; i
++)
767 finfo
.section_info
[i
].relocs
= NULL
;
768 finfo
.section_info
[i
].rel_hashes
= NULL
;
772 /* We now know the size of the relocs, so we can determine the file
773 positions of the line numbers. */
774 line_filepos
= rel_filepos
;
775 linesz
= bfd_coff_linesz (abfd
);
776 max_output_reloc_count
= 0;
777 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
779 if (o
->lineno_count
== 0)
783 o
->line_filepos
= line_filepos
;
784 line_filepos
+= o
->lineno_count
* linesz
;
787 if (o
->reloc_count
!= 0)
789 /* We don't know the indices of global symbols until we have
790 written out all the local symbols. For each section in
791 the output file, we keep an array of pointers to hash
792 table entries. Each entry in the array corresponds to a
793 reloc. When we find a reloc against a global symbol, we
794 set the corresponding entry in this array so that we can
795 fix up the symbol index after we have written out all the
798 Because of this problem, we also keep the relocs in
799 memory until the end of the link. This wastes memory,
800 but only when doing a relocateable link, which is not the
802 BFD_ASSERT (info
->relocateable
);
803 finfo
.section_info
[o
->target_index
].relocs
=
804 ((struct internal_reloc
*)
805 malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
806 finfo
.section_info
[o
->target_index
].rel_hashes
=
807 ((struct coff_link_hash_entry
**)
808 malloc (o
->reloc_count
809 * sizeof (struct coff_link_hash_entry
*)));
810 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
811 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
813 bfd_set_error (bfd_error_no_memory
);
817 if (o
->reloc_count
> max_output_reloc_count
)
818 max_output_reloc_count
= o
->reloc_count
;
821 /* Reset the reloc and lineno counts, so that we can use them to
822 count the number of entries we have output so far. */
827 obj_sym_filepos (abfd
) = line_filepos
;
829 /* Figure out the largest number of symbols in an input BFD. Take
830 the opportunity to clear the output_has_begun fields of all the
833 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
837 sub
->output_has_begun
= false;
838 sz
= obj_raw_syment_count (sub
);
839 if (sz
> max_sym_count
)
843 /* Allocate some buffers used while linking. */
844 finfo
.internal_syms
= ((struct internal_syment
*)
845 malloc (max_sym_count
846 * sizeof (struct internal_syment
)));
847 finfo
.sec_ptrs
= (asection
**) malloc (max_sym_count
* sizeof (asection
*));
848 finfo
.sym_indices
= (long *) malloc (max_sym_count
* sizeof (long));
849 finfo
.outsyms
= ((bfd_byte
*)
850 malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
851 finfo
.linenos
= (bfd_byte
*) malloc (max_lineno_count
852 * bfd_coff_linesz (abfd
));
853 finfo
.contents
= (bfd_byte
*) malloc (max_contents_size
);
854 finfo
.external_relocs
= (bfd_byte
*) malloc (max_reloc_count
* relsz
);
855 if (! info
->relocateable
)
856 finfo
.internal_relocs
= ((struct internal_reloc
*)
857 malloc (max_reloc_count
858 * sizeof (struct internal_reloc
)));
859 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
860 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
861 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
862 || finfo
.outsyms
== NULL
863 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
864 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
865 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
866 || (! info
->relocateable
867 && finfo
.internal_relocs
== NULL
868 && max_reloc_count
> 0))
870 bfd_set_error (bfd_error_no_memory
);
874 /* We now know the position of everything in the file, except that
875 we don't know the size of the symbol table and therefore we don't
876 know where the string table starts. We just build the string
877 table in memory as we go along. We process all the relocations
878 for a single input file at once. */
879 obj_raw_syment_count (abfd
) = 0;
880 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
882 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
884 if (p
->type
== bfd_indirect_link_order
885 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
886 == bfd_target_coff_flavour
))
888 sub
= p
->u
.indirect
.section
->owner
;
889 if (! sub
->output_has_begun
)
891 if (! coff_link_input_bfd (&finfo
, sub
))
893 sub
->output_has_begun
= true;
896 else if (p
->type
== bfd_section_reloc_link_order
897 || p
->type
== bfd_symbol_reloc_link_order
)
899 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
904 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
910 /* Free up the buffers used by coff_link_input_bfd. */
911 if (finfo
.internal_syms
!= NULL
)
913 free (finfo
.internal_syms
);
914 finfo
.internal_syms
= NULL
;
916 if (finfo
.sec_ptrs
!= NULL
)
918 free (finfo
.sec_ptrs
);
919 finfo
.sec_ptrs
= NULL
;
921 if (finfo
.sym_indices
!= NULL
)
923 free (finfo
.sym_indices
);
924 finfo
.sym_indices
= NULL
;
926 if (finfo
.linenos
!= NULL
)
928 free (finfo
.linenos
);
929 finfo
.linenos
= NULL
;
931 if (finfo
.contents
!= NULL
)
933 free (finfo
.contents
);
934 finfo
.contents
= NULL
;
936 if (finfo
.external_relocs
!= NULL
)
938 free (finfo
.external_relocs
);
939 finfo
.external_relocs
= NULL
;
941 if (finfo
.internal_relocs
!= NULL
)
943 free (finfo
.internal_relocs
);
944 finfo
.internal_relocs
= NULL
;
947 /* The value of the last C_FILE symbol is supposed to be the symbol
948 index of the first external symbol. Write it out again if
950 if (finfo
.last_file_index
!= -1
951 && finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
953 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
954 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
955 (PTR
) finfo
.outsyms
);
957 (obj_sym_filepos (abfd
)
958 + finfo
.last_file_index
* symesz
),
960 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
964 /* Write out the global symbols. */
965 finfo
.failed
= false;
966 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
971 /* The outsyms buffer is used by coff_write_global_sym. */
972 if (finfo
.outsyms
!= NULL
)
974 free (finfo
.outsyms
);
975 finfo
.outsyms
= NULL
;
978 if (info
->relocateable
)
980 /* Now that we have written out all the global symbols, we know
981 the symbol indices to use for relocs against them, and we can
982 finally write out the relocs. */
983 external_relocs
= (bfd_byte
*) malloc (max_output_reloc_count
* relsz
);
984 if (external_relocs
== NULL
)
986 bfd_set_error (bfd_error_no_memory
);
990 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
992 struct internal_reloc
*irel
;
993 struct internal_reloc
*irelend
;
994 struct coff_link_hash_entry
**rel_hash
;
997 if (o
->reloc_count
== 0)
1000 irel
= finfo
.section_info
[o
->target_index
].relocs
;
1001 irelend
= irel
+ o
->reloc_count
;
1002 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
1003 erel
= external_relocs
;
1004 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
1006 if (*rel_hash
!= NULL
)
1008 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
1009 irel
->r_symndx
= (*rel_hash
)->indx
;
1011 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
1014 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
1015 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
1016 abfd
) != relsz
* o
->reloc_count
)
1020 free (external_relocs
);
1021 external_relocs
= NULL
;
1024 /* Free up the section information. */
1025 if (finfo
.section_info
!= NULL
)
1029 for (i
= 0; i
< abfd
->section_count
; i
++)
1031 if (finfo
.section_info
[i
].relocs
!= NULL
)
1032 free (finfo
.section_info
[i
].relocs
);
1033 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1034 free (finfo
.section_info
[i
].rel_hashes
);
1036 free (finfo
.section_info
);
1037 finfo
.section_info
= NULL
;
1040 /* Write out the string table. */
1042 (obj_sym_filepos (abfd
)
1043 + obj_raw_syment_count (abfd
) * symesz
),
1047 #if STRING_SIZE_SIZE == 4
1049 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
1050 (bfd_byte
*) strbuf
);
1052 #error Change bfd_h_put_32
1055 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
1058 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
1061 _bfd_stringtab_free (finfo
.strtab
);
1063 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
1064 not try to write out the symbols. */
1065 bfd_get_symcount (abfd
) = 0;
1070 if (finfo
.strtab
!= NULL
)
1071 _bfd_stringtab_free (finfo
.strtab
);
1072 if (finfo
.section_info
!= NULL
)
1076 for (i
= 0; i
< abfd
->section_count
; i
++)
1078 if (finfo
.section_info
[i
].relocs
!= NULL
)
1079 free (finfo
.section_info
[i
].relocs
);
1080 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
1081 free (finfo
.section_info
[i
].rel_hashes
);
1083 free (finfo
.section_info
);
1085 if (finfo
.internal_syms
!= NULL
)
1086 free (finfo
.internal_syms
);
1087 if (finfo
.sec_ptrs
!= NULL
)
1088 free (finfo
.sec_ptrs
);
1089 if (finfo
.sym_indices
!= NULL
)
1090 free (finfo
.sym_indices
);
1091 if (finfo
.outsyms
!= NULL
)
1092 free (finfo
.outsyms
);
1093 if (finfo
.linenos
!= NULL
)
1094 free (finfo
.linenos
);
1095 if (finfo
.contents
!= NULL
)
1096 free (finfo
.contents
);
1097 if (finfo
.external_relocs
!= NULL
)
1098 free (finfo
.external_relocs
);
1099 if (finfo
.internal_relocs
!= NULL
)
1100 free (finfo
.internal_relocs
);
1101 if (external_relocs
!= NULL
)
1102 free (external_relocs
);
1106 /* Read in and swap the relocs. This returns a buffer holding the
1107 relocs for section SEC in file ABFD. If CACHE is true and
1108 INTERNAL_RELOCS is NULL, the relocs read in wil be saved in case
1109 the function is called again. If EXTERNAL_RELOCS is not NULL, it
1110 is a buffer large enough to hold the unswapped relocs. If
1111 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
1112 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
1113 value must be INTERNAL_RELOCS. The function returns NULL on error. */
1115 struct internal_reloc
*
1116 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
1117 require_internal
, internal_relocs
)
1121 bfd_byte
*external_relocs
;
1122 boolean require_internal
;
1123 struct internal_reloc
*internal_relocs
;
1125 bfd_size_type relsz
;
1126 bfd_byte
*free_external
= NULL
;
1127 struct internal_reloc
*free_internal
= NULL
;
1130 struct internal_reloc
*irel
;
1132 if (coff_section_data (abfd
, sec
) != NULL
1133 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
1135 if (! require_internal
)
1136 return coff_section_data (abfd
, sec
)->relocs
;
1137 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
1138 sec
->reloc_count
* sizeof (struct internal_reloc
));
1139 return internal_relocs
;
1142 relsz
= bfd_coff_relsz (abfd
);
1144 if (external_relocs
== NULL
)
1146 free_external
= (bfd_byte
*) malloc (sec
->reloc_count
* relsz
);
1147 if (free_external
== NULL
&& sec
->reloc_count
> 0)
1149 bfd_set_error (bfd_error_no_memory
);
1152 external_relocs
= free_external
;
1155 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
1156 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
1157 != relsz
* sec
->reloc_count
))
1160 if (internal_relocs
== NULL
)
1162 free_internal
= ((struct internal_reloc
*)
1163 malloc (sec
->reloc_count
1164 * sizeof (struct internal_reloc
)));
1165 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
1167 bfd_set_error (bfd_error_no_memory
);
1170 internal_relocs
= free_internal
;
1173 /* Swap in the relocs. */
1174 erel
= external_relocs
;
1175 erel_end
= erel
+ relsz
* sec
->reloc_count
;
1176 irel
= internal_relocs
;
1177 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
1178 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
1180 if (free_external
!= NULL
)
1182 free (free_external
);
1183 free_external
= NULL
;
1186 if (cache
&& free_internal
!= NULL
)
1188 if (coff_section_data (abfd
, sec
) == NULL
)
1190 sec
->used_by_bfd
= ((PTR
) bfd_zalloc (abfd
,
1191 sizeof (struct coff_section_tdata
)));
1192 if (sec
->used_by_bfd
== NULL
)
1194 bfd_set_error (bfd_error_no_memory
);
1197 coff_section_data (abfd
, sec
)->contents
= NULL
;
1199 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
1202 return internal_relocs
;
1205 if (free_external
!= NULL
)
1206 free (free_external
);
1207 if (free_internal
!= NULL
)
1208 free (free_internal
);
1212 /* Link an input file into the linker output file. This function
1213 handles all the sections and relocations of the input file at once. */
1216 coff_link_input_bfd (finfo
, input_bfd
)
1217 struct coff_final_link_info
*finfo
;
1220 boolean (*sym_is_global
) PARAMS ((bfd
*, struct internal_syment
*));
1221 boolean (*adjust_symndx
) PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*,
1222 asection
*, struct internal_reloc
*,
1225 const char *strings
;
1226 bfd_size_type syment_base
;
1227 unsigned int n_tmask
;
1228 unsigned int n_btshft
;
1230 bfd_size_type isymesz
;
1231 bfd_size_type osymesz
;
1232 bfd_size_type linesz
;
1235 struct internal_syment
*isymp
;
1240 struct coff_link_hash_entry
**sym_hash
;
1243 /* Move all the symbols to the output file. */
1245 output_bfd
= finfo
->output_bfd
;
1246 sym_is_global
= coff_backend_info (input_bfd
)->_bfd_coff_sym_is_global
;
1248 syment_base
= obj_raw_syment_count (output_bfd
);
1249 isymesz
= bfd_coff_symesz (input_bfd
);
1250 osymesz
= bfd_coff_symesz (output_bfd
);
1251 linesz
= bfd_coff_linesz (input_bfd
);
1252 BFD_ASSERT (linesz
== bfd_coff_linesz (output_bfd
));
1254 n_tmask
= coff_data (input_bfd
)->local_n_tmask
;
1255 n_btshft
= coff_data (input_bfd
)->local_n_btshft
;
1257 /* Define macros so that ISFCN, et. al., macros work correctly. */
1258 #define N_TMASK n_tmask
1259 #define N_BTSHFT n_btshft
1262 if (! finfo
->info
->keep_memory
)
1265 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1268 if (! _bfd_coff_get_external_symbols (input_bfd
))
1271 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1272 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1273 isymp
= finfo
->internal_syms
;
1274 secpp
= finfo
->sec_ptrs
;
1275 indexp
= finfo
->sym_indices
;
1276 output_index
= syment_base
;
1277 outsym
= finfo
->outsyms
;
1279 if (obj_pe (output_bfd
))
1281 if (!process_embedded_commands (input_bfd
))
1285 while (esym
< esym_end
)
1287 struct internal_syment isym
;
1292 bfd_coff_swap_sym_in (input_bfd
, (PTR
) esym
, (PTR
) isymp
);
1294 /* Make a copy of *isymp so that the relocate_section function
1295 always sees the original values. This is more reliable than
1296 always recomputing the symbol value even if we are stripping
1300 if (isym
.n_scnum
!= 0)
1301 *secpp
= coff_section_from_bfd_index (input_bfd
, isym
.n_scnum
);
1304 if (isym
.n_value
== 0)
1305 *secpp
= bfd_und_section_ptr
;
1307 *secpp
= bfd_com_section_ptr
;
1314 add
= 1 + isym
.n_numaux
;
1316 /* If we are stripping all symbols, we want to skip this one. */
1317 if (finfo
->info
->strip
== strip_all
)
1322 if (isym
.n_sclass
== C_EXT
1323 || (sym_is_global
&& (*sym_is_global
) (input_bfd
, &isym
)))
1325 /* This is a global symbol. Global symbols come at the
1326 end of the symbol table, so skip them for now.
1327 Function symbols, however, are an exception, and are
1328 not moved to the end. */
1330 if (! ISFCN (isym
.n_type
))
1335 /* This is a local symbol. Skip it if we are discarding
1337 if (finfo
->info
->discard
== discard_all
)
1342 /* If we stripping debugging symbols, and this is a debugging
1343 symbol, then skip it. */
1345 && finfo
->info
->strip
== strip_debugger
1346 && isym
.n_scnum
== N_DEBUG
)
1349 /* If some symbols are stripped based on the name, work out the
1350 name and decide whether to skip this symbol. */
1352 && (finfo
->info
->strip
== strip_some
1353 || finfo
->info
->discard
== discard_l
))
1356 char buf
[SYMNMLEN
+ 1];
1358 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
, buf
);
1362 if ((finfo
->info
->strip
== strip_some
1363 && (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false,
1366 && finfo
->info
->discard
== discard_l
1367 && strncmp (name
, finfo
->info
->lprefix
,
1368 finfo
->info
->lprefix_len
) == 0))
1372 /* We now know whether we are to skip this symbol or not. */
1375 /* Adjust the symbol in order to output it. */
1377 if (isym
._n
._n_n
._n_zeroes
== 0
1378 && isym
._n
._n_n
._n_offset
!= 0)
1383 /* This symbol has a long name. Enter it in the string
1384 table we are building. Note that we do not check
1385 bfd_coff_symname_in_debug. That is only true for
1386 XCOFF, and XCOFF requires different linking code
1388 name
= _bfd_coff_internal_syment_name (input_bfd
, &isym
,
1392 indx
= _bfd_stringtab_add (finfo
->strtab
, name
, hash
, copy
);
1393 if (indx
== (bfd_size_type
) -1)
1395 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1398 if (isym
.n_scnum
> 0)
1400 isym
.n_scnum
= (*secpp
)->output_section
->target_index
;
1401 isym
.n_value
+= ((*secpp
)->output_section
->vma
1402 + (*secpp
)->output_offset
1406 /* The value of a C_FILE symbol is the symbol index of the
1407 next C_FILE symbol. The value of the last C_FILE symbol
1408 is the symbol index to the first external symbol
1409 (actually, coff_renumber_symbols does not get this
1410 right--it just sets the value of the last C_FILE symbol
1411 to zero--and nobody has ever complained about it). We
1412 try to get this right, below, just before we write the
1413 symbols out, but in the general case we may have to write
1414 the symbol out twice. */
1415 if (isym
.n_sclass
== C_FILE
)
1417 if (finfo
->last_file_index
!= -1
1418 && finfo
->last_file
.n_value
!= output_index
)
1420 /* We must correct the value of the last C_FILE entry. */
1421 finfo
->last_file
.n_value
= output_index
;
1422 if (finfo
->last_file_index
>= syment_base
)
1424 /* The last C_FILE symbol is in this input file. */
1425 bfd_coff_swap_sym_out (output_bfd
,
1426 (PTR
) &finfo
->last_file
,
1427 (PTR
) (finfo
->outsyms
1428 + ((finfo
->last_file_index
1434 /* We have already written out the last C_FILE
1435 symbol. We need to write it out again. We
1436 borrow *outsym temporarily. */
1437 bfd_coff_swap_sym_out (output_bfd
,
1438 (PTR
) &finfo
->last_file
,
1440 if (bfd_seek (output_bfd
,
1441 (obj_sym_filepos (output_bfd
)
1442 + finfo
->last_file_index
* osymesz
),
1444 || (bfd_write (outsym
, osymesz
, 1, output_bfd
)
1450 finfo
->last_file_index
= output_index
;
1451 finfo
->last_file
= isym
;
1454 /* Output the symbol. */
1456 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) outsym
);
1458 *indexp
= output_index
;
1463 struct coff_link_hash_entry
*h
;
1465 indx
= ((esym
- (bfd_byte
*) obj_coff_external_syms (input_bfd
))
1467 h
= obj_coff_sym_hashes (input_bfd
)[indx
];
1468 BFD_ASSERT (h
!= NULL
);
1469 h
->indx
= output_index
;
1472 output_index
+= add
;
1473 outsym
+= add
* osymesz
;
1476 esym
+= add
* isymesz
;
1480 for (--add
; add
> 0; --add
)
1487 /* Fix up the aux entries. This must be done in a separate pass,
1488 because we don't know the correct symbol indices until we have
1489 already decided which symbols we are going to keep. */
1491 esym
= (bfd_byte
*) obj_coff_external_syms (input_bfd
);
1492 esym_end
= esym
+ obj_raw_syment_count (input_bfd
) * isymesz
;
1493 isymp
= finfo
->internal_syms
;
1494 indexp
= finfo
->sym_indices
;
1495 sym_hash
= obj_coff_sym_hashes (input_bfd
);
1496 outsym
= finfo
->outsyms
;
1497 while (esym
< esym_end
)
1501 add
= 1 + isymp
->n_numaux
;
1504 && (*sym_hash
== NULL
1505 || (*sym_hash
)->auxbfd
!= input_bfd
))
1506 esym
+= add
* isymesz
;
1509 struct coff_link_hash_entry
*h
;
1516 BFD_ASSERT (h
->numaux
== isymp
->n_numaux
);
1524 /* Handle the aux entries. This handling is based on
1525 coff_pointerize_aux. I don't know if it always correct. */
1526 for (i
= 0; i
< isymp
->n_numaux
&& esym
< esym_end
; i
++)
1528 union internal_auxent aux
;
1529 union internal_auxent
*auxp
;
1535 bfd_coff_swap_aux_in (input_bfd
, (PTR
) esym
, isymp
->n_type
,
1536 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1541 if (isymp
->n_sclass
== C_FILE
)
1543 /* If this is a long filename, we must put it in the
1545 if (auxp
->x_file
.x_n
.x_zeroes
== 0
1546 && auxp
->x_file
.x_n
.x_offset
!= 0)
1548 const char *filename
;
1551 BFD_ASSERT (auxp
->x_file
.x_n
.x_offset
1552 >= STRING_SIZE_SIZE
);
1553 if (strings
== NULL
)
1555 strings
= _bfd_coff_read_string_table (input_bfd
);
1556 if (strings
== NULL
)
1559 filename
= strings
+ auxp
->x_file
.x_n
.x_offset
;
1560 indx
= _bfd_stringtab_add (finfo
->strtab
, filename
,
1562 if (indx
== (bfd_size_type
) -1)
1564 auxp
->x_file
.x_n
.x_offset
= STRING_SIZE_SIZE
+ indx
;
1567 else if (isymp
->n_sclass
!= C_STAT
|| isymp
->n_type
!= T_NULL
)
1571 if (ISFCN (isymp
->n_type
)
1572 || ISTAG (isymp
->n_sclass
)
1573 || isymp
->n_sclass
== C_BLOCK
)
1575 indx
= auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1577 && indx
< obj_raw_syment_count (input_bfd
))
1579 /* We look forward through the symbol for
1580 the index of the next symbol we are going
1581 to include. I don't know if this is
1583 while (finfo
->sym_indices
[indx
] < 0
1584 && indx
< obj_raw_syment_count (input_bfd
))
1586 if (indx
>= obj_raw_syment_count (input_bfd
))
1587 indx
= output_index
;
1589 indx
= finfo
->sym_indices
[indx
];
1590 auxp
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
= indx
;
1594 indx
= auxp
->x_sym
.x_tagndx
.l
;
1595 if (indx
> 0 && indx
< obj_raw_syment_count (input_bfd
))
1597 indx
= finfo
->sym_indices
[indx
];
1599 auxp
->x_sym
.x_tagndx
.l
= 0;
1601 auxp
->x_sym
.x_tagndx
.l
= indx
;
1607 bfd_coff_swap_aux_out (output_bfd
, (PTR
) auxp
, isymp
->n_type
,
1608 isymp
->n_sclass
, i
, isymp
->n_numaux
,
1622 /* Relocate the line numbers, unless we are stripping them. */
1623 if (finfo
->info
->strip
== strip_none
1624 || finfo
->info
->strip
== strip_some
)
1626 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1632 if (o
->lineno_count
== 0)
1635 if (bfd_seek (input_bfd
, o
->line_filepos
, SEEK_SET
) != 0
1636 || bfd_read (finfo
->linenos
, linesz
, o
->lineno_count
,
1637 input_bfd
) != linesz
* o
->lineno_count
)
1640 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1641 eline
= finfo
->linenos
;
1642 elineend
= eline
+ linesz
* o
->lineno_count
;
1643 for (; eline
< elineend
; eline
+= linesz
)
1645 struct internal_lineno iline
;
1647 bfd_coff_swap_lineno_in (input_bfd
, (PTR
) eline
, (PTR
) &iline
);
1649 if (iline
.l_lnno
!= 0)
1650 iline
.l_addr
.l_paddr
+= offset
;
1651 else if (iline
.l_addr
.l_symndx
>= 0
1652 && (iline
.l_addr
.l_symndx
1653 < obj_raw_syment_count (input_bfd
)))
1657 indx
= finfo
->sym_indices
[iline
.l_addr
.l_symndx
];
1661 /* These line numbers are attached to a symbol
1662 which we are stripping. We should really
1663 just discard the line numbers, but that would
1664 be a pain because we have already counted
1670 struct internal_syment is
;
1671 union internal_auxent ia
;
1673 /* Fix up the lnnoptr field in the aux entry of
1674 the symbol. It turns out that we can't do
1675 this when we modify the symbol aux entries,
1676 because gas sometimes screws up the lnnoptr
1677 field and makes it an offset from the start
1678 of the line numbers rather than an absolute
1680 bfd_coff_swap_sym_in (output_bfd
,
1681 (PTR
) (finfo
->outsyms
1682 + ((indx
- syment_base
)
1685 if ((ISFCN (is
.n_type
)
1686 || is
.n_sclass
== C_BLOCK
)
1687 && is
.n_numaux
>= 1)
1691 auxptr
= (PTR
) (finfo
->outsyms
1692 + ((indx
- syment_base
+ 1)
1694 bfd_coff_swap_aux_in (output_bfd
, auxptr
,
1695 is
.n_type
, is
.n_sclass
,
1696 0, is
.n_numaux
, (PTR
) &ia
);
1697 ia
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1698 (o
->output_section
->line_filepos
1699 + o
->output_section
->lineno_count
* linesz
1700 + eline
- finfo
->linenos
);
1701 bfd_coff_swap_aux_out (output_bfd
, (PTR
) &ia
,
1702 is
.n_type
, is
.n_sclass
, 0,
1703 is
.n_numaux
, auxptr
);
1707 iline
.l_addr
.l_symndx
= indx
;
1710 bfd_coff_swap_lineno_out (output_bfd
, (PTR
) &iline
, (PTR
) eline
);
1713 if (bfd_seek (output_bfd
,
1714 (o
->output_section
->line_filepos
1715 + o
->output_section
->lineno_count
* linesz
),
1717 || bfd_write (finfo
->linenos
, linesz
, o
->lineno_count
,
1718 output_bfd
) != linesz
* o
->lineno_count
)
1721 o
->output_section
->lineno_count
+= o
->lineno_count
;
1725 /* If we swapped out a C_FILE symbol, guess that the next C_FILE
1726 symbol will be the first symbol in the next input file. In the
1727 normal case, this will save us from writing out the C_FILE symbol
1729 if (finfo
->last_file_index
!= -1
1730 && finfo
->last_file_index
>= syment_base
)
1732 finfo
->last_file
.n_value
= output_index
;
1733 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &finfo
->last_file
,
1734 (PTR
) (finfo
->outsyms
1735 + ((finfo
->last_file_index
- syment_base
)
1739 /* Write the modified symbols to the output file. */
1740 if (outsym
> finfo
->outsyms
)
1742 if (bfd_seek (output_bfd
,
1743 obj_sym_filepos (output_bfd
) + syment_base
* osymesz
,
1745 || bfd_write (finfo
->outsyms
, outsym
- finfo
->outsyms
, 1,
1746 output_bfd
) != outsym
- finfo
->outsyms
)
1749 BFD_ASSERT ((obj_raw_syment_count (output_bfd
)
1750 + (outsym
- finfo
->outsyms
) / osymesz
)
1753 obj_raw_syment_count (output_bfd
) = output_index
;
1756 /* Relocate the contents of each section. */
1757 adjust_symndx
= coff_backend_info (input_bfd
)->_bfd_coff_adjust_symndx
;
1758 for (o
= input_bfd
->sections
; o
!= NULL
; o
= o
->next
)
1762 if ((o
->flags
& SEC_HAS_CONTENTS
) == 0)
1765 if (coff_section_data (input_bfd
, o
) != NULL
1766 && coff_section_data (input_bfd
, o
)->contents
!= NULL
)
1767 contents
= coff_section_data (input_bfd
, o
)->contents
;
1770 if (! bfd_get_section_contents (input_bfd
, o
, finfo
->contents
,
1771 (file_ptr
) 0, o
->_raw_size
))
1773 contents
= finfo
->contents
;
1776 if ((o
->flags
& SEC_RELOC
) != 0)
1779 struct internal_reloc
*internal_relocs
;
1780 struct internal_reloc
*irel
;
1782 /* Read in the relocs. */
1783 target_index
= o
->output_section
->target_index
;
1784 internal_relocs
= (_bfd_coff_read_internal_relocs
1785 (input_bfd
, o
, false, finfo
->external_relocs
,
1786 finfo
->info
->relocateable
,
1787 (finfo
->info
->relocateable
1788 ? (finfo
->section_info
[target_index
].relocs
1789 + o
->output_section
->reloc_count
)
1790 : finfo
->internal_relocs
)));
1791 if (internal_relocs
== NULL
)
1794 /* Call processor specific code to relocate the section
1796 if (! bfd_coff_relocate_section (output_bfd
, finfo
->info
,
1800 finfo
->internal_syms
,
1804 if (finfo
->info
->relocateable
)
1807 struct internal_reloc
*irelend
;
1808 struct coff_link_hash_entry
**rel_hash
;
1810 offset
= o
->output_section
->vma
+ o
->output_offset
- o
->vma
;
1811 irel
= internal_relocs
;
1812 irelend
= irel
+ o
->reloc_count
;
1813 rel_hash
= (finfo
->section_info
[target_index
].rel_hashes
1814 + o
->output_section
->reloc_count
);
1815 for (; irel
< irelend
; irel
++, rel_hash
++)
1817 struct coff_link_hash_entry
*h
;
1822 /* Adjust the reloc address and symbol index. */
1824 irel
->r_vaddr
+= offset
;
1826 if (irel
->r_symndx
== -1)
1831 if (! (*adjust_symndx
) (output_bfd
, finfo
->info
,
1839 h
= obj_coff_sym_hashes (input_bfd
)[irel
->r_symndx
];
1842 /* This is a global symbol. */
1844 irel
->r_symndx
= h
->indx
;
1847 /* This symbol is being written at the end
1848 of the file, and we do not yet know the
1849 symbol index. We save the pointer to the
1850 hash table entry in the rel_hash list.
1851 We set the indx field to -2 to indicate
1852 that this symbol must not be stripped. */
1861 indx
= finfo
->sym_indices
[irel
->r_symndx
];
1863 irel
->r_symndx
= indx
;
1866 struct internal_syment
*is
;
1868 char buf
[SYMNMLEN
+ 1];
1870 /* This reloc is against a symbol we are
1871 stripping. It would be possible to
1872 handle this case, but I don't think it's
1874 is
= finfo
->internal_syms
+ irel
->r_symndx
;
1876 name
= (_bfd_coff_internal_syment_name
1877 (input_bfd
, is
, buf
));
1881 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
1882 (finfo
->info
, name
, input_bfd
, o
,
1889 o
->output_section
->reloc_count
+= o
->reloc_count
;
1893 /* Write out the modified section contents. */
1894 if (! bfd_set_section_contents (output_bfd
, o
->output_section
,
1895 contents
, o
->output_offset
,
1896 (o
->_cooked_size
!= 0
1902 if (! finfo
->info
->keep_memory
)
1904 if (! _bfd_coff_free_symbols (input_bfd
))
1911 /* Write out a global symbol. Called via coff_link_hash_traverse. */
1914 coff_write_global_sym (h
, data
)
1915 struct coff_link_hash_entry
*h
;
1918 struct coff_final_link_info
*finfo
= (struct coff_final_link_info
*) data
;
1920 struct internal_syment isym
;
1921 bfd_size_type symesz
;
1924 output_bfd
= finfo
->output_bfd
;
1930 && (finfo
->info
->strip
== strip_all
1931 || (finfo
->info
->strip
== strip_some
1932 && (bfd_hash_lookup (finfo
->info
->keep_hash
,
1933 h
->root
.root
.string
, false, false)
1937 switch (h
->root
.type
)
1940 case bfd_link_hash_new
:
1944 case bfd_link_hash_undefined
:
1945 case bfd_link_hash_undefweak
:
1946 isym
.n_scnum
= N_UNDEF
;
1950 case bfd_link_hash_defined
:
1951 case bfd_link_hash_defweak
:
1955 sec
= h
->root
.u
.def
.section
->output_section
;
1956 if (bfd_is_abs_section (sec
))
1957 isym
.n_scnum
= N_ABS
;
1959 isym
.n_scnum
= sec
->target_index
;
1960 isym
.n_value
= (h
->root
.u
.def
.value
1962 + h
->root
.u
.def
.section
->output_offset
);
1966 case bfd_link_hash_common
:
1967 isym
.n_scnum
= N_UNDEF
;
1968 isym
.n_value
= h
->root
.u
.c
.size
;
1971 case bfd_link_hash_indirect
:
1972 case bfd_link_hash_warning
:
1973 /* Just ignore these. They can't be handled anyhow. */
1977 if (strlen (h
->root
.root
.string
) <= SYMNMLEN
)
1978 strncpy (isym
._n
._n_name
, h
->root
.root
.string
, SYMNMLEN
);
1985 if ((output_bfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1987 indx
= _bfd_stringtab_add (finfo
->strtab
, h
->root
.root
.string
, hash
,
1989 if (indx
== (bfd_size_type
) -1)
1991 finfo
->failed
= true;
1994 isym
._n
._n_n
._n_zeroes
= 0;
1995 isym
._n
._n_n
._n_offset
= STRING_SIZE_SIZE
+ indx
;
1998 isym
.n_sclass
= h
->class;
1999 isym
.n_type
= h
->type
;
2001 if (isym
.n_sclass
== C_NULL
)
2002 isym
.n_sclass
= C_EXT
;
2004 isym
.n_numaux
= h
->numaux
;
2006 bfd_coff_swap_sym_out (output_bfd
, (PTR
) &isym
, (PTR
) finfo
->outsyms
);
2008 symesz
= bfd_coff_symesz (output_bfd
);
2010 if (bfd_seek (output_bfd
,
2011 (obj_sym_filepos (output_bfd
)
2012 + obj_raw_syment_count (output_bfd
) * symesz
),
2014 || bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2016 finfo
->failed
= true;
2020 h
->indx
= obj_raw_syment_count (output_bfd
);
2022 ++obj_raw_syment_count (output_bfd
);
2024 /* Write out any associated aux entries. There normally will be
2025 none. If there are any, I have no idea how to modify them. */
2026 for (i
= 0; i
< isym
.n_numaux
; i
++)
2028 bfd_coff_swap_aux_out (output_bfd
, (PTR
) (h
->aux
+ i
), isym
.n_type
,
2029 isym
.n_sclass
, i
, isym
.n_numaux
,
2030 (PTR
) finfo
->outsyms
);
2031 if (bfd_write (finfo
->outsyms
, symesz
, 1, output_bfd
) != symesz
)
2033 finfo
->failed
= true;
2036 ++obj_raw_syment_count (output_bfd
);
2042 /* Handle a link order which is supposed to generate a reloc. */
2045 coff_reloc_link_order (output_bfd
, finfo
, output_section
, link_order
)
2047 struct coff_final_link_info
*finfo
;
2048 asection
*output_section
;
2049 struct bfd_link_order
*link_order
;
2051 reloc_howto_type
*howto
;
2052 struct internal_reloc
*irel
;
2053 struct coff_link_hash_entry
**rel_hash_ptr
;
2055 howto
= bfd_reloc_type_lookup (output_bfd
, link_order
->u
.reloc
.p
->reloc
);
2058 bfd_set_error (bfd_error_bad_value
);
2062 if (link_order
->u
.reloc
.p
->addend
!= 0)
2066 bfd_reloc_status_type rstat
;
2069 size
= bfd_get_reloc_size (howto
);
2070 buf
= (bfd_byte
*) bfd_zmalloc (size
);
2073 bfd_set_error (bfd_error_no_memory
);
2077 rstat
= _bfd_relocate_contents (howto
, output_bfd
,
2078 link_order
->u
.reloc
.p
->addend
, buf
);
2084 case bfd_reloc_outofrange
:
2086 case bfd_reloc_overflow
:
2087 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
2089 (link_order
->type
== bfd_section_reloc_link_order
2090 ? bfd_section_name (output_bfd
,
2091 link_order
->u
.reloc
.p
->u
.section
)
2092 : link_order
->u
.reloc
.p
->u
.name
),
2093 howto
->name
, link_order
->u
.reloc
.p
->addend
,
2094 (bfd
*) NULL
, (asection
*) NULL
, (bfd_vma
) 0)))
2101 ok
= bfd_set_section_contents (output_bfd
, output_section
, (PTR
) buf
,
2102 (file_ptr
) link_order
->offset
, size
);
2108 /* Store the reloc information in the right place. It will get
2109 swapped and written out at the end of the final_link routine. */
2111 irel
= (finfo
->section_info
[output_section
->target_index
].relocs
2112 + output_section
->reloc_count
);
2113 rel_hash_ptr
= (finfo
->section_info
[output_section
->target_index
].rel_hashes
2114 + output_section
->reloc_count
);
2116 memset (irel
, 0, sizeof (struct internal_reloc
));
2117 *rel_hash_ptr
= NULL
;
2119 irel
->r_vaddr
= output_section
->vma
+ link_order
->offset
;
2121 if (link_order
->type
== bfd_section_reloc_link_order
)
2123 /* We need to somehow locate a symbol in the right section. The
2124 symbol must either have a value of zero, or we must adjust
2125 the addend by the value of the symbol. FIXME: Write this
2126 when we need it. The old linker couldn't handle this anyhow. */
2128 *rel_hash_ptr
= NULL
;
2133 struct coff_link_hash_entry
*h
;
2135 h
= coff_link_hash_lookup (coff_hash_table (finfo
->info
),
2136 link_order
->u
.reloc
.p
->u
.name
,
2137 false, false, true);
2141 irel
->r_symndx
= h
->indx
;
2144 /* Set the index to -2 to force this symbol to get
2153 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
2154 (finfo
->info
, link_order
->u
.reloc
.p
->u
.name
, (bfd
*) NULL
,
2155 (asection
*) NULL
, (bfd_vma
) 0)))
2161 /* FIXME: Is this always right? */
2162 irel
->r_type
= howto
->type
;
2164 /* r_size is only used on the RS/6000, which needs its own linker
2165 routines anyhow. r_extern is only used for ECOFF. */
2167 /* FIXME: What is the right value for r_offset? Is zero OK? */
2169 ++output_section
->reloc_count
;
2174 /* A basic reloc handling routine which may be used by processors with
2178 _bfd_coff_generic_relocate_section (output_bfd
, info
, input_bfd
,
2179 input_section
, contents
, relocs
, syms
,
2182 struct bfd_link_info
*info
;
2184 asection
*input_section
;
2186 struct internal_reloc
*relocs
;
2187 struct internal_syment
*syms
;
2188 asection
**sections
;
2190 struct internal_reloc
*rel
;
2191 struct internal_reloc
*relend
;
2195 relend
= rel
+ input_section
->reloc_count
;
2196 for (; rel
< relend
; rel
++)
2199 struct coff_link_hash_entry
*h
;
2200 struct internal_syment
*sym
;
2203 reloc_howto_type
*howto
;
2204 bfd_reloc_status_type rstat
;
2206 symndx
= rel
->r_symndx
;
2215 h
= obj_coff_sym_hashes (input_bfd
)[symndx
];
2216 sym
= syms
+ symndx
;
2219 /* COFF treats common symbols in one of two ways. Either the
2220 size of the symbol is included in the section contents, or it
2221 is not. We assume that the size is not included, and force
2222 the rtype_to_howto function to adjust the addend as needed. */
2224 if (sym
!= NULL
&& sym
->n_scnum
!= 0)
2225 addend
= - sym
->n_value
;
2230 howto
= bfd_coff_rtype_to_howto (input_bfd
, input_section
, rel
, h
,
2235 /* WINDOWS_NT; in this next section, the value of 'val' will be computed.
2236 With respect to the .idata and .rsrc sections, the NT_IMAGE_BASE
2237 must be removed from the value that is to be relocated (NT_IMAGE_BASE
2238 is currently defined in internal.h and has value 400000). Now this
2239 value should only be removed from addresses being relocated in the
2240 .idata and .rsrc sections, not the .text section which should have
2241 the 'real' address. In addition, the .rsrc val's must also be
2242 adjusted by the input_section->vma. */
2252 sec
= bfd_abs_section_ptr
;
2257 sec
= sections
[symndx
];
2258 val
= (sec
->output_section
->vma
2259 + sec
->output_offset
2266 if (h
->root
.type
== bfd_link_hash_defined
2267 || h
->root
.type
== bfd_link_hash_defweak
)
2271 sec
= h
->root
.u
.def
.section
;
2272 val
= (h
->root
.u
.def
.value
2273 + sec
->output_section
->vma
2274 + sec
->output_offset
);
2277 else if (! info
->relocateable
)
2279 if (! ((*info
->callbacks
->undefined_symbol
)
2280 (info
, h
->root
.root
.string
, input_bfd
, input_section
,
2281 rel
->r_vaddr
- input_section
->vma
)))
2286 if (info
->base_file
)
2288 /* So if this is non pcrelative, and is referenced
2289 to a section or a common symbol, then it needs a reloc */
2290 if (!howto
->pc_relative
2294 /* relocation to a symbol in a section which
2295 isn't absolute - we output the address here
2297 bfd_vma addr
= rel
->r_vaddr
2298 + input_section
->output_offset
2299 + input_section
->output_section
->vma
;
2300 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
2304 rstat
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
2306 rel
->r_vaddr
- input_section
->vma
,
2315 case bfd_reloc_overflow
:
2318 char buf
[SYMNMLEN
+ 1];
2323 name
= h
->root
.root
.string
;
2326 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
2331 if (! ((*info
->callbacks
->reloc_overflow
)
2332 (info
, name
, howto
->name
, (bfd_vma
) 0, input_bfd
,
2333 input_section
, rel
->r_vaddr
- input_section
->vma
)))