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/{3|5}00 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) */
87 static symbolS
*last_line_symbol
;
89 /* Add 4 to the real value to get the index and compensate the
90 negatives. This vector is used by S_GET_SEGMENT to turn a coff
91 section number into a segment number
93 static symbolS
*previous_file_symbol
= NULL
;
94 void c_symbol_merge ();
97 symbolS
*c_section_symbol ();
99 void EXFUN (bfd_as_write_hook
, (struct internal_filehdr
*,
102 static void EXFUN (fixup_segment
, (segment_info_type
*segP
,
103 segT this_segment_type
));
106 static void EXFUN (fixup_mdeps
, (fragS
*,
111 static void EXFUN (fill_section
, (bfd
* abfd
,
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_bss
,(void));
142 static void EXFUN( obj_coff_ident
,(void));
143 static void EXFUN (obj_coff_endef
, (void));
144 static void EXFUN (obj_coff_line
, (void));
145 static void EXFUN (obj_coff_ln
, (int));
146 static void EXFUN (obj_coff_scl
, (void));
147 static void EXFUN (obj_coff_size
, (void));
148 static void EXFUN (obj_coff_tag
, (void));
149 static void EXFUN (obj_coff_type
, (void));
150 static void EXFUN (obj_coff_val
, (void));
151 void EXFUN (obj_coff_section
, (void));
152 static void EXFUN (tag_init
, (void));
153 static void EXFUN (tag_insert
, (char *name
, symbolS
* symbolP
));
156 static struct hash_control
*tag_hash
;
157 static symbolS
*def_symbol_in_progress
= NULL
;
159 const pseudo_typeS obj_pseudo_table
[] =
161 {"def", obj_coff_def
, 0},
162 {"dim", obj_coff_dim
, 0},
163 {"endef", obj_coff_endef
, 0},
164 {"line", obj_coff_line
, 0},
165 {"ln", obj_coff_ln
, 0},
166 {"appline", obj_coff_ln
, 1},
167 {"scl", obj_coff_scl
, 0},
168 {"size", obj_coff_size
, 0},
169 {"tag", obj_coff_tag
, 0},
170 {"type", obj_coff_type
, 0},
171 {"val", obj_coff_val
, 0},
172 {"section", obj_coff_section
, 0},
173 {"use", obj_coff_section
, 0},
174 {"sect", obj_coff_section
, 0},
175 {"text", obj_coff_text
, 0},
176 {"data", obj_coff_data
, 0},
177 {"bss", obj_coff_bss
, 0},
178 {"ident", obj_coff_ident
, 0},
179 {"ABORT", s_abort
, 0},
180 {"lcomm", obj_coff_lcomm
, 0},
182 /* The m88k uses sdef instead of def. */
183 {"sdef", obj_coff_def
, 0},
185 {NULL
} /* end sentinel */
186 }; /* obj_pseudo_table */
192 We allow more than just the standard 3 sections, infact, we allow
193 10 sections, (though the usual three have to be there).
195 This structure performs the mappings for us:
200 static struct internal_scnhdr bss_section_header;
201 struct internal_scnhdr data_section_header;
202 struct internal_scnhdr text_section_header;
204 const segT N_TYPE_seg [32] =
218 seg_info_type seg_info_off_by_4
[N_SEG
] =
249 #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
250 #define SEG_INFO_FROM_SEG_NUMBER(x) (seg_info_off_by_4[(x)])
254 DEFUN (relax_align
, (address
, alignment
),
255 register relax_addressT address AND
256 register long alignment
)
259 relax_addressT new_address
;
261 mask
= ~((~0) << alignment
);
262 new_address
= (address
+ mask
) & (~mask
);
263 return (new_address
- address
);
264 } /* relax_align() */
268 DEFUN (s_get_segment
, (x
),
271 return SEG_INFO_FROM_SECTION_NUMBER (x
->sy_symbol
.ost_entry
.n_scnum
).seg_t
;
276 /* calculate the size of the frag chain and fill in the section header
277 to contain all of it, also fill in the addr of the sections */
279 DEFUN (size_section
, (abfd
, idx
),
284 unsigned int size
= 0;
285 fragS
*frag
= segment_info
[idx
].frchainP
->frch_root
;
288 size
= frag
->fr_address
;
289 if (frag
->fr_address
!= size
)
291 printf ("Out of step\n");
292 size
= frag
->fr_address
;
295 switch (frag
->fr_type
)
297 #ifdef TC_COFF_SIZEMACHDEP
298 case rs_machine_dependent
:
299 size
+= TC_COFF_SIZEMACHDEP (frag
);
304 size
+= frag
->fr_fix
;
305 size
+= frag
->fr_offset
* frag
->fr_var
;
308 size
+= frag
->fr_fix
;
309 size
+= relax_align (size
, frag
->fr_offset
);
311 frag
= frag
->fr_next
;
313 segment_info
[idx
].scnhdr
.s_size
= size
;
319 DEFUN (count_entries_in_chain
, (idx
),
322 unsigned int nrelocs
;
325 /* Count the relocations */
326 fixup_ptr
= segment_info
[idx
].fix_root
;
328 while (fixup_ptr
!= (fixS
*) NULL
)
330 if (TC_COUNT_RELOC (fixup_ptr
))
335 if (fixup_ptr
->fx_r_type
== RELOC_CONSTH
)
344 fixup_ptr
= fixup_ptr
->fx_next
;
349 /* output all the relocations for a section */
351 DEFUN (do_relocs_for
, (abfd
, h
, file_cursor
),
353 object_headers
* h AND
354 unsigned long *file_cursor
)
356 unsigned int nrelocs
;
358 unsigned long reloc_start
= *file_cursor
;
360 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
362 if (segment_info
[idx
].scnhdr
.s_name
[0])
364 struct external_reloc
*ext_ptr
;
365 struct external_reloc
*external_reloc_vec
;
366 unsigned int external_reloc_size
;
367 unsigned int base
= segment_info
[idx
].scnhdr
.s_paddr
;
368 fixS
*fix_ptr
= segment_info
[idx
].fix_root
;
369 nrelocs
= count_entries_in_chain (idx
);
372 /* Bypass this stuff if no relocs. This also incidentally
373 avoids a SCO bug, where free(malloc(0)) tends to crash. */
375 external_reloc_size
= nrelocs
* RELSZ
;
377 (struct external_reloc
*) malloc (external_reloc_size
);
379 ext_ptr
= external_reloc_vec
;
381 /* Fill in the internal coff style reloc struct from the
382 internal fix list. */
386 struct internal_reloc intr
;
388 /* Only output some of the relocations */
389 if (TC_COUNT_RELOC (fix_ptr
))
391 #ifdef TC_RELOC_MANGLE
392 TC_RELOC_MANGLE (fix_ptr
, &intr
, base
);
396 symbol_ptr
= fix_ptr
->fx_addsy
;
398 intr
.r_type
= TC_COFF_FIX2RTYPE (fix_ptr
);
400 base
+ fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
;
402 intr
.r_offset
= fix_ptr
->fx_offset
;
406 /* Turn the segment of the symbol into an offset. */
409 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
412 intr
.r_symndx
= dot
->sy_number
;
416 intr
.r_symndx
= symbol_ptr
->sy_number
;
426 (void) bfd_coff_swap_reloc_out (abfd
, &intr
, ext_ptr
);
431 /* The 29k has a special kludge for the high 16 bit
432 reloc. Two relocations are emited, R_IHIHALF,
433 and R_IHCONST. The second one doesn't contain a
434 symbol, but uses the value for offset. */
436 if (intr
.r_type
== R_IHIHALF
)
438 /* now emit the second bit */
439 intr
.r_type
= R_IHCONST
;
440 intr
.r_symndx
= fix_ptr
->fx_addnumber
;
441 (void) bfd_coff_swap_reloc_out (abfd
, &intr
, ext_ptr
);
447 fix_ptr
= fix_ptr
->fx_next
;
450 /* Write out the reloc table */
451 bfd_write ((PTR
) external_reloc_vec
, 1, external_reloc_size
,
453 free (external_reloc_vec
);
455 /* Fill in section header info. */
456 segment_info
[idx
].scnhdr
.s_relptr
= *file_cursor
;
457 *file_cursor
+= external_reloc_size
;
458 segment_info
[idx
].scnhdr
.s_nreloc
= nrelocs
;
463 segment_info
[idx
].scnhdr
.s_relptr
= 0;
467 /* Set relocation_size field in file headers */
468 H_SET_RELOCATION_SIZE (h
, *file_cursor
- reloc_start
, 0);
472 /* run through a frag chain and write out the data to go with it, fill
473 in the scnhdrs with the info on the file postions
476 DEFUN (fill_section
, (abfd
, h
, file_cursor
),
478 object_headers
*h AND
479 unsigned long *file_cursor
)
483 unsigned int paddr
= 0;
485 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
487 unsigned int offset
= 0;
489 struct internal_scnhdr
*s
= &(segment_info
[i
].scnhdr
);
493 fragS
*frag
= segment_info
[i
].frchainP
->frch_root
;
500 buffer
= xmalloc (s
->s_size
);
501 s
->s_scnptr
= *file_cursor
;
503 know (s
->s_paddr
== paddr
);
505 if (strcmp (s
->s_name
, ".text") == 0)
506 s
->s_flags
|= STYP_TEXT
;
507 else if (strcmp (s
->s_name
, ".data") == 0)
508 s
->s_flags
|= STYP_DATA
;
509 else if (strcmp (s
->s_name
, ".bss") == 0)
512 s
->s_flags
|= STYP_BSS
;
515 /* Apparently the SVR3 linker is confused by noload
516 sections. So is the UDI mondfe program. */
517 s
->s_flags
|= STYP_NOLOAD
;
521 else if (strcmp (s
->s_name
, ".lit") == 0)
522 s
->s_flags
= STYP_LIT
| STYP_TEXT
;
523 else if (strcmp (s
->s_name
, ".init") == 0)
524 s
->s_flags
|= STYP_TEXT
;
525 else if (strcmp (s
->s_name
, ".fini") == 0)
526 s
->s_flags
|= STYP_TEXT
;
527 else if (strncmp (s
->s_name
, ".comment", 8) == 0)
528 s
->s_flags
|= STYP_INFO
;
532 unsigned int fill_size
;
533 switch (frag
->fr_type
)
535 case rs_machine_dependent
:
538 memcpy (buffer
+ frag
->fr_address
,
541 offset
+= frag
->fr_fix
;
550 memcpy (buffer
+ frag
->fr_address
,
553 offset
+= frag
->fr_fix
;
556 fill_size
= frag
->fr_var
;
557 if (fill_size
&& frag
->fr_offset
> 0)
560 unsigned int off
= frag
->fr_fix
;
561 for (count
= frag
->fr_offset
; count
; count
--)
563 if (fill_size
< s
->s_size
)
565 memcpy (buffer
+ frag
->fr_address
+ off
,
566 frag
->fr_literal
+ frag
->fr_fix
,
579 frag
= frag
->fr_next
;
584 if (s
->s_scnptr
!= 0)
586 bfd_write (buffer
, s
->s_size
, 1, abfd
);
587 *file_cursor
+= s
->s_size
;
596 /* Coff file generation & utilities */
599 DEFUN (coff_header_append
, (abfd
, h
),
607 bfd_seek (abfd
, 0, 0);
609 #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
610 H_SET_MAGIC_NUMBER (h
, COFF_MAGIC
);
611 H_SET_VERSION_STAMP (h
, 0);
612 H_SET_ENTRY_POINT (h
, 0);
613 H_SET_TEXT_START (h
, segment_info
[SEG_E0
].frchainP
->frch_root
->fr_address
);
614 H_SET_DATA_START (h
, segment_info
[SEG_E1
].frchainP
->frch_root
->fr_address
);
615 H_SET_SIZEOF_OPTIONAL_HEADER (h
, bfd_coff_swap_aouthdr_out(abfd
, &h
->aouthdr
,
617 #else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
618 H_SET_SIZEOF_OPTIONAL_HEADER (h
, 0);
619 #endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
621 i
= bfd_coff_swap_filehdr_out (abfd
, &h
->filehdr
, buffer
);
623 bfd_write (buffer
, i
, 1, abfd
);
624 bfd_write (buffero
, H_GET_SIZEOF_OPTIONAL_HEADER (h
), 1, abfd
);
626 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
628 if (segment_info
[i
].scnhdr
.s_name
[0])
631 bfd_coff_swap_scnhdr_out (abfd
,
632 &(segment_info
[i
].scnhdr
),
634 bfd_write (buffer
, size
, 1, abfd
);
641 DEFUN (symbol_to_chars
, (abfd
, where
, symbolP
),
646 unsigned int numaux
= symbolP
->sy_symbol
.ost_entry
.n_numaux
;
650 /* Turn any symbols with register attributes into abs symbols */
651 if (S_GET_SEGMENT (symbolP
) == SEG_REGISTER
)
653 S_SET_SEGMENT (symbolP
, SEG_ABSOLUTE
);
655 /* At the same time, relocate all symbols to their output value */
657 val
= (segment_info
[S_GET_SEGMENT (symbolP
)].scnhdr
.s_paddr
658 + S_GET_VALUE (symbolP
));
660 S_SET_VALUE (symbolP
, val
);
662 symbolP
->sy_symbol
.ost_entry
.n_value
= val
;
664 where
+= bfd_coff_swap_sym_out (abfd
, &symbolP
->sy_symbol
.ost_entry
,
667 for (i
= 0; i
< numaux
; i
++)
669 where
+= bfd_coff_swap_aux_out (abfd
,
670 &symbolP
->sy_symbol
.ost_auxent
[i
],
671 S_GET_DATA_TYPE (symbolP
),
672 S_GET_STORAGE_CLASS (symbolP
),
681 obj_symbol_new_hook (symbolP
)
684 char underscore
= 0; /* Symbol has leading _ */
686 /* Effective symbol */
687 /* Store the pointer in the offset. */
688 S_SET_ZEROES (symbolP
, 0L);
689 S_SET_DATA_TYPE (symbolP
, T_NULL
);
690 S_SET_STORAGE_CLASS (symbolP
, 0);
691 S_SET_NUMBER_AUXILIARY (symbolP
, 0);
692 /* Additional information */
693 symbolP
->sy_symbol
.ost_flags
= 0;
694 /* Auxiliary entries */
695 bzero ((char *) &symbolP
->sy_symbol
.ost_auxent
[0], AUXESZ
);
697 #ifdef STRIP_UNDERSCORE
698 /* Remove leading underscore at the beginning of the symbol.
699 * This is to be compatible with the standard librairies.
701 if (*S_GET_NAME (symbolP
) == '_')
704 S_SET_NAME (symbolP
, S_GET_NAME (symbolP
) + 1);
705 } /* strip underscore */
706 #endif /* STRIP_UNDERSCORE */
708 if (S_IS_STRING (symbolP
))
709 SF_SET_STRING (symbolP
);
710 if (!underscore
&& S_IS_LOCAL (symbolP
))
711 SF_SET_LOCAL (symbolP
);
714 } /* obj_symbol_new_hook() */
718 stack_init (chunk_size
, element_size
)
719 unsigned long chunk_size
;
720 unsigned long element_size
;
724 if ((st
= (stack
*) malloc (sizeof (stack
))) == (stack
*) 0)
726 if ((st
->data
= malloc (chunk_size
)) == (char *) 0)
732 st
->size
= chunk_size
;
733 st
->chunk_size
= chunk_size
;
734 st
->element_size
= element_size
;
747 stack_push (st
, element
)
751 if (st
->pointer
+ st
->element_size
>= st
->size
)
753 st
->size
+= st
->chunk_size
;
754 if ((st
->data
= xrealloc (st
->data
, st
->size
)) == (char *) 0)
757 memcpy (st
->data
+ st
->pointer
, element
, st
->element_size
);
758 st
->pointer
+= st
->element_size
;
759 return st
->data
+ st
->pointer
;
766 if ((st
->pointer
-= st
->element_size
) < 0)
772 return st
->data
+ st
->pointer
;
779 return st
->data
+ st
->pointer
- st
->element_size
;
784 * Handle .ln directives.
788 obj_coff_ln (appline
)
793 if (! appline
&& def_symbol_in_progress
!= NULL
)
795 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
796 demand_empty_rest_of_line ();
798 } /* wrong context */
801 obstack_next_free (&frags
) - frag_now
->fr_literal
,
802 l
= get_absolute_expression (),
812 listing_source_line (l
);
817 demand_empty_rest_of_line ();
819 } /* obj_coff_line() */
824 * Handle .def directives.
826 * One might ask : why can't we symbol_new if the symbol does not
827 * already exist and fill it with debug information. Because of
828 * the C_EFCN special symbol. It would clobber the value of the
829 * function symbol before we have a chance to notice that it is
830 * a C_EFCN. And a second reason is that the code is more clear this
831 * way. (at least I think it is :-).
835 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
836 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
837 *input_line_pointer == '\t') \
838 input_line_pointer++;
841 DEFUN (obj_coff_def
, (what
),
844 char name_end
; /* Char after the end of name */
845 char *symbol_name
; /* Name of the debug symbol */
846 char *symbol_name_copy
; /* Temporary copy of the name */
847 unsigned int symbol_name_length
;
848 /*$char* directiveP;$ *//* Name of the pseudo opcode */
849 /*$char directive[MAX_DIRECTIVE];$ *//* Backup of the directive */
850 /*$char end = 0;$ *//* If 1, stop parsing */
852 if (def_symbol_in_progress
!= NULL
)
854 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
855 demand_empty_rest_of_line ();
857 } /* if not inside .def/.endef */
861 def_symbol_in_progress
= (symbolS
*) obstack_alloc (¬es
, sizeof (*def_symbol_in_progress
));
862 bzero (def_symbol_in_progress
, sizeof (*def_symbol_in_progress
));
864 symbol_name
= input_line_pointer
;
865 name_end
= get_symbol_end ();
866 symbol_name_length
= strlen (symbol_name
);
867 symbol_name_copy
= xmalloc (symbol_name_length
+ 1);
868 strcpy (symbol_name_copy
, symbol_name
);
870 /* Initialize the new symbol */
871 #ifdef STRIP_UNDERSCORE
872 S_SET_NAME (def_symbol_in_progress
, (*symbol_name_copy
== '_'
873 ? symbol_name_copy
+ 1
874 : symbol_name_copy
));
875 #else /* STRIP_UNDERSCORE */
876 S_SET_NAME (def_symbol_in_progress
, symbol_name_copy
);
877 #endif /* STRIP_UNDERSCORE */
878 /* free(symbol_name_copy); */
879 def_symbol_in_progress
->sy_name_offset
= ~0;
880 def_symbol_in_progress
->sy_number
= ~0;
881 def_symbol_in_progress
->sy_frag
= &zero_address_frag
;
883 if (S_IS_STRING (def_symbol_in_progress
))
885 SF_SET_STRING (def_symbol_in_progress
);
888 *input_line_pointer
= name_end
;
890 demand_empty_rest_of_line ();
892 } /* obj_coff_def() */
894 unsigned int dim_index
;
896 DEFUN_VOID (obj_coff_endef
)
898 symbolS
*symbolP
= 0;
899 /* DIM BUG FIX sac@cygnus.com */
901 if (def_symbol_in_progress
== NULL
)
903 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
904 demand_empty_rest_of_line ();
906 } /* if not inside .def/.endef */
908 /* Set the section number according to storage class. */
909 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress
))
914 SF_SET_TAG (def_symbol_in_progress
);
915 /* intentional fallthrough */
918 SF_SET_DEBUG (def_symbol_in_progress
);
919 S_SET_SEGMENT (def_symbol_in_progress
, SEG_DEBUG
);
923 SF_SET_LOCAL (def_symbol_in_progress
); /* Do not emit this symbol. */
924 /* intentional fallthrough */
926 SF_SET_PROCESS (def_symbol_in_progress
); /* Will need processing before writing */
927 /* intentional fallthrough */
929 S_SET_SEGMENT (def_symbol_in_progress
, SEG_E0
);
931 if (strcmp (S_GET_NAME (def_symbol_in_progress
), ".bf") == 0)
933 if (function_lineoff
< 0)
935 fprintf (stderr
, "`.bf' symbol without preceding function\n");
936 } /* missing function symbol */
937 SA_GET_SYM_LNNOPTR (last_line_symbol
) = function_lineoff
;
939 SF_SET_PROCESS (last_line_symbol
);
940 function_lineoff
= -1;
946 #endif /* C_AUTOARG */
956 SF_SET_DEBUG (def_symbol_in_progress
);
957 S_SET_SEGMENT (def_symbol_in_progress
, SEG_ABSOLUTE
);
963 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
969 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress
));
971 } /* switch on storage class */
973 /* Now that we have built a debug symbol, try to find if we should
974 merge with an existing symbol or not. If a symbol is C_EFCN or
975 SEG_ABSOLUTE or untagged SEG_DEBUG it never merges. We also
976 don't merge labels, which are in a different namespace, nor
977 symbols which have not yet been defined since they are typically
978 unique, nor do we merge tags with non-tags. */
980 /* Two cases for functions. Either debug followed by definition or
981 definition followed by debug. For definition first, we will
982 merge the debug symbol into the definition. For debug first, the
983 lineno entry MUST point to the definition function or else it
984 will point off into space when crawl_symbols() merges the debug
985 symbol into the real symbol. Therefor, let's presume the debug
986 symbol is a real function reference. */
988 /* FIXME-SOON If for some reason the definition label/symbol is
989 never seen, this will probably leave an undefined symbol at link
992 if (S_GET_STORAGE_CLASS (def_symbol_in_progress
) == C_EFCN
993 || S_GET_STORAGE_CLASS (def_symbol_in_progress
) == C_LABEL
994 || (S_GET_SEGMENT (def_symbol_in_progress
) == SEG_DEBUG
995 && !SF_GET_TAG (def_symbol_in_progress
))
996 || S_GET_SEGMENT (def_symbol_in_progress
) == SEG_ABSOLUTE
997 || def_symbol_in_progress
->sy_value
.X_seg
!= absolute_section
998 || (symbolP
= symbol_find_base (S_GET_NAME (def_symbol_in_progress
), DO_NOT_STRIP
)) == NULL
999 || (SF_GET_TAG (def_symbol_in_progress
) != SF_GET_TAG (symbolP
)))
1001 symbol_append (def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
,
1006 /* This symbol already exists, merge the newly created symbol
1007 into the This is not mandatory. The linker can handle
1008 duplicate symbols correctly. But I guess that it save a *lot*
1009 of space if the assembly file defines a lot of
1012 /* The debug entry (def_symbol_in_progress) is merged into the
1013 previous definition. */
1015 c_symbol_merge (def_symbol_in_progress
, symbolP
);
1016 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
1017 def_symbol_in_progress
= symbolP
;
1019 if (SF_GET_FUNCTION (def_symbol_in_progress
)
1020 || SF_GET_TAG (def_symbol_in_progress
))
1022 /* For functions, and tags, the symbol *must* be where the
1023 debug symbol appears. Move the existing symbol to the
1025 /* If it already is at the end of the symbol list, do nothing */
1026 if (def_symbol_in_progress
!= symbol_lastP
)
1028 symbol_remove (def_symbol_in_progress
, &symbol_rootP
,
1030 symbol_append (def_symbol_in_progress
, symbol_lastP
,
1031 &symbol_rootP
, &symbol_lastP
);
1032 } /* if not already in place */
1034 } /* normal or mergable */
1036 if (SF_GET_TAG (def_symbol_in_progress
)
1037 && symbol_find_base (S_GET_NAME (def_symbol_in_progress
), DO_NOT_STRIP
) == NULL
)
1039 tag_insert (S_GET_NAME (def_symbol_in_progress
), def_symbol_in_progress
);
1042 if (SF_GET_FUNCTION (def_symbol_in_progress
))
1044 know (sizeof (def_symbol_in_progress
) <= sizeof (long));
1046 = c_line_new (def_symbol_in_progress
, 0, 0, &zero_address_frag
);
1048 SF_SET_PROCESS (def_symbol_in_progress
);
1050 if (symbolP
== NULL
)
1052 /* That is, if this is the first time we've seen the
1054 symbol_table_insert (def_symbol_in_progress
);
1055 } /* definition follows debug */
1056 } /* Create the line number entry pointing to the function being defined */
1058 def_symbol_in_progress
= NULL
;
1059 demand_empty_rest_of_line ();
1064 DEFUN_VOID (obj_coff_dim
)
1066 register int dim_index
;
1068 if (def_symbol_in_progress
== NULL
)
1070 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
1071 demand_empty_rest_of_line ();
1073 } /* if not inside .def/.endef */
1075 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1077 for (dim_index
= 0; dim_index
< DIMNUM
; dim_index
++)
1079 SKIP_WHITESPACES ();
1080 SA_SET_SYM_DIMEN (def_symbol_in_progress
, dim_index
, get_absolute_expression ());
1082 switch (*input_line_pointer
)
1086 input_line_pointer
++;
1090 as_warn ("badly formed .dim directive ignored");
1091 /* intentional fallthrough */
1096 } /* switch on following character */
1097 } /* for each dimension */
1099 demand_empty_rest_of_line ();
1101 } /* obj_coff_dim() */
1108 if (def_symbol_in_progress
== NULL
)
1112 } /* if it looks like a stabs style line */
1114 this_base
= get_absolute_expression ();
1115 if (this_base
> line_base
)
1117 line_base
= this_base
;
1126 listing_source_line (line_base
);
1130 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1131 SA_SET_SYM_LNNO (def_symbol_in_progress
, line_base
);
1133 demand_empty_rest_of_line ();
1135 } /* obj_coff_line() */
1140 if (def_symbol_in_progress
== NULL
)
1142 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1143 demand_empty_rest_of_line ();
1145 } /* if not inside .def/.endef */
1147 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1148 SA_SET_SYM_SIZE (def_symbol_in_progress
, get_absolute_expression ());
1149 demand_empty_rest_of_line ();
1151 } /* obj_coff_size() */
1156 if (def_symbol_in_progress
== NULL
)
1158 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1159 demand_empty_rest_of_line ();
1161 } /* if not inside .def/.endef */
1163 S_SET_STORAGE_CLASS (def_symbol_in_progress
, get_absolute_expression ());
1164 demand_empty_rest_of_line ();
1166 } /* obj_coff_scl() */
1174 if (def_symbol_in_progress
== NULL
)
1176 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1177 demand_empty_rest_of_line ();
1179 } /* if not inside .def/.endef */
1181 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1182 symbol_name
= input_line_pointer
;
1183 name_end
= get_symbol_end ();
1185 /* Assume that the symbol referred to by .tag is always defined. */
1186 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1187 SA_SET_SYM_TAGNDX (def_symbol_in_progress
, (long) tag_find_or_make (symbol_name
));
1188 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress
) == 0L)
1190 as_warn ("tag not found for .tag %s", symbol_name
);
1193 SF_SET_TAGGED (def_symbol_in_progress
);
1194 *input_line_pointer
= name_end
;
1196 demand_empty_rest_of_line ();
1198 } /* obj_coff_tag() */
1203 if (def_symbol_in_progress
== NULL
)
1205 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1206 demand_empty_rest_of_line ();
1208 } /* if not inside .def/.endef */
1210 S_SET_DATA_TYPE (def_symbol_in_progress
, get_absolute_expression ());
1212 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress
)) &&
1213 S_GET_STORAGE_CLASS (def_symbol_in_progress
) != C_TPDEF
)
1215 SF_SET_FUNCTION (def_symbol_in_progress
);
1216 } /* is a function */
1218 demand_empty_rest_of_line ();
1220 } /* obj_coff_type() */
1225 if (def_symbol_in_progress
== NULL
)
1227 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1228 demand_empty_rest_of_line ();
1230 } /* if not inside .def/.endef */
1232 if (is_name_beginner (*input_line_pointer
))
1234 char *symbol_name
= input_line_pointer
;
1235 char name_end
= get_symbol_end ();
1237 if (!strcmp (symbol_name
, "."))
1239 def_symbol_in_progress
->sy_frag
= frag_now
;
1240 S_SET_VALUE (def_symbol_in_progress
, obstack_next_free (&frags
) - frag_now
->fr_literal
);
1241 /* If the .val is != from the .def (e.g. statics) */
1243 else if (strcmp (S_GET_NAME (def_symbol_in_progress
), symbol_name
))
1245 def_symbol_in_progress
->sy_value
.X_add_symbol
=
1246 symbol_find_or_make (symbol_name
);
1247 def_symbol_in_progress
->sy_value
.X_subtract_symbol
= NULL
;
1248 def_symbol_in_progress
->sy_value
.X_add_number
= 0;
1249 def_symbol_in_progress
->sy_value
.X_seg
= undefined_section
;
1251 /* If the segment is undefined when the forward reference is
1252 resolved, then copy the segment id from the forward
1254 SF_SET_GET_SEGMENT (def_symbol_in_progress
);
1256 /* FIXME: gcc can generate address expressions
1257 here in unusual cases (search for "obscure"
1258 in sdbout.c). We just ignore the offset
1259 here, thus generating incorrect debugging
1260 information. We ignore the rest of the
1263 /* Otherwise, it is the name of a non debug symbol and
1264 its value will be calculated later. */
1265 *input_line_pointer
= name_end
;
1267 /* FIXME: this is to avoid an error message in the
1268 FIXME case mentioned just above. */
1269 while (! is_end_of_line
[*input_line_pointer
])
1270 ++input_line_pointer
;
1274 S_SET_VALUE (def_symbol_in_progress
, get_absolute_expression ());
1275 } /* if symbol based */
1277 demand_empty_rest_of_line ();
1279 } /* obj_coff_val() */
1282 * Maintain a list of the tagnames of the structres.
1288 tag_hash
= hash_new ();
1293 tag_insert (name
, symbolP
)
1297 register char *error_string
;
1299 if (*(error_string
= hash_jam (tag_hash
, name
, (char *) symbolP
)))
1301 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1302 name
, error_string
);
1305 } /* tag_insert() */
1308 tag_find_or_make (name
)
1313 if ((symbolP
= tag_find (name
)) == NULL
)
1315 symbolP
= symbol_new (name
,
1318 &zero_address_frag
);
1320 tag_insert (S_GET_NAME (symbolP
), symbolP
);
1324 } /* tag_find_or_make() */
1330 #ifdef STRIP_UNDERSCORE
1333 #endif /* STRIP_UNDERSCORE */
1334 return ((symbolS
*) hash_find (tag_hash
, name
));
1338 obj_read_begin_hook ()
1340 /* These had better be the same. Usually 18 bytes. */
1342 know (sizeof (SYMENT
) == sizeof (AUXENT
));
1343 know (SYMESZ
== AUXESZ
);
1348 } /* obj_read_begin_hook() */
1350 /* This function runs through the symbol table and puts all the
1351 externals onto another chain */
1353 /* The chain of externals */
1354 symbolS
*symbol_externP
= NULL
;
1355 symbolS
*symbol_extern_lastP
= NULL
;
1358 symbolS
*last_functionP
= NULL
;
1362 DEFUN_VOID (yank_symbols
)
1365 unsigned int symbol_number
= 0;
1366 unsigned int last_file_symno
= 0;
1368 for (symbolP
= symbol_rootP
;
1370 symbolP
= symbolP
? symbol_next (symbolP
) : symbol_rootP
)
1372 if (!SF_GET_DEBUG (symbolP
))
1374 /* Debug symbols do not need all this rubbish */
1375 symbolS
*real_symbolP
;
1377 /* L* and C_EFCN symbols never merge. */
1378 if (!SF_GET_LOCAL (symbolP
)
1379 && S_GET_STORAGE_CLASS (symbolP
) != C_LABEL
1380 && symbolP
->sy_value
.X_seg
== absolute_section
1381 && (real_symbolP
= symbol_find_base (S_GET_NAME (symbolP
), DO_NOT_STRIP
))
1382 && real_symbolP
!= symbolP
)
1384 /* FIXME-SOON: where do dups come from?
1385 Maybe tag references before definitions? xoxorich. */
1386 /* Move the debug data from the debug symbol to the
1387 real symbol. Do NOT do the oposite (i.e. move from
1388 real symbol to debug symbol and remove real symbol from the
1389 list.) Because some pointers refer to the real symbol
1390 whereas no pointers refer to the debug symbol. */
1391 c_symbol_merge (symbolP
, real_symbolP
);
1392 /* Replace the current symbol by the real one */
1393 /* The symbols will never be the last or the first
1394 because : 1st symbol is .file and 3 last symbols are
1395 .text, .data, .bss */
1396 symbol_remove (real_symbolP
, &symbol_rootP
, &symbol_lastP
);
1397 symbol_insert (real_symbolP
, symbolP
, &symbol_rootP
, &symbol_lastP
);
1398 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1399 symbolP
= real_symbolP
;
1400 } /* if not local but dup'd */
1402 if (flagseen
['R'] && (S_GET_SEGMENT (symbolP
) == SEG_E1
))
1404 S_SET_SEGMENT (symbolP
, SEG_E0
);
1405 } /* push data into text */
1407 resolve_symbol_value (symbolP
);
1409 if (!S_IS_DEFINED (symbolP
) && !SF_GET_LOCAL (symbolP
))
1411 S_SET_EXTERNAL (symbolP
);
1413 else if (S_GET_STORAGE_CLASS (symbolP
) == C_NULL
)
1415 if (S_GET_SEGMENT (symbolP
) == SEG_E0
)
1417 S_SET_STORAGE_CLASS (symbolP
, C_LABEL
);
1421 S_SET_STORAGE_CLASS (symbolP
, C_STAT
);
1425 /* Mainly to speed up if not -g */
1426 if (SF_GET_PROCESS (symbolP
))
1428 /* Handle the nested blocks auxiliary info. */
1429 if (S_GET_STORAGE_CLASS (symbolP
) == C_BLOCK
)
1431 if (!strcmp (S_GET_NAME (symbolP
), ".bb"))
1432 stack_push (block_stack
, (char *) &symbolP
);
1435 register symbolS
*begin_symbolP
;
1436 begin_symbolP
= *(symbolS
**) stack_pop (block_stack
);
1437 if (begin_symbolP
== (symbolS
*) 0)
1438 as_warn ("mismatched .eb");
1440 SA_SET_SYM_ENDNDX (begin_symbolP
, symbol_number
+ 2);
1443 /* If we are able to identify the type of a function, and we
1444 are out of a function (last_functionP == 0) then, the
1445 function symbol will be associated with an auxiliary
1447 if (last_functionP
== (symbolS
*) 0 &&
1448 SF_GET_FUNCTION (symbolP
))
1450 last_functionP
= symbolP
;
1452 if (S_GET_NUMBER_AUXILIARY (symbolP
) < 1)
1454 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
1455 } /* make it at least 1 */
1457 /* Clobber possible stale .dim information. */
1459 /* Iffed out by steve - this fries the lnnoptr info too */
1460 bzero (symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
,
1461 sizeof (symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
));
1464 /* The C_FCN doesn't need any additional information. I
1465 don't even know if this is needed for sdb. But the
1466 standard assembler generates it, so... */
1467 if (S_GET_STORAGE_CLASS (symbolP
) == C_EFCN
)
1469 if (last_functionP
== (symbolS
*) 0)
1470 as_fatal ("C_EFCN symbol out of scope");
1471 SA_SET_SYM_FSIZE (last_functionP
,
1472 (long) (S_GET_VALUE (symbolP
) -
1473 S_GET_VALUE (last_functionP
)));
1474 SA_SET_SYM_ENDNDX (last_functionP
, symbol_number
);
1475 last_functionP
= (symbolS
*) 0;
1479 else if (SF_GET_TAG (symbolP
))
1481 /* First descriptor of a structure must point to
1482 the first slot after the structure description. */
1483 last_tagP
= symbolP
;
1486 else if (S_GET_STORAGE_CLASS (symbolP
) == C_EOS
)
1488 /* +2 take in account the current symbol */
1489 SA_SET_SYM_ENDNDX (last_tagP
, symbol_number
+ 2);
1491 else if (S_GET_STORAGE_CLASS (symbolP
) == C_FILE
)
1493 if (S_GET_VALUE (symbolP
))
1495 S_SET_VALUE (symbolP
, last_file_symno
);
1496 last_file_symno
= symbol_number
;
1497 } /* no one points at the first .file symbol */
1498 } /* if debug or tag or eos or file */
1500 /* We must put the external symbols apart. The loader
1501 does not bomb if we do not. But the references in
1502 the endndx field for a .bb symbol are not corrected
1503 if an external symbol is removed between .bb and .be.
1504 I.e in the following case :
1505 [20] .bb endndx = 22
1508 ld will move the symbol 21 to the end of the list but
1509 endndx will still be 22 instead of 21. */
1512 if (SF_GET_LOCAL (symbolP
))
1514 /* remove C_EFCN and LOCAL (L...) symbols */
1515 /* next pointer remains valid */
1516 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1519 else if (!S_IS_DEFINED (symbolP
)
1520 && !S_IS_DEBUG (symbolP
)
1521 && !SF_GET_STATICS (symbolP
) &&
1522 S_GET_STORAGE_CLASS (symbolP
) == C_EXT
)
1523 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1524 /* if external, Remove from the list */
1525 symbolS
*hold
= symbol_previous (symbolP
);
1527 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1528 symbol_clear_list_pointers (symbolP
);
1529 symbol_append (symbolP
, symbol_extern_lastP
, &symbol_externP
, &symbol_extern_lastP
);
1534 if (SF_GET_STRING (symbolP
))
1536 symbolP
->sy_name_offset
= string_byte_count
;
1537 string_byte_count
+= strlen (S_GET_NAME (symbolP
)) + 1;
1541 symbolP
->sy_name_offset
= 0;
1542 } /* fix "long" names */
1544 symbolP
->sy_number
= symbol_number
;
1545 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (symbolP
);
1546 } /* if local symbol */
1547 } /* traverse the symbol list */
1548 return symbol_number
;
1554 DEFUN_VOID (glue_symbols
)
1556 unsigned int symbol_number
= 0;
1558 for (symbolP
= symbol_externP
; symbol_externP
;)
1560 symbolS
*tmp
= symbol_externP
;
1563 symbol_remove (tmp
, &symbol_externP
, &symbol_extern_lastP
);
1564 symbol_append (tmp
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
1567 if (SF_GET_STRING (tmp
))
1569 tmp
->sy_name_offset
= string_byte_count
;
1570 string_byte_count
+= strlen (S_GET_NAME (tmp
)) + 1;
1574 tmp
->sy_name_offset
= 0;
1575 } /* fix "long" names */
1577 tmp
->sy_number
= symbol_number
;
1578 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (tmp
);
1579 } /* append the entire extern chain */
1580 return symbol_number
;
1585 DEFUN_VOID (tie_tags
)
1587 unsigned int symbol_number
= 0;
1590 for (symbolP
= symbol_rootP
; symbolP
; symbolP
=
1591 symbol_next (symbolP
))
1593 symbolP
->sy_number
= symbol_number
;
1597 if (SF_GET_TAGGED (symbolP
))
1601 ((symbolS
*) SA_GET_SYM_TAGNDX (symbolP
))->sy_number
);
1604 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (symbolP
);
1606 return symbol_number
;
1611 DEFUN (crawl_symbols
, (h
, abfd
),
1612 object_headers
*h AND
1619 /* Initialize the stack used to keep track of the matching .bb .be */
1621 block_stack
= stack_init (512, sizeof (symbolS
*));
1623 /* The symbol list should be ordered according to the following sequence
1626 * . debug entries for functions
1627 * . fake symbols for the sections, including.text .data and .bss
1629 * . undefined symbols
1630 * But this is not mandatory. The only important point is to put the
1631 * undefined symbols at the end of the list.
1634 if (symbol_rootP
== NULL
1635 || S_GET_STORAGE_CLASS (symbol_rootP
) != C_FILE
)
1637 c_dot_file_symbol ("fake");
1639 /* Is there a .file symbol ? If not insert one at the beginning. */
1642 * Build up static symbols for the sections, they are filled in later
1646 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
1648 if (segment_info
[i
].scnhdr
.s_name
[0])
1652 strncpy (name
, segment_info
[i
].scnhdr
.s_name
, 8);
1654 segment_info
[i
].dot
= c_section_symbol (name
, i
- SEG_E0
+ 1);
1659 /* Take all the externals out and put them into another chain */
1660 H_SET_SYMBOL_TABLE_SIZE (h
, yank_symbols ());
1661 /* Take the externals and glue them onto the end.*/
1662 H_SET_SYMBOL_TABLE_SIZE (h
, H_GET_SYMBOL_COUNT (h
) + glue_symbols ());
1664 H_SET_SYMBOL_TABLE_SIZE (h
, tie_tags ());
1665 know (symbol_externP
== NULL
);
1666 know (symbol_extern_lastP
== NULL
);
1672 * Find strings by crawling along symbol table chain.
1676 DEFUN (w_strings
, (where
),
1681 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
1682 md_number_to_chars (where
, string_byte_count
, 4);
1684 for (symbolP
= symbol_rootP
;
1686 symbolP
= symbol_next (symbolP
))
1690 if (SF_GET_STRING (symbolP
))
1692 size
= strlen (S_GET_NAME (symbolP
)) + 1;
1694 memcpy (where
, S_GET_NAME (symbolP
), size
);
1703 DEFUN (do_linenos_for
, (abfd
, h
, file_cursor
),
1705 object_headers
* h AND
1706 unsigned long *file_cursor
)
1709 unsigned long start
= *file_cursor
;
1711 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
1713 segment_info_type
*s
= segment_info
+ idx
;
1716 if (s
->scnhdr
.s_nlnno
!= 0)
1718 struct lineno_list
*line_ptr
;
1720 struct external_lineno
*buffer
=
1721 (struct external_lineno
*) xmalloc (s
->scnhdr
.s_nlnno
* LINESZ
);
1723 struct external_lineno
*dst
= buffer
;
1725 /* Run through the table we've built and turn it into its external
1726 form, take this chance to remove duplicates */
1728 for (line_ptr
= s
->lineno_list_head
;
1729 line_ptr
!= (struct lineno_list
*) NULL
;
1730 line_ptr
= line_ptr
->next
)
1733 if (line_ptr
->line
.l_lnno
== 0)
1735 /* Turn a pointer to a symbol into the symbols' index */
1736 line_ptr
->line
.l_addr
.l_symndx
=
1737 ((symbolS
*) line_ptr
->line
.l_addr
.l_symndx
)->sy_number
;
1741 line_ptr
->line
.l_addr
.l_paddr
+= ((struct frag
*) (line_ptr
->frag
))->fr_address
;
1745 (void) bfd_coff_swap_lineno_out (abfd
, &(line_ptr
->line
), dst
);
1750 s
->scnhdr
.s_lnnoptr
= *file_cursor
;
1752 bfd_write (buffer
, 1, s
->scnhdr
.s_nlnno
* LINESZ
, abfd
);
1755 *file_cursor
+= s
->scnhdr
.s_nlnno
* LINESZ
;
1758 H_SET_LINENO_SIZE (h
, *file_cursor
- start
);
1762 /* Now we run through the list of frag chains in a segment and
1763 make all the subsegment frags appear at the end of the
1764 list, as if the seg 0 was extra long */
1767 DEFUN_VOID (remove_subsegs
)
1771 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1773 frchainS
*head
= segment_info
[i
].frchainP
;
1775 fragS
*prev_frag
= &dummy
;
1777 while (head
&& head
->frch_seg
== i
)
1779 prev_frag
->fr_next
= head
->frch_root
;
1780 prev_frag
= head
->frch_last
;
1781 head
= head
->frch_next
;
1783 prev_frag
->fr_next
= 0;
1790 DEFUN_VOID (write_object_file
)
1793 struct frchain
*frchain_ptr
;
1795 object_headers headers
;
1796 unsigned long file_cursor
;
1799 abfd
= bfd_openw (out_file_name
, TARGET_FORMAT
);
1804 as_perror ("FATAL: Can't create %s", out_file_name
);
1807 bfd_set_format (abfd
, bfd_object
);
1808 bfd_set_arch_mach (abfd
, BFD_ARCH
, machine
);
1810 string_byte_count
= 4;
1812 for (frchain_ptr
= frchain_root
;
1813 frchain_ptr
!= (struct frchain
*) NULL
;
1814 frchain_ptr
= frchain_ptr
->frch_next
)
1816 /* Run through all the sub-segments and align them up. Also close any
1817 open frags. We tack a .fill onto the end of the frag chain so
1818 that any .align's size can be worked by looking at the next
1821 subseg_new (frchain_ptr
->frch_seg
, frchain_ptr
->frch_subseg
);
1822 #ifndef SUB_SEGMENT_ALIGN
1823 #define SUB_SEGMENT_ALIGN(SEG) 1
1825 frag_align (SUB_SEGMENT_ALIGN (now_seg
), NOP_OPCODE
);
1826 frag_wane (frag_now
);
1827 frag_now
->fr_fix
= 0;
1828 know (frag_now
->fr_next
== NULL
);
1835 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1837 relax_segment (segment_info
[i
].frchainP
->frch_root
, i
);
1840 H_SET_NUMBER_OF_SECTIONS (&headers
, 0);
1842 /* Find out how big the sections are, and set the addresses. */
1844 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1848 segment_info
[i
].scnhdr
.s_paddr
= addr
;
1849 segment_info
[i
].scnhdr
.s_vaddr
= addr
;
1851 if (segment_info
[i
].scnhdr
.s_name
[0])
1853 H_SET_NUMBER_OF_SECTIONS (&headers
,
1854 H_GET_NUMBER_OF_SECTIONS (&headers
) + 1);
1857 size
= size_section (abfd
, i
);
1861 H_SET_TEXT_SIZE (&headers
, size
);
1862 else if (i
== SEG_E1
)
1863 H_SET_DATA_SIZE (&headers
, size
);
1864 else if (i
== SEG_E2
)
1865 H_SET_BSS_SIZE (&headers
, size
);
1868 /* Turn the gas native symbol table shape into a coff symbol table */
1869 crawl_symbols (&headers
, abfd
);
1871 if (string_byte_count
== 4)
1872 string_byte_count
= 0;
1874 H_SET_STRING_SIZE (&headers
, string_byte_count
);
1876 #if !defined(TC_H8300) && !defined(TC_Z8K)
1877 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1879 fixup_mdeps (segment_info
[i
].frchainP
->frch_root
, &headers
, i
);
1880 fixup_segment (&segment_info
[i
], i
);
1884 file_cursor
= H_GET_TEXT_FILE_OFFSET (&headers
);
1886 bfd_seek (abfd
, file_cursor
, 0);
1888 /* Plant the data */
1890 fill_section (abfd
, &headers
, &file_cursor
);
1892 do_relocs_for (abfd
, &headers
, &file_cursor
);
1894 do_linenos_for (abfd
, &headers
, &file_cursor
);
1896 H_SET_FILE_MAGIC_NUMBER (&headers
, COFF_MAGIC
);
1897 #ifndef OBJ_COFF_OMIT_TIMESTAMP
1898 H_SET_TIME_STAMP (&headers
, (long)time((long*)0));
1900 H_SET_TIME_STAMP (&headers
, 0);
1903 #ifdef KEEP_RELOC_INFO
1904 H_SET_FLAGS (&headers
, ((H_GET_LINENO_SIZE(&headers
) ? 0 : F_LNNO
) |
1905 COFF_FLAGS
| coff_flags
));
1907 H_SET_FLAGS (&headers
, ((H_GET_LINENO_SIZE(&headers
) ? 0 : F_LNNO
) |
1908 (H_GET_RELOCATION_SIZE(&headers
) ? 0 : F_RELFLG
) |
1909 COFF_FLAGS
| coff_flags
));
1913 unsigned int symtable_size
= H_GET_SYMBOL_TABLE_SIZE (&headers
);
1914 char *buffer1
= xmalloc (symtable_size
+ string_byte_count
+ 1);
1915 char *ptr
= buffer1
;
1916 H_SET_SYMBOL_TABLE_POINTER (&headers
, bfd_tell (abfd
));
1917 w_symbols (abfd
, buffer1
, symbol_rootP
);
1918 if (string_byte_count
> 0)
1919 w_strings (buffer1
+ symtable_size
);
1920 bfd_write (buffer1
, 1, symtable_size
+ string_byte_count
, abfd
);
1924 coff_header_append (abfd
, &headers
);
1926 if (bfd_close_all_done (abfd
) == false)
1927 as_fatal ("Can't close %s: %s", out_file_name
,
1928 bfd_errmsg (bfd_error
));
1933 DEFUN (change_to_section
, (name
, len
, exp
),
1935 unsigned int len AND
1939 /* Find out if we've already got a section of this name etc */
1940 for (i
= SEG_E0
; i
< SEG_E9
&& segment_info
[i
].scnhdr
.s_name
[0]; i
++)
1942 if (strncmp (segment_info
[i
].scnhdr
.s_name
, name
, len
) == 0)
1944 subseg_new (i
, exp
);
1948 /* No section, add one */
1949 strncpy (segment_info
[i
].scnhdr
.s_name
, name
, 8);
1950 segment_info
[i
].scnhdr
.s_flags
= STYP_REG
;
1951 subseg_new (i
, exp
);
1955 * implement the .section pseudo op:
1956 * .section name {, "flags"}
1958 * | +--- optional flags: 'b' for bss
1960 * +-- section name 'l' for lib
1965 * But if the argument is not a quoted string, treat it as a
1966 * subsegment number.
1970 DEFUN_VOID (obj_coff_section
)
1972 /* Strip out the section name */
1974 char *section_name_end
;
1981 section_name
= input_line_pointer
;
1982 c
= get_symbol_end ();
1983 section_name_end
= input_line_pointer
;
1985 len
= section_name_end
- section_name
;
1986 input_line_pointer
++;
1992 else if (*input_line_pointer
== ',')
1995 ++input_line_pointer
;
2003 if (*input_line_pointer
!= '"')
2004 exp
= get_absolute_expression ();
2007 ++input_line_pointer
;
2008 while (*input_line_pointer
!= '"'
2009 && ! is_end_of_line
[*input_line_pointer
])
2011 switch (*input_line_pointer
)
2013 case 'b': flags
|= STYP_BSS
; break;
2014 case 'i': flags
|= STYP_INFO
; break;
2015 case 'l': flags
|= STYP_LIB
; break;
2016 case 'n': flags
|= STYP_NOLOAD
; break;
2017 case 'o': flags
|= STYP_OVER
; break;
2018 case 'w': flags
|= STYP_DATA
; break;
2019 case 'x': flags
|= STYP_TEXT
; break;
2021 as_warn("unknown section attribute '%c'",
2022 *input_line_pointer
);
2025 ++input_line_pointer
;
2027 if (*input_line_pointer
== '"')
2028 ++input_line_pointer
;
2032 change_to_section (section_name
, len
, exp
);
2034 segment_info
[now_seg
].scnhdr
.s_flags
|= flags
;
2036 *section_name_end
= c
;
2043 change_to_section (".text", 5, get_absolute_expression ());
2050 change_to_section (".data", 5, get_absolute_expression ());
2056 if (*input_line_pointer
== '\n') /* .bss */
2057 change_to_section(".bss",4, get_absolute_expression());
2058 else /* .bss id,expr */
2065 segT current_seg
= now_seg
; /* save current seg */
2066 subsegT current_subseg
= now_subseg
;
2067 change_to_section (".comment", 8, 0); /* .comment seg */
2068 stringer (1); /* read string */
2069 subseg_new (current_seg
, current_subseg
); /* restore current seg */
2073 c_symbol_merge (debug
, normal
)
2077 S_SET_DATA_TYPE (normal
, S_GET_DATA_TYPE (debug
));
2078 S_SET_STORAGE_CLASS (normal
, S_GET_STORAGE_CLASS (debug
));
2080 if (S_GET_NUMBER_AUXILIARY (debug
) > S_GET_NUMBER_AUXILIARY (normal
))
2082 S_SET_NUMBER_AUXILIARY (normal
, S_GET_NUMBER_AUXILIARY (debug
));
2083 } /* take the most we have */
2085 if (S_GET_NUMBER_AUXILIARY (debug
) > 0)
2087 memcpy ((char *) &normal
->sy_symbol
.ost_auxent
[0], (char *) &debug
->sy_symbol
.ost_auxent
[0], S_GET_NUMBER_AUXILIARY (debug
) * AUXESZ
);
2088 } /* Move all the auxiliary information */
2090 /* Move the debug flags. */
2091 SF_SET_DEBUG_FIELD (normal
, SF_GET_DEBUG_FIELD (debug
));
2092 } /* c_symbol_merge() */
2095 DEFUN (c_line_new
, (symbol
, paddr
, line_number
, frag
),
2096 symbolS
* symbol AND
2098 unsigned short line_number AND
2101 struct lineno_list
*new_line
=
2102 (struct lineno_list
*) xmalloc (sizeof (struct lineno_list
));
2104 segment_info_type
*s
= segment_info
+ now_seg
;
2105 new_line
->line
.l_lnno
= line_number
;
2107 if (line_number
== 0)
2109 last_line_symbol
= symbol
;
2110 new_line
->line
.l_addr
.l_symndx
= (long) symbol
;
2114 new_line
->line
.l_addr
.l_paddr
= paddr
;
2117 new_line
->frag
= (char *) frag
;
2118 new_line
->next
= (struct lineno_list
*) NULL
;
2121 if (s
->lineno_list_head
== (struct lineno_list
*) NULL
)
2123 s
->lineno_list_head
= new_line
;
2127 s
->lineno_list_tail
->next
= new_line
;
2129 s
->lineno_list_tail
= new_line
;
2130 return LINESZ
* s
->scnhdr
.s_nlnno
++;
2134 c_dot_file_symbol (filename
)
2139 symbolP
= symbol_new (".file",
2142 &zero_address_frag
);
2144 S_SET_STORAGE_CLASS (symbolP
, C_FILE
);
2145 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
2146 SA_SET_FILE_FNAME (symbolP
, filename
);
2152 listing_source_file (filename
);
2158 SF_SET_DEBUG (symbolP
);
2159 S_SET_VALUE (symbolP
, (long) previous_file_symbol
);
2161 previous_file_symbol
= symbolP
;
2163 /* Make sure that the symbol is first on the symbol chain */
2164 if (symbol_rootP
!= symbolP
)
2166 if (symbolP
== symbol_lastP
)
2168 symbol_lastP
= symbol_lastP
->sy_previous
;
2169 } /* if it was the last thing on the list */
2171 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
2172 symbol_insert (symbolP
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
2173 symbol_rootP
= symbolP
;
2174 } /* if not first on the list */
2176 } /* c_dot_file_symbol() */
2179 * Build a 'section static' symbol.
2183 c_section_symbol (name
, idx
)
2189 symbolP
= symbol_new (name
, idx
,
2191 &zero_address_frag
);
2193 S_SET_STORAGE_CLASS (symbolP
, C_STAT
);
2194 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
2196 SF_SET_STATICS (symbolP
);
2199 } /* c_section_symbol() */
2202 DEFUN (w_symbols
, (abfd
, where
, symbol_rootP
),
2205 symbolS
* symbol_rootP
)
2210 /* First fill in those values we have only just worked out */
2211 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
2213 symbolP
= segment_info
[i
].dot
;
2217 SA_SET_SCN_SCNLEN (symbolP
, segment_info
[i
].scnhdr
.s_size
);
2218 SA_SET_SCN_NRELOC (symbolP
, segment_info
[i
].scnhdr
.s_nreloc
);
2219 SA_SET_SCN_NLINNO (symbolP
, segment_info
[i
].scnhdr
.s_nlnno
);
2225 * Emit all symbols left in the symbol chain.
2227 for (symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next (symbolP
))
2229 /* Used to save the offset of the name. It is used to point
2230 to the string in memory but must be a file offset. */
2231 register char *temp
;
2233 tc_coff_symbol_emit_hook (symbolP
);
2235 temp
= S_GET_NAME (symbolP
);
2236 if (SF_GET_STRING (symbolP
))
2238 S_SET_OFFSET (symbolP
, symbolP
->sy_name_offset
);
2239 S_SET_ZEROES (symbolP
, 0);
2243 bzero (symbolP
->sy_symbol
.ost_entry
.n_name
, SYMNMLEN
);
2244 strncpy (symbolP
->sy_symbol
.ost_entry
.n_name
, temp
, SYMNMLEN
);
2246 where
= symbol_to_chars (abfd
, where
, symbolP
);
2247 S_SET_NAME (symbolP
, temp
);
2253 DEFUN_VOID (obj_coff_lcomm
)
2261 name
= input_line_pointer
;
2263 c
= get_symbol_end ();
2264 p
= input_line_pointer
;
2267 if (*input_line_pointer
!= ',')
2269 as_bad ("Expected comma after name");
2270 ignore_rest_of_line ();
2273 if (*input_line_pointer
== '\n')
2275 as_bad ("Missing size expression");
2278 input_line_pointer
++;
2279 if ((temp
= get_absolute_expression ()) < 0)
2281 as_warn ("lcomm length (%d.) <0! Ignored.", temp
);
2282 ignore_rest_of_line ();
2287 symbolP
= symbol_find_or_make(name
);
2289 if (S_GET_SEGMENT(symbolP
) == SEG_UNKNOWN
&&
2290 S_GET_VALUE(symbolP
) == 0)
2295 segT current_seg
= now_seg
; /* save current seg */
2296 subsegT current_subseg
= now_subseg
;
2298 subseg_new (SEG_E2
, 1);
2299 symbolP
->sy_frag
= frag_now
;
2300 p
= frag_var(rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
2303 subseg_new (current_seg
, current_subseg
); /* restore current seg */
2304 S_SET_SEGMENT(symbolP
, SEG_E2
);
2305 S_SET_STORAGE_CLASS(symbolP
, C_STAT
);
2309 as_bad("Symbol %s already defined", name
);
2311 demand_empty_rest_of_line();
2315 DEFUN (fixup_mdeps
, (frags
, h
, this_segment
),
2317 object_headers
* h AND
2320 subseg_change (this_segment
, 0);
2323 switch (frags
->fr_type
)
2327 frags
->fr_type
= rs_fill
;
2329 (frags
->fr_next
->fr_address
- frags
->fr_address
- frags
->fr_fix
);
2331 case rs_machine_dependent
:
2332 md_convert_frag (h
, frags
);
2338 frags
= frags
->fr_next
;
2344 DEFUN (fixup_segment
, (segP
, this_segment_type
),
2345 segment_info_type
* segP AND
2346 segT this_segment_type
)
2348 register fixS
* fixP
;
2349 register symbolS
*add_symbolP
;
2350 register symbolS
*sub_symbolP
;
2351 register long add_number
;
2353 register char *place
;
2354 register long where
;
2355 register char pcrel
;
2356 register fragS
*fragP
;
2357 register segT add_symbol_segment
= SEG_ABSOLUTE
;
2360 for (fixP
= segP
->fix_root
; fixP
; fixP
= fixP
->fx_next
)
2362 fragP
= fixP
->fx_frag
;
2364 where
= fixP
->fx_where
;
2365 place
= fragP
->fr_literal
+ where
;
2366 size
= fixP
->fx_size
;
2367 add_symbolP
= fixP
->fx_addsy
;
2369 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME (add_symbolP
))
2371 /* Relocation should be done via the
2372 associated 'bal' entry point
2375 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP
)))
2377 as_bad ("No 'bal' entry point for leafproc %s",
2378 S_GET_NAME (add_symbolP
));
2381 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call (add_symbolP
);
2382 } /* callj relocation */
2384 sub_symbolP
= fixP
->fx_subsy
;
2385 add_number
= fixP
->fx_offset
;
2386 pcrel
= fixP
->fx_pcrel
;
2390 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2391 } /* if there is an addend */
2398 if (S_GET_SEGMENT (sub_symbolP
) != SEG_ABSOLUTE
)
2400 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP
));
2401 } /* not absolute */
2403 add_number
-= S_GET_VALUE (sub_symbolP
);
2406 /* if sub_symbol is in the same segment that add_symbol
2407 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
2409 else if ((S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
)
2410 && (SEG_NORMAL (add_symbol_segment
)
2411 || (add_symbol_segment
== SEG_ABSOLUTE
)))
2413 /* Difference of 2 symbols from same segment. */
2414 /* Can't make difference of 2 undefineds: 'value' means */
2415 /* something different for N_UNDF. */
2417 /* Makes no sense to use the difference of 2 arbitrary symbols
2418 as the target of a call instruction. */
2421 as_bad ("callj to difference of 2 symbols");
2423 #endif /* TC_I960 */
2424 add_number
+= S_GET_VALUE (add_symbolP
) -
2425 S_GET_VALUE (sub_symbolP
);
2428 fixP
->fx_addsy
= NULL
;
2432 /* Different segments in subtraction. */
2433 know (!(S_IS_EXTERNAL (sub_symbolP
) && (S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
)));
2435 if ((S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
))
2437 add_number
-= S_GET_VALUE (sub_symbolP
);
2441 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
2442 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2443 S_GET_NAME (sub_symbolP
), fragP
->fr_address
+ where
);
2446 } /* if sub_symbolP */
2450 if (add_symbol_segment
== this_segment_type
&& pcrel
)
2453 * This fixup was made when the symbol's segment was
2454 * SEG_UNKNOWN, but it is now in the local segment.
2455 * So we know how to do the address without relocation.
2458 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
2459 * in which cases it modifies *fixP as appropriate. In the case
2460 * of a 'calls', no further work is required, and *fixP has been
2461 * set up to make the rest of the code below a no-op.
2464 #endif /* TC_I960 */
2466 add_number
+= S_GET_VALUE (add_symbolP
);
2467 add_number
-= md_pcrel_from (fixP
);
2469 /* On the 386 we must adjust by the segment
2470 vaddr as well. Ian Taylor. */
2471 add_number
-= segP
->scnhdr
.s_vaddr
;
2473 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
2474 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2478 switch (add_symbol_segment
)
2482 reloc_callj (fixP
); /* See comment about reloc_callj() above*/
2483 #endif /* TC_I960 */
2484 add_number
+= S_GET_VALUE (add_symbolP
);
2485 fixP
->fx_addsy
= NULL
;
2491 /* This really should be handled in the linker, but
2492 backward compatibility forbids. */
2493 add_number
+= S_GET_VALUE (add_symbolP
);
2495 add_number
+= S_GET_VALUE (add_symbolP
) +
2496 segment_info
[S_GET_SEGMENT (add_symbolP
)].scnhdr
.s_paddr
;
2502 if ((int) fixP
->fx_bit_fixP
== 13)
2504 /* This is a COBR instruction. They have only a
2505 * 13-bit displacement and are only to be used
2506 * for local branches: flag as error, don't generate
2509 as_bad ("can't use COBR format with external label");
2510 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2513 #endif /* TC_I960 */
2515 /* 386 COFF uses a peculiar format in
2516 which the value of a common symbol is
2517 stored in the .text segment (I've
2518 checked this on SVR3.2 and SCO 3.2.2)
2519 Ian Taylor <ian@cygnus.com>. */
2520 if (S_IS_COMMON (add_symbolP
))
2521 add_number
+= S_GET_VALUE (add_symbolP
);
2526 } /* switch on symbol seg */
2527 } /* if not in local seg */
2528 } /* if there was a + symbol */
2532 add_number
-= md_pcrel_from (fixP
);
2533 if (add_symbolP
== 0)
2535 fixP
->fx_addsy
= &abs_symbol
;
2536 } /* if there's an add_symbol */
2538 /* On the 386 we must adjust by the segment vaddr
2539 as well. Ian Taylor. */
2540 add_number
-= segP
->scnhdr
.s_vaddr
;
2544 if (!fixP
->fx_bit_fixP
)
2547 (add_number
& ~0xFF) && ((add_number
& ~0xFF) != (-1 & ~0xFF))) ||
2549 (add_number
& ~0xFFFF) && ((add_number
& ~0xFFFF) != (-1 & ~0xFFFF))))
2551 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
2552 add_number
, size
, fragP
->fr_address
+ where
);
2553 } /* generic error checking */
2554 #ifdef WARN_SIGNED_OVERFLOW_WORD
2555 /* Warn if a .word value is too large when treated as
2556 a signed number. We already know it is not too
2557 negative. This is to catch over-large switches
2558 generated by gcc on the 68k. */
2561 && add_number
> 0x7fff)
2562 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
2563 add_number
, fragP
->fr_address
+ where
);
2565 } /* not a bit fix */
2566 /* once this fix has been applied, we don't have to output anything
2567 nothing more need be done -*/
2568 md_apply_fix (fixP
, add_number
);
2569 } /* For each fixS in this segment. */
2570 } /* fixup_segment() */