hack around memset().
[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         extern int memset();
200         
201         name_length = strlen(name) + 1; /* +1 for \0 */
202         obstack_grow(&notes, name, name_length);
203         preserved_copy_of_name = obstack_finish(&notes);
204         symbolP = (symbolS *)obstack_alloc(&notes, sizeof(symbolS));
205
206  /* symbol must be born in some fixed state.  This seems as good as any. */
207         memset(symbolP, 0, sizeof(symbolS));
208
209 #if STRIP_UNDERSCORE
210         S_SET_NAME(symbolP, (*preserved_copy_of_name == '_'
211                                             ? preserved_copy_of_name + 1
212                                             : preserved_copy_of_name));
213 #else /* STRIP_UNDERSCORE */
214         S_SET_NAME(symbolP, preserved_copy_of_name);
215 #endif /* STRIP_UNDERSCORE */
216
217         S_SET_SEGMENT(symbolP, segment);
218         S_SET_VALUE(symbolP, value);
219 /*      symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
220
221         symbolP->sy_frag = frag;
222         /* krm: uneeded if symbol is born zeroed.
223            symbolP->sy_forward = NULL; */ /* JF */
224         symbolP->sy_number = ~0;
225         symbolP->sy_name_offset = ~0;
226
227         /*
228          * Link to end of symbol chain.
229          */
230         symbol_append(symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
231
232         obj_symbol_new_hook(symbolP);
233         
234 #ifdef DEBUG
235         verify_symbol_chain(symbol_rootP, symbol_lastP);
236 #endif /* DEBUG */
237
238         return(symbolP);
239 } /* symbol_new() */
240
241 \f
242 /*
243  *                      colon()
244  *
245  * We have just seen "<name>:".
246  * Creates a struct symbol unless it already exists.
247  *
248  * Gripes if we are redefining a symbol incompatibly (and ignores it).
249  *
250  */
251 void colon(sym_name)            /* just seen "x:" - rattle symbols & frags */
252      register char *  sym_name; /* symbol name, as a cannonical string */
253                                 /* We copy this string: OK to alter later. */
254 {
255   register symbolS * symbolP; /* symbol we are working with */
256
257 #ifdef LOCAL_LABELS_DOLLAR
258   /* Sun local labels go out of scope whenever a non-local symbol is defined.  */
259
260   if(*sym_name !='L')
261     bzero((void *) local_label_defined, sizeof(local_label_defined));
262 #endif
263
264 #ifndef WORKING_DOT_WORD
265   if(new_broken_words) {
266     struct broken_word *a;
267     int possible_bytes;
268     fragS *frag_tmp;
269     char *frag_opcode;
270     extern md_short_jump_size;
271     extern md_long_jump_size;
272
273     possible_bytes=md_short_jump_size + new_broken_words * md_long_jump_size;
274     frag_tmp=frag_now;
275     frag_opcode=frag_var(rs_broken_word,
276                          possible_bytes,
277                          possible_bytes,
278                          (relax_substateT) 0,
279                          (symbolS *) broken_words,
280                          0L,
281                          NULL);
282
283     /* We want to store the pointer to where to insert the jump table in the
284        fr_opcode of the rs_broken_word frag.  This requires a little hackery */
285     while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
286       frag_tmp=frag_tmp->fr_next;
287     know(frag_tmp);
288     frag_tmp->fr_opcode=frag_opcode;
289     new_broken_words = 0;
290
291     for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
292       a->dispfrag=frag_tmp;
293   }
294 #endif
295   if ((symbolP = symbol_find(sym_name)) != 0) {
296 #ifdef  VMS
297           /*
298            *    If the new symbol is .comm AND it has a size of zero,
299            *    we ignore it (i.e. the old symbol overrides it)
300            */
301           if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg) == (N_UNDF | N_EXT)) &&
302               ((obstack_next_free(& frags) - frag_now->fr_literal) == 0))
303               return;
304           /*
305            *    If the old symbol is .comm and it has a size of zero,
306            *    we override it with the new symbol value.
307            */
308           if ((symbolP->sy_type == (N_UNDF | N_EXT))
309               && (S_GET_VALUE(symbolP) == 0)) {
310                   symbolP->sy_frag  = frag_now;
311                   symbolP->sy_other = const_flag;
312                   S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
313                   symbolP->sy_type |= SEGMENT_TO_SYMBOL_TYPE((int) now_seg); /* keep N_EXT bit */
314                   return;
315           }
316 #endif  /* VMS */
317           /*
318            *    Now check for undefined symbols
319            */
320           if (!S_IS_DEFINED(symbolP)) {
321                   if (S_GET_VALUE(symbolP) == 0) {
322                           symbolP->sy_frag  = frag_now;
323 #ifdef VMS
324                           symbolP->sy_other = const_flag;
325 #endif
326                           S_SET_VALUE(symbolP, obstack_next_free(&frags) - frag_now->fr_literal);
327                           S_SET_SEGMENT(symbolP, now_seg);
328 #ifdef N_UNDF
329                           know(N_UNDF == 0);
330 #endif /* if we have one, it better be zero. */
331                           
332                   } else {
333                           /*
334                            *    There are still several cases to check:
335                            *            A .comm/.lcomm symbol being redefined as
336                            *                    initialized data is OK
337                            *            A .comm/.lcomm symbol being redefined with
338                            *                    a larger size is also OK
339                            *
340                            * This only used to be allowed on VMS gas, but Sun cc
341                            * on the sparc also depends on it.
342                            */
343 /*                        char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
344                           
345                           if (((!S_IS_DEBUG(symbolP) && !S_IS_DEFINED(symbolP) && S_IS_EXTERNAL(symbolP))
346                                || (S_GET_SEGMENT(symbolP) == SEG_BSS))
347                               && ((now_seg == SEG_DATA)
348                                   || (now_seg == S_GET_SEGMENT(symbolP)))) {
349                                   /*
350                                    *    Select which of the 2 cases this is
351                                    */
352                                   if (now_seg != SEG_DATA) {
353                                           /*
354                                            *   New .comm for prev .comm symbol.
355                                            *    If the new size is larger we just
356                                            *    change its value.  If the new size
357                                            *    is smaller, we ignore this symbol
358                                            */
359                                           if (S_GET_VALUE(symbolP)
360                                               < ((unsigned) (obstack_next_free(& frags) - frag_now->fr_literal))) {
361                                                   S_SET_VALUE(symbolP, 
362                                                               obstack_next_free(& frags) -
363                                                               frag_now->fr_literal);
364                                           }
365                                   } else {
366                                           /*
367                                            *    It is a .comm/.lcomm being converted
368                                            *    to initialized data.
369                                            */
370                                           symbolP->sy_frag  = frag_now;
371 #ifdef VMS
372                                           symbolP->sy_other = const_flag;
373 #endif /* VMS */
374                                           S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
375                                           S_SET_SEGMENT(symbolP, now_seg); /* keep N_EXT bit */
376                                   }
377                           } else {
378 #ifdef OBJ_COFF
379                                   as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
380                                            sym_name,
381                                            segment_name(S_GET_SEGMENT(symbolP)),
382                                            S_GET_VALUE(symbolP));
383 #else /* OBJ_COFF */
384                                   as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
385                                            sym_name,
386                                            segment_name(S_GET_SEGMENT(symbolP)),
387                                            S_GET_OTHER(symbolP), S_GET_DESC(symbolP),
388                                            S_GET_VALUE(symbolP));
389 #endif /* OBJ_COFF */
390                           }
391                   } /* if the undefined symbol has no value */
392           } else {
393                   as_fatal("Symbol %s already defined.", sym_name);
394           } /* if this symbol is not yet defined */
395
396   } else {
397           symbolP = symbol_new(sym_name,
398                                 now_seg, 
399                                 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
400                                 frag_now);
401 #ifdef VMS
402           S_SET_OTHER(symbolP, const_flag);
403 #endif /* VMS */
404           
405           symbol_table_insert(symbolP);
406   } /* if we have seen this symbol before */
407
408   return;
409 } /* colon() */
410
411 \f
412 /*
413  *                      symbol_table_insert()
414  *
415  * Die if we can't insert the symbol.
416  *
417  */
418
419 void symbol_table_insert(symbolP)
420 symbolS *symbolP;
421 {
422         register char *error_string;
423         
424         know(symbolP);
425         know(S_GET_NAME(symbolP));
426         
427         if (*(error_string = hash_jam(sy_hash, S_GET_NAME(symbolP), (char *)symbolP))) {
428                 as_fatal("Inserting \"%s\" into symbol table failed: %s",
429                          S_GET_NAME(symbolP), error_string);
430         } /* on error */
431 } /* symbol_table_insert() */
432 \f
433 /*
434  *                      symbol_find_or_make()
435  *
436  * If a symbol name does not exist, create it as undefined, and insert
437  * it into the symbol table. Return a pointer to it.
438  */
439 symbolS *symbol_find_or_make(name)
440 char *name;
441 {
442         register symbolS *symbolP;
443         
444         symbolP = symbol_find(name);
445         
446         if (symbolP == NULL) {
447                 symbolP = symbol_make(name);
448
449                 symbol_table_insert(symbolP);
450         } /* if symbol wasn't found */
451
452         return(symbolP);
453 } /* symbol_find_or_make() */
454
455 symbolS *symbol_make(name)
456 char *name;
457 {
458         symbolS *symbolP;
459
460         /* Let the machine description default it, e.g. for register names. */
461         symbolP = md_undefined_symbol(name);
462         
463         if (!symbolP) {
464                 symbolP = symbol_new(name,
465                                      SEG_UNKNOWN,
466                                      0,
467                                      &zero_address_frag);
468         } /* if md didn't build us a symbol */
469
470         return(symbolP);
471 } /* symbol_make() */
472
473 /*
474  *                      symbol_find()
475  * 
476  * Implement symbol table lookup.
477  * In:  A symbol's name as a string: '\0' can't be part of a symbol name.
478  * Out: NULL if the name was not in the symbol table, else the address
479  *      of a struct symbol associated with that name.
480  */
481
482 symbolS *symbol_find(name)
483 char *name;
484 {
485 #ifndef STRIP_UNDERSCORE
486 #define STRIP_UNDERSCORE 0
487 #endif /* STRIP_UNDERSCORE */
488     return symbol_find_base(name, STRIP_UNDERSCORE);
489 }
490
491 symbolS *symbol_find_base(name, strip_underscore)
492 char *name;
493 int strip_underscore;
494 {
495     if(strip_underscore && *name == '_') name++;
496     return ( (symbolS *) hash_find( sy_hash, name ));
497 }
498
499 /*
500  * Once upon a time, symbols were kept in a singly linked list.  At
501  * least coff needs to be able to rearrange them from time to time, for
502  * which a doubly linked list is much more convenient.  Loic did these
503  * as macros which seemed dangerous to me so they're now functions.
504  * xoxorich.
505  */
506
507 /* Link symbol ADDME after symbol TARGET in the chain. */
508 void symbol_append(addme, target, rootPP, lastPP)
509 symbolS *addme;
510 symbolS *target;
511 symbolS **rootPP;
512 symbolS **lastPP;
513 {
514         if (target == NULL) {
515                 know(*rootPP == NULL);
516                 know(*lastPP == NULL);
517                 *rootPP = addme;
518                 *lastPP = addme;
519                 return;
520         } /* if the list is empty */
521
522         if (target->sy_next != NULL) {
523 #ifdef SYMBOLS_NEED_BACKPOINTERS
524                 target->sy_next->sy_previous = addme;
525 #endif /* SYMBOLS_NEED_BACKPOINTERS */
526         } else {
527                 know(*lastPP == target);
528                 *lastPP = addme;
529         } /* if we have a next */
530         
531         addme->sy_next = target->sy_next;
532         target->sy_next = addme;
533
534 #ifdef SYMBOLS_NEED_BACKPOINTERS
535         addme->sy_previous = target;
536 #endif /* SYMBOLS_NEED_BACKPOINTERS */
537
538 #ifdef DEBUG
539         verify_symbol_chain(*rootPP, *lastPP);
540 #endif /* DEBUG */
541
542         return;
543 } /* symbol_append() */
544
545 #ifdef SYMBOLS_NEED_BACKPOINTERS
546 /* Remove SYMBOLP from the list. */
547 void symbol_remove(symbolP, rootPP, lastPP)
548 symbolS *symbolP;
549 symbolS **rootPP;
550 symbolS **lastPP;
551 {
552         if (symbolP == *rootPP) {
553                 *rootPP = symbolP->sy_next;
554         } /* if it was the root */
555
556         if (symbolP == *lastPP) {
557                 *lastPP = symbolP->sy_previous;
558         } /* if it was the tail */
559
560         if (symbolP->sy_next != NULL) {
561                 symbolP->sy_next->sy_previous = symbolP->sy_previous;
562         } /* if not last */
563         
564         if (symbolP->sy_previous != NULL) {
565                 symbolP->sy_previous->sy_next = symbolP->sy_next;
566         } /* if not first */
567         
568 #ifdef DEBUG
569         verify_symbol_chain(*rootPP, *lastPP);
570 #endif /* DEBUG */
571
572         return;
573 } /* symbol_remove() */
574
575 /* Set the chain pointers of SYMBOL to null. */
576 void symbol_clear_list_pointers(symbolP)
577 symbolS *symbolP;
578 {
579     symbolP->sy_next = NULL;
580     symbolP->sy_previous = NULL;
581 } /* symbol_clear_list_pointers() */
582
583 /* Link symbol ADDME before symbol TARGET in the chain. */
584 void symbol_insert(addme, target, rootPP, lastPP)
585 symbolS *addme;
586 symbolS *target;
587 symbolS **rootPP;
588 symbolS **lastPP;
589 {
590     if (target->sy_previous != NULL) {
591             target->sy_previous->sy_next = addme;
592     } else {
593             know(*rootPP == target);
594             *rootPP = addme;
595     } /* if not first */
596
597     addme->sy_previous = target->sy_previous;
598     target->sy_previous = addme;
599     addme->sy_next = target;
600
601 #ifdef DEBUG
602     verify_symbol_chain(*rootPP, *lastPP);
603 #endif /* DEBUG */
604
605     return;
606 } /* symbol_insert() */
607 #endif /* SYMBOLS_NEED_BACKPOINTERS */
608
609 void verify_symbol_chain(rootP, lastP)
610 symbolS *rootP;
611 symbolS *lastP;
612 {
613         symbolS *symbolP = rootP;
614
615         if (symbolP == NULL) {
616                 return;
617         } /* empty chain */
618
619         for ( ; symbol_next(symbolP) != NULL; symbolP = symbol_next(symbolP)) {
620 #ifdef SYMBOLS_NEED_BACKPOINTERS
621                 /*$if (symbolP->sy_previous) {
622                         know(symbolP->sy_previous->sy_next == symbolP);
623                 } else {
624                         know(symbolP == rootP);
625                 }$*/ /* both directions */
626                 know(symbolP->sy_next->sy_previous == symbolP);
627 #else /* SYMBOLS_NEED_BACKPOINTERS */
628                 ;
629 #endif /* SYMBOLS_NEED_BACKPOINTERS */
630         } /* verify pointers */
631
632         know(lastP == symbolP);
633
634         return;
635 } /* verify_symbol_chain() */
636
637
638 /*
639  * Local Variables:
640  * comment-column: 0
641  * fill-column: 131
642  * End:
643  */
644
645 /* end: symbols.c */