1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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. */
22 #include "obstack.h" /* For "symbols.h" */
25 #ifndef WORKING_DOT_WORD
26 extern int new_broken_words;
29 extern char const_flag;
34 sy_hash; /* symbol-name => struct symbol pointer */
36 /* Below are commented in "symbols.h". */
37 unsigned int local_bss_counter;
38 symbolS * symbol_rootP;
39 symbolS * symbol_lastP;
42 symbolS* dot_text_symbol;
43 symbolS* dot_data_symbol;
44 symbolS* dot_bss_symbol;
49 * Un*x idea of local labels. They are made by "n:" where n
50 * is any decimal digit. Refer to them with
51 * "nb" for previous (backward) n:
52 * or "nf" for next (forward) n:.
54 * Like Un*x AS, we have one set of local label counters for entire assembly,
55 * not one set per (sub)segment like in most assemblers. This implies that
56 * one can refer to a label in another segment, and indeed some crufty
57 * compilers have done just that.
59 * I document the symbol names here to save duplicating words elsewhere.
60 * The mth occurence of label n: is turned into the symbol "Ln^Am" where
61 * n is a digit and m is a decimal number. "L" makes it a label discarded
62 * unless debugging and "^A"('\1') ensures no ordinary symbol SHOULD get the
63 * same name as a local label symbol. The first "4:" is "L4^A1" - the m
67 typedef short unsigned int
70 static local_label_countT
71 local_label_counter[10];
73 static /* Returned to caller, then copied. */
74 char symbol_name_build[12]; /* used for created names ("4f") */
76 #ifdef LOCAL_LABELS_DOLLAR
77 int local_label_defined[10];
85 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
87 bzero ((char *)(& abs_symbol), sizeof(abs_symbol));
88 S_SET_SEGMENT(&abs_symbol, SEG_ABSOLUTE); /* Can't initialise a union. Sigh. */
89 bzero ((char *)(local_label_counter), sizeof(local_label_counter) );
90 local_bss_counter = 0;
96 * Caller must copy returned name: we re-use the area for the next name.
99 char * /* Return local label name. */
100 local_label_name(n, augend)
101 register int n; /* we just saw "n:", "nf" or "nb" : n a digit */
102 register int augend; /* 0 for nb, 1 for n:, nf */
106 char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
109 know( augend == 0 || augend == 1 );
110 p = symbol_name_build;
112 * p ++ = n + '0'; /* Make into ASCII */
114 n = local_label_counter [ n ] + augend;
115 /* version number of this local label */
117 * Next code just does sprintf( {}, "%d", n);
118 * It is more elegant to do the next part recursively, but a procedure
119 * call for each digit emitted is considered too costly.
121 q = symbol_name_temporary;
122 for (*q++=0; n; q++) /* emits NOTHING if n starts as 0 */
124 know(n>0); /* We expect n > 0 always */
128 while (( * p ++ = * -- q ) != '\0') ;;
130 /* The label, as a '\0' ended string, starts at symbol_name_build. */
131 return(symbol_name_build);
132 } /* local_label_name() */
136 int n; /* just saw "n:" */
138 local_label_counter [n] ++;
139 #ifdef LOCAL_LABELS_DOLLAR
140 local_label_defined[n]=1;
142 colon (local_label_name (n, 0));
148 * Return a pointer to a new symbol.
149 * Die if we can't make a new symbol.
150 * Fill in the symbol's values.
151 * Add symbol to end of symbol chain.
154 * Please always call this to create a new symbol.
156 * Changes since 1985: Symbol names may not contain '\0'. Sigh.
157 * 2nd argument is now a SEG rather than a TYPE. The mapping between
158 * segments and types is mostly encapsulated herein (actually, we inherit it
159 * from macros in struc-symbol.h).
162 symbolS *symbol_new(name, segment, value, frag)
163 char *name; /* It is copied, the caller can destroy/modify */
164 segT segment; /* Segment identifier (SEG_<something>) */
165 long value; /* Symbol value */
166 fragS *frag; /* Associated fragment */
168 unsigned int name_length;
169 char *preserved_copy_of_name;
172 name_length = strlen(name) + 1; /* +1 for \0 */
173 obstack_grow(¬es, name, name_length);
174 preserved_copy_of_name = obstack_finish(¬es);
175 symbolP = (symbolS *)obstack_alloc(¬es, sizeof(symbolS));
177 /* symbol must be born in some fixed state. This seems as good as any. */
178 memset(symbolP, 0, sizeof(symbolS));
180 #ifdef STRIP_UNDERSCORE
181 S_SET_NAME(symbolP, (*preserved_copy_of_name == '_'
182 ? preserved_copy_of_name + 1
183 : preserved_copy_of_name));
184 #else /* STRIP_UNDERSCORE */
185 S_SET_NAME(symbolP, preserved_copy_of_name);
186 #endif /* STRIP_UNDERSCORE */
188 S_SET_SEGMENT(symbolP, segment);
189 S_SET_VALUE(symbolP, value);
190 /* symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
192 symbolP->sy_frag = frag;
193 /* krm: uneeded if symbol is born zeroed.
194 symbolP->sy_forward = NULL; */ /* JF */
195 symbolP->sy_number = ~0;
196 symbolP->sy_name_offset = ~0;
199 * Link to end of symbol chain.
201 symbol_append(symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
203 obj_symbol_new_hook(symbolP);
206 /* verify_symbol_chain(symbol_rootP, symbol_lastP); */
216 * We have just seen "<name>:".
217 * Creates a struct symbol unless it already exists.
219 * Gripes if we are redefining a symbol incompatibly (and ignores it).
222 void colon(sym_name) /* just seen "x:" - rattle symbols & frags */
223 register char * sym_name; /* symbol name, as a cannonical string */
224 /* We copy this string: OK to alter later. */
226 register symbolS * symbolP; /* symbol we are working with */
228 #ifdef LOCAL_LABELS_DOLLAR
229 /* Sun local labels go out of scope whenever a non-local symbol is defined. */
232 bzero((void *) local_label_defined, sizeof(local_label_defined));
235 #ifndef WORKING_DOT_WORD
236 if(new_broken_words) {
237 struct broken_word *a;
242 extern md_short_jump_size;
243 extern md_long_jump_size;
244 possible_bytes=md_short_jump_size + new_broken_words * md_long_jump_size;
247 frag_opcode=frag_var(rs_broken_word,
251 (symbolS *) broken_words,
255 /* We want to store the pointer to where to insert the jump table in the
256 fr_opcode of the rs_broken_word frag. This requires a little hackery */
257 while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
258 frag_tmp=frag_tmp->fr_next;
260 frag_tmp->fr_opcode=frag_opcode;
261 new_broken_words = 0;
263 for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
264 a->dispfrag=frag_tmp;
267 if ((symbolP = symbol_find(sym_name)) != 0) {
270 * If the new symbol is .comm AND it has a size of zero,
271 * we ignore it (i.e. the old symbol overrides it)
273 if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg) == (N_UNDF | N_EXT)) &&
274 ((obstack_next_free(& frags) - frag_now->fr_literal) == 0))
277 * If the old symbol is .comm and it has a size of zero,
278 * we override it with the new symbol value.
280 if ((symbolP->sy_type == (N_UNDF | N_EXT))
281 && (S_GET_VALUE(symbolP) == 0)) {
282 symbolP->sy_frag = frag_now;
283 symbolP->sy_other = const_flag;
284 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
285 symbolP->sy_type |= SEGMENT_TO_SYMBOL_TYPE((int) now_seg); /* keep N_EXT bit */
290 * Now check for undefined symbols
292 if (!S_IS_DEFINED(symbolP)) {
293 if (S_GET_VALUE(symbolP) == 0) {
294 symbolP->sy_frag = frag_now;
296 symbolP->sy_other = const_flag;
298 S_SET_VALUE(symbolP, obstack_next_free(&frags) - frag_now->fr_literal);
299 S_SET_SEGMENT(symbolP, now_seg);
302 #endif /* if we have one, it better be zero. */
306 * There are still several cases to check:
307 * A .comm/.lcomm symbol being redefined as
308 * initialized data is OK
309 * A .comm/.lcomm symbol being redefined with
310 * a larger size is also OK
312 * This only used to be allowed on VMS gas, but Sun cc
313 * on the sparc also depends on it.
315 /* char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
317 #define SEG_BSS SEG_E2
318 #define SEG_DATA SEG_E1
321 if (((!S_IS_DEBUG(symbolP) && !S_IS_DEFINED(symbolP) && S_IS_EXTERNAL(symbolP))
322 || (S_GET_SEGMENT(symbolP) == SEG_BSS))
323 && ((now_seg == SEG_DATA)
324 || (now_seg == S_GET_SEGMENT(symbolP)))) {
326 * Select which of the 2 cases this is
328 if (now_seg != SEG_DATA) {
330 * New .comm for prev .comm symbol.
331 * If the new size is larger we just
332 * change its value. If the new size
333 * is smaller, we ignore this symbol
335 if (S_GET_VALUE(symbolP)
336 < ((unsigned) (obstack_next_free(& frags) - frag_now->fr_literal))) {
338 obstack_next_free(& frags) -
339 frag_now->fr_literal);
343 * It is a .comm/.lcomm being converted
344 * to initialized data.
346 symbolP->sy_frag = frag_now;
348 symbolP->sy_other = const_flag;
350 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
351 S_SET_SEGMENT(symbolP, now_seg); /* keep N_EXT bit */
355 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
357 segment_name(S_GET_SEGMENT(symbolP)),
358 S_GET_VALUE(symbolP));
360 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
362 segment_name(S_GET_SEGMENT(symbolP)),
363 S_GET_OTHER(symbolP), S_GET_DESC(symbolP),
364 S_GET_VALUE(symbolP));
365 #endif /* OBJ_COFF */
367 } /* if the undefined symbol has no value */
370 /* Don't blow up if the definition is the same */
371 if (!(frag_now == symbolP->sy_frag
372 && S_GET_VALUE(symbolP) == obstack_next_free(&frags) - frag_now->fr_literal
373 && S_GET_SEGMENT(symbolP) == now_seg) )
374 as_fatal("Symbol %s already defined.", sym_name);
375 } /* if this symbol is not yet defined */
378 symbolP = symbol_new(sym_name,
380 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
383 S_SET_OTHER(symbolP, const_flag);
386 symbol_table_insert(symbolP);
387 } /* if we have seen this symbol before */
394 * symbol_table_insert()
396 * Die if we can't insert the symbol.
400 void symbol_table_insert(symbolP)
403 register char *error_string;
406 know(S_GET_NAME(symbolP));
408 if (*(error_string = hash_jam(sy_hash, S_GET_NAME(symbolP), (char *)symbolP))) {
409 as_fatal("Inserting \"%s\" into symbol table failed: %s",
410 S_GET_NAME(symbolP), error_string);
412 } /* symbol_table_insert() */
415 * symbol_find_or_make()
417 * If a symbol name does not exist, create it as undefined, and insert
418 * it into the symbol table. Return a pointer to it.
420 symbolS *symbol_find_or_make(name)
423 register symbolS *symbolP;
425 symbolP = symbol_find(name);
427 if (symbolP == NULL) {
428 symbolP = symbol_make(name);
430 symbol_table_insert(symbolP);
431 } /* if symbol wasn't found */
434 } /* symbol_find_or_make() */
436 symbolS *symbol_make(name)
441 /* Let the machine description default it, e.g. for register names. */
442 symbolP = md_undefined_symbol(name);
445 symbolP = symbol_new(name,
449 } /* if md didn't build us a symbol */
452 } /* symbol_make() */
457 * Implement symbol table lookup.
458 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
459 * Out: NULL if the name was not in the symbol table, else the address
460 * of a struct symbol associated with that name.
463 symbolS *symbol_find(name)
466 #ifdef STRIP_UNDERSCORE
467 return(symbol_find_base(name, 1));
468 #else /* STRIP_UNDERSCORE */
469 return(symbol_find_base(name, 0));
470 #endif /* STRIP_UNDERSCORE */
471 } /* symbol_find() */
473 symbolS *symbol_find_base(name, strip_underscore)
475 int strip_underscore;
477 if(strip_underscore && *name == '_') name++;
478 return ( (symbolS *) hash_find( sy_hash, name ));
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.
489 /* Link symbol ADDME after symbol TARGET in the chain. */
490 void symbol_append(addme, target, rootPP, lastPP)
496 if (target == NULL) {
497 know(*rootPP == NULL);
498 know(*lastPP == NULL);
502 } /* if the list is empty */
504 if (target->sy_next != NULL) {
505 #ifdef SYMBOLS_NEED_BACKPOINTERS
506 target->sy_next->sy_previous = addme;
507 #endif /* SYMBOLS_NEED_BACKPOINTERS */
509 know(*lastPP == target);
511 } /* if we have a next */
513 addme->sy_next = target->sy_next;
514 target->sy_next = addme;
516 #ifdef SYMBOLS_NEED_BACKPOINTERS
517 addme->sy_previous = target;
518 #endif /* SYMBOLS_NEED_BACKPOINTERS */
521 /* verify_symbol_chain(*rootPP, *lastPP); */
525 } /* symbol_append() */
527 #ifdef SYMBOLS_NEED_BACKPOINTERS
528 /* Remove SYMBOLP from the list. */
529 void symbol_remove(symbolP, rootPP, lastPP)
534 if (symbolP == *rootPP) {
535 *rootPP = symbolP->sy_next;
536 } /* if it was the root */
538 if (symbolP == *lastPP) {
539 *lastPP = symbolP->sy_previous;
540 } /* if it was the tail */
542 if (symbolP->sy_next != NULL) {
543 symbolP->sy_next->sy_previous = symbolP->sy_previous;
546 if (symbolP->sy_previous != NULL) {
547 symbolP->sy_previous->sy_next = symbolP->sy_next;
551 verify_symbol_chain(*rootPP, *lastPP);
555 } /* symbol_remove() */
557 /* Set the chain pointers of SYMBOL to null. */
558 void symbol_clear_list_pointers(symbolP)
561 symbolP->sy_next = NULL;
562 symbolP->sy_previous = NULL;
563 } /* symbol_clear_list_pointers() */
565 /* Link symbol ADDME before symbol TARGET in the chain. */
566 void symbol_insert(addme, target, rootPP, lastPP)
572 if (target->sy_previous != NULL) {
573 target->sy_previous->sy_next = addme;
575 know(*rootPP == target);
579 addme->sy_previous = target->sy_previous;
580 target->sy_previous = addme;
581 addme->sy_next = target;
584 verify_symbol_chain(*rootPP, *lastPP);
588 } /* symbol_insert() */
589 #endif /* SYMBOLS_NEED_BACKPOINTERS */
591 void verify_symbol_chain(rootP, lastP)
595 symbolS *symbolP = rootP;
597 if (symbolP == NULL) {
601 for ( ; symbol_next(symbolP) != NULL; symbolP = symbol_next(symbolP)) {
602 #ifdef SYMBOLS_NEED_BACKPOINTERS
603 /*$if (symbolP->sy_previous) {
604 know(symbolP->sy_previous->sy_next == symbolP);
606 know(symbolP == rootP);
607 }$*/ /* both directions */
608 know(symbolP->sy_next->sy_previous == symbolP);
609 #else /* SYMBOLS_NEED_BACKPOINTERS */
611 #endif /* SYMBOLS_NEED_BACKPOINTERS */
612 } /* verify pointers */
614 know(lastP == symbolP);
617 } /* verify_symbol_chain() */
621 * decode name that may have been generated by local_label_name() above. If
622 * the name wasn't generated by local_label_name(), then return it unaltered.
623 * This is used for error messages.
626 char *decode_local_label_name(s)
631 /* int label_version; */
632 char *message_format = "\"%d\" (instance number %s of a local label)";
637 } /* not a local_label_name() generated name. */
639 label_number = s[1] - '0';
641 (void) sprintf(symbol_decode = obstack_alloc(¬es, strlen(s + 3) + strlen(message_format) + 10),
642 message_format, label_number, s + 3);
644 return(symbol_decode);
645 } /* decode_local_label_name() */