1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
28 #include "obstack.h" /* For "symbols.h" */
31 /* This is non-zero if symbols are case sensitive, which is the
33 int symbols_case_sensitive
= 1;
35 #ifndef WORKING_DOT_WORD
36 extern int new_broken_words
;
39 /* symbol-name => struct symbol pointer */
40 static struct hash_control
*sy_hash
;
42 /* Below are commented in "symbols.h". */
43 symbolS
*symbol_rootP
;
44 symbolS
*symbol_lastP
;
48 #define debug_verify_symchain verify_symbol_chain
50 #define debug_verify_symchain(root, last) ((void) 0)
55 static void fb_label_init
PARAMS ((void));
59 Return a pointer to a new symbol. Die if we can't make a new
60 symbol. Fill in the symbol's values. Add symbol to end of symbol
63 This function should be called in the general case of creating a
64 symbol. However, if the output file symbol table has already been
65 set, and you are certain that this symbol won't be wanted in the
66 output file, you can call symbol_create. */
69 symbol_new (name
, segment
, valu
, frag
)
75 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
78 * Link to end of symbol chain.
82 extern int symbol_table_frozen
;
83 if (symbol_table_frozen
)
87 symbol_append (symbolP
, symbol_lastP
, &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
)
181 local
= bfd_is_local_label_name (stdoutput
, sym_name
);
183 local
= LOCAL_LABEL (sym_name
);
187 dollar_label_clear ();
190 #ifndef WORKING_DOT_WORD
191 if (new_broken_words
)
193 struct broken_word
*a
;
198 extern const int md_short_jump_size
;
199 extern const int md_long_jump_size
;
200 possible_bytes
= (md_short_jump_size
201 + new_broken_words
* md_long_jump_size
);
204 frag_opcode
= frag_var (rs_broken_word
,
208 (symbolS
*) broken_words
,
212 /* We want to store the pointer to where to insert the jump table in the
213 fr_opcode of the rs_broken_word frag. This requires a little
216 && (frag_tmp
->fr_type
!= rs_broken_word
217 || frag_tmp
->fr_opcode
))
218 frag_tmp
= frag_tmp
->fr_next
;
220 frag_tmp
->fr_opcode
= frag_opcode
;
221 new_broken_words
= 0;
223 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
224 a
->dispfrag
= frag_tmp
;
226 #endif /* WORKING_DOT_WORD */
228 if ((symbolP
= symbol_find (sym_name
)) != 0)
230 #ifdef RESOLVE_SYMBOL_REDEFINITION
231 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
235 * Now check for undefined symbols
237 if (!S_IS_DEFINED (symbolP
))
239 if (S_GET_VALUE (symbolP
) == 0)
241 symbolP
->sy_frag
= frag_now
;
243 S_SET_OTHER(symbolP
, const_flag
);
245 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
246 S_SET_SEGMENT (symbolP
, now_seg
);
249 #endif /* if we have one, it better be zero. */
255 * There are still several cases to check:
256 * A .comm/.lcomm symbol being redefined as
257 * initialized data is OK
258 * A .comm/.lcomm symbol being redefined with
259 * a larger size is also OK
261 * This only used to be allowed on VMS gas, but Sun cc
262 * on the sparc also depends on it.
265 if (((!S_IS_DEBUG (symbolP
)
266 && !S_IS_DEFINED (symbolP
)
267 && S_IS_EXTERNAL (symbolP
))
268 || S_GET_SEGMENT (symbolP
) == bss_section
)
269 && (now_seg
== data_section
270 || now_seg
== S_GET_SEGMENT (symbolP
)))
273 * Select which of the 2 cases this is
275 if (now_seg
!= data_section
)
278 * New .comm for prev .comm symbol.
279 * If the new size is larger we just
280 * change its value. If the new size
281 * is smaller, we ignore this symbol
283 if (S_GET_VALUE (symbolP
)
284 < ((unsigned) frag_now_fix ()))
286 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
291 /* It is a .comm/.lcomm being converted to initialized
293 symbolP
->sy_frag
= frag_now
;
295 S_SET_OTHER(symbolP
, const_flag
);
297 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
298 S_SET_SEGMENT (symbolP
, now_seg
); /* keep N_EXT bit */
303 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
304 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
306 segment_name (S_GET_SEGMENT (symbolP
)),
307 S_GET_OTHER (symbolP
), S_GET_DESC (symbolP
),
308 (long) S_GET_VALUE (symbolP
));
310 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
312 segment_name (S_GET_SEGMENT (symbolP
)),
313 (long) S_GET_VALUE (symbolP
));
316 } /* if the undefined symbol has no value */
320 /* Don't blow up if the definition is the same */
321 if (!(frag_now
== symbolP
->sy_frag
322 && S_GET_VALUE (symbolP
) == frag_now_fix ()
323 && S_GET_SEGMENT (symbolP
) == now_seg
))
324 as_fatal ("Symbol %s already defined.", sym_name
);
325 } /* if this symbol is not yet defined */
330 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
333 S_SET_OTHER (symbolP
, const_flag
);
336 symbol_table_insert (symbolP
);
337 } /* if we have seen this symbol before */
339 if (mri_common_symbol
!= NULL
)
341 /* This symbol is actually being defined within an MRI common
342 section. This requires special handling. */
343 symbolP
->sy_value
.X_op
= O_symbol
;
344 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
345 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
346 symbolP
->sy_frag
= &zero_address_frag
;
347 S_SET_SEGMENT (symbolP
, expr_section
);
348 symbolP
->sy_mri_common
= 1;
352 tc_frob_label (symbolP
);
360 * symbol_table_insert()
362 * Die if we can't insert the symbol.
367 symbol_table_insert (symbolP
)
370 register const char *error_string
;
373 know (S_GET_NAME (symbolP
));
375 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (PTR
) symbolP
)))
377 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
378 S_GET_NAME (symbolP
), error_string
);
380 } /* symbol_table_insert() */
383 * symbol_find_or_make()
385 * If a symbol name does not exist, create it as undefined, and insert
386 * it into the symbol table. Return a pointer to it.
389 symbol_find_or_make (name
)
392 register symbolS
*symbolP
;
394 symbolP
= symbol_find (name
);
398 symbolP
= symbol_make (name
);
400 symbol_table_insert (symbolP
);
401 } /* if symbol wasn't found */
404 } /* symbol_find_or_make() */
412 /* Let the machine description default it, e.g. for register names. */
413 symbolP
= md_undefined_symbol ((char *) name
);
416 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
419 } /* symbol_make() */
424 * Implement symbol table lookup.
425 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
426 * Out: NULL if the name was not in the symbol table, else the address
427 * of a struct symbol associated with that name.
434 #ifdef STRIP_UNDERSCORE
435 return (symbol_find_base (name
, 1));
436 #else /* STRIP_UNDERSCORE */
437 return (symbol_find_base (name
, 0));
438 #endif /* STRIP_UNDERSCORE */
439 } /* symbol_find() */
442 symbol_find_base (name
, strip_underscore
)
444 int strip_underscore
;
446 if (strip_underscore
&& *name
== '_')
449 #ifdef tc_canonicalize_symbol_name
453 copy
= (char *) alloca (strlen (name
) + 1);
455 name
= tc_canonicalize_symbol_name (copy
);
459 if (! symbols_case_sensitive
)
463 copy
= (unsigned char *) alloca (strlen (name
) + 1);
464 name
= (const char *) copy
;
465 for (; *copy
!= '\0'; copy
++)
467 *copy
= toupper (*copy
);
470 return ((symbolS
*) hash_find (sy_hash
, name
));
474 * Once upon a time, symbols were kept in a singly linked list. At
475 * least coff needs to be able to rearrange them from time to time, for
476 * which a doubly linked list is much more convenient. Loic did these
477 * as macros which seemed dangerous to me so they're now functions.
481 /* Link symbol ADDME after symbol TARGET in the chain. */
483 symbol_append (addme
, target
, rootPP
, lastPP
)
491 know (*rootPP
== NULL
);
492 know (*lastPP
== NULL
);
493 addme
->sy_next
= NULL
;
494 #ifdef SYMBOLS_NEED_BACKPOINTERS
495 addme
->sy_previous
= NULL
;
500 } /* if the list is empty */
502 if (target
->sy_next
!= NULL
)
504 #ifdef SYMBOLS_NEED_BACKPOINTERS
505 target
->sy_next
->sy_previous
= addme
;
506 #endif /* SYMBOLS_NEED_BACKPOINTERS */
510 know (*lastPP
== target
);
512 } /* if we have a next */
514 addme
->sy_next
= target
->sy_next
;
515 target
->sy_next
= addme
;
517 #ifdef SYMBOLS_NEED_BACKPOINTERS
518 addme
->sy_previous
= target
;
519 #endif /* SYMBOLS_NEED_BACKPOINTERS */
521 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
524 /* Set the chain pointers of SYMBOL to null. */
526 symbol_clear_list_pointers (symbolP
)
529 symbolP
->sy_next
= NULL
;
530 #ifdef SYMBOLS_NEED_BACKPOINTERS
531 symbolP
->sy_previous
= NULL
;
535 #ifdef SYMBOLS_NEED_BACKPOINTERS
536 /* Remove SYMBOLP from the list. */
538 symbol_remove (symbolP
, rootPP
, lastPP
)
543 if (symbolP
== *rootPP
)
545 *rootPP
= symbolP
->sy_next
;
546 } /* if it was the root */
548 if (symbolP
== *lastPP
)
550 *lastPP
= symbolP
->sy_previous
;
551 } /* if it was the tail */
553 if (symbolP
->sy_next
!= NULL
)
555 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
558 if (symbolP
->sy_previous
!= NULL
)
560 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
563 debug_verify_symchain (*rootPP
, *lastPP
);
566 /* Link symbol ADDME before symbol TARGET in the chain. */
568 symbol_insert (addme
, target
, rootPP
, lastPP
)
574 if (target
->sy_previous
!= NULL
)
576 target
->sy_previous
->sy_next
= addme
;
580 know (*rootPP
== target
);
584 addme
->sy_previous
= target
->sy_previous
;
585 target
->sy_previous
= addme
;
586 addme
->sy_next
= target
;
588 debug_verify_symchain (*rootPP
, *lastPP
);
591 #endif /* SYMBOLS_NEED_BACKPOINTERS */
594 verify_symbol_chain (rootP
, lastP
)
598 symbolS
*symbolP
= rootP
;
603 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
605 #ifdef SYMBOLS_NEED_BACKPOINTERS
606 assert (symbolP
->sy_next
->sy_previous
== symbolP
);
608 /* Walk the list anyways, to make sure pointers are still good. */
610 #endif /* SYMBOLS_NEED_BACKPOINTERS */
613 assert (lastP
== symbolP
);
617 verify_symbol_chain_2 (sym
)
620 symbolS
*p
= sym
, *n
= sym
;
621 #ifdef SYMBOLS_NEED_BACKPOINTERS
622 while (symbol_previous (p
))
623 p
= symbol_previous (p
);
625 while (symbol_next (n
))
627 verify_symbol_chain (p
, n
);
630 /* Resolve the value of a symbol. This is called during the final
631 pass over the symbol table to resolve any symbols with complex
635 resolve_symbol_value (symp
)
640 if (symp
->sy_resolved
)
645 if (symp
->sy_resolving
)
647 as_bad ("Symbol definition loop encountered at %s",
649 S_SET_VALUE (symp
, (valueT
) 0);
654 offsetT left
, right
, val
;
655 segT seg_left
, seg_right
;
657 symp
->sy_resolving
= 1;
659 /* Simplify addition or subtraction of a constant by folding the
660 constant into X_add_number. */
661 if (symp
->sy_value
.X_op
== O_add
662 || symp
->sy_value
.X_op
== O_subtract
)
664 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
665 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
666 if (S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) == absolute_section
)
668 right
= S_GET_VALUE (symp
->sy_value
.X_op_symbol
);
669 if (symp
->sy_value
.X_op
== O_add
)
670 symp
->sy_value
.X_add_number
+= right
;
672 symp
->sy_value
.X_add_number
-= right
;
673 symp
->sy_value
.X_op
= O_symbol
;
674 symp
->sy_value
.X_op_symbol
= NULL
;
676 else if ((S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
)
678 && symp
->sy_value
.X_op
== O_add
)
680 left
= S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
681 symp
->sy_value
.X_add_symbol
= symp
->sy_value
.X_op_symbol
;
682 symp
->sy_value
.X_add_number
+= left
;
683 symp
->sy_value
.X_op
= O_symbol
;
684 symp
->sy_value
.X_op_symbol
= NULL
;
688 switch (symp
->sy_value
.X_op
)
691 S_SET_VALUE (symp
, 0);
694 S_SET_VALUE (symp
, S_GET_VALUE (symp
) + symp
->sy_frag
->fr_address
);
695 if (S_GET_SEGMENT (symp
) == expr_section
)
696 S_SET_SEGMENT (symp
, absolute_section
);
701 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
703 if (symp
->sy_mri_common
)
705 /* This is a symbol inside an MRI common section. The
706 relocation routines are going to handle it specially.
707 Don't change the value. */
708 S_SET_VALUE (symp
, symp
->sy_value
.X_add_number
);
709 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
713 if (symp
->sy_value
.X_add_number
== 0)
714 copy_symbol_attributes (symp
, symp
->sy_value
.X_add_symbol
);
716 /* If we have equated this symbol to an undefined symbol, we
717 keep X_op set to O_symbol, and we don't change
718 X_add_number. This permits the routine which writes out
719 relocation to detect this case, and convert the
720 relocation to be against the symbol to which this symbol
722 if (! S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
723 || S_IS_COMMON (symp
->sy_value
.X_add_symbol
))
725 symp
->sy_value
.X_op
= O_symbol
;
727 S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
));
732 (symp
->sy_value
.X_add_number
733 + symp
->sy_frag
->fr_address
734 + S_GET_VALUE (symp
->sy_value
.X_add_symbol
)));
735 if (S_GET_SEGMENT (symp
) == expr_section
736 || S_GET_SEGMENT (symp
) == undefined_section
)
738 S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
));
741 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
747 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
748 if (symp
->sy_value
.X_op
== O_uminus
)
749 val
= - S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
750 else if (symp
->sy_value
.X_op
== O_logical_not
)
751 val
= ! S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
753 val
= ~ S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
756 + symp
->sy_value
.X_add_number
757 + symp
->sy_frag
->fr_address
));
758 if (S_GET_SEGMENT (symp
) == expr_section
759 || S_GET_SEGMENT (symp
) == undefined_section
)
760 S_SET_SEGMENT (symp
, absolute_section
);
761 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
769 case O_bit_inclusive_or
:
771 case O_bit_exclusive_or
:
783 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
784 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
785 seg_left
= S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
);
786 seg_right
= S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
);
787 left
= S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
788 right
= S_GET_VALUE (symp
->sy_value
.X_op_symbol
);
790 /* Subtraction is permitted if both operands are in the same
791 section. Otherwise, both operands must be absolute. We
792 already handled the case of addition or subtraction of a
793 constant above. This will probably need to be changed
794 for an object file format which supports arbitrary
795 expressions, such as IEEE-695. */
796 if ((seg_left
!= absolute_section
797 || seg_right
!= absolute_section
)
798 && (symp
->sy_value
.X_op
!= O_subtract
799 || seg_left
!= seg_right
))
804 if (expr_symbol_where (symp
, &file
, &line
))
806 if (seg_left
== undefined_section
)
807 as_bad_where (file
, line
,
808 "undefined symbol %s in operation",
809 S_GET_NAME (symp
->sy_value
.X_add_symbol
));
810 if (seg_right
== undefined_section
)
811 as_bad_where (file
, line
,
812 "undefined symbol %s in operation",
813 S_GET_NAME (symp
->sy_value
.X_op_symbol
));
814 if (seg_left
!= undefined_section
815 && seg_right
!= undefined_section
)
816 as_bad_where (file
, line
, "invalid section for operation");
820 if (seg_left
== undefined_section
)
821 as_bad ("undefined symbol %s in operation setting %s",
822 S_GET_NAME (symp
->sy_value
.X_add_symbol
),
824 if (seg_right
== undefined_section
)
825 as_bad ("undefined symbol %s in operation setting %s",
826 S_GET_NAME (symp
->sy_value
.X_op_symbol
),
828 if (seg_left
!= undefined_section
829 && seg_right
!= undefined_section
)
830 as_bad ("invalid section for operation setting %s",
835 switch (symp
->sy_value
.X_op
)
837 case O_multiply
: val
= left
* right
; break;
838 case O_divide
: val
= left
/ right
; break;
839 case O_modulus
: val
= left
% right
; break;
840 case O_left_shift
: val
= left
<< right
; break;
841 case O_right_shift
: val
= left
>> right
; break;
842 case O_bit_inclusive_or
: val
= left
| right
; break;
843 case O_bit_or_not
: val
= left
|~ right
; break;
844 case O_bit_exclusive_or
: val
= left
^ right
; break;
845 case O_bit_and
: val
= left
& right
; break;
846 case O_add
: val
= left
+ right
; break;
847 case O_subtract
: val
= left
- right
; break;
848 case O_eq
: val
= left
== right
? ~ (offsetT
) 0 : 0;
849 case O_ne
: val
= left
!= right
? ~ (offsetT
) 0 : 0;
850 case O_lt
: val
= left
< right
? ~ (offsetT
) 0 : 0;
851 case O_le
: val
= left
<= right
? ~ (offsetT
) 0 : 0;
852 case O_ge
: val
= left
>= right
? ~ (offsetT
) 0 : 0;
853 case O_gt
: val
= left
> right
? ~ (offsetT
) 0 : 0;
854 case O_logical_and
: val
= left
&& right
; break;
855 case O_logical_or
: val
= left
|| right
; break;
859 (symp
->sy_value
.X_add_number
860 + symp
->sy_frag
->fr_address
862 if (S_GET_SEGMENT (symp
) == expr_section
863 || S_GET_SEGMENT (symp
) == undefined_section
)
864 S_SET_SEGMENT (symp
, absolute_section
);
865 resolved
= (symp
->sy_value
.X_add_symbol
->sy_resolved
866 && symp
->sy_value
.X_op_symbol
->sy_resolved
);
872 /* Give an error (below) if not in expr_section. We don't
873 want to worry about expr_section symbols, because they
874 are fictional (they are created as part of expression
875 resolution), and any problems may not actually mean
881 /* Don't worry if we can't resolve an expr_section symbol. */
883 symp
->sy_resolved
= 1;
884 else if (S_GET_SEGMENT (symp
) != expr_section
)
886 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp
));
887 symp
->sy_resolved
= 1;
891 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
892 They are *really* local. That is, they go out of scope whenever we see a
893 label that isn't local. Also, like fb labels, there can be multiple
894 instances of a dollar label. Therefor, we name encode each instance with
895 the instance number, keep a list of defined symbols separate from the real
896 symbol table, and we treat these buggers as a sparse array. */
898 static long *dollar_labels
;
899 static long *dollar_label_instances
;
900 static char *dollar_label_defines
;
901 static long dollar_label_count
;
902 static unsigned long dollar_label_max
;
905 dollar_label_defined (label
)
910 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
912 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
914 return dollar_label_defines
[i
- dollar_labels
];
916 /* if we get here, label isn't defined */
918 } /* dollar_label_defined() */
921 dollar_label_instance (label
)
926 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
928 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
930 return (dollar_label_instances
[i
- dollar_labels
]);
932 /* If we get here, we haven't seen the label before, therefore its instance
938 dollar_label_clear ()
940 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
943 #define DOLLAR_LABEL_BUMP_BY 10
946 define_dollar_label (label
)
951 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
954 ++dollar_label_instances
[i
- dollar_labels
];
955 dollar_label_defines
[i
- dollar_labels
] = 1;
959 /* if we get to here, we don't have label listed yet. */
961 if (dollar_labels
== NULL
)
963 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
964 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
965 dollar_label_defines
= xmalloc (DOLLAR_LABEL_BUMP_BY
);
966 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
967 dollar_label_count
= 0;
969 else if (dollar_label_count
== dollar_label_max
)
971 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
972 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
973 dollar_label_max
* sizeof (long));
974 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
975 dollar_label_max
* sizeof (long));
976 dollar_label_defines
= xrealloc (dollar_label_defines
, dollar_label_max
);
977 } /* if we needed to grow */
979 dollar_labels
[dollar_label_count
] = label
;
980 dollar_label_instances
[dollar_label_count
] = 1;
981 dollar_label_defines
[dollar_label_count
] = 1;
982 ++dollar_label_count
;
986 * dollar_label_name()
988 * Caller must copy returned name: we re-use the area for the next name.
990 * The mth occurence of label n: is turned into the symbol "Ln^Am"
991 * where n is the label number and m is the instance number. "L" makes
992 * it a label discarded unless debugging and "^A"('\1') ensures no
993 * ordinary symbol SHOULD get the same name as a local label
994 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
996 * fb labels get the same treatment, except that ^B is used in place of ^A.
999 char * /* Return local label name. */
1000 dollar_label_name (n
, augend
)
1001 register long n
; /* we just saw "n$:" : n a number */
1002 register int augend
; /* 0 for current instance, 1 for new instance */
1005 /* Returned to caller, then copied. used for created names ("4f") */
1006 static char symbol_name_build
[24];
1009 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
1012 know (augend
== 0 || augend
== 1);
1013 p
= symbol_name_build
;
1016 /* Next code just does sprintf( {}, "%d", n); */
1018 q
= symbol_name_temporary
;
1019 for (*q
++ = 0, i
= n
; i
; ++q
)
1024 while ((*p
= *--q
) != '\0')
1029 /* instance number */
1030 q
= symbol_name_temporary
;
1031 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1036 while ((*p
++ = *--q
) != '\0');;
1038 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1039 return symbol_name_build
;
1043 * Sombody else's idea of local labels. They are made by "n:" where n
1044 * is any decimal digit. Refer to them with
1045 * "nb" for previous (backward) n:
1046 * or "nf" for next (forward) n:.
1048 * We do a little better and let n be any number, not just a single digit, but
1049 * since the other guy's assembler only does ten, we treat the first ten
1052 * Like someone else's assembler, we have one set of local label counters for
1053 * entire assembly, not one set per (sub)segment like in most assemblers. This
1054 * implies that one can refer to a label in another segment, and indeed some
1055 * crufty compilers have done just that.
1057 * Since there could be a LOT of these things, treat them as a sparse array.
1060 #define FB_LABEL_SPECIAL (10)
1062 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1063 static long *fb_labels
;
1064 static long *fb_label_instances
;
1065 static long fb_label_count
;
1066 static long fb_label_max
;
1068 /* this must be more than FB_LABEL_SPECIAL */
1069 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1074 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1075 } /* fb_label_init() */
1077 /* add one to the instance number of this fb label */
1079 fb_label_instance_inc (label
)
1084 if (label
< FB_LABEL_SPECIAL
)
1086 ++fb_low_counter
[label
];
1090 if (fb_labels
!= NULL
)
1092 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1093 i
< fb_labels
+ fb_label_count
; ++i
)
1097 ++fb_label_instances
[i
- fb_labels
];
1099 } /* if we find it */
1100 } /* for each existing label */
1103 /* if we get to here, we don't have label listed yet. */
1105 if (fb_labels
== NULL
)
1107 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1108 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1109 fb_label_max
= FB_LABEL_BUMP_BY
;
1110 fb_label_count
= FB_LABEL_SPECIAL
;
1113 else if (fb_label_count
== fb_label_max
)
1115 fb_label_max
+= FB_LABEL_BUMP_BY
;
1116 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1117 fb_label_max
* sizeof (long));
1118 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1119 fb_label_max
* sizeof (long));
1120 } /* if we needed to grow */
1122 fb_labels
[fb_label_count
] = label
;
1123 fb_label_instances
[fb_label_count
] = 1;
1128 fb_label_instance (label
)
1133 if (label
< FB_LABEL_SPECIAL
)
1135 return (fb_low_counter
[label
]);
1138 if (fb_labels
!= NULL
)
1140 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1141 i
< fb_labels
+ fb_label_count
; ++i
)
1145 return (fb_label_instances
[i
- fb_labels
]);
1146 } /* if we find it */
1147 } /* for each existing label */
1150 /* We didn't find the label, so this must be a reference to the
1158 * Caller must copy returned name: we re-use the area for the next name.
1160 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1161 * where n is the label number and m is the instance number. "L" makes
1162 * it a label discarded unless debugging and "^B"('\2') ensures no
1163 * ordinary symbol SHOULD get the same name as a local label
1164 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1166 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1168 char * /* Return local label name. */
1169 fb_label_name (n
, augend
)
1170 long n
; /* we just saw "n:", "nf" or "nb" : n a number */
1171 long augend
; /* 0 for nb, 1 for n:, nf */
1174 /* Returned to caller, then copied. used for created names ("4f") */
1175 static char symbol_name_build
[24];
1178 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
1181 know (augend
== 0 || augend
== 1);
1182 p
= symbol_name_build
;
1185 /* Next code just does sprintf( {}, "%d", n); */
1187 q
= symbol_name_temporary
;
1188 for (*q
++ = 0, i
= n
; i
; ++q
)
1193 while ((*p
= *--q
) != '\0')
1198 /* instance number */
1199 q
= symbol_name_temporary
;
1200 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1205 while ((*p
++ = *--q
) != '\0');;
1207 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1208 return (symbol_name_build
);
1209 } /* fb_label_name() */
1212 * decode name that may have been generated by foo_label_name() above. If
1213 * the name wasn't generated by foo_label_name(), then return it unaltered.
1214 * This is used for error messages.
1218 decode_local_label_name (s
)
1222 char *symbol_decode
;
1224 int instance_number
;
1226 const char *message_format
= "\"%d\" (instance number %d of a %s label)";
1231 for (label_number
= 0, p
= s
+ 1; isdigit (*p
); ++p
)
1232 label_number
= (10 * label_number
) + *p
- '0';
1241 for (instance_number
= 0, p
++; isdigit (*p
); ++p
)
1242 instance_number
= (10 * instance_number
) + *p
- '0';
1244 symbol_decode
= obstack_alloc (¬es
, strlen (message_format
) + 30);
1245 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1247 return symbol_decode
;
1250 /* Get the value of a symbol. */
1256 if (!s
->sy_resolved
&& !s
->sy_resolving
&& s
->sy_value
.X_op
!= O_constant
)
1257 resolve_symbol_value (s
);
1258 if (s
->sy_value
.X_op
!= O_constant
)
1260 static symbolS
*recur
;
1262 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1263 may call S_GET_VALUE. We use a static symbol to avoid the
1264 immediate recursion. */
1266 return (valueT
) s
->sy_value
.X_add_number
;
1268 if (! s
->sy_resolved
1269 || s
->sy_value
.X_op
!= O_symbol
1270 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1271 as_bad ("Attempt to get value of unresolved symbol %s",
1275 return (valueT
) s
->sy_value
.X_add_number
;
1278 /* Set the value of a symbol. */
1281 S_SET_VALUE (s
, val
)
1285 s
->sy_value
.X_op
= O_constant
;
1286 s
->sy_value
.X_add_number
= (offsetT
) val
;
1287 s
->sy_value
.X_unsigned
= 0;
1291 copy_symbol_attributes (dest
, src
)
1292 symbolS
*dest
, *src
;
1294 #ifdef BFD_ASSEMBLER
1295 /* In an expression, transfer the settings of these flags.
1296 The user can override later, of course. */
1297 #define COPIED_SYMFLAGS (BSF_FUNCTION)
1298 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1301 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1302 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1306 #ifdef BFD_ASSEMBLER
1312 flagword flags
= s
->bsym
->flags
;
1315 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1318 return (flags
& BSF_GLOBAL
) != 0;
1325 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
1332 return bfd_is_com_section (s
->bsym
->section
);
1339 return s
->bsym
->section
!= undefined_section
;
1346 if (s
->bsym
->flags
& BSF_DEBUGGING
)
1355 flagword flags
= s
->bsym
->flags
;
1359 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1362 if (bfd_get_section (s
->bsym
) == reg_section
)
1365 name
= S_GET_NAME (s
);
1366 return (name
!= NULL
1368 && (strchr (name
, '\001')
1369 || strchr (name
, '\002')
1370 || (! flag_keep_locals
1371 && (bfd_is_local_label (stdoutput
, s
->bsym
)
1374 && name
[1] == '?')))));
1381 return S_IS_EXTERNAL (s
);
1388 return S_GET_NAME (s
) == 0;
1395 return s
->bsym
->name
;
1402 return s
->bsym
->section
;
1406 S_SET_SEGMENT (s
, seg
)
1410 s
->bsym
->section
= seg
;
1417 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1419 /* Let .weak override .global. */
1422 s
->bsym
->flags
|= BSF_GLOBAL
;
1423 s
->bsym
->flags
&= ~(BSF_LOCAL
|BSF_WEAK
);
1427 S_CLEAR_EXTERNAL (s
)
1430 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1432 /* Let .weak override. */
1435 s
->bsym
->flags
|= BSF_LOCAL
;
1436 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_WEAK
);
1443 s
->bsym
->flags
|= BSF_WEAK
;
1444 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_LOCAL
);
1448 S_SET_NAME (s
, name
)
1452 s
->bsym
->name
= name
;
1454 #endif /* BFD_ASSEMBLER */
1459 symbol_lastP
= NULL
;
1460 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
1461 sy_hash
= hash_new ();
1463 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
1464 #ifdef BFD_ASSEMBLER
1465 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1466 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
1469 /* Can't initialise a union. Sigh. */
1470 S_SET_SEGMENT (&abs_symbol
, absolute_section
);
1472 abs_symbol
.sy_value
.X_op
= O_constant
;
1473 abs_symbol
.sy_frag
= &zero_address_frag
;
1475 if (LOCAL_LABELS_FB
)
1487 printf ("%*s", indent_level
* 4, "");
1492 void print_expr_1
PARAMS ((FILE *, expressionS
*));
1493 void print_symbol_value_1
PARAMS ((FILE *, symbolS
*));
1496 print_symbol_value_1 (file
, sym
)
1500 const char *name
= S_GET_NAME (sym
);
1501 if (!name
|| !name
[0])
1503 fprintf (file
, "sym %lx %s", (unsigned long) sym
, name
);
1504 if (sym
->sy_frag
!= &zero_address_frag
)
1505 fprintf (file
, " frag %lx", (long) sym
->sy_frag
);
1507 fprintf (file
, " written");
1508 if (sym
->sy_resolved
)
1509 fprintf (file
, " resolved");
1510 else if (sym
->sy_resolving
)
1511 fprintf (file
, " resolving");
1512 if (sym
->sy_used_in_reloc
)
1513 fprintf (file
, " used-in-reloc");
1515 fprintf (file
, " used");
1516 if (S_IS_LOCAL (sym
))
1517 fprintf (file
, " local");
1518 if (S_IS_EXTERN (sym
))
1519 fprintf (file
, " extern");
1520 if (S_IS_DEBUG (sym
))
1521 fprintf (file
, " debug");
1522 if (S_IS_DEFINED (sym
))
1523 fprintf (file
, " defined");
1524 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
1525 if (sym
->sy_resolved
)
1527 segT s
= S_GET_SEGMENT (sym
);
1529 if (s
!= undefined_section
1530 && s
!= expr_section
)
1531 fprintf (file
, " %lx", (long) S_GET_VALUE (sym
));
1533 else if (indent_level
< 8 && S_GET_SEGMENT (sym
) != undefined_section
)
1536 fprintf (file
, "\n%*s<", indent_level
* 4, "");
1537 print_expr_1 (file
, &sym
->sy_value
);
1538 fprintf (file
, ">");
1545 print_symbol_value (sym
)
1549 print_symbol_value_1 (stderr
, sym
);
1550 fprintf (stderr
, "\n");
1554 print_expr_1 (file
, exp
)
1558 fprintf (file
, "expr %lx ", (long) exp
);
1562 fprintf (file
, "illegal");
1565 fprintf (file
, "absent");
1568 fprintf (file
, "constant %lx", (long) exp
->X_add_number
);
1572 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
1573 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1574 fprintf (file
, ">");
1576 if (exp
->X_add_number
)
1577 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
1578 (long) exp
->X_add_number
);
1582 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
1585 fprintf (file
, "big");
1588 fprintf (file
, "uminus -<");
1590 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1591 fprintf (file
, ">");
1592 goto maybe_print_addnum
;
1594 fprintf (file
, "bit_not");
1597 fprintf (file
, "multiply");
1600 fprintf (file
, "divide");
1603 fprintf (file
, "modulus");
1606 fprintf (file
, "lshift");
1609 fprintf (file
, "rshift");
1611 case O_bit_inclusive_or
:
1612 fprintf (file
, "bit_ior");
1614 case O_bit_exclusive_or
:
1615 fprintf (file
, "bit_xor");
1618 fprintf (file
, "bit_and");
1621 fprintf (file
, "eq");
1624 fprintf (file
, "ne");
1627 fprintf (file
, "lt");
1630 fprintf (file
, "le");
1633 fprintf (file
, "ge");
1636 fprintf (file
, "gt");
1639 fprintf (file
, "logical_and");
1642 fprintf (file
, "logical_or");
1646 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
1647 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1648 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1649 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1650 fprintf (file
, ">");
1651 goto maybe_print_addnum
;
1654 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
1655 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1656 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1657 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1658 fprintf (file
, ">");
1659 goto maybe_print_addnum
;
1661 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
1671 print_expr_1 (stderr
, exp
);
1672 fprintf (stderr
, "\n");
1676 symbol_print_statistics (file
)
1679 hash_print_statistics (file
, "symbol table", sy_hash
);
1682 /* end of symbols.c */
This page took 0.06352 seconds and 5 git commands to generate.