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. */
20 /* static const char rcsid[] = "$Id$"; */
24 #include "obstack.h" /* For "symbols.h" */
27 #ifndef WORKING_DOT_WORD
28 extern int new_broken_words;
31 extern char const_flag;
36 sy_hash; /* symbol-name => struct symbol pointer */
38 /* Below are commented in "symbols.h". */
39 unsigned int local_bss_counter;
40 symbolS * symbol_rootP;
41 symbolS * symbol_lastP;
44 symbolS* dot_text_symbol;
45 symbolS* dot_data_symbol;
46 symbolS* dot_bss_symbol;
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:.
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.
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
69 typedef short unsigned int
72 static local_label_countT
73 local_label_counter[10];
75 static /* Returned to caller, then copied. */
76 char symbol_name_build[12]; /* used for created names ("4f") */
78 #ifdef LOCAL_LABELS_DOLLAR
79 int local_label_defined[10];
87 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
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;
98 * Caller must copy returned name: we re-use the area for the next name.
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 */
108 char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
111 know( augend == 0 || augend == 1 );
112 p = symbol_name_build;
114 * p ++ = n + '0'; /* Make into ASCII */
116 n = local_label_counter [ n ] + augend;
117 /* version number of this local label */
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.
123 q = symbol_name_temporary;
124 for (*q++=0; n; q++) /* emits NOTHING if n starts as 0 */
126 know(n>0); /* We expect n > 0 always */
130 while (( * p ++ = * -- q ) != '\0') ;;
132 /* The label, as a '\0' ended string, starts at symbol_name_build. */
133 return(symbol_name_build);
134 } /* local_label_name() */
138 int n; /* just saw "n:" */
140 local_label_counter [n] ++;
141 #ifdef LOCAL_LABELS_DOLLAR
142 local_label_defined[n]=1;
144 colon (local_label_name (n, 0));
150 * Return a pointer to a new symbol.
151 * Die if we can't make a new symbol.
152 * Fill in the symbol's values.
153 * Add symbol to end of symbol chain.
156 * Please always call this to create a new symbol.
158 * Changes since 1985: Symbol names may not contain '\0'. Sigh.
159 * 2nd argument is now a SEG rather than a TYPE. The mapping between
160 * segments and types is mostly encapsulated herein (actually, we inherit it
161 * from macros in struc-symbol.h).
164 symbolS *symbol_new(name, segment, value, frag)
165 char *name; /* It is copied, the caller can destroy/modify */
166 segT segment; /* Segment identifier (SEG_<something>) */
167 long value; /* Symbol value */
168 fragS *frag; /* Associated fragment */
170 unsigned int name_length;
171 char *preserved_copy_of_name;
174 name_length = strlen(name) + 1; /* +1 for \0 */
175 obstack_grow(¬es, name, name_length);
176 preserved_copy_of_name = obstack_finish(¬es);
177 symbolP = (symbolS *)obstack_alloc(¬es, sizeof(symbolS));
179 /* symbol must be born in some fixed state. This seems as good as any. */
180 memset(symbolP, 0, sizeof(symbolS));
182 #ifdef STRIP_UNDERSCORE
183 S_SET_NAME(symbolP, (*preserved_copy_of_name == '_'
184 ? preserved_copy_of_name + 1
185 : preserved_copy_of_name));
186 #else /* STRIP_UNDERSCORE */
187 S_SET_NAME(symbolP, preserved_copy_of_name);
188 #endif /* STRIP_UNDERSCORE */
190 S_SET_SEGMENT(symbolP, segment);
191 S_SET_VALUE(symbolP, value);
192 /* symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
194 symbolP->sy_frag = frag;
195 /* krm: uneeded if symbol is born zeroed.
196 symbolP->sy_forward = NULL; */ /* JF */
197 symbolP->sy_number = ~0;
198 symbolP->sy_name_offset = ~0;
201 * Link to end of symbol chain.
203 symbol_append(symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
205 obj_symbol_new_hook(symbolP);
208 verify_symbol_chain(symbol_rootP, symbol_lastP);
218 * We have just seen "<name>:".
219 * Creates a struct symbol unless it already exists.
221 * Gripes if we are redefining a symbol incompatibly (and ignores it).
224 void colon(sym_name) /* just seen "x:" - rattle symbols & frags */
225 register char * sym_name; /* symbol name, as a cannonical string */
226 /* We copy this string: OK to alter later. */
228 register symbolS * symbolP; /* symbol we are working with */
230 #ifdef LOCAL_LABELS_DOLLAR
231 /* Sun local labels go out of scope whenever a non-local symbol is defined. */
234 bzero((void *) local_label_defined, sizeof(local_label_defined));
237 #ifndef WORKING_DOT_WORD
238 if(new_broken_words) {
239 struct broken_word *a;
244 extern md_short_jump_size;
245 extern md_long_jump_size;
246 possible_bytes=md_short_jump_size + new_broken_words * md_long_jump_size;
249 frag_opcode=frag_var(rs_broken_word,
253 (symbolS *) broken_words,
257 /* We want to store the pointer to where to insert the jump table in the
258 fr_opcode of the rs_broken_word frag. This requires a little hackery */
259 while(frag_tmp && (frag_tmp->fr_type!=rs_broken_word || frag_tmp->fr_opcode))
260 frag_tmp=frag_tmp->fr_next;
262 frag_tmp->fr_opcode=frag_opcode;
263 new_broken_words = 0;
265 for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
266 a->dispfrag=frag_tmp;
269 if ((symbolP = symbol_find(sym_name)) != 0) {
272 * If the new symbol is .comm AND it has a size of zero,
273 * we ignore it (i.e. the old symbol overrides it)
275 if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg) == (N_UNDF | N_EXT)) &&
276 ((obstack_next_free(& frags) - frag_now->fr_literal) == 0))
279 * If the old symbol is .comm and it has a size of zero,
280 * we override it with the new symbol value.
282 if ((symbolP->sy_type == (N_UNDF | N_EXT))
283 && (S_GET_VALUE(symbolP) == 0)) {
284 symbolP->sy_frag = frag_now;
285 symbolP->sy_other = const_flag;
286 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
287 symbolP->sy_type |= SEGMENT_TO_SYMBOL_TYPE((int) now_seg); /* keep N_EXT bit */
292 * Now check for undefined symbols
294 if (!S_IS_DEFINED(symbolP)) {
295 if (S_GET_VALUE(symbolP) == 0) {
296 symbolP->sy_frag = frag_now;
298 symbolP->sy_other = const_flag;
300 S_SET_VALUE(symbolP, obstack_next_free(&frags) - frag_now->fr_literal);
301 S_SET_SEGMENT(symbolP, now_seg);
304 #endif /* if we have one, it better be zero. */
308 * There are still several cases to check:
309 * A .comm/.lcomm symbol being redefined as
310 * initialized data is OK
311 * A .comm/.lcomm symbol being redefined with
312 * a larger size is also OK
314 * This only used to be allowed on VMS gas, but Sun cc
315 * on the sparc also depends on it.
317 /* char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
319 #define SEG_BSS SEG_E2
320 #define SEG_DATA SEG_E1
323 if (((!S_IS_DEBUG(symbolP) && !S_IS_DEFINED(symbolP) && S_IS_EXTERNAL(symbolP))
324 || (S_GET_SEGMENT(symbolP) == SEG_BSS))
325 && ((now_seg == SEG_DATA)
326 || (now_seg == S_GET_SEGMENT(symbolP)))) {
328 * Select which of the 2 cases this is
330 if (now_seg != SEG_DATA) {
332 * New .comm for prev .comm symbol.
333 * If the new size is larger we just
334 * change its value. If the new size
335 * is smaller, we ignore this symbol
337 if (S_GET_VALUE(symbolP)
338 < ((unsigned) (obstack_next_free(& frags) - frag_now->fr_literal))) {
340 obstack_next_free(& frags) -
341 frag_now->fr_literal);
345 * It is a .comm/.lcomm being converted
346 * to initialized data.
348 symbolP->sy_frag = frag_now;
350 symbolP->sy_other = const_flag;
352 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
353 S_SET_SEGMENT(symbolP, now_seg); /* keep N_EXT bit */
357 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
359 segment_name(S_GET_SEGMENT(symbolP)),
360 S_GET_VALUE(symbolP));
362 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
364 segment_name(S_GET_SEGMENT(symbolP)),
365 S_GET_OTHER(symbolP), S_GET_DESC(symbolP),
366 S_GET_VALUE(symbolP));
367 #endif /* OBJ_COFF */
369 } /* if the undefined symbol has no value */
371 as_fatal("Symbol %s already defined.", sym_name);
372 } /* if this symbol is not yet defined */
375 symbolP = symbol_new(sym_name,
377 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
380 S_SET_OTHER(symbolP, const_flag);
383 symbol_table_insert(symbolP);
384 } /* if we have seen this symbol before */
391 * symbol_table_insert()
393 * Die if we can't insert the symbol.
397 void symbol_table_insert(symbolP)
400 register char *error_string;
403 know(S_GET_NAME(symbolP));
405 if (*(error_string = hash_jam(sy_hash, S_GET_NAME(symbolP), (char *)symbolP))) {
406 as_fatal("Inserting \"%s\" into symbol table failed: %s",
407 S_GET_NAME(symbolP), error_string);
409 } /* symbol_table_insert() */
412 * symbol_find_or_make()
414 * If a symbol name does not exist, create it as undefined, and insert
415 * it into the symbol table. Return a pointer to it.
417 symbolS *symbol_find_or_make(name)
420 register symbolS *symbolP;
422 symbolP = symbol_find(name);
424 if (symbolP == NULL) {
425 symbolP = symbol_make(name);
427 symbol_table_insert(symbolP);
428 } /* if symbol wasn't found */
431 } /* symbol_find_or_make() */
433 symbolS *symbol_make(name)
438 /* Let the machine description default it, e.g. for register names. */
439 symbolP = md_undefined_symbol(name);
442 symbolP = symbol_new(name,
446 } /* if md didn't build us a symbol */
449 } /* symbol_make() */
454 * Implement symbol table lookup.
455 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
456 * Out: NULL if the name was not in the symbol table, else the address
457 * of a struct symbol associated with that name.
460 symbolS *symbol_find(name)
463 #ifdef STRIP_UNDERSCORE
464 return(symbol_find_base(name, 1));
465 #else /* STRIP_UNDERSCORE */
466 return(symbol_find_base(name, ));
467 #endif /* STRIP_UNDERSCORE */
468 } /* symbol_find() */
470 symbolS *symbol_find_base(name, strip_underscore)
472 int strip_underscore;
474 if(strip_underscore && *name == '_') name++;
475 return ( (symbolS *) hash_find( sy_hash, name ));
479 * Once upon a time, symbols were kept in a singly linked list. At
480 * least coff needs to be able to rearrange them from time to time, for
481 * which a doubly linked list is much more convenient. Loic did these
482 * as macros which seemed dangerous to me so they're now functions.
486 /* Link symbol ADDME after symbol TARGET in the chain. */
487 void symbol_append(addme, target, rootPP, lastPP)
493 if (target == NULL) {
494 know(*rootPP == NULL);
495 know(*lastPP == NULL);
499 } /* if the list is empty */
501 if (target->sy_next != NULL) {
502 #ifdef SYMBOLS_NEED_BACKPOINTERS
503 target->sy_next->sy_previous = addme;
504 #endif /* SYMBOLS_NEED_BACKPOINTERS */
506 know(*lastPP == target);
508 } /* if we have a next */
510 addme->sy_next = target->sy_next;
511 target->sy_next = addme;
513 #ifdef SYMBOLS_NEED_BACKPOINTERS
514 addme->sy_previous = target;
515 #endif /* SYMBOLS_NEED_BACKPOINTERS */
518 verify_symbol_chain(*rootPP, *lastPP);
522 } /* symbol_append() */
524 #ifdef SYMBOLS_NEED_BACKPOINTERS
525 /* Remove SYMBOLP from the list. */
526 void symbol_remove(symbolP, rootPP, lastPP)
531 if (symbolP == *rootPP) {
532 *rootPP = symbolP->sy_next;
533 } /* if it was the root */
535 if (symbolP == *lastPP) {
536 *lastPP = symbolP->sy_previous;
537 } /* if it was the tail */
539 if (symbolP->sy_next != NULL) {
540 symbolP->sy_next->sy_previous = symbolP->sy_previous;
543 if (symbolP->sy_previous != NULL) {
544 symbolP->sy_previous->sy_next = symbolP->sy_next;
548 verify_symbol_chain(*rootPP, *lastPP);
552 } /* symbol_remove() */
554 /* Set the chain pointers of SYMBOL to null. */
555 void symbol_clear_list_pointers(symbolP)
558 symbolP->sy_next = NULL;
559 symbolP->sy_previous = NULL;
560 } /* symbol_clear_list_pointers() */
562 /* Link symbol ADDME before symbol TARGET in the chain. */
563 void symbol_insert(addme, target, rootPP, lastPP)
569 if (target->sy_previous != NULL) {
570 target->sy_previous->sy_next = addme;
572 know(*rootPP == target);
576 addme->sy_previous = target->sy_previous;
577 target->sy_previous = addme;
578 addme->sy_next = target;
581 verify_symbol_chain(*rootPP, *lastPP);
585 } /* symbol_insert() */
586 #endif /* SYMBOLS_NEED_BACKPOINTERS */
588 void verify_symbol_chain(rootP, lastP)
592 symbolS *symbolP = rootP;
594 if (symbolP == NULL) {
598 for ( ; symbol_next(symbolP) != NULL; symbolP = symbol_next(symbolP)) {
599 #ifdef SYMBOLS_NEED_BACKPOINTERS
600 /*$if (symbolP->sy_previous) {
601 know(symbolP->sy_previous->sy_next == symbolP);
603 know(symbolP == rootP);
604 }$*/ /* both directions */
605 know(symbolP->sy_next->sy_previous == symbolP);
606 #else /* SYMBOLS_NEED_BACKPOINTERS */
608 #endif /* SYMBOLS_NEED_BACKPOINTERS */
609 } /* verify pointers */
611 know(lastP == symbolP);
614 } /* verify_symbol_chain() */
618 * decode name that may have been generated by local_label_name() above. If
619 * the name wasn't generated by local_label_name(), then return it unaltered.
620 * This is used for error messages.
623 char *decode_local_label_name(s)
628 /* int label_version; */
629 char *message_format = "\"%d\" (instance number %s of a local label)";
634 } /* not a local_label_name() generated name. */
636 label_number = s[1] - '0';
638 (void) sprintf(symbol_decode = obstack_alloc(¬es, strlen(s + 3) + strlen(message_format) + 10),
639 message_format, label_number, s + 3);
641 return(symbol_decode);
642 } /* decode_local_label_name() */