1 /* coff object file format with bfd
2 Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GAS.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 How does this releate to the rest of GAS ?
24 Well, all the other files in gas are more or less a black box. It
25 takes care of opening files, parsing command lines, stripping blanks
26 etc etc. This module gets a chance to register what it wants to do by
27 saying that it is interested in various pseduo ops. The other big
28 change is write_object_file. This runs through all the data
29 structures that gas builds, and outputs the file in the format of our
32 Hacked for BFDness by steve chamberlain
34 This object module now supports the Hitachi H8/300 and the AMD 29k
43 #include "../bfd/libbfd.h"
46 #define MIN(a,b) ((a) < (b)? (a) : (b))
47 /* This vector is used to turn an internal segment into a section #
48 suitable for insertion into a coff symbol table
51 const short seg_N_TYPE
[] = { /* in: segT out: N_TYPE bits */
63 C_UNDEF_SECTION
, /* SEG_UNKNOWN */
64 C_UNDEF_SECTION
, /* SEG_ABSENT */
65 C_UNDEF_SECTION
, /* SEG_PASS1 */
66 C_UNDEF_SECTION
, /* SEG_GOOF */
67 C_UNDEF_SECTION
, /* SEG_BIG */
68 C_UNDEF_SECTION
, /* SEG_DIFFERENCE */
69 C_DEBUG_SECTION
, /* SEG_DEBUG */
70 C_NTV_SECTION
, /* SEG_NTV */
71 C_PTV_SECTION
, /* SEG_PTV */
72 C_REGISTER_SECTION
, /* SEG_REGISTER */
76 int function_lineoff
= -1; /* Offset in line#s where the last function
77 started (the odd entry for line #0) */
83 static symbolS
*last_line_symbol
;
84 /* Add 4 to the real value to get the index and compensate the
85 negatives. This vector is used by S_GET_SEGMENT to turn a coff
86 section number into a segment number
88 static symbolS
*previous_file_symbol
= NULL
;
89 void c_symbol_merge();
92 symbolS
*c_section_symbol();
94 void EXFUN(bfd_as_write_hook
,(struct internal_filehdr
*,
97 static void EXFUN(fixup_segment
,(fixS
* fixP
,
98 segT this_segment_type
));
100 static void EXFUN(fill_section
,(bfd
*abfd
,
101 struct internal_filehdr
*f
, unsigned
105 char *EXFUN(s_get_name
,(symbolS
*s
));
106 static symbolS
*EXFUN(tag_find_or_make
,(char *name
));
107 static symbolS
* EXFUN(tag_find
,(char *name
));
114 unsigned short line_number
,
118 static void EXFUN(w_symbols
,
121 symbolS
*symbol_rootP
));
125 static void EXFUN( obj_coff_def
,(int what
));
126 static void EXFUN( obj_coff_lcomm
,(void));
127 static void EXFUN( obj_coff_dim
,(void));
128 static void EXFUN( obj_coff_text
,(void));
129 static void EXFUN( obj_coff_data
,(void));
130 static void EXFUN( obj_coff_endef
,(void));
131 static void EXFUN( obj_coff_line
,(void));
132 static void EXFUN( obj_coff_ln
,(void));
133 static void EXFUN( obj_coff_scl
,(void));
134 static void EXFUN( obj_coff_size
,(void));
135 static void EXFUN( obj_coff_tag
,(void));
136 static void EXFUN( obj_coff_type
,(void));
137 static void EXFUN( obj_coff_val
,(void));
138 static void EXFUN( obj_coff_section
,(void));
139 static void EXFUN( tag_init
,(void));
140 static void EXFUN( tag_insert
,(char *name
, symbolS
*symbolP
));
143 static struct hash_control
*tag_hash
;
144 static symbolS
*def_symbol_in_progress
= NULL
;
146 const pseudo_typeS obj_pseudo_table
[] = {
147 { "def", obj_coff_def
, 0 },
148 { "dim", obj_coff_dim
, 0 },
149 { "endef", obj_coff_endef
, 0 },
150 { "line", obj_coff_line
, 0 },
151 { "ln", obj_coff_ln
, 0 },
152 { "scl", obj_coff_scl
, 0 },
153 { "size", obj_coff_size
, 0 },
154 { "tag", obj_coff_tag
, 0 },
155 { "type", obj_coff_type
, 0 },
156 { "val", obj_coff_val
, 0 },
157 { "section", obj_coff_section
, 0 },
158 { "use", obj_coff_section
, 0 },
159 { "sect", obj_coff_section
, 0 },
160 { "text", obj_coff_text
, 0 },
161 { "data", obj_coff_data
, 0 },
162 /* we don't yet handle this. */
163 { "ident", s_ignore
, 0 },
164 { "ABORT", s_abort
, 0 },
165 { "lcomm", obj_coff_lcomm
, 0},
166 { NULL
} /* end sentinel */
167 }; /* obj_pseudo_table */
173 We allow more than just the standard 3 sections, infact, we allow
174 10 sections, (though the usual three have to be there).
176 This structure performs the mappings for us:
181 static struct internal_scnhdr bss_section_header;
182 struct internal_scnhdr data_section_header;
183 struct internal_scnhdr text_section_header;
185 const segT N_TYPE_seg [32] =
199 seg_info_type seg_info_off_by_4
[N_SEG
] =
225 {SEG_REGISTER
},0,0,0,0};
227 #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
228 #define SEG_INFO_FROM_SEG_NUMBER(x) (seg_info_off_by_4[(x)])
232 DEFUN(relax_align
,(address
, alignment
),
233 register relax_addressT address AND
234 register long alignment
)
237 relax_addressT new_address
;
239 mask
= ~ ( (~0) << alignment
);
240 new_address
= (address
+ mask
) & (~ mask
);
241 return (new_address
- address
);
242 } /* relax_align() */
246 DEFUN(s_get_segment
,(x
) ,
249 return SEG_INFO_FROM_SECTION_NUMBER(x
->sy_symbol
.ost_entry
.n_scnum
).seg_t
;
254 /* calculate the size of the frag chain and fill in the section header
255 to contain all of it, also fill in the addr of the sections */
256 static unsigned int DEFUN(size_section
,(abfd
, idx
),
261 unsigned int size
= 0;
262 fragS
*frag
= segment_info
[idx
].frchainP
->frch_root
;
264 if (frag
->fr_address
!= size
) {
265 printf("Out of step\n");
266 size
= frag
->fr_address
;
268 size
+= frag
->fr_fix
;
269 switch (frag
->fr_type
) {
272 size
+= frag
->fr_offset
* frag
->fr_var
;
275 size
+= relax_align(size
, frag
->fr_offset
);
277 frag
= frag
->fr_next
;
279 segment_info
[idx
].scnhdr
.s_size
= size
;
284 static unsigned int DEFUN(count_entries_in_chain
,(idx
),
287 unsigned int nrelocs
;
290 /* Count the relocations */
291 fixup_ptr
= segment_info
[idx
].fix_root
;
293 while (fixup_ptr
!= (fixS
*)NULL
)
295 if (TC_COUNT_RELOC(fixup_ptr
))
300 if (fixup_ptr
->fx_r_type
== RELOC_CONSTH
)
309 fixup_ptr
= fixup_ptr
->fx_next
;
314 /* output all the relocations for a section */
315 void DEFUN(do_relocs_for
,(abfd
, file_cursor
),
317 unsigned long *file_cursor
)
319 unsigned int nrelocs
;
321 unsigned int addr
= 0;
322 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
324 if (segment_info
[idx
].scnhdr
.s_name
[0])
327 struct external_reloc
*ext_ptr
;
328 struct external_reloc
*external_reloc_vec
;
329 unsigned int external_reloc_size
;
330 unsigned int count
= 0;
331 unsigned int base
= addr
;
332 fixS
* fix_ptr
= segment_info
[idx
].fix_root
;
333 nrelocs
= count_entries_in_chain(idx
);
338 external_reloc_size
= nrelocs
* RELSZ
;
340 (struct external_reloc
*)malloc(external_reloc_size
);
344 ext_ptr
= external_reloc_vec
;
346 /* Fill in the internal coff style reloc struct from the
351 struct internal_reloc intr
;
353 /* Only output some of the relocations */
354 if (TC_COUNT_RELOC(fix_ptr
))
356 #ifdef TC_RELOC_MANGLE
357 TC_RELOC_MANGLE(fix_ptr
, &intr
, base
);
361 symbol_ptr
= fix_ptr
->fx_addsy
;
363 intr
.r_type
= TC_COFF_FIX2RTYPE(fix_ptr
);
365 base
+ fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
;
367 intr
.r_offset
= fix_ptr
->fx_offset
;
371 /* Turn the segment of the symbol into an offset
375 dot
= segment_info
[S_GET_SEGMENT(symbol_ptr
)].dot
;
378 intr
.r_symndx
= dot
->sy_number
;
382 intr
.r_symndx
= symbol_ptr
->sy_number
;
394 (void)bfd_coff_swap_reloc_out(abfd
, &intr
, ext_ptr
);
398 /* The 29k has a special kludge for the high 16 bit reloc.
399 Two relocations are emmited, R_IHIHALF, and
400 R_IHCONST. The second one doesn't contain a symbol,
401 but uses the value for offset */
403 if (intr
.r_type
== R_IHIHALF
)
405 /* now emit the second bit */
406 intr
.r_type
= R_IHCONST
;
407 intr
.r_symndx
= fix_ptr
->fx_addnumber
;
408 (void)bfd_coff_swap_reloc_out(abfd
,&intr
,ext_ptr
);
414 fix_ptr
= fix_ptr
->fx_next
;
417 /* Write out the reloc table */
418 segment_info
[idx
].scnhdr
.s_relptr
= *file_cursor
;
419 segment_info
[idx
].scnhdr
.s_nreloc
= nrelocs
;
420 bfd_write((PTR
)external_reloc_vec
, 1, external_reloc_size
, abfd
);
421 *file_cursor
+= external_reloc_size
;
422 free( external_reloc_vec
);
424 addr
+= segment_info
[idx
].scnhdr
.s_size
;
429 /* run through a frag chain and write out the data to go with it, fill
430 in the scnhdrs with the info on the file postions
432 static void DEFUN(fill_section
,(abfd
, filehdr
, file_cursor
),
434 struct internal_filehdr
*filehdr AND
435 unsigned long *file_cursor
)
439 unsigned int paddr
= 0;
441 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
443 unsigned int offset
= 0;
445 struct internal_scnhdr
*s
= &( segment_info
[i
].scnhdr
);
449 fragS
*frag
= segment_info
[i
].frchainP
->frch_root
;
450 char *buffer
= malloc(s
->s_size
);
453 s
->s_scnptr
= *file_cursor
;
467 s
->s_flags
= STYP_REG
;
468 if (strcmp(s
->s_name
,".text")==0)
469 s
->s_flags
|= STYP_TEXT
;
470 else if (strcmp(s
->s_name
,".data")==0)
471 s
->s_flags
|= STYP_DATA
;
472 else if (strcmp(s
->s_name
,".bss")==0)
473 s
->s_flags
|= STYP_BSS
| STYP_NOLOAD
;
476 unsigned int fill_size
;
477 switch (frag
->fr_type
) {
484 memcpy(buffer
+ frag
->fr_address
,
487 offset
+= frag
->fr_fix
;
490 fill_size
= frag
->fr_var
;
494 unsigned int off
= frag
->fr_fix
;
495 for (count
= frag
->fr_offset
; count
; count
--)
497 memcpy(buffer
+ frag
->fr_address
+ off
,
498 frag
->fr_literal
+ frag
->fr_fix
,
510 frag
= frag
->fr_next
;
514 bfd_write(buffer
, s
->s_size
,1,abfd
);
517 *file_cursor
+= s
->s_size
;
526 /* Coff file generation & utilities */
530 DEFUN(coff_header_append
,(abfd
, filehdr
, aouthdr
),
532 struct internal_filehdr
*filehdr AND
533 struct internal_aouthdr
*aouthdr
)
539 bfd_seek(abfd
, 0, 0);
541 filehdr
.f_opthdr
= bfd_coff_swap_aouthdr_out(abfd
, aouthdr
,
544 filehdr
->f_opthdr
= 0;
546 i
= bfd_coff_swap_filehdr_out(abfd
, filehdr
, buffer
);
548 bfd_write(buffer
, i
,1, abfd
);
549 bfd_write(buffero
, filehdr
->f_opthdr
, 1, abfd
);
551 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
553 if (segment_info
[i
].scnhdr
.s_name
[0])
556 bfd_coff_swap_scnhdr_out(abfd
,
557 &(segment_info
[i
].scnhdr
),
559 bfd_write(buffer
, size
, 1, abfd
);
566 DEFUN(symbol_to_chars
,(abfd
, where
, symbolP
),
571 unsigned int numaux
= symbolP
->sy_symbol
.ost_entry
.n_numaux
;
574 /* Turn any symbols with register attributes into abs symbols */
575 if (S_GET_SEGMENT(symbolP
) == SEG_REGISTER
)
577 S_SET_SEGMENT(symbolP
, SEG_ABSOLUTE
);
579 /* At the same time, relocate all symbols to their output value */
582 segment_info
[S_GET_SEGMENT(symbolP
)].scnhdr
.s_paddr
583 + S_GET_VALUE(symbolP
));
585 where
+= bfd_coff_swap_sym_out(abfd
, &symbolP
->sy_symbol
.ost_entry
,
588 for (i
= 0; i
< numaux
; i
++)
590 where
+= bfd_coff_swap_aux_out(abfd
,
591 &symbolP
->sy_symbol
.ost_auxent
[i
],
592 S_GET_DATA_TYPE(symbolP
),
593 S_GET_STORAGE_CLASS(symbolP
),
603 void obj_symbol_new_hook(symbolP
)
606 char underscore
= 0; /* Symbol has leading _ */
608 /* Effective symbol */
609 /* Store the pointer in the offset. */
610 S_SET_ZEROES(symbolP
, 0L);
611 S_SET_DATA_TYPE(symbolP
, T_NULL
);
612 S_SET_STORAGE_CLASS(symbolP
, 0);
613 S_SET_NUMBER_AUXILIARY(symbolP
, 0);
614 /* Additional information */
615 symbolP
->sy_symbol
.ost_flags
= 0;
616 /* Auxiliary entries */
617 bzero((char*)&symbolP
->sy_symbol
.ost_auxent
[0], AUXESZ
);
619 #ifdef STRIP_UNDERSCORE
620 /* Remove leading underscore at the beginning of the symbol.
621 * This is to be compatible with the standard librairies.
623 if (*S_GET_NAME(symbolP
) == '_') {
625 S_SET_NAME(symbolP
, S_GET_NAME(symbolP
) + 1);
626 } /* strip underscore */
627 #endif /* STRIP_UNDERSCORE */
629 if (S_IS_STRING(symbolP
))
630 SF_SET_STRING(symbolP
);
631 if (!underscore
&& S_IS_LOCAL(symbolP
))
632 SF_SET_LOCAL(symbolP
);
635 } /* obj_symbol_new_hook() */
638 stack
* stack_init(chunk_size
, element_size
)
639 unsigned long chunk_size
;
640 unsigned long element_size
;
644 if ((st
= (stack
*)malloc(sizeof(stack
))) == (stack
*)0)
646 if ((st
->data
= malloc(chunk_size
)) == (char*)0) {
651 st
->size
= chunk_size
;
652 st
->chunk_size
= chunk_size
;
653 st
->element_size
= element_size
;
657 void stack_delete(st
)
664 char *stack_push(st
, element
)
668 if (st
->pointer
+ st
->element_size
>= st
->size
) {
669 st
->size
+= st
->chunk_size
;
670 if ((st
->data
= xrealloc(st
->data
, st
->size
)) == (char*)0)
673 memcpy(st
->data
+ st
->pointer
, element
, st
->element_size
);
674 st
->pointer
+= st
->element_size
;
675 return st
->data
+ st
->pointer
;
681 if ((st
->pointer
-= st
->element_size
) < 0) {
686 return st
->data
+ st
->pointer
;
692 return st
->data
+ st
->pointer
- st
->element_size
;
697 * Handle .ln directives.
700 static void obj_coff_ln()
704 if (def_symbol_in_progress
!= NULL
) {
705 as_warn(".ln pseudo-op inside .def/.endef: ignored.");
706 demand_empty_rest_of_line();
708 } /* wrong context */
711 obstack_next_free(&frags
) - frag_now
->fr_literal
,
712 l
= get_absolute_expression(),
720 listing_source_line(l
+ line_base
- 1);
725 demand_empty_rest_of_line();
727 } /* obj_coff_line() */
732 * Handle .def directives.
734 * One might ask : why can't we symbol_new if the symbol does not
735 * already exist and fill it with debug information. Because of
736 * the C_EFCN special symbol. It would clobber the value of the
737 * function symbol before we have a chance to notice that it is
738 * a C_EFCN. And a second reason is that the code is more clear this
739 * way. (at least I think it is :-).
743 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
744 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
745 *input_line_pointer == '\t') \
746 input_line_pointer++;
749 DEFUN(obj_coff_def
,(what
),
752 char name_end
; /* Char after the end of name */
753 char *symbol_name
; /* Name of the debug symbol */
754 char *symbol_name_copy
; /* Temporary copy of the name */
755 unsigned int symbol_name_length
;
756 /*$char* directiveP;$ */ /* Name of the pseudo opcode */
757 /*$char directive[MAX_DIRECTIVE];$ */ /* Backup of the directive */
758 /*$char end = 0;$ */ /* If 1, stop parsing */
760 if (def_symbol_in_progress
!= NULL
) {
761 as_warn(".def pseudo-op used inside of .def/.endef: ignored.");
762 demand_empty_rest_of_line();
764 } /* if not inside .def/.endef */
768 def_symbol_in_progress
= (symbolS
*) obstack_alloc(¬es
, sizeof(*def_symbol_in_progress
));
769 bzero(def_symbol_in_progress
, sizeof(*def_symbol_in_progress
));
771 symbol_name
= input_line_pointer
;
772 name_end
= get_symbol_end();
773 symbol_name_length
= strlen(symbol_name
);
774 symbol_name_copy
= xmalloc(symbol_name_length
+ 1);
775 strcpy(symbol_name_copy
, symbol_name
);
777 /* Initialize the new symbol */
778 #ifdef STRIP_UNDERSCORE
779 S_SET_NAME(def_symbol_in_progress
, (*symbol_name_copy
== '_'
780 ? symbol_name_copy
+ 1
781 : symbol_name_copy
));
782 #else /* STRIP_UNDERSCORE */
783 S_SET_NAME(def_symbol_in_progress
, symbol_name_copy
);
784 #endif /* STRIP_UNDERSCORE */
785 /* free(symbol_name_copy); */
786 def_symbol_in_progress
->sy_name_offset
= ~0;
787 def_symbol_in_progress
->sy_number
= ~0;
788 def_symbol_in_progress
->sy_frag
= &zero_address_frag
;
790 if (S_IS_STRING(def_symbol_in_progress
)) {
791 SF_SET_STRING(def_symbol_in_progress
);
794 *input_line_pointer
= name_end
;
796 demand_empty_rest_of_line();
798 } /* obj_coff_def() */
800 unsigned int dim_index
;
802 DEFUN_VOID(obj_coff_endef
)
804 symbolS
*symbolP
= 0;
805 /* DIM BUG FIX sac@cygnus.com */
807 if (def_symbol_in_progress
== NULL
) {
808 as_warn(".endef pseudo-op used outside of .def/.endef: ignored.");
809 demand_empty_rest_of_line();
811 } /* if not inside .def/.endef */
813 /* Set the section number according to storage class. */
814 switch (S_GET_STORAGE_CLASS(def_symbol_in_progress
)) {
818 SF_SET_TAG(def_symbol_in_progress
);
819 /* intentional fallthrough */
822 SF_SET_DEBUG(def_symbol_in_progress
);
823 S_SET_SEGMENT(def_symbol_in_progress
, SEG_DEBUG
);
827 SF_SET_LOCAL(def_symbol_in_progress
); /* Do not emit this symbol. */
828 /* intentional fallthrough */
830 SF_SET_PROCESS(def_symbol_in_progress
); /* Will need processing before writing */
831 /* intentional fallthrough */
833 S_SET_SEGMENT(def_symbol_in_progress
, SEG_E0
);
835 if (def_symbol_in_progress
->sy_symbol
.ost_entry
._n
._n_nptr
[1][1] == 'b'
836 && def_symbol_in_progress
->sy_symbol
.ost_entry
._n
._n_nptr
[1][2] == 'f') { /* .bf */
837 if (function_lineoff
< 0) {
838 fprintf(stderr
, "`.bf' symbol without preceding function\n");
839 } /* missing function symbol */
840 SA_GET_SYM_LNNOPTR(last_line_symbol
) = function_lineoff
;
842 SF_SET_PROCESS(last_line_symbol
);
843 function_lineoff
= -1;
849 #endif /* C_AUTOARG */
859 SF_SET_DEBUG(def_symbol_in_progress
);
860 S_SET_SEGMENT(def_symbol_in_progress
, SEG_ABSOLUTE
);
866 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
872 as_warn("unexpected storage class %d", S_GET_STORAGE_CLASS(def_symbol_in_progress
));
874 } /* switch on storage class */
876 /* Now that we have built a debug symbol, try to
877 find if we should merge with an existing symbol
878 or not. If a symbol is C_EFCN or SEG_ABSOLUTE or
879 untagged SEG_DEBUG it never merges. */
881 /* Two cases for functions. Either debug followed
882 by definition or definition followed by debug.
883 For definition first, we will merge the debug
884 symbol into the definition. For debug first, the
885 lineno entry MUST point to the definition
886 function or else it will point off into space
887 when crawl_symbols() merges the debug
888 symbol into the real symbol. Therefor, let's
889 presume the debug symbol is a real function
892 /* FIXME-SOON If for some reason the definition
893 label/symbol is never seen, this will probably
894 leave an undefined symbol at link time. */
896 if (S_GET_STORAGE_CLASS(def_symbol_in_progress
) == C_EFCN
897 || (S_GET_SEGMENT(def_symbol_in_progress
) == SEG_DEBUG
898 && !SF_GET_TAG(def_symbol_in_progress
))
899 || S_GET_SEGMENT(def_symbol_in_progress
) == SEG_ABSOLUTE
900 || (symbolP
= symbol_find_base(S_GET_NAME(def_symbol_in_progress
), DO_NOT_STRIP
)) == NULL
) {
902 symbol_append(def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
905 /* This symbol already exists, merge the
906 newly created symbol into the old one.
907 This is not mandatory. The linker can
908 handle duplicate symbols correctly. But I
909 guess that it save a *lot* of space if
910 the assembly file defines a lot of
913 /* The debug entry (def_symbol_in_progress)
914 is merged into the previous definition. */
916 c_symbol_merge(def_symbol_in_progress
, symbolP
);
917 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
918 def_symbol_in_progress
= symbolP
;
920 if (SF_GET_FUNCTION(def_symbol_in_progress
)
921 || SF_GET_TAG(def_symbol_in_progress
)) {
922 /* For functions, and tags, the symbol *must* be where the debug symbol
923 appears. Move the existing symbol to the current place. */
924 /* If it already is at the end of the symbol list, do nothing */
925 if (def_symbol_in_progress
!= symbol_lastP
) {
926 symbol_remove(def_symbol_in_progress
, &symbol_rootP
, &symbol_lastP
);
927 symbol_append(def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
928 } /* if not already in place */
930 } /* normal or mergable */
932 if (SF_GET_TAG(def_symbol_in_progress
)
933 && symbol_find_base(S_GET_NAME(def_symbol_in_progress
), DO_NOT_STRIP
) == NULL
) {
934 tag_insert(S_GET_NAME(def_symbol_in_progress
), def_symbol_in_progress
);
935 } /* If symbol is a {structure,union} tag, associate symbol to its name. */
937 if (SF_GET_FUNCTION(def_symbol_in_progress
)) {
938 know(sizeof(def_symbol_in_progress
) <= sizeof(long));
940 = c_line_new(def_symbol_in_progress
,0, 0, &zero_address_frag
);
944 SF_SET_PROCESS(def_symbol_in_progress
);
946 if (symbolP
== NULL
) {
947 /* That is, if this is the first
948 time we've seen the function... */
949 symbol_table_insert(def_symbol_in_progress
);
950 } /* definition follows debug */
951 } /* Create the line number entry pointing to the function being defined */
953 def_symbol_in_progress
= NULL
;
954 demand_empty_rest_of_line();
956 } /* obj_coff_endef() */
959 DEFUN_VOID(obj_coff_dim
)
961 register int dim_index
;
963 if (def_symbol_in_progress
== NULL
)
965 as_warn(".dim pseudo-op used outside of .def/.endef: ignored.");
966 demand_empty_rest_of_line();
968 } /* if not inside .def/.endef */
970 S_SET_NUMBER_AUXILIARY(def_symbol_in_progress
, 1);
972 for (dim_index
= 0; dim_index
< DIMNUM
; dim_index
++)
975 SA_SET_SYM_DIMEN(def_symbol_in_progress
, dim_index
, get_absolute_expression());
977 switch (*input_line_pointer
)
981 input_line_pointer
++;
985 as_warn("badly formed .dim directive ignored");
986 /* intentional fallthrough */
991 } /* switch on following character */
992 } /* for each dimension */
994 demand_empty_rest_of_line();
996 } /* obj_coff_dim() */
998 static void obj_coff_line()
1002 if (def_symbol_in_progress
== NULL
) {
1005 } /* if it looks like a stabs style line */
1007 this_base
= get_absolute_expression();
1008 if (this_base
> line_base
)
1010 line_base
= this_base
;
1018 listing_source_line(line_base
);
1022 S_SET_NUMBER_AUXILIARY(def_symbol_in_progress
, 1);
1023 SA_SET_SYM_LNNO(def_symbol_in_progress
, line_base
);
1025 demand_empty_rest_of_line();
1027 } /* obj_coff_line() */
1029 static void obj_coff_size() {
1030 if (def_symbol_in_progress
== NULL
) {
1031 as_warn(".size pseudo-op used outside of .def/.endef ignored.");
1032 demand_empty_rest_of_line();
1034 } /* if not inside .def/.endef */
1036 S_SET_NUMBER_AUXILIARY(def_symbol_in_progress
, 1);
1037 SA_SET_SYM_SIZE(def_symbol_in_progress
, get_absolute_expression());
1038 demand_empty_rest_of_line();
1040 } /* obj_coff_size() */
1042 static void obj_coff_scl() {
1043 if (def_symbol_in_progress
== NULL
) {
1044 as_warn(".scl pseudo-op used outside of .def/.endef ignored.");
1045 demand_empty_rest_of_line();
1047 } /* if not inside .def/.endef */
1049 S_SET_STORAGE_CLASS(def_symbol_in_progress
, get_absolute_expression());
1050 demand_empty_rest_of_line();
1052 } /* obj_coff_scl() */
1054 static void obj_coff_tag() {
1058 if (def_symbol_in_progress
== NULL
) {
1059 as_warn(".tag pseudo-op used outside of .def/.endef ignored.");
1060 demand_empty_rest_of_line();
1062 } /* if not inside .def/.endef */
1064 S_SET_NUMBER_AUXILIARY(def_symbol_in_progress
, 1);
1065 symbol_name
= input_line_pointer
;
1066 name_end
= get_symbol_end();
1068 /* Assume that the symbol referred to by .tag is always defined. */
1069 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1070 SA_SET_SYM_TAGNDX(def_symbol_in_progress
, (long) tag_find_or_make(symbol_name
));
1071 if (SA_GET_SYM_TAGNDX(def_symbol_in_progress
) == 0L) {
1072 as_warn("tag not found for .tag %s", symbol_name
);
1075 SF_SET_TAGGED(def_symbol_in_progress
);
1076 *input_line_pointer
= name_end
;
1078 demand_empty_rest_of_line();
1080 } /* obj_coff_tag() */
1082 static void obj_coff_type() {
1083 if (def_symbol_in_progress
== NULL
) {
1084 as_warn(".type pseudo-op used outside of .def/.endef ignored.");
1085 demand_empty_rest_of_line();
1087 } /* if not inside .def/.endef */
1089 S_SET_DATA_TYPE(def_symbol_in_progress
, get_absolute_expression());
1091 if (ISFCN(S_GET_DATA_TYPE(def_symbol_in_progress
)) &&
1092 S_GET_STORAGE_CLASS(def_symbol_in_progress
) != C_TPDEF
) {
1093 SF_SET_FUNCTION(def_symbol_in_progress
);
1094 } /* is a function */
1096 demand_empty_rest_of_line();
1098 } /* obj_coff_type() */
1100 static void obj_coff_val() {
1101 if (def_symbol_in_progress
== NULL
) {
1102 as_warn(".val pseudo-op used outside of .def/.endef ignored.");
1103 demand_empty_rest_of_line();
1105 } /* if not inside .def/.endef */
1107 if (is_name_beginner(*input_line_pointer
)) {
1108 char *symbol_name
= input_line_pointer
;
1109 char name_end
= get_symbol_end();
1111 if (!strcmp(symbol_name
, ".")) {
1112 def_symbol_in_progress
->sy_frag
= frag_now
;
1113 S_SET_VALUE(def_symbol_in_progress
, obstack_next_free(&frags
) - frag_now
->fr_literal
);
1114 /* If the .val is != from the .def (e.g. statics) */
1115 } else if (strcmp(S_GET_NAME(def_symbol_in_progress
), symbol_name
)) {
1116 def_symbol_in_progress
->sy_forward
= symbol_find_or_make(symbol_name
);
1118 /* If the segment is undefined when the forward
1119 reference is solved, then copy the segment id
1120 from the forward symbol. */
1121 SF_SET_GET_SEGMENT(def_symbol_in_progress
);
1123 /* Otherwise, it is the name of a non debug symbol and its value will be calculated later. */
1124 *input_line_pointer
= name_end
;
1126 S_SET_VALUE(def_symbol_in_progress
, get_absolute_expression());
1127 } /* if symbol based */
1129 demand_empty_rest_of_line();
1131 } /* obj_coff_val() */
1134 * Maintain a list of the tagnames of the structres.
1137 static void tag_init() {
1138 tag_hash
= hash_new();
1142 static void tag_insert(name
, symbolP
)
1146 register char * error_string
;
1148 if (*(error_string
= hash_jam(tag_hash
, name
, (char *)symbolP
))) {
1149 as_fatal("Inserting \"%s\" into structure table failed: %s",
1150 name
, error_string
);
1153 } /* tag_insert() */
1155 static symbolS
*tag_find_or_make(name
)
1160 if ((symbolP
= tag_find(name
)) == NULL
) {
1161 symbolP
= symbol_new(name
,
1164 &zero_address_frag
);
1166 tag_insert(S_GET_NAME(symbolP
), symbolP
);
1167 symbol_table_insert(symbolP
);
1171 } /* tag_find_or_make() */
1173 static symbolS
*tag_find(name
)
1176 #ifdef STRIP_UNDERSCORE
1177 if (*name
== '_') name
++;
1178 #endif /* STRIP_UNDERSCORE */
1179 return((symbolS
*)hash_find(tag_hash
, name
));
1182 void obj_read_begin_hook() {
1183 /* These had better be the same. Usually 18 bytes. */
1185 know(sizeof(SYMENT
) == sizeof(AUXENT
));
1186 know(SYMESZ
== AUXESZ
);
1191 } /* obj_read_begin_hook() */
1193 /* This function runs through the symbol table and puts all the
1194 externals onto another chain */
1196 /* The chain of externals */
1197 symbolS
*symbol_externP
= NULL
;
1198 symbolS
*symbol_extern_lastP
= NULL
;
1201 symbolS
*last_functionP
= NULL
;
1205 static unsigned int DEFUN_VOID(yank_symbols
)
1208 unsigned int symbol_number
=0;
1210 for (symbolP
= symbol_rootP
;
1212 symbolP
= symbolP
? symbol_next(symbolP
) : symbol_rootP
) {
1213 if (!SF_GET_DEBUG(symbolP
)) {
1214 /* Debug symbols do not need all this rubbish */
1215 symbolS
* real_symbolP
;
1217 /* L* and C_EFCN symbols never merge. */
1218 if (!SF_GET_LOCAL(symbolP
)
1219 && (real_symbolP
= symbol_find_base(S_GET_NAME(symbolP
), DO_NOT_STRIP
))
1220 && real_symbolP
!= symbolP
) {
1221 /* FIXME-SOON: where do dups come from?
1222 Maybe tag references before definitions? xoxorich. */
1223 /* Move the debug data from the debug symbol to the
1224 real symbol. Do NOT do the oposite (i.e. move from
1225 real symbol to debug symbol and remove real symbol from the
1226 list.) Because some pointers refer to the real symbol
1227 whereas no pointers refer to the debug symbol. */
1228 c_symbol_merge(symbolP
, real_symbolP
);
1229 /* Replace the current symbol by the real one */
1230 /* The symbols will never be the last or the first
1231 because : 1st symbol is .file and 3 last symbols are
1232 .text, .data, .bss */
1233 symbol_remove(real_symbolP
, &symbol_rootP
, &symbol_lastP
);
1234 symbol_insert(real_symbolP
, symbolP
, &symbol_rootP
, &symbol_lastP
);
1235 symbol_remove(symbolP
, &symbol_rootP
, &symbol_lastP
);
1236 symbolP
= real_symbolP
;
1237 } /* if not local but dup'd */
1239 if (flagseen
['R'] && (S_GET_SEGMENT(symbolP
) == SEG_E1
)) {
1240 S_SET_SEGMENT(symbolP
, SEG_E0
);
1241 } /* push data into text */
1243 S_SET_VALUE(symbolP
,
1244 S_GET_VALUE(symbolP
) + symbolP
->sy_frag
->fr_address
);
1246 if (!S_IS_DEFINED(symbolP
) && !SF_GET_LOCAL(symbolP
))
1248 S_SET_EXTERNAL(symbolP
);
1250 else if (S_GET_STORAGE_CLASS(symbolP
) == C_NULL
)
1252 if (S_GET_SEGMENT(symbolP
) == SEG_E0
)
1254 S_SET_STORAGE_CLASS(symbolP
, C_LABEL
);
1258 S_SET_STORAGE_CLASS(symbolP
, C_STAT
);
1262 /* Mainly to speed up if not -g */
1263 if (SF_GET_PROCESS(symbolP
))
1265 /* Handle the nested blocks auxiliary info. */
1266 if (S_GET_STORAGE_CLASS(symbolP
) == C_BLOCK
) {
1267 if (!strcmp(S_GET_NAME(symbolP
), ".bb"))
1268 stack_push(block_stack
, (char *) &symbolP
);
1270 register symbolS
* begin_symbolP
;
1271 begin_symbolP
= *(symbolS
**)stack_pop(block_stack
);
1272 if (begin_symbolP
== (symbolS
*)0)
1273 as_warn("mismatched .eb");
1275 SA_SET_SYM_ENDNDX(begin_symbolP
, symbol_number
+2);
1278 /* If we are able to identify the type of a function, and we
1279 are out of a function (last_functionP == 0) then, the
1280 function symbol will be associated with an auxiliary
1282 if (last_functionP
== (symbolS
*)0 &&
1283 SF_GET_FUNCTION(symbolP
)) {
1284 last_functionP
= symbolP
;
1286 if (S_GET_NUMBER_AUXILIARY(symbolP
) < 1) {
1287 S_SET_NUMBER_AUXILIARY(symbolP
, 1);
1288 } /* make it at least 1 */
1290 /* Clobber possible stale .dim information. */
1292 /* Iffed out by steve - this fries the lnnoptr info too */
1293 bzero(symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
,
1294 sizeof(symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
));
1297 /* The C_FCN doesn't need any additional information.
1298 I don't even know if this is needed for sdb. But the
1299 standard assembler generates it, so...
1301 if (S_GET_STORAGE_CLASS(symbolP
) == C_EFCN
) {
1302 if (last_functionP
== (symbolS
*)0)
1303 as_fatal("C_EFCN symbol out of scope");
1304 SA_SET_SYM_FSIZE(last_functionP
,
1305 (long)(S_GET_VALUE(symbolP
) -
1306 S_GET_VALUE(last_functionP
)));
1307 SA_SET_SYM_ENDNDX(last_functionP
, symbol_number
);
1308 last_functionP
= (symbolS
*)0;
1311 } else if (SF_GET_TAG(symbolP
)) {
1312 /* First descriptor of a structure must point to
1313 the first slot after the structure description. */
1314 last_tagP
= symbolP
;
1316 } else if (S_GET_STORAGE_CLASS(symbolP
) == C_EOS
) {
1317 /* +2 take in account the current symbol */
1318 SA_SET_SYM_ENDNDX(last_tagP
, symbol_number
+ 2);
1319 } else if (S_GET_STORAGE_CLASS(symbolP
) == C_FILE
) {
1320 if (S_GET_VALUE(symbolP
)) {
1321 S_SET_VALUE((symbolS
*) S_GET_VALUE(symbolP
), symbol_number
);
1322 S_SET_VALUE(symbolP
, 0);
1323 } /* no one points at the first .file symbol */
1324 } /* if debug or tag or eos or file */
1326 /* We must put the external symbols apart. The loader
1327 does not bomb if we do not. But the references in
1328 the endndx field for a .bb symbol are not corrected
1329 if an external symbol is removed between .bb and .be.
1330 I.e in the following case :
1331 [20] .bb endndx = 22
1334 ld will move the symbol 21 to the end of the list but
1335 endndx will still be 22 instead of 21. */
1338 if (SF_GET_LOCAL(symbolP
)) {
1339 /* remove C_EFCN and LOCAL (L...) symbols */
1340 /* next pointer remains valid */
1341 symbol_remove(symbolP
, &symbol_rootP
, &symbol_lastP
);
1344 else if (!S_IS_DEFINED(symbolP
)
1345 && !S_IS_DEBUG(symbolP
)
1346 && !SF_GET_STATICS(symbolP
) &&
1347 S_GET_STORAGE_CLASS(symbolP
) == C_EXT
)
1348 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1349 /* if external, Remove from the list */
1350 symbolS
*hold
= symbol_previous(symbolP
);
1352 symbol_remove(symbolP
, &symbol_rootP
, &symbol_lastP
);
1353 symbol_clear_list_pointers(symbolP
);
1354 symbol_append(symbolP
, symbol_extern_lastP
, &symbol_externP
, &symbol_extern_lastP
);
1357 if (SF_GET_STRING(symbolP
)) {
1358 symbolP
->sy_name_offset
= string_byte_count
;
1359 string_byte_count
+= strlen(S_GET_NAME(symbolP
)) + 1;
1361 symbolP
->sy_name_offset
= 0;
1362 } /* fix "long" names */
1364 symbolP
->sy_number
= symbol_number
;
1365 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY(symbolP
);
1366 } /* if local symbol */
1367 } /* traverse the symbol list */
1368 return symbol_number
;
1373 static unsigned int DEFUN_VOID(glue_symbols
)
1375 unsigned int symbol_number
= 0;
1377 for (symbolP
= symbol_externP
; symbol_externP
;) {
1378 symbolS
*tmp
= symbol_externP
;
1381 symbol_remove(tmp
, &symbol_externP
, &symbol_extern_lastP
);
1382 symbol_append(tmp
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
1385 if (SF_GET_STRING(tmp
)) {
1386 tmp
->sy_name_offset
= string_byte_count
;
1387 string_byte_count
+= strlen(S_GET_NAME(tmp
)) + 1;
1389 tmp
->sy_name_offset
= 0;
1390 } /* fix "long" names */
1392 tmp
->sy_number
= symbol_number
;
1393 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY(tmp
);
1394 } /* append the entire extern chain */
1395 return symbol_number
;
1399 static unsigned int DEFUN_VOID(tie_tags
)
1401 unsigned int symbol_number
= 0;
1404 for (symbolP
= symbol_rootP
; symbolP
; symbolP
=
1405 symbol_next(symbolP
))
1407 symbolP
->sy_number
= symbol_number
;
1411 if (SF_GET_TAGGED(symbolP
))
1415 ((symbolS
*) SA_GET_SYM_TAGNDX(symbolP
))->sy_number
);
1418 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY(symbolP
);
1420 return symbol_number
;
1425 DEFUN(crawl_symbols
,(headers
, abfd
),
1426 struct internal_filehdr
*headers AND
1431 unsigned int ptr
= 0;
1436 /* Initialize the stack used to keep track of the matching .bb .be */
1438 block_stack
= stack_init(512, sizeof(symbolS
*));
1439 /* JF deal with forward references first... */
1440 for (symbolP
= symbol_rootP
;
1442 symbolP
= symbol_next(symbolP
))
1445 if (symbolP
->sy_forward
) {
1446 S_SET_VALUE(symbolP
, (S_GET_VALUE(symbolP
)
1447 + S_GET_VALUE(symbolP
->sy_forward
)
1448 + symbolP
->sy_forward
->sy_frag
->fr_address
));
1450 if (SF_GET_GET_SEGMENT(symbolP
)) {
1451 S_SET_SEGMENT(symbolP
, S_GET_SEGMENT(symbolP
->sy_forward
));
1452 } /* forward segment also */
1454 symbolP
->sy_forward
=0;
1455 } /* if it has a forward reference */
1456 } /* walk the symbol chain */
1459 /* The symbol list should be ordered according to the following sequence
1462 * . debug entries for functions
1463 * . fake symbols for the sections, including.text .data and .bss
1465 * . undefined symbols
1466 * But this is not mandatory. The only important point is to put the
1467 * undefined symbols at the end of the list.
1470 if (symbol_rootP
== NULL
1471 || S_GET_STORAGE_CLASS(symbol_rootP
) != C_FILE
) {
1472 c_dot_file_symbol("fake");
1474 /* Is there a .file symbol ? If not insert one at the beginning. */
1477 * Build up static symbols for the sections, they are filled in later
1481 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
1483 if (segment_info
[i
].scnhdr
.s_name
[0])
1485 segment_info
[i
].dot
=
1486 c_section_symbol(segment_info
[i
].scnhdr
.s_name
,
1493 /* Take all the externals out and put them into another chain */
1494 headers
->f_nsyms
= yank_symbols();
1495 /* Take the externals and glue them onto the end.*/
1496 headers
->f_nsyms
+= glue_symbols();
1498 headers
->f_nsyms
= tie_tags();
1499 know(symbol_externP
== NULL
);
1500 know(symbol_extern_lastP
== NULL
);
1506 * Find strings by crawling along symbol table chain.
1509 void DEFUN(w_strings
,(where
),
1514 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
1515 md_number_to_chars(where
, string_byte_count
, sizeof(string_byte_count
));
1516 where
+= sizeof(string_byte_count
);
1517 for (symbolP
= symbol_rootP
;
1519 symbolP
= symbol_next(symbolP
))
1523 if (SF_GET_STRING(symbolP
)) {
1524 size
= strlen(S_GET_NAME(symbolP
)) + 1;
1526 memcpy(where
, S_GET_NAME(symbolP
),size
);
1539 DEFUN(do_linenos_for
,(abfd
, file_cursor
),
1541 unsigned long *file_cursor
)
1545 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
1547 segment_info_type
*s
= segment_info
+ idx
;
1550 if (s
->scnhdr
.s_nlnno
!= 0)
1552 struct lineno_list
*line_ptr
;
1554 struct external_lineno
*buffer
=
1555 (struct external_lineno
*)xmalloc(s
->scnhdr
.s_nlnno
* LINESZ
);
1557 struct external_lineno
*dst
= buffer
;
1559 /* Run through the table we've built and turn it into its external
1560 form, take this chance to remove duplicates */
1562 for (line_ptr
= s
->lineno_list_head
;
1563 line_ptr
!= (struct lineno_list
*)NULL
;
1564 line_ptr
= line_ptr
->next
)
1567 if (line_ptr
->line
.l_lnno
== 0)
1569 /* Turn a pointer to a symbol into the symbols' index */
1570 line_ptr
->line
.l_addr
.l_symndx
=
1571 ( (symbolS
*)line_ptr
->line
.l_addr
.l_symndx
)->sy_number
;
1575 line_ptr
->line
.l_addr
.l_paddr
+= ((struct frag
* )(line_ptr
->frag
))->fr_address
;
1579 (void) bfd_coff_swap_lineno_out(abfd
, &(line_ptr
->line
), dst
);
1584 s
->scnhdr
.s_lnnoptr
= *file_cursor
;
1586 bfd_write(buffer
, 1, s
->scnhdr
.s_nlnno
* LINESZ
, abfd
);
1589 *file_cursor
+= s
->scnhdr
.s_nlnno
* LINESZ
;
1595 /* Now we run through the list of frag chains in a segment and
1596 make all the subsegment frags appear at the end of the
1597 list, as if the seg 0 was extra long */
1599 static void DEFUN_VOID(remove_subsegs
)
1603 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1605 frchainS
*head
= segment_info
[i
].frchainP
;
1607 fragS
* prev_frag
= &dummy
;
1609 while (head
&& head
->frch_seg
== i
)
1611 prev_frag
->fr_next
= head
->frch_root
;
1612 prev_frag
= head
->frch_last
;
1613 head
= head
->frch_next
;
1615 prev_frag
->fr_next
= 0;
1620 extern void DEFUN_VOID(write_object_file
)
1623 struct frchain
*frchain_ptr
;
1625 struct internal_filehdr filehdr
;
1626 struct internal_aouthdr aouthdr
;
1627 unsigned long file_cursor
;
1629 unsigned int addr
= 0;
1630 abfd
= bfd_openw(out_file_name
, TARGET_FORMAT
);
1634 as_perror ("FATAL: Can't create %s", out_file_name
);
1637 bfd_set_format(abfd
, bfd_object
);
1638 bfd_set_arch_mach(abfd
, BFD_ARCH
, 0);
1642 string_byte_count
= 4;
1644 for (frchain_ptr
= frchain_root
;
1645 frchain_ptr
!= (struct frchain
*)NULL
;
1646 frchain_ptr
= frchain_ptr
->frch_next
) {
1647 /* Run through all the sub-segments and align them up. Also close any
1648 open frags. We tack a .fill onto the end of the frag chain so
1649 that any .align's size can be worked by looking at the next
1652 subseg_new(frchain_ptr
->frch_seg
, frchain_ptr
->frch_subseg
);
1653 #define SUB_SEGMENT_ALIGN 1
1654 frag_align(SUB_SEGMENT_ALIGN
,0);
1655 frag_wane(frag_now
);
1656 frag_now
->fr_fix
= 0;
1657 know( frag_now
->fr_next
== NULL
);
1664 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1666 relax_segment(segment_info
[i
].frchainP
->frch_root
, i
);
1673 filehdr
.f_nscns
= 0;
1675 /* Find out how big the sections are */
1676 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1679 if (segment_info
[i
].scnhdr
.s_name
[0])
1686 /* THis is a special case, we leave the size alone, which will have */
1687 /* been made up from all and any lcomms seen */
1691 addr
+= size_section(abfd
, i
);
1697 /* Turn the gas native symbol table shape into a coff symbol table */
1698 crawl_symbols(&filehdr
, abfd
);
1700 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1702 fixup_segment(segment_info
[i
].fix_root
, i
);
1706 file_cursor
= FILHSZ
+ SCNHSZ
* filehdr
.f_nscns
;
1708 bfd_seek(abfd
, file_cursor
, 0);
1711 do_relocs_for(abfd
, &file_cursor
);
1713 do_linenos_for(abfd
, &file_cursor
);
1716 /* Plant the data */
1718 fill_section(abfd
,&filehdr
, &file_cursor
);
1722 filehdr
.f_magic
= COFF_MAGIC
;
1723 filehdr
.f_timdat
= time(0);
1724 filehdr
.f_flags
= COFF_FLAGS
;
1728 filehdr
.f_flags
|= F_LNNO
;
1732 filehdr
.f_flags
|= F_RELFLG
;
1743 unsigned int symtable_size
= filehdr
.f_nsyms
* SYMESZ
;
1744 char *buffer1
= malloc(symtable_size
+ string_byte_count
+ 4);
1745 char *ptr
= buffer1
;
1746 filehdr
.f_symptr
= bfd_tell(abfd
);
1747 w_symbols(abfd
, buffer1
, symbol_rootP
);
1748 w_strings(buffer1
+ symtable_size
);
1749 bfd_write(buffer1
, 1,symtable_size
+ string_byte_count
+ 4, abfd
);
1753 coff_header_append(abfd
, &filehdr
, &aouthdr
);
1755 bfd_close_all_done(abfd
);
1759 static void DEFUN(change_to_section
,(name
, len
, exp
),
1761 unsigned int len AND
1765 /* Find out if we've already got a section of this name etc */
1766 for(i
= SEG_E0
; i
< SEG_E9
&& segment_info
[i
].scnhdr
.s_name
[0] ; i
++)
1768 if (strncmp(segment_info
[i
].scnhdr
.s_name
, name
, len
) == 0)
1775 /* No section, add one */
1776 strncpy(segment_info
[i
].scnhdr
.s_name
, name
, 8);
1781 DEFUN_VOID(obj_coff_section
)
1783 /* Strip out the section name */
1784 char *section_name
;
1785 char *section_name_end
;
1791 section_name
= input_line_pointer
;
1792 c
= get_symbol_end();
1793 section_name_end
= input_line_pointer
;
1795 len
= section_name_end
- section_name
;
1796 input_line_pointer
++;
1800 exp
= get_absolute_expression();
1802 else if ( *input_line_pointer
== ',')
1805 input_line_pointer
++;
1806 exp
= get_absolute_expression();
1813 change_to_section(section_name
, len
,exp
);
1814 *section_name_end
= c
;
1819 static void obj_coff_text()
1821 change_to_section(".text",5, get_absolute_expression());
1825 static void obj_coff_data()
1827 change_to_section(".data",5, get_absolute_expression());
1830 void c_symbol_merge(debug
, normal
)
1834 S_SET_DATA_TYPE(normal
, S_GET_DATA_TYPE(debug
));
1835 S_SET_STORAGE_CLASS(normal
, S_GET_STORAGE_CLASS(debug
));
1837 if (S_GET_NUMBER_AUXILIARY(debug
) > S_GET_NUMBER_AUXILIARY(normal
)) {
1838 S_SET_NUMBER_AUXILIARY(normal
, S_GET_NUMBER_AUXILIARY(debug
));
1839 } /* take the most we have */
1841 if (S_GET_NUMBER_AUXILIARY(debug
) > 0) {
1842 memcpy((char*)&normal
->sy_symbol
.ost_auxent
[0], (char*)&debug
->sy_symbol
.ost_auxent
[0], S_GET_NUMBER_AUXILIARY(debug
) * AUXESZ
);
1843 } /* Move all the auxiliary information */
1845 /* Move the debug flags. */
1846 SF_SET_DEBUG_FIELD(normal
, SF_GET_DEBUG_FIELD(debug
));
1847 } /* c_symbol_merge() */
1850 DEFUN(c_line_new
,(symbol
, paddr
, line_number
, frag
),
1853 unsigned short line_number AND
1856 struct lineno_list
* new_line
=
1857 (struct lineno_list
*)xmalloc(sizeof(struct lineno_list
));
1859 segment_info_type
*s
= segment_info
+ now_seg
;
1860 new_line
->line
.l_lnno
= line_number
;
1864 if (line_number
== 0)
1866 last_line_symbol
= symbol
;
1867 new_line
->line
.l_addr
.l_symndx
= (long)symbol
;
1871 new_line
->line
.l_addr
.l_paddr
= paddr
;
1874 new_line
->frag
= (char*)frag
;
1875 new_line
->next
= (struct lineno_list
*)NULL
;
1878 if (s
->lineno_list_head
== (struct lineno_list
*)NULL
)
1880 s
->lineno_list_head
= new_line
;
1884 s
->lineno_list_tail
->next
= new_line
;
1886 s
->lineno_list_tail
= new_line
;
1887 return LINESZ
* s
->scnhdr
.s_nlnno
++;
1890 void c_dot_file_symbol(filename
)
1895 symbolP
= symbol_new(".file",
1898 &zero_address_frag
);
1900 S_SET_STORAGE_CLASS(symbolP
, C_FILE
);
1901 S_SET_NUMBER_AUXILIARY(symbolP
, 1);
1902 SA_SET_FILE_FNAME(symbolP
, filename
);
1908 listing_source_file(filename
);
1914 SF_SET_DEBUG(symbolP
);
1915 S_SET_VALUE(symbolP
, (long) previous_file_symbol
);
1917 previous_file_symbol
= symbolP
;
1919 /* Make sure that the symbol is first on the symbol chain */
1920 if (symbol_rootP
!= symbolP
) {
1921 if (symbolP
== symbol_lastP
) {
1922 symbol_lastP
= symbol_lastP
->sy_previous
;
1923 } /* if it was the last thing on the list */
1925 symbol_remove(symbolP
, &symbol_rootP
, &symbol_lastP
);
1926 symbol_insert(symbolP
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
1927 symbol_rootP
= symbolP
;
1928 } /* if not first on the list */
1930 } /* c_dot_file_symbol() */
1933 * Build a 'section static' symbol.
1936 symbolS
*c_section_symbol(name
,idx
)
1942 symbolP
= symbol_new(name
,idx
,
1944 &zero_address_frag
);
1946 S_SET_STORAGE_CLASS(symbolP
, C_STAT
);
1947 S_SET_NUMBER_AUXILIARY(symbolP
, 1);
1949 SF_SET_STATICS(symbolP
);
1952 } /* c_section_symbol() */
1955 DEFUN(w_symbols
,(abfd
, where
, symbol_rootP
),
1958 symbolS
*symbol_rootP
)
1963 /* First fill in those values we have only just worked out */
1964 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
1966 symbolP
= segment_info
[i
].dot
;
1970 SA_SET_SCN_SCNLEN(symbolP
, segment_info
[i
].scnhdr
.s_size
);
1971 SA_SET_SCN_NRELOC(symbolP
, segment_info
[i
].scnhdr
.s_nreloc
);
1972 SA_SET_SCN_NLINNO(symbolP
, segment_info
[i
].scnhdr
.s_nlnno
);
1978 * Emit all symbols left in the symbol chain.
1980 for (symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next(symbolP
)) {
1981 /* Used to save the offset of the name. It is used to point
1982 to the string in memory but must be a file offset. */
1983 register char * temp
;
1985 tc_coff_symbol_emit_hook(symbolP
);
1987 temp
= S_GET_NAME(symbolP
);
1988 if (SF_GET_STRING(symbolP
)) {
1989 S_SET_OFFSET(symbolP
, symbolP
->sy_name_offset
);
1990 S_SET_ZEROES(symbolP
, 0);
1992 bzero(symbolP
->sy_symbol
.ost_entry
.n_name
, SYMNMLEN
);
1993 strncpy(symbolP
->sy_symbol
.ost_entry
.n_name
, temp
, SYMNMLEN
);
1995 where
= symbol_to_chars(abfd
, where
, symbolP
);
1996 S_SET_NAME(symbolP
,temp
);
2001 static void DEFUN_VOID(obj_coff_lcomm
)
2010 name
= input_line_pointer
;
2014 c
= get_symbol_end();
2015 p
= input_line_pointer
;
2018 if (*input_line_pointer
!= ',') {
2019 as_bad("Expected comma after name");
2020 ignore_rest_of_line();
2023 if (*input_line_pointer
== '\n') {
2024 as_bad("Missing size expression");
2027 input_line_pointer
++;
2028 if ((temp
= get_absolute_expression ()) < 0) {
2029 as_warn("lcomm length (%d.) <0! Ignored.", temp
);
2030 ignore_rest_of_line();
2034 symbolP
= symbol_find_or_make(name
);
2035 vma
= segment_info
[SEG_E2
].scnhdr
.s_size
;
2036 vma
+= relax_align(vma
, MIN(8, temp
));
2037 S_SET_VALUE(symbolP
,vma
);
2038 S_SET_SEGMENT(symbolP
, SEG_E2
);
2039 segment_info
[SEG_E2
].scnhdr
.s_size
= vma
+ temp
;
2040 S_SET_STORAGE_CLASS(symbolP
, C_STAT
);
2041 demand_empty_rest_of_line();
2046 static void DEFUN(fixup_segment
,(fixP
, this_segment_type
),
2047 register fixS
* fixP AND
2048 segT this_segment_type
)
2050 register symbolS
*add_symbolP
;
2051 register symbolS
*sub_symbolP
;
2052 register long add_number
;
2054 register char *place
;
2055 register long where
;
2056 register char pcrel
;
2057 register fragS
*fragP
;
2058 register segT add_symbol_segment
= SEG_ABSOLUTE
;
2061 for ( ; fixP
; fixP
= fixP
->fx_next
)
2063 fragP
= fixP
->fx_frag
;
2065 where
= fixP
->fx_where
;
2066 place
= fragP
->fr_literal
+ where
;
2067 size
= fixP
->fx_size
;
2068 add_symbolP
= fixP
->fx_addsy
;
2070 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME(add_symbolP
)) {
2071 /* Relocation should be done via the
2072 associated 'bal' entry point
2075 if (!TC_S_IS_BALNAME(tc_get_bal_of_call(add_symbolP
))) {
2076 as_bad("No 'bal' entry point for leafproc %s",
2077 S_GET_NAME(add_symbolP
));
2080 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call(add_symbolP
);
2081 } /* callj relocation */
2083 sub_symbolP
= fixP
->fx_subsy
;
2084 add_number
= fixP
->fx_offset
;
2085 pcrel
= fixP
->fx_pcrel
;
2088 add_symbol_segment
= S_GET_SEGMENT(add_symbolP
);
2089 } /* if there is an addend */
2094 if (S_GET_SEGMENT(sub_symbolP
) != SEG_ABSOLUTE
) {
2095 as_bad("Negative of non-absolute symbol %s", S_GET_NAME(sub_symbolP
));
2096 } /* not absolute */
2098 add_number
-= S_GET_VALUE(sub_symbolP
);
2100 /* if sub_symbol is in the same segment that add_symbol
2101 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
2102 } else if ((S_GET_SEGMENT(sub_symbolP
) == add_symbol_segment
)
2103 && (SEG_NORMAL(add_symbol_segment
)
2104 || (add_symbol_segment
== SEG_ABSOLUTE
))) {
2105 /* Difference of 2 symbols from same segment. */
2106 /* Can't make difference of 2 undefineds: 'value' means */
2107 /* something different for N_UNDF. */
2109 /* Makes no sense to use the difference of 2 arbitrary symbols
2110 * as the target of a call instruction.
2112 if (fixP
->fx_callj
) {
2113 as_bad("callj to difference of 2 symbols");
2115 #endif /* TC_I960 */
2116 add_number
+= S_GET_VALUE(add_symbolP
) -
2117 S_GET_VALUE(sub_symbolP
);
2120 fixP
->fx_addsy
= NULL
;
2122 /* Different segments in subtraction. */
2123 know(!(S_IS_EXTERNAL(sub_symbolP
) && (S_GET_SEGMENT(sub_symbolP
) == SEG_ABSOLUTE
)));
2125 if ((S_GET_SEGMENT(sub_symbolP
) == SEG_ABSOLUTE
)) {
2126 add_number
-= S_GET_VALUE(sub_symbolP
);
2128 as_bad("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
2129 segment_name(S_GET_SEGMENT(sub_symbolP
)),
2130 S_GET_NAME(sub_symbolP
), fragP
->fr_address
+ where
);
2133 } /* if sub_symbolP */
2136 if (add_symbol_segment
== this_segment_type
&& pcrel
) {
2138 * This fixup was made when the symbol's segment was
2139 * SEG_UNKNOWN, but it is now in the local segment.
2140 * So we know how to do the address without relocation.
2143 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
2144 * in which cases it modifies *fixP as appropriate. In the case
2145 * of a 'calls', no further work is required, and *fixP has been
2146 * set up to make the rest of the code below a no-op.
2149 #endif /* TC_I960 */
2151 add_number
+= S_GET_VALUE(add_symbolP
);
2152 add_number
-= md_pcrel_from (fixP
);
2153 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
2154 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2157 switch (add_symbol_segment
)
2161 reloc_callj(fixP
); /* See comment about reloc_callj() above*/
2162 #endif /* TC_I960 */
2163 add_number
+= S_GET_VALUE(add_symbolP
);
2164 fixP
->fx_addsy
= NULL
;
2169 add_number
+= S_GET_VALUE(add_symbolP
) +
2170 segment_info
[S_GET_SEGMENT(add_symbolP
)].scnhdr
.s_paddr
;
2175 if ((int)fixP
->fx_bit_fixP
== 13) {
2176 /* This is a COBR instruction. They have only a
2177 * 13-bit displacement and are only to be used
2178 * for local branches: flag as error, don't generate
2181 as_bad("can't use COBR format with external label");
2182 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2185 #endif /* TC_I960 */
2192 } /* switch on symbol seg */
2193 } /* if not in local seg */
2194 } /* if there was a + symbol */
2197 add_number
-= md_pcrel_from(fixP
);
2198 if (add_symbolP
== 0) {
2199 fixP
->fx_addsy
= & abs_symbol
;
2200 } /* if there's an add_symbol */
2203 if (!fixP
->fx_bit_fixP
) {
2205 (add_number
& ~0xFF) && (add_number
&~0xFF!=(-1&~0xFF))) ||
2207 (add_number
& ~0xFFFF) && (add_number
&~0xFFFF!=(-1&~0xFFFF)))) {
2208 as_bad("Value of %d too large for field of %d bytes at 0x%x",
2209 add_number
, size
, fragP
->fr_address
+ where
);
2210 } /* generic error checking */
2211 } /* not a bit fix */
2212 /* once this fix has been applied, we don't have to output anything
2213 nothing more need be done -*/
2214 md_apply_fix(fixP
, add_number
);
2216 } /* For each fixS in this segment. */
2219 } /* fixup_segment() */