19990502 sourceware import
[external/binutils.git] / gas / symbols.c
1 /* symbols.c -symbol table-
2    Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
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)
10    any later version.
11
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.
16
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
20    02111-1307, USA.  */
21
22 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
23
24 #include <ctype.h>
25
26 #include "as.h"
27
28 #include "obstack.h"            /* For "symbols.h" */
29 #include "subsegs.h"
30
31 /* This is non-zero if symbols are case sensitive, which is the
32    default.  */
33 int symbols_case_sensitive = 1;
34
35 #ifndef WORKING_DOT_WORD
36 extern int new_broken_words;
37 #endif
38
39 /* symbol-name => struct symbol pointer */
40 static struct hash_control *sy_hash;
41
42 /* Below are commented in "symbols.h". */
43 symbolS *symbol_rootP;
44 symbolS *symbol_lastP;
45 symbolS abs_symbol;
46
47 #ifdef DEBUG_SYMS
48 #define debug_verify_symchain verify_symbol_chain
49 #else
50 #define debug_verify_symchain(root, last) ((void) 0)
51 #endif
52
53 struct obstack notes;
54
55 static void fb_label_init PARAMS ((void));
56 static long dollar_label_instance PARAMS ((long));
57 static long fb_label_instance PARAMS ((long));
58
59 static void print_binary PARAMS ((FILE *, const char *, expressionS *));
60
61 /* symbol_new()
62   
63    Return a pointer to a new symbol.  Die if we can't make a new
64    symbol.  Fill in the symbol's values.  Add symbol to end of symbol
65    chain.
66  
67    This function should be called in the general case of creating a
68    symbol.  However, if the output file symbol table has already been
69    set, and you are certain that this symbol won't be wanted in the
70    output file, you can call symbol_create.  */
71
72 symbolS *
73 symbol_new (name, segment, valu, frag)
74      const char *name;
75      segT segment;
76      valueT valu;
77      fragS *frag;
78 {
79   symbolS *symbolP = symbol_create (name, segment, valu, frag);
80
81   /*
82    * Link to end of symbol chain.
83    */
84 #ifdef BFD_ASSEMBLER
85   {
86     extern int symbol_table_frozen;
87     if (symbol_table_frozen)
88       abort ();
89   }
90 #endif
91   symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
92
93   return symbolP;
94 }
95
96 symbolS *
97 symbol_create (name, segment, valu, frag)
98      const char *name;          /* It is copied, the caller can destroy/modify */
99      segT segment;              /* Segment identifier (SEG_<something>) */
100      valueT valu;               /* Symbol value */
101      fragS *frag;               /* Associated fragment */
102 {
103   unsigned int name_length;
104   char *preserved_copy_of_name;
105   symbolS *symbolP;
106
107   name_length = strlen (name) + 1;      /* +1 for \0 */
108   obstack_grow (&notes, name, name_length);
109   preserved_copy_of_name = obstack_finish (&notes);
110 #ifdef STRIP_UNDERSCORE
111   if (preserved_copy_of_name[0] == '_')
112     preserved_copy_of_name++;
113 #endif
114
115 #ifdef tc_canonicalize_symbol_name
116   preserved_copy_of_name =
117     tc_canonicalize_symbol_name (preserved_copy_of_name);
118 #endif
119
120   if (! symbols_case_sensitive)
121     {
122       unsigned char *s;
123
124       for (s = (unsigned char *) preserved_copy_of_name; *s != '\0'; s++)
125         if (islower (*s))
126           *s = toupper (*s);
127     }
128
129   symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
130
131   /* symbol must be born in some fixed state.  This seems as good as any. */
132   memset (symbolP, 0, sizeof (symbolS));
133
134 #ifdef BFD_ASSEMBLER
135   symbolP->bsym = bfd_make_empty_symbol (stdoutput);
136   if (symbolP->bsym == NULL)
137     as_perror ("%s", "bfd_make_empty_symbol");
138   symbolP->bsym->udata.p = (PTR) symbolP;
139 #endif
140   S_SET_NAME (symbolP, preserved_copy_of_name);
141
142   S_SET_SEGMENT (symbolP, segment);
143   S_SET_VALUE (symbolP, valu);
144   symbol_clear_list_pointers (symbolP);
145
146   symbolP->sy_frag = frag;
147 #ifndef BFD_ASSEMBLER
148   symbolP->sy_number = ~0;
149   symbolP->sy_name_offset = (unsigned int) ~0;
150 #endif
151
152   obj_symbol_new_hook (symbolP);
153
154 #ifdef tc_symbol_new_hook
155   tc_symbol_new_hook (symbolP);
156 #endif
157
158   return symbolP;
159 }
160 \f
161
162 /*
163  *                      colon()
164  *
165  * We have just seen "<name>:".
166  * Creates a struct symbol unless it already exists.
167  *
168  * Gripes if we are redefining a symbol incompatibly (and ignores it).
169  *
170  */
171 symbolS *
172 colon (sym_name)                /* just seen "x:" - rattle symbols & frags */
173      const char *sym_name;      /* symbol name, as a cannonical string */
174      /* We copy this string: OK to alter later. */
175 {
176   register symbolS *symbolP;    /* symbol we are working with */
177
178   /* Sun local labels go out of scope whenever a non-local symbol is
179      defined.  */
180   if (LOCAL_LABELS_DOLLAR)
181     {
182       int local;
183
184 #ifdef BFD_ASSEMBLER
185       local = bfd_is_local_label_name (stdoutput, sym_name);
186 #else
187       local = LOCAL_LABEL (sym_name);
188 #endif
189
190       if (! local)
191         dollar_label_clear ();
192     }
193
194 #ifndef WORKING_DOT_WORD
195   if (new_broken_words)
196     {
197       struct broken_word *a;
198       int possible_bytes;
199       fragS *frag_tmp;
200       char *frag_opcode;
201
202       extern const int md_short_jump_size;
203       extern const int md_long_jump_size;
204       possible_bytes = (md_short_jump_size
205                         + new_broken_words * md_long_jump_size);
206
207       frag_tmp = frag_now;
208       frag_opcode = frag_var (rs_broken_word,
209                               possible_bytes,
210                               possible_bytes,
211                               (relax_substateT) 0,
212                               (symbolS *) broken_words,
213                               (offsetT) 0,
214                               NULL);
215
216       /* We want to store the pointer to where to insert the jump table in the
217          fr_opcode of the rs_broken_word frag.  This requires a little
218          hackery.  */
219       while (frag_tmp
220              && (frag_tmp->fr_type != rs_broken_word
221                  || frag_tmp->fr_opcode))
222         frag_tmp = frag_tmp->fr_next;
223       know (frag_tmp);
224       frag_tmp->fr_opcode = frag_opcode;
225       new_broken_words = 0;
226
227       for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
228         a->dispfrag = frag_tmp;
229     }
230 #endif /* WORKING_DOT_WORD */
231
232   if ((symbolP = symbol_find (sym_name)) != 0)
233     {
234 #ifdef RESOLVE_SYMBOL_REDEFINITION
235       if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
236         return symbolP;
237 #endif
238       /*
239        *        Now check for undefined symbols
240        */
241       if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
242         {
243           if (S_GET_VALUE (symbolP) == 0)
244             {
245               symbolP->sy_frag = frag_now;
246 #ifdef OBJ_VMS
247               S_SET_OTHER(symbolP, const_flag);
248 #endif
249               S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
250               S_SET_SEGMENT (symbolP, now_seg);
251 #ifdef N_UNDF
252               know (N_UNDF == 0);
253 #endif /* if we have one, it better be zero. */
254
255             }
256           else
257             {
258               /*
259                *        There are still several cases to check:
260                *                A .comm/.lcomm symbol being redefined as
261                *                        initialized data is OK
262                *                A .comm/.lcomm symbol being redefined with
263                *                        a larger size is also OK
264                *
265                * This only used to be allowed on VMS gas, but Sun cc
266                * on the sparc also depends on it.
267                */
268
269               if (((!S_IS_DEBUG (symbolP)
270                     && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
271                     && S_IS_EXTERNAL (symbolP))
272                    || S_GET_SEGMENT (symbolP) == bss_section)
273                   && (now_seg == data_section
274                       || now_seg == S_GET_SEGMENT (symbolP)))
275                 {
276                   /*
277                    *    Select which of the 2 cases this is
278                    */
279                   if (now_seg != data_section)
280                     {
281                       /*
282                        *   New .comm for prev .comm symbol.
283                        *        If the new size is larger we just
284                        *        change its value.  If the new size
285                        *        is smaller, we ignore this symbol
286                        */
287                       if (S_GET_VALUE (symbolP)
288                           < ((unsigned) frag_now_fix ()))
289                         {
290                           S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
291                         }
292                     }
293                   else
294                     {
295                       /* It is a .comm/.lcomm being converted to initialized
296                          data.  */
297                       symbolP->sy_frag = frag_now;
298 #ifdef OBJ_VMS
299                       S_SET_OTHER(symbolP, const_flag);
300 #endif
301                       S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
302                       S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
303                     }
304                 }
305               else
306                 {
307 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
308                   as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld."),
309                             sym_name,
310                             segment_name (S_GET_SEGMENT (symbolP)),
311                             S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
312                             (long) S_GET_VALUE (symbolP));
313 #else
314                   as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%ld."),
315                             sym_name,
316                             segment_name (S_GET_SEGMENT (symbolP)),
317                             (long) S_GET_VALUE (symbolP));
318 #endif
319                 }
320             }                   /* if the undefined symbol has no value */
321         }
322       else
323         {
324           /* Don't blow up if the definition is the same */
325           if (!(frag_now == symbolP->sy_frag
326                 && S_GET_VALUE (symbolP) == frag_now_fix ()
327                 && S_GET_SEGMENT (symbolP) == now_seg))
328             as_fatal (_("Symbol %s already defined."), sym_name);
329         }                       /* if this symbol is not yet defined */
330
331     }
332   else
333     {
334       symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
335                             frag_now);
336 #ifdef OBJ_VMS
337       S_SET_OTHER (symbolP, const_flag);
338 #endif /* OBJ_VMS */
339
340       symbol_table_insert (symbolP);
341     }                           /* if we have seen this symbol before */
342
343   if (mri_common_symbol != NULL)
344     {
345       /* This symbol is actually being defined within an MRI common
346          section.  This requires special handling.  */
347       symbolP->sy_value.X_op = O_symbol;
348       symbolP->sy_value.X_add_symbol = mri_common_symbol;
349       symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
350       symbolP->sy_frag = &zero_address_frag;
351       S_SET_SEGMENT (symbolP, expr_section);
352       symbolP->sy_mri_common = 1;
353     }
354
355 #ifdef tc_frob_label
356   tc_frob_label (symbolP);
357 #endif
358 #ifdef obj_frob_label
359   obj_frob_label (symbolP);
360 #endif
361
362   return symbolP;
363 }
364 \f
365
366 /*
367  *                      symbol_table_insert()
368  *
369  * Die if we can't insert the symbol.
370  *
371  */
372
373 void 
374 symbol_table_insert (symbolP)
375      symbolS *symbolP;
376 {
377   register const char *error_string;
378
379   know (symbolP);
380   know (S_GET_NAME (symbolP));
381
382   if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
383     {
384       as_fatal (_("Inserting \"%s\" into symbol table failed: %s"),
385                 S_GET_NAME (symbolP), error_string);
386     }                           /* on error */
387 }                               /* symbol_table_insert() */
388 \f
389 /*
390  *                      symbol_find_or_make()
391  *
392  * If a symbol name does not exist, create it as undefined, and insert
393  * it into the symbol table. Return a pointer to it.
394  */
395 symbolS *
396 symbol_find_or_make (name)
397      const char *name;
398 {
399   register symbolS *symbolP;
400
401   symbolP = symbol_find (name);
402
403   if (symbolP == NULL)
404     {
405       symbolP = symbol_make (name);
406
407       symbol_table_insert (symbolP);
408     }                           /* if symbol wasn't found */
409
410   return (symbolP);
411 }                               /* symbol_find_or_make() */
412
413 symbolS *
414 symbol_make (name)
415      CONST char *name;
416 {
417   symbolS *symbolP;
418
419   /* Let the machine description default it, e.g. for register names. */
420   symbolP = md_undefined_symbol ((char *) name);
421
422   if (!symbolP)
423     symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
424
425   return (symbolP);
426 }                               /* symbol_make() */
427
428 /*
429  *                      symbol_find()
430  *
431  * Implement symbol table lookup.
432  * In:  A symbol's name as a string: '\0' can't be part of a symbol name.
433  * Out: NULL if the name was not in the symbol table, else the address
434  *      of a struct symbol associated with that name.
435  */
436
437 symbolS *
438 symbol_find (name)
439      CONST char *name;
440 {
441 #ifdef STRIP_UNDERSCORE
442   return (symbol_find_base (name, 1));
443 #else /* STRIP_UNDERSCORE */
444   return (symbol_find_base (name, 0));
445 #endif /* STRIP_UNDERSCORE */
446 }                               /* symbol_find() */
447
448 symbolS *
449 symbol_find_base (name, strip_underscore)
450      CONST char *name;
451      int strip_underscore;
452 {
453   if (strip_underscore && *name == '_')
454     name++;
455
456 #ifdef tc_canonicalize_symbol_name
457   {
458     char *copy;
459
460     copy = (char *) alloca (strlen (name) + 1);
461     strcpy (copy, name);
462     name = tc_canonicalize_symbol_name (copy);
463   }
464 #endif
465
466   if (! symbols_case_sensitive)
467     {
468       unsigned char *copy;
469
470       copy = (unsigned char *) alloca (strlen (name) + 1);
471       strcpy (copy, name);
472       name = (const char *) copy;
473       for (; *copy != '\0'; copy++)
474         if (islower (*copy))
475           *copy = toupper (*copy);
476     }
477
478   return ((symbolS *) hash_find (sy_hash, name));
479 }
480
481 /*
482  * Once upon a time, symbols were kept in a singly linked list.  At
483  * least coff needs to be able to rearrange them from time to time, for
484  * which a doubly linked list is much more convenient.  Loic did these
485  * as macros which seemed dangerous to me so they're now functions.
486  * xoxorich.
487  */
488
489 /* Link symbol ADDME after symbol TARGET in the chain. */
490 void 
491 symbol_append (addme, target, rootPP, lastPP)
492      symbolS *addme;
493      symbolS *target;
494      symbolS **rootPP;
495      symbolS **lastPP;
496 {
497   if (target == NULL)
498     {
499       know (*rootPP == NULL);
500       know (*lastPP == NULL);
501       addme->sy_next = NULL;
502 #ifdef SYMBOLS_NEED_BACKPOINTERS
503       addme->sy_previous = NULL;
504 #endif
505       *rootPP = addme;
506       *lastPP = addme;
507       return;
508     }                           /* if the list is empty */
509
510   if (target->sy_next != NULL)
511     {
512 #ifdef SYMBOLS_NEED_BACKPOINTERS
513       target->sy_next->sy_previous = addme;
514 #endif /* SYMBOLS_NEED_BACKPOINTERS */
515     }
516   else
517     {
518       know (*lastPP == target);
519       *lastPP = addme;
520     }                           /* if we have a next */
521
522   addme->sy_next = target->sy_next;
523   target->sy_next = addme;
524
525 #ifdef SYMBOLS_NEED_BACKPOINTERS
526   addme->sy_previous = target;
527 #endif /* SYMBOLS_NEED_BACKPOINTERS */
528
529   debug_verify_symchain (symbol_rootP, symbol_lastP);
530 }
531
532 /* Set the chain pointers of SYMBOL to null. */
533 void 
534 symbol_clear_list_pointers (symbolP)
535      symbolS *symbolP;
536 {
537   symbolP->sy_next = NULL;
538 #ifdef SYMBOLS_NEED_BACKPOINTERS
539   symbolP->sy_previous = NULL;
540 #endif
541 }
542
543 #ifdef SYMBOLS_NEED_BACKPOINTERS
544 /* Remove SYMBOLP from the list. */
545 void 
546 symbol_remove (symbolP, rootPP, lastPP)
547      symbolS *symbolP;
548      symbolS **rootPP;
549      symbolS **lastPP;
550 {
551   if (symbolP == *rootPP)
552     {
553       *rootPP = symbolP->sy_next;
554     }                           /* if it was the root */
555
556   if (symbolP == *lastPP)
557     {
558       *lastPP = symbolP->sy_previous;
559     }                           /* if it was the tail */
560
561   if (symbolP->sy_next != NULL)
562     {
563       symbolP->sy_next->sy_previous = symbolP->sy_previous;
564     }                           /* if not last */
565
566   if (symbolP->sy_previous != NULL)
567     {
568       symbolP->sy_previous->sy_next = symbolP->sy_next;
569     }                           /* if not first */
570
571   debug_verify_symchain (*rootPP, *lastPP);
572 }
573
574 /* Link symbol ADDME before symbol TARGET in the chain. */
575 void 
576 symbol_insert (addme, target, rootPP, lastPP)
577      symbolS *addme;
578      symbolS *target;
579      symbolS **rootPP;
580      symbolS **lastPP;
581 {
582   if (target->sy_previous != NULL)
583     {
584       target->sy_previous->sy_next = addme;
585     }
586   else
587     {
588       know (*rootPP == target);
589       *rootPP = addme;
590     }                           /* if not first */
591
592   addme->sy_previous = target->sy_previous;
593   target->sy_previous = addme;
594   addme->sy_next = target;
595
596   debug_verify_symchain (*rootPP, *lastPP);
597 }
598
599 #endif /* SYMBOLS_NEED_BACKPOINTERS */
600
601 void 
602 verify_symbol_chain (rootP, lastP)
603      symbolS *rootP;
604      symbolS *lastP;
605 {
606   symbolS *symbolP = rootP;
607
608   if (symbolP == NULL)
609     return;
610
611   for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
612     {
613 #ifdef SYMBOLS_NEED_BACKPOINTERS
614       assert (symbolP->sy_next->sy_previous == symbolP);
615 #else
616       /* Walk the list anyways, to make sure pointers are still good.  */
617       ;
618 #endif /* SYMBOLS_NEED_BACKPOINTERS */
619     }
620
621   assert (lastP == symbolP);
622 }
623
624 void
625 verify_symbol_chain_2 (sym)
626      symbolS *sym;
627 {
628   symbolS *p = sym, *n = sym;
629 #ifdef SYMBOLS_NEED_BACKPOINTERS
630   while (symbol_previous (p))
631     p = symbol_previous (p);
632 #endif
633   while (symbol_next (n))
634     n = symbol_next (n);
635   verify_symbol_chain (p, n);
636 }
637
638 /* Resolve the value of a symbol.  This is called during the final
639    pass over the symbol table to resolve any symbols with complex
640    values.  */
641
642 valueT
643 resolve_symbol_value (symp, finalize)
644      symbolS *symp;
645      int finalize;
646 {
647   int resolved;
648   valueT final_val;
649   segT final_seg;
650
651   if (symp->sy_resolved)
652     {
653       if (symp->sy_value.X_op == O_constant)
654         return (valueT) symp->sy_value.X_add_number;
655       else
656         return 0;
657     }
658
659   resolved = 0;
660   final_seg = S_GET_SEGMENT (symp);
661
662   if (symp->sy_resolving)
663     {
664       if (finalize)
665         as_bad (_("Symbol definition loop encountered at %s"), S_GET_NAME (symp));
666       final_val = 0;
667       resolved = 1;
668     }
669   else
670     {
671       symbolS *add_symbol, *op_symbol;
672       offsetT left, right;
673       segT seg_left, seg_right;
674       operatorT op;
675
676       symp->sy_resolving = 1;
677
678       /* Help out with CSE.  */
679       add_symbol = symp->sy_value.X_add_symbol;
680       op_symbol = symp->sy_value.X_op_symbol;
681       final_val = symp->sy_value.X_add_number;
682       op = symp->sy_value.X_op;
683
684       switch (op)
685         {
686         default:
687           BAD_CASE (op);
688           break;
689
690         case O_absent:
691           final_val = 0;
692           /* Fall through.  */
693
694         case O_constant:
695           final_val += symp->sy_frag->fr_address;
696           if (final_seg == expr_section)
697             final_seg = absolute_section;
698           resolved = 1;
699           break;
700
701         case O_symbol:
702         case O_symbol_rva:
703           left = resolve_symbol_value (add_symbol, finalize);
704         do_symbol:
705
706           if (symp->sy_mri_common)
707             {
708               /* This is a symbol inside an MRI common section.  The
709                  relocation routines are going to handle it specially.
710                  Don't change the value.  */
711               resolved = add_symbol->sy_resolved;
712               break;
713             }
714
715           if (finalize && final_val == 0)
716             copy_symbol_attributes (symp, add_symbol);
717
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
723              is equated.  */
724           if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
725             {
726               if (finalize)
727                 {
728                   S_SET_SEGMENT (symp, S_GET_SEGMENT (add_symbol));
729                   symp->sy_value.X_op = O_symbol;
730                   symp->sy_value.X_add_symbol = add_symbol;
731                   symp->sy_value.X_add_number = final_val;
732                 }
733               final_val = 0;
734               resolved = add_symbol->sy_resolved;
735               goto exit_dont_set_value;
736             }
737           else
738             {
739               final_val += symp->sy_frag->fr_address + left;
740               if (final_seg == expr_section || final_seg == undefined_section)
741                 final_seg = S_GET_SEGMENT (add_symbol);
742             }
743
744           resolved = add_symbol->sy_resolved;
745           break;
746
747         case O_uminus:
748         case O_bit_not:
749         case O_logical_not:
750           left = resolve_symbol_value (add_symbol, finalize);
751
752           if (op == O_uminus)
753             left = -left;
754           else if (op == O_logical_not)
755             left = !left;
756           else
757             left = ~left;
758
759           final_val += left + symp->sy_frag->fr_address;
760           if (final_seg == expr_section || final_seg == undefined_section)
761             final_seg = absolute_section;
762
763           resolved = add_symbol->sy_resolved;
764           break;
765
766         case O_multiply:
767         case O_divide:
768         case O_modulus:
769         case O_left_shift:
770         case O_right_shift:
771         case O_bit_inclusive_or:
772         case O_bit_or_not:
773         case O_bit_exclusive_or:
774         case O_bit_and:
775         case O_add:
776         case O_subtract:
777         case O_eq:
778         case O_ne:
779         case O_lt:
780         case O_le:
781         case O_ge:
782         case O_gt:
783         case O_logical_and:
784         case O_logical_or:
785           left = resolve_symbol_value (add_symbol, finalize);
786           right = resolve_symbol_value (op_symbol, finalize);
787           seg_left = S_GET_SEGMENT (add_symbol);
788           seg_right = S_GET_SEGMENT (op_symbol);
789
790           /* Simplify addition or subtraction of a constant by folding the
791              constant into X_add_number.  */
792           if (op == O_add || op == O_subtract)
793             {
794               if (seg_right == absolute_section)
795                 {
796                   if (op == O_add)
797                     final_val += right;
798                   else
799                     final_val -= right;
800                   op = O_symbol;
801                   op_symbol = NULL;
802                   goto do_symbol;
803                 }
804               else if (seg_left == absolute_section && op == O_add)
805                 {
806                   op = O_symbol;
807                   final_val += left;
808                   add_symbol = op_symbol;
809                   left = right;
810                   op_symbol = NULL;
811                   goto do_symbol;
812                 }
813             }
814
815           /* Subtraction is permitted if both operands are in the same
816              section.  Otherwise, both operands must be absolute.  We
817              already handled the case of addition or subtraction of a
818              constant above.  This will probably need to be changed
819              for an object file format which supports arbitrary
820              expressions, such as IEEE-695.  */
821           /* Don't emit messages unless we're finalizing the symbol value,
822              otherwise we may get the same message multiple times.  */
823           if ((seg_left != absolute_section || seg_right != absolute_section)
824               && (op != O_subtract || seg_left != seg_right)
825               && finalize)
826             {
827               char *file;
828               unsigned int line;
829
830               if (expr_symbol_where (symp, &file, &line))
831                 {
832                   if (seg_left == undefined_section)
833                     as_bad_where (file, line,
834                                   _("undefined symbol %s in operation"),
835                                   S_GET_NAME (symp->sy_value.X_add_symbol));
836                   if (seg_right == undefined_section)
837                     as_bad_where (file, line,
838                                   _("undefined symbol %s in operation"),
839                                   S_GET_NAME (symp->sy_value.X_op_symbol));
840                   if (seg_left != undefined_section
841                       && seg_right != undefined_section)
842                     as_bad_where (file, line, _("invalid section for operation"));
843                 }
844               else
845                 {
846                   if (seg_left == undefined_section)
847                     as_bad (_("undefined symbol %s in operation setting %s"),
848                             S_GET_NAME (symp->sy_value.X_add_symbol),
849                             S_GET_NAME (symp));
850                   if (seg_right == undefined_section)
851                     as_bad (_("undefined symbol %s in operation setting %s"),
852                             S_GET_NAME (symp->sy_value.X_op_symbol),
853                             S_GET_NAME (symp));
854                   if (seg_left != undefined_section
855                       && seg_right != undefined_section)
856                     as_bad (_("invalid section for operation setting %s"),
857                             S_GET_NAME (symp));
858                 }
859             }
860
861           /* Check for division by zero.  */
862           if ((op == O_divide || op == O_modulus) && right == 0)
863             {
864               /* If seg_right is not absolute_section, then we've
865                  already issued a warning about using a bad symbol.  */
866               if (seg_right == absolute_section && finalize)
867                 {
868                   char *file;
869                   unsigned int line;
870
871                   if (expr_symbol_where (symp, &file, &line))
872                     as_bad_where (file, line, _("division by zero"));
873                   else
874                     as_bad (_("division by zero when setting %s"),
875                             S_GET_NAME (symp));
876                 }
877
878               right = 1;
879             }
880
881           switch (symp->sy_value.X_op)
882             {
883             case O_multiply:            left *= right; break;
884             case O_divide:              left /= right; break;
885             case O_modulus:             left %= right; break;
886             case O_left_shift:          left <<= right; break;
887             case O_right_shift:         left >>= right; break;
888             case O_bit_inclusive_or:    left |= right; break;
889             case O_bit_or_not:          left |= ~right; break;
890             case O_bit_exclusive_or:    left ^= right; break;
891             case O_bit_and:             left &= right; break;
892             case O_add:                 left += right; break;
893             case O_subtract:            left -= right; break;
894             case O_eq:  left = left == right ? ~ (offsetT) 0 : 0; break;
895             case O_ne:  left = left != right ? ~ (offsetT) 0 : 0; break;
896             case O_lt:  left = left <  right ? ~ (offsetT) 0 : 0; break;
897             case O_le:  left = left <= right ? ~ (offsetT) 0 : 0; break;
898             case O_ge:  left = left >= right ? ~ (offsetT) 0 : 0; break;
899             case O_gt:  left = left >  right ? ~ (offsetT) 0 : 0; break;
900             case O_logical_and: left = left && right; break;
901             case O_logical_or:  left = left || right; break;
902             default:            abort ();
903             }
904
905           final_val += symp->sy_frag->fr_address + left;
906           if (final_seg == expr_section || final_seg == undefined_section)
907             final_seg = absolute_section;
908           resolved = (add_symbol->sy_resolved && op_symbol->sy_resolved);
909           break;
910
911         case O_register:
912         case O_big:
913         case O_illegal:
914           /* Give an error (below) if not in expr_section.  We don't
915              want to worry about expr_section symbols, because they
916              are fictional (they are created as part of expression
917              resolution), and any problems may not actually mean
918              anything.  */
919           break;
920         }
921
922       symp->sy_resolving = 0;
923     }
924
925   if (finalize)
926     {
927       S_SET_VALUE (symp, final_val);
928
929 #if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
930       /* The old a.out backend does not handle S_SET_SEGMENT correctly
931          for a stab symbol, so we use this bad hack.  */
932       if (final_seg != S_GET_SEGMENT (symp))
933 #endif
934         S_SET_SEGMENT (symp, final_seg);
935     }
936
937 exit_dont_set_value:
938   /* Don't worry if we can't resolve an expr_section symbol.  */
939   if (finalize)
940     {
941       if (resolved)
942         symp->sy_resolved = 1;
943       else if (S_GET_SEGMENT (symp) != expr_section)
944         {
945           as_bad (_("can't resolve value for symbol \"%s\""), S_GET_NAME (symp));
946           symp->sy_resolved = 1;
947         }
948     }
949
950   return final_val;
951 }
952
953 /* Dollar labels look like a number followed by a dollar sign.  Eg, "42$".
954    They are *really* local.  That is, they go out of scope whenever we see a
955    label that isn't local.  Also, like fb labels, there can be multiple
956    instances of a dollar label.  Therefor, we name encode each instance with
957    the instance number, keep a list of defined symbols separate from the real
958    symbol table, and we treat these buggers as a sparse array.  */
959
960 static long *dollar_labels;
961 static long *dollar_label_instances;
962 static char *dollar_label_defines;
963 static unsigned long dollar_label_count;
964 static unsigned long dollar_label_max;
965
966 int 
967 dollar_label_defined (label)
968      long label;
969 {
970   long *i;
971
972   know ((dollar_labels != NULL) || (dollar_label_count == 0));
973
974   for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
975     if (*i == label)
976       return dollar_label_defines[i - dollar_labels];
977
978   /* if we get here, label isn't defined */
979   return 0;
980 }                               /* dollar_label_defined() */
981
982 static long
983 dollar_label_instance (label)
984      long label;
985 {
986   long *i;
987
988   know ((dollar_labels != NULL) || (dollar_label_count == 0));
989
990   for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
991     if (*i == label)
992       return (dollar_label_instances[i - dollar_labels]);
993
994   /* If we get here, we haven't seen the label before, therefore its instance
995      count is zero.  */
996   return 0;
997 }
998
999 void 
1000 dollar_label_clear ()
1001 {
1002   memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1003 }
1004
1005 #define DOLLAR_LABEL_BUMP_BY 10
1006
1007 void 
1008 define_dollar_label (label)
1009      long label;
1010 {
1011   long *i;
1012
1013   for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1014     if (*i == label)
1015       {
1016         ++dollar_label_instances[i - dollar_labels];
1017         dollar_label_defines[i - dollar_labels] = 1;
1018         return;
1019       }
1020
1021   /* if we get to here, we don't have label listed yet. */
1022
1023   if (dollar_labels == NULL)
1024     {
1025       dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1026       dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1027       dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1028       dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1029       dollar_label_count = 0;
1030     }
1031   else if (dollar_label_count == dollar_label_max)
1032     {
1033       dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1034       dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1035                                          dollar_label_max * sizeof (long));
1036       dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1037                                           dollar_label_max * sizeof (long));
1038       dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1039     }                           /* if we needed to grow */
1040
1041   dollar_labels[dollar_label_count] = label;
1042   dollar_label_instances[dollar_label_count] = 1;
1043   dollar_label_defines[dollar_label_count] = 1;
1044   ++dollar_label_count;
1045 }
1046
1047 /*
1048  *                      dollar_label_name()
1049  *
1050  * Caller must copy returned name: we re-use the area for the next name.
1051  *
1052  * The mth occurence of label n: is turned into the symbol "Ln^Am"
1053  * where n is the label number and m is the instance number. "L" makes
1054  * it a label discarded unless debugging and "^A"('\1') ensures no
1055  * ordinary symbol SHOULD get the same name as a local label
1056  * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1057  *
1058  * fb labels get the same treatment, except that ^B is used in place of ^A.
1059  */
1060
1061 char *                          /* Return local label name. */
1062 dollar_label_name (n, augend)
1063      register long n;           /* we just saw "n$:" : n a number */
1064      register int augend;       /* 0 for current instance, 1 for new instance */
1065 {
1066   long i;
1067   /* Returned to caller, then copied.  used for created names ("4f") */
1068   static char symbol_name_build[24];
1069   register char *p;
1070   register char *q;
1071   char symbol_name_temporary[20];       /* build up a number, BACKWARDS */
1072
1073   know (n >= 0);
1074   know (augend == 0 || augend == 1);
1075   p = symbol_name_build;
1076   *p++ = 'L';
1077
1078   /* Next code just does sprintf( {}, "%d", n); */
1079   /* label number */
1080   q = symbol_name_temporary;
1081   for (*q++ = 0, i = n; i; ++q)
1082     {
1083       *q = i % 10 + '0';
1084       i /= 10;
1085     }
1086   while ((*p = *--q) != '\0')
1087     ++p;
1088
1089   *p++ = 1;                     /* ^A */
1090
1091   /* instance number */
1092   q = symbol_name_temporary;
1093   for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1094     {
1095       *q = i % 10 + '0';
1096       i /= 10;
1097     }
1098   while ((*p++ = *--q) != '\0');;
1099
1100   /* The label, as a '\0' ended string, starts at symbol_name_build. */
1101   return symbol_name_build;
1102 }
1103
1104 /*
1105  * Sombody else's idea of local labels. They are made by "n:" where n
1106  * is any decimal digit. Refer to them with
1107  *  "nb" for previous (backward) n:
1108  *  or "nf" for next (forward) n:.
1109  *
1110  * We do a little better and let n be any number, not just a single digit, but
1111  * since the other guy's assembler only does ten, we treat the first ten
1112  * specially.
1113  *
1114  * Like someone else's assembler, we have one set of local label counters for
1115  * entire assembly, not one set per (sub)segment like in most assemblers. This
1116  * implies that one can refer to a label in another segment, and indeed some
1117  * crufty compilers have done just that.
1118  *
1119  * Since there could be a LOT of these things, treat them as a sparse array.
1120  */
1121
1122 #define FB_LABEL_SPECIAL (10)
1123
1124 static long fb_low_counter[FB_LABEL_SPECIAL];
1125 static long *fb_labels;
1126 static long *fb_label_instances;
1127 static long fb_label_count;
1128 static long fb_label_max;
1129
1130 /* this must be more than FB_LABEL_SPECIAL */
1131 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1132
1133 static void 
1134 fb_label_init ()
1135 {
1136   memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1137 }                               /* fb_label_init() */
1138
1139 /* add one to the instance number of this fb label */
1140 void 
1141 fb_label_instance_inc (label)
1142      long label;
1143 {
1144   long *i;
1145
1146   if (label < FB_LABEL_SPECIAL)
1147     {
1148       ++fb_low_counter[label];
1149       return;
1150     }
1151
1152   if (fb_labels != NULL)
1153     {
1154       for (i = fb_labels + FB_LABEL_SPECIAL;
1155            i < fb_labels + fb_label_count; ++i)
1156         {
1157           if (*i == label)
1158             {
1159               ++fb_label_instances[i - fb_labels];
1160               return;
1161             }                   /* if we find it */
1162         }                       /* for each existing label */
1163     }
1164
1165   /* if we get to here, we don't have label listed yet. */
1166
1167   if (fb_labels == NULL)
1168     {
1169       fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1170       fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1171       fb_label_max = FB_LABEL_BUMP_BY;
1172       fb_label_count = FB_LABEL_SPECIAL;
1173
1174     }
1175   else if (fb_label_count == fb_label_max)
1176     {
1177       fb_label_max += FB_LABEL_BUMP_BY;
1178       fb_labels = (long *) xrealloc ((char *) fb_labels,
1179                                      fb_label_max * sizeof (long));
1180       fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1181                                               fb_label_max * sizeof (long));
1182     }                           /* if we needed to grow */
1183
1184   fb_labels[fb_label_count] = label;
1185   fb_label_instances[fb_label_count] = 1;
1186   ++fb_label_count;
1187 }
1188
1189 static long 
1190 fb_label_instance (label)
1191      long label;
1192 {
1193   long *i;
1194
1195   if (label < FB_LABEL_SPECIAL)
1196     {
1197       return (fb_low_counter[label]);
1198     }
1199
1200   if (fb_labels != NULL)
1201     {
1202       for (i = fb_labels + FB_LABEL_SPECIAL;
1203            i < fb_labels + fb_label_count; ++i)
1204         {
1205           if (*i == label)
1206             {
1207               return (fb_label_instances[i - fb_labels]);
1208             }                   /* if we find it */
1209         }                       /* for each existing label */
1210     }
1211
1212   /* We didn't find the label, so this must be a reference to the
1213      first instance.  */
1214   return 0;
1215 }
1216
1217 /*
1218  *                      fb_label_name()
1219  *
1220  * Caller must copy returned name: we re-use the area for the next name.
1221  *
1222  * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1223  * where n is the label number and m is the instance number. "L" makes
1224  * it a label discarded unless debugging and "^B"('\2') ensures no
1225  * ordinary symbol SHOULD get the same name as a local label
1226  * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1227  *
1228  * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1229
1230 char *                          /* Return local label name. */
1231 fb_label_name (n, augend)
1232      long n;                    /* we just saw "n:", "nf" or "nb" : n a number */
1233      long augend;               /* 0 for nb, 1 for n:, nf */
1234 {
1235   long i;
1236   /* Returned to caller, then copied.  used for created names ("4f") */
1237   static char symbol_name_build[24];
1238   register char *p;
1239   register char *q;
1240   char symbol_name_temporary[20];       /* build up a number, BACKWARDS */
1241
1242   know (n >= 0);
1243   know (augend == 0 || augend == 1);
1244   p = symbol_name_build;
1245   *p++ = 'L';
1246
1247   /* Next code just does sprintf( {}, "%d", n); */
1248   /* label number */
1249   q = symbol_name_temporary;
1250   for (*q++ = 0, i = n; i; ++q)
1251     {
1252       *q = i % 10 + '0';
1253       i /= 10;
1254     }
1255   while ((*p = *--q) != '\0')
1256     ++p;
1257
1258   *p++ = 2;                     /* ^B */
1259
1260   /* instance number */
1261   q = symbol_name_temporary;
1262   for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1263     {
1264       *q = i % 10 + '0';
1265       i /= 10;
1266     }
1267   while ((*p++ = *--q) != '\0');;
1268
1269   /* The label, as a '\0' ended string, starts at symbol_name_build. */
1270   return (symbol_name_build);
1271 }                               /* fb_label_name() */
1272
1273 /*
1274  * decode name that may have been generated by foo_label_name() above.  If
1275  * the name wasn't generated by foo_label_name(), then return it unaltered.
1276  * This is used for error messages.
1277  */
1278
1279 char *
1280 decode_local_label_name (s)
1281      char *s;
1282 {
1283   char *p;
1284   char *symbol_decode;
1285   int label_number;
1286   int instance_number;
1287   char *type;
1288   const char *message_format = _("\"%d\" (instance number %d of a %s label)");
1289
1290   if (s[0] != 'L')
1291     return s;
1292
1293   for (label_number = 0, p = s + 1; isdigit ((unsigned char) *p); ++p)
1294     label_number = (10 * label_number) + *p - '0';
1295
1296   if (*p == 1)
1297     type = "dollar";
1298   else if (*p == 2)
1299     type = "fb";
1300   else
1301     return s;
1302
1303   for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
1304     instance_number = (10 * instance_number) + *p - '0';
1305
1306   symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1307   sprintf (symbol_decode, message_format, label_number, instance_number, type);
1308
1309   return symbol_decode;
1310 }
1311
1312 /* Get the value of a symbol.  */
1313
1314 valueT
1315 S_GET_VALUE (s)
1316      symbolS *s;
1317 {
1318   if (!s->sy_resolved && s->sy_value.X_op != O_constant)
1319     resolve_symbol_value (s, 1);
1320   if (s->sy_value.X_op != O_constant)
1321     {
1322       static symbolS *recur;
1323
1324       /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1325          may call S_GET_VALUE.  We use a static symbol to avoid the
1326          immediate recursion.  */
1327       if (recur == s)
1328         return (valueT) s->sy_value.X_add_number;
1329       recur = s;
1330       if (! s->sy_resolved
1331           || s->sy_value.X_op != O_symbol
1332           || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1333         as_bad (_("Attempt to get value of unresolved symbol %s"),
1334                 S_GET_NAME (s));
1335       recur = NULL;
1336     }
1337   return (valueT) s->sy_value.X_add_number;
1338 }
1339
1340 /* Set the value of a symbol.  */
1341
1342 void
1343 S_SET_VALUE (s, val)
1344      symbolS *s;
1345      valueT val;
1346 {
1347   s->sy_value.X_op = O_constant;
1348   s->sy_value.X_add_number = (offsetT) val;
1349   s->sy_value.X_unsigned = 0;
1350 }
1351
1352 void
1353 copy_symbol_attributes (dest, src)
1354      symbolS *dest, *src;
1355 {
1356 #ifdef BFD_ASSEMBLER
1357   /* In an expression, transfer the settings of these flags.
1358      The user can override later, of course.  */
1359 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1360   dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1361 #endif
1362
1363 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1364   OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1365 #endif
1366 }
1367
1368 #ifdef BFD_ASSEMBLER
1369
1370 int
1371 S_IS_FUNCTION (s)
1372      symbolS *s;
1373 {
1374   flagword flags = s->bsym->flags;
1375
1376   return (flags & BSF_FUNCTION) != 0;
1377 }
1378
1379 int
1380 S_IS_EXTERNAL (s)
1381      symbolS *s;
1382 {
1383   flagword flags = s->bsym->flags;
1384
1385   /* sanity check */
1386   if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1387     abort ();
1388
1389   return (flags & BSF_GLOBAL) != 0;
1390 }
1391
1392 int
1393 S_IS_WEAK (s)
1394      symbolS *s;
1395 {
1396   return (s->bsym->flags & BSF_WEAK) != 0;
1397 }
1398
1399 int
1400 S_IS_COMMON (s)
1401      symbolS *s;
1402 {
1403   return bfd_is_com_section (s->bsym->section);
1404 }
1405
1406 int
1407 S_IS_DEFINED (s)
1408      symbolS *s;
1409 {
1410   return s->bsym->section != undefined_section;
1411 }
1412
1413 int
1414 S_IS_DEBUG (s)
1415      symbolS *s;
1416 {
1417   if (s->bsym->flags & BSF_DEBUGGING)
1418     return 1;
1419   return 0;
1420 }
1421
1422 int
1423 S_IS_LOCAL (s)
1424      symbolS *s;
1425 {
1426   flagword flags = s->bsym->flags;
1427   const char *name;
1428
1429   /* sanity check */
1430   if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1431     abort ();
1432
1433   if (bfd_get_section (s->bsym) == reg_section)
1434     return 1;
1435
1436   if (flag_strip_local_absolute
1437       && (flags & BSF_GLOBAL) == 0
1438       && bfd_get_section (s->bsym) == absolute_section)
1439     return 1;
1440
1441   name = S_GET_NAME (s);
1442   return (name != NULL
1443           && ! S_IS_DEBUG (s)
1444           && (strchr (name, '\001')
1445               || strchr (name, '\002')
1446               || (! flag_keep_locals
1447                   && (bfd_is_local_label (stdoutput, s->bsym)
1448                       || (flag_mri
1449                           && name[0] == '?'
1450                           && name[1] == '?')))));
1451 }
1452
1453 int
1454 S_IS_EXTERN (s)
1455      symbolS *s;
1456 {
1457   return S_IS_EXTERNAL (s);
1458 }
1459
1460 int
1461 S_IS_STABD (s)
1462      symbolS *s;
1463 {
1464   return S_GET_NAME (s) == 0;
1465 }
1466
1467 CONST char *
1468 S_GET_NAME (s)
1469      symbolS *s;
1470 {
1471   return s->bsym->name;
1472 }
1473
1474 segT
1475 S_GET_SEGMENT (s)
1476      symbolS *s;
1477 {
1478   return s->bsym->section;
1479 }
1480
1481 void
1482 S_SET_SEGMENT (s, seg)
1483      symbolS *s;
1484      segT seg;
1485 {
1486   /* Don't reassign section symbols.  The direct reason is to prevent seg
1487      faults assigning back to const global symbols such as *ABS*, but it
1488      shouldn't happen anyway.  */
1489
1490   if (s->bsym->flags & BSF_SECTION_SYM)
1491     {
1492       if (s->bsym->section != seg)
1493         abort();
1494     }
1495   else
1496     s->bsym->section = seg;
1497 }
1498
1499 void
1500 S_SET_EXTERNAL (s)
1501      symbolS *s;
1502 {
1503   if ((s->bsym->flags & BSF_WEAK) != 0)
1504     {
1505       /* Let .weak override .global.  */
1506       return;
1507     }
1508   s->bsym->flags |= BSF_GLOBAL;
1509   s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1510 }
1511
1512 void
1513 S_CLEAR_EXTERNAL (s)
1514      symbolS *s;
1515 {
1516   if ((s->bsym->flags & BSF_WEAK) != 0)
1517     {
1518       /* Let .weak override.  */
1519       return;
1520     }
1521   s->bsym->flags |= BSF_LOCAL;
1522   s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1523 }
1524
1525 void
1526 S_SET_WEAK (s)
1527      symbolS *s;
1528 {
1529   s->bsym->flags |= BSF_WEAK;
1530   s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1531 }
1532
1533 void
1534 S_SET_NAME (s, name)
1535      symbolS *s;
1536      char *name;
1537 {
1538   s->bsym->name = name;
1539 }
1540 #endif /* BFD_ASSEMBLER */
1541
1542 void
1543 symbol_begin ()
1544 {
1545   symbol_lastP = NULL;
1546   symbol_rootP = NULL;          /* In case we have 0 symbols (!!) */
1547   sy_hash = hash_new ();
1548
1549   memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1550 #ifdef BFD_ASSEMBLER
1551 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1552   abs_symbol.bsym = bfd_abs_section.symbol;
1553 #endif
1554 #else
1555   /* Can't initialise a union. Sigh. */
1556   S_SET_SEGMENT (&abs_symbol, absolute_section);
1557 #endif
1558   abs_symbol.sy_value.X_op = O_constant;
1559   abs_symbol.sy_frag = &zero_address_frag;
1560
1561   if (LOCAL_LABELS_FB)
1562     fb_label_init ();
1563 }
1564
1565 \f
1566 int indent_level;
1567
1568 /* Maximum indent level.
1569    Available for modification inside a gdb session.  */
1570 int max_indent_level = 8;
1571
1572 #if 0
1573
1574 static void
1575 indent ()
1576 {
1577   printf ("%*s", indent_level * 4, "");
1578 }
1579
1580 #endif
1581
1582 void
1583 print_symbol_value_1 (file, sym)
1584      FILE *file;
1585      symbolS *sym;
1586 {
1587   const char *name = S_GET_NAME (sym);
1588   if (!name || !name[0])
1589     name = "(unnamed)";
1590   fprintf (file, "sym %lx %s", (unsigned long) sym, name);
1591   if (sym->sy_frag != &zero_address_frag)
1592     fprintf (file, " frag %lx", (long) sym->sy_frag);
1593   if (sym->written)
1594     fprintf (file, " written");
1595   if (sym->sy_resolved)
1596     fprintf (file, " resolved");
1597   else if (sym->sy_resolving)
1598     fprintf (file, " resolving");
1599   if (sym->sy_used_in_reloc)
1600     fprintf (file, " used-in-reloc");
1601   if (sym->sy_used)
1602     fprintf (file, " used");
1603   if (S_IS_LOCAL (sym))
1604     fprintf (file, " local");
1605   if (S_IS_EXTERN (sym))
1606     fprintf (file, " extern");
1607   if (S_IS_DEBUG (sym))
1608     fprintf (file, " debug");
1609   if (S_IS_DEFINED (sym))
1610     fprintf (file, " defined");
1611   fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
1612   if (sym->sy_resolved)
1613     {
1614       segT s = S_GET_SEGMENT (sym);
1615
1616       if (s != undefined_section
1617           && s != expr_section)
1618         fprintf (file, " %lx", (long) S_GET_VALUE (sym));
1619     }
1620   else if (indent_level < max_indent_level
1621            && S_GET_SEGMENT (sym) != undefined_section)
1622     {
1623       indent_level++;
1624       fprintf (file, "\n%*s<", indent_level * 4, "");
1625       print_expr_1 (file, &sym->sy_value);
1626       fprintf (file, ">");
1627       indent_level--;
1628     }
1629   fflush (file);
1630 }
1631
1632 void
1633 print_symbol_value (sym)
1634      symbolS *sym;
1635 {
1636   indent_level = 0;
1637   print_symbol_value_1 (stderr, sym);
1638   fprintf (stderr, "\n");
1639 }
1640
1641 static void
1642 print_binary (file, name, exp)
1643      FILE *file;
1644      const char * name;
1645      expressionS *exp;
1646 {
1647   indent_level++;
1648   fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
1649   print_symbol_value_1 (file, exp->X_add_symbol);
1650   fprintf (file, ">\n%*s<", indent_level * 4, "");
1651   print_symbol_value_1 (file, exp->X_op_symbol);
1652   fprintf (file, ">");
1653   indent_level--;
1654 }
1655
1656 void
1657 print_expr_1 (file, exp)
1658      FILE *file;
1659      expressionS *exp;
1660 {
1661   fprintf (file, "expr %lx ", (long) exp);
1662   switch (exp->X_op)
1663     {
1664     case O_illegal:
1665       fprintf (file, "illegal");
1666       break;
1667     case O_absent:
1668       fprintf (file, "absent");
1669       break;
1670     case O_constant:
1671       fprintf (file, "constant %lx", (long) exp->X_add_number);
1672       break;
1673     case O_symbol:
1674       indent_level++;
1675       fprintf (file, "symbol\n%*s<", indent_level * 4, "");
1676       print_symbol_value_1 (file, exp->X_add_symbol);
1677       fprintf (file, ">");
1678     maybe_print_addnum:
1679       if (exp->X_add_number)
1680         fprintf (file, "\n%*s%lx", indent_level * 4, "",
1681                  (long) exp->X_add_number);
1682       indent_level--;
1683       break;
1684     case O_register:
1685       fprintf (file, "register #%d", (int) exp->X_add_number);
1686       break;
1687     case O_big:
1688       fprintf (file, "big");
1689       break;
1690     case O_uminus:
1691       fprintf (file, "uminus -<");
1692       indent_level++;
1693       print_symbol_value_1 (file, exp->X_add_symbol);
1694       fprintf (file, ">");
1695       goto maybe_print_addnum;
1696     case O_bit_not:
1697       fprintf (file, "bit_not");
1698       break;
1699     case O_multiply:
1700       print_binary (file, "multiply", exp);
1701       break;
1702     case O_divide:
1703       print_binary (file, "divide", exp);
1704       break;
1705     case O_modulus:
1706       print_binary (file, "modulus", exp);
1707       break;
1708     case O_left_shift:
1709       print_binary (file, "lshift", exp);
1710       break;
1711     case O_right_shift:
1712       print_binary (file, "rshift", exp);
1713       break;
1714     case O_bit_inclusive_or:
1715       print_binary (file, "bit_ior", exp);
1716       break;
1717     case O_bit_exclusive_or:
1718       print_binary (file, "bit_xor", exp);
1719       break;
1720     case O_bit_and:
1721       print_binary (file, "bit_and", exp);
1722       break;
1723     case O_eq:
1724       print_binary (file, "eq", exp);
1725       break;
1726     case O_ne:
1727       print_binary (file, "ne", exp);
1728       break;
1729     case O_lt:
1730       print_binary (file, "lt", exp);
1731       break;
1732     case O_le:
1733       print_binary (file, "le", exp);
1734       break;
1735     case O_ge:
1736       print_binary (file, "ge", exp);
1737       break;
1738     case O_gt:
1739       print_binary (file, "gt", exp);
1740       break;
1741     case O_logical_and:
1742       print_binary (file, "logical_and", exp);
1743       break;
1744     case O_logical_or:
1745       print_binary (file, "logical_or", exp);
1746       break;
1747     case O_add:
1748       indent_level++;
1749       fprintf (file, "add\n%*s<", indent_level * 4, "");
1750       print_symbol_value_1 (file, exp->X_add_symbol);
1751       fprintf (file, ">\n%*s<", indent_level * 4, "");
1752       print_symbol_value_1 (file, exp->X_op_symbol);
1753       fprintf (file, ">");
1754       goto maybe_print_addnum;
1755     case O_subtract:
1756       indent_level++;
1757       fprintf (file, "subtract\n%*s<", indent_level * 4, "");
1758       print_symbol_value_1 (file, exp->X_add_symbol);
1759       fprintf (file, ">\n%*s<", indent_level * 4, "");
1760       print_symbol_value_1 (file, exp->X_op_symbol);
1761       fprintf (file, ">");
1762       goto maybe_print_addnum;
1763     default:
1764       fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
1765       break;
1766     }
1767   fflush (stdout);
1768 }
1769
1770 void
1771 print_expr (exp)
1772      expressionS *exp;
1773 {
1774   print_expr_1 (stderr, exp);
1775   fprintf (stderr, "\n");
1776 }
1777
1778 void
1779 symbol_print_statistics (file)
1780      FILE *file;
1781 {
1782   hash_print_statistics (file, "symbol table", sy_hash);
1783 }
1784
1785 /* end of symbols.c */