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