White space and comments only. The devo tree prior to this delta is
[deliverable/binutils-gdb.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 #include "as.h"
21
22 #include "obstack.h" /* For "symbols.h" */
23 #include "subsegs.h"
24
25 #ifndef WORKING_DOT_WORD
26 extern int new_broken_words;
27 #endif
28 #ifdef VMS
29 extern char const_flag;
30 #endif
31
32 static
33 struct hash_control *
34 sy_hash; /* symbol-name => struct symbol pointer */
35
36 /* Below are commented in "symbols.h". */
37 unsigned int local_bss_counter;
38 symbolS * symbol_rootP;
39 symbolS * symbol_lastP;
40 symbolS abs_symbol;
41
42 symbolS* dot_text_symbol;
43 symbolS* dot_data_symbol;
44 symbolS* dot_bss_symbol;
45
46 struct obstack notes;
47
48 /*
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:.
53 *
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.
58 *
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
64 * numbers begin at 1.
65 */
66
67 typedef short unsigned int
68 local_label_countT;
69
70 static local_label_countT
71 local_label_counter[10];
72
73 static /* Returned to caller, then copied. */
74 char symbol_name_build[12]; /* used for created names ("4f") */
75
76 #ifdef LOCAL_LABELS_DOLLAR
77 int local_label_defined[10];
78 #endif
79
80 \f
81 void
82 symbol_begin()
83 {
84 symbol_lastP = NULL;
85 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
86 sy_hash = hash_new();
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;
91 }
92 \f
93 /*
94 * local_label_name()
95 *
96 * Caller must copy returned name: we re-use the area for the next name.
97 */
98
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 */
103 {
104 register char * p;
105 register char * q;
106 char symbol_name_temporary[10]; /* build up a number, BACKWARDS */
107
108 know( n >= 0 );
109 know( augend == 0 || augend == 1 );
110 p = symbol_name_build;
111 * p ++ = 'L';
112 * p ++ = n + '0'; /* Make into ASCII */
113 * p ++ = 1; /* ^A */
114 n = local_label_counter [ n ] + augend;
115 /* version number of this local label */
116 /*
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.
120 */
121 q = symbol_name_temporary;
122 for (*q++=0; n; q++) /* emits NOTHING if n starts as 0 */
123 {
124 know(n>0); /* We expect n > 0 always */
125 *q = n % 10 + '0';
126 n /= 10;
127 }
128 while (( * p ++ = * -- q ) != '\0') ;;
129
130 /* The label, as a '\0' ended string, starts at symbol_name_build. */
131 return(symbol_name_build);
132 } /* local_label_name() */
133
134
135 void local_colon (n)
136 int n; /* just saw "n:" */
137 {
138 local_label_counter [n] ++;
139 #ifdef LOCAL_LABELS_DOLLAR
140 local_label_defined[n]=1;
141 #endif
142 colon (local_label_name (n, 0));
143 }
144 \f
145 /*
146 * symbol_new()
147 *
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.
152 *
153 *
154 * Please always call this to create a new symbol.
155 *
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).
160 */
161
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 */
167 {
168 unsigned int name_length;
169 char *preserved_copy_of_name;
170 symbolS *symbolP;
171
172 name_length = strlen(name) + 1; /* +1 for \0 */
173 obstack_grow(&notes, name, name_length);
174 preserved_copy_of_name = obstack_finish(&notes);
175 symbolP = (symbolS *)obstack_alloc(&notes, sizeof(symbolS));
176
177 /* symbol must be born in some fixed state. This seems as good as any. */
178 memset(symbolP, 0, sizeof(symbolS));
179
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 */
187
188 S_SET_SEGMENT(symbolP, segment);
189 S_SET_VALUE(symbolP, value);
190 /* symbol_clear_list_pointers(symbolP); uneeded if symbol is born zeroed. */
191
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;
197
198 /*
199 * Link to end of symbol chain.
200 */
201 symbol_append(symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
202
203 obj_symbol_new_hook(symbolP);
204
205 #ifdef DEBUG
206 /* verify_symbol_chain(symbol_rootP, symbol_lastP); */
207 #endif /* DEBUG */
208
209 return(symbolP);
210 } /* symbol_new() */
211
212 \f
213 /*
214 * colon()
215 *
216 * We have just seen "<name>:".
217 * Creates a struct symbol unless it already exists.
218 *
219 * Gripes if we are redefining a symbol incompatibly (and ignores it).
220 *
221 */
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. */
225 {
226 register symbolS * symbolP; /* symbol we are working with */
227
228 #ifdef LOCAL_LABELS_DOLLAR
229 /* Sun local labels go out of scope whenever a non-local symbol is defined. */
230
231 if(*sym_name !='L')
232 bzero((void *) local_label_defined, sizeof(local_label_defined));
233 #endif
234
235 #ifndef WORKING_DOT_WORD
236 if(new_broken_words) {
237 struct broken_word *a;
238 int possible_bytes;
239 fragS *frag_tmp;
240 char *frag_opcode;
241
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;
245
246 frag_tmp=frag_now;
247 frag_opcode=frag_var(rs_broken_word,
248 possible_bytes,
249 possible_bytes,
250 (relax_substateT) 0,
251 (symbolS *) broken_words,
252 0L,
253 NULL);
254
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;
259 know(frag_tmp);
260 frag_tmp->fr_opcode=frag_opcode;
261 new_broken_words = 0;
262
263 for(a=broken_words;a && a->dispfrag==0;a=a->next_broken_word)
264 a->dispfrag=frag_tmp;
265 }
266 #endif
267 if ((symbolP = symbol_find(sym_name)) != 0) {
268 #ifdef VMS
269 /*
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)
272 */
273 if ((SEGMENT_TO_SYMBOL_TYPE((int) now_seg) == (N_UNDF | N_EXT)) &&
274 ((obstack_next_free(& frags) - frag_now->fr_literal) == 0))
275 return;
276 /*
277 * If the old symbol is .comm and it has a size of zero,
278 * we override it with the new symbol value.
279 */
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 */
286 return;
287 }
288 #endif /* VMS */
289 /*
290 * Now check for undefined symbols
291 */
292 if (!S_IS_DEFINED(symbolP)) {
293 if (S_GET_VALUE(symbolP) == 0) {
294 symbolP->sy_frag = frag_now;
295 #ifdef VMS
296 symbolP->sy_other = const_flag;
297 #endif
298 S_SET_VALUE(symbolP, obstack_next_free(&frags) - frag_now->fr_literal);
299 S_SET_SEGMENT(symbolP, now_seg);
300 #ifdef N_UNDF
301 know(N_UNDF == 0);
302 #endif /* if we have one, it better be zero. */
303
304 } else {
305 /*
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
311 *
312 * This only used to be allowed on VMS gas, but Sun cc
313 * on the sparc also depends on it.
314 */
315 /* char New_Type = SEGMENT_TO_SYMBOL_TYPE((int) now_seg); */
316 #ifdef MANY_SEGMENTS
317 #define SEG_BSS SEG_E2
318 #define SEG_DATA SEG_E1
319 #endif
320
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)))) {
325 /*
326 * Select which of the 2 cases this is
327 */
328 if (now_seg != SEG_DATA) {
329 /*
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
334 */
335 if (S_GET_VALUE(symbolP)
336 < ((unsigned) (obstack_next_free(& frags) - frag_now->fr_literal))) {
337 S_SET_VALUE(symbolP,
338 obstack_next_free(& frags) -
339 frag_now->fr_literal);
340 }
341 } else {
342 /*
343 * It is a .comm/.lcomm being converted
344 * to initialized data.
345 */
346 symbolP->sy_frag = frag_now;
347 #ifdef VMS
348 symbolP->sy_other = const_flag;
349 #endif /* VMS */
350 S_SET_VALUE(symbolP, obstack_next_free(& frags) - frag_now->fr_literal);
351 S_SET_SEGMENT(symbolP, now_seg); /* keep N_EXT bit */
352 }
353 } else {
354 #ifdef OBJ_COFF
355 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.",
356 sym_name,
357 segment_name(S_GET_SEGMENT(symbolP)),
358 S_GET_VALUE(symbolP));
359 #else /* OBJ_COFF */
360 as_fatal("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%d.",
361 sym_name,
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 */
366 }
367 } /* if the undefined symbol has no value */
368 } else
369 {
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 */
376
377 } else {
378 symbolP = symbol_new(sym_name,
379 now_seg,
380 (valueT)(obstack_next_free(&frags)-frag_now->fr_literal),
381 frag_now);
382 #ifdef VMS
383 S_SET_OTHER(symbolP, const_flag);
384 #endif /* VMS */
385
386 symbol_table_insert(symbolP);
387 } /* if we have seen this symbol before */
388
389 return;
390 } /* colon() */
391
392 \f
393 /*
394 * symbol_table_insert()
395 *
396 * Die if we can't insert the symbol.
397 *
398 */
399
400 void symbol_table_insert(symbolP)
401 symbolS *symbolP;
402 {
403 register char *error_string;
404
405 know(symbolP);
406 know(S_GET_NAME(symbolP));
407
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);
411 } /* on error */
412 } /* symbol_table_insert() */
413 \f
414 /*
415 * symbol_find_or_make()
416 *
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.
419 */
420 symbolS *symbol_find_or_make(name)
421 char *name;
422 {
423 register symbolS *symbolP;
424
425 symbolP = symbol_find(name);
426
427 if (symbolP == NULL) {
428 symbolP = symbol_make(name);
429
430 symbol_table_insert(symbolP);
431 } /* if symbol wasn't found */
432
433 return(symbolP);
434 } /* symbol_find_or_make() */
435
436 symbolS *symbol_make(name)
437 char *name;
438 {
439 symbolS *symbolP;
440
441 /* Let the machine description default it, e.g. for register names. */
442 symbolP = md_undefined_symbol(name);
443
444 if (!symbolP) {
445 symbolP = symbol_new(name,
446 SEG_UNKNOWN,
447 0,
448 &zero_address_frag);
449 } /* if md didn't build us a symbol */
450
451 return(symbolP);
452 } /* symbol_make() */
453
454 /*
455 * symbol_find()
456 *
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.
461 */
462
463 symbolS *symbol_find(name)
464 char *name;
465 {
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() */
472
473 symbolS *symbol_find_base(name, strip_underscore)
474 char *name;
475 int strip_underscore;
476 {
477 if(strip_underscore && *name == '_') name++;
478 return ( (symbolS *) hash_find( sy_hash, name ));
479 }
480
481 /*
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.
486 * xoxorich.
487 */
488
489 /* Link symbol ADDME after symbol TARGET in the chain. */
490 void symbol_append(addme, target, rootPP, lastPP)
491 symbolS *addme;
492 symbolS *target;
493 symbolS **rootPP;
494 symbolS **lastPP;
495 {
496 if (target == NULL) {
497 know(*rootPP == NULL);
498 know(*lastPP == NULL);
499 *rootPP = addme;
500 *lastPP = addme;
501 return;
502 } /* if the list is empty */
503
504 if (target->sy_next != NULL) {
505 #ifdef SYMBOLS_NEED_BACKPOINTERS
506 target->sy_next->sy_previous = addme;
507 #endif /* SYMBOLS_NEED_BACKPOINTERS */
508 } else {
509 know(*lastPP == target);
510 *lastPP = addme;
511 } /* if we have a next */
512
513 addme->sy_next = target->sy_next;
514 target->sy_next = addme;
515
516 #ifdef SYMBOLS_NEED_BACKPOINTERS
517 addme->sy_previous = target;
518 #endif /* SYMBOLS_NEED_BACKPOINTERS */
519
520 #ifdef DEBUG
521 /* verify_symbol_chain(*rootPP, *lastPP); */
522 #endif /* DEBUG */
523
524 return;
525 } /* symbol_append() */
526
527 #ifdef SYMBOLS_NEED_BACKPOINTERS
528 /* Remove SYMBOLP from the list. */
529 void symbol_remove(symbolP, rootPP, lastPP)
530 symbolS *symbolP;
531 symbolS **rootPP;
532 symbolS **lastPP;
533 {
534 if (symbolP == *rootPP) {
535 *rootPP = symbolP->sy_next;
536 } /* if it was the root */
537
538 if (symbolP == *lastPP) {
539 *lastPP = symbolP->sy_previous;
540 } /* if it was the tail */
541
542 if (symbolP->sy_next != NULL) {
543 symbolP->sy_next->sy_previous = symbolP->sy_previous;
544 } /* if not last */
545
546 if (symbolP->sy_previous != NULL) {
547 symbolP->sy_previous->sy_next = symbolP->sy_next;
548 } /* if not first */
549
550 #ifdef DEBUG
551 verify_symbol_chain(*rootPP, *lastPP);
552 #endif /* DEBUG */
553
554 return;
555 } /* symbol_remove() */
556
557 /* Set the chain pointers of SYMBOL to null. */
558 void symbol_clear_list_pointers(symbolP)
559 symbolS *symbolP;
560 {
561 symbolP->sy_next = NULL;
562 symbolP->sy_previous = NULL;
563 } /* symbol_clear_list_pointers() */
564
565 /* Link symbol ADDME before symbol TARGET in the chain. */
566 void symbol_insert(addme, target, rootPP, lastPP)
567 symbolS *addme;
568 symbolS *target;
569 symbolS **rootPP;
570 symbolS **lastPP;
571 {
572 if (target->sy_previous != NULL) {
573 target->sy_previous->sy_next = addme;
574 } else {
575 know(*rootPP == target);
576 *rootPP = addme;
577 } /* if not first */
578
579 addme->sy_previous = target->sy_previous;
580 target->sy_previous = addme;
581 addme->sy_next = target;
582
583 #ifdef DEBUG
584 verify_symbol_chain(*rootPP, *lastPP);
585 #endif /* DEBUG */
586
587 return;
588 } /* symbol_insert() */
589 #endif /* SYMBOLS_NEED_BACKPOINTERS */
590
591 void verify_symbol_chain(rootP, lastP)
592 symbolS *rootP;
593 symbolS *lastP;
594 {
595 symbolS *symbolP = rootP;
596
597 if (symbolP == NULL) {
598 return;
599 } /* empty chain */
600
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);
605 } else {
606 know(symbolP == rootP);
607 }$*/ /* both directions */
608 know(symbolP->sy_next->sy_previous == symbolP);
609 #else /* SYMBOLS_NEED_BACKPOINTERS */
610 ;
611 #endif /* SYMBOLS_NEED_BACKPOINTERS */
612 } /* verify pointers */
613
614 know(lastP == symbolP);
615
616 return;
617 } /* verify_symbol_chain() */
618
619
620 /*
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.
624 */
625
626 char *decode_local_label_name(s)
627 char *s;
628 {
629 char *symbol_decode;
630 int label_number;
631 /* int label_version; */
632 char *message_format = "\"%d\" (instance number %s of a local label)";
633
634 if (s[0] != 'L'
635 || s[2] != 1) {
636 return(s);
637 } /* not a local_label_name() generated name. */
638
639 label_number = s[1] - '0';
640
641 (void) sprintf(symbol_decode = obstack_alloc(&notes, strlen(s + 3) + strlen(message_format) + 10),
642 message_format, label_number, s + 3);
643
644 return(symbol_decode);
645 } /* decode_local_label_name() */
646
647
648 /*
649 * Local Variables:
650 * comment-column: 0
651 * fill-column: 131
652 * End:
653 */
654
655 /* end: symbols.c */
This page took 0.043574 seconds and 5 git commands to generate.