c20eaf51bbe923507e460360e4120098e7bdca7d
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() */
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.
142 char *decode_local_label_name(s
)
148 char *message_format
= "\"%d\" (instance number %s of a local label)";
153 } /* not a local_label_name() generated name. */
155 label_number
= s
[1] - '0';
157 (void) sprintf(symbol_decode
= obstack_alloc(¬es
, strlen(s
+ 3) + strlen(message_format
) + 10),
158 message_format
, label_number
, s
+ 3);
160 return(symbol_decode
);
161 } /* decode_local_label_name() */
164 int n
; /* just saw "n:" */
166 local_label_counter
[n
] ++;
167 #ifdef LOCAL_LABELS_DOLLAR
168 local_label_defined
[n
]=1;
170 colon (local_label_name (n
, 0));
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.
182 * Please always call this to create a new symbol.
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).
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 */
196 unsigned int name_length
;
197 char *preserved_copy_of_name
;
200 name_length
= strlen(name
) + 1; /* +1 for \0 */
201 obstack_grow(¬es
, name
, name_length
);
202 preserved_copy_of_name
= obstack_finish(¬es
);
203 symbolP
= (symbolS
*)obstack_alloc(¬es
, sizeof(symbolS
));
205 /* symbol must be born in some fixed state. This seems as good as any. */
206 memset(symbolP
, 0, sizeof(symbolS
));
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 */
216 S_SET_SEGMENT(symbolP
, segment
);
217 S_SET_VALUE(symbolP
, value
);
218 /* symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
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;
227 * Link to end of symbol chain.
229 symbol_append(symbolP
, symbol_lastP
, &symbol_rootP
, &symbol_lastP
);
231 obj_symbol_new_hook(symbolP
);
234 verify_symbol_chain(symbol_rootP
, symbol_lastP
);
244 * We have just seen "<name>:".
245 * Creates a struct symbol unless it already exists.
247 * Gripes if we are redefining a symbol incompatibly (and ignores it).
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. */
254 register symbolS
* symbolP
; /* symbol we are working with */
256 #ifdef LOCAL_LABELS_DOLLAR
257 /* Sun local labels go out of scope whenever a non-local symbol is defined. */
260 bzero((void *) local_label_defined
, sizeof(local_label_defined
));
263 #ifndef WORKING_DOT_WORD
264 if(new_broken_words
) {
265 struct broken_word
*a
;
269 extern md_short_jump_size
;
270 extern md_long_jump_size
;
272 possible_bytes
=md_short_jump_size
+ new_broken_words
* md_long_jump_size
;
274 frag_opcode
=frag_var(rs_broken_word
,
278 (symbolS
*) broken_words
,
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
;
287 frag_tmp
->fr_opcode
=frag_opcode
;
288 new_broken_words
= 0;
290 for(a
=broken_words
;a
&& a
->dispfrag
==0;a
=a
->next_broken_word
)
291 a
->dispfrag
=frag_tmp
;
294 if ((symbolP
= symbol_find(sym_name
)) != 0) {
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)
300 if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg
) == (N_UNDF
| N_EXT
)) &&
301 ((obstack_next_free(& frags
) - frag_now
->fr_literal
) == 0))
304 * If the old symbol is .comm and it has a size of zero,
305 * we override it with the new symbol value.
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 */
317 * Now check for undefined symbols
319 if (!S_IS_DEFINED(symbolP
)) {
320 if (S_GET_VALUE(symbolP
) == 0) {
321 symbolP
->sy_frag
= frag_now
;
323 symbolP
->sy_other
= const_flag
;
325 S_SET_VALUE(symbolP
, obstack_next_free(&frags
) - frag_now
->fr_literal
);
326 S_SET_SEGMENT(symbolP
, now_seg
);
329 #endif /* if we have one, it better be zero. */
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
339 * This only used to be allowed on VMS gas, but Sun cc
340 * on the sparc also depends on it.
342 /* char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
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
)))) {
349 * Select which of the 2 cases this is
351 if (now_seg
!= SEG_DATA
) {
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
358 if (S_GET_VALUE(symbolP
)
359 < ((unsigned) (obstack_next_free(& frags
) - frag_now
->fr_literal
))) {
361 obstack_next_free(& frags
) -
362 frag_now
->fr_literal
);
366 * It is a .comm/.lcomm being converted
367 * to initialized data.
369 symbolP
->sy_frag
= frag_now
;
371 symbolP
->sy_other
= const_flag
;
373 S_SET_VALUE(symbolP
, obstack_next_free(& frags
) - frag_now
->fr_literal
);
374 S_SET_SEGMENT(symbolP
, now_seg
); /* keep N_EXT bit */
378 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
380 segment_name(S_GET_SEGMENT(symbolP
)),
381 S_GET_VALUE(symbolP
));
383 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
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 */
390 } /* if the undefined symbol has no value */
392 as_fatal("Symbol %s already defined.", sym_name
);
393 } /* if this symbol is not yet defined */
396 symbolP
= symbol_new(sym_name
,
398 (valueT
)(obstack_next_free(&frags
)-frag_now
->fr_literal
),
401 S_SET_OTHER(symbolP
, const_flag
);
404 symbol_table_insert(symbolP
);
405 } /* if we have seen this symbol before */
412 * symbol_table_insert()
414 * Die if we can't insert the symbol.
418 void symbol_table_insert(symbolP
)
421 register char *error_string
;
424 know(S_GET_NAME(symbolP
));
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
);
430 } /* symbol_table_insert() */
433 * symbol_find_or_make()
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.
438 symbolS
*symbol_find_or_make(name
)
441 register symbolS
*symbolP
;
443 symbolP
= symbol_find(name
);
445 if (symbolP
== NULL
) {
446 symbolP
= symbol_make(name
);
448 symbol_table_insert(symbolP
);
449 } /* if symbol wasn't found */
452 } /* symbol_find_or_make() */
454 symbolS
*symbol_make(name
)
459 /* Let the machine description default it, e.g. for register names. */
460 symbolP
= md_undefined_symbol(name
);
463 symbolP
= symbol_new(name
,
467 } /* if md didn't build us a symbol */
470 } /* symbol_make() */
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.
481 symbolS
*symbol_find(name
)
484 #ifndef STRIP_UNDERSCORE
485 #define STRIP_UNDERSCORE 0
486 #endif /* STRIP_UNDERSCORE */
487 return symbol_find_base(name
, STRIP_UNDERSCORE
);
490 symbolS
*symbol_find_base(name
, strip_underscore
)
492 int strip_underscore
;
494 if(strip_underscore
&& *name
== '_') name
++;
495 return ( (symbolS
*) hash_find( sy_hash
, name
));
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.
506 /* Link symbol ADDME after symbol TARGET in the chain. */
507 void symbol_append(addme
, target
, rootPP
, lastPP
)
513 if (target
== NULL
) {
514 know(*rootPP
== NULL
);
515 know(*lastPP
== NULL
);
519 } /* if the list is empty */
521 if (target
->sy_next
!= NULL
) {
522 #ifdef SYMBOLS_NEED_BACKPOINTERS
523 target
->sy_next
->sy_previous
= addme
;
524 #endif /* SYMBOLS_NEED_BACKPOINTERS */
526 know(*lastPP
== target
);
528 } /* if we have a next */
530 addme
->sy_next
= target
->sy_next
;
531 target
->sy_next
= addme
;
533 #ifdef SYMBOLS_NEED_BACKPOINTERS
534 addme
->sy_previous
= target
;
535 #endif /* SYMBOLS_NEED_BACKPOINTERS */
538 verify_symbol_chain(*rootPP
, *lastPP
);
542 } /* symbol_append() */
544 #ifdef SYMBOLS_NEED_BACKPOINTERS
545 /* Remove SYMBOLP from the list. */
546 void symbol_remove(symbolP
, rootPP
, lastPP
)
551 if (symbolP
== *rootPP
) {
552 *rootPP
= symbolP
->sy_next
;
553 } /* if it was the root */
555 if (symbolP
== *lastPP
) {
556 *lastPP
= symbolP
->sy_previous
;
557 } /* if it was the tail */
559 if (symbolP
->sy_next
!= NULL
) {
560 symbolP
->sy_next
->sy_previous
= symbolP
->sy_previous
;
563 if (symbolP
->sy_previous
!= NULL
) {
564 symbolP
->sy_previous
->sy_next
= symbolP
->sy_next
;
568 verify_symbol_chain(*rootPP
, *lastPP
);
572 } /* symbol_remove() */
574 /* Set the chain pointers of SYMBOL to null. */
575 void symbol_clear_list_pointers(symbolP
)
578 symbolP
->sy_next
= NULL
;
579 symbolP
->sy_previous
= NULL
;
580 } /* symbol_clear_list_pointers() */
582 /* Link symbol ADDME before symbol TARGET in the chain. */
583 void symbol_insert(addme
, target
, rootPP
, lastPP
)
589 if (target
->sy_previous
!= NULL
) {
590 target
->sy_previous
->sy_next
= addme
;
592 know(*rootPP
== target
);
596 addme
->sy_previous
= target
->sy_previous
;
597 target
->sy_previous
= addme
;
598 addme
->sy_next
= target
;
601 verify_symbol_chain(*rootPP
, *lastPP
);
605 } /* symbol_insert() */
606 #endif /* SYMBOLS_NEED_BACKPOINTERS */
608 void verify_symbol_chain(rootP
, lastP
)
612 symbolS
*symbolP
= rootP
;
614 if (symbolP
== NULL
) {
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);
623 know(symbolP == rootP);
624 }$*/ /* both directions */
625 know(symbolP
->sy_next
->sy_previous
== symbolP
);
626 #else /* SYMBOLS_NEED_BACKPOINTERS */
628 #endif /* SYMBOLS_NEED_BACKPOINTERS */
629 } /* verify pointers */
631 know(lastP
== symbolP
);
634 } /* verify_symbol_chain() */
This page took 0.044895 seconds and 4 git commands to generate.