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));
56 static long dollar_label_instance
PARAMS ((long));
57 static long fb_label_instance
PARAMS ((long));
61 Return a pointer to a new symbol. Die if we can't make a new
62 symbol. Fill in the symbol's values. Add symbol to end of symbol
65 This function should be called in the general case of creating a
66 symbol. However, if the output file symbol table has already been
67 set, and you are certain that this symbol won't be wanted in the
68 output file, you can call symbol_create. */
71 symbol_new (name
, segment
, valu
, frag
)
77 symbolS
*symbolP
= symbol_create (name
, segment
, valu
, frag
);
80 * Link to end of symbol chain.
84 extern int symbol_table_frozen
;
85 if (symbol_table_frozen
)
89 symbol_append (symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
95 symbol_create (name
, segment
, valu
, frag
)
96 const char *name
; /* It is copied, the caller can destroy/modify */
97 segT segment
; /* Segment identifier (SEG_<something>) */
98 valueT valu
; /* Symbol value */
99 fragS
*frag
; /* Associated fragment */
101 unsigned int name_length
;
102 char *preserved_copy_of_name
;
105 name_length
= strlen (name
) + 1; /* +1 for \0 */
106 obstack_grow (¬es
, name
, name_length
);
107 preserved_copy_of_name
= obstack_finish (¬es
);
108 #ifdef STRIP_UNDERSCORE
109 if (preserved_copy_of_name
[0] == '_')
110 preserved_copy_of_name
++;
113 #ifdef tc_canonicalize_symbol_name
114 preserved_copy_of_name
=
115 tc_canonicalize_symbol_name (preserved_copy_of_name
);
118 if (! symbols_case_sensitive
)
122 for (s
= (unsigned char *) preserved_copy_of_name
; *s
!= '\0'; s
++)
127 symbolP
= (symbolS
*) obstack_alloc (¬es
, sizeof (symbolS
));
129 /* symbol must be born in some fixed state. This seems as good as any. */
130 memset (symbolP
, 0, sizeof (symbolS
));
133 symbolP
->bsym
= bfd_make_empty_symbol (stdoutput
);
134 if (symbolP
->bsym
== NULL
)
135 as_perror ("%s", "bfd_make_empty_symbol");
136 symbolP
->bsym
->udata
.p
= (PTR
) symbolP
;
138 S_SET_NAME (symbolP
, preserved_copy_of_name
);
140 S_SET_SEGMENT (symbolP
, segment
);
141 S_SET_VALUE (symbolP
, valu
);
142 symbol_clear_list_pointers (symbolP
);
144 symbolP
->sy_frag
= frag
;
145 #ifndef BFD_ASSEMBLER
146 symbolP
->sy_number
= ~0;
147 symbolP
->sy_name_offset
= (unsigned int) ~0;
150 obj_symbol_new_hook (symbolP
);
152 #ifdef tc_symbol_new_hook
153 tc_symbol_new_hook (symbolP
);
163 * We have just seen "<name>:".
164 * Creates a struct symbol unless it already exists.
166 * Gripes if we are redefining a symbol incompatibly (and ignores it).
170 colon (sym_name
) /* just seen "x:" - rattle symbols & frags */
171 const char *sym_name
; /* symbol name, as a cannonical string */
172 /* We copy this string: OK to alter later. */
174 register symbolS
*symbolP
; /* symbol we are working with */
176 /* Sun local labels go out of scope whenever a non-local symbol is
178 if (LOCAL_LABELS_DOLLAR
)
183 local
= bfd_is_local_label_name (stdoutput
, sym_name
);
185 local
= LOCAL_LABEL (sym_name
);
189 dollar_label_clear ();
192 #ifndef WORKING_DOT_WORD
193 if (new_broken_words
)
195 struct broken_word
*a
;
200 extern const int md_short_jump_size
;
201 extern const int md_long_jump_size
;
202 possible_bytes
= (md_short_jump_size
203 + new_broken_words
* md_long_jump_size
);
206 frag_opcode
= frag_var (rs_broken_word
,
210 (symbolS
*) broken_words
,
214 /* We want to store the pointer to where to insert the jump table in the
215 fr_opcode of the rs_broken_word frag. This requires a little
218 && (frag_tmp
->fr_type
!= rs_broken_word
219 || frag_tmp
->fr_opcode
))
220 frag_tmp
= frag_tmp
->fr_next
;
222 frag_tmp
->fr_opcode
= frag_opcode
;
223 new_broken_words
= 0;
225 for (a
= broken_words
; a
&& a
->dispfrag
== 0; a
= a
->next_broken_word
)
226 a
->dispfrag
= frag_tmp
;
228 #endif /* WORKING_DOT_WORD */
230 if ((symbolP
= symbol_find (sym_name
)) != 0)
232 #ifdef RESOLVE_SYMBOL_REDEFINITION
233 if (RESOLVE_SYMBOL_REDEFINITION (symbolP
))
237 * Now check for undefined symbols
239 if (!S_IS_DEFINED (symbolP
))
241 if (S_GET_VALUE (symbolP
) == 0)
243 symbolP
->sy_frag
= frag_now
;
245 S_SET_OTHER(symbolP
, const_flag
);
247 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
248 S_SET_SEGMENT (symbolP
, now_seg
);
251 #endif /* if we have one, it better be zero. */
257 * There are still several cases to check:
258 * A .comm/.lcomm symbol being redefined as
259 * initialized data is OK
260 * A .comm/.lcomm symbol being redefined with
261 * a larger size is also OK
263 * This only used to be allowed on VMS gas, but Sun cc
264 * on the sparc also depends on it.
267 if (((!S_IS_DEBUG (symbolP
)
268 && !S_IS_DEFINED (symbolP
)
269 && S_IS_EXTERNAL (symbolP
))
270 || S_GET_SEGMENT (symbolP
) == bss_section
)
271 && (now_seg
== data_section
272 || now_seg
== S_GET_SEGMENT (symbolP
)))
275 * Select which of the 2 cases this is
277 if (now_seg
!= data_section
)
280 * New .comm for prev .comm symbol.
281 * If the new size is larger we just
282 * change its value. If the new size
283 * is smaller, we ignore this symbol
285 if (S_GET_VALUE (symbolP
)
286 < ((unsigned) frag_now_fix ()))
288 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
293 /* It is a .comm/.lcomm being converted to initialized
295 symbolP
->sy_frag
= frag_now
;
297 S_SET_OTHER(symbolP
, const_flag
);
299 S_SET_VALUE (symbolP
, (valueT
) frag_now_fix ());
300 S_SET_SEGMENT (symbolP
, now_seg
); /* keep N_EXT bit */
305 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
306 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
308 segment_name (S_GET_SEGMENT (symbolP
)),
309 S_GET_OTHER (symbolP
), S_GET_DESC (symbolP
),
310 (long) S_GET_VALUE (symbolP
));
312 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
314 segment_name (S_GET_SEGMENT (symbolP
)),
315 (long) S_GET_VALUE (symbolP
));
318 } /* if the undefined symbol has no value */
322 /* Don't blow up if the definition is the same */
323 if (!(frag_now
== symbolP
->sy_frag
324 && S_GET_VALUE (symbolP
) == frag_now_fix ()
325 && S_GET_SEGMENT (symbolP
) == now_seg
))
326 as_fatal ("Symbol %s already defined.", sym_name
);
327 } /* if this symbol is not yet defined */
332 symbolP
= symbol_new (sym_name
, now_seg
, (valueT
) frag_now_fix (),
335 S_SET_OTHER (symbolP
, const_flag
);
338 symbol_table_insert (symbolP
);
339 } /* if we have seen this symbol before */
341 if (mri_common_symbol
!= NULL
)
343 /* This symbol is actually being defined within an MRI common
344 section. This requires special handling. */
345 symbolP
->sy_value
.X_op
= O_symbol
;
346 symbolP
->sy_value
.X_add_symbol
= mri_common_symbol
;
347 symbolP
->sy_value
.X_add_number
= S_GET_VALUE (mri_common_symbol
);
348 symbolP
->sy_frag
= &zero_address_frag
;
349 S_SET_SEGMENT (symbolP
, expr_section
);
350 symbolP
->sy_mri_common
= 1;
354 tc_frob_label (symbolP
);
362 * symbol_table_insert()
364 * Die if we can't insert the symbol.
369 symbol_table_insert (symbolP
)
372 register const char *error_string
;
375 know (S_GET_NAME (symbolP
));
377 if ((error_string
= hash_jam (sy_hash
, S_GET_NAME (symbolP
), (PTR
) symbolP
)))
379 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
380 S_GET_NAME (symbolP
), error_string
);
382 } /* symbol_table_insert() */
385 * symbol_find_or_make()
387 * If a symbol name does not exist, create it as undefined, and insert
388 * it into the symbol table. Return a pointer to it.
391 symbol_find_or_make (name
)
394 register symbolS
*symbolP
;
396 symbolP
= symbol_find (name
);
400 symbolP
= symbol_make (name
);
402 symbol_table_insert (symbolP
);
403 } /* if symbol wasn't found */
406 } /* symbol_find_or_make() */
414 /* Let the machine description default it, e.g. for register names. */
415 symbolP
= md_undefined_symbol ((char *) name
);
418 symbolP
= symbol_new (name
, undefined_section
, (valueT
) 0, &zero_address_frag
);
421 } /* symbol_make() */
426 * Implement symbol table lookup.
427 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
428 * Out: NULL if the name was not in the symbol table, else the address
429 * of a struct symbol associated with that name.
436 #ifdef STRIP_UNDERSCORE
437 return (symbol_find_base (name
, 1));
438 #else /* STRIP_UNDERSCORE */
439 return (symbol_find_base (name
, 0));
440 #endif /* STRIP_UNDERSCORE */
441 } /* symbol_find() */
444 symbol_find_base (name
, strip_underscore
)
446 int strip_underscore
;
448 if (strip_underscore
&& *name
== '_')
451 #ifdef tc_canonicalize_symbol_name
455 copy
= (char *) alloca (strlen (name
) + 1);
457 name
= tc_canonicalize_symbol_name (copy
);
461 if (! symbols_case_sensitive
)
465 copy
= (unsigned char *) alloca (strlen (name
) + 1);
466 name
= (const char *) copy
;
467 for (; *copy
!= '\0'; copy
++)
469 *copy
= toupper (*copy
);
472 return ((symbolS
*) hash_find (sy_hash
, name
));
476 * Once upon a time, symbols were kept in a singly linked list. At
477 * least coff needs to be able to rearrange them from time to time, for
478 * which a doubly linked list is much more convenient. Loic did these
479 * as macros which seemed dangerous to me so they're now functions.
483 /* Link symbol ADDME after symbol TARGET in the chain. */
485 symbol_append (addme
, target
, rootPP
, lastPP
)
493 know (*rootPP
== NULL
);
494 know (*lastPP
== NULL
);
495 addme
->sy_next
= NULL
;
496 #ifdef SYMBOLS_NEED_BACKPOINTERS
497 addme
->sy_previous
= NULL
;
502 } /* if the list is empty */
504 if (target
->sy_next
!= NULL
)
506 #ifdef SYMBOLS_NEED_BACKPOINTERS
507 target
->sy_next
->sy_previous
= addme
;
508 #endif /* SYMBOLS_NEED_BACKPOINTERS */
512 know (*lastPP
== target
);
514 } /* if we have a next */
516 addme
->sy_next
= target
->sy_next
;
517 target
->sy_next
= addme
;
519 #ifdef SYMBOLS_NEED_BACKPOINTERS
520 addme
->sy_previous
= target
;
521 #endif /* SYMBOLS_NEED_BACKPOINTERS */
523 debug_verify_symchain (symbol_rootP
, symbol_lastP
);
526 /* Set the chain pointers of SYMBOL to null. */
528 symbol_clear_list_pointers (symbolP
)
531 symbolP
->sy_next
= NULL
;
532 #ifdef SYMBOLS_NEED_BACKPOINTERS
533 symbolP
->sy_previous
= NULL
;
537 #ifdef SYMBOLS_NEED_BACKPOINTERS
538 /* Remove SYMBOLP from the list. */
540 symbol_remove (symbolP
, rootPP
, lastPP
)
545 if (symbolP
== *rootPP
)
547 *rootPP
= symbolP
->sy_next
;
548 } /* if it was the root */
550 if (symbolP
== *lastPP
)
552 *lastPP
= symbolP
->sy_previous
;
553 } /* if it was the tail */
555 if (symbolP
->sy_next
!= NULL
)
557 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
560 if (symbolP
->sy_previous
!= NULL
)
562 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
565 debug_verify_symchain (*rootPP
, *lastPP
);
568 /* Link symbol ADDME before symbol TARGET in the chain. */
570 symbol_insert (addme
, target
, rootPP
, lastPP
)
576 if (target
->sy_previous
!= NULL
)
578 target
->sy_previous
->sy_next
= addme
;
582 know (*rootPP
== target
);
586 addme
->sy_previous
= target
->sy_previous
;
587 target
->sy_previous
= addme
;
588 addme
->sy_next
= target
;
590 debug_verify_symchain (*rootPP
, *lastPP
);
593 #endif /* SYMBOLS_NEED_BACKPOINTERS */
596 verify_symbol_chain (rootP
, lastP
)
600 symbolS
*symbolP
= rootP
;
605 for (; symbol_next (symbolP
) != NULL
; symbolP
= symbol_next (symbolP
))
607 #ifdef SYMBOLS_NEED_BACKPOINTERS
608 assert (symbolP
->sy_next
->sy_previous
== symbolP
);
610 /* Walk the list anyways, to make sure pointers are still good. */
612 #endif /* SYMBOLS_NEED_BACKPOINTERS */
615 assert (lastP
== symbolP
);
619 verify_symbol_chain_2 (sym
)
622 symbolS
*p
= sym
, *n
= sym
;
623 #ifdef SYMBOLS_NEED_BACKPOINTERS
624 while (symbol_previous (p
))
625 p
= symbol_previous (p
);
627 while (symbol_next (n
))
629 verify_symbol_chain (p
, n
);
632 /* Resolve the value of a symbol. This is called during the final
633 pass over the symbol table to resolve any symbols with complex
637 resolve_symbol_value (symp
)
642 if (symp
->sy_resolved
)
647 if (symp
->sy_resolving
)
649 as_bad ("Symbol definition loop encountered at %s",
651 S_SET_VALUE (symp
, (valueT
) 0);
656 offsetT left
, right
, val
;
657 segT seg_left
, seg_right
;
659 symp
->sy_resolving
= 1;
661 /* Simplify addition or subtraction of a constant by folding the
662 constant into X_add_number. */
663 if (symp
->sy_value
.X_op
== O_add
664 || symp
->sy_value
.X_op
== O_subtract
)
666 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
667 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
668 if (S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
) == absolute_section
)
670 right
= S_GET_VALUE (symp
->sy_value
.X_op_symbol
);
671 if (symp
->sy_value
.X_op
== O_add
)
672 symp
->sy_value
.X_add_number
+= right
;
674 symp
->sy_value
.X_add_number
-= right
;
675 symp
->sy_value
.X_op
= O_symbol
;
676 symp
->sy_value
.X_op_symbol
= NULL
;
678 else if ((S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
)
680 && symp
->sy_value
.X_op
== O_add
)
682 left
= S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
683 symp
->sy_value
.X_add_symbol
= symp
->sy_value
.X_op_symbol
;
684 symp
->sy_value
.X_add_number
+= left
;
685 symp
->sy_value
.X_op
= O_symbol
;
686 symp
->sy_value
.X_op_symbol
= NULL
;
690 switch (symp
->sy_value
.X_op
)
693 S_SET_VALUE (symp
, 0);
696 S_SET_VALUE (symp
, S_GET_VALUE (symp
) + symp
->sy_frag
->fr_address
);
697 if (S_GET_SEGMENT (symp
) == expr_section
)
698 S_SET_SEGMENT (symp
, absolute_section
);
703 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
705 if (symp
->sy_mri_common
)
707 /* This is a symbol inside an MRI common section. The
708 relocation routines are going to handle it specially.
709 Don't change the value. */
710 S_SET_VALUE (symp
, symp
->sy_value
.X_add_number
);
711 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
715 if (symp
->sy_value
.X_add_number
== 0)
716 copy_symbol_attributes (symp
, symp
->sy_value
.X_add_symbol
);
718 /* If we have equated this symbol to an undefined symbol, we
719 keep X_op set to O_symbol, and we don't change
720 X_add_number. This permits the routine which writes out
721 relocation to detect this case, and convert the
722 relocation to be against the symbol to which this symbol
724 if (! S_IS_DEFINED (symp
->sy_value
.X_add_symbol
)
725 || S_IS_COMMON (symp
->sy_value
.X_add_symbol
))
727 symp
->sy_value
.X_op
= O_symbol
;
729 S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
));
734 (symp
->sy_value
.X_add_number
735 + symp
->sy_frag
->fr_address
736 + S_GET_VALUE (symp
->sy_value
.X_add_symbol
)));
737 if (S_GET_SEGMENT (symp
) == expr_section
738 || S_GET_SEGMENT (symp
) == undefined_section
)
740 S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
));
743 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
749 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
750 if (symp
->sy_value
.X_op
== O_uminus
)
751 val
= - S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
752 else if (symp
->sy_value
.X_op
== O_logical_not
)
753 val
= ! S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
755 val
= ~ S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
758 + symp
->sy_value
.X_add_number
759 + symp
->sy_frag
->fr_address
));
760 if (S_GET_SEGMENT (symp
) == expr_section
761 || S_GET_SEGMENT (symp
) == undefined_section
)
762 S_SET_SEGMENT (symp
, absolute_section
);
763 resolved
= symp
->sy_value
.X_add_symbol
->sy_resolved
;
771 case O_bit_inclusive_or
:
773 case O_bit_exclusive_or
:
785 resolve_symbol_value (symp
->sy_value
.X_add_symbol
);
786 resolve_symbol_value (symp
->sy_value
.X_op_symbol
);
787 seg_left
= S_GET_SEGMENT (symp
->sy_value
.X_add_symbol
);
788 seg_right
= S_GET_SEGMENT (symp
->sy_value
.X_op_symbol
);
789 left
= S_GET_VALUE (symp
->sy_value
.X_add_symbol
);
790 right
= S_GET_VALUE (symp
->sy_value
.X_op_symbol
);
792 /* Subtraction is permitted if both operands are in the same
793 section. Otherwise, both operands must be absolute. We
794 already handled the case of addition or subtraction of a
795 constant above. This will probably need to be changed
796 for an object file format which supports arbitrary
797 expressions, such as IEEE-695. */
798 if ((seg_left
!= absolute_section
799 || seg_right
!= absolute_section
)
800 && (symp
->sy_value
.X_op
!= O_subtract
801 || seg_left
!= seg_right
))
806 if (expr_symbol_where (symp
, &file
, &line
))
808 if (seg_left
== undefined_section
)
809 as_bad_where (file
, line
,
810 "undefined symbol %s in operation",
811 S_GET_NAME (symp
->sy_value
.X_add_symbol
));
812 if (seg_right
== undefined_section
)
813 as_bad_where (file
, line
,
814 "undefined symbol %s in operation",
815 S_GET_NAME (symp
->sy_value
.X_op_symbol
));
816 if (seg_left
!= undefined_section
817 && seg_right
!= undefined_section
)
818 as_bad_where (file
, line
, "invalid section for operation");
822 if (seg_left
== undefined_section
)
823 as_bad ("undefined symbol %s in operation setting %s",
824 S_GET_NAME (symp
->sy_value
.X_add_symbol
),
826 if (seg_right
== undefined_section
)
827 as_bad ("undefined symbol %s in operation setting %s",
828 S_GET_NAME (symp
->sy_value
.X_op_symbol
),
830 if (seg_left
!= undefined_section
831 && seg_right
!= undefined_section
)
832 as_bad ("invalid section for operation setting %s",
837 switch (symp
->sy_value
.X_op
)
839 case O_multiply
: val
= left
* right
; break;
840 case O_divide
: val
= left
/ right
; break;
841 case O_modulus
: val
= left
% right
; break;
842 case O_left_shift
: val
= left
<< right
; break;
843 case O_right_shift
: val
= left
>> right
; break;
844 case O_bit_inclusive_or
: val
= left
| right
; break;
845 case O_bit_or_not
: val
= left
|~ right
; break;
846 case O_bit_exclusive_or
: val
= left
^ right
; break;
847 case O_bit_and
: val
= left
& right
; break;
848 case O_add
: val
= left
+ right
; break;
849 case O_subtract
: val
= left
- right
; break;
850 case O_eq
: val
= left
== right
? ~ (offsetT
) 0 : 0;
851 case O_ne
: val
= left
!= right
? ~ (offsetT
) 0 : 0;
852 case O_lt
: val
= left
< right
? ~ (offsetT
) 0 : 0;
853 case O_le
: val
= left
<= right
? ~ (offsetT
) 0 : 0;
854 case O_ge
: val
= left
>= right
? ~ (offsetT
) 0 : 0;
855 case O_gt
: val
= left
> right
? ~ (offsetT
) 0 : 0;
856 case O_logical_and
: val
= left
&& right
; break;
857 case O_logical_or
: val
= left
|| right
; break;
861 (symp
->sy_value
.X_add_number
862 + symp
->sy_frag
->fr_address
864 if (S_GET_SEGMENT (symp
) == expr_section
865 || S_GET_SEGMENT (symp
) == undefined_section
)
866 S_SET_SEGMENT (symp
, absolute_section
);
867 resolved
= (symp
->sy_value
.X_add_symbol
->sy_resolved
868 && symp
->sy_value
.X_op_symbol
->sy_resolved
);
874 /* Give an error (below) if not in expr_section. We don't
875 want to worry about expr_section symbols, because they
876 are fictional (they are created as part of expression
877 resolution), and any problems may not actually mean
883 /* Don't worry if we can't resolve an expr_section symbol. */
885 symp
->sy_resolved
= 1;
886 else if (S_GET_SEGMENT (symp
) != expr_section
)
888 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp
));
889 symp
->sy_resolved
= 1;
893 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
894 They are *really* local. That is, they go out of scope whenever we see a
895 label that isn't local. Also, like fb labels, there can be multiple
896 instances of a dollar label. Therefor, we name encode each instance with
897 the instance number, keep a list of defined symbols separate from the real
898 symbol table, and we treat these buggers as a sparse array. */
900 static long *dollar_labels
;
901 static long *dollar_label_instances
;
902 static char *dollar_label_defines
;
903 static long dollar_label_count
;
904 static unsigned long dollar_label_max
;
907 dollar_label_defined (label
)
912 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
914 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
916 return dollar_label_defines
[i
- dollar_labels
];
918 /* if we get here, label isn't defined */
920 } /* dollar_label_defined() */
923 dollar_label_instance (label
)
928 know ((dollar_labels
!= NULL
) || (dollar_label_count
== 0));
930 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
932 return (dollar_label_instances
[i
- dollar_labels
]);
934 /* If we get here, we haven't seen the label before, therefore its instance
940 dollar_label_clear ()
942 memset (dollar_label_defines
, '\0', (unsigned int) dollar_label_count
);
945 #define DOLLAR_LABEL_BUMP_BY 10
948 define_dollar_label (label
)
953 for (i
= dollar_labels
; i
< dollar_labels
+ dollar_label_count
; ++i
)
956 ++dollar_label_instances
[i
- dollar_labels
];
957 dollar_label_defines
[i
- dollar_labels
] = 1;
961 /* if we get to here, we don't have label listed yet. */
963 if (dollar_labels
== NULL
)
965 dollar_labels
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
966 dollar_label_instances
= (long *) xmalloc (DOLLAR_LABEL_BUMP_BY
* sizeof (long));
967 dollar_label_defines
= xmalloc (DOLLAR_LABEL_BUMP_BY
);
968 dollar_label_max
= DOLLAR_LABEL_BUMP_BY
;
969 dollar_label_count
= 0;
971 else if (dollar_label_count
== dollar_label_max
)
973 dollar_label_max
+= DOLLAR_LABEL_BUMP_BY
;
974 dollar_labels
= (long *) xrealloc ((char *) dollar_labels
,
975 dollar_label_max
* sizeof (long));
976 dollar_label_instances
= (long *) xrealloc ((char *) dollar_label_instances
,
977 dollar_label_max
* sizeof (long));
978 dollar_label_defines
= xrealloc (dollar_label_defines
, dollar_label_max
);
979 } /* if we needed to grow */
981 dollar_labels
[dollar_label_count
] = label
;
982 dollar_label_instances
[dollar_label_count
] = 1;
983 dollar_label_defines
[dollar_label_count
] = 1;
984 ++dollar_label_count
;
988 * dollar_label_name()
990 * Caller must copy returned name: we re-use the area for the next name.
992 * The mth occurence of label n: is turned into the symbol "Ln^Am"
993 * where n is the label number and m is the instance number. "L" makes
994 * it a label discarded unless debugging and "^A"('\1') ensures no
995 * ordinary symbol SHOULD get the same name as a local label
996 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
998 * fb labels get the same treatment, except that ^B is used in place of ^A.
1001 char * /* Return local label name. */
1002 dollar_label_name (n
, augend
)
1003 register long n
; /* we just saw "n$:" : n a number */
1004 register int augend
; /* 0 for current instance, 1 for new instance */
1007 /* Returned to caller, then copied. used for created names ("4f") */
1008 static char symbol_name_build
[24];
1011 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
1014 know (augend
== 0 || augend
== 1);
1015 p
= symbol_name_build
;
1018 /* Next code just does sprintf( {}, "%d", n); */
1020 q
= symbol_name_temporary
;
1021 for (*q
++ = 0, i
= n
; i
; ++q
)
1026 while ((*p
= *--q
) != '\0')
1031 /* instance number */
1032 q
= symbol_name_temporary
;
1033 for (*q
++ = 0, i
= dollar_label_instance (n
) + augend
; i
; ++q
)
1038 while ((*p
++ = *--q
) != '\0');;
1040 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1041 return symbol_name_build
;
1045 * Sombody else's idea of local labels. They are made by "n:" where n
1046 * is any decimal digit. Refer to them with
1047 * "nb" for previous (backward) n:
1048 * or "nf" for next (forward) n:.
1050 * We do a little better and let n be any number, not just a single digit, but
1051 * since the other guy's assembler only does ten, we treat the first ten
1054 * Like someone else's assembler, we have one set of local label counters for
1055 * entire assembly, not one set per (sub)segment like in most assemblers. This
1056 * implies that one can refer to a label in another segment, and indeed some
1057 * crufty compilers have done just that.
1059 * Since there could be a LOT of these things, treat them as a sparse array.
1062 #define FB_LABEL_SPECIAL (10)
1064 static long fb_low_counter
[FB_LABEL_SPECIAL
];
1065 static long *fb_labels
;
1066 static long *fb_label_instances
;
1067 static long fb_label_count
;
1068 static long fb_label_max
;
1070 /* this must be more than FB_LABEL_SPECIAL */
1071 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1076 memset ((void *) fb_low_counter
, '\0', sizeof (fb_low_counter
));
1077 } /* fb_label_init() */
1079 /* add one to the instance number of this fb label */
1081 fb_label_instance_inc (label
)
1086 if (label
< FB_LABEL_SPECIAL
)
1088 ++fb_low_counter
[label
];
1092 if (fb_labels
!= NULL
)
1094 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1095 i
< fb_labels
+ fb_label_count
; ++i
)
1099 ++fb_label_instances
[i
- fb_labels
];
1101 } /* if we find it */
1102 } /* for each existing label */
1105 /* if we get to here, we don't have label listed yet. */
1107 if (fb_labels
== NULL
)
1109 fb_labels
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1110 fb_label_instances
= (long *) xmalloc (FB_LABEL_BUMP_BY
* sizeof (long));
1111 fb_label_max
= FB_LABEL_BUMP_BY
;
1112 fb_label_count
= FB_LABEL_SPECIAL
;
1115 else if (fb_label_count
== fb_label_max
)
1117 fb_label_max
+= FB_LABEL_BUMP_BY
;
1118 fb_labels
= (long *) xrealloc ((char *) fb_labels
,
1119 fb_label_max
* sizeof (long));
1120 fb_label_instances
= (long *) xrealloc ((char *) fb_label_instances
,
1121 fb_label_max
* sizeof (long));
1122 } /* if we needed to grow */
1124 fb_labels
[fb_label_count
] = label
;
1125 fb_label_instances
[fb_label_count
] = 1;
1130 fb_label_instance (label
)
1135 if (label
< FB_LABEL_SPECIAL
)
1137 return (fb_low_counter
[label
]);
1140 if (fb_labels
!= NULL
)
1142 for (i
= fb_labels
+ FB_LABEL_SPECIAL
;
1143 i
< fb_labels
+ fb_label_count
; ++i
)
1147 return (fb_label_instances
[i
- fb_labels
]);
1148 } /* if we find it */
1149 } /* for each existing label */
1152 /* We didn't find the label, so this must be a reference to the
1160 * Caller must copy returned name: we re-use the area for the next name.
1162 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1163 * where n is the label number and m is the instance number. "L" makes
1164 * it a label discarded unless debugging and "^B"('\2') ensures no
1165 * ordinary symbol SHOULD get the same name as a local label
1166 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1168 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1170 char * /* Return local label name. */
1171 fb_label_name (n
, augend
)
1172 long n
; /* we just saw "n:", "nf" or "nb" : n a number */
1173 long augend
; /* 0 for nb, 1 for n:, nf */
1176 /* Returned to caller, then copied. used for created names ("4f") */
1177 static char symbol_name_build
[24];
1180 char symbol_name_temporary
[20]; /* build up a number, BACKWARDS */
1183 know (augend
== 0 || augend
== 1);
1184 p
= symbol_name_build
;
1187 /* Next code just does sprintf( {}, "%d", n); */
1189 q
= symbol_name_temporary
;
1190 for (*q
++ = 0, i
= n
; i
; ++q
)
1195 while ((*p
= *--q
) != '\0')
1200 /* instance number */
1201 q
= symbol_name_temporary
;
1202 for (*q
++ = 0, i
= fb_label_instance (n
) + augend
; i
; ++q
)
1207 while ((*p
++ = *--q
) != '\0');;
1209 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1210 return (symbol_name_build
);
1211 } /* fb_label_name() */
1214 * decode name that may have been generated by foo_label_name() above. If
1215 * the name wasn't generated by foo_label_name(), then return it unaltered.
1216 * This is used for error messages.
1220 decode_local_label_name (s
)
1224 char *symbol_decode
;
1226 int instance_number
;
1228 const char *message_format
= "\"%d\" (instance number %d of a %s label)";
1233 for (label_number
= 0, p
= s
+ 1; isdigit (*p
); ++p
)
1234 label_number
= (10 * label_number
) + *p
- '0';
1243 for (instance_number
= 0, p
++; isdigit (*p
); ++p
)
1244 instance_number
= (10 * instance_number
) + *p
- '0';
1246 symbol_decode
= obstack_alloc (¬es
, strlen (message_format
) + 30);
1247 sprintf (symbol_decode
, message_format
, label_number
, instance_number
, type
);
1249 return symbol_decode
;
1252 /* Get the value of a symbol. */
1258 if (!s
->sy_resolved
&& !s
->sy_resolving
&& s
->sy_value
.X_op
!= O_constant
)
1259 resolve_symbol_value (s
);
1260 if (s
->sy_value
.X_op
!= O_constant
)
1262 static symbolS
*recur
;
1264 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1265 may call S_GET_VALUE. We use a static symbol to avoid the
1266 immediate recursion. */
1268 return (valueT
) s
->sy_value
.X_add_number
;
1270 if (! s
->sy_resolved
1271 || s
->sy_value
.X_op
!= O_symbol
1272 || (S_IS_DEFINED (s
) && ! S_IS_COMMON (s
)))
1273 as_bad ("Attempt to get value of unresolved symbol %s",
1277 return (valueT
) s
->sy_value
.X_add_number
;
1280 /* Set the value of a symbol. */
1283 S_SET_VALUE (s
, val
)
1287 s
->sy_value
.X_op
= O_constant
;
1288 s
->sy_value
.X_add_number
= (offsetT
) val
;
1289 s
->sy_value
.X_unsigned
= 0;
1293 copy_symbol_attributes (dest
, src
)
1294 symbolS
*dest
, *src
;
1296 #ifdef BFD_ASSEMBLER
1297 /* In an expression, transfer the settings of these flags.
1298 The user can override later, of course. */
1299 #define COPIED_SYMFLAGS (BSF_FUNCTION)
1300 dest
->bsym
->flags
|= src
->bsym
->flags
& COPIED_SYMFLAGS
;
1303 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1304 OBJ_COPY_SYMBOL_ATTRIBUTES (dest
, src
);
1308 #ifdef BFD_ASSEMBLER
1314 flagword flags
= s
->bsym
->flags
;
1317 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1320 return (flags
& BSF_GLOBAL
) != 0;
1327 return (s
->bsym
->flags
& BSF_WEAK
) != 0;
1334 return bfd_is_com_section (s
->bsym
->section
);
1341 return s
->bsym
->section
!= undefined_section
;
1348 if (s
->bsym
->flags
& BSF_DEBUGGING
)
1357 flagword flags
= s
->bsym
->flags
;
1361 if (flags
& BSF_LOCAL
&& flags
& BSF_GLOBAL
)
1364 if (bfd_get_section (s
->bsym
) == reg_section
)
1367 name
= S_GET_NAME (s
);
1368 return (name
!= NULL
1370 && (strchr (name
, '\001')
1371 || strchr (name
, '\002')
1372 || (! flag_keep_locals
1373 && (bfd_is_local_label (stdoutput
, s
->bsym
)
1376 && name
[1] == '?')))));
1383 return S_IS_EXTERNAL (s
);
1390 return S_GET_NAME (s
) == 0;
1397 return s
->bsym
->name
;
1404 return s
->bsym
->section
;
1408 S_SET_SEGMENT (s
, seg
)
1412 s
->bsym
->section
= seg
;
1419 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1421 /* Let .weak override .global. */
1424 s
->bsym
->flags
|= BSF_GLOBAL
;
1425 s
->bsym
->flags
&= ~(BSF_LOCAL
|BSF_WEAK
);
1429 S_CLEAR_EXTERNAL (s
)
1432 if ((s
->bsym
->flags
& BSF_WEAK
) != 0)
1434 /* Let .weak override. */
1437 s
->bsym
->flags
|= BSF_LOCAL
;
1438 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_WEAK
);
1445 s
->bsym
->flags
|= BSF_WEAK
;
1446 s
->bsym
->flags
&= ~(BSF_GLOBAL
|BSF_LOCAL
);
1450 S_SET_NAME (s
, name
)
1454 s
->bsym
->name
= name
;
1456 #endif /* BFD_ASSEMBLER */
1461 symbol_lastP
= NULL
;
1462 symbol_rootP
= NULL
; /* In case we have 0 symbols (!!) */
1463 sy_hash
= hash_new ();
1465 memset ((char *) (&abs_symbol
), '\0', sizeof (abs_symbol
));
1466 #ifdef BFD_ASSEMBLER
1467 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1468 abs_symbol
.bsym
= bfd_abs_section
.symbol
;
1471 /* Can't initialise a union. Sigh. */
1472 S_SET_SEGMENT (&abs_symbol
, absolute_section
);
1474 abs_symbol
.sy_value
.X_op
= O_constant
;
1475 abs_symbol
.sy_frag
= &zero_address_frag
;
1477 if (LOCAL_LABELS_FB
)
1489 printf ("%*s", indent_level
* 4, "");
1495 print_symbol_value_1 (file
, sym
)
1499 const char *name
= S_GET_NAME (sym
);
1500 if (!name
|| !name
[0])
1502 fprintf (file
, "sym %lx %s", (unsigned long) sym
, name
);
1503 if (sym
->sy_frag
!= &zero_address_frag
)
1504 fprintf (file
, " frag %lx", (long) sym
->sy_frag
);
1506 fprintf (file
, " written");
1507 if (sym
->sy_resolved
)
1508 fprintf (file
, " resolved");
1509 else if (sym
->sy_resolving
)
1510 fprintf (file
, " resolving");
1511 if (sym
->sy_used_in_reloc
)
1512 fprintf (file
, " used-in-reloc");
1514 fprintf (file
, " used");
1515 if (S_IS_LOCAL (sym
))
1516 fprintf (file
, " local");
1517 if (S_IS_EXTERN (sym
))
1518 fprintf (file
, " extern");
1519 if (S_IS_DEBUG (sym
))
1520 fprintf (file
, " debug");
1521 if (S_IS_DEFINED (sym
))
1522 fprintf (file
, " defined");
1523 fprintf (file
, " %s", segment_name (S_GET_SEGMENT (sym
)));
1524 if (sym
->sy_resolved
)
1526 segT s
= S_GET_SEGMENT (sym
);
1528 if (s
!= undefined_section
1529 && s
!= expr_section
)
1530 fprintf (file
, " %lx", (long) S_GET_VALUE (sym
));
1532 else if (indent_level
< 8 && S_GET_SEGMENT (sym
) != undefined_section
)
1535 fprintf (file
, "\n%*s<", indent_level
* 4, "");
1536 print_expr_1 (file
, &sym
->sy_value
);
1537 fprintf (file
, ">");
1544 print_symbol_value (sym
)
1548 print_symbol_value_1 (stderr
, sym
);
1549 fprintf (stderr
, "\n");
1553 print_expr_1 (file
, exp
)
1557 fprintf (file
, "expr %lx ", (long) exp
);
1561 fprintf (file
, "illegal");
1564 fprintf (file
, "absent");
1567 fprintf (file
, "constant %lx", (long) exp
->X_add_number
);
1571 fprintf (file
, "symbol\n%*s<", indent_level
* 4, "");
1572 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1573 fprintf (file
, ">");
1575 if (exp
->X_add_number
)
1576 fprintf (file
, "\n%*s%lx", indent_level
* 4, "",
1577 (long) exp
->X_add_number
);
1581 fprintf (file
, "register #%d", (int) exp
->X_add_number
);
1584 fprintf (file
, "big");
1587 fprintf (file
, "uminus -<");
1589 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1590 fprintf (file
, ">");
1591 goto maybe_print_addnum
;
1593 fprintf (file
, "bit_not");
1596 fprintf (file
, "multiply");
1599 fprintf (file
, "divide");
1602 fprintf (file
, "modulus");
1605 fprintf (file
, "lshift");
1608 fprintf (file
, "rshift");
1610 case O_bit_inclusive_or
:
1611 fprintf (file
, "bit_ior");
1613 case O_bit_exclusive_or
:
1614 fprintf (file
, "bit_xor");
1617 fprintf (file
, "bit_and");
1620 fprintf (file
, "eq");
1623 fprintf (file
, "ne");
1626 fprintf (file
, "lt");
1629 fprintf (file
, "le");
1632 fprintf (file
, "ge");
1635 fprintf (file
, "gt");
1638 fprintf (file
, "logical_and");
1641 fprintf (file
, "logical_or");
1645 fprintf (file
, "add\n%*s<", indent_level
* 4, "");
1646 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1647 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1648 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1649 fprintf (file
, ">");
1650 goto maybe_print_addnum
;
1653 fprintf (file
, "subtract\n%*s<", indent_level
* 4, "");
1654 print_symbol_value_1 (file
, exp
->X_add_symbol
);
1655 fprintf (file
, ">\n%*s<", indent_level
* 4, "");
1656 print_symbol_value_1 (file
, exp
->X_op_symbol
);
1657 fprintf (file
, ">");
1658 goto maybe_print_addnum
;
1660 fprintf (file
, "{unknown opcode %d}", (int) exp
->X_op
);
1670 print_expr_1 (stderr
, exp
);
1671 fprintf (stderr
, "\n");
1675 symbol_print_statistics (file
)
1678 hash_print_statistics (file
, "symbol table", sy_hash
);
1681 /* end of symbols.c */
This page took 0.075807 seconds and 5 git commands to generate.