Better error reporting on missing local labels.
[platform/upstream/binutils.git] / gas / symbols.c
1 /* symbols.c -symbol table-
2    Copyright (C) 1987, 1990, 1991 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 /* static const char rcsid[] = "$Id$"; */
21
22 #include "as.h"
23
24 #include "obstack.h"            /* For "symbols.h" */
25 #include "subsegs.h"
26
27 #ifndef WORKING_DOT_WORD
28 extern int new_broken_words;
29 #endif
30 #ifdef VMS
31 extern char const_flag;
32 #endif
33
34 static
35 struct hash_control *
36 sy_hash;                        /* symbol-name => struct symbol pointer */
37
38                                 /* Below are commented in "symbols.h". */
39 unsigned int local_bss_counter;
40 symbolS * symbol_rootP;
41 symbolS * symbol_lastP;
42 symbolS abs_symbol;
43
44 symbolS*                dot_text_symbol;
45 symbolS*                dot_data_symbol;
46 symbolS*                dot_bss_symbol;
47
48 struct obstack  notes;
49
50 /*
51  * Un*x idea of local labels. They are made by "n:" where n
52  * is any decimal digit. Refer to them with
53  *  "nb" for previous (backward) n:
54  *  or "nf" for next (forward) n:.
55  *
56  * Like Un*x AS, we have one set of local label counters for entire assembly,
57  * not one set per (sub)segment like in most assemblers. This implies that
58  * one can refer to a label in another segment, and indeed some crufty
59  * compilers have done just that.
60  *
61  * I document the symbol names here to save duplicating words elsewhere.
62  * The mth occurence of label n: is turned into the symbol "Ln^Am" where
63  * n is a digit and m is a decimal number. "L" makes it a label discarded
64  * unless debugging and "^A"('\1') ensures no ordinary symbol SHOULD get the
65  * same name as a local label symbol. The first "4:" is "L4^A1" - the m
66  * numbers begin at 1.
67  */
68
69 typedef short unsigned int
70 local_label_countT;
71
72 static local_label_countT
73 local_label_counter[10];
74
75 static                          /* Returned to caller, then copied. */
76   char symbol_name_build[12];   /* used for created names ("4f") */
77
78 #ifdef LOCAL_LABELS_DOLLAR
79 int local_label_defined[10];
80 #endif
81
82 \f
83 void
84 symbol_begin()
85 {
86   symbol_lastP = NULL;
87   symbol_rootP = NULL;          /* In case we have 0 symbols (!!) */
88   sy_hash = hash_new();
89   bzero ((char *)(& abs_symbol), sizeof(abs_symbol));
90   S_SET_SEGMENT(&abs_symbol, SEG_ABSOLUTE);     /* Can't initialise a union. Sigh. */
91   bzero ((char *)(local_label_counter), sizeof(local_label_counter) );
92   local_bss_counter = 0;
93 }
94 \f
95 /*
96  *                      local_label_name()
97  *
98  * Caller must copy returned name: we re-use the area for the next name.
99  */
100
101 char *                          /* Return local label name. */
102 local_label_name(n, augend)
103      register int n;    /* we just saw "n:", "nf" or "nb" : n a digit */
104      register int augend; /* 0 for nb, 1 for n:, nf */
105 {
106   register char *       p;
107   register char *       q;
108   char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
109
110   know( n >= 0 );
111   know( augend == 0 || augend == 1 );
112   p = symbol_name_build;
113   * p ++ = 'L';
114   * p ++ = n + '0';             /* Make into ASCII */
115   * p ++ = 1;                   /* ^A */
116   n = local_label_counter [ n ] + augend;
117                                 /* version number of this local label */
118   /*
119    * Next code just does sprintf( {}, "%d", n);
120    * It is more elegant to do the next part recursively, but a procedure
121    * call for each digit emitted is considered too costly.
122    */
123   q = symbol_name_temporary;
124   for (*q++=0; n; q++)          /* emits NOTHING if n starts as 0 */
125     {
126       know(n>0);                /* We expect n > 0 always */
127       *q = n % 10 + '0';
128       n /= 10;
129     }
130   while (( * p ++ = * -- q ) != '\0') ;;
131
132   /* The label, as a '\0' ended string, starts at symbol_name_build. */
133   return(symbol_name_build);
134 } /* local_label_name() */
135
136 /*
137  * decode name that may have been generated by local_label_name() above.  If
138  * the name wasn't generated by local_label_name(), then return it unaltered.
139  * This is used for error messages.
140  */
141
142 char *decode_local_label_name(s)
143 char *s;
144 {
145         char *symbol_decode;
146         int label_number;
147         int label_version;
148         char *message_format = "\"%d\" (instance number %s of a local label)";
149
150         if (s[0] != 'L'
151             || s[2] != 1) {
152                 return(s);
153         } /* not a local_label_name() generated name. */
154
155         label_number = s[1] - '0';
156
157         (void) sprintf(symbol_decode = obstack_alloc(&notes, strlen(s + 3) + strlen(message_format) + 10),
158                 message_format, label_number, s + 3);
159
160         return(symbol_decode);
161 } /* decode_local_label_name() */
162
163 void local_colon (n)
164 int n;  /* just saw "n:" */
165 {
166   local_label_counter [n] ++;
167 #ifdef LOCAL_LABELS_DOLLAR
168   local_label_defined[n]=1;
169 #endif
170   colon (local_label_name (n, 0));
171 }
172 \f
173 /*
174  *                      symbol_new()
175  *
176  * Return a pointer to a new symbol.
177  * Die if we can't make a new symbol.
178  * Fill in the symbol's values.
179  * Add symbol to end of symbol chain.
180  *
181  *
182  * Please always call this to create a new symbol.
183  *
184  * Changes since 1985: Symbol names may not contain '\0'. Sigh.
185  * 2nd argument is now a SEG rather than a TYPE.  The mapping between
186  * segments and types is mostly encapsulated herein (actually, we inherit it
187  * from macros in struc-symbol.h).
188  */
189
190 symbolS *symbol_new(name, segment, value, frag)
191 char *name;                     /* It is copied, the caller can destroy/modify */
192 segT segment;                   /* Segment identifier (SEG_<something>) */
193 long value;                     /* Symbol value */
194 fragS *frag;                    /* Associated fragment */
195 {
196         unsigned int name_length;
197         char *preserved_copy_of_name;
198         symbolS *symbolP;
199         
200         name_length = strlen(name) + 1; /* +1 for \0 */
201         obstack_grow(&notes, name, name_length);
202         preserved_copy_of_name = obstack_finish(&notes);
203         symbolP = (symbolS *)obstack_alloc(&notes, sizeof(symbolS));
204
205  /* symbol must be born in some fixed state.  This seems as good as any. */
206         memset(symbolP, 0, sizeof(symbolS));
207
208 #if STRIP_UNDERSCORE
209         S_SET_NAME(symbolP, (*preserved_copy_of_name == '_'
210                                             ? preserved_copy_of_name + 1
211                                             : preserved_copy_of_name));
212 #else /* STRIP_UNDERSCORE */
213         S_SET_NAME(symbolP, preserved_copy_of_name);
214 #endif /* STRIP_UNDERSCORE */
215
216         S_SET_SEGMENT(symbolP, segment);
217         S_SET_VALUE(symbolP, value);
218 /*      symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
219
220         symbolP->sy_frag = frag;
221         /* krm: uneeded if symbol is born zeroed.
222            symbolP->sy_forward = NULL; */ /* JF */
223         symbolP->sy_number = ~0;
224         symbolP->sy_name_offset = ~0;
225
226         /*
227          * Link to end of symbol chain.
228          */
229         symbol_append(symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
230
231         obj_symbol_new_hook(symbolP);
232         
233 #ifdef DEBUG
234         verify_symbol_chain(symbol_rootP, symbol_lastP);
235 #endif /* DEBUG */
236
237         return(symbolP);
238 } /* symbol_new() */
239
240 \f
241 /*
242  *                      colon()
243  *
244  * We have just seen "<name>:".
245  * Creates a struct symbol unless it already exists.
246  *
247  * Gripes if we are redefining a symbol incompatibly (and ignores it).
248  *
249  */
250 void colon(sym_name)            /* just seen "x:" - rattle symbols & frags */
251      register char *  sym_name; /* symbol name, as a cannonical string */
252                                 /* We copy this string: OK to alter later. */
253 {
254   register symbolS * symbolP; /* symbol we are working with */
255
256 #ifdef LOCAL_LABELS_DOLLAR
257   /* Sun local labels go out of scope whenever a non-local symbol is defined.  */
258
259   if(*sym_name !='L')
260     bzero((void *) local_label_defined, sizeof(local_label_defined));
261 #endif
262
263 #ifndef WORKING_DOT_WORD
264   if(new_broken_words) {
265     struct broken_word *a;
266     int possible_bytes;
267     fragS *frag_tmp;
268     char *frag_opcode;
269     extern md_short_jump_size;
270     extern md_long_jump_size;
271
272     possible_bytes=md_short_jump_size + new_broken_words * md_long_jump_size;
273     frag_tmp=frag_now;
274     frag_opcode=frag_var(rs_broken_word,
275                          possible_bytes,
276                          possible_bytes,
277                          (relax_substateT) 0,
278                          (symbolS *) broken_words,
279                          0L,
280                          NULL);
281
282     /* We want to store the pointer to where to insert the jump table in the
283        fr_opcode of the rs_broken_word frag.  This requires a little hackery */
284     while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
285       frag_tmp=frag_tmp->fr_next;
286     know(frag_tmp);
287     frag_tmp->fr_opcode=frag_opcode;
288     new_broken_words = 0;
289
290     for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
291       a->dispfrag=frag_tmp;
292   }
293 #endif
294   if ((symbolP = symbol_find(sym_name)) != 0) {
295 #ifdef  VMS
296           /*
297            *    If the new symbol is .comm AND it has a size of zero,
298            *    we ignore it (i.e. the old symbol overrides it)
299            */
300           if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg) == (N_UNDF | N_EXT)) &&
301               ((obstack_next_free(& frags) - frag_now->fr_literal) == 0))
302               return;
303           /*
304            *    If the old symbol is .comm and it has a size of zero,
305            *    we override it with the new symbol value.
306            */
307           if ((symbolP->sy_type == (N_UNDF | N_EXT))
308               && (S_GET_VALUE(symbolP) == 0)) {
309                   symbolP->sy_frag  = frag_now;
310                   symbolP->sy_other = const_flag;
311                   S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
312                   symbolP->sy_type |= SEGMENT_TO_SYMBOL_TYPE((int) now_seg); /* keep N_EXT bit */
313                   return;
314           }
315 #endif  /* VMS */
316           /*
317            *    Now check for undefined symbols
318            */
319           if (!S_IS_DEFINED(symbolP)) {
320                   if (S_GET_VALUE(symbolP) == 0) {
321                           symbolP->sy_frag  = frag_now;
322 #ifdef VMS
323                           symbolP->sy_other = const_flag;
324 #endif
325                           S_SET_VALUE(symbolP, obstack_next_free(&frags) - frag_now->fr_literal);
326                           S_SET_SEGMENT(symbolP, now_seg);
327 #ifdef N_UNDF
328                           know(N_UNDF == 0);
329 #endif /* if we have one, it better be zero. */
330                           
331                   } else {
332                           /*
333                            *    There are still several cases to check:
334                            *            A .comm/.lcomm symbol being redefined as
335                            *                    initialized data is OK
336                            *            A .comm/.lcomm symbol being redefined with
337                            *                    a larger size is also OK
338                            *
339                            * This only used to be allowed on VMS gas, but Sun cc
340                            * on the sparc also depends on it.
341                            */
342 /*                        char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
343                           
344                           if (((!S_IS_DEBUG(symbolP) && !S_IS_DEFINED(symbolP) && S_IS_EXTERNAL(symbolP))
345                                || (S_GET_SEGMENT(symbolP) == SEG_BSS))
346                               && ((now_seg == SEG_DATA)
347                                   || (now_seg == S_GET_SEGMENT(symbolP)))) {
348                                   /*
349                                    *    Select which of the 2 cases this is
350                                    */
351                                   if (now_seg != SEG_DATA) {
352                                           /*
353                                            *   New .comm for prev .comm symbol.
354                                            *    If the new size is larger we just
355                                            *    change its value.  If the new size
356                                            *    is smaller, we ignore this symbol
357                                            */
358                                           if (S_GET_VALUE(symbolP)
359                                               < ((unsigned) (obstack_next_free(& frags) - frag_now->fr_literal))) {
360                                                   S_SET_VALUE(symbolP, 
361                                                               obstack_next_free(& frags) -
362                                                               frag_now->fr_literal);
363                                           }
364                                   } else {
365                                           /*
366                                            *    It is a .comm/.lcomm being converted
367                                            *    to initialized data.
368                                            */
369                                           symbolP->sy_frag  = frag_now;
370 #ifdef VMS
371                                           symbolP->sy_other = const_flag;
372 #endif /* VMS */
373                                           S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
374                                           S_SET_SEGMENT(symbolP, now_seg); /* keep N_EXT bit */
375                                   }
376                           } else {
377 #ifdef OBJ_COFF
378                                   as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
379                                            sym_name,
380                                            segment_name(S_GET_SEGMENT(symbolP)),
381                                            S_GET_VALUE(symbolP));
382 #else /* OBJ_COFF */
383                                   as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
384                                            sym_name,
385                                            segment_name(S_GET_SEGMENT(symbolP)),
386                                            S_GET_OTHER(symbolP), S_GET_DESC(symbolP),
387                                            S_GET_VALUE(symbolP));
388 #endif /* OBJ_COFF */
389                           }
390                   } /* if the undefined symbol has no value */
391           } else {
392                   as_fatal("Symbol %s already defined.", sym_name);
393           } /* if this symbol is not yet defined */
394
395   } else {
396           symbolP = symbol_new(sym_name,
397                                 now_seg, 
398                                 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
399                                 frag_now);
400 #ifdef VMS
401           S_SET_OTHER(symbolP, const_flag);
402 #endif /* VMS */
403           
404           symbol_table_insert(symbolP);
405   } /* if we have seen this symbol before */
406
407   return;
408 } /* colon() */
409
410 \f
411 /*
412  *                      symbol_table_insert()
413  *
414  * Die if we can't insert the symbol.
415  *
416  */
417
418 void symbol_table_insert(symbolP)
419 symbolS *symbolP;
420 {
421         register char *error_string;
422         
423         know(symbolP);
424         know(S_GET_NAME(symbolP));
425         
426         if (*(error_string = hash_jam(sy_hash, S_GET_NAME(symbolP), (char *)symbolP))) {
427                 as_fatal("Inserting \"%s\" into symbol table failed: %s",
428                          S_GET_NAME(symbolP), error_string);
429         } /* on error */
430 } /* symbol_table_insert() */
431 \f
432 /*
433  *                      symbol_find_or_make()
434  *
435  * If a symbol name does not exist, create it as undefined, and insert
436  * it into the symbol table. Return a pointer to it.
437  */
438 symbolS *symbol_find_or_make(name)
439 char *name;
440 {
441         register symbolS *symbolP;
442         
443         symbolP = symbol_find(name);
444         
445         if (symbolP == NULL) {
446                 symbolP = symbol_make(name);
447
448                 symbol_table_insert(symbolP);
449         } /* if symbol wasn't found */
450
451         return(symbolP);
452 } /* symbol_find_or_make() */
453
454 symbolS *symbol_make(name)
455 char *name;
456 {
457         symbolS *symbolP;
458
459         /* Let the machine description default it, e.g. for register names. */
460         symbolP = md_undefined_symbol(name);
461         
462         if (!symbolP) {
463                 symbolP = symbol_new(name,
464                                      SEG_UNKNOWN,
465                                      0,
466                                      &zero_address_frag);
467         } /* if md didn't build us a symbol */
468
469         return(symbolP);
470 } /* symbol_make() */
471
472 /*
473  *                      symbol_find()
474  * 
475  * Implement symbol table lookup.
476  * In:  A symbol's name as a string: '\0' can't be part of a symbol name.
477  * Out: NULL if the name was not in the symbol table, else the address
478  *      of a struct symbol associated with that name.
479  */
480
481 symbolS *symbol_find(name)
482 char *name;
483 {
484 #ifndef STRIP_UNDERSCORE
485 #define STRIP_UNDERSCORE 0
486 #endif /* STRIP_UNDERSCORE */
487     return symbol_find_base(name, STRIP_UNDERSCORE);
488 }
489
490 symbolS *symbol_find_base(name, strip_underscore)
491 char *name;
492 int strip_underscore;
493 {
494     if(strip_underscore && *name == '_') name++;
495     return ( (symbolS *) hash_find( sy_hash, name ));
496 }
497
498 /*
499  * Once upon a time, symbols were kept in a singly linked list.  At
500  * least coff needs to be able to rearrange them from time to time, for
501  * which a doubly linked list is much more convenient.  Loic did these
502  * as macros which seemed dangerous to me so they're now functions.
503  * xoxorich.
504  */
505
506 /* Link symbol ADDME after symbol TARGET in the chain. */
507 void symbol_append(addme, target, rootPP, lastPP)
508 symbolS *addme;
509 symbolS *target;
510 symbolS **rootPP;
511 symbolS **lastPP;
512 {
513         if (target == NULL) {
514                 know(*rootPP == NULL);
515                 know(*lastPP == NULL);
516                 *rootPP = addme;
517                 *lastPP = addme;
518                 return;
519         } /* if the list is empty */
520
521         if (target->sy_next != NULL) {
522 #ifdef SYMBOLS_NEED_BACKPOINTERS
523                 target->sy_next->sy_previous = addme;
524 #endif /* SYMBOLS_NEED_BACKPOINTERS */
525         } else {
526                 know(*lastPP == target);
527                 *lastPP = addme;
528         } /* if we have a next */
529         
530         addme->sy_next = target->sy_next;
531         target->sy_next = addme;
532
533 #ifdef SYMBOLS_NEED_BACKPOINTERS
534         addme->sy_previous = target;
535 #endif /* SYMBOLS_NEED_BACKPOINTERS */
536
537 #ifdef DEBUG
538         verify_symbol_chain(*rootPP, *lastPP);
539 #endif /* DEBUG */
540
541         return;
542 } /* symbol_append() */
543
544 #ifdef SYMBOLS_NEED_BACKPOINTERS
545 /* Remove SYMBOLP from the list. */
546 void symbol_remove(symbolP, rootPP, lastPP)
547 symbolS *symbolP;
548 symbolS **rootPP;
549 symbolS **lastPP;
550 {
551         if (symbolP == *rootPP) {
552                 *rootPP = symbolP->sy_next;
553         } /* if it was the root */
554
555         if (symbolP == *lastPP) {
556                 *lastPP = symbolP->sy_previous;
557         } /* if it was the tail */
558
559         if (symbolP->sy_next != NULL) {
560                 symbolP->sy_next->sy_previous = symbolP->sy_previous;
561         } /* if not last */
562         
563         if (symbolP->sy_previous != NULL) {
564                 symbolP->sy_previous->sy_next = symbolP->sy_next;
565         } /* if not first */
566         
567 #ifdef DEBUG
568         verify_symbol_chain(*rootPP, *lastPP);
569 #endif /* DEBUG */
570
571         return;
572 } /* symbol_remove() */
573
574 /* Set the chain pointers of SYMBOL to null. */
575 void symbol_clear_list_pointers(symbolP)
576 symbolS *symbolP;
577 {
578     symbolP->sy_next = NULL;
579     symbolP->sy_previous = NULL;
580 } /* symbol_clear_list_pointers() */
581
582 /* Link symbol ADDME before symbol TARGET in the chain. */
583 void symbol_insert(addme, target, rootPP, lastPP)
584 symbolS *addme;
585 symbolS *target;
586 symbolS **rootPP;
587 symbolS **lastPP;
588 {
589     if (target->sy_previous != NULL) {
590             target->sy_previous->sy_next = addme;
591     } else {
592             know(*rootPP == target);
593             *rootPP = addme;
594     } /* if not first */
595
596     addme->sy_previous = target->sy_previous;
597     target->sy_previous = addme;
598     addme->sy_next = target;
599
600 #ifdef DEBUG
601     verify_symbol_chain(*rootPP, *lastPP);
602 #endif /* DEBUG */
603
604     return;
605 } /* symbol_insert() */
606 #endif /* SYMBOLS_NEED_BACKPOINTERS */
607
608 void verify_symbol_chain(rootP, lastP)
609 symbolS *rootP;
610 symbolS *lastP;
611 {
612         symbolS *symbolP = rootP;
613
614         if (symbolP == NULL) {
615                 return;
616         } /* empty chain */
617
618         for ( ; symbol_next(symbolP) != NULL; symbolP = symbol_next(symbolP)) {
619 #ifdef SYMBOLS_NEED_BACKPOINTERS
620                 /*$if (symbolP->sy_previous) {
621                         know(symbolP->sy_previous->sy_next == symbolP);
622                 } else {
623                         know(symbolP == rootP);
624                 }$*/ /* both directions */
625                 know(symbolP->sy_next->sy_previous == symbolP);
626 #else /* SYMBOLS_NEED_BACKPOINTERS */
627                 ;
628 #endif /* SYMBOLS_NEED_BACKPOINTERS */
629         } /* verify pointers */
630
631         know(lastP == symbolP);
632
633         return;
634 } /* verify_symbol_chain() */
635
636
637 /*
638  * Local Variables:
639  * comment-column: 0
640  * fill-column: 131
641  * End:
642  */
643
644 /* end: symbols.c */