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"
44 #include "../bfd/libcoff.h"
46 /* The NOP_OPCODE is for the alignment fill value. Fill with nop so
47 that we can stick sections together without causing trouble. */
49 #define NOP_OPCODE 0x00
52 #define MIN(a,b) ((a) < (b)? (a) : (b))
53 /* This vector is used to turn an internal segment into a section #
54 suitable for insertion into a coff symbol table
57 const short seg_N_TYPE
[] =
58 { /* in: segT out: N_TYPE bits */
70 C_UNDEF_SECTION
, /* SEG_UNKNOWN */
71 C_UNDEF_SECTION
, /* SEG_ABSENT */
72 C_UNDEF_SECTION
, /* SEG_PASS1 */
73 C_UNDEF_SECTION
, /* SEG_GOOF */
74 C_UNDEF_SECTION
, /* SEG_BIG */
75 C_UNDEF_SECTION
, /* SEG_DIFFERENCE */
76 C_DEBUG_SECTION
, /* SEG_DEBUG */
77 C_NTV_SECTION
, /* SEG_NTV */
78 C_PTV_SECTION
, /* SEG_PTV */
79 C_REGISTER_SECTION
, /* SEG_REGISTER */
83 int function_lineoff
= -1; /* Offset in line#s where the last function
84 started (the odd entry for line #0) */
90 static symbolS
*last_line_symbol
;
91 /* Add 4 to the real value to get the index and compensate the
92 negatives. This vector is used by S_GET_SEGMENT to turn a coff
93 section number into a segment number
95 static symbolS
*previous_file_symbol
= NULL
;
96 void c_symbol_merge ();
99 symbolS
*c_section_symbol ();
101 void EXFUN (bfd_as_write_hook
, (struct internal_filehdr
*,
104 static void EXFUN (fixup_segment
, (segment_info_type
*segP
,
105 segT this_segment_type
));
108 static void EXFUN (fixup_mdeps
, (fragS
*));
111 static void EXFUN (fill_section
, (bfd
* abfd
,
112 struct internal_filehdr
* f
, unsigned
116 char *EXFUN (s_get_name
, (symbolS
* s
));
117 static symbolS
*EXFUN (tag_find_or_make
, (char *name
));
118 static symbolS
*EXFUN (tag_find
, (char *name
));
125 unsigned short line_number
,
129 static void EXFUN (w_symbols
,
132 symbolS
* symbol_rootP
));
136 static void EXFUN (obj_coff_def
, (int what
));
137 static void EXFUN (obj_coff_lcomm
, (void));
138 static void EXFUN (obj_coff_dim
, (void));
139 static void EXFUN (obj_coff_text
, (void));
140 static void EXFUN (obj_coff_data
, (void));
141 static void EXFUN (obj_coff_endef
, (void));
142 static void EXFUN (obj_coff_line
, (void));
143 static void EXFUN (obj_coff_ln
, (void));
144 static void EXFUN (obj_coff_scl
, (void));
145 static void EXFUN (obj_coff_size
, (void));
146 static void EXFUN (obj_coff_tag
, (void));
147 static void EXFUN (obj_coff_type
, (void));
148 static void EXFUN (obj_coff_val
, (void));
149 void EXFUN (obj_coff_section
, (void));
150 static void EXFUN (tag_init
, (void));
151 static void EXFUN (tag_insert
, (char *name
, symbolS
* symbolP
));
154 static struct hash_control
*tag_hash
;
155 static symbolS
*def_symbol_in_progress
= NULL
;
157 const pseudo_typeS obj_pseudo_table
[] =
159 {"def", obj_coff_def
, 0},
160 {"dim", obj_coff_dim
, 0},
161 {"endef", obj_coff_endef
, 0},
162 {"line", obj_coff_line
, 0},
163 {"ln", obj_coff_ln
, 0},
164 {"scl", obj_coff_scl
, 0},
165 {"size", obj_coff_size
, 0},
166 {"tag", obj_coff_tag
, 0},
167 {"type", obj_coff_type
, 0},
168 {"val", obj_coff_val
, 0},
169 {"section", obj_coff_section
, 0},
170 {"use", obj_coff_section
, 0},
171 {"sect", obj_coff_section
, 0},
172 {"text", obj_coff_text
, 0},
173 {"data", obj_coff_data
, 0},
174 /* we don't yet handle this. */
175 {"ident", s_ignore
, 0},
176 {"ABORT", s_abort
, 0},
177 {"lcomm", obj_coff_lcomm
, 0},
178 {NULL
} /* end sentinel */
179 }; /* obj_pseudo_table */
185 We allow more than just the standard 3 sections, infact, we allow
186 10 sections, (though the usual three have to be there).
188 This structure performs the mappings for us:
193 static struct internal_scnhdr bss_section_header;
194 struct internal_scnhdr data_section_header;
195 struct internal_scnhdr text_section_header;
197 const segT N_TYPE_seg [32] =
213 seg_info_type seg_info_off_by_4
[N_SEG
] =
239 {SEG_REGISTER
}, 0, 0, 0, 0};
241 #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
242 #define SEG_INFO_FROM_SEG_NUMBER(x) (seg_info_off_by_4[(x)])
246 DEFUN (relax_align
, (address
, alignment
),
247 register relax_addressT address AND
248 register long alignment
)
251 relax_addressT new_address
;
253 mask
= ~((~0) << alignment
);
254 new_address
= (address
+ mask
) & (~mask
);
255 return (new_address
- address
);
256 } /* relax_align() */
260 DEFUN (s_get_segment
, (x
),
263 return SEG_INFO_FROM_SECTION_NUMBER (x
->sy_symbol
.ost_entry
.n_scnum
).seg_t
;
268 /* calculate the size of the frag chain and fill in the section header
269 to contain all of it, also fill in the addr of the sections */
271 DEFUN (size_section
, (abfd
, idx
),
276 unsigned int size
= 0;
277 fragS
*frag
= segment_info
[idx
].frchainP
->frch_root
;
280 size
= frag
->fr_address
;
282 if (frag
->fr_address
!= size
)
284 printf ("Out of step\n");
285 size
= frag
->fr_address
;
288 switch (frag
->fr_type
)
290 #ifdef TC_COFF_SIZEMACHDEP
291 case rs_machine_dependent
:
292 size
+= TC_COFF_SIZEMACHDEP (frag
);
297 size
+= frag
->fr_fix
;
298 size
+= frag
->fr_offset
* frag
->fr_var
;
301 size
+= frag
->fr_fix
;
302 size
+= relax_align (size
, frag
->fr_offset
);
305 frag
= frag
->fr_next
;
307 segment_info
[idx
].scnhdr
.s_size
= size
;
313 DEFUN (count_entries_in_chain
, (idx
),
316 unsigned int nrelocs
;
319 /* Count the relocations */
320 fixup_ptr
= segment_info
[idx
].fix_root
;
322 while (fixup_ptr
!= (fixS
*) NULL
)
324 if (TC_COUNT_RELOC (fixup_ptr
))
329 if (fixup_ptr
->fx_r_type
== RELOC_CONSTH
)
338 fixup_ptr
= fixup_ptr
->fx_next
;
343 /* output all the relocations for a section */
345 DEFUN (do_relocs_for
, (abfd
, file_cursor
),
347 unsigned long *file_cursor
)
349 unsigned int nrelocs
;
351 unsigned int addr
= 0;
352 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
354 if (segment_info
[idx
].scnhdr
.s_name
[0])
357 struct external_reloc
*ext_ptr
;
358 struct external_reloc
*external_reloc_vec
;
359 unsigned int external_reloc_size
;
360 unsigned int count
= 0;
361 unsigned int base
= addr
;
362 fixS
*fix_ptr
= segment_info
[idx
].fix_root
;
363 nrelocs
= count_entries_in_chain (idx
);
368 external_reloc_size
= nrelocs
* RELSZ
;
370 (struct external_reloc
*) malloc (external_reloc_size
);
374 ext_ptr
= external_reloc_vec
;
376 /* Fill in the internal coff style reloc struct from the
381 struct internal_reloc intr
;
383 /* Only output some of the relocations */
384 if (TC_COUNT_RELOC (fix_ptr
))
386 #ifdef TC_RELOC_MANGLE
387 TC_RELOC_MANGLE (fix_ptr
, &intr
, base
);
391 symbol_ptr
= fix_ptr
->fx_addsy
;
393 intr
.r_type
= TC_COFF_FIX2RTYPE (fix_ptr
);
395 base
+ fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
;
397 intr
.r_offset
= fix_ptr
->fx_offset
;
401 /* Turn the segment of the symbol into an offset
405 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
408 intr
.r_symndx
= dot
->sy_number
;
412 intr
.r_symndx
= symbol_ptr
->sy_number
;
424 (void) bfd_coff_swap_reloc_out (abfd
, &intr
, ext_ptr
);
428 /* The 29k has a special kludge for the high 16 bit reloc.
429 Two relocations are emmited, R_IHIHALF, and
430 R_IHCONST. The second one doesn't contain a symbol,
431 but uses the value for offset */
433 if (intr
.r_type
== R_IHIHALF
)
435 /* now emit the second bit */
436 intr
.r_type
= R_IHCONST
;
437 intr
.r_symndx
= fix_ptr
->fx_addnumber
;
438 (void) bfd_coff_swap_reloc_out (abfd
, &intr
, ext_ptr
);
444 fix_ptr
= fix_ptr
->fx_next
;
447 /* Write out the reloc table */
448 segment_info
[idx
].scnhdr
.s_relptr
= *file_cursor
;
449 segment_info
[idx
].scnhdr
.s_nreloc
= nrelocs
;
450 bfd_write ((PTR
) external_reloc_vec
, 1, external_reloc_size
, abfd
);
451 *file_cursor
+= external_reloc_size
;
452 free (external_reloc_vec
);
454 addr
+= segment_info
[idx
].scnhdr
.s_size
;
459 /* run through a frag chain and write out the data to go with it, fill
460 in the scnhdrs with the info on the file postions
463 DEFUN (fill_section
, (abfd
, filehdr
, file_cursor
),
465 struct internal_filehdr
*filehdr AND
466 unsigned long *file_cursor
)
470 #ifdef ZERO_BASED_SEGMENTS
471 unsigned int paddr
= 0;
474 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
476 unsigned int offset
= 0;
478 struct internal_scnhdr
*s
= &(segment_info
[i
].scnhdr
);
482 fragS
*frag
= segment_info
[i
].frchainP
->frch_root
;
485 if (s
->s_size
!= 0 && i
!= SEG_E2
)
487 buffer
= malloc (s
->s_size
);
488 s
->s_scnptr
= *file_cursor
;
489 #ifdef ZERO_BASED_SEGMENTS
500 s
->s_flags
= STYP_REG
;
501 if (strcmp (s
->s_name
, ".text") == 0)
502 s
->s_flags
|= STYP_TEXT
;
503 else if (strcmp (s
->s_name
, ".data") == 0)
504 s
->s_flags
|= STYP_DATA
;
505 else if (strcmp (s
->s_name
, ".bss") == 0)
506 s
->s_flags
|= STYP_BSS
| STYP_NOLOAD
;
507 else if (strcmp (s
->s_name
, ".lit") == 0)
508 s
->s_flags
= STYP_LIT
| STYP_TEXT
;
509 else if (strcmp (s
->s_name
, ".init") == 0)
510 s
->s_flags
|= STYP_TEXT
;
511 else if (strcmp (s
->s_name
, ".fini") == 0)
512 s
->s_flags
|= STYP_TEXT
;
516 unsigned int fill_size
;
517 switch (frag
->fr_type
)
519 case rs_machine_dependent
:
522 memcpy (buffer
+ frag
->fr_address
,
525 offset
+= frag
->fr_fix
;
534 memcpy (buffer
+ frag
->fr_address
,
537 offset
+= frag
->fr_fix
;
540 fill_size
= frag
->fr_var
;
544 unsigned int off
= frag
->fr_fix
;
545 for (count
= frag
->fr_offset
; count
; count
--)
547 memcpy (buffer
+ frag
->fr_address
+ off
,
548 frag
->fr_literal
+ frag
->fr_fix
,
562 frag
= frag
->fr_next
;
565 if (s
->s_size
!= 0 && i
!= SEG_E2
)
567 bfd_write (buffer
, s
->s_size
, 1, abfd
);
569 *file_cursor
+= s
->s_size
;
571 #ifdef ZERO_BASED_SEGMENTS
580 /* Coff file generation & utilities */
584 DEFUN (coff_header_append
, (abfd
, filehdr
, aouthdr
),
586 struct internal_filehdr
*filehdr AND
587 struct internal_aouthdr
*aouthdr
)
593 bfd_seek (abfd
, 0, 0);
595 filehdr
.f_opthdr
= bfd_coff_swap_aouthdr_out (abfd
, aouthdr
,
598 filehdr
->f_opthdr
= 0;
600 i
= bfd_coff_swap_filehdr_out (abfd
, filehdr
, buffer
);
602 bfd_write (buffer
, i
, 1, abfd
);
603 bfd_write (buffero
, filehdr
->f_opthdr
, 1, abfd
);
605 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
607 if (segment_info
[i
].scnhdr
.s_name
[0])
610 bfd_coff_swap_scnhdr_out (abfd
,
611 &(segment_info
[i
].scnhdr
),
613 bfd_write (buffer
, size
, 1, abfd
);
620 DEFUN (symbol_to_chars
, (abfd
, where
, symbolP
),
625 unsigned int numaux
= symbolP
->sy_symbol
.ost_entry
.n_numaux
;
628 /* Turn any symbols with register attributes into abs symbols */
629 if (S_GET_SEGMENT (symbolP
) == SEG_REGISTER
)
631 S_SET_SEGMENT (symbolP
, SEG_ABSOLUTE
);
633 /* At the same time, relocate all symbols to their output value */
635 S_SET_VALUE (symbolP
,
636 segment_info
[S_GET_SEGMENT (symbolP
)].scnhdr
.s_paddr
637 + S_GET_VALUE (symbolP
));
639 where
+= bfd_coff_swap_sym_out (abfd
, &symbolP
->sy_symbol
.ost_entry
,
642 for (i
= 0; i
< numaux
; i
++)
644 where
+= bfd_coff_swap_aux_out (abfd
,
645 &symbolP
->sy_symbol
.ost_auxent
[i
],
646 S_GET_DATA_TYPE (symbolP
),
647 S_GET_STORAGE_CLASS (symbolP
),
658 obj_symbol_new_hook (symbolP
)
661 char underscore
= 0; /* Symbol has leading _ */
663 /* Effective symbol */
664 /* Store the pointer in the offset. */
665 S_SET_ZEROES (symbolP
, 0L);
666 S_SET_DATA_TYPE (symbolP
, T_NULL
);
667 S_SET_STORAGE_CLASS (symbolP
, 0);
668 S_SET_NUMBER_AUXILIARY (symbolP
, 0);
669 /* Additional information */
670 symbolP
->sy_symbol
.ost_flags
= 0;
671 /* Auxiliary entries */
672 bzero ((char *) &symbolP
->sy_symbol
.ost_auxent
[0], AUXESZ
);
674 #ifdef STRIP_UNDERSCORE
675 /* Remove leading underscore at the beginning of the symbol.
676 * This is to be compatible with the standard librairies.
678 if (*S_GET_NAME (symbolP
) == '_')
681 S_SET_NAME (symbolP
, S_GET_NAME (symbolP
) + 1);
682 } /* strip underscore */
683 #endif /* STRIP_UNDERSCORE */
685 if (S_IS_STRING (symbolP
))
686 SF_SET_STRING (symbolP
);
687 if (!underscore
&& S_IS_LOCAL (symbolP
))
688 SF_SET_LOCAL (symbolP
);
691 } /* obj_symbol_new_hook() */
695 stack_init (chunk_size
, element_size
)
696 unsigned long chunk_size
;
697 unsigned long element_size
;
701 if ((st
= (stack
*) malloc (sizeof (stack
))) == (stack
*) 0)
703 if ((st
->data
= malloc (chunk_size
)) == (char *) 0)
709 st
->size
= chunk_size
;
710 st
->chunk_size
= chunk_size
;
711 st
->element_size
= element_size
;
724 stack_push (st
, element
)
728 if (st
->pointer
+ st
->element_size
>= st
->size
)
730 st
->size
+= st
->chunk_size
;
731 if ((st
->data
= xrealloc (st
->data
, st
->size
)) == (char *) 0)
734 memcpy (st
->data
+ st
->pointer
, element
, st
->element_size
);
735 st
->pointer
+= st
->element_size
;
736 return st
->data
+ st
->pointer
;
743 if ((st
->pointer
-= st
->element_size
) < 0)
749 return st
->data
+ st
->pointer
;
756 return st
->data
+ st
->pointer
- st
->element_size
;
761 * Handle .ln directives.
769 if (def_symbol_in_progress
!= NULL
)
771 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
772 demand_empty_rest_of_line ();
774 } /* wrong context */
777 obstack_next_free (&frags
) - frag_now
->fr_literal
,
778 l
= get_absolute_expression (),
786 listing_source_line (l
+ line_base
- 1);
791 demand_empty_rest_of_line ();
793 } /* obj_coff_line() */
798 * Handle .def directives.
800 * One might ask : why can't we symbol_new if the symbol does not
801 * already exist and fill it with debug information. Because of
802 * the C_EFCN special symbol. It would clobber the value of the
803 * function symbol before we have a chance to notice that it is
804 * a C_EFCN. And a second reason is that the code is more clear this
805 * way. (at least I think it is :-).
809 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
810 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
811 *input_line_pointer == '\t') \
812 input_line_pointer++;
815 DEFUN (obj_coff_def
, (what
),
818 char name_end
; /* Char after the end of name */
819 char *symbol_name
; /* Name of the debug symbol */
820 char *symbol_name_copy
; /* Temporary copy of the name */
821 unsigned int symbol_name_length
;
822 /*$char* directiveP;$ *//* Name of the pseudo opcode */
823 /*$char directive[MAX_DIRECTIVE];$ *//* Backup of the directive */
824 /*$char end = 0;$ *//* If 1, stop parsing */
826 if (def_symbol_in_progress
!= NULL
)
828 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
829 demand_empty_rest_of_line ();
831 } /* if not inside .def/.endef */
835 def_symbol_in_progress
= (symbolS
*) obstack_alloc (¬es
, sizeof (*def_symbol_in_progress
));
836 bzero (def_symbol_in_progress
, sizeof (*def_symbol_in_progress
));
838 symbol_name
= input_line_pointer
;
839 name_end
= get_symbol_end ();
840 symbol_name_length
= strlen (symbol_name
);
841 symbol_name_copy
= xmalloc (symbol_name_length
+ 1);
842 strcpy (symbol_name_copy
, symbol_name
);
844 /* Initialize the new symbol */
845 #ifdef STRIP_UNDERSCORE
846 S_SET_NAME (def_symbol_in_progress
, (*symbol_name_copy
== '_'
847 ? symbol_name_copy
+ 1
848 : symbol_name_copy
));
849 #else /* STRIP_UNDERSCORE */
850 S_SET_NAME (def_symbol_in_progress
, symbol_name_copy
);
851 #endif /* STRIP_UNDERSCORE */
852 /* free(symbol_name_copy); */
853 def_symbol_in_progress
->sy_name_offset
= ~0;
854 def_symbol_in_progress
->sy_number
= ~0;
855 def_symbol_in_progress
->sy_frag
= &zero_address_frag
;
857 if (S_IS_STRING (def_symbol_in_progress
))
859 SF_SET_STRING (def_symbol_in_progress
);
862 *input_line_pointer
= name_end
;
864 demand_empty_rest_of_line ();
866 } /* obj_coff_def() */
868 unsigned int dim_index
;
870 DEFUN_VOID (obj_coff_endef
)
872 symbolS
*symbolP
= 0;
873 /* DIM BUG FIX sac@cygnus.com */
875 if (def_symbol_in_progress
== NULL
)
877 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
878 demand_empty_rest_of_line ();
880 } /* if not inside .def/.endef */
882 /* Set the section number according to storage class. */
883 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress
))
888 SF_SET_TAG (def_symbol_in_progress
);
889 /* intentional fallthrough */
892 SF_SET_DEBUG (def_symbol_in_progress
);
893 S_SET_SEGMENT (def_symbol_in_progress
, SEG_DEBUG
);
897 SF_SET_LOCAL (def_symbol_in_progress
); /* Do not emit this symbol. */
898 /* intentional fallthrough */
900 SF_SET_PROCESS (def_symbol_in_progress
); /* Will need processing before writing */
901 /* intentional fallthrough */
903 S_SET_SEGMENT (def_symbol_in_progress
, SEG_E0
);
905 if (strcmp (def_symbol_in_progress
->sy_symbol
.ost_entry
._n
._n_nptr
,
908 if (function_lineoff
< 0)
910 fprintf (stderr
, "`.bf' symbol without preceding function\n");
911 } /* missing function symbol */
912 SA_GET_SYM_LNNOPTR (last_line_symbol
) = function_lineoff
;
914 SF_SET_PROCESS (last_line_symbol
);
915 function_lineoff
= -1;
921 #endif /* C_AUTOARG */
931 SF_SET_DEBUG (def_symbol_in_progress
);
932 S_SET_SEGMENT (def_symbol_in_progress
, SEG_ABSOLUTE
);
938 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
944 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress
));
946 } /* switch on storage class */
948 /* Now that we have built a debug symbol, try to find if
949 we should merge with an existing symbol or not. If a
950 symbol is C_EFCN or SEG_ABSOLUTE or untagged
951 SEG_DEBUG it never merges. We also don't merge
952 labels, which are in a different namespace, nor
953 symbols which have not yet been defined since they
954 are typically unique, nor do we merge tags with
957 /* Two cases for functions. Either debug followed
958 by definition or definition followed by debug.
959 For definition first, we will merge the debug
960 symbol into the definition. For debug first, the
961 lineno entry MUST point to the definition
962 function or else it will point off into space
963 when crawl_symbols() merges the debug
964 symbol into the real symbol. Therefor, let's
965 presume the debug symbol is a real function
968 /* FIXME-SOON If for some reason the definition
969 label/symbol is never seen, this will probably
970 leave an undefined symbol at link time. */
972 if (S_GET_STORAGE_CLASS (def_symbol_in_progress
) == C_EFCN
973 || S_GET_STORAGE_CLASS (def_symbol_in_progress
) == C_LABEL
974 || (S_GET_SEGMENT (def_symbol_in_progress
) == SEG_DEBUG
975 && !SF_GET_TAG (def_symbol_in_progress
))
976 || S_GET_SEGMENT (def_symbol_in_progress
) == SEG_ABSOLUTE
977 || def_symbol_in_progress
->sy_forward
!= NULL
978 || (symbolP
= symbol_find_base (S_GET_NAME (def_symbol_in_progress
), DO_NOT_STRIP
)) == NULL
979 || (SF_GET_TAG (def_symbol_in_progress
) != SF_GET_TAG (symbolP
)))
982 symbol_append (def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
987 /* This symbol already exists, merge the
988 newly created symbol into the old one.
989 This is not mandatory. The linker can
990 handle duplicate symbols correctly. But I
991 guess that it save a *lot* of space if
992 the assembly file defines a lot of
995 /* The debug entry (def_symbol_in_progress)
996 is merged into the previous definition. */
998 c_symbol_merge (def_symbol_in_progress
, symbolP
);
999 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
1000 def_symbol_in_progress
= symbolP
;
1002 if (SF_GET_FUNCTION (def_symbol_in_progress
)
1003 || SF_GET_TAG (def_symbol_in_progress
))
1005 /* For functions, and tags, the symbol *must* be where the debug symbol
1006 appears. Move the existing symbol to the current place. */
1007 /* If it already is at the end of the symbol list, do nothing */
1008 if (def_symbol_in_progress
!= symbol_lastP
)
1010 symbol_remove (def_symbol_in_progress
, &symbol_rootP
, &symbol_lastP
);
1011 symbol_append (def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
1012 } /* if not already in place */
1014 } /* normal or mergable */
1016 if (SF_GET_TAG (def_symbol_in_progress
)
1017 && symbol_find_base (S_GET_NAME (def_symbol_in_progress
), DO_NOT_STRIP
) == NULL
)
1019 tag_insert (S_GET_NAME (def_symbol_in_progress
), def_symbol_in_progress
);
1020 } /* If symbol is a {structure,union} tag, associate symbol to its name. */
1022 if (SF_GET_FUNCTION (def_symbol_in_progress
))
1024 know (sizeof (def_symbol_in_progress
) <= sizeof (long));
1026 = c_line_new (def_symbol_in_progress
, 0, 0, &zero_address_frag
);
1030 SF_SET_PROCESS (def_symbol_in_progress
);
1032 if (symbolP
== NULL
)
1034 /* That is, if this is the first
1035 time we've seen the function... */
1036 symbol_table_insert (def_symbol_in_progress
);
1037 } /* definition follows debug */
1038 } /* Create the line number entry pointing to the function being defined */
1040 def_symbol_in_progress
= NULL
;
1041 demand_empty_rest_of_line ();
1043 } /* obj_coff_endef() */
1046 DEFUN_VOID (obj_coff_dim
)
1048 register int dim_index
;
1050 if (def_symbol_in_progress
== NULL
)
1052 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
1053 demand_empty_rest_of_line ();
1055 } /* if not inside .def/.endef */
1057 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1059 for (dim_index
= 0; dim_index
< DIMNUM
; dim_index
++)
1061 SKIP_WHITESPACES ();
1062 SA_SET_SYM_DIMEN (def_symbol_in_progress
, dim_index
, get_absolute_expression ());
1064 switch (*input_line_pointer
)
1068 input_line_pointer
++;
1072 as_warn ("badly formed .dim directive ignored");
1073 /* intentional fallthrough */
1078 } /* switch on following character */
1079 } /* for each dimension */
1081 demand_empty_rest_of_line ();
1083 } /* obj_coff_dim() */
1090 if (def_symbol_in_progress
== NULL
)
1094 } /* if it looks like a stabs style line */
1096 this_base
= get_absolute_expression ();
1097 if (this_base
> line_base
)
1099 line_base
= this_base
;
1108 listing_source_line (line_base
);
1112 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1113 SA_SET_SYM_LNNO (def_symbol_in_progress
, line_base
);
1115 demand_empty_rest_of_line ();
1117 } /* obj_coff_line() */
1122 if (def_symbol_in_progress
== NULL
)
1124 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1125 demand_empty_rest_of_line ();
1127 } /* if not inside .def/.endef */
1129 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1130 SA_SET_SYM_SIZE (def_symbol_in_progress
, get_absolute_expression ());
1131 demand_empty_rest_of_line ();
1133 } /* obj_coff_size() */
1138 if (def_symbol_in_progress
== NULL
)
1140 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1141 demand_empty_rest_of_line ();
1143 } /* if not inside .def/.endef */
1145 S_SET_STORAGE_CLASS (def_symbol_in_progress
, get_absolute_expression ());
1146 demand_empty_rest_of_line ();
1148 } /* obj_coff_scl() */
1156 if (def_symbol_in_progress
== NULL
)
1158 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1159 demand_empty_rest_of_line ();
1161 } /* if not inside .def/.endef */
1163 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1164 symbol_name
= input_line_pointer
;
1165 name_end
= get_symbol_end ();
1167 /* Assume that the symbol referred to by .tag is always defined. */
1168 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1169 SA_SET_SYM_TAGNDX (def_symbol_in_progress
, (long) tag_find_or_make (symbol_name
));
1170 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress
) == 0L)
1172 as_warn ("tag not found for .tag %s", symbol_name
);
1175 SF_SET_TAGGED (def_symbol_in_progress
);
1176 *input_line_pointer
= name_end
;
1178 demand_empty_rest_of_line ();
1180 } /* obj_coff_tag() */
1185 if (def_symbol_in_progress
== NULL
)
1187 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1188 demand_empty_rest_of_line ();
1190 } /* if not inside .def/.endef */
1192 S_SET_DATA_TYPE (def_symbol_in_progress
, get_absolute_expression ());
1194 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress
)) &&
1195 S_GET_STORAGE_CLASS (def_symbol_in_progress
) != C_TPDEF
)
1197 SF_SET_FUNCTION (def_symbol_in_progress
);
1198 } /* is a function */
1200 demand_empty_rest_of_line ();
1202 } /* obj_coff_type() */
1207 if (def_symbol_in_progress
== NULL
)
1209 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1210 demand_empty_rest_of_line ();
1212 } /* if not inside .def/.endef */
1214 if (is_name_beginner (*input_line_pointer
))
1216 char *symbol_name
= input_line_pointer
;
1217 char name_end
= get_symbol_end ();
1219 if (!strcmp (symbol_name
, "."))
1221 def_symbol_in_progress
->sy_frag
= frag_now
;
1222 S_SET_VALUE (def_symbol_in_progress
, obstack_next_free (&frags
) - frag_now
->fr_literal
);
1223 /* If the .val is != from the .def (e.g. statics) */
1225 else if (strcmp (S_GET_NAME (def_symbol_in_progress
), symbol_name
))
1227 def_symbol_in_progress
->sy_forward
= symbol_find_or_make (symbol_name
);
1229 /* If the segment is undefined when the forward
1230 reference is solved, then copy the segment id
1231 from the forward symbol. */
1232 SF_SET_GET_SEGMENT (def_symbol_in_progress
);
1234 /* FIXME: gcc can generate address expressions
1235 here in unusual cases (search for "obscure"
1236 in sdbout.c). We just ignore the offset
1237 here, thus generating incorrect debugging
1238 information. We ignore the rest of the
1241 /* Otherwise, it is the name of a non debug symbol and
1242 its value will be calculated later. */
1243 *input_line_pointer
= name_end
;
1245 /* FIXME: this is to avoid an error message in the
1246 FIXME case mentioned just above. */
1247 while (! is_end_of_line
[*input_line_pointer
])
1248 ++input_line_pointer
;
1252 S_SET_VALUE (def_symbol_in_progress
, get_absolute_expression ());
1253 } /* if symbol based */
1255 demand_empty_rest_of_line ();
1257 } /* obj_coff_val() */
1260 * Maintain a list of the tagnames of the structres.
1266 tag_hash
= hash_new ();
1271 tag_insert (name
, symbolP
)
1275 register char *error_string
;
1277 if (*(error_string
= hash_jam (tag_hash
, name
, (char *) symbolP
)))
1279 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1280 name
, error_string
);
1283 } /* tag_insert() */
1286 tag_find_or_make (name
)
1291 if ((symbolP
= tag_find (name
)) == NULL
)
1293 symbolP
= symbol_new (name
,
1296 &zero_address_frag
);
1298 tag_insert (S_GET_NAME (symbolP
), symbolP
);
1302 } /* tag_find_or_make() */
1308 #ifdef STRIP_UNDERSCORE
1311 #endif /* STRIP_UNDERSCORE */
1312 return ((symbolS
*) hash_find (tag_hash
, name
));
1316 obj_read_begin_hook ()
1318 /* These had better be the same. Usually 18 bytes. */
1320 know (sizeof (SYMENT
) == sizeof (AUXENT
));
1321 know (SYMESZ
== AUXESZ
);
1326 } /* obj_read_begin_hook() */
1328 /* This function runs through the symbol table and puts all the
1329 externals onto another chain */
1331 /* The chain of externals */
1332 symbolS
*symbol_externP
= NULL
;
1333 symbolS
*symbol_extern_lastP
= NULL
;
1336 symbolS
*last_functionP
= NULL
;
1341 DEFUN_VOID (yank_symbols
)
1344 unsigned int symbol_number
= 0;
1346 for (symbolP
= symbol_rootP
;
1348 symbolP
= symbolP
? symbol_next (symbolP
) : symbol_rootP
)
1350 if (!SF_GET_DEBUG (symbolP
))
1352 /* Debug symbols do not need all this rubbish */
1353 symbolS
*real_symbolP
;
1355 /* L* and C_EFCN symbols never merge. */
1356 if (!SF_GET_LOCAL (symbolP
)
1357 && S_GET_STORAGE_CLASS (symbolP
) != C_LABEL
1358 && (real_symbolP
= symbol_find_base (S_GET_NAME (symbolP
), DO_NOT_STRIP
))
1359 && real_symbolP
!= symbolP
)
1361 /* FIXME-SOON: where do dups come from?
1362 Maybe tag references before definitions? xoxorich. */
1363 /* Move the debug data from the debug symbol to the
1364 real symbol. Do NOT do the oposite (i.e. move from
1365 real symbol to debug symbol and remove real symbol from the
1366 list.) Because some pointers refer to the real symbol
1367 whereas no pointers refer to the debug symbol. */
1368 c_symbol_merge (symbolP
, real_symbolP
);
1369 /* Replace the current symbol by the real one */
1370 /* The symbols will never be the last or the first
1371 because : 1st symbol is .file and 3 last symbols are
1372 .text, .data, .bss */
1373 symbol_remove (real_symbolP
, &symbol_rootP
, &symbol_lastP
);
1374 symbol_insert (real_symbolP
, symbolP
, &symbol_rootP
, &symbol_lastP
);
1375 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1376 symbolP
= real_symbolP
;
1377 } /* if not local but dup'd */
1379 if (flagseen
['R'] && (S_GET_SEGMENT (symbolP
) == SEG_E1
))
1381 S_SET_SEGMENT (symbolP
, SEG_E0
);
1382 } /* push data into text */
1384 S_SET_VALUE (symbolP
,
1385 S_GET_VALUE (symbolP
) + symbolP
->sy_frag
->fr_address
);
1387 if (!S_IS_DEFINED (symbolP
) && !SF_GET_LOCAL (symbolP
))
1389 S_SET_EXTERNAL (symbolP
);
1391 else if (S_GET_STORAGE_CLASS (symbolP
) == C_NULL
)
1393 if (S_GET_SEGMENT (symbolP
) == SEG_E0
)
1395 S_SET_STORAGE_CLASS (symbolP
, C_LABEL
);
1399 S_SET_STORAGE_CLASS (symbolP
, C_STAT
);
1403 /* Mainly to speed up if not -g */
1404 if (SF_GET_PROCESS (symbolP
))
1406 /* Handle the nested blocks auxiliary info. */
1407 if (S_GET_STORAGE_CLASS (symbolP
) == C_BLOCK
)
1409 if (!strcmp (S_GET_NAME (symbolP
), ".bb"))
1410 stack_push (block_stack
, (char *) &symbolP
);
1413 register symbolS
*begin_symbolP
;
1414 begin_symbolP
= *(symbolS
**) stack_pop (block_stack
);
1415 if (begin_symbolP
== (symbolS
*) 0)
1416 as_warn ("mismatched .eb");
1418 SA_SET_SYM_ENDNDX (begin_symbolP
, symbol_number
+ 2);
1421 /* If we are able to identify the type of a function, and we
1422 are out of a function (last_functionP == 0) then, the
1423 function symbol will be associated with an auxiliary
1425 if (last_functionP
== (symbolS
*) 0 &&
1426 SF_GET_FUNCTION (symbolP
))
1428 last_functionP
= symbolP
;
1430 if (S_GET_NUMBER_AUXILIARY (symbolP
) < 1)
1432 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
1433 } /* make it at least 1 */
1435 /* Clobber possible stale .dim information. */
1437 /* Iffed out by steve - this fries the lnnoptr info too */
1438 bzero (symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
,
1439 sizeof (symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
));
1442 /* The C_FCN doesn't need any additional information.
1443 I don't even know if this is needed for sdb. But the
1444 standard assembler generates it, so...
1446 if (S_GET_STORAGE_CLASS (symbolP
) == C_EFCN
)
1448 if (last_functionP
== (symbolS
*) 0)
1449 as_fatal ("C_EFCN symbol out of scope");
1450 SA_SET_SYM_FSIZE (last_functionP
,
1451 (long) (S_GET_VALUE (symbolP
) -
1452 S_GET_VALUE (last_functionP
)));
1453 SA_SET_SYM_ENDNDX (last_functionP
, symbol_number
);
1454 last_functionP
= (symbolS
*) 0;
1458 else if (SF_GET_TAG (symbolP
))
1460 /* First descriptor of a structure must point to
1461 the first slot after the structure description. */
1462 last_tagP
= symbolP
;
1465 else if (S_GET_STORAGE_CLASS (symbolP
) == C_EOS
)
1467 /* +2 take in account the current symbol */
1468 SA_SET_SYM_ENDNDX (last_tagP
, symbol_number
+ 2);
1470 else if (S_GET_STORAGE_CLASS (symbolP
) == C_FILE
)
1472 if (S_GET_VALUE (symbolP
))
1474 S_SET_VALUE ((symbolS
*) S_GET_VALUE (symbolP
), symbol_number
);
1475 S_SET_VALUE (symbolP
, 0);
1476 } /* no one points at the first .file symbol */
1477 } /* if debug or tag or eos or file */
1479 /* We must put the external symbols apart. The loader
1480 does not bomb if we do not. But the references in
1481 the endndx field for a .bb symbol are not corrected
1482 if an external symbol is removed between .bb and .be.
1483 I.e in the following case :
1484 [20] .bb endndx = 22
1487 ld will move the symbol 21 to the end of the list but
1488 endndx will still be 22 instead of 21. */
1491 if (SF_GET_LOCAL (symbolP
))
1493 /* remove C_EFCN and LOCAL (L...) symbols */
1494 /* next pointer remains valid */
1495 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1498 else if (!S_IS_DEFINED (symbolP
)
1499 && !S_IS_DEBUG (symbolP
)
1500 && !SF_GET_STATICS (symbolP
) &&
1501 S_GET_STORAGE_CLASS (symbolP
) == C_EXT
)
1502 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1503 /* if external, Remove from the list */
1504 symbolS
*hold
= symbol_previous (symbolP
);
1506 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1507 symbol_clear_list_pointers (symbolP
);
1508 symbol_append (symbolP
, symbol_extern_lastP
, &symbol_externP
, &symbol_extern_lastP
);
1513 if (SF_GET_STRING (symbolP
))
1515 symbolP
->sy_name_offset
= string_byte_count
;
1516 string_byte_count
+= strlen (S_GET_NAME (symbolP
)) + 1;
1520 symbolP
->sy_name_offset
= 0;
1521 } /* fix "long" names */
1523 symbolP
->sy_number
= symbol_number
;
1524 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (symbolP
);
1525 } /* if local symbol */
1526 } /* traverse the symbol list */
1527 return symbol_number
;
1533 DEFUN_VOID (glue_symbols
)
1535 unsigned int symbol_number
= 0;
1537 for (symbolP
= symbol_externP
; symbol_externP
;)
1539 symbolS
*tmp
= symbol_externP
;
1542 symbol_remove (tmp
, &symbol_externP
, &symbol_extern_lastP
);
1543 symbol_append (tmp
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
1546 if (SF_GET_STRING (tmp
))
1548 tmp
->sy_name_offset
= string_byte_count
;
1549 string_byte_count
+= strlen (S_GET_NAME (tmp
)) + 1;
1553 tmp
->sy_name_offset
= 0;
1554 } /* fix "long" names */
1556 tmp
->sy_number
= symbol_number
;
1557 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (tmp
);
1558 } /* append the entire extern chain */
1559 return symbol_number
;
1564 DEFUN_VOID (tie_tags
)
1566 unsigned int symbol_number
= 0;
1569 for (symbolP
= symbol_rootP
; symbolP
; symbolP
=
1570 symbol_next (symbolP
))
1572 symbolP
->sy_number
= symbol_number
;
1576 if (SF_GET_TAGGED (symbolP
))
1580 ((symbolS
*) SA_GET_SYM_TAGNDX (symbolP
))->sy_number
);
1583 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (symbolP
);
1585 return symbol_number
;
1590 DEFUN (crawl_symbols
, (headers
, abfd
),
1591 struct internal_filehdr
*headers AND
1596 unsigned int ptr
= 0;
1601 /* Initialize the stack used to keep track of the matching .bb .be */
1603 block_stack
= stack_init (512, sizeof (symbolS
*));
1604 /* JF deal with forward references first... */
1605 for (symbolP
= symbol_rootP
;
1607 symbolP
= symbol_next (symbolP
))
1610 if (symbolP
->sy_forward
)
1612 S_SET_VALUE (symbolP
, (S_GET_VALUE (symbolP
)
1613 + S_GET_VALUE (symbolP
->sy_forward
)
1614 + symbolP
->sy_forward
->sy_frag
->fr_address
));
1616 if (SF_GET_GET_SEGMENT (symbolP
))
1618 S_SET_SEGMENT (symbolP
, S_GET_SEGMENT (symbolP
->sy_forward
));
1619 } /* forward segment also */
1621 symbolP
->sy_forward
= 0;
1622 } /* if it has a forward reference */
1623 } /* walk the symbol chain */
1626 /* The symbol list should be ordered according to the following sequence
1629 * . debug entries for functions
1630 * . fake symbols for the sections, including.text .data and .bss
1632 * . undefined symbols
1633 * But this is not mandatory. The only important point is to put the
1634 * undefined symbols at the end of the list.
1637 if (symbol_rootP
== NULL
1638 || S_GET_STORAGE_CLASS (symbol_rootP
) != C_FILE
)
1640 c_dot_file_symbol ("fake");
1642 /* Is there a .file symbol ? If not insert one at the beginning. */
1645 * Build up static symbols for the sections, they are filled in later
1649 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
1651 if (segment_info
[i
].scnhdr
.s_name
[0])
1653 segment_info
[i
].dot
=
1654 c_section_symbol (segment_info
[i
].scnhdr
.s_name
,
1661 /* Take all the externals out and put them into another chain */
1662 headers
->f_nsyms
= yank_symbols ();
1663 /* Take the externals and glue them onto the end.*/
1664 headers
->f_nsyms
+= glue_symbols ();
1666 headers
->f_nsyms
= tie_tags ();
1667 know (symbol_externP
== NULL
);
1668 know (symbol_extern_lastP
== NULL
);
1674 * Find strings by crawling along symbol table chain.
1678 DEFUN (w_strings
, (where
),
1683 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
1684 md_number_to_chars (where
, string_byte_count
, sizeof (string_byte_count
));
1685 where
+= sizeof (string_byte_count
);
1686 for (symbolP
= symbol_rootP
;
1688 symbolP
= symbol_next (symbolP
))
1692 if (SF_GET_STRING (symbolP
))
1694 size
= strlen (S_GET_NAME (symbolP
)) + 1;
1696 memcpy (where
, S_GET_NAME (symbolP
), size
);
1709 DEFUN (do_linenos_for
, (abfd
, file_cursor
),
1711 unsigned long *file_cursor
)
1715 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
1717 segment_info_type
*s
= segment_info
+ idx
;
1720 if (s
->scnhdr
.s_nlnno
!= 0)
1722 struct lineno_list
*line_ptr
;
1724 struct external_lineno
*buffer
=
1725 (struct external_lineno
*) xmalloc (s
->scnhdr
.s_nlnno
* LINESZ
);
1727 struct external_lineno
*dst
= buffer
;
1729 /* Run through the table we've built and turn it into its external
1730 form, take this chance to remove duplicates */
1732 for (line_ptr
= s
->lineno_list_head
;
1733 line_ptr
!= (struct lineno_list
*) NULL
;
1734 line_ptr
= line_ptr
->next
)
1737 if (line_ptr
->line
.l_lnno
== 0)
1739 /* Turn a pointer to a symbol into the symbols' index */
1740 line_ptr
->line
.l_addr
.l_symndx
=
1741 ((symbolS
*) line_ptr
->line
.l_addr
.l_symndx
)->sy_number
;
1745 line_ptr
->line
.l_addr
.l_paddr
+= ((struct frag
*) (line_ptr
->frag
))->fr_address
;
1749 (void) bfd_coff_swap_lineno_out (abfd
, &(line_ptr
->line
), dst
);
1754 s
->scnhdr
.s_lnnoptr
= *file_cursor
;
1756 bfd_write (buffer
, 1, s
->scnhdr
.s_nlnno
* LINESZ
, abfd
);
1759 *file_cursor
+= s
->scnhdr
.s_nlnno
* LINESZ
;
1765 /* Now we run through the list of frag chains in a segment and
1766 make all the subsegment frags appear at the end of the
1767 list, as if the seg 0 was extra long */
1770 DEFUN_VOID (remove_subsegs
)
1774 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1776 frchainS
*head
= segment_info
[i
].frchainP
;
1778 fragS
*prev_frag
= &dummy
;
1780 while (head
&& head
->frch_seg
== i
)
1782 prev_frag
->fr_next
= head
->frch_root
;
1783 prev_frag
= head
->frch_last
;
1784 head
= head
->frch_next
;
1786 prev_frag
->fr_next
= 0;
1793 DEFUN_VOID (write_object_file
)
1796 struct frchain
*frchain_ptr
;
1798 struct internal_filehdr filehdr
;
1799 struct internal_aouthdr aouthdr
;
1800 unsigned long file_cursor
;
1803 abfd
= bfd_openw (out_file_name
, TARGET_FORMAT
);
1808 as_perror ("FATAL: Can't create %s", out_file_name
);
1811 bfd_set_format (abfd
, bfd_object
);
1812 bfd_set_arch_mach (abfd
, BFD_ARCH
, machine
);
1816 string_byte_count
= 4;
1818 for (frchain_ptr
= frchain_root
;
1819 frchain_ptr
!= (struct frchain
*) NULL
;
1820 frchain_ptr
= frchain_ptr
->frch_next
)
1822 /* Run through all the sub-segments and align them up. Also close any
1823 open frags. We tack a .fill onto the end of the frag chain so
1824 that any .align's size can be worked by looking at the next
1827 subseg_new (frchain_ptr
->frch_seg
, frchain_ptr
->frch_subseg
);
1828 #ifndef SUB_SEGMENT_ALIGN
1829 #define SUB_SEGMENT_ALIGN 1
1831 frag_align (SUB_SEGMENT_ALIGN
, NOP_OPCODE
);
1832 frag_wane (frag_now
);
1833 frag_now
->fr_fix
= 0;
1834 know (frag_now
->fr_next
== NULL
);
1841 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1843 relax_segment (segment_info
[i
].frchainP
->frch_root
, i
);
1846 filehdr
.f_nscns
= 0;
1848 /* Find out how big the sections are, and set the addresses. */
1850 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1852 segment_info
[i
].scnhdr
.s_paddr
= addr
;
1853 segment_info
[i
].scnhdr
.s_vaddr
= addr
;
1855 if (segment_info
[i
].scnhdr
.s_name
[0])
1860 /* Supposedly setting segment addresses non-zero
1861 causes problems for some platforms, although it
1862 shouldn't. If you define ZERO_BASED_SEGMENTS, all
1863 the segments will be based at 0. Please don't make
1864 this the default, since some systems (e.g., SVR3.2)
1865 require the segments to be non-zero based. Ian
1866 Taylor <ian@cygnus.com>. */
1870 /* This is a special case, we leave the size alone, which
1871 will have been made up from all and any lcomms seen. */
1872 #ifndef ZERO_BASED_SEGMENTS
1873 addr
+= segment_info
[i
].scnhdr
.s_size
;
1878 #ifndef ZERO_BASED_SEGMENTS
1879 addr
+= size_section (abfd
, i
);
1881 size_section (abfd
, i
);
1888 /* Turn the gas native symbol table shape into a coff symbol table */
1889 crawl_symbols (&filehdr
, abfd
);
1890 #if !defined(TC_H8300) && !defined(TC_Z8K)
1891 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1893 fixup_mdeps (segment_info
[i
].frchainP
->frch_root
);
1894 fixup_segment (&segment_info
[i
], i
);
1898 file_cursor
= FILHSZ
+ SCNHSZ
* filehdr
.f_nscns
;
1900 bfd_seek (abfd
, file_cursor
, 0);
1903 do_relocs_for (abfd
, &file_cursor
);
1905 do_linenos_for (abfd
, &file_cursor
);
1908 /* Plant the data */
1910 fill_section (abfd
, &filehdr
, &file_cursor
);
1914 filehdr
.f_magic
= COFF_MAGIC
;
1915 filehdr
.f_timdat
= time (0);
1916 filehdr
.f_flags
= COFF_FLAGS
| coff_flags
;
1920 filehdr
.f_flags
|= F_LNNO
;
1924 filehdr
.f_flags
|= F_RELFLG
;
1935 unsigned int symtable_size
= filehdr
.f_nsyms
* SYMESZ
;
1936 char *buffer1
= malloc (symtable_size
+ string_byte_count
+ 4);
1937 char *ptr
= buffer1
;
1938 filehdr
.f_symptr
= bfd_tell (abfd
);
1939 w_symbols (abfd
, buffer1
, symbol_rootP
);
1940 w_strings (buffer1
+ symtable_size
);
1941 bfd_write (buffer1
, 1, symtable_size
+ string_byte_count
+ 4, abfd
);
1945 coff_header_append (abfd
, &filehdr
, &aouthdr
);
1947 if (bfd_close_all_done (abfd
) == false)
1948 as_fatal ("Can't close %s: %s", out_file_name
,
1949 bfd_errmsg (bfd_error
));
1954 DEFUN (change_to_section
, (name
, len
, exp
),
1956 unsigned int len AND
1960 /* Find out if we've already got a section of this name etc */
1961 for (i
= SEG_E0
; i
< SEG_E9
&& segment_info
[i
].scnhdr
.s_name
[0]; i
++)
1963 if (strncmp (segment_info
[i
].scnhdr
.s_name
, name
, len
) == 0)
1965 subseg_new (i
, exp
);
1970 /* No section, add one */
1971 strncpy (segment_info
[i
].scnhdr
.s_name
, name
, 8);
1972 subseg_new (i
, exp
);
1976 DEFUN_VOID (obj_coff_section
)
1978 /* Strip out the section name */
1980 char *section_name_end
;
1986 section_name
= input_line_pointer
;
1987 c
= get_symbol_end ();
1988 section_name_end
= input_line_pointer
;
1990 len
= section_name_end
- section_name
;
1991 input_line_pointer
++;
1994 /* Some 386 assemblers stick a quoted string at the end of
1995 a .section; we just ignore it. */
1996 if (c
== ',' && *input_line_pointer
!= '"')
1998 exp
= get_absolute_expression ();
2000 else if (*input_line_pointer
== ','
2001 && input_line_pointer
[1] != '"')
2003 input_line_pointer
++;
2004 exp
= get_absolute_expression ();
2009 while (! is_end_of_line
[*input_line_pointer
])
2010 ++input_line_pointer
;
2013 change_to_section (section_name
, len
, exp
);
2014 *section_name_end
= c
;
2021 change_to_section (".text", 5, get_absolute_expression ());
2028 change_to_section (".data", 5, get_absolute_expression ());
2032 c_symbol_merge (debug
, normal
)
2036 S_SET_DATA_TYPE (normal
, S_GET_DATA_TYPE (debug
));
2037 S_SET_STORAGE_CLASS (normal
, S_GET_STORAGE_CLASS (debug
));
2039 if (S_GET_NUMBER_AUXILIARY (debug
) > S_GET_NUMBER_AUXILIARY (normal
))
2041 S_SET_NUMBER_AUXILIARY (normal
, S_GET_NUMBER_AUXILIARY (debug
));
2042 } /* take the most we have */
2044 if (S_GET_NUMBER_AUXILIARY (debug
) > 0)
2046 memcpy ((char *) &normal
->sy_symbol
.ost_auxent
[0], (char *) &debug
->sy_symbol
.ost_auxent
[0], S_GET_NUMBER_AUXILIARY (debug
) * AUXESZ
);
2047 } /* Move all the auxiliary information */
2049 /* Move the debug flags. */
2050 SF_SET_DEBUG_FIELD (normal
, SF_GET_DEBUG_FIELD (debug
));
2051 } /* c_symbol_merge() */
2054 DEFUN (c_line_new
, (symbol
, paddr
, line_number
, frag
),
2055 symbolS
* symbol AND
2057 unsigned short line_number AND
2060 struct lineno_list
*new_line
=
2061 (struct lineno_list
*) xmalloc (sizeof (struct lineno_list
));
2063 segment_info_type
*s
= segment_info
+ now_seg
;
2064 new_line
->line
.l_lnno
= line_number
;
2068 if (line_number
== 0)
2070 last_line_symbol
= symbol
;
2071 new_line
->line
.l_addr
.l_symndx
= (long) symbol
;
2075 new_line
->line
.l_addr
.l_paddr
= paddr
;
2078 new_line
->frag
= (char *) frag
;
2079 new_line
->next
= (struct lineno_list
*) NULL
;
2082 if (s
->lineno_list_head
== (struct lineno_list
*) NULL
)
2084 s
->lineno_list_head
= new_line
;
2088 s
->lineno_list_tail
->next
= new_line
;
2090 s
->lineno_list_tail
= new_line
;
2091 return LINESZ
* s
->scnhdr
.s_nlnno
++;
2095 c_dot_file_symbol (filename
)
2100 symbolP
= symbol_new (".file",
2103 &zero_address_frag
);
2105 S_SET_STORAGE_CLASS (symbolP
, C_FILE
);
2106 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
2107 SA_SET_FILE_FNAME (symbolP
, filename
);
2113 listing_source_file (filename
);
2119 SF_SET_DEBUG (symbolP
);
2120 S_SET_VALUE (symbolP
, (long) previous_file_symbol
);
2122 previous_file_symbol
= symbolP
;
2124 /* Make sure that the symbol is first on the symbol chain */
2125 if (symbol_rootP
!= symbolP
)
2127 if (symbolP
== symbol_lastP
)
2129 symbol_lastP
= symbol_lastP
->sy_previous
;
2130 } /* if it was the last thing on the list */
2132 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
2133 symbol_insert (symbolP
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
2134 symbol_rootP
= symbolP
;
2135 } /* if not first on the list */
2137 } /* c_dot_file_symbol() */
2140 * Build a 'section static' symbol.
2144 c_section_symbol (name
, idx
)
2150 symbolP
= symbol_new (name
, idx
,
2152 &zero_address_frag
);
2154 S_SET_STORAGE_CLASS (symbolP
, C_STAT
);
2155 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
2157 SF_SET_STATICS (symbolP
);
2160 } /* c_section_symbol() */
2163 DEFUN (w_symbols
, (abfd
, where
, symbol_rootP
),
2166 symbolS
* symbol_rootP
)
2171 /* First fill in those values we have only just worked out */
2172 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
2174 symbolP
= segment_info
[i
].dot
;
2178 SA_SET_SCN_SCNLEN (symbolP
, segment_info
[i
].scnhdr
.s_size
);
2179 SA_SET_SCN_NRELOC (symbolP
, segment_info
[i
].scnhdr
.s_nreloc
);
2180 SA_SET_SCN_NLINNO (symbolP
, segment_info
[i
].scnhdr
.s_nlnno
);
2186 * Emit all symbols left in the symbol chain.
2188 for (symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next (symbolP
))
2190 /* Used to save the offset of the name. It is used to point
2191 to the string in memory but must be a file offset. */
2192 register char *temp
;
2194 tc_coff_symbol_emit_hook (symbolP
);
2196 temp
= S_GET_NAME (symbolP
);
2197 if (SF_GET_STRING (symbolP
))
2199 S_SET_OFFSET (symbolP
, symbolP
->sy_name_offset
);
2200 S_SET_ZEROES (symbolP
, 0);
2204 bzero (symbolP
->sy_symbol
.ost_entry
.n_name
, SYMNMLEN
);
2205 strncpy (symbolP
->sy_symbol
.ost_entry
.n_name
, temp
, SYMNMLEN
);
2207 where
= symbol_to_chars (abfd
, where
, symbolP
);
2208 S_SET_NAME (symbolP
, temp
);
2214 DEFUN_VOID (obj_coff_lcomm
)
2223 name
= input_line_pointer
;
2226 c
= get_symbol_end ();
2227 p
= input_line_pointer
;
2230 if (*input_line_pointer
!= ',')
2232 as_bad ("Expected comma after name");
2233 ignore_rest_of_line ();
2236 if (*input_line_pointer
== '\n')
2238 as_bad ("Missing size expression");
2241 input_line_pointer
++;
2242 if ((temp
= get_absolute_expression ()) < 0)
2244 as_warn ("lcomm length (%d.) <0! Ignored.", temp
);
2245 ignore_rest_of_line ();
2251 /* Allocate zero static local data in the .data section now
2252 instead of the bss section as a symbol with a value */
2254 segT oldseg
= now_seg
;
2255 int oldsubseg
= now_subseg
;
2257 subseg_new (SEG_DATA
, 10);
2260 record_alignment (SEG_DATA
, 4);
2261 x
= frag_var (rs_fill
, 1, 1, (relax_substateT
) 0, (symbolS
*) 0,
2265 subseg_new (oldseg
, oldsubseg
);
2267 demand_empty_rest_of_line ();
2271 DEFUN (fixup_mdeps
, (frags
),
2276 switch (frags
->fr_type
)
2280 frags
->fr_type
= rs_fill
;
2282 (frags
->fr_next
->fr_address
- frags
->fr_address
- frags
->fr_fix
);
2284 case rs_machine_dependent
:
2285 md_convert_frag (0, frags
);
2290 frags
= frags
->fr_next
;
2297 DEFUN (fixup_segment
, (fixP
, this_segment_type
),
2298 segment_info_type
* segP AND
2299 segT this_segment_type
)
2301 register fixS
* fixP
;
2302 register symbolS
*add_symbolP
;
2303 register symbolS
*sub_symbolP
;
2304 register long add_number
;
2306 register char *place
;
2307 register long where
;
2308 register char pcrel
;
2309 register fragS
*fragP
;
2310 register segT add_symbol_segment
= SEG_ABSOLUTE
;
2313 for (fixP
= segP
->fix_root
; fixP
; fixP
= fixP
->fx_next
)
2315 fragP
= fixP
->fx_frag
;
2317 where
= fixP
->fx_where
;
2318 place
= fragP
->fr_literal
+ where
;
2319 size
= fixP
->fx_size
;
2320 add_symbolP
= fixP
->fx_addsy
;
2322 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME (add_symbolP
))
2324 /* Relocation should be done via the
2325 associated 'bal' entry point
2328 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP
)))
2330 as_bad ("No 'bal' entry point for leafproc %s",
2331 S_GET_NAME (add_symbolP
));
2334 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call (add_symbolP
);
2335 } /* callj relocation */
2337 sub_symbolP
= fixP
->fx_subsy
;
2338 add_number
= fixP
->fx_offset
;
2339 pcrel
= fixP
->fx_pcrel
;
2343 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2344 } /* if there is an addend */
2351 if (S_GET_SEGMENT (sub_symbolP
) != SEG_ABSOLUTE
)
2353 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP
));
2354 } /* not absolute */
2356 add_number
-= S_GET_VALUE (sub_symbolP
);
2359 /* if sub_symbol is in the same segment that add_symbol
2360 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
2362 else if ((S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
)
2363 && (SEG_NORMAL (add_symbol_segment
)
2364 || (add_symbol_segment
== SEG_ABSOLUTE
)))
2366 /* Difference of 2 symbols from same segment. */
2367 /* Can't make difference of 2 undefineds: 'value' means */
2368 /* something different for N_UNDF. */
2370 /* Makes no sense to use the difference of 2 arbitrary symbols
2371 * as the target of a call instruction.
2375 as_bad ("callj to difference of 2 symbols");
2377 #endif /* TC_I960 */
2378 add_number
+= S_GET_VALUE (add_symbolP
) -
2379 S_GET_VALUE (sub_symbolP
);
2382 fixP
->fx_addsy
= NULL
;
2386 /* Different segments in subtraction. */
2387 know (!(S_IS_EXTERNAL (sub_symbolP
) && (S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
)));
2389 if ((S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
))
2391 add_number
-= S_GET_VALUE (sub_symbolP
);
2395 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
2396 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2397 S_GET_NAME (sub_symbolP
), fragP
->fr_address
+ where
);
2400 } /* if sub_symbolP */
2404 if (add_symbol_segment
== this_segment_type
&& pcrel
)
2407 * This fixup was made when the symbol's segment was
2408 * SEG_UNKNOWN, but it is now in the local segment.
2409 * So we know how to do the address without relocation.
2412 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
2413 * in which cases it modifies *fixP as appropriate. In the case
2414 * of a 'calls', no further work is required, and *fixP has been
2415 * set up to make the rest of the code below a no-op.
2418 #endif /* TC_I960 */
2420 add_number
+= S_GET_VALUE (add_symbolP
);
2421 add_number
-= md_pcrel_from (fixP
);
2423 /* On the 386 we must adjust by the segment
2424 vaddr as well. Ian Taylor. */
2425 add_number
-= segP
->scnhdr
.s_vaddr
;
2427 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
2428 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2432 switch (add_symbol_segment
)
2436 reloc_callj (fixP
); /* See comment about reloc_callj() above*/
2437 #endif /* TC_I960 */
2438 add_number
+= S_GET_VALUE (add_symbolP
);
2439 fixP
->fx_addsy
= NULL
;
2444 add_number
+= S_GET_VALUE (add_symbolP
) +
2445 segment_info
[S_GET_SEGMENT (add_symbolP
)].scnhdr
.s_paddr
;
2450 if ((int) fixP
->fx_bit_fixP
== 13)
2452 /* This is a COBR instruction. They have only a
2453 * 13-bit displacement and are only to be used
2454 * for local branches: flag as error, don't generate
2457 as_bad ("can't use COBR format with external label");
2458 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2461 #endif /* TC_I960 */
2463 /* 386 COFF uses a peculiar format in
2464 which the value of a common symbol is
2465 stored in the .text segment (I've
2466 checked this on SVR3.2 and SCO 3.2.2)
2467 Ian Taylor <ian@cygnus.com>. */
2468 add_number
+= S_GET_VALUE (add_symbolP
);
2473 } /* switch on symbol seg */
2474 } /* if not in local seg */
2475 } /* if there was a + symbol */
2479 add_number
-= md_pcrel_from (fixP
);
2480 if (add_symbolP
== 0)
2482 fixP
->fx_addsy
= &abs_symbol
;
2483 } /* if there's an add_symbol */
2485 /* On the 386 we must adjust by the segment vaddr
2486 as well. Ian Taylor. */
2487 add_number
-= segP
->scnhdr
.s_vaddr
;
2491 if (!fixP
->fx_bit_fixP
)
2494 (add_number
& ~0xFF) && ((add_number
& ~0xFF) != (-1 & ~0xFF))) ||
2496 (add_number
& ~0xFFFF) && ((add_number
& ~0xFFFF) != (-1 & ~0xFFFF))))
2498 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
2499 add_number
, size
, fragP
->fr_address
+ where
);
2500 } /* generic error checking */
2501 #ifdef WARN_SIGNED_OVERFLOW_WORD
2502 /* Warn if a .word value is too large when treated as
2503 a signed number. We already know it is not too
2504 negative. This is to catch over-large switches
2505 generated by gcc on the 68k. */
2508 && add_number
> 0x7fff)
2509 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
2510 add_number
, fragP
->fr_address
+ where
);
2512 } /* not a bit fix */
2513 /* once this fix has been applied, we don't have to output anything
2514 nothing more need be done -*/
2515 md_apply_fix (fixP
, add_number
);
2517 } /* For each fixS in this segment. */
2520 } /* fixup_segment() */