1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
27 #include "obstack.h" /* For "symbols.h" */
30 #ifndef WORKING_DOT_WORD
31 extern int new_broken_words
;
34 /* symbol-name => struct symbol pointer */
35 static struct hash_control
*sy_hash
;
37 /* Below are commented in "symbols.h". */
38 symbolS
*symbol_rootP
;
39 symbolS
*symbol_lastP
;
43 #define debug_verify_symchain verify_symbol_chain
45 #define debug_verify_symchain (void)
50 static void fb_label_init
PARAMS ((void));
54 Return a pointer to a new symbol. Die if we can't make a new
55 symbol. Fill in the symbol's values. Add symbol to end of symbol
58 This function should be called in the general case of creating a
59 symbol. However, if the output file symbol table has already been
60 set, and you are certain that this symbol won't be wanted in the
61 output file, you can call symbol_create. */
64 symbol_new (name
, segment
, valu
, frag
)
70 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
73 * Link to end of symbol chain.
77 extern int symbol_table_frozen
;
78 if (symbol_table_frozen
)
82 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
83 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
89 symbol_create (name
, segment
, valu
, frag
)
90 const char *name
; /* It is copied, the caller can destroy/modify */
91 segT segment
; /* Segment identifier (SEG_<something>) */
92 valueT valu
; /* Symbol value */
93 fragS
*frag
; /* Associated fragment */
95 unsigned int name_length
;
96 char *preserved_copy_of_name
;
99 name_length
= strlen (name
) + 1; /* +1 for \0 */
100 obstack_grow (¬es
, name
, name_length
);
101 preserved_copy_of_name
= obstack_finish (¬es
);
102 #ifdef STRIP_UNDERSCORE
103 if (preserved_copy_of_name
[0] == '_')
104 preserved_copy_of_name
++;
107 #ifdef tc_canonicalize_symbol_name
108 preserved_copy_of_name
=
109 tc_canonicalize_symbol_name (preserved_copy_of_name
);
112 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
114 /* symbol must be born in some fixed state. This seems as good as any. */
115 memset (symbolP
, 0, sizeof (symbolS
));
118 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
119 assert (symbolP
->bsym
!= 0);
120 symbolP
->bsym
->udata
= (PTR
) symbolP
;
122 S_SET_NAME (symbolP
, preserved_copy_of_name
);
124 S_SET_SEGMENT (symbolP
, segment
);
125 S_SET_VALUE (symbolP
, valu
);
126 symbol_clear_list_pointers (symbolP
);
128 symbolP
->sy_frag
= frag
;
129 #ifndef BFD_ASSEMBLER
130 symbolP
->sy_number
= ~0;
131 symbolP
->sy_name_offset
= (unsigned int) ~0;
134 obj_symbol_new_hook (symbolP
);
136 #ifdef tc_symbol_new_hook
137 tc_symbol_new_hook (symbolP
);
147 * We have just seen "<name>:".
148 * Creates a struct symbol unless it already exists.
150 * Gripes if we are redefining a symbol incompatibly (and ignores it).
154 colon (sym_name
) /* just seen "x:" - rattle symbols & frags */
155 register char *sym_name
; /* symbol name, as a cannonical string */
156 /* We copy this string: OK to alter later. */
158 register symbolS
*symbolP
; /* symbol we are working with */
160 #ifdef LOCAL_LABELS_DOLLAR
161 /* Sun local labels go out of scope whenever a non-local symbol is
164 if (*sym_name
!= 'L')
165 dollar_label_clear ();
166 #endif /* LOCAL_LABELS_DOLLAR */
168 #ifndef WORKING_DOT_WORD
169 if (new_broken_words
)
171 struct broken_word
*a
;
176 extern const int md_short_jump_size
;
177 extern const int md_long_jump_size
;
178 possible_bytes
= (md_short_jump_size
179 + new_broken_words
* md_long_jump_size
);
182 frag_opcode
= frag_var (rs_broken_word
,
186 (symbolS
*) broken_words
,
190 /* We want to store the pointer to where to insert the jump table in the
191 fr_opcode of the rs_broken_word frag. This requires a little
194 && (frag_tmp
->fr_type
!= rs_broken_word
195 || frag_tmp
->fr_opcode
))
196 frag_tmp
= frag_tmp
->fr_next
;
198 frag_tmp
->fr_opcode
= frag_opcode
;
199 new_broken_words
= 0;
201 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
202 a
->dispfrag
= frag_tmp
;
204 #endif /* WORKING_DOT_WORD */
206 if ((symbolP
= symbol_find (sym_name
)) != 0)
208 #ifdef RESOLVE_SYMBOL_REDEFINITION
209 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
213 * Now check for undefined symbols
215 if (!S_IS_DEFINED (symbolP
))
217 if (S_GET_VALUE (symbolP
) == 0)
219 symbolP
->sy_frag
= frag_now
;
221 S_GET_OTHER(symbolP
) = const_flag
;
223 S_SET_VALUE (symbolP
, (valueT
) ((char*)obstack_next_free (&frags
) - frag_now
->fr_literal
));
224 S_SET_SEGMENT (symbolP
, now_seg
);
227 #endif /* if we have one, it better be zero. */
233 * There are still several cases to check:
234 * A .comm/.lcomm symbol being redefined as
235 * initialized data is OK
236 * A .comm/.lcomm symbol being redefined with
237 * a larger size is also OK
239 * This only used to be allowed on VMS gas, but Sun cc
240 * on the sparc also depends on it.
243 if (((!S_IS_DEBUG (symbolP
)
244 && !S_IS_DEFINED (symbolP
)
245 && S_IS_EXTERNAL (symbolP
))
246 || S_GET_SEGMENT (symbolP
) == bss_section
)
247 && (now_seg
== data_section
248 || now_seg
== S_GET_SEGMENT (symbolP
)))
251 * Select which of the 2 cases this is
253 if (now_seg
!= data_section
)
256 * New .comm for prev .comm symbol.
257 * If the new size is larger we just
258 * change its value. If the new size
259 * is smaller, we ignore this symbol
261 if (S_GET_VALUE (symbolP
)
262 < ((unsigned) frag_now_fix ()))
264 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
269 /* It is a .comm/.lcomm being converted to initialized
271 symbolP
->sy_frag
= frag_now
;
273 S_GET_OTHER(symbolP
) = const_flag
;
275 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
276 S_SET_SEGMENT (symbolP
, now_seg
); /* keep N_EXT bit */
281 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
282 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
284 segment_name (S_GET_SEGMENT (symbolP
)),
285 S_GET_OTHER (symbolP
), S_GET_DESC (symbolP
),
286 (long) S_GET_VALUE (symbolP
));
288 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
290 segment_name (S_GET_SEGMENT (symbolP
)),
291 (long) S_GET_VALUE (symbolP
));
294 } /* if the undefined symbol has no value */
298 /* Don't blow up if the definition is the same */
299 if (!(frag_now
== symbolP
->sy_frag
300 && S_GET_VALUE (symbolP
) == (char*)obstack_next_free (&frags
) - frag_now
->fr_literal
301 && S_GET_SEGMENT (symbolP
) == now_seg
))
302 as_fatal ("Symbol %s already defined.", sym_name
);
303 } /* if this symbol is not yet defined */
308 symbolP
= symbol_new (sym_name
,
310 (valueT
) ((char*)obstack_next_free (&frags
) - frag_now
->fr_literal
),
313 S_SET_OTHER (symbolP
, const_flag
);
316 symbol_table_insert (symbolP
);
317 } /* if we have seen this symbol before */
320 tc_frob_label (symbolP
);
326 * symbol_table_insert()
328 * Die if we can't insert the symbol.
333 symbol_table_insert (symbolP
)
336 register const char *error_string
;
339 know (S_GET_NAME (symbolP
));
341 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (PTR
) symbolP
)))
343 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
344 S_GET_NAME (symbolP
), error_string
);
346 } /* symbol_table_insert() */
349 * symbol_find_or_make()
351 * If a symbol name does not exist, create it as undefined, and insert
352 * it into the symbol table. Return a pointer to it.
355 symbol_find_or_make (name
)
358 register symbolS
*symbolP
;
360 symbolP
= symbol_find (name
);
364 symbolP
= symbol_make (name
);
366 symbol_table_insert (symbolP
);
367 } /* if symbol wasn't found */
370 } /* symbol_find_or_make() */
378 /* Let the machine description default it, e.g. for register names. */
379 symbolP
= md_undefined_symbol ((char *) name
);
382 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
385 } /* symbol_make() */
390 * Implement symbol table lookup.
391 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
392 * Out: NULL if the name was not in the symbol table, else the address
393 * of a struct symbol associated with that name.
400 #ifdef STRIP_UNDERSCORE
401 return (symbol_find_base (name
, 1));
402 #else /* STRIP_UNDERSCORE */
403 return (symbol_find_base (name
, 0));
404 #endif /* STRIP_UNDERSCORE */
405 } /* symbol_find() */
408 symbol_find_base (name
, strip_underscore
)
410 int strip_underscore
;
412 if (strip_underscore
&& *name
== '_')
415 #ifdef tc_canonicalize_symbol_name
419 copy
= (char *) alloca (strlen (name
) + 1);
421 name
= tc_canonicalize_symbol_name (copy
);
425 return ((symbolS
*) hash_find (sy_hash
, name
));
429 * Once upon a time, symbols were kept in a singly linked list. At
430 * least coff needs to be able to rearrange them from time to time, for
431 * which a doubly linked list is much more convenient. Loic did these
432 * as macros which seemed dangerous to me so they're now functions.
436 /* Link symbol ADDME after symbol TARGET in the chain. */
438 symbol_append (addme
, target
, rootPP
, lastPP
)
446 know (*rootPP
== NULL
);
447 know (*lastPP
== NULL
);
451 } /* if the list is empty */
453 if (target
->sy_next
!= NULL
)
455 #ifdef SYMBOLS_NEED_BACKPOINTERS
456 target
->sy_next
->sy_previous
= addme
;
457 #endif /* SYMBOLS_NEED_BACKPOINTERS */
461 know (*lastPP
== target
);
463 } /* if we have a next */
465 addme
->sy_next
= target
->sy_next
;
466 target
->sy_next
= addme
;
468 #ifdef SYMBOLS_NEED_BACKPOINTERS
469 addme
->sy_previous
= target
;
470 #endif /* SYMBOLS_NEED_BACKPOINTERS */
473 /* Set the chain pointers of SYMBOL to null. */
475 symbol_clear_list_pointers (symbolP
)
478 symbolP
->sy_next
= NULL
;
479 #ifdef SYMBOLS_NEED_BACKPOINTERS
480 symbolP
->sy_previous
= NULL
;
484 #ifdef SYMBOLS_NEED_BACKPOINTERS
485 /* Remove SYMBOLP from the list. */
487 symbol_remove (symbolP
, rootPP
, lastPP
)
492 if (symbolP
== *rootPP
)
494 *rootPP
= symbolP
->sy_next
;
495 } /* if it was the root */
497 if (symbolP
== *lastPP
)
499 *lastPP
= symbolP
->sy_previous
;
500 } /* if it was the tail */
502 if (symbolP
->sy_next
!= NULL
)
504 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
507 if (symbolP
->sy_previous
!= NULL
)
509 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
512 debug_verify_symchain (*rootPP
, *lastPP
);
515 /* Link symbol ADDME before symbol TARGET in the chain. */
517 symbol_insert (addme
, target
, rootPP
, lastPP
)
523 if (target
->sy_previous
!= NULL
)
525 target
->sy_previous
->sy_next
= addme
;
529 know (*rootPP
== target
);
533 addme
->sy_previous
= target
->sy_previous
;
534 target
->sy_previous
= addme
;
535 addme
->sy_next
= target
;
537 debug_verify_symchain (*rootPP
, *lastPP
);
540 #endif /* SYMBOLS_NEED_BACKPOINTERS */
543 verify_symbol_chain (rootP
, lastP
)
547 symbolS
*symbolP
= rootP
;
552 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
554 #ifdef SYMBOLS_NEED_BACKPOINTERS
555 know (symbolP
->sy_next
->sy_previous
== symbolP
);
557 /* Walk the list anyways, to make sure pointers are still good. */
559 #endif /* SYMBOLS_NEED_BACKPOINTERS */
562 assert (lastP
== symbolP
);
566 verify_symbol_chain_2 (sym
)
569 symbolS
*p
= sym
, *n
= sym
;
570 #ifdef SYMBOLS_NEED_BACKPOINTERS
571 while (symbol_previous (p
))
572 p
= symbol_previous (p
);
574 while (symbol_next (n
))
576 verify_symbol_chain (p
, n
);
579 /* Resolve the value of a symbol. This is called during the final
580 pass over the symbol table to resolve any symbols with complex
584 resolve_symbol_value (symp
)
589 if (symp
->sy_resolved
)
594 if (symp
->sy_resolving
)
596 as_bad ("Symbol definition loop encountered at %s",
598 S_SET_VALUE (symp
, (valueT
) 0);
603 offsetT left
, right
, val
;
604 segT seg_left
, seg_right
;
606 symp
->sy_resolving
= 1;
608 switch (symp
->sy_value
.X_op
)
611 S_SET_VALUE (symp
, 0);
614 S_SET_VALUE (symp
, S_GET_VALUE (symp
) + symp
->sy_frag
->fr_address
);
615 if (S_GET_SEGMENT (symp
) == expr_section
)
616 S_SET_SEGMENT (symp
, absolute_section
);
621 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
623 #ifdef obj_frob_forward_symbol
624 /* Some object formats need to forward the segment. */
625 obj_frob_forward_symbol (symp
);
629 (symp
->sy_value
.X_add_number
630 + symp
->sy_frag
->fr_address
631 + S_GET_VALUE (symp
->sy_value
.X_add_symbol
)));
632 if (S_GET_SEGMENT (symp
) == expr_section
633 || S_GET_SEGMENT (symp
) == undefined_section
)
635 S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
));
637 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
642 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
643 if (symp
->sy_value
.X_op
== O_uminus
)
644 val
= - S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
646 val
= ~ S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
649 + symp
->sy_value
.X_add_number
650 + symp
->sy_frag
->fr_address
));
651 if (S_GET_SEGMENT (symp
) == expr_section
652 || S_GET_SEGMENT (symp
) == undefined_section
)
653 S_SET_SEGMENT (symp
, absolute_section
);
654 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
662 case O_bit_inclusive_or
:
664 case O_bit_exclusive_or
:
668 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
669 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
670 seg_left
= S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
);
671 seg_right
= S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
);
672 if (seg_left
!= seg_right
673 && seg_left
!= undefined_section
674 && seg_right
!= undefined_section
)
675 as_bad ("%s is operation on symbols in different sections",
677 if ((S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
)
679 && symp
->sy_value
.X_op
!= O_subtract
)
680 as_bad ("%s is illegal operation on non-absolute symbols",
682 left
= S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
683 right
= S_GET_VALUE (symp
->sy_value
.X_op_symbol
);
684 switch (symp
->sy_value
.X_op
)
686 case O_multiply
: val
= left
* right
; break;
687 case O_divide
: val
= left
/ right
; break;
688 case O_modulus
: val
= left
% right
; break;
689 case O_left_shift
: val
= left
<< right
; break;
690 case O_right_shift
: val
= left
>> right
; break;
691 case O_bit_inclusive_or
: val
= left
| right
; break;
692 case O_bit_or_not
: val
= left
|~ right
; break;
693 case O_bit_exclusive_or
: val
= left
^ right
; break;
694 case O_bit_and
: val
= left
& right
; break;
695 case O_add
: val
= left
+ right
; break;
696 case O_subtract
: val
= left
- right
; break;
700 (symp
->sy_value
.X_add_number
701 + symp
->sy_frag
->fr_address
703 if (S_GET_SEGMENT (symp
) == expr_section
704 || S_GET_SEGMENT (symp
) == undefined_section
)
705 S_SET_SEGMENT (symp
, absolute_section
);
706 resolved
= (symp
->sy_value
.X_add_symbol
->sy_resolved
707 && symp
->sy_value
.X_op_symbol
->sy_resolved
);
713 /* Give an error (below) if not in expr_section. We don't
714 want to worry about expr_section symbols, because they
715 are fictional (they are created as part of expression
716 resolution), and any problems may not actually mean
722 /* Don't worry if we can't resolve an expr_section symbol. */
724 symp
->sy_resolved
= 1;
725 else if (S_GET_SEGMENT (symp
) != expr_section
)
727 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp
));
728 symp
->sy_resolved
= 1;
732 #ifdef LOCAL_LABELS_DOLLAR
734 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
735 They are *really* local. That is, they go out of scope whenever we see a
736 label that isn't local. Also, like fb labels, there can be multiple
737 instances of a dollar label. Therefor, we name encode each instance with
738 the instance number, keep a list of defined symbols separate from the real
739 symbol table, and we treat these buggers as a sparse array. */
741 static long *dollar_labels
;
742 static long *dollar_label_instances
;
743 static char *dollar_label_defines
;
744 static long dollar_label_count
;
745 static unsigned long dollar_label_max
;
748 dollar_label_defined (label
)
753 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
755 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
757 return dollar_label_defines
[i
- dollar_labels
];
759 /* if we get here, label isn't defined */
761 } /* dollar_label_defined() */
764 dollar_label_instance (label
)
769 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
771 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
773 return (dollar_label_instances
[i
- dollar_labels
]);
775 /* If we get here, we haven't seen the label before, therefore its instance
781 dollar_label_clear ()
783 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
786 #define DOLLAR_LABEL_BUMP_BY 10
789 define_dollar_label (label
)
794 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
797 ++dollar_label_instances
[i
- dollar_labels
];
798 dollar_label_defines
[i
- dollar_labels
] = 1;
802 /* if we get to here, we don't have label listed yet. */
804 if (dollar_labels
== NULL
)
806 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
807 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
808 dollar_label_defines
= xmalloc (DOLLAR_LABEL_BUMP_BY
);
809 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
810 dollar_label_count
= 0;
812 else if (dollar_label_count
== dollar_label_max
)
814 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
815 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
816 dollar_label_max
* sizeof (long));
817 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
818 dollar_label_max
* sizeof (long));
819 dollar_label_defines
= xrealloc (dollar_label_defines
, dollar_label_max
);
820 } /* if we needed to grow */
822 dollar_labels
[dollar_label_count
] = label
;
823 dollar_label_instances
[dollar_label_count
] = 1;
824 dollar_label_defines
[dollar_label_count
] = 1;
825 ++dollar_label_count
;
829 * dollar_label_name()
831 * Caller must copy returned name: we re-use the area for the next name.
833 * The mth occurence of label n: is turned into the symbol "Ln^Am"
834 * where n is the label number and m is the instance number. "L" makes
835 * it a label discarded unless debugging and "^A"('\1') ensures no
836 * ordinary symbol SHOULD get the same name as a local label
837 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
839 * fb labels get the same treatment, except that ^B is used in place of ^A.
842 char * /* Return local label name. */
843 dollar_label_name (n
, augend
)
844 register long n
; /* we just saw "n$:" : n a number */
845 register int augend
; /* 0 for current instance, 1 for new instance */
848 /* Returned to caller, then copied. used for created names ("4f") */
849 static char symbol_name_build
[24];
852 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
855 know (augend
== 0 || augend
== 1);
856 p
= symbol_name_build
;
859 /* Next code just does sprintf( {}, "%d", n); */
861 q
= symbol_name_temporary
;
862 for (*q
++ = 0, i
= n
; i
; ++q
)
867 while ((*p
= *--q
) != '\0')
872 /* instance number */
873 q
= symbol_name_temporary
;
874 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
879 while ((*p
++ = *--q
) != '\0');;
881 /* The label, as a '\0' ended string, starts at symbol_name_build. */
882 return symbol_name_build
;
885 #endif /* LOCAL_LABELS_DOLLAR */
887 #ifdef LOCAL_LABELS_FB
890 * Sombody else's idea of local labels. They are made by "n:" where n
891 * is any decimal digit. Refer to them with
892 * "nb" for previous (backward) n:
893 * or "nf" for next (forward) n:.
895 * We do a little better and let n be any number, not just a single digit, but
896 * since the other guy's assembler only does ten, we treat the first ten
899 * Like someone else's assembler, we have one set of local label counters for
900 * entire assembly, not one set per (sub)segment like in most assemblers. This
901 * implies that one can refer to a label in another segment, and indeed some
902 * crufty compilers have done just that.
904 * Since there could be a LOT of these things, treat them as a sparse array.
907 #define FB_LABEL_SPECIAL (10)
909 static long fb_low_counter
[FB_LABEL_SPECIAL
];
910 static long *fb_labels
;
911 static long *fb_label_instances
;
912 static long fb_label_count
;
913 static long fb_label_max
;
915 /* this must be more than FB_LABEL_SPECIAL */
916 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
921 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
922 } /* fb_label_init() */
924 /* add one to the instance number of this fb label */
926 fb_label_instance_inc (label
)
931 if (label
< FB_LABEL_SPECIAL
)
933 ++fb_low_counter
[label
];
937 if (fb_labels
!= NULL
)
939 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
940 i
< fb_labels
+ fb_label_count
; ++i
)
944 ++fb_label_instances
[i
- fb_labels
];
946 } /* if we find it */
947 } /* for each existing label */
950 /* if we get to here, we don't have label listed yet. */
952 if (fb_labels
== NULL
)
954 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
955 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
956 fb_label_max
= FB_LABEL_BUMP_BY
;
957 fb_label_count
= FB_LABEL_SPECIAL
;
960 else if (fb_label_count
== fb_label_max
)
962 fb_label_max
+= FB_LABEL_BUMP_BY
;
963 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
964 fb_label_max
* sizeof (long));
965 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
966 fb_label_max
* sizeof (long));
967 } /* if we needed to grow */
969 fb_labels
[fb_label_count
] = label
;
970 fb_label_instances
[fb_label_count
] = 1;
975 fb_label_instance (label
)
980 if (label
< FB_LABEL_SPECIAL
)
982 return (fb_low_counter
[label
]);
985 if (fb_labels
!= NULL
)
987 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
988 i
< fb_labels
+ fb_label_count
; ++i
)
992 return (fb_label_instances
[i
- fb_labels
]);
993 } /* if we find it */
994 } /* for each existing label */
997 /* We didn't find the label, so this must be a reference to the
1005 * Caller must copy returned name: we re-use the area for the next name.
1007 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1008 * where n is the label number and m is the instance number. "L" makes
1009 * it a label discarded unless debugging and "^B"('\2') ensures no
1010 * ordinary symbol SHOULD get the same name as a local label
1011 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1013 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1015 char * /* Return local label name. */
1016 fb_label_name (n
, augend
)
1017 long n
; /* we just saw "n:", "nf" or "nb" : n a number */
1018 long augend
; /* 0 for nb, 1 for n:, nf */
1021 /* Returned to caller, then copied. used for created names ("4f") */
1022 static char symbol_name_build
[24];
1025 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
1028 know (augend
== 0 || augend
== 1);
1029 p
= symbol_name_build
;
1032 /* Next code just does sprintf( {}, "%d", n); */
1034 q
= symbol_name_temporary
;
1035 for (*q
++ = 0, i
= n
; i
; ++q
)
1040 while ((*p
= *--q
) != '\0')
1045 /* instance number */
1046 q
= symbol_name_temporary
;
1047 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1052 while ((*p
++ = *--q
) != '\0');;
1054 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1055 return (symbol_name_build
);
1056 } /* fb_label_name() */
1058 #endif /* LOCAL_LABELS_FB */
1062 * decode name that may have been generated by foo_label_name() above. If
1063 * the name wasn't generated by foo_label_name(), then return it unaltered.
1064 * This is used for error messages.
1068 decode_local_label_name (s
)
1072 char *symbol_decode
;
1074 int instance_number
;
1076 const char *message_format
= "\"%d\" (instance number %d of a %s label)";
1081 for (label_number
= 0, p
= s
+ 1; isdigit (*p
); ++p
)
1083 label_number
= (10 * label_number
) + *p
- '0';
1099 for (instance_number
= 0, p
= s
+ 1; isdigit (*p
); ++p
)
1101 instance_number
= (10 * instance_number
) + *p
- '0';
1104 symbol_decode
= obstack_alloc (¬es
, strlen (message_format
) + 30);
1105 (void) sprintf (symbol_decode
, message_format
, label_number
,
1106 instance_number
, type
);
1108 return (symbol_decode
);
1109 } /* decode_local_label_name() */
1111 /* Get the value of a symbol. */
1117 if (s
->sy_value
.X_op
!= O_constant
)
1118 as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s
));
1119 return (valueT
) s
->sy_value
.X_add_number
;
1122 /* Set the value of a symbol. */
1125 S_SET_VALUE (s
, val
)
1129 s
->sy_value
.X_op
= O_constant
;
1130 s
->sy_value
.X_add_number
= (offsetT
) val
;
1131 s
->sy_value
.X_unsigned
= 0;
1134 #ifdef BFD_ASSEMBLER
1140 flagword flags
= s
->bsym
->flags
;
1143 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1146 return (flags
& BSF_GLOBAL
) != 0;
1153 return bfd_is_com_section (s
->bsym
->section
);
1160 return s
->bsym
->section
!= undefined_section
;
1167 if (s
->bsym
->flags
& BSF_DEBUGGING
)
1176 flagword flags
= s
->bsym
->flags
;
1179 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1182 return (S_GET_NAME (s
)
1184 && (strchr (S_GET_NAME (s
), '\001')
1185 || strchr (S_GET_NAME (s
), '\002')
1186 || (S_LOCAL_NAME (s
)
1187 && !flag_keep_locals
)));
1194 return S_IS_EXTERNAL (s
);
1201 return S_GET_NAME (s
) == 0;
1208 return s
->bsym
->name
;
1215 return s
->bsym
->section
;
1219 S_SET_SEGMENT (s
, seg
)
1223 s
->bsym
->section
= seg
;
1230 s
->bsym
->flags
|= BSF_GLOBAL
;
1231 s
->bsym
->flags
&= ~(BSF_LOCAL
|BSF_WEAK
);
1235 S_CLEAR_EXTERNAL (s
)
1238 s
->bsym
->flags
|= BSF_LOCAL
;
1239 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_WEAK
);
1246 s
->bsym
->flags
|= BSF_WEAK
;
1247 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_LOCAL
);
1251 S_SET_NAME (s
, name
)
1255 s
->bsym
->name
= name
;
1257 #endif /* BFD_ASSEMBLER */
1262 symbol_lastP
= NULL
;
1263 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
1264 sy_hash
= hash_new ();
1266 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
1267 #ifdef BFD_ASSEMBLER
1268 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1269 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
1272 /* Can't initialise a union. Sigh. */
1273 S_SET_SEGMENT (&abs_symbol
, absolute_section
);
1275 abs_symbol
.sy_value
.X_op
= O_constant
;
1277 #ifdef LOCAL_LABELS_FB
1279 #endif /* LOCAL_LABELS_FB */
1282 static int indent_level
;
1287 printf ("%*s", indent_level
* 4, "");
1290 void print_expr_1
PARAMS ((FILE *, expressionS
*));
1291 void print_symbol_value_1
PARAMS ((FILE *, symbolS
*));
1294 print_symbol_value_1 (file
, sym
)
1298 const char *name
= S_GET_NAME (sym
);
1299 if (!name
|| !name
[0])
1301 fprintf (file
, "sym %lx %s frag %lx", sym
, name
, (long) sym
->sy_frag
);
1303 fprintf (file
, " written");
1304 if (sym
->sy_resolved
)
1305 fprintf (file
, " resolved");
1306 if (sym
->sy_resolving
)
1307 fprintf (file
, " resolving");
1308 if (sym
->sy_used_in_reloc
)
1309 fprintf (file
, " used-in-reloc");
1311 fprintf (file
, " used");
1312 if (sym
->sy_resolved
)
1314 /* XXX print segment name too */
1315 fprintf (file
, " value %lx", (long) S_GET_VALUE (sym
));
1317 else if (indent_level
< 8)
1320 fprintf (file
, "\n%*s<", indent_level
* 4, "");
1321 print_expr_1 (file
, &sym
->sy_value
);
1322 fprintf (file
, ">");
1329 print_symbol_value (sym
)
1333 print_symbol_value_1 (stderr
, sym
);
1334 fprintf (stderr
, "\n");
1338 print_expr_1 (file
, exp
)
1342 fprintf (file
, "expr %lx ", (long) exp
);
1346 fprintf (file
, "illegal");
1349 fprintf (file
, "absent");
1352 fprintf (file
, "constant %lx", (long) exp
->X_add_number
);
1356 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
1357 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1358 fprintf (file
, ">");
1361 if (exp
->X_add_number
)
1362 fprintf (file
, "\n%*s %lx", indent_level
* 4, "",
1363 (long) exp
->X_add_number
);
1366 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
1369 fprintf (file
, "big");
1372 fprintf (file
, "uminus -<");
1374 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1375 fprintf (file
, ">");
1376 goto maybe_print_addnum
;
1378 fprintf (file
, "bit_not");
1381 fprintf (file
, "multiply");
1384 fprintf (file
, "divide");
1387 fprintf (file
, "modulus");
1390 fprintf (file
, "lshift");
1393 fprintf (file
, "rshift");
1395 case O_bit_inclusive_or
:
1396 fprintf (file
, "bit_ior");
1398 case O_bit_exclusive_or
:
1399 fprintf (file
, "bit_xor");
1402 fprintf (file
, "bit_and");
1406 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
1407 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1408 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1409 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1410 fprintf (file
, ">");
1411 goto maybe_print_addnum
;
1414 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
1415 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1416 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1417 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1418 fprintf (file
, ">");
1419 goto maybe_print_addnum
;
1421 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
1431 print_expr_1 (stderr
, exp
);
1432 fprintf (stderr
, "\n");
1435 /* end of symbols.c */
This page took 0.063466 seconds and 5 git commands to generate.