This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / gnu / usr.bin / as / symbols.c
CommitLineData
7b374118 1/* symbols.c -symbol table-
7b374118 2
94f74247
PR
3 Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#ifndef lint
22static char rcsid[] = "$Id: symbols.c,v 1.3 1993/10/02 20:57:56 pk Exp $";
23#endif
7b374118
NW
24
25#include "as.h"
94f74247 26
7b374118 27#include "obstack.h" /* For "symbols.h" */
94f74247 28#include "subsegs.h"
7b374118
NW
29
30#ifndef WORKING_DOT_WORD
31extern int new_broken_words;
32#endif
7b374118
NW
33
34static
94f74247
PR
35 struct hash_control *
36 sy_hash; /* symbol-name => struct symbol pointer */
7b374118 37
94f74247 38/* Below are commented in "symbols.h". */
7b374118
NW
39unsigned int local_bss_counter;
40symbolS * symbol_rootP;
41symbolS * symbol_lastP;
42symbolS abs_symbol;
7b374118 43
94f74247
PR
44symbolS* dot_text_symbol;
45symbolS* dot_data_symbol;
46symbolS* dot_bss_symbol;
7b374118 47
94f74247 48struct obstack notes;
7b374118
NW
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
69typedef short unsigned int
94f74247 70 local_label_countT;
7b374118
NW
71
72static local_label_countT
94f74247 73 local_label_counter[10];
7b374118
NW
74
75static /* Returned to caller, then copied. */
94f74247 76 char symbol_name_build[12]; /* used for created names ("4f") */
7b374118 77
94f74247 78#ifdef LOCAL_LABELS_DOLLAR
7b374118
NW
79int local_label_defined[10];
80#endif
81
82\f
83void
94f74247 84 symbol_begin()
7b374118 85{
94f74247
PR
86 symbol_lastP = NULL;
87 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
88 sy_hash = hash_new();
89 memset((char *)(& abs_symbol), '\0', sizeof(abs_symbol));
90 S_SET_SEGMENT(&abs_symbol, SEG_ABSOLUTE); /* Can't initialise a union. Sigh. */
91 memset((char *)(local_label_counter), '\0', sizeof(local_label_counter) );
92 local_bss_counter = 0;
7b374118
NW
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
101char * /* Return local label name. */
94f74247
PR
102 local_label_name(n, augend)
103register int n; /* we just saw "n:", "nf" or "nb" : n a digit */
104register int augend; /* 0 for nb, 1 for n:, nf */
7b374118 105{
94f74247
PR
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 ++ = 1; /* ^A */
114 * p ++ = 'L';
115 * p ++ = n + '0'; /* Make into ASCII */
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() */
7b374118
NW
135
136
94f74247
PR
137void local_colon (n)
138int n; /* just saw "n:" */
7b374118 139{
94f74247
PR
140 local_label_counter[n] ++;
141#ifdef LOCAL_LABELS_DOLLAR
142 local_label_defined[n]=1;
7b374118 143#endif
94f74247 144 colon (local_label_name (n, 0));
7b374118
NW
145}
146\f
147/*
148 * symbol_new()
149 *
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.
154 *
155 *
156 * Please always call this to create a new symbol.
157 *
158 * Changes since 1985: Symbol names may not contain '\0'. Sigh.
94f74247
PR
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).
7b374118
NW
162 */
163
94f74247
PR
164symbolS *symbol_new(name, segment, value, frag)
165char *name; /* It is copied, the caller can destroy/modify */
166segT segment; /* Segment identifier (SEG_<something>) */
167long value; /* Symbol value */
168fragS *frag; /* Associated fragment */
7b374118 169{
94f74247
PR
170 unsigned int name_length;
171 char *preserved_copy_of_name;
172 symbolS *symbolP;
173
174 name_length = strlen(name) + 1; /* +1 for \0 */
175 obstack_grow(&notes, name, name_length);
176 preserved_copy_of_name = obstack_finish(&notes);
177 symbolP = (symbolS *) obstack_alloc(&notes, sizeof(symbolS));
178
179 /* symbol must be born in some fixed state. This seems as good as any. */
180 memset(symbolP, 0, sizeof(symbolS));
181
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 */
189
190 S_SET_SEGMENT(symbolP, segment);
191 S_SET_VALUE(symbolP, value);
192 /* symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
193
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;
199
200 /*
201 * Link to end of symbol chain.
202 */
203 symbol_append(symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
204
205 obj_symbol_new_hook(symbolP);
206
207#ifdef DEBUG
208 /* verify_symbol_chain(symbol_rootP, symbol_lastP); */
209#endif /* DEBUG */
210
211 return(symbolP);
212} /* symbol_new() */
213
7b374118
NW
214\f
215/*
216 * colon()
217 *
218 * We have just seen "<name>:".
219 * Creates a struct symbol unless it already exists.
220 *
221 * Gripes if we are redefining a symbol incompatibly (and ignores it).
222 *
223 */
94f74247
PR
224void colon(sym_name) /* just seen "x:" - rattle symbols & frags */
225register char * sym_name; /* symbol name, as a cannonical string */
226/* We copy this string: OK to alter later. */
7b374118 227{
94f74247
PR
228 register symbolS * symbolP; /* symbol we are working with */
229
230#ifdef LOCAL_LABELS_DOLLAR
231 /* Sun local labels go out of scope whenever a non-local symbol is defined. */
232
233 if (*sym_name != 'L')
234 memset((void *) local_label_defined, '\0', sizeof(local_label_defined));
7b374118 235#endif
94f74247 236
7b374118 237#ifndef WORKING_DOT_WORD
94f74247
PR
238 if (new_broken_words) {
239 struct broken_word *a;
240 int possible_bytes;
241 fragS *frag_tmp;
242 char *frag_opcode;
243
244 extern const md_short_jump_size;
245 extern const md_long_jump_size;
246 possible_bytes=md_short_jump_size + new_broken_words * md_long_jump_size;
247
248 frag_tmp=frag_now;
249 frag_opcode=frag_var(rs_broken_word,
250 possible_bytes,
251 possible_bytes,
252 (relax_substateT) 0,
253 (symbolS *) broken_words,
254 0L,
255 NULL);
256
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;
261 know(frag_tmp);
262 frag_tmp->fr_opcode=frag_opcode;
263 new_broken_words = 0;
264
265 for (a=broken_words;a && a->dispfrag == 0;a=a->next_broken_word)
266 a->dispfrag=frag_tmp;
267 }
7b374118 268#endif
94f74247
PR
269 if ((symbolP = symbol_find(sym_name)) != 0) {
270#ifdef OBJ_VMS
271 /*
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)
274 */
275 if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg) == (N_UNDF | N_EXT)) &&
276 ((obstack_next_free(& frags) - frag_now->fr_literal) == 0))
277 return;
278 /*
279 * If the old symbol is .comm and it has a size of zero,
280 * we override it with the new symbol value.
281 */
282 if (S_IS_EXTERNAL(symbolP) && S_IS_DEFINED(symbolP)
283 && (S_GET_VALUE(symbolP) == 0)) {
284 symbolP->sy_frag = frag_now;
285 S_GET_OTHER(symbolP) = const_flag;
286 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
287 symbolP->sy_symbol.n_type |=
288 SEGMENT_TO_SYMBOL_TYPE((int) now_seg); /* keep N_EXT bit */
289 return;
290 }
291#endif /* OBJ_VMS */
292 /*
293 * Now check for undefined symbols
294 */
295 if (!S_IS_DEFINED(symbolP)) {
296 if (S_GET_VALUE(symbolP) == 0) {
297 symbolP->sy_frag = frag_now;
298#ifdef OBJ_VMS
299 S_GET_OTHER(symbolP) = const_flag;
7b374118 300#endif
94f74247
PR
301 S_SET_VALUE(symbolP, obstack_next_free(&frags) - frag_now->fr_literal);
302 S_SET_SEGMENT(symbolP, now_seg);
303#ifdef N_UNDF
304 know(N_UNDF == 0);
305#endif /* if we have one, it better be zero. */
306
7b374118
NW
307 } else {
308 /*
94f74247
PR
309 * There are still several cases to check:
310 * A .comm/.lcomm symbol being redefined as
311 * initialized data is OK
312 * A .comm/.lcomm symbol being redefined with
313 * a larger size is also OK
314 *
315 * This only used to be allowed on VMS gas, but Sun cc
316 * on the sparc also depends on it.
7b374118 317 */
94f74247
PR
318 /* char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
319#ifdef MANY_SEGMENTS
320#define SEG_BSS SEG_E2
321#define SEG_DATA SEG_E1
7b374118 322#endif
94f74247
PR
323
324 if (((!S_IS_DEBUG(symbolP) && !S_IS_DEFINED(symbolP) && S_IS_EXTERNAL(symbolP))
325 || (S_GET_SEGMENT(symbolP) == SEG_BSS))
326 && ((now_seg == SEG_DATA)
327 || (now_seg == S_GET_SEGMENT(symbolP)))) {
328 /*
329 * Select which of the 2 cases this is
330 */
331 if (now_seg != SEG_DATA) {
332 /*
333 * New .comm for prev .comm symbol.
334 * If the new size is larger we just
335 * change its value. If the new size
336 * is smaller, we ignore this symbol
337 */
338 if (S_GET_VALUE(symbolP)
339 < ((unsigned) (obstack_next_free(& frags) - frag_now->fr_literal))) {
340 S_SET_VALUE(symbolP,
341 obstack_next_free(& frags) -
342 frag_now->fr_literal);
343 }
344 } else {
345 /*
346 * It is a .comm/.lcomm being converted
347 * to initialized data.
348 */
349 symbolP->sy_frag = frag_now;
350#ifdef OBJ_VMS
351 S_GET_OTHER(symbolP) = const_flag;
352#endif /* OBJ_VMS */
353 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
354 S_SET_SEGMENT(symbolP, now_seg); /* keep N_EXT bit */
355 }
356 } else {
357#ifdef OBJ_COFF
358 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
359 sym_name,
360 segment_name(S_GET_SEGMENT(symbolP)),
361 S_GET_VALUE(symbolP));
362#else /* OBJ_COFF */
363 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
364 sym_name,
365 segment_name(S_GET_SEGMENT(symbolP)),
366 S_GET_OTHER(symbolP), S_GET_DESC(symbolP),
367 S_GET_VALUE(symbolP));
368#endif /* OBJ_COFF */
369 }
370 } /* if the undefined symbol has no value */
371 } else
372 {
373 /* Don't blow up if the definition is the same */
374 if (!(frag_now == symbolP->sy_frag
375 && S_GET_VALUE(symbolP) == obstack_next_free(&frags) - frag_now->fr_literal
376 && S_GET_SEGMENT(symbolP) == now_seg) )
377 as_fatal("Symbol %s already defined.", sym_name);
378 } /* if this symbol is not yet defined */
379
380 } else {
381 symbolP = symbol_new(sym_name,
382 now_seg,
383 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
384 frag_now);
385#ifdef OBJ_VMS
386 S_SET_OTHER(symbolP, const_flag);
387#endif /* OBJ_VMS */
388
389 symbol_table_insert(symbolP);
390 } /* if we have seen this symbol before */
391
392 return;
393} /* colon() */
7b374118
NW
394
395\f
396/*
397 * symbol_table_insert()
398 *
399 * Die if we can't insert the symbol.
400 *
401 */
402
94f74247
PR
403void symbol_table_insert(symbolP)
404symbolS *symbolP;
7b374118 405{
94f74247
PR
406 register char *error_string;
407
408 know(symbolP);
409 know(S_GET_NAME(symbolP));
410
411 if (*(error_string = hash_jam(sy_hash, S_GET_NAME(symbolP), (char *)symbolP))) {
412 as_fatal("Inserting \"%s\" into symbol table failed: %s",
413 S_GET_NAME(symbolP), error_string);
414 } /* on error */
415} /* symbol_table_insert() */
7b374118
NW
416\f
417/*
418 * symbol_find_or_make()
419 *
420 * If a symbol name does not exist, create it as undefined, and insert
421 * it into the symbol table. Return a pointer to it.
422 */
94f74247
PR
423symbolS *symbol_find_or_make(name)
424char *name;
7b374118 425{
94f74247
PR
426 register symbolS *symbolP;
427
428 symbolP = symbol_find(name);
429
430 if (symbolP == NULL) {
431 symbolP = symbol_make(name);
432
433 symbol_table_insert(symbolP);
434 } /* if symbol wasn't found */
435
436 return(symbolP);
437} /* symbol_find_or_make() */
438
439symbolS *symbol_make(name)
440char *name;
441{
442 symbolS *symbolP;
443
444 /* Let the machine description default it, e.g. for register names. */
445 symbolP = md_undefined_symbol(name);
446
447 if (!symbolP) {
448 symbolP = symbol_new(name,
449 SEG_UNKNOWN,
450 0,
451 &zero_address_frag);
452 } /* if md didn't build us a symbol */
453
454 return(symbolP);
455} /* symbol_make() */
7b374118
NW
456
457/*
458 * symbol_find()
459 *
460 * Implement symbol table lookup.
461 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
462 * Out: NULL if the name was not in the symbol table, else the address
463 * of a struct symbol associated with that name.
464 */
465
94f74247
PR
466symbolS *symbol_find(name)
467char *name;
468{
469#ifdef STRIP_UNDERSCORE
470 return(symbol_find_base(name, 1));
471#else /* STRIP_UNDERSCORE */
472 return(symbol_find_base(name, 0));
473#endif /* STRIP_UNDERSCORE */
474} /* symbol_find() */
475
476symbolS *symbol_find_base(name, strip_underscore)
477char *name;
478int strip_underscore;
7b374118 479{
94f74247
PR
480 if (strip_underscore && *name == '_') name++;
481 return ( (symbolS *) hash_find( sy_hash, name ));
7b374118
NW
482}
483
94f74247
PR
484/*
485 * Once upon a time, symbols were kept in a singly linked list. At
486 * least coff needs to be able to rearrange them from time to time, for
487 * which a doubly linked list is much more convenient. Loic did these
488 * as macros which seemed dangerous to me so they're now functions.
489 * xoxorich.
490 */
491
492/* Link symbol ADDME after symbol TARGET in the chain. */
493void symbol_append(addme, target, rootPP, lastPP)
494symbolS *addme;
495symbolS *target;
496symbolS **rootPP;
497symbolS **lastPP;
498{
499 if (target == NULL) {
500 know(*rootPP == NULL);
501 know(*lastPP == NULL);
502 *rootPP = addme;
503 *lastPP = addme;
504 return;
505 } /* if the list is empty */
506
507 if (target->sy_next != NULL) {
508#ifdef SYMBOLS_NEED_BACKPOINTERS
509 target->sy_next->sy_previous = addme;
510#endif /* SYMBOLS_NEED_BACKPOINTERS */
511 } else {
512 know(*lastPP == target);
513 *lastPP = addme;
514 } /* if we have a next */
515
516 addme->sy_next = target->sy_next;
517 target->sy_next = addme;
518
519#ifdef SYMBOLS_NEED_BACKPOINTERS
520 addme->sy_previous = target;
521#endif /* SYMBOLS_NEED_BACKPOINTERS */
522
523#ifdef DEBUG
524 /* verify_symbol_chain(*rootPP, *lastPP); */
525#endif /* DEBUG */
526
527 return;
528} /* symbol_append() */
529
530#ifdef SYMBOLS_NEED_BACKPOINTERS
531/* Remove SYMBOLP from the list. */
532void symbol_remove(symbolP, rootPP, lastPP)
533symbolS *symbolP;
534symbolS **rootPP;
535symbolS **lastPP;
536{
537 if (symbolP == *rootPP) {
538 *rootPP = symbolP->sy_next;
539 } /* if it was the root */
540
541 if (symbolP == *lastPP) {
542 *lastPP = symbolP->sy_previous;
543 } /* if it was the tail */
544
545 if (symbolP->sy_next != NULL) {
546 symbolP->sy_next->sy_previous = symbolP->sy_previous;
547 } /* if not last */
548
549 if (symbolP->sy_previous != NULL) {
550 symbolP->sy_previous->sy_next = symbolP->sy_next;
551 } /* if not first */
552
553#ifdef DEBUG
554 verify_symbol_chain(*rootPP, *lastPP);
555#endif /* DEBUG */
556
557 return;
558} /* symbol_remove() */
559
560/* Set the chain pointers of SYMBOL to null. */
561void symbol_clear_list_pointers(symbolP)
562symbolS *symbolP;
563{
564 symbolP->sy_next = NULL;
565 symbolP->sy_previous = NULL;
566} /* symbol_clear_list_pointers() */
567
568/* Link symbol ADDME before symbol TARGET in the chain. */
569void symbol_insert(addme, target, rootPP, lastPP)
570symbolS *addme;
571symbolS *target;
572symbolS **rootPP;
573symbolS **lastPP;
574{
575 if (target->sy_previous != NULL) {
576 target->sy_previous->sy_next = addme;
577 } else {
578 know(*rootPP == target);
579 *rootPP = addme;
580 } /* if not first */
581
582 addme->sy_previous = target->sy_previous;
583 target->sy_previous = addme;
584 addme->sy_next = target;
585
586#ifdef DEBUG
587 verify_symbol_chain(*rootPP, *lastPP);
588#endif /* DEBUG */
589
590 return;
591} /* symbol_insert() */
592#endif /* SYMBOLS_NEED_BACKPOINTERS */
593
594void verify_symbol_chain(rootP, lastP)
595symbolS *rootP;
596symbolS *lastP;
597{
598 symbolS *symbolP = rootP;
599
600 if (symbolP == NULL) {
601 return;
602 } /* empty chain */
603
604 for ( ; symbol_next(symbolP) != NULL; symbolP = symbol_next(symbolP)) {
605#ifdef SYMBOLS_NEED_BACKPOINTERS
606 /*$if (symbolP->sy_previous) {
607 know(symbolP->sy_previous->sy_next == symbolP);
608 } else {
609 know(symbolP == rootP);
610 }$*/ /* both directions */
611 know(symbolP->sy_next->sy_previous == symbolP);
612#else /* SYMBOLS_NEED_BACKPOINTERS */
613 ;
614#endif /* SYMBOLS_NEED_BACKPOINTERS */
615 } /* verify pointers */
616
617 know(lastP == symbolP);
618
619 return;
620} /* verify_symbol_chain() */
621
622
623/*
624 * decode name that may have been generated by local_label_name() above. If
625 * the name wasn't generated by local_label_name(), then return it unaltered.
626 * This is used for error messages.
627 */
628
629char *decode_local_label_name(s)
630char *s;
631{
632 char *symbol_decode;
633 int label_number;
634 /* int label_version; */
635 char *message_format = "\"%d\" (instance number %s of a local label)";
636
637 if (s[0] != 'L'
638 || s[2] != 1) {
639 return(s);
640 } /* not a local_label_name() generated name. */
641
642 label_number = s[1] - '0';
643
644 (void) sprintf(symbol_decode = obstack_alloc(&notes, strlen(s + 3) + strlen(message_format) + 10),
645 message_format, label_number, s + 3);
646
647 return(symbol_decode);
648} /* decode_local_label_name() */
649
650
651/*
652 * Local Variables:
653 * comment-column: 0
654 * fill-column: 131
655 * End:
656 */
7b374118 657
94f74247 658/* end of symbols.c */