1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
27 #include "obstack.h" /* For "symbols.h" */
30 /* This is non-zero if symbols are case sensitive, which is the
32 int symbols_case_sensitive
= 1;
34 #ifndef WORKING_DOT_WORD
35 extern int new_broken_words
;
38 /* symbol-name => struct symbol pointer */
39 static struct hash_control
*sy_hash
;
41 /* Below are commented in "symbols.h". */
42 symbolS
*symbol_rootP
;
43 symbolS
*symbol_lastP
;
47 #define debug_verify_symchain verify_symbol_chain
49 #define debug_verify_symchain(root, last) ((void) 0)
54 static void fb_label_init
PARAMS ((void));
58 Return a pointer to a new symbol. Die if we can't make a new
59 symbol. Fill in the symbol's values. Add symbol to end of symbol
62 This function should be called in the general case of creating a
63 symbol. However, if the output file symbol table has already been
64 set, and you are certain that this symbol won't be wanted in the
65 output file, you can call symbol_create. */
68 symbol_new (name
, segment
, valu
, frag
)
74 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
77 * Link to end of symbol chain.
81 extern int symbol_table_frozen
;
82 if (symbol_table_frozen
)
86 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
87 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
93 symbol_create (name
, segment
, valu
, frag
)
94 const char *name
; /* It is copied, the caller can destroy/modify */
95 segT segment
; /* Segment identifier (SEG_<something>) */
96 valueT valu
; /* Symbol value */
97 fragS
*frag
; /* Associated fragment */
99 unsigned int name_length
;
100 char *preserved_copy_of_name
;
103 name_length
= strlen (name
) + 1; /* +1 for \0 */
104 obstack_grow (¬es
, name
, name_length
);
105 preserved_copy_of_name
= obstack_finish (¬es
);
106 #ifdef STRIP_UNDERSCORE
107 if (preserved_copy_of_name
[0] == '_')
108 preserved_copy_of_name
++;
111 #ifdef tc_canonicalize_symbol_name
112 preserved_copy_of_name
=
113 tc_canonicalize_symbol_name (preserved_copy_of_name
);
116 if (! symbols_case_sensitive
)
120 for (s
= (unsigned char *) preserved_copy_of_name
; *s
!= '\0'; s
++)
125 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
127 /* symbol must be born in some fixed state. This seems as good as any. */
128 memset (symbolP
, 0, sizeof (symbolS
));
131 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
132 if (symbolP
->bsym
== NULL
)
133 as_perror ("%s", "bfd_make_empty_symbol");
134 symbolP
->bsym
->udata
.p
= (PTR
) symbolP
;
136 S_SET_NAME (symbolP
, preserved_copy_of_name
);
138 S_SET_SEGMENT (symbolP
, segment
);
139 S_SET_VALUE (symbolP
, valu
);
140 symbol_clear_list_pointers (symbolP
);
142 symbolP
->sy_frag
= frag
;
143 #ifndef BFD_ASSEMBLER
144 symbolP
->sy_number
= ~0;
145 symbolP
->sy_name_offset
= (unsigned int) ~0;
148 obj_symbol_new_hook (symbolP
);
150 #ifdef tc_symbol_new_hook
151 tc_symbol_new_hook (symbolP
);
161 * We have just seen "<name>:".
162 * Creates a struct symbol unless it already exists.
164 * Gripes if we are redefining a symbol incompatibly (and ignores it).
168 colon (sym_name
) /* just seen "x:" - rattle symbols & frags */
169 const char *sym_name
; /* symbol name, as a cannonical string */
170 /* We copy this string: OK to alter later. */
172 register symbolS
*symbolP
; /* symbol we are working with */
174 /* Sun local labels go out of scope whenever a non-local symbol is
176 if (LOCAL_LABELS_DOLLAR
&& ! LOCAL_LABEL (sym_name
))
177 dollar_label_clear ();
179 #ifndef WORKING_DOT_WORD
180 if (new_broken_words
)
182 struct broken_word
*a
;
187 extern const int md_short_jump_size
;
188 extern const int md_long_jump_size
;
189 possible_bytes
= (md_short_jump_size
190 + new_broken_words
* md_long_jump_size
);
193 frag_opcode
= frag_var (rs_broken_word
,
197 (symbolS
*) broken_words
,
201 /* We want to store the pointer to where to insert the jump table in the
202 fr_opcode of the rs_broken_word frag. This requires a little
205 && (frag_tmp
->fr_type
!= rs_broken_word
206 || frag_tmp
->fr_opcode
))
207 frag_tmp
= frag_tmp
->fr_next
;
209 frag_tmp
->fr_opcode
= frag_opcode
;
210 new_broken_words
= 0;
212 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
213 a
->dispfrag
= frag_tmp
;
215 #endif /* WORKING_DOT_WORD */
217 if ((symbolP
= symbol_find (sym_name
)) != 0)
219 #ifdef RESOLVE_SYMBOL_REDEFINITION
220 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
224 * Now check for undefined symbols
226 if (!S_IS_DEFINED (symbolP
))
228 if (S_GET_VALUE (symbolP
) == 0)
230 symbolP
->sy_frag
= frag_now
;
232 S_SET_OTHER(symbolP
, const_flag
);
234 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
235 S_SET_SEGMENT (symbolP
, now_seg
);
238 #endif /* if we have one, it better be zero. */
244 * There are still several cases to check:
245 * A .comm/.lcomm symbol being redefined as
246 * initialized data is OK
247 * A .comm/.lcomm symbol being redefined with
248 * a larger size is also OK
250 * This only used to be allowed on VMS gas, but Sun cc
251 * on the sparc also depends on it.
254 if (((!S_IS_DEBUG (symbolP
)
255 && !S_IS_DEFINED (symbolP
)
256 && S_IS_EXTERNAL (symbolP
))
257 || S_GET_SEGMENT (symbolP
) == bss_section
)
258 && (now_seg
== data_section
259 || now_seg
== S_GET_SEGMENT (symbolP
)))
262 * Select which of the 2 cases this is
264 if (now_seg
!= data_section
)
267 * New .comm for prev .comm symbol.
268 * If the new size is larger we just
269 * change its value. If the new size
270 * is smaller, we ignore this symbol
272 if (S_GET_VALUE (symbolP
)
273 < ((unsigned) frag_now_fix ()))
275 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
280 /* It is a .comm/.lcomm being converted to initialized
282 symbolP
->sy_frag
= frag_now
;
284 S_SET_OTHER(symbolP
, const_flag
);
286 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
287 S_SET_SEGMENT (symbolP
, now_seg
); /* keep N_EXT bit */
292 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
293 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
295 segment_name (S_GET_SEGMENT (symbolP
)),
296 S_GET_OTHER (symbolP
), S_GET_DESC (symbolP
),
297 (long) S_GET_VALUE (symbolP
));
299 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
301 segment_name (S_GET_SEGMENT (symbolP
)),
302 (long) S_GET_VALUE (symbolP
));
305 } /* if the undefined symbol has no value */
309 /* Don't blow up if the definition is the same */
310 if (!(frag_now
== symbolP
->sy_frag
311 && S_GET_VALUE (symbolP
) == frag_now_fix ()
312 && S_GET_SEGMENT (symbolP
) == now_seg
))
313 as_fatal ("Symbol %s already defined.", sym_name
);
314 } /* if this symbol is not yet defined */
319 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
322 S_SET_OTHER (symbolP
, const_flag
);
325 symbol_table_insert (symbolP
);
326 } /* if we have seen this symbol before */
328 if (mri_common_symbol
!= NULL
)
330 /* This symbol is actually being defined within an MRI common
331 section. This requires special handling. */
332 symbolP
->sy_value
.X_op
= O_symbol
;
333 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
334 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
335 symbolP
->sy_frag
= &zero_address_frag
;
336 S_SET_SEGMENT (symbolP
, expr_section
);
337 symbolP
->sy_mri_common
= 1;
341 tc_frob_label (symbolP
);
349 * symbol_table_insert()
351 * Die if we can't insert the symbol.
356 symbol_table_insert (symbolP
)
359 register const char *error_string
;
362 know (S_GET_NAME (symbolP
));
364 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (PTR
) symbolP
)))
366 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
367 S_GET_NAME (symbolP
), error_string
);
369 } /* symbol_table_insert() */
372 * symbol_find_or_make()
374 * If a symbol name does not exist, create it as undefined, and insert
375 * it into the symbol table. Return a pointer to it.
378 symbol_find_or_make (name
)
381 register symbolS
*symbolP
;
383 symbolP
= symbol_find (name
);
387 symbolP
= symbol_make (name
);
389 symbol_table_insert (symbolP
);
390 } /* if symbol wasn't found */
393 } /* symbol_find_or_make() */
401 /* Let the machine description default it, e.g. for register names. */
402 symbolP
= md_undefined_symbol ((char *) name
);
405 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
408 } /* symbol_make() */
413 * Implement symbol table lookup.
414 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
415 * Out: NULL if the name was not in the symbol table, else the address
416 * of a struct symbol associated with that name.
423 #ifdef STRIP_UNDERSCORE
424 return (symbol_find_base (name
, 1));
425 #else /* STRIP_UNDERSCORE */
426 return (symbol_find_base (name
, 0));
427 #endif /* STRIP_UNDERSCORE */
428 } /* symbol_find() */
431 symbol_find_base (name
, strip_underscore
)
433 int strip_underscore
;
435 if (strip_underscore
&& *name
== '_')
438 #ifdef tc_canonicalize_symbol_name
442 copy
= (char *) alloca (strlen (name
) + 1);
444 name
= tc_canonicalize_symbol_name (copy
);
448 if (! symbols_case_sensitive
)
452 copy
= (unsigned char *) alloca (strlen (name
) + 1);
453 name
= (const char *) copy
;
454 for (; *copy
!= '\0'; copy
++)
456 *copy
= toupper (*copy
);
459 return ((symbolS
*) hash_find (sy_hash
, name
));
463 * Once upon a time, symbols were kept in a singly linked list. At
464 * least coff needs to be able to rearrange them from time to time, for
465 * which a doubly linked list is much more convenient. Loic did these
466 * as macros which seemed dangerous to me so they're now functions.
470 /* Link symbol ADDME after symbol TARGET in the chain. */
472 symbol_append (addme
, target
, rootPP
, lastPP
)
480 know (*rootPP
== NULL
);
481 know (*lastPP
== NULL
);
485 } /* if the list is empty */
487 if (target
->sy_next
!= NULL
)
489 #ifdef SYMBOLS_NEED_BACKPOINTERS
490 target
->sy_next
->sy_previous
= addme
;
491 #endif /* SYMBOLS_NEED_BACKPOINTERS */
495 know (*lastPP
== target
);
497 } /* if we have a next */
499 addme
->sy_next
= target
->sy_next
;
500 target
->sy_next
= addme
;
502 #ifdef SYMBOLS_NEED_BACKPOINTERS
503 addme
->sy_previous
= target
;
504 #endif /* SYMBOLS_NEED_BACKPOINTERS */
507 /* Set the chain pointers of SYMBOL to null. */
509 symbol_clear_list_pointers (symbolP
)
512 symbolP
->sy_next
= NULL
;
513 #ifdef SYMBOLS_NEED_BACKPOINTERS
514 symbolP
->sy_previous
= NULL
;
518 #ifdef SYMBOLS_NEED_BACKPOINTERS
519 /* Remove SYMBOLP from the list. */
521 symbol_remove (symbolP
, rootPP
, lastPP
)
526 if (symbolP
== *rootPP
)
528 *rootPP
= symbolP
->sy_next
;
529 } /* if it was the root */
531 if (symbolP
== *lastPP
)
533 *lastPP
= symbolP
->sy_previous
;
534 } /* if it was the tail */
536 if (symbolP
->sy_next
!= NULL
)
538 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
541 if (symbolP
->sy_previous
!= NULL
)
543 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
546 debug_verify_symchain (*rootPP
, *lastPP
);
549 /* Link symbol ADDME before symbol TARGET in the chain. */
551 symbol_insert (addme
, target
, rootPP
, lastPP
)
557 if (target
->sy_previous
!= NULL
)
559 target
->sy_previous
->sy_next
= addme
;
563 know (*rootPP
== target
);
567 addme
->sy_previous
= target
->sy_previous
;
568 target
->sy_previous
= addme
;
569 addme
->sy_next
= target
;
571 debug_verify_symchain (*rootPP
, *lastPP
);
574 #endif /* SYMBOLS_NEED_BACKPOINTERS */
577 verify_symbol_chain (rootP
, lastP
)
581 symbolS
*symbolP
= rootP
;
586 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
588 #ifdef SYMBOLS_NEED_BACKPOINTERS
589 know (symbolP
->sy_next
->sy_previous
== symbolP
);
591 /* Walk the list anyways, to make sure pointers are still good. */
593 #endif /* SYMBOLS_NEED_BACKPOINTERS */
596 assert (lastP
== symbolP
);
600 verify_symbol_chain_2 (sym
)
603 symbolS
*p
= sym
, *n
= sym
;
604 #ifdef SYMBOLS_NEED_BACKPOINTERS
605 while (symbol_previous (p
))
606 p
= symbol_previous (p
);
608 while (symbol_next (n
))
610 verify_symbol_chain (p
, n
);
613 /* Resolve the value of a symbol. This is called during the final
614 pass over the symbol table to resolve any symbols with complex
618 resolve_symbol_value (symp
)
623 if (symp
->sy_resolved
)
628 if (symp
->sy_resolving
)
630 as_bad ("Symbol definition loop encountered at %s",
632 S_SET_VALUE (symp
, (valueT
) 0);
637 offsetT left
, right
, val
;
638 segT seg_left
, seg_right
;
640 symp
->sy_resolving
= 1;
643 switch (symp
->sy_value
.X_op
)
646 S_SET_VALUE (symp
, 0);
649 S_SET_VALUE (symp
, S_GET_VALUE (symp
) + symp
->sy_frag
->fr_address
);
650 if (S_GET_SEGMENT (symp
) == expr_section
)
651 S_SET_SEGMENT (symp
, absolute_section
);
656 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
658 if (symp
->sy_mri_common
)
660 /* This is a symbol inside an MRI common section. The
661 relocation routines are going to handle it specially.
662 Don't change the value. */
663 S_SET_VALUE (symp
, symp
->sy_value
.X_add_number
);
664 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
668 if (symp
->sy_value
.X_add_number
== 0)
669 copy_symbol_attributes (symp
, symp
->sy_value
.X_add_symbol
);
672 (symp
->sy_value
.X_add_number
673 + symp
->sy_frag
->fr_address
674 + S_GET_VALUE (symp
->sy_value
.X_add_symbol
)));
675 if (S_GET_SEGMENT (symp
) == expr_section
676 || S_GET_SEGMENT (symp
) == undefined_section
)
678 S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
));
680 /* If we have equated this symbol to an undefined symbol, we
681 keep X_op set to O_symbol. This permits the routine
682 which writes out relocation to detect this case, and
683 convert the relocation to be against the symbol to which
684 this symbol is equated. */
685 if (! S_IS_DEFINED (symp
) || S_IS_COMMON (symp
))
686 symp
->sy_value
.X_op
= O_symbol
;
688 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
694 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
695 if (symp
->sy_value
.X_op
== O_uminus
)
696 val
= - S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
697 else if (symp
->sy_value
.X_op
== O_logical_not
)
698 val
= ! S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
700 val
= ~ S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
703 + symp
->sy_value
.X_add_number
704 + symp
->sy_frag
->fr_address
));
705 if (S_GET_SEGMENT (symp
) == expr_section
706 || S_GET_SEGMENT (symp
) == undefined_section
)
707 S_SET_SEGMENT (symp
, absolute_section
);
708 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
712 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
713 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
714 seg_left
= S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
);
715 seg_right
= S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
);
716 /* This case comes up with PIC support. */
718 symbolS
*s_left
= symp
->sy_value
.X_add_symbol
;
719 symbolS
*s_right
= symp
->sy_value
.X_op_symbol
;
721 if (seg_left
== absolute_section
)
729 seg_left
= seg_right
;
732 if (seg_right
== absolute_section
733 && s_right
->sy_resolved
)
735 symp
->sy_value
.X_add_number
+= S_GET_VALUE (s_right
);
736 symp
->sy_value
.X_op_symbol
= 0;
737 symp
->sy_value
.X_add_symbol
= s_left
;
738 symp
->sy_value
.X_op
= O_symbol
;
749 case O_bit_inclusive_or
:
751 case O_bit_exclusive_or
:
762 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
763 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
764 seg_left
= S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
);
765 seg_right
= S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
);
766 if (seg_left
!= seg_right
767 && seg_left
!= undefined_section
768 && seg_right
!= undefined_section
)
773 if (expr_symbol_where (symp
, &file
, &line
))
776 "illegal operation on symbols in different sections");
779 ("%s set to illegal operation on symbols in different sections",
782 if ((S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
)
784 && symp
->sy_value
.X_op
!= O_subtract
)
789 if (expr_symbol_where (symp
, &file
, &line
))
790 as_bad_where (file
, line
,
791 "illegal operation on non-absolute symbols");
793 as_bad ("%s set to illegal operation on non-absolute symbols",
796 left
= S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
797 right
= S_GET_VALUE (symp
->sy_value
.X_op_symbol
);
798 switch (symp
->sy_value
.X_op
)
800 case O_multiply
: val
= left
* right
; break;
801 case O_divide
: val
= left
/ right
; break;
802 case O_modulus
: val
= left
% right
; break;
803 case O_left_shift
: val
= left
<< right
; break;
804 case O_right_shift
: val
= left
>> right
; break;
805 case O_bit_inclusive_or
: val
= left
| right
; break;
806 case O_bit_or_not
: val
= left
|~ right
; break;
807 case O_bit_exclusive_or
: val
= left
^ right
; break;
808 case O_bit_and
: val
= left
& right
; break;
809 case O_add
: val
= left
+ right
; break;
810 case O_subtract
: val
= left
- right
; break;
811 case O_eq
: val
= left
== right
? ~ (offsetT
) 0 : 0;
812 case O_ne
: val
= left
!= right
? ~ (offsetT
) 0 : 0;
813 case O_lt
: val
= left
< right
? ~ (offsetT
) 0 : 0;
814 case O_le
: val
= left
<= right
? ~ (offsetT
) 0 : 0;
815 case O_ge
: val
= left
>= right
? ~ (offsetT
) 0 : 0;
816 case O_gt
: val
= left
> right
? ~ (offsetT
) 0 : 0;
817 case O_logical_and
: val
= left
&& right
; break;
818 case O_logical_or
: val
= left
|| right
; break;
822 (symp
->sy_value
.X_add_number
823 + symp
->sy_frag
->fr_address
825 if (S_GET_SEGMENT (symp
) == expr_section
826 || S_GET_SEGMENT (symp
) == undefined_section
)
827 S_SET_SEGMENT (symp
, absolute_section
);
828 resolved
= (symp
->sy_value
.X_add_symbol
->sy_resolved
829 && symp
->sy_value
.X_op_symbol
->sy_resolved
);
835 /* Give an error (below) if not in expr_section. We don't
836 want to worry about expr_section symbols, because they
837 are fictional (they are created as part of expression
838 resolution), and any problems may not actually mean
844 /* Don't worry if we can't resolve an expr_section symbol. */
846 symp
->sy_resolved
= 1;
847 else if (S_GET_SEGMENT (symp
) != expr_section
)
849 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp
));
850 symp
->sy_resolved
= 1;
854 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
855 They are *really* local. That is, they go out of scope whenever we see a
856 label that isn't local. Also, like fb labels, there can be multiple
857 instances of a dollar label. Therefor, we name encode each instance with
858 the instance number, keep a list of defined symbols separate from the real
859 symbol table, and we treat these buggers as a sparse array. */
861 static long *dollar_labels
;
862 static long *dollar_label_instances
;
863 static char *dollar_label_defines
;
864 static long dollar_label_count
;
865 static unsigned long dollar_label_max
;
868 dollar_label_defined (label
)
873 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
875 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
877 return dollar_label_defines
[i
- dollar_labels
];
879 /* if we get here, label isn't defined */
881 } /* dollar_label_defined() */
884 dollar_label_instance (label
)
889 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
891 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
893 return (dollar_label_instances
[i
- dollar_labels
]);
895 /* If we get here, we haven't seen the label before, therefore its instance
901 dollar_label_clear ()
903 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
906 #define DOLLAR_LABEL_BUMP_BY 10
909 define_dollar_label (label
)
914 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
917 ++dollar_label_instances
[i
- dollar_labels
];
918 dollar_label_defines
[i
- dollar_labels
] = 1;
922 /* if we get to here, we don't have label listed yet. */
924 if (dollar_labels
== NULL
)
926 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
927 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
928 dollar_label_defines
= xmalloc (DOLLAR_LABEL_BUMP_BY
);
929 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
930 dollar_label_count
= 0;
932 else if (dollar_label_count
== dollar_label_max
)
934 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
935 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
936 dollar_label_max
* sizeof (long));
937 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
938 dollar_label_max
* sizeof (long));
939 dollar_label_defines
= xrealloc (dollar_label_defines
, dollar_label_max
);
940 } /* if we needed to grow */
942 dollar_labels
[dollar_label_count
] = label
;
943 dollar_label_instances
[dollar_label_count
] = 1;
944 dollar_label_defines
[dollar_label_count
] = 1;
945 ++dollar_label_count
;
949 * dollar_label_name()
951 * Caller must copy returned name: we re-use the area for the next name.
953 * The mth occurence of label n: is turned into the symbol "Ln^Am"
954 * where n is the label number and m is the instance number. "L" makes
955 * it a label discarded unless debugging and "^A"('\1') ensures no
956 * ordinary symbol SHOULD get the same name as a local label
957 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
959 * fb labels get the same treatment, except that ^B is used in place of ^A.
962 char * /* Return local label name. */
963 dollar_label_name (n
, augend
)
964 register long n
; /* we just saw "n$:" : n a number */
965 register int augend
; /* 0 for current instance, 1 for new instance */
968 /* Returned to caller, then copied. used for created names ("4f") */
969 static char symbol_name_build
[24];
972 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
975 know (augend
== 0 || augend
== 1);
976 p
= symbol_name_build
;
979 /* Next code just does sprintf( {}, "%d", n); */
981 q
= symbol_name_temporary
;
982 for (*q
++ = 0, i
= n
; i
; ++q
)
987 while ((*p
= *--q
) != '\0')
992 /* instance number */
993 q
= symbol_name_temporary
;
994 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
999 while ((*p
++ = *--q
) != '\0');;
1001 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1002 return symbol_name_build
;
1006 * Sombody else's idea of local labels. They are made by "n:" where n
1007 * is any decimal digit. Refer to them with
1008 * "nb" for previous (backward) n:
1009 * or "nf" for next (forward) n:.
1011 * We do a little better and let n be any number, not just a single digit, but
1012 * since the other guy's assembler only does ten, we treat the first ten
1015 * Like someone else's assembler, we have one set of local label counters for
1016 * entire assembly, not one set per (sub)segment like in most assemblers. This
1017 * implies that one can refer to a label in another segment, and indeed some
1018 * crufty compilers have done just that.
1020 * Since there could be a LOT of these things, treat them as a sparse array.
1023 #define FB_LABEL_SPECIAL (10)
1025 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1026 static long *fb_labels
;
1027 static long *fb_label_instances
;
1028 static long fb_label_count
;
1029 static long fb_label_max
;
1031 /* this must be more than FB_LABEL_SPECIAL */
1032 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1037 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1038 } /* fb_label_init() */
1040 /* add one to the instance number of this fb label */
1042 fb_label_instance_inc (label
)
1047 if (label
< FB_LABEL_SPECIAL
)
1049 ++fb_low_counter
[label
];
1053 if (fb_labels
!= NULL
)
1055 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1056 i
< fb_labels
+ fb_label_count
; ++i
)
1060 ++fb_label_instances
[i
- fb_labels
];
1062 } /* if we find it */
1063 } /* for each existing label */
1066 /* if we get to here, we don't have label listed yet. */
1068 if (fb_labels
== NULL
)
1070 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1071 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1072 fb_label_max
= FB_LABEL_BUMP_BY
;
1073 fb_label_count
= FB_LABEL_SPECIAL
;
1076 else if (fb_label_count
== fb_label_max
)
1078 fb_label_max
+= FB_LABEL_BUMP_BY
;
1079 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1080 fb_label_max
* sizeof (long));
1081 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1082 fb_label_max
* sizeof (long));
1083 } /* if we needed to grow */
1085 fb_labels
[fb_label_count
] = label
;
1086 fb_label_instances
[fb_label_count
] = 1;
1091 fb_label_instance (label
)
1096 if (label
< FB_LABEL_SPECIAL
)
1098 return (fb_low_counter
[label
]);
1101 if (fb_labels
!= NULL
)
1103 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1104 i
< fb_labels
+ fb_label_count
; ++i
)
1108 return (fb_label_instances
[i
- fb_labels
]);
1109 } /* if we find it */
1110 } /* for each existing label */
1113 /* We didn't find the label, so this must be a reference to the
1121 * Caller must copy returned name: we re-use the area for the next name.
1123 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1124 * where n is the label number and m is the instance number. "L" makes
1125 * it a label discarded unless debugging and "^B"('\2') ensures no
1126 * ordinary symbol SHOULD get the same name as a local label
1127 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1129 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1131 char * /* Return local label name. */
1132 fb_label_name (n
, augend
)
1133 long n
; /* we just saw "n:", "nf" or "nb" : n a number */
1134 long augend
; /* 0 for nb, 1 for n:, nf */
1137 /* Returned to caller, then copied. used for created names ("4f") */
1138 static char symbol_name_build
[24];
1141 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
1144 know (augend
== 0 || augend
== 1);
1145 p
= symbol_name_build
;
1148 /* Next code just does sprintf( {}, "%d", n); */
1150 q
= symbol_name_temporary
;
1151 for (*q
++ = 0, i
= n
; i
; ++q
)
1156 while ((*p
= *--q
) != '\0')
1161 /* instance number */
1162 q
= symbol_name_temporary
;
1163 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1168 while ((*p
++ = *--q
) != '\0');;
1170 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1171 return (symbol_name_build
);
1172 } /* fb_label_name() */
1175 * decode name that may have been generated by foo_label_name() above. If
1176 * the name wasn't generated by foo_label_name(), then return it unaltered.
1177 * This is used for error messages.
1181 decode_local_label_name (s
)
1185 char *symbol_decode
;
1187 int instance_number
;
1189 const char *message_format
= "\"%d\" (instance number %d of a %s label)";
1194 for (label_number
= 0, p
= s
+ 1; isdigit (*p
); ++p
)
1195 label_number
= (10 * label_number
) + *p
- '0';
1204 for (instance_number
= 0, p
++; isdigit (*p
); ++p
)
1205 instance_number
= (10 * instance_number
) + *p
- '0';
1207 symbol_decode
= obstack_alloc (¬es
, strlen (message_format
) + 30);
1208 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1210 return symbol_decode
;
1213 /* Get the value of a symbol. */
1219 if (!s
->sy_resolved
&& !s
->sy_resolving
&& s
->sy_value
.X_op
!= O_constant
)
1220 resolve_symbol_value (s
);
1221 if (s
->sy_value
.X_op
!= O_constant
)
1223 static symbolS
*recur
;
1225 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1226 may call S_GET_VALUE. We use a static symbol to avoid the
1227 immediate recursion. */
1229 return (valueT
) s
->sy_value
.X_add_number
;
1231 if (! s
->sy_resolved
1232 || s
->sy_value
.X_op
!= O_symbol
1233 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1234 as_bad ("Attempt to get value of unresolved symbol %s",
1238 return (valueT
) s
->sy_value
.X_add_number
;
1241 /* Set the value of a symbol. */
1244 S_SET_VALUE (s
, val
)
1248 s
->sy_value
.X_op
= O_constant
;
1249 s
->sy_value
.X_add_number
= (offsetT
) val
;
1250 s
->sy_value
.X_unsigned
= 0;
1254 copy_symbol_attributes (dest
, src
)
1255 symbolS
*dest
, *src
;
1257 #ifdef BFD_ASSEMBLER
1258 /* In an expression, transfer the settings of these flags.
1259 The user can override later, of course. */
1260 #define COPIED_SYMFLAGS (BSF_FUNCTION)
1261 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1264 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1265 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1269 #ifdef BFD_ASSEMBLER
1275 flagword flags
= s
->bsym
->flags
;
1278 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1281 return (flags
& BSF_GLOBAL
) != 0;
1288 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
1295 return bfd_is_com_section (s
->bsym
->section
);
1302 return s
->bsym
->section
!= undefined_section
;
1309 if (s
->bsym
->flags
& BSF_DEBUGGING
)
1318 flagword flags
= s
->bsym
->flags
;
1322 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1325 if (bfd_get_section (s
->bsym
) == reg_section
)
1328 name
= S_GET_NAME (s
);
1329 return (name
!= NULL
1331 && (strchr (name
, '\001')
1332 || strchr (name
, '\002')
1333 || (! flag_keep_locals
1334 && (LOCAL_LABEL (name
)
1337 && name
[1] == '?')))));
1344 return S_IS_EXTERNAL (s
);
1351 return S_GET_NAME (s
) == 0;
1358 return s
->bsym
->name
;
1365 return s
->bsym
->section
;
1369 S_SET_SEGMENT (s
, seg
)
1373 s
->bsym
->section
= seg
;
1380 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1382 /* Let .weak override .global. */
1385 s
->bsym
->flags
|= BSF_GLOBAL
;
1386 s
->bsym
->flags
&= ~(BSF_LOCAL
|BSF_WEAK
);
1390 S_CLEAR_EXTERNAL (s
)
1393 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1395 /* Let .weak override. */
1398 s
->bsym
->flags
|= BSF_LOCAL
;
1399 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_WEAK
);
1406 s
->bsym
->flags
|= BSF_WEAK
;
1407 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_LOCAL
);
1411 S_SET_NAME (s
, name
)
1415 s
->bsym
->name
= name
;
1417 #endif /* BFD_ASSEMBLER */
1422 symbol_lastP
= NULL
;
1423 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
1424 sy_hash
= hash_new ();
1426 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
1427 #ifdef BFD_ASSEMBLER
1428 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1429 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
1432 /* Can't initialise a union. Sigh. */
1433 S_SET_SEGMENT (&abs_symbol
, absolute_section
);
1435 abs_symbol
.sy_value
.X_op
= O_constant
;
1436 abs_symbol
.sy_frag
= &zero_address_frag
;
1438 if (LOCAL_LABELS_FB
)
1450 printf ("%*s", indent_level
* 4, "");
1455 void print_expr_1
PARAMS ((FILE *, expressionS
*));
1456 void print_symbol_value_1
PARAMS ((FILE *, symbolS
*));
1459 print_symbol_value_1 (file
, sym
)
1463 const char *name
= S_GET_NAME (sym
);
1464 if (!name
|| !name
[0])
1466 fprintf (file
, "sym %lx %s", (unsigned long) sym
, name
);
1467 if (sym
->sy_frag
!= &zero_address_frag
)
1468 fprintf (file
, " frag %lx", (long) sym
->sy_frag
);
1470 fprintf (file
, " written");
1471 if (sym
->sy_resolved
)
1472 fprintf (file
, " resolved");
1473 else if (sym
->sy_resolving
)
1474 fprintf (file
, " resolving");
1475 if (sym
->sy_used_in_reloc
)
1476 fprintf (file
, " used-in-reloc");
1478 fprintf (file
, " used");
1479 if (S_IS_LOCAL (sym
))
1480 fprintf (file
, " local");
1481 if (S_IS_EXTERN (sym
))
1482 fprintf (file
, " extern");
1483 if (S_IS_DEBUG (sym
))
1484 fprintf (file
, " debug");
1485 if (S_IS_DEFINED (sym
))
1486 fprintf (file
, " defined");
1487 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
1488 if (sym
->sy_resolved
)
1490 segT s
= S_GET_SEGMENT (sym
);
1492 if (s
!= undefined_section
1493 && s
!= expr_section
)
1494 fprintf (file
, " %lx", (long) S_GET_VALUE (sym
));
1496 else if (indent_level
< 8 && S_GET_SEGMENT (sym
) != undefined_section
)
1499 fprintf (file
, "\n%*s<", indent_level
* 4, "");
1500 print_expr_1 (file
, &sym
->sy_value
);
1501 fprintf (file
, ">");
1508 print_symbol_value (sym
)
1512 print_symbol_value_1 (stderr
, sym
);
1513 fprintf (stderr
, "\n");
1517 print_expr_1 (file
, exp
)
1521 fprintf (file
, "expr %lx ", (long) exp
);
1525 fprintf (file
, "illegal");
1528 fprintf (file
, "absent");
1531 fprintf (file
, "constant %lx", (long) exp
->X_add_number
);
1535 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
1536 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1537 fprintf (file
, ">");
1539 if (exp
->X_add_number
)
1540 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
1541 (long) exp
->X_add_number
);
1545 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
1548 fprintf (file
, "big");
1551 fprintf (file
, "uminus -<");
1553 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1554 fprintf (file
, ">");
1555 goto maybe_print_addnum
;
1557 fprintf (file
, "bit_not");
1560 fprintf (file
, "multiply");
1563 fprintf (file
, "divide");
1566 fprintf (file
, "modulus");
1569 fprintf (file
, "lshift");
1572 fprintf (file
, "rshift");
1574 case O_bit_inclusive_or
:
1575 fprintf (file
, "bit_ior");
1577 case O_bit_exclusive_or
:
1578 fprintf (file
, "bit_xor");
1581 fprintf (file
, "bit_and");
1584 fprintf (file
, "eq");
1587 fprintf (file
, "ne");
1590 fprintf (file
, "lt");
1593 fprintf (file
, "le");
1596 fprintf (file
, "ge");
1599 fprintf (file
, "gt");
1602 fprintf (file
, "logical_and");
1605 fprintf (file
, "logical_or");
1609 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
1610 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1611 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1612 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1613 fprintf (file
, ">");
1614 goto maybe_print_addnum
;
1617 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
1618 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1619 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1620 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1621 fprintf (file
, ">");
1622 goto maybe_print_addnum
;
1624 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
1634 print_expr_1 (stderr
, exp
);
1635 fprintf (stderr
, "\n");
1639 symbol_print_statistics (file
)
1642 hash_print_statistics (file
, "symbol table", sy_hash
);
1645 /* end of symbols.c */
This page took 0.061454 seconds and 4 git commands to generate.