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
, (void));
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 {"scl", obj_coff_scl
, 0},
167 {"size", obj_coff_size
, 0},
168 {"tag", obj_coff_tag
, 0},
169 {"type", obj_coff_type
, 0},
170 {"val", obj_coff_val
, 0},
171 {"section", obj_coff_section
, 0},
172 {"use", obj_coff_section
, 0},
173 {"sect", obj_coff_section
, 0},
174 {"text", obj_coff_text
, 0},
175 {"data", obj_coff_data
, 0},
176 {"bss", obj_coff_bss
, 0},
177 {"ident", obj_coff_ident
, 0},
178 {"ABORT", s_abort
, 0},
179 {"lcomm", obj_coff_lcomm
, 0},
180 {NULL
} /* end sentinel */
181 }; /* obj_pseudo_table */
187 We allow more than just the standard 3 sections, infact, we allow
188 10 sections, (though the usual three have to be there).
190 This structure performs the mappings for us:
195 static struct internal_scnhdr bss_section_header;
196 struct internal_scnhdr data_section_header;
197 struct internal_scnhdr text_section_header;
199 const segT N_TYPE_seg [32] =
213 seg_info_type seg_info_off_by_4
[N_SEG
] =
244 #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4])
245 #define SEG_INFO_FROM_SEG_NUMBER(x) (seg_info_off_by_4[(x)])
249 DEFUN (relax_align
, (address
, alignment
),
250 register relax_addressT address AND
251 register long alignment
)
254 relax_addressT new_address
;
256 mask
= ~((~0) << alignment
);
257 new_address
= (address
+ mask
) & (~mask
);
258 return (new_address
- address
);
259 } /* relax_align() */
263 DEFUN (s_get_segment
, (x
),
266 return SEG_INFO_FROM_SECTION_NUMBER (x
->sy_symbol
.ost_entry
.n_scnum
).seg_t
;
271 /* calculate the size of the frag chain and fill in the section header
272 to contain all of it, also fill in the addr of the sections */
274 DEFUN (size_section
, (abfd
, idx
),
279 unsigned int size
= 0;
280 fragS
*frag
= segment_info
[idx
].frchainP
->frch_root
;
283 size
= frag
->fr_address
;
284 if (frag
->fr_address
!= size
)
286 printf ("Out of step\n");
287 size
= frag
->fr_address
;
290 switch (frag
->fr_type
)
292 #ifdef TC_COFF_SIZEMACHDEP
293 case rs_machine_dependent
:
294 size
+= TC_COFF_SIZEMACHDEP (frag
);
299 size
+= frag
->fr_fix
;
300 size
+= frag
->fr_offset
* frag
->fr_var
;
303 size
+= frag
->fr_fix
;
304 size
+= relax_align (size
, frag
->fr_offset
);
306 frag
= frag
->fr_next
;
308 segment_info
[idx
].scnhdr
.s_size
= size
;
314 DEFUN (count_entries_in_chain
, (idx
),
317 unsigned int nrelocs
;
320 /* Count the relocations */
321 fixup_ptr
= segment_info
[idx
].fix_root
;
323 while (fixup_ptr
!= (fixS
*) NULL
)
325 if (TC_COUNT_RELOC (fixup_ptr
))
330 if (fixup_ptr
->fx_r_type
== RELOC_CONSTH
)
339 fixup_ptr
= fixup_ptr
->fx_next
;
344 /* output all the relocations for a section */
346 DEFUN (do_relocs_for
, (abfd
, h
, file_cursor
),
348 object_headers
* h AND
349 unsigned long *file_cursor
)
351 unsigned int nrelocs
;
353 unsigned long reloc_start
= *file_cursor
;
355 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
357 if (segment_info
[idx
].scnhdr
.s_name
[0])
359 struct external_reloc
*ext_ptr
;
360 struct external_reloc
*external_reloc_vec
;
361 unsigned int external_reloc_size
;
362 unsigned int count
= 0;
363 unsigned int base
= segment_info
[idx
].scnhdr
.s_paddr
;
364 fixS
*fix_ptr
= segment_info
[idx
].fix_root
;
365 nrelocs
= count_entries_in_chain (idx
);
367 external_reloc_size
= nrelocs
* RELSZ
;
369 (struct external_reloc
*) malloc (external_reloc_size
);
371 ext_ptr
= external_reloc_vec
;
373 /* Fill in the internal coff style reloc struct from the
374 internal fix list. */
378 struct internal_reloc intr
;
380 /* Only output some of the relocations */
381 if (TC_COUNT_RELOC (fix_ptr
))
383 #ifdef TC_RELOC_MANGLE
384 TC_RELOC_MANGLE (fix_ptr
, &intr
, base
);
388 symbol_ptr
= fix_ptr
->fx_addsy
;
390 intr
.r_type
= TC_COFF_FIX2RTYPE (fix_ptr
);
392 base
+ fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
;
394 intr
.r_offset
= fix_ptr
->fx_offset
;
398 /* Turn the segment of the symbol into an offset. */
401 dot
= segment_info
[S_GET_SEGMENT (symbol_ptr
)].dot
;
404 intr
.r_symndx
= dot
->sy_number
;
408 intr
.r_symndx
= symbol_ptr
->sy_number
;
418 (void) bfd_coff_swap_reloc_out (abfd
, &intr
, ext_ptr
);
423 /* The 29k has a special kludge for the high 16 bit
424 reloc. Two relocations are emited, R_IHIHALF,
425 and R_IHCONST. The second one doesn't contain a
426 symbol, but uses the value for offset. */
428 if (intr
.r_type
== R_IHIHALF
)
430 /* now emit the second bit */
431 intr
.r_type
= R_IHCONST
;
432 intr
.r_symndx
= fix_ptr
->fx_addnumber
;
434 /* The offset to the segment holding the symbol
435 has already been counted in the R_IHIHALF.
436 We don't want to add it in again for the
440 segment_info
[S_GET_SEGMENT (symbol_ptr
)].scnhdr
.s_paddr
;
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 segment_info
[idx
].scnhdr
.s_relptr
= nrelocs
? *file_cursor
: 0;
452 segment_info
[idx
].scnhdr
.s_nreloc
= nrelocs
;
453 bfd_write ((PTR
) external_reloc_vec
, 1, external_reloc_size
, abfd
);
454 *file_cursor
+= external_reloc_size
;
455 free (external_reloc_vec
);
458 /* Set relocation_size field in file headers */
459 H_SET_RELOCATION_SIZE (h
, *file_cursor
- reloc_start
, 0);
463 /* run through a frag chain and write out the data to go with it, fill
464 in the scnhdrs with the info on the file postions
467 DEFUN (fill_section
, (abfd
, h
, file_cursor
),
469 object_headers
*h AND
470 unsigned long *file_cursor
)
474 unsigned int paddr
= 0;
476 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
478 unsigned int offset
= 0;
480 struct internal_scnhdr
*s
= &(segment_info
[i
].scnhdr
);
484 fragS
*frag
= segment_info
[i
].frchainP
->frch_root
;
491 buffer
= xmalloc (s
->s_size
);
492 s
->s_scnptr
= *file_cursor
;
494 know (s
->s_paddr
== paddr
);
496 s
->s_flags
= STYP_REG
;
497 if (strcmp (s
->s_name
, ".text") == 0)
498 s
->s_flags
|= STYP_TEXT
;
499 else if (strcmp (s
->s_name
, ".data") == 0)
500 s
->s_flags
|= STYP_DATA
;
501 else if (strcmp (s
->s_name
, ".bss") == 0)
504 s
->s_flags
|= STYP_BSS
;
506 /* Apparently the SVR3 linker is confused by noload
508 s
->s_flags
|= STYP_NOLOAD
;
511 else if (strcmp (s
->s_name
, ".lit") == 0)
512 s
->s_flags
= STYP_LIT
| STYP_TEXT
;
513 else if (strcmp (s
->s_name
, ".init") == 0)
514 s
->s_flags
|= STYP_TEXT
;
515 else if (strcmp (s
->s_name
, ".fini") == 0)
516 s
->s_flags
|= STYP_TEXT
;
517 else if (strncmp (s
->s_name
, ".comment", 8) == 0)
518 s
->s_flags
|= STYP_INFO
;
522 unsigned int fill_size
;
523 switch (frag
->fr_type
)
525 case rs_machine_dependent
:
528 memcpy (buffer
+ frag
->fr_address
,
531 offset
+= frag
->fr_fix
;
540 memcpy (buffer
+ frag
->fr_address
,
543 offset
+= frag
->fr_fix
;
546 fill_size
= frag
->fr_var
;
550 unsigned int off
= frag
->fr_fix
;
551 for (count
= frag
->fr_offset
; count
; count
--)
553 if (fill_size
< s
->s_size
)
555 memcpy (buffer
+ frag
->fr_address
+ off
,
556 frag
->fr_literal
+ frag
->fr_fix
,
569 frag
= frag
->fr_next
;
574 if (s
->s_scnptr
!= 0)
576 bfd_write (buffer
, s
->s_size
, 1, abfd
);
577 *file_cursor
+= s
->s_size
;
586 /* Coff file generation & utilities */
589 DEFUN (coff_header_append
, (abfd
, h
),
597 bfd_seek (abfd
, 0, 0);
599 #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
600 H_SET_MAGIC_NUMBER (h
, COFF_MAGIC
);
601 H_SET_VERSION_STAMP (h
, 0);
602 H_SET_ENTRY_POINT (h
, 0);
603 H_SET_TEXT_START (h
, segment_info
[SEG_E0
].frchainP
->frch_root
->fr_address
);
604 H_SET_DATA_START (h
, segment_info
[SEG_E1
].frchainP
->frch_root
->fr_address
);
605 H_SET_SIZEOF_OPTIONAL_HEADER (h
, bfd_coff_swap_aouthdr_out(abfd
, &h
->aouthdr
,
607 #else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
608 H_SET_SIZEOF_OPTIONAL_HEADER (h
, 0);
609 #endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */
611 i
= bfd_coff_swap_filehdr_out (abfd
, &h
->filehdr
, buffer
);
613 bfd_write (buffer
, i
, 1, abfd
);
614 bfd_write (buffero
, H_GET_SIZEOF_OPTIONAL_HEADER (h
), 1, abfd
);
616 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
618 if (segment_info
[i
].scnhdr
.s_name
[0])
621 bfd_coff_swap_scnhdr_out (abfd
,
622 &(segment_info
[i
].scnhdr
),
624 bfd_write (buffer
, size
, 1, abfd
);
631 DEFUN (symbol_to_chars
, (abfd
, where
, symbolP
),
636 unsigned int numaux
= symbolP
->sy_symbol
.ost_entry
.n_numaux
;
639 /* Turn any symbols with register attributes into abs symbols */
640 if (S_GET_SEGMENT (symbolP
) == SEG_REGISTER
)
642 S_SET_SEGMENT (symbolP
, SEG_ABSOLUTE
);
644 /* At the same time, relocate all symbols to their output value */
646 S_SET_VALUE (symbolP
,
647 segment_info
[S_GET_SEGMENT (symbolP
)].scnhdr
.s_paddr
648 + S_GET_VALUE (symbolP
));
650 where
+= bfd_coff_swap_sym_out (abfd
, &symbolP
->sy_symbol
.ost_entry
,
653 for (i
= 0; i
< numaux
; i
++)
655 where
+= bfd_coff_swap_aux_out (abfd
,
656 &symbolP
->sy_symbol
.ost_auxent
[i
],
657 S_GET_DATA_TYPE (symbolP
),
658 S_GET_STORAGE_CLASS (symbolP
),
669 obj_symbol_new_hook (symbolP
)
672 char underscore
= 0; /* Symbol has leading _ */
674 /* Effective symbol */
675 /* Store the pointer in the offset. */
676 S_SET_ZEROES (symbolP
, 0L);
677 S_SET_DATA_TYPE (symbolP
, T_NULL
);
678 S_SET_STORAGE_CLASS (symbolP
, 0);
679 S_SET_NUMBER_AUXILIARY (symbolP
, 0);
680 /* Additional information */
681 symbolP
->sy_symbol
.ost_flags
= 0;
682 /* Auxiliary entries */
683 bzero ((char *) &symbolP
->sy_symbol
.ost_auxent
[0], AUXESZ
);
685 #ifdef STRIP_UNDERSCORE
686 /* Remove leading underscore at the beginning of the symbol.
687 * This is to be compatible with the standard librairies.
689 if (*S_GET_NAME (symbolP
) == '_')
692 S_SET_NAME (symbolP
, S_GET_NAME (symbolP
) + 1);
693 } /* strip underscore */
694 #endif /* STRIP_UNDERSCORE */
696 if (S_IS_STRING (symbolP
))
697 SF_SET_STRING (symbolP
);
698 if (!underscore
&& S_IS_LOCAL (symbolP
))
699 SF_SET_LOCAL (symbolP
);
702 } /* obj_symbol_new_hook() */
706 stack_init (chunk_size
, element_size
)
707 unsigned long chunk_size
;
708 unsigned long element_size
;
712 if ((st
= (stack
*) malloc (sizeof (stack
))) == (stack
*) 0)
714 if ((st
->data
= malloc (chunk_size
)) == (char *) 0)
720 st
->size
= chunk_size
;
721 st
->chunk_size
= chunk_size
;
722 st
->element_size
= element_size
;
735 stack_push (st
, element
)
739 if (st
->pointer
+ st
->element_size
>= st
->size
)
741 st
->size
+= st
->chunk_size
;
742 if ((st
->data
= xrealloc (st
->data
, st
->size
)) == (char *) 0)
745 memcpy (st
->data
+ st
->pointer
, element
, st
->element_size
);
746 st
->pointer
+= st
->element_size
;
747 return st
->data
+ st
->pointer
;
754 if ((st
->pointer
-= st
->element_size
) < 0)
760 return st
->data
+ st
->pointer
;
767 return st
->data
+ st
->pointer
- st
->element_size
;
772 * Handle .ln directives.
780 if (def_symbol_in_progress
!= NULL
)
782 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
783 demand_empty_rest_of_line ();
785 } /* wrong context */
788 obstack_next_free (&frags
) - frag_now
->fr_literal
,
789 l
= get_absolute_expression (),
797 listing_source_line (l
+ line_base
- 1);
802 demand_empty_rest_of_line ();
804 } /* obj_coff_line() */
809 * Handle .def directives.
811 * One might ask : why can't we symbol_new if the symbol does not
812 * already exist and fill it with debug information. Because of
813 * the C_EFCN special symbol. It would clobber the value of the
814 * function symbol before we have a chance to notice that it is
815 * a C_EFCN. And a second reason is that the code is more clear this
816 * way. (at least I think it is :-).
820 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
821 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
822 *input_line_pointer == '\t') \
823 input_line_pointer++;
826 DEFUN (obj_coff_def
, (what
),
829 char name_end
; /* Char after the end of name */
830 char *symbol_name
; /* Name of the debug symbol */
831 char *symbol_name_copy
; /* Temporary copy of the name */
832 unsigned int symbol_name_length
;
833 /*$char* directiveP;$ *//* Name of the pseudo opcode */
834 /*$char directive[MAX_DIRECTIVE];$ *//* Backup of the directive */
835 /*$char end = 0;$ *//* If 1, stop parsing */
837 if (def_symbol_in_progress
!= NULL
)
839 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
840 demand_empty_rest_of_line ();
842 } /* if not inside .def/.endef */
846 def_symbol_in_progress
= (symbolS
*) obstack_alloc (¬es
, sizeof (*def_symbol_in_progress
));
847 bzero (def_symbol_in_progress
, sizeof (*def_symbol_in_progress
));
849 symbol_name
= input_line_pointer
;
850 name_end
= get_symbol_end ();
851 symbol_name_length
= strlen (symbol_name
);
852 symbol_name_copy
= xmalloc (symbol_name_length
+ 1);
853 strcpy (symbol_name_copy
, symbol_name
);
855 /* Initialize the new symbol */
856 #ifdef STRIP_UNDERSCORE
857 S_SET_NAME (def_symbol_in_progress
, (*symbol_name_copy
== '_'
858 ? symbol_name_copy
+ 1
859 : symbol_name_copy
));
860 #else /* STRIP_UNDERSCORE */
861 S_SET_NAME (def_symbol_in_progress
, symbol_name_copy
);
862 #endif /* STRIP_UNDERSCORE */
863 /* free(symbol_name_copy); */
864 def_symbol_in_progress
->sy_name_offset
= ~0;
865 def_symbol_in_progress
->sy_number
= ~0;
866 def_symbol_in_progress
->sy_frag
= &zero_address_frag
;
868 if (S_IS_STRING (def_symbol_in_progress
))
870 SF_SET_STRING (def_symbol_in_progress
);
873 *input_line_pointer
= name_end
;
875 demand_empty_rest_of_line ();
877 } /* obj_coff_def() */
879 unsigned int dim_index
;
881 DEFUN_VOID (obj_coff_endef
)
883 symbolS
*symbolP
= 0;
884 /* DIM BUG FIX sac@cygnus.com */
886 if (def_symbol_in_progress
== NULL
)
888 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
889 demand_empty_rest_of_line ();
891 } /* if not inside .def/.endef */
893 /* Set the section number according to storage class. */
894 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress
))
899 SF_SET_TAG (def_symbol_in_progress
);
900 /* intentional fallthrough */
903 SF_SET_DEBUG (def_symbol_in_progress
);
904 S_SET_SEGMENT (def_symbol_in_progress
, SEG_DEBUG
);
908 SF_SET_LOCAL (def_symbol_in_progress
); /* Do not emit this symbol. */
909 /* intentional fallthrough */
911 SF_SET_PROCESS (def_symbol_in_progress
); /* Will need processing before writing */
912 /* intentional fallthrough */
914 S_SET_SEGMENT (def_symbol_in_progress
, SEG_E0
);
916 if (strcmp (def_symbol_in_progress
->sy_symbol
.ost_entry
._n
._n_nptr
,
919 if (function_lineoff
< 0)
921 fprintf (stderr
, "`.bf' symbol without preceding function\n");
922 } /* missing function symbol */
923 SA_GET_SYM_LNNOPTR (last_line_symbol
) = function_lineoff
;
925 SF_SET_PROCESS (last_line_symbol
);
926 function_lineoff
= -1;
932 #endif /* C_AUTOARG */
942 SF_SET_DEBUG (def_symbol_in_progress
);
943 S_SET_SEGMENT (def_symbol_in_progress
, SEG_ABSOLUTE
);
949 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
955 as_warn ("unexpected storage class %d", S_GET_STORAGE_CLASS (def_symbol_in_progress
));
957 } /* switch on storage class */
959 /* Now that we have built a debug symbol, try to find if
960 we should merge with an existing symbol or not. If a
961 symbol is C_EFCN or SEG_ABSOLUTE or untagged
962 SEG_DEBUG it never merges. We also don't merge
963 labels, which are in a different namespace, nor
964 symbols which have not yet been defined since they
965 are typically unique, nor do we merge tags with
968 /* Two cases for functions. Either debug followed
969 by definition or definition followed by debug.
970 For definition first, we will merge the debug
971 symbol into the definition. For debug first, the
972 lineno entry MUST point to the definition
973 function or else it will point off into space
974 when crawl_symbols() merges the debug
975 symbol into the real symbol. Therefor, let's
976 presume the debug symbol is a real function
979 /* FIXME-SOON If for some reason the definition
980 label/symbol is never seen, this will probably
981 leave an undefined symbol at link time. */
983 if (S_GET_STORAGE_CLASS (def_symbol_in_progress
) == C_EFCN
984 || S_GET_STORAGE_CLASS (def_symbol_in_progress
) == C_LABEL
985 || (S_GET_SEGMENT (def_symbol_in_progress
) == SEG_DEBUG
986 && !SF_GET_TAG (def_symbol_in_progress
))
987 || S_GET_SEGMENT (def_symbol_in_progress
) == SEG_ABSOLUTE
988 || def_symbol_in_progress
->sy_forward
!= NULL
989 || (symbolP
= symbol_find_base (S_GET_NAME (def_symbol_in_progress
), DO_NOT_STRIP
)) == NULL
990 || (SF_GET_TAG (def_symbol_in_progress
) != SF_GET_TAG (symbolP
)))
993 symbol_append (def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
998 /* This symbol already exists, merge the
999 newly created symbol into the old one.
1000 This is not mandatory. The linker can
1001 handle duplicate symbols correctly. But I
1002 guess that it save a *lot* of space if
1003 the assembly file defines a lot of
1006 /* The debug entry (def_symbol_in_progress)
1007 is merged into the previous definition. */
1009 c_symbol_merge (def_symbol_in_progress
, symbolP
);
1010 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
1011 def_symbol_in_progress
= symbolP
;
1013 if (SF_GET_FUNCTION (def_symbol_in_progress
)
1014 || SF_GET_TAG (def_symbol_in_progress
))
1016 /* For functions, and tags, the symbol *must* be where the debug symbol
1017 appears. Move the existing symbol to the current place. */
1018 /* If it already is at the end of the symbol list, do nothing */
1019 if (def_symbol_in_progress
!= symbol_lastP
)
1021 symbol_remove (def_symbol_in_progress
, &symbol_rootP
, &symbol_lastP
);
1022 symbol_append (def_symbol_in_progress
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
1023 } /* if not already in place */
1025 } /* normal or mergable */
1027 if (SF_GET_TAG (def_symbol_in_progress
)
1028 && symbol_find_base (S_GET_NAME (def_symbol_in_progress
), DO_NOT_STRIP
) == NULL
)
1030 tag_insert (S_GET_NAME (def_symbol_in_progress
), def_symbol_in_progress
);
1031 } /* If symbol is a {structure,union} tag, associate symbol to its name. */
1033 if (SF_GET_FUNCTION (def_symbol_in_progress
))
1035 know (sizeof (def_symbol_in_progress
) <= sizeof (long));
1037 = c_line_new (def_symbol_in_progress
, 0, 0, &zero_address_frag
);
1041 SF_SET_PROCESS (def_symbol_in_progress
);
1043 if (symbolP
== NULL
)
1045 /* That is, if this is the first
1046 time we've seen the function... */
1047 symbol_table_insert (def_symbol_in_progress
);
1048 } /* definition follows debug */
1049 } /* Create the line number entry pointing to the function being defined */
1051 def_symbol_in_progress
= NULL
;
1052 demand_empty_rest_of_line ();
1054 } /* obj_coff_endef() */
1057 DEFUN_VOID (obj_coff_dim
)
1059 register int dim_index
;
1061 if (def_symbol_in_progress
== NULL
)
1063 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
1064 demand_empty_rest_of_line ();
1066 } /* if not inside .def/.endef */
1068 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1070 for (dim_index
= 0; dim_index
< DIMNUM
; dim_index
++)
1072 SKIP_WHITESPACES ();
1073 SA_SET_SYM_DIMEN (def_symbol_in_progress
, dim_index
, get_absolute_expression ());
1075 switch (*input_line_pointer
)
1079 input_line_pointer
++;
1083 as_warn ("badly formed .dim directive ignored");
1084 /* intentional fallthrough */
1089 } /* switch on following character */
1090 } /* for each dimension */
1092 demand_empty_rest_of_line ();
1094 } /* obj_coff_dim() */
1101 if (def_symbol_in_progress
== NULL
)
1105 } /* if it looks like a stabs style line */
1107 this_base
= get_absolute_expression ();
1108 if (this_base
> line_base
)
1110 line_base
= this_base
;
1119 listing_source_line (line_base
);
1123 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1124 SA_SET_SYM_LNNO (def_symbol_in_progress
, line_base
);
1126 demand_empty_rest_of_line ();
1128 } /* obj_coff_line() */
1133 if (def_symbol_in_progress
== NULL
)
1135 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1136 demand_empty_rest_of_line ();
1138 } /* if not inside .def/.endef */
1140 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1141 SA_SET_SYM_SIZE (def_symbol_in_progress
, get_absolute_expression ());
1142 demand_empty_rest_of_line ();
1144 } /* obj_coff_size() */
1149 if (def_symbol_in_progress
== NULL
)
1151 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1152 demand_empty_rest_of_line ();
1154 } /* if not inside .def/.endef */
1156 S_SET_STORAGE_CLASS (def_symbol_in_progress
, get_absolute_expression ());
1157 demand_empty_rest_of_line ();
1159 } /* obj_coff_scl() */
1167 if (def_symbol_in_progress
== NULL
)
1169 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1170 demand_empty_rest_of_line ();
1172 } /* if not inside .def/.endef */
1174 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress
, 1);
1175 symbol_name
= input_line_pointer
;
1176 name_end
= get_symbol_end ();
1178 /* Assume that the symbol referred to by .tag is always defined. */
1179 /* This was a bad assumption. I've added find_or_make. xoxorich. */
1180 SA_SET_SYM_TAGNDX (def_symbol_in_progress
, (long) tag_find_or_make (symbol_name
));
1181 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress
) == 0L)
1183 as_warn ("tag not found for .tag %s", symbol_name
);
1186 SF_SET_TAGGED (def_symbol_in_progress
);
1187 *input_line_pointer
= name_end
;
1189 demand_empty_rest_of_line ();
1191 } /* obj_coff_tag() */
1196 if (def_symbol_in_progress
== NULL
)
1198 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1199 demand_empty_rest_of_line ();
1201 } /* if not inside .def/.endef */
1203 S_SET_DATA_TYPE (def_symbol_in_progress
, get_absolute_expression ());
1205 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress
)) &&
1206 S_GET_STORAGE_CLASS (def_symbol_in_progress
) != C_TPDEF
)
1208 SF_SET_FUNCTION (def_symbol_in_progress
);
1209 } /* is a function */
1211 demand_empty_rest_of_line ();
1213 } /* obj_coff_type() */
1218 if (def_symbol_in_progress
== NULL
)
1220 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1221 demand_empty_rest_of_line ();
1223 } /* if not inside .def/.endef */
1225 if (is_name_beginner (*input_line_pointer
))
1227 char *symbol_name
= input_line_pointer
;
1228 char name_end
= get_symbol_end ();
1230 if (!strcmp (symbol_name
, "."))
1232 def_symbol_in_progress
->sy_frag
= frag_now
;
1233 S_SET_VALUE (def_symbol_in_progress
, obstack_next_free (&frags
) - frag_now
->fr_literal
);
1234 /* If the .val is != from the .def (e.g. statics) */
1236 else if (strcmp (S_GET_NAME (def_symbol_in_progress
), symbol_name
))
1238 def_symbol_in_progress
->sy_forward
= symbol_find_or_make (symbol_name
);
1240 /* If the segment is undefined when the forward
1241 reference is solved, then copy the segment id
1242 from the forward symbol. */
1243 SF_SET_GET_SEGMENT (def_symbol_in_progress
);
1245 /* FIXME: gcc can generate address expressions
1246 here in unusual cases (search for "obscure"
1247 in sdbout.c). We just ignore the offset
1248 here, thus generating incorrect debugging
1249 information. We ignore the rest of the
1252 /* Otherwise, it is the name of a non debug symbol and
1253 its value will be calculated later. */
1254 *input_line_pointer
= name_end
;
1256 /* FIXME: this is to avoid an error message in the
1257 FIXME case mentioned just above. */
1258 while (! is_end_of_line
[*input_line_pointer
])
1259 ++input_line_pointer
;
1263 S_SET_VALUE (def_symbol_in_progress
, get_absolute_expression ());
1264 } /* if symbol based */
1266 demand_empty_rest_of_line ();
1268 } /* obj_coff_val() */
1271 * Maintain a list of the tagnames of the structres.
1277 tag_hash
= hash_new ();
1282 tag_insert (name
, symbolP
)
1286 register char *error_string
;
1288 if (*(error_string
= hash_jam (tag_hash
, name
, (char *) symbolP
)))
1290 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1291 name
, error_string
);
1294 } /* tag_insert() */
1297 tag_find_or_make (name
)
1302 if ((symbolP
= tag_find (name
)) == NULL
)
1304 symbolP
= symbol_new (name
,
1307 &zero_address_frag
);
1309 tag_insert (S_GET_NAME (symbolP
), symbolP
);
1313 } /* tag_find_or_make() */
1319 #ifdef STRIP_UNDERSCORE
1322 #endif /* STRIP_UNDERSCORE */
1323 return ((symbolS
*) hash_find (tag_hash
, name
));
1327 obj_read_begin_hook ()
1329 /* These had better be the same. Usually 18 bytes. */
1331 know (sizeof (SYMENT
) == sizeof (AUXENT
));
1332 know (SYMESZ
== AUXESZ
);
1337 } /* obj_read_begin_hook() */
1339 /* This function runs through the symbol table and puts all the
1340 externals onto another chain */
1342 /* The chain of externals */
1343 symbolS
*symbol_externP
= NULL
;
1344 symbolS
*symbol_extern_lastP
= NULL
;
1347 symbolS
*last_functionP
= NULL
;
1352 DEFUN_VOID (yank_symbols
)
1355 unsigned int symbol_number
= 0;
1357 for (symbolP
= symbol_rootP
;
1359 symbolP
= symbolP
? symbol_next (symbolP
) : symbol_rootP
)
1361 if (!SF_GET_DEBUG (symbolP
))
1363 /* Debug symbols do not need all this rubbish */
1364 symbolS
*real_symbolP
;
1366 /* L* and C_EFCN symbols never merge. */
1367 if (!SF_GET_LOCAL (symbolP
)
1368 && S_GET_STORAGE_CLASS (symbolP
) != C_LABEL
1369 && (real_symbolP
= symbol_find_base (S_GET_NAME (symbolP
), DO_NOT_STRIP
))
1370 && real_symbolP
!= symbolP
)
1372 /* FIXME-SOON: where do dups come from?
1373 Maybe tag references before definitions? xoxorich. */
1374 /* Move the debug data from the debug symbol to the
1375 real symbol. Do NOT do the oposite (i.e. move from
1376 real symbol to debug symbol and remove real symbol from the
1377 list.) Because some pointers refer to the real symbol
1378 whereas no pointers refer to the debug symbol. */
1379 c_symbol_merge (symbolP
, real_symbolP
);
1380 /* Replace the current symbol by the real one */
1381 /* The symbols will never be the last or the first
1382 because : 1st symbol is .file and 3 last symbols are
1383 .text, .data, .bss */
1384 symbol_remove (real_symbolP
, &symbol_rootP
, &symbol_lastP
);
1385 symbol_insert (real_symbolP
, symbolP
, &symbol_rootP
, &symbol_lastP
);
1386 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1387 symbolP
= real_symbolP
;
1388 } /* if not local but dup'd */
1390 if (flagseen
['R'] && (S_GET_SEGMENT (symbolP
) == SEG_E1
))
1392 S_SET_SEGMENT (symbolP
, SEG_E0
);
1393 } /* push data into text */
1395 S_SET_VALUE (symbolP
,
1396 S_GET_VALUE (symbolP
) + symbolP
->sy_frag
->fr_address
);
1398 if (!S_IS_DEFINED (symbolP
) && !SF_GET_LOCAL (symbolP
))
1400 S_SET_EXTERNAL (symbolP
);
1402 else if (S_GET_STORAGE_CLASS (symbolP
) == C_NULL
)
1404 if (S_GET_SEGMENT (symbolP
) == SEG_E0
)
1406 S_SET_STORAGE_CLASS (symbolP
, C_LABEL
);
1410 S_SET_STORAGE_CLASS (symbolP
, C_STAT
);
1414 /* Mainly to speed up if not -g */
1415 if (SF_GET_PROCESS (symbolP
))
1417 /* Handle the nested blocks auxiliary info. */
1418 if (S_GET_STORAGE_CLASS (symbolP
) == C_BLOCK
)
1420 if (!strcmp (S_GET_NAME (symbolP
), ".bb"))
1421 stack_push (block_stack
, (char *) &symbolP
);
1424 register symbolS
*begin_symbolP
;
1425 begin_symbolP
= *(symbolS
**) stack_pop (block_stack
);
1426 if (begin_symbolP
== (symbolS
*) 0)
1427 as_warn ("mismatched .eb");
1429 SA_SET_SYM_ENDNDX (begin_symbolP
, symbol_number
+ 2);
1432 /* If we are able to identify the type of a function, and we
1433 are out of a function (last_functionP == 0) then, the
1434 function symbol will be associated with an auxiliary
1436 if (last_functionP
== (symbolS
*) 0 &&
1437 SF_GET_FUNCTION (symbolP
))
1439 last_functionP
= symbolP
;
1441 if (S_GET_NUMBER_AUXILIARY (symbolP
) < 1)
1443 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
1444 } /* make it at least 1 */
1446 /* Clobber possible stale .dim information. */
1448 /* Iffed out by steve - this fries the lnnoptr info too */
1449 bzero (symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
,
1450 sizeof (symbolP
->sy_symbol
.ost_auxent
[0].x_sym
.x_fcnary
.x_ary
.x_dimen
));
1453 /* The C_FCN doesn't need any additional information.
1454 I don't even know if this is needed for sdb. But the
1455 standard assembler generates it, so...
1457 if (S_GET_STORAGE_CLASS (symbolP
) == C_EFCN
)
1459 if (last_functionP
== (symbolS
*) 0)
1460 as_fatal ("C_EFCN symbol out of scope");
1461 SA_SET_SYM_FSIZE (last_functionP
,
1462 (long) (S_GET_VALUE (symbolP
) -
1463 S_GET_VALUE (last_functionP
)));
1464 SA_SET_SYM_ENDNDX (last_functionP
, symbol_number
);
1465 last_functionP
= (symbolS
*) 0;
1469 else if (SF_GET_TAG (symbolP
))
1471 /* First descriptor of a structure must point to
1472 the first slot after the structure description. */
1473 last_tagP
= symbolP
;
1476 else if (S_GET_STORAGE_CLASS (symbolP
) == C_EOS
)
1478 /* +2 take in account the current symbol */
1479 SA_SET_SYM_ENDNDX (last_tagP
, symbol_number
+ 2);
1481 else if (S_GET_STORAGE_CLASS (symbolP
) == C_FILE
)
1483 if (S_GET_VALUE (symbolP
))
1485 S_SET_VALUE ((symbolS
*) S_GET_VALUE (symbolP
), symbol_number
);
1486 S_SET_VALUE (symbolP
, 0);
1487 } /* no one points at the first .file symbol */
1488 } /* if debug or tag or eos or file */
1490 /* We must put the external symbols apart. The loader
1491 does not bomb if we do not. But the references in
1492 the endndx field for a .bb symbol are not corrected
1493 if an external symbol is removed between .bb and .be.
1494 I.e in the following case :
1495 [20] .bb endndx = 22
1498 ld will move the symbol 21 to the end of the list but
1499 endndx will still be 22 instead of 21. */
1502 if (SF_GET_LOCAL (symbolP
))
1504 /* remove C_EFCN and LOCAL (L...) symbols */
1505 /* next pointer remains valid */
1506 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1509 else if (!S_IS_DEFINED (symbolP
)
1510 && !S_IS_DEBUG (symbolP
)
1511 && !SF_GET_STATICS (symbolP
) &&
1512 S_GET_STORAGE_CLASS (symbolP
) == C_EXT
)
1513 { /* C_EXT && !SF_GET_FUNCTION(symbolP)) */
1514 /* if external, Remove from the list */
1515 symbolS
*hold
= symbol_previous (symbolP
);
1517 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
1518 symbol_clear_list_pointers (symbolP
);
1519 symbol_append (symbolP
, symbol_extern_lastP
, &symbol_externP
, &symbol_extern_lastP
);
1524 if (SF_GET_STRING (symbolP
))
1526 symbolP
->sy_name_offset
= string_byte_count
;
1527 string_byte_count
+= strlen (S_GET_NAME (symbolP
)) + 1;
1531 symbolP
->sy_name_offset
= 0;
1532 } /* fix "long" names */
1534 symbolP
->sy_number
= symbol_number
;
1535 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (symbolP
);
1536 } /* if local symbol */
1537 } /* traverse the symbol list */
1538 return symbol_number
;
1544 DEFUN_VOID (glue_symbols
)
1546 unsigned int symbol_number
= 0;
1548 for (symbolP
= symbol_externP
; symbol_externP
;)
1550 symbolS
*tmp
= symbol_externP
;
1553 symbol_remove (tmp
, &symbol_externP
, &symbol_extern_lastP
);
1554 symbol_append (tmp
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
1557 if (SF_GET_STRING (tmp
))
1559 tmp
->sy_name_offset
= string_byte_count
;
1560 string_byte_count
+= strlen (S_GET_NAME (tmp
)) + 1;
1564 tmp
->sy_name_offset
= 0;
1565 } /* fix "long" names */
1567 tmp
->sy_number
= symbol_number
;
1568 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (tmp
);
1569 } /* append the entire extern chain */
1570 return symbol_number
;
1575 DEFUN_VOID (tie_tags
)
1577 unsigned int symbol_number
= 0;
1580 for (symbolP
= symbol_rootP
; symbolP
; symbolP
=
1581 symbol_next (symbolP
))
1583 symbolP
->sy_number
= symbol_number
;
1587 if (SF_GET_TAGGED (symbolP
))
1591 ((symbolS
*) SA_GET_SYM_TAGNDX (symbolP
))->sy_number
);
1594 symbol_number
+= 1 + S_GET_NUMBER_AUXILIARY (symbolP
);
1596 return symbol_number
;
1601 DEFUN (crawl_symbols
, (h
, abfd
),
1602 object_headers
*h AND
1607 unsigned int ptr
= 0;
1612 /* Initialize the stack used to keep track of the matching .bb .be */
1614 block_stack
= stack_init (512, sizeof (symbolS
*));
1615 /* JF deal with forward references first... */
1616 for (symbolP
= symbol_rootP
;
1618 symbolP
= symbol_next (symbolP
))
1621 if (symbolP
->sy_forward
)
1623 S_SET_VALUE (symbolP
, (S_GET_VALUE (symbolP
)
1624 + S_GET_VALUE (symbolP
->sy_forward
)
1625 + symbolP
->sy_forward
->sy_frag
->fr_address
));
1627 if (SF_GET_GET_SEGMENT (symbolP
))
1629 S_SET_SEGMENT (symbolP
, S_GET_SEGMENT (symbolP
->sy_forward
));
1630 } /* forward segment also */
1632 symbolP
->sy_forward
= 0;
1633 } /* if it has a forward reference */
1634 } /* walk the symbol chain */
1637 /* The symbol list should be ordered according to the following sequence
1640 * . debug entries for functions
1641 * . fake symbols for the sections, including.text .data and .bss
1643 * . undefined symbols
1644 * But this is not mandatory. The only important point is to put the
1645 * undefined symbols at the end of the list.
1648 if (symbol_rootP
== NULL
1649 || S_GET_STORAGE_CLASS (symbol_rootP
) != C_FILE
)
1651 c_dot_file_symbol ("fake");
1653 /* Is there a .file symbol ? If not insert one at the beginning. */
1656 * Build up static symbols for the sections, they are filled in later
1660 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
1662 if (segment_info
[i
].scnhdr
.s_name
[0])
1666 strncpy (name
, segment_info
[i
].scnhdr
.s_name
, 8);
1668 segment_info
[i
].dot
= c_section_symbol (name
, i
- SEG_E0
+ 1);
1673 /* Take all the externals out and put them into another chain */
1674 H_SET_SYMBOL_TABLE_SIZE (h
, yank_symbols ());
1675 /* Take the externals and glue them onto the end.*/
1676 H_SET_SYMBOL_TABLE_SIZE (h
, H_GET_SYMBOL_COUNT (h
) + glue_symbols ());
1678 H_SET_SYMBOL_TABLE_SIZE (h
, tie_tags ());
1679 know (symbol_externP
== NULL
);
1680 know (symbol_extern_lastP
== NULL
);
1686 * Find strings by crawling along symbol table chain.
1690 DEFUN (w_strings
, (where
),
1695 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
1696 md_number_to_chars (where
, string_byte_count
, sizeof (string_byte_count
));
1697 where
+= sizeof (string_byte_count
);
1698 for (symbolP
= symbol_rootP
;
1700 symbolP
= symbol_next (symbolP
))
1704 if (SF_GET_STRING (symbolP
))
1706 size
= strlen (S_GET_NAME (symbolP
)) + 1;
1708 memcpy (where
, S_GET_NAME (symbolP
), size
);
1717 DEFUN (do_linenos_for
, (abfd
, h
, file_cursor
),
1719 object_headers
* h AND
1720 unsigned long *file_cursor
)
1723 unsigned long start
= *file_cursor
;
1725 for (idx
= SEG_E0
; idx
< SEG_E9
; idx
++)
1727 segment_info_type
*s
= segment_info
+ idx
;
1730 if (s
->scnhdr
.s_nlnno
!= 0)
1732 struct lineno_list
*line_ptr
;
1734 struct external_lineno
*buffer
=
1735 (struct external_lineno
*) xmalloc (s
->scnhdr
.s_nlnno
* LINESZ
);
1737 struct external_lineno
*dst
= buffer
;
1739 /* Run through the table we've built and turn it into its external
1740 form, take this chance to remove duplicates */
1742 for (line_ptr
= s
->lineno_list_head
;
1743 line_ptr
!= (struct lineno_list
*) NULL
;
1744 line_ptr
= line_ptr
->next
)
1747 if (line_ptr
->line
.l_lnno
== 0)
1749 /* Turn a pointer to a symbol into the symbols' index */
1750 line_ptr
->line
.l_addr
.l_symndx
=
1751 ((symbolS
*) line_ptr
->line
.l_addr
.l_symndx
)->sy_number
;
1755 line_ptr
->line
.l_addr
.l_paddr
+= ((struct frag
*) (line_ptr
->frag
))->fr_address
;
1759 (void) bfd_coff_swap_lineno_out (abfd
, &(line_ptr
->line
), dst
);
1764 s
->scnhdr
.s_lnnoptr
= *file_cursor
;
1766 bfd_write (buffer
, 1, s
->scnhdr
.s_nlnno
* LINESZ
, abfd
);
1769 *file_cursor
+= s
->scnhdr
.s_nlnno
* LINESZ
;
1772 H_SET_LINENO_SIZE (h
, *file_cursor
- start
);
1776 /* Now we run through the list of frag chains in a segment and
1777 make all the subsegment frags appear at the end of the
1778 list, as if the seg 0 was extra long */
1781 DEFUN_VOID (remove_subsegs
)
1785 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1787 frchainS
*head
= segment_info
[i
].frchainP
;
1789 fragS
*prev_frag
= &dummy
;
1791 while (head
&& head
->frch_seg
== i
)
1793 prev_frag
->fr_next
= head
->frch_root
;
1794 prev_frag
= head
->frch_last
;
1795 head
= head
->frch_next
;
1797 prev_frag
->fr_next
= 0;
1804 DEFUN_VOID (write_object_file
)
1807 struct frchain
*frchain_ptr
;
1809 object_headers headers
;
1810 unsigned long file_cursor
;
1813 abfd
= bfd_openw (out_file_name
, TARGET_FORMAT
);
1818 as_perror ("FATAL: Can't create %s", out_file_name
);
1821 bfd_set_format (abfd
, bfd_object
);
1822 bfd_set_arch_mach (abfd
, BFD_ARCH
, machine
);
1824 string_byte_count
= 4;
1826 for (frchain_ptr
= frchain_root
;
1827 frchain_ptr
!= (struct frchain
*) NULL
;
1828 frchain_ptr
= frchain_ptr
->frch_next
)
1830 /* Run through all the sub-segments and align them up. Also close any
1831 open frags. We tack a .fill onto the end of the frag chain so
1832 that any .align's size can be worked by looking at the next
1835 subseg_new (frchain_ptr
->frch_seg
, frchain_ptr
->frch_subseg
);
1836 #ifndef SUB_SEGMENT_ALIGN
1837 #define SUB_SEGMENT_ALIGN(SEG) 1
1839 frag_align (SUB_SEGMENT_ALIGN (now_seg
), NOP_OPCODE
);
1840 frag_wane (frag_now
);
1841 frag_now
->fr_fix
= 0;
1842 know (frag_now
->fr_next
== NULL
);
1849 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1851 relax_segment (segment_info
[i
].frchainP
->frch_root
, i
);
1854 H_SET_NUMBER_OF_SECTIONS (&headers
, 0);
1856 /* Find out how big the sections are, and set the addresses. */
1858 for (i
= SEG_E0
; i
< SEG_UNKNOWN
; i
++)
1862 segment_info
[i
].scnhdr
.s_paddr
= addr
;
1863 segment_info
[i
].scnhdr
.s_vaddr
= addr
;
1865 if (segment_info
[i
].scnhdr
.s_name
[0])
1867 H_SET_NUMBER_OF_SECTIONS (&headers
,
1868 H_GET_NUMBER_OF_SECTIONS (&headers
) + 1);
1871 size
= size_section (abfd
, i
);
1875 H_SET_TEXT_SIZE (&headers
, size
);
1876 else if (i
== SEG_E1
)
1877 H_SET_DATA_SIZE (&headers
, size
);
1878 else if (i
== SEG_E2
)
1879 H_SET_BSS_SIZE (&headers
, size
);
1882 /* Turn the gas native symbol table shape into a coff symbol table */
1883 crawl_symbols (&headers
, abfd
);
1885 if (string_byte_count
== 4)
1886 string_byte_count
= 0;
1888 H_SET_STRING_SIZE (&headers
, string_byte_count
);
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
, &headers
, i
);
1894 fixup_segment (&segment_info
[i
], i
);
1898 file_cursor
= H_GET_TEXT_FILE_OFFSET (&headers
);
1900 bfd_seek (abfd
, file_cursor
, 0);
1902 /* Plant the data */
1904 fill_section (abfd
, &headers
, &file_cursor
);
1906 do_relocs_for (abfd
, &headers
, &file_cursor
);
1908 do_linenos_for (abfd
, &headers
, &file_cursor
);
1910 H_SET_FILE_MAGIC_NUMBER (&headers
, COFF_MAGIC
);
1911 #ifndef OBJ_COFF_OMIT_TIMESTAMP
1912 H_SET_TIME_STAMP (&headers
, (long)time((long*)0));
1914 H_SET_TIME_STAMP (&headers
, 0);
1917 #ifdef KEEP_RELOC_INFO
1918 H_SET_FLAGS (&headers
, ((H_GET_LINENO_SIZE(&headers
) ? 0 : F_LNNO
) |
1919 COFF_FLAGS
| coff_flags
));
1921 H_SET_FLAGS (&headers
, ((H_GET_LINENO_SIZE(&headers
) ? 0 : F_LNNO
) |
1922 (H_GET_RELOCATION_SIZE(&headers
) ? 0 : F_RELFLG
) |
1923 COFF_FLAGS
| coff_flags
));
1927 unsigned int symtable_size
= H_GET_SYMBOL_TABLE_SIZE (&headers
);
1928 char *buffer1
= xmalloc (symtable_size
+ string_byte_count
+ 1);
1929 char *ptr
= buffer1
;
1930 H_SET_SYMBOL_TABLE_POINTER (&headers
, bfd_tell (abfd
));
1931 w_symbols (abfd
, buffer1
, symbol_rootP
);
1932 if (string_byte_count
> 0)
1933 w_strings (buffer1
+ symtable_size
);
1934 bfd_write (buffer1
, 1, symtable_size
+ string_byte_count
, abfd
);
1938 coff_header_append (abfd
, &headers
);
1940 if (bfd_close_all_done (abfd
) == false)
1941 as_fatal ("Can't close %s: %s", out_file_name
,
1942 bfd_errmsg (bfd_error
));
1947 DEFUN (change_to_section
, (name
, len
, exp
),
1949 unsigned int len AND
1953 /* Find out if we've already got a section of this name etc */
1954 for (i
= SEG_E0
; i
< SEG_E9
&& segment_info
[i
].scnhdr
.s_name
[0]; i
++)
1956 if (strncmp (segment_info
[i
].scnhdr
.s_name
, name
, len
) == 0)
1958 subseg_new (i
, exp
);
1963 /* No section, add one */
1964 strncpy (segment_info
[i
].scnhdr
.s_name
, name
, 8);
1965 subseg_new (i
, exp
);
1969 * implement the .section pseudo op:
1970 * .section name {, "flags"}
1972 * | +--- optional flags: 'b' for bss
1974 * +-- section name 'l' for lib
1979 * But if the argument is not a quoted string, treat it as a
1980 * subsegment number.
1984 DEFUN_VOID (obj_coff_section
)
1986 /* Strip out the section name */
1988 char *section_name_end
;
1995 section_name
= input_line_pointer
;
1996 c
= get_symbol_end ();
1997 section_name_end
= input_line_pointer
;
1999 len
= section_name_end
- section_name
;
2000 input_line_pointer
++;
2006 else if (*input_line_pointer
== ',')
2009 ++input_line_pointer
;
2017 if (*input_line_pointer
!= '"')
2018 exp
= get_absolute_expression ();
2021 ++input_line_pointer
;
2022 while (*input_line_pointer
!= '"'
2023 && ! is_end_of_line
[*input_line_pointer
])
2025 switch (*input_line_pointer
)
2027 case 'b': flags
|= STYP_BSS
; break;
2028 case 'i': flags
|= STYP_INFO
; break;
2029 case 'l': flags
|= STYP_LIB
; break;
2030 case 'n': flags
|= STYP_NOLOAD
; break;
2031 case 'o': flags
|= STYP_OVER
; break;
2032 case 'w': flags
|= STYP_DATA
; break;
2033 case 'x': flags
|= STYP_TEXT
; break;
2035 as_warn("unknown section attribute '%c'",
2036 *input_line_pointer
);
2039 ++input_line_pointer
;
2041 if (*input_line_pointer
== '"')
2042 ++input_line_pointer
;
2046 change_to_section (section_name
, len
, exp
);
2048 segment_info
[now_seg
].scnhdr
.s_flags
|= flags
;
2050 *section_name_end
= c
;
2057 change_to_section (".text", 5, get_absolute_expression ());
2064 change_to_section (".data", 5, get_absolute_expression ());
2070 if (*input_line_pointer
== '\n') /* .bss */
2071 change_to_section(".bss",4, get_absolute_expression());
2072 else /* .bss id,expr */
2079 segT current_seg
= now_seg
; /* save current seg */
2080 subsegT current_subseg
= now_subseg
;
2081 change_to_section (".comment", 8, 0); /* .comment seg */
2082 stringer (1); /* read string */
2083 subseg_new (current_seg
, current_subseg
); /* restore current seg */
2087 c_symbol_merge (debug
, normal
)
2091 S_SET_DATA_TYPE (normal
, S_GET_DATA_TYPE (debug
));
2092 S_SET_STORAGE_CLASS (normal
, S_GET_STORAGE_CLASS (debug
));
2094 if (S_GET_NUMBER_AUXILIARY (debug
) > S_GET_NUMBER_AUXILIARY (normal
))
2096 S_SET_NUMBER_AUXILIARY (normal
, S_GET_NUMBER_AUXILIARY (debug
));
2097 } /* take the most we have */
2099 if (S_GET_NUMBER_AUXILIARY (debug
) > 0)
2101 memcpy ((char *) &normal
->sy_symbol
.ost_auxent
[0], (char *) &debug
->sy_symbol
.ost_auxent
[0], S_GET_NUMBER_AUXILIARY (debug
) * AUXESZ
);
2102 } /* Move all the auxiliary information */
2104 /* Move the debug flags. */
2105 SF_SET_DEBUG_FIELD (normal
, SF_GET_DEBUG_FIELD (debug
));
2106 } /* c_symbol_merge() */
2109 DEFUN (c_line_new
, (symbol
, paddr
, line_number
, frag
),
2110 symbolS
* symbol AND
2112 unsigned short line_number AND
2115 struct lineno_list
*new_line
=
2116 (struct lineno_list
*) xmalloc (sizeof (struct lineno_list
));
2118 segment_info_type
*s
= segment_info
+ now_seg
;
2119 new_line
->line
.l_lnno
= line_number
;
2121 if (line_number
== 0)
2123 last_line_symbol
= symbol
;
2124 new_line
->line
.l_addr
.l_symndx
= (long) symbol
;
2128 new_line
->line
.l_addr
.l_paddr
= paddr
;
2131 new_line
->frag
= (char *) frag
;
2132 new_line
->next
= (struct lineno_list
*) NULL
;
2135 if (s
->lineno_list_head
== (struct lineno_list
*) NULL
)
2137 s
->lineno_list_head
= new_line
;
2141 s
->lineno_list_tail
->next
= new_line
;
2143 s
->lineno_list_tail
= new_line
;
2144 return LINESZ
* s
->scnhdr
.s_nlnno
++;
2148 c_dot_file_symbol (filename
)
2153 symbolP
= symbol_new (".file",
2156 &zero_address_frag
);
2158 S_SET_STORAGE_CLASS (symbolP
, C_FILE
);
2159 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
2160 SA_SET_FILE_FNAME (symbolP
, filename
);
2166 listing_source_file (filename
);
2172 SF_SET_DEBUG (symbolP
);
2173 S_SET_VALUE (symbolP
, (long) previous_file_symbol
);
2175 previous_file_symbol
= symbolP
;
2177 /* Make sure that the symbol is first on the symbol chain */
2178 if (symbol_rootP
!= symbolP
)
2180 if (symbolP
== symbol_lastP
)
2182 symbol_lastP
= symbol_lastP
->sy_previous
;
2183 } /* if it was the last thing on the list */
2185 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
2186 symbol_insert (symbolP
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
2187 symbol_rootP
= symbolP
;
2188 } /* if not first on the list */
2190 } /* c_dot_file_symbol() */
2193 * Build a 'section static' symbol.
2197 c_section_symbol (name
, idx
)
2203 symbolP
= symbol_new (name
, idx
,
2205 &zero_address_frag
);
2207 S_SET_STORAGE_CLASS (symbolP
, C_STAT
);
2208 S_SET_NUMBER_AUXILIARY (symbolP
, 1);
2210 SF_SET_STATICS (symbolP
);
2213 } /* c_section_symbol() */
2216 DEFUN (w_symbols
, (abfd
, where
, symbol_rootP
),
2219 symbolS
* symbol_rootP
)
2224 /* First fill in those values we have only just worked out */
2225 for (i
= SEG_E0
; i
< SEG_E9
; i
++)
2227 symbolP
= segment_info
[i
].dot
;
2231 SA_SET_SCN_SCNLEN (symbolP
, segment_info
[i
].scnhdr
.s_size
);
2232 SA_SET_SCN_NRELOC (symbolP
, segment_info
[i
].scnhdr
.s_nreloc
);
2233 SA_SET_SCN_NLINNO (symbolP
, segment_info
[i
].scnhdr
.s_nlnno
);
2239 * Emit all symbols left in the symbol chain.
2241 for (symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next (symbolP
))
2243 /* Used to save the offset of the name. It is used to point
2244 to the string in memory but must be a file offset. */
2245 register char *temp
;
2247 tc_coff_symbol_emit_hook (symbolP
);
2249 temp
= S_GET_NAME (symbolP
);
2250 if (SF_GET_STRING (symbolP
))
2252 S_SET_OFFSET (symbolP
, symbolP
->sy_name_offset
);
2253 S_SET_ZEROES (symbolP
, 0);
2257 bzero (symbolP
->sy_symbol
.ost_entry
.n_name
, SYMNMLEN
);
2258 strncpy (symbolP
->sy_symbol
.ost_entry
.n_name
, temp
, SYMNMLEN
);
2260 where
= symbol_to_chars (abfd
, where
, symbolP
);
2261 S_SET_NAME (symbolP
, temp
);
2267 DEFUN_VOID (obj_coff_lcomm
)
2275 name
= input_line_pointer
;
2277 c
= get_symbol_end ();
2278 p
= input_line_pointer
;
2281 if (*input_line_pointer
!= ',')
2283 as_bad ("Expected comma after name");
2284 ignore_rest_of_line ();
2287 if (*input_line_pointer
== '\n')
2289 as_bad ("Missing size expression");
2292 input_line_pointer
++;
2293 if ((temp
= get_absolute_expression ()) < 0)
2295 as_warn ("lcomm length (%d.) <0! Ignored.", temp
);
2296 ignore_rest_of_line ();
2301 symbolP
= symbol_find_or_make(name
);
2303 if (S_GET_SEGMENT(symbolP
) == SEG_UNKNOWN
&&
2304 S_GET_VALUE(symbolP
) == 0)
2309 segT current_seg
= now_seg
; /* save current seg */
2310 subsegT current_subseg
= now_subseg
;
2312 subseg_new (SEG_E2
, 1);
2313 symbolP
->sy_frag
= frag_now
;
2314 p
= frag_var(rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
2317 subseg_new (current_seg
, current_subseg
); /* restore current seg */
2318 S_SET_SEGMENT(symbolP
, SEG_E2
);
2319 S_SET_STORAGE_CLASS(symbolP
, C_STAT
);
2323 as_bad("Symbol %s already defined", name
);
2325 demand_empty_rest_of_line();
2329 DEFUN (fixup_mdeps
, (frags
, h
, this_segment
),
2331 object_headers
* h AND
2334 subseg_change (this_segment
, 0);
2337 switch (frags
->fr_type
)
2341 frags
->fr_type
= rs_fill
;
2343 (frags
->fr_next
->fr_address
- frags
->fr_address
- frags
->fr_fix
);
2345 case rs_machine_dependent
:
2346 md_convert_frag (h
, frags
);
2352 frags
= frags
->fr_next
;
2358 DEFUN (fixup_segment
, (segP
, this_segment_type
),
2359 segment_info_type
* segP AND
2360 segT this_segment_type
)
2362 register fixS
* fixP
;
2363 register symbolS
*add_symbolP
;
2364 register symbolS
*sub_symbolP
;
2365 register long add_number
;
2367 register char *place
;
2368 register long where
;
2369 register char pcrel
;
2370 register fragS
*fragP
;
2371 register segT add_symbol_segment
= SEG_ABSOLUTE
;
2374 for (fixP
= segP
->fix_root
; fixP
; fixP
= fixP
->fx_next
)
2376 fragP
= fixP
->fx_frag
;
2378 where
= fixP
->fx_where
;
2379 place
= fragP
->fr_literal
+ where
;
2380 size
= fixP
->fx_size
;
2381 add_symbolP
= fixP
->fx_addsy
;
2383 if (fixP
->fx_callj
&& TC_S_IS_CALLNAME (add_symbolP
))
2385 /* Relocation should be done via the
2386 associated 'bal' entry point
2389 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (add_symbolP
)))
2391 as_bad ("No 'bal' entry point for leafproc %s",
2392 S_GET_NAME (add_symbolP
));
2395 fixP
->fx_addsy
= add_symbolP
= tc_get_bal_of_call (add_symbolP
);
2396 } /* callj relocation */
2398 sub_symbolP
= fixP
->fx_subsy
;
2399 add_number
= fixP
->fx_offset
;
2400 pcrel
= fixP
->fx_pcrel
;
2404 add_symbol_segment
= S_GET_SEGMENT (add_symbolP
);
2405 } /* if there is an addend */
2412 if (S_GET_SEGMENT (sub_symbolP
) != SEG_ABSOLUTE
)
2414 as_bad ("Negative of non-absolute symbol %s", S_GET_NAME (sub_symbolP
));
2415 } /* not absolute */
2417 add_number
-= S_GET_VALUE (sub_symbolP
);
2420 /* if sub_symbol is in the same segment that add_symbol
2421 and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */
2423 else if ((S_GET_SEGMENT (sub_symbolP
) == add_symbol_segment
)
2424 && (SEG_NORMAL (add_symbol_segment
)
2425 || (add_symbol_segment
== SEG_ABSOLUTE
)))
2427 /* Difference of 2 symbols from same segment. */
2428 /* Can't make difference of 2 undefineds: 'value' means */
2429 /* something different for N_UNDF. */
2431 /* Makes no sense to use the difference of 2 arbitrary symbols
2432 as the target of a call instruction. */
2435 as_bad ("callj to difference of 2 symbols");
2437 #endif /* TC_I960 */
2438 add_number
+= S_GET_VALUE (add_symbolP
) -
2439 S_GET_VALUE (sub_symbolP
);
2442 fixP
->fx_addsy
= NULL
;
2446 /* Different segments in subtraction. */
2447 know (!(S_IS_EXTERNAL (sub_symbolP
) && (S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
)));
2449 if ((S_GET_SEGMENT (sub_symbolP
) == SEG_ABSOLUTE
))
2451 add_number
-= S_GET_VALUE (sub_symbolP
);
2455 as_bad ("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %d.",
2456 segment_name (S_GET_SEGMENT (sub_symbolP
)),
2457 S_GET_NAME (sub_symbolP
), fragP
->fr_address
+ where
);
2460 } /* if sub_symbolP */
2464 if (add_symbol_segment
== this_segment_type
&& pcrel
)
2467 * This fixup was made when the symbol's segment was
2468 * SEG_UNKNOWN, but it is now in the local segment.
2469 * So we know how to do the address without relocation.
2472 /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal',
2473 * in which cases it modifies *fixP as appropriate. In the case
2474 * of a 'calls', no further work is required, and *fixP has been
2475 * set up to make the rest of the code below a no-op.
2478 #endif /* TC_I960 */
2480 add_number
+= S_GET_VALUE (add_symbolP
);
2481 add_number
-= md_pcrel_from (fixP
);
2483 /* On the 386 we must adjust by the segment
2484 vaddr as well. Ian Taylor. */
2485 add_number
-= segP
->scnhdr
.s_vaddr
;
2487 pcrel
= 0; /* Lie. Don't want further pcrel processing. */
2488 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2492 switch (add_symbol_segment
)
2496 reloc_callj (fixP
); /* See comment about reloc_callj() above*/
2497 #endif /* TC_I960 */
2498 add_number
+= S_GET_VALUE (add_symbolP
);
2499 fixP
->fx_addsy
= NULL
;
2504 add_number
+= S_GET_VALUE (add_symbolP
) +
2505 segment_info
[S_GET_SEGMENT (add_symbolP
)].scnhdr
.s_paddr
;
2510 if ((int) fixP
->fx_bit_fixP
== 13)
2512 /* This is a COBR instruction. They have only a
2513 * 13-bit displacement and are only to be used
2514 * for local branches: flag as error, don't generate
2517 as_bad ("can't use COBR format with external label");
2518 fixP
->fx_addsy
= NULL
; /* No relocations please. */
2521 #endif /* TC_I960 */
2523 /* 386 COFF uses a peculiar format in
2524 which the value of a common symbol is
2525 stored in the .text segment (I've
2526 checked this on SVR3.2 and SCO 3.2.2)
2527 Ian Taylor <ian@cygnus.com>. */
2528 if (S_IS_COMMON (add_symbolP
))
2529 add_number
+= S_GET_VALUE (add_symbolP
);
2534 } /* switch on symbol seg */
2535 } /* if not in local seg */
2536 } /* if there was a + symbol */
2540 add_number
-= md_pcrel_from (fixP
);
2541 if (add_symbolP
== 0)
2543 fixP
->fx_addsy
= &abs_symbol
;
2544 } /* if there's an add_symbol */
2546 /* On the 386 we must adjust by the segment vaddr
2547 as well. Ian Taylor. */
2548 add_number
-= segP
->scnhdr
.s_vaddr
;
2552 if (!fixP
->fx_bit_fixP
)
2555 (add_number
& ~0xFF) && ((add_number
& ~0xFF) != (-1 & ~0xFF))) ||
2557 (add_number
& ~0xFFFF) && ((add_number
& ~0xFFFF) != (-1 & ~0xFFFF))))
2559 as_bad ("Value of %d too large for field of %d bytes at 0x%x",
2560 add_number
, size
, fragP
->fr_address
+ where
);
2561 } /* generic error checking */
2562 #ifdef WARN_SIGNED_OVERFLOW_WORD
2563 /* Warn if a .word value is too large when treated as
2564 a signed number. We already know it is not too
2565 negative. This is to catch over-large switches
2566 generated by gcc on the 68k. */
2569 && add_number
> 0x7fff)
2570 as_bad ("Signed .word overflow; switch may be too large; %d at 0x%x",
2571 add_number
, fragP
->fr_address
+ where
);
2573 } /* not a bit fix */
2574 /* once this fix has been applied, we don't have to output anything
2575 nothing more need be done -*/
2576 md_apply_fix (fixP
, add_number
);
2577 } /* For each fixS in this segment. */
2578 } /* fixup_segment() */