b63e6eb986eb1934660fc3f3224bcc98cbb272e0
[binutils-gdb.git] / gas / symbols.c
1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
21
22 #include <ctype.h>
23
24 #include "as.h"
25
26 #include "obstack.h" /* For "symbols.h" */
27 #include "subsegs.h"
28
29 #ifndef WORKING_DOT_WORD
30 extern int new_broken_words;
31 #endif
32
33 /* symbol-name => struct symbol pointer */
34 static struct hash_control *sy_hash;
35
36 /* Below are commented in "symbols.h". */
37 symbolS *symbol_rootP;
38 symbolS *symbol_lastP;
39 symbolS abs_symbol;
40
41 symbolS *dot_text_symbol;
42 symbolS *dot_data_symbol;
43 symbolS *dot_bss_symbol;
44
45 struct obstack notes;
46
47 static void fb_label_init PARAMS ((void));
48
49 void
50 symbol_begin ()
51 {
52 symbol_lastP = NULL;
53 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
54 sy_hash = hash_new ();
55 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
56 #ifdef BFD_ASSEMBLER
57 abs_symbol.bsym = bfd_abs_section.symbol;
58 #else
59 /* Can't initialise a union. Sigh. */
60 S_SET_SEGMENT (&abs_symbol, absolute_section);
61 #endif
62 #ifdef LOCAL_LABELS_FB
63 fb_label_init ();
64 #endif /* LOCAL_LABELS_FB */
65 }
66
67 /*
68 * symbol_new()
69 *
70 * Return a pointer to a new symbol.
71 * Die if we can't make a new symbol.
72 * Fill in the symbol's values.
73 * Add symbol to end of symbol chain.
74 *
75 *
76 * Please always call this to create a new symbol.
77 *
78 * Changes since 1985: Symbol names may not contain '\0'. Sigh.
79 * 2nd argument is now a SEG rather than a TYPE. The mapping between
80 * segments and types is mostly encapsulated herein (actually, we inherit it
81 * from macros in struc-symbol.h).
82 */
83
84 symbolS *
85 symbol_new (name, segment, valu, frag)
86 CONST char *name; /* It is copied, the caller can destroy/modify */
87 segT segment; /* Segment identifier (SEG_<something>) */
88 valueT valu; /* Symbol value */
89 fragS *frag; /* Associated fragment */
90 {
91 unsigned int name_length;
92 char *preserved_copy_of_name;
93 symbolS *symbolP;
94
95 name_length = strlen (name) + 1; /* +1 for \0 */
96 obstack_grow (&notes, name, name_length);
97 preserved_copy_of_name = obstack_finish (&notes);
98 #ifdef STRIP_UNDERSCORE
99 if (preserved_copy_of_name[0] == '_')
100 preserved_copy_of_name++;
101 #endif
102 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
103
104 /* symbol must be born in some fixed state. This seems as good as any. */
105 memset (symbolP, 0, sizeof (symbolS));
106
107
108 #ifdef BFD_ASSEMBLER
109 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
110 assert (symbolP->bsym != 0);
111 symbolP->bsym->udata = (PTR) symbolP;
112 #endif
113 S_SET_NAME (symbolP, preserved_copy_of_name);
114
115 S_SET_SEGMENT (symbolP, segment);
116 S_SET_VALUE (symbolP, valu);
117 symbol_clear_list_pointers(symbolP);
118
119 symbolP->sy_frag = frag;
120 #ifndef BFD_ASSEMBLER
121 symbolP->sy_number = ~0;
122 symbolP->sy_name_offset = (unsigned int) ~0;
123 #endif
124
125 /*
126 * Link to end of symbol chain.
127 */
128 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
129
130 obj_symbol_new_hook (symbolP);
131
132 #ifdef DEBUG_SYMS
133 verify_symbol_chain(symbol_rootP, symbol_lastP);
134 #endif /* DEBUG_SYMS */
135
136 return symbolP;
137 }
138 \f
139
140 /*
141 * colon()
142 *
143 * We have just seen "<name>:".
144 * Creates a struct symbol unless it already exists.
145 *
146 * Gripes if we are redefining a symbol incompatibly (and ignores it).
147 *
148 */
149 void
150 colon (sym_name) /* just seen "x:" - rattle symbols & frags */
151 register char *sym_name; /* symbol name, as a cannonical string */
152 /* We copy this string: OK to alter later. */
153 {
154 register symbolS *symbolP; /* symbol we are working with */
155
156 #ifdef LOCAL_LABELS_DOLLAR
157 /* Sun local labels go out of scope whenever a non-local symbol is
158 defined. */
159
160 if (*sym_name != 'L')
161 dollar_label_clear ();
162 #endif /* LOCAL_LABELS_DOLLAR */
163
164 #ifndef WORKING_DOT_WORD
165 if (new_broken_words)
166 {
167 struct broken_word *a;
168 int possible_bytes;
169 fragS *frag_tmp;
170 char *frag_opcode;
171
172 extern const int md_short_jump_size;
173 extern const int md_long_jump_size;
174 possible_bytes = (md_short_jump_size
175 + new_broken_words * md_long_jump_size);
176
177 frag_tmp = frag_now;
178 frag_opcode = frag_var (rs_broken_word,
179 possible_bytes,
180 possible_bytes,
181 (relax_substateT) 0,
182 (symbolS *) broken_words,
183 0L,
184 NULL);
185
186 /* We want to store the pointer to where to insert the jump table in the
187 fr_opcode of the rs_broken_word frag. This requires a little
188 hackery. */
189 while (frag_tmp
190 && (frag_tmp->fr_type != rs_broken_word
191 || frag_tmp->fr_opcode))
192 frag_tmp = frag_tmp->fr_next;
193 know (frag_tmp);
194 frag_tmp->fr_opcode = frag_opcode;
195 new_broken_words = 0;
196
197 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
198 a->dispfrag = frag_tmp;
199 }
200 #endif /* WORKING_DOT_WORD */
201
202 if ((symbolP = symbol_find (sym_name)) != 0)
203 {
204 #ifdef RESOLVE_SYMBOL_REDEFINITION
205 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
206 return;
207 #endif
208 /*
209 * Now check for undefined symbols
210 */
211 if (!S_IS_DEFINED (symbolP))
212 {
213 if (S_GET_VALUE (symbolP) == 0)
214 {
215 symbolP->sy_frag = frag_now;
216 #ifdef OBJ_VMS
217 S_GET_OTHER(symbolP) = const_flag;
218 #endif
219 S_SET_VALUE (symbolP, (valueT) ((char*)obstack_next_free (&frags) - frag_now->fr_literal));
220 S_SET_SEGMENT (symbolP, now_seg);
221 #ifdef N_UNDF
222 know (N_UNDF == 0);
223 #endif /* if we have one, it better be zero. */
224
225 }
226 else
227 {
228 /*
229 * There are still several cases to check:
230 * A .comm/.lcomm symbol being redefined as
231 * initialized data is OK
232 * A .comm/.lcomm symbol being redefined with
233 * a larger size is also OK
234 *
235 * This only used to be allowed on VMS gas, but Sun cc
236 * on the sparc also depends on it.
237 */
238
239 if (((!S_IS_DEBUG (symbolP)
240 && !S_IS_DEFINED (symbolP)
241 && S_IS_EXTERNAL (symbolP))
242 || S_GET_SEGMENT (symbolP) == bss_section)
243 && (now_seg == data_section
244 || now_seg == S_GET_SEGMENT (symbolP)))
245 {
246 /*
247 * Select which of the 2 cases this is
248 */
249 if (now_seg != data_section)
250 {
251 /*
252 * New .comm for prev .comm symbol.
253 * If the new size is larger we just
254 * change its value. If the new size
255 * is smaller, we ignore this symbol
256 */
257 if (S_GET_VALUE (symbolP)
258 < ((unsigned) frag_now_fix ()))
259 {
260 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
261 }
262 }
263 else
264 {
265 /* It is a .comm/.lcomm being converted to initialized
266 data. */
267 symbolP->sy_frag = frag_now;
268 #ifdef OBJ_VMS
269 S_GET_OTHER(symbolP) = const_flag;
270 #endif /* OBJ_VMS */
271 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
272 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
273 }
274 }
275 else
276 {
277 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
278 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
279 sym_name,
280 segment_name (S_GET_SEGMENT (symbolP)),
281 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
282 (long) S_GET_VALUE (symbolP));
283 #else
284 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
285 sym_name,
286 segment_name (S_GET_SEGMENT (symbolP)),
287 (long) S_GET_VALUE (symbolP));
288 #endif
289 }
290 } /* if the undefined symbol has no value */
291 }
292 else
293 {
294 /* Don't blow up if the definition is the same */
295 if (!(frag_now == symbolP->sy_frag
296 && S_GET_VALUE (symbolP) == (char*)obstack_next_free (&frags) - frag_now->fr_literal
297 && S_GET_SEGMENT (symbolP) == now_seg))
298 as_fatal ("Symbol %s already defined.", sym_name);
299 } /* if this symbol is not yet defined */
300
301 }
302 else
303 {
304 symbolP = symbol_new (sym_name,
305 now_seg,
306 (valueT) ((char*)obstack_next_free (&frags) - frag_now->fr_literal),
307 frag_now);
308 #ifdef OBJ_VMS
309 S_SET_OTHER (symbolP, const_flag);
310 #endif /* OBJ_VMS */
311
312 symbol_table_insert (symbolP);
313 } /* if we have seen this symbol before */
314
315 #ifdef tc_frob_label
316 tc_frob_label (symbolP);
317 #endif
318 }
319 \f
320
321 /*
322 * symbol_table_insert()
323 *
324 * Die if we can't insert the symbol.
325 *
326 */
327
328 void
329 symbol_table_insert (symbolP)
330 symbolS *symbolP;
331 {
332 register const char *error_string;
333
334 know (symbolP);
335 know (S_GET_NAME (symbolP));
336
337 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
338 {
339 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
340 S_GET_NAME (symbolP), error_string);
341 } /* on error */
342 } /* symbol_table_insert() */
343 \f
344 /*
345 * symbol_find_or_make()
346 *
347 * If a symbol name does not exist, create it as undefined, and insert
348 * it into the symbol table. Return a pointer to it.
349 */
350 symbolS *
351 symbol_find_or_make (name)
352 char *name;
353 {
354 register symbolS *symbolP;
355
356 symbolP = symbol_find (name);
357
358 if (symbolP == NULL)
359 {
360 symbolP = symbol_make (name);
361
362 symbol_table_insert (symbolP);
363 } /* if symbol wasn't found */
364
365 return (symbolP);
366 } /* symbol_find_or_make() */
367
368 symbolS *
369 symbol_make (name)
370 CONST char *name;
371 {
372 symbolS *symbolP;
373
374 /* Let the machine description default it, e.g. for register names. */
375 symbolP = md_undefined_symbol ((char *) name);
376
377 if (!symbolP)
378 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
379
380 return (symbolP);
381 } /* symbol_make() */
382
383 /*
384 * symbol_find()
385 *
386 * Implement symbol table lookup.
387 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
388 * Out: NULL if the name was not in the symbol table, else the address
389 * of a struct symbol associated with that name.
390 */
391
392 symbolS *
393 symbol_find (name)
394 CONST char *name;
395 {
396 #ifdef STRIP_UNDERSCORE
397 return (symbol_find_base (name, 1));
398 #else /* STRIP_UNDERSCORE */
399 return (symbol_find_base (name, 0));
400 #endif /* STRIP_UNDERSCORE */
401 } /* symbol_find() */
402
403 symbolS *
404 symbol_find_base (name, strip_underscore)
405 CONST char *name;
406 int strip_underscore;
407 {
408 if (strip_underscore && *name == '_')
409 name++;
410 return ((symbolS *) hash_find (sy_hash, name));
411 }
412
413 /*
414 * Once upon a time, symbols were kept in a singly linked list. At
415 * least coff needs to be able to rearrange them from time to time, for
416 * which a doubly linked list is much more convenient. Loic did these
417 * as macros which seemed dangerous to me so they're now functions.
418 * xoxorich.
419 */
420
421 /* Link symbol ADDME after symbol TARGET in the chain. */
422 void
423 symbol_append (addme, target, rootPP, lastPP)
424 symbolS *addme;
425 symbolS *target;
426 symbolS **rootPP;
427 symbolS **lastPP;
428 {
429 if (target == NULL)
430 {
431 know (*rootPP == NULL);
432 know (*lastPP == NULL);
433 *rootPP = addme;
434 *lastPP = addme;
435 return;
436 } /* if the list is empty */
437
438 if (target->sy_next != NULL)
439 {
440 #ifdef SYMBOLS_NEED_BACKPOINTERS
441 target->sy_next->sy_previous = addme;
442 #endif /* SYMBOLS_NEED_BACKPOINTERS */
443 }
444 else
445 {
446 know (*lastPP == target);
447 *lastPP = addme;
448 } /* if we have a next */
449
450 addme->sy_next = target->sy_next;
451 target->sy_next = addme;
452
453 #ifdef SYMBOLS_NEED_BACKPOINTERS
454 addme->sy_previous = target;
455 #endif /* SYMBOLS_NEED_BACKPOINTERS */
456 }
457
458 #ifdef SYMBOLS_NEED_BACKPOINTERS
459 /* Remove SYMBOLP from the list. */
460 void
461 symbol_remove (symbolP, rootPP, lastPP)
462 symbolS *symbolP;
463 symbolS **rootPP;
464 symbolS **lastPP;
465 {
466 if (symbolP == *rootPP)
467 {
468 *rootPP = symbolP->sy_next;
469 } /* if it was the root */
470
471 if (symbolP == *lastPP)
472 {
473 *lastPP = symbolP->sy_previous;
474 } /* if it was the tail */
475
476 if (symbolP->sy_next != NULL)
477 {
478 symbolP->sy_next->sy_previous = symbolP->sy_previous;
479 } /* if not last */
480
481 if (symbolP->sy_previous != NULL)
482 {
483 symbolP->sy_previous->sy_next = symbolP->sy_next;
484 } /* if not first */
485
486 #ifdef DEBUG_SYMS
487 verify_symbol_chain (*rootPP, *lastPP);
488 #endif /* DEBUG_SYMS */
489 }
490
491 /* Set the chain pointers of SYMBOL to null. */
492 void
493 symbol_clear_list_pointers (symbolP)
494 symbolS *symbolP;
495 {
496 symbolP->sy_next = NULL;
497 symbolP->sy_previous = NULL;
498 }
499
500 /* Link symbol ADDME before symbol TARGET in the chain. */
501 void
502 symbol_insert (addme, target, rootPP, lastPP)
503 symbolS *addme;
504 symbolS *target;
505 symbolS **rootPP;
506 symbolS **lastPP;
507 {
508 if (target->sy_previous != NULL)
509 {
510 target->sy_previous->sy_next = addme;
511 }
512 else
513 {
514 know (*rootPP == target);
515 *rootPP = addme;
516 } /* if not first */
517
518 addme->sy_previous = target->sy_previous;
519 target->sy_previous = addme;
520 addme->sy_next = target;
521
522 #ifdef DEBUG_SYMS
523 verify_symbol_chain (*rootPP, *lastPP);
524 #endif /* DEBUG_SYMS */
525 }
526
527 #endif /* SYMBOLS_NEED_BACKPOINTERS */
528
529 void
530 verify_symbol_chain (rootP, lastP)
531 symbolS *rootP;
532 symbolS *lastP;
533 {
534 symbolS *symbolP = rootP;
535
536 if (symbolP == NULL)
537 return;
538
539 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
540 {
541 #ifdef SYMBOLS_NEED_BACKPOINTERS
542 know (symbolP->sy_next->sy_previous == symbolP);
543 #else
544 /* Walk the list anyways, to make sure pointers are still good. */
545 ;
546 #endif /* SYMBOLS_NEED_BACKPOINTERS */
547 }
548
549 assert (lastP == symbolP);
550 }
551
552 void
553 verify_symbol_chain_2 (sym)
554 symbolS *sym;
555 {
556 symbolS *p = sym, *n = sym;
557 #ifdef SYMBOLS_NEED_BACKPOINTERS
558 while (symbol_previous (p))
559 p = symbol_previous (p);
560 #endif
561 while (symbol_next (n))
562 n = symbol_next (n);
563 verify_symbol_chain (p, n);
564 }
565
566 /* Resolve the value of a symbol. This is called during the final
567 pass over the symbol table to resolve any symbols with complex
568 values. */
569
570 void
571 resolve_symbol_value (symp)
572 symbolS *symp;
573 {
574 int resolved;
575
576 if (symp->sy_resolved)
577 return;
578
579 resolved = 0;
580
581 if (symp->sy_resolving)
582 {
583 as_bad ("Symbol definition loop encountered at %s",
584 S_GET_NAME (symp));
585 S_SET_VALUE (symp, (valueT) 0);
586 resolved = 1;
587 }
588 else
589 {
590 offsetT left, right, val;
591 segT seg_left, seg_right;
592
593 symp->sy_resolving = 1;
594
595 switch (symp->sy_value.X_op)
596 {
597 case O_absent:
598 S_SET_VALUE (symp, 0);
599 /* Fall through. */
600 case O_constant:
601 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
602 if (S_GET_SEGMENT (symp) == expr_section)
603 S_SET_SEGMENT (symp, absolute_section);
604 resolved = 1;
605 break;
606
607 case O_symbol:
608 resolve_symbol_value (symp->sy_value.X_add_symbol);
609
610 #ifdef obj_frob_forward_symbol
611 /* Some object formats need to forward the segment. */
612 obj_frob_forward_symbol (symp);
613 #endif
614
615 S_SET_VALUE (symp,
616 (symp->sy_value.X_add_number
617 + symp->sy_frag->fr_address
618 + S_GET_VALUE (symp->sy_value.X_add_symbol)));
619 if (S_GET_SEGMENT (symp) == expr_section
620 || S_GET_SEGMENT (symp) == undefined_section)
621 S_SET_SEGMENT (symp,
622 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
623
624 resolved = symp->sy_value.X_add_symbol->sy_resolved;
625 break;
626
627 case O_uminus:
628 case O_bit_not:
629 resolve_symbol_value (symp->sy_value.X_add_symbol);
630 if (symp->sy_value.X_op == O_uminus)
631 val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
632 else
633 val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
634 S_SET_VALUE (symp,
635 (val
636 + symp->sy_value.X_add_number
637 + symp->sy_frag->fr_address));
638 if (S_GET_SEGMENT (symp) == expr_section
639 || S_GET_SEGMENT (symp) == undefined_section)
640 S_SET_SEGMENT (symp, absolute_section);
641 resolved = symp->sy_value.X_add_symbol->sy_resolved;
642 break;
643
644 case O_multiply:
645 case O_divide:
646 case O_modulus:
647 case O_left_shift:
648 case O_right_shift:
649 case O_bit_inclusive_or:
650 case O_bit_or_not:
651 case O_bit_exclusive_or:
652 case O_bit_and:
653 case O_add:
654 case O_subtract:
655 resolve_symbol_value (symp->sy_value.X_add_symbol);
656 resolve_symbol_value (symp->sy_value.X_op_symbol);
657 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
658 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
659 if (seg_left != seg_right
660 && seg_left != undefined_section
661 && seg_right != undefined_section)
662 as_bad ("%s is operation on symbols in different sections",
663 S_GET_NAME (symp));
664 if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
665 != absolute_section)
666 && symp->sy_value.X_op != O_subtract)
667 as_bad ("%s is illegal operation on non-absolute symbols",
668 S_GET_NAME (symp));
669 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
670 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
671 switch (symp->sy_value.X_op)
672 {
673 case O_multiply: val = left * right; break;
674 case O_divide: val = left / right; break;
675 case O_modulus: val = left % right; break;
676 case O_left_shift: val = left << right; break;
677 case O_right_shift: val = left >> right; break;
678 case O_bit_inclusive_or: val = left | right; break;
679 case O_bit_or_not: val = left |~ right; break;
680 case O_bit_exclusive_or: val = left ^ right; break;
681 case O_bit_and: val = left & right; break;
682 case O_add: val = left + right; break;
683 case O_subtract: val = left - right; break;
684 default: abort ();
685 }
686 S_SET_VALUE (symp,
687 (symp->sy_value.X_add_number
688 + symp->sy_frag->fr_address
689 + val));
690 if (S_GET_SEGMENT (symp) == expr_section
691 || S_GET_SEGMENT (symp) == undefined_section)
692 S_SET_SEGMENT (symp, absolute_section);
693 resolved = (symp->sy_value.X_add_symbol->sy_resolved
694 && symp->sy_value.X_op_symbol->sy_resolved);
695 break;
696
697 case O_register:
698 case O_big:
699 case O_illegal:
700 /* Give an error (below) if not in expr_section. We don't
701 want to worry about expr_section symbols, because they
702 are fictional (they are created as part of expression
703 resolution), and any problems may not actually mean
704 anything. */
705 break;
706 }
707 }
708
709 /* Don't worry if we can't resolve an expr_section symbol. */
710 if (resolved)
711 symp->sy_resolved = 1;
712 else if (S_GET_SEGMENT (symp) != expr_section)
713 {
714 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp));
715 symp->sy_resolved = 1;
716 }
717 }
718
719 #ifdef LOCAL_LABELS_DOLLAR
720
721 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
722 They are *really* local. That is, they go out of scope whenever we see a
723 label that isn't local. Also, like fb labels, there can be multiple
724 instances of a dollar label. Therefor, we name encode each instance with
725 the instance number, keep a list of defined symbols separate from the real
726 symbol table, and we treat these buggers as a sparse array. */
727
728 static long *dollar_labels;
729 static long *dollar_label_instances;
730 static char *dollar_label_defines;
731 static long dollar_label_count;
732 static unsigned long dollar_label_max;
733
734 int
735 dollar_label_defined (label)
736 long label;
737 {
738 long *i;
739
740 know ((dollar_labels != NULL) || (dollar_label_count == 0));
741
742 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
743 if (*i == label)
744 return dollar_label_defines[i - dollar_labels];
745
746 /* if we get here, label isn't defined */
747 return 0;
748 } /* dollar_label_defined() */
749
750 static int
751 dollar_label_instance (label)
752 long label;
753 {
754 long *i;
755
756 know ((dollar_labels != NULL) || (dollar_label_count == 0));
757
758 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
759 if (*i == label)
760 return (dollar_label_instances[i - dollar_labels]);
761
762 /* If we get here, we haven't seen the label before, therefore its instance
763 count is zero. */
764 return 0;
765 }
766
767 void
768 dollar_label_clear ()
769 {
770 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
771 }
772
773 #define DOLLAR_LABEL_BUMP_BY 10
774
775 void
776 define_dollar_label (label)
777 long label;
778 {
779 long *i;
780
781 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
782 if (*i == label)
783 {
784 ++dollar_label_instances[i - dollar_labels];
785 dollar_label_defines[i - dollar_labels] = 1;
786 return;
787 }
788
789 /* if we get to here, we don't have label listed yet. */
790
791 if (dollar_labels == NULL)
792 {
793 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
794 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
795 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
796 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
797 dollar_label_count = 0;
798 }
799 else if (dollar_label_count == dollar_label_max)
800 {
801 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
802 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
803 dollar_label_max * sizeof (long));
804 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
805 dollar_label_max * sizeof (long));
806 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
807 } /* if we needed to grow */
808
809 dollar_labels[dollar_label_count] = label;
810 dollar_label_instances[dollar_label_count] = 1;
811 dollar_label_defines[dollar_label_count] = 1;
812 ++dollar_label_count;
813 }
814
815 /*
816 * dollar_label_name()
817 *
818 * Caller must copy returned name: we re-use the area for the next name.
819 *
820 * The mth occurence of label n: is turned into the symbol "Ln^Am"
821 * where n is the label number and m is the instance number. "L" makes
822 * it a label discarded unless debugging and "^A"('\1') ensures no
823 * ordinary symbol SHOULD get the same name as a local label
824 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
825 *
826 * fb labels get the same treatment, except that ^B is used in place of ^A.
827 */
828
829 char * /* Return local label name. */
830 dollar_label_name (n, augend)
831 register long n; /* we just saw "n$:" : n a number */
832 register int augend; /* 0 for current instance, 1 for new instance */
833 {
834 long i;
835 /* Returned to caller, then copied. used for created names ("4f") */
836 static char symbol_name_build[24];
837 register char *p;
838 register char *q;
839 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
840
841 know (n >= 0);
842 know (augend == 0 || augend == 1);
843 p = symbol_name_build;
844 *p++ = 'L';
845
846 /* Next code just does sprintf( {}, "%d", n); */
847 /* label number */
848 q = symbol_name_temporary;
849 for (*q++ = 0, i = n; i; ++q)
850 {
851 *q = i % 10 + '0';
852 i /= 10;
853 }
854 while ((*p = *--q) != '\0')
855 ++p;
856
857 *p++ = 1; /* ^A */
858
859 /* instance number */
860 q = symbol_name_temporary;
861 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
862 {
863 *q = i % 10 + '0';
864 i /= 10;
865 }
866 while ((*p++ = *--q) != '\0');;
867
868 /* The label, as a '\0' ended string, starts at symbol_name_build. */
869 return symbol_name_build;
870 }
871
872 #endif /* LOCAL_LABELS_DOLLAR */
873
874 #ifdef LOCAL_LABELS_FB
875
876 /*
877 * Sombody else's idea of local labels. They are made by "n:" where n
878 * is any decimal digit. Refer to them with
879 * "nb" for previous (backward) n:
880 * or "nf" for next (forward) n:.
881 *
882 * We do a little better and let n be any number, not just a single digit, but
883 * since the other guy's assembler only does ten, we treat the first ten
884 * specially.
885 *
886 * Like someone else's assembler, we have one set of local label counters for
887 * entire assembly, not one set per (sub)segment like in most assemblers. This
888 * implies that one can refer to a label in another segment, and indeed some
889 * crufty compilers have done just that.
890 *
891 * Since there could be a LOT of these things, treat them as a sparse array.
892 */
893
894 #define FB_LABEL_SPECIAL (10)
895
896 static long fb_low_counter[FB_LABEL_SPECIAL];
897 static long *fb_labels;
898 static long *fb_label_instances;
899 static long fb_label_count = 0;
900 static long fb_label_max = 0;
901
902 /* this must be more than FB_LABEL_SPECIAL */
903 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
904
905 static void
906 fb_label_init ()
907 {
908 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
909 } /* fb_label_init() */
910
911 /* add one to the instance number of this fb label */
912 void
913 fb_label_instance_inc (label)
914 long label;
915 {
916 long *i;
917
918 if (label < FB_LABEL_SPECIAL)
919 {
920 ++fb_low_counter[label];
921 return;
922 }
923
924 if (fb_labels != NULL)
925 {
926 for (i = fb_labels + FB_LABEL_SPECIAL;
927 i < fb_labels + fb_label_count; ++i)
928 {
929 if (*i == label)
930 {
931 ++fb_label_instances[i - fb_labels];
932 return;
933 } /* if we find it */
934 } /* for each existing label */
935 }
936
937 /* if we get to here, we don't have label listed yet. */
938
939 if (fb_labels == NULL)
940 {
941 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
942 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
943 fb_label_max = FB_LABEL_BUMP_BY;
944 fb_label_count = FB_LABEL_SPECIAL;
945
946 }
947 else if (fb_label_count == fb_label_max)
948 {
949 fb_label_max += FB_LABEL_BUMP_BY;
950 fb_labels = (long *) xrealloc ((char *) fb_labels,
951 fb_label_max * sizeof (long));
952 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
953 fb_label_max * sizeof (long));
954 } /* if we needed to grow */
955
956 fb_labels[fb_label_count] = label;
957 fb_label_instances[fb_label_count] = 1;
958 ++fb_label_count;
959 }
960
961 static long
962 fb_label_instance (label)
963 long label;
964 {
965 long *i;
966
967 if (label < FB_LABEL_SPECIAL)
968 {
969 return (fb_low_counter[label]);
970 }
971
972 if (fb_labels != NULL)
973 {
974 for (i = fb_labels + FB_LABEL_SPECIAL;
975 i < fb_labels + fb_label_count; ++i)
976 {
977 if (*i == label)
978 {
979 return (fb_label_instances[i - fb_labels]);
980 } /* if we find it */
981 } /* for each existing label */
982 }
983
984 /* We didn't find the label, so this must be a reference to the
985 first instance. */
986 return 0;
987 }
988
989 /*
990 * fb_label_name()
991 *
992 * Caller must copy returned name: we re-use the area for the next name.
993 *
994 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
995 * where n is the label number and m is the instance number. "L" makes
996 * it a label discarded unless debugging and "^B"('\2') ensures no
997 * ordinary symbol SHOULD get the same name as a local label
998 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
999 *
1000 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1001
1002 char * /* Return local label name. */
1003 fb_label_name (n, augend)
1004 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1005 long augend; /* 0 for nb, 1 for n:, nf */
1006 {
1007 long i;
1008 /* Returned to caller, then copied. used for created names ("4f") */
1009 static char symbol_name_build[24];
1010 register char *p;
1011 register char *q;
1012 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1013
1014 know (n >= 0);
1015 know (augend == 0 || augend == 1);
1016 p = symbol_name_build;
1017 *p++ = 'L';
1018
1019 /* Next code just does sprintf( {}, "%d", n); */
1020 /* label number */
1021 q = symbol_name_temporary;
1022 for (*q++ = 0, i = n; i; ++q)
1023 {
1024 *q = i % 10 + '0';
1025 i /= 10;
1026 }
1027 while ((*p = *--q) != '\0')
1028 ++p;
1029
1030 *p++ = 2; /* ^B */
1031
1032 /* instance number */
1033 q = symbol_name_temporary;
1034 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1035 {
1036 *q = i % 10 + '0';
1037 i /= 10;
1038 }
1039 while ((*p++ = *--q) != '\0');;
1040
1041 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1042 return (symbol_name_build);
1043 } /* fb_label_name() */
1044
1045 #endif /* LOCAL_LABELS_FB */
1046
1047
1048 /*
1049 * decode name that may have been generated by foo_label_name() above. If
1050 * the name wasn't generated by foo_label_name(), then return it unaltered.
1051 * This is used for error messages.
1052 */
1053
1054 char *
1055 decode_local_label_name (s)
1056 char *s;
1057 {
1058 char *p;
1059 char *symbol_decode;
1060 int label_number;
1061 int instance_number;
1062 char *type;
1063 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1064
1065 if (s[0] != 'L')
1066 return (s);
1067
1068 for (label_number = 0, p = s + 1; isdigit (*p); ++p)
1069 {
1070 label_number = (10 * label_number) + *p - '0';
1071 }
1072
1073 if (*p == 1)
1074 {
1075 type = "dollar";
1076 }
1077 else if (*p == 2)
1078 {
1079 type = "fb";
1080 }
1081 else
1082 {
1083 return (s);
1084 }
1085
1086 for (instance_number = 0, p = s + 1; isdigit (*p); ++p)
1087 {
1088 instance_number = (10 * instance_number) + *p - '0';
1089 }
1090
1091 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1092 (void) sprintf (symbol_decode, message_format, label_number,
1093 instance_number, type);
1094
1095 return (symbol_decode);
1096 } /* decode_local_label_name() */
1097
1098 /* Get the value of a symbol. */
1099
1100 valueT
1101 S_GET_VALUE (s)
1102 symbolS *s;
1103 {
1104 if (s->sy_value.X_op != O_constant)
1105 as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s));
1106 return (valueT) s->sy_value.X_add_number;
1107 }
1108
1109 /* Set the value of a symbol. */
1110
1111 void
1112 S_SET_VALUE (s, val)
1113 symbolS *s;
1114 valueT val;
1115 {
1116 s->sy_value.X_op = O_constant;
1117 s->sy_value.X_add_number = (offsetT) val;
1118 s->sy_value.X_unsigned = 0;
1119 }
1120
1121 #ifdef BFD_ASSEMBLER
1122
1123 int
1124 S_IS_EXTERNAL (s)
1125 symbolS *s;
1126 {
1127 flagword flags = s->bsym->flags;
1128
1129 /* sanity check */
1130 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
1131 abort ();
1132
1133 return (flags & BSF_GLOBAL) != 0;
1134 }
1135
1136 int
1137 S_IS_COMMON (s)
1138 symbolS *s;
1139 {
1140 return bfd_is_com_section (s->bsym->section);
1141 }
1142
1143 int
1144 S_IS_DEFINED (s)
1145 symbolS *s;
1146 {
1147 return s->bsym->section != undefined_section;
1148 }
1149
1150 int
1151 S_IS_DEBUG (s)
1152 symbolS *s;
1153 {
1154 if (s->bsym->flags & BSF_DEBUGGING)
1155 return 1;
1156 return 0;
1157 }
1158
1159 int
1160 S_IS_LOCAL (s)
1161 symbolS *s;
1162 {
1163 flagword flags = s->bsym->flags;
1164
1165 /* sanity check */
1166 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
1167 abort ();
1168
1169 return (S_GET_NAME (s)
1170 && ! S_IS_DEBUG (s)
1171 && (strchr (S_GET_NAME (s), '\001')
1172 || strchr (S_GET_NAME (s), '\002')
1173 || (S_LOCAL_NAME (s)
1174 && !flagseen['L'])));
1175 }
1176
1177 int
1178 S_IS_EXTERN (s)
1179 symbolS *s;
1180 {
1181 return S_IS_EXTERNAL (s);
1182 }
1183
1184 int
1185 S_IS_STABD (s)
1186 symbolS *s;
1187 {
1188 return S_GET_NAME (s) == 0;
1189 }
1190
1191 CONST char *
1192 S_GET_NAME (s)
1193 symbolS *s;
1194 {
1195 return s->bsym->name;
1196 }
1197
1198 segT
1199 S_GET_SEGMENT (s)
1200 symbolS *s;
1201 {
1202 return s->bsym->section;
1203 }
1204
1205 void
1206 S_SET_SEGMENT (s, seg)
1207 symbolS *s;
1208 segT seg;
1209 {
1210 s->bsym->section = seg;
1211 }
1212
1213 void
1214 S_SET_EXTERNAL (s)
1215 symbolS *s;
1216 {
1217 s->bsym->flags |= BSF_GLOBAL;
1218 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1219 }
1220
1221 void
1222 S_CLEAR_EXTERNAL (s)
1223 symbolS *s;
1224 {
1225 s->bsym->flags |= BSF_LOCAL;
1226 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1227 }
1228
1229 void
1230 S_SET_WEAK (s)
1231 symbolS *s;
1232 {
1233 s->bsym->flags |= BSF_WEAK;
1234 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1235 }
1236
1237 void
1238 S_SET_NAME (s, name)
1239 symbolS *s;
1240 char *name;
1241 {
1242 s->bsym->name = name;
1243 }
1244 #endif /* BFD_ASSEMBLER */
1245
1246 /* end of symbols.c */