* Extensive changes to permit symbols to contain any expression
[deliverable/binutils-gdb.git] / gas / symbols.c
CommitLineData
fecd2382 1/* symbols.c -symbol table-
2b68b820 2 Copyright (C) 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
6efd877d 3
a39116f1 4 This file is part of GAS, the GNU Assembler.
6efd877d 5
a39116f1
RP
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.
6efd877d 10
a39116f1
RP
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.
6efd877d 15
a39116f1
RP
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. */
fecd2382 19
2b68b820
KR
20#define DEBUG
21
6efd877d
KR
22#include <ctype.h>
23
fecd2382
RP
24#include "as.h"
25
26#include "obstack.h" /* For "symbols.h" */
27#include "subsegs.h"
28
29#ifndef WORKING_DOT_WORD
30extern int new_broken_words;
31#endif
fecd2382 32
2b68b820
KR
33/* symbol-name => struct symbol pointer */
34static struct hash_control *sy_hash;
fecd2382 35
a39116f1 36/* Below are commented in "symbols.h". */
6efd877d
KR
37symbolS *symbol_rootP;
38symbolS *symbol_lastP;
39symbolS abs_symbol;
fecd2382 40
6efd877d
KR
41symbolS *dot_text_symbol;
42symbolS *dot_data_symbol;
43symbolS *dot_bss_symbol;
fecd2382 44
6efd877d 45struct obstack notes;
fecd2382 46
85825401 47static void fb_label_init PARAMS ((void));
fecd2382 48
fecd2382 49void
6efd877d 50symbol_begin ()
fecd2382 51{
6efd877d
KR
52 symbol_lastP = NULL;
53 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
54 sy_hash = hash_new ();
55 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2b68b820
KR
56#ifdef BFD_ASSEMBLER
57 abs_symbol.bsym = bfd_abs_section.symbol;
58#else
59 /* Can't initialise a union. Sigh. */
60 S_SET_SEGMENT (&abs_symbol, absolute_section);
61#endif
6efd877d
KR
62#ifdef LOCAL_LABELS_FB
63 fb_label_init ();
64#endif /* LOCAL_LABELS_FB */
fecd2382 65}
fecd2382 66
fecd2382
RP
67/*
68 * symbol_new()
69 *
70 * Return a pointer to a new symbol.
71 * Die if we can't make a new symbol.
72 * Fill in the symbol's values.
73 * Add symbol to end of symbol chain.
74 *
75 *
76 * Please always call this to create a new symbol.
77 *
78 * Changes since 1985: Symbol names may not contain '\0'. Sigh.
79 * 2nd argument is now a SEG rather than a TYPE. The mapping between
80 * segments and types is mostly encapsulated herein (actually, we inherit it
81 * from macros in struc-symbol.h).
82 */
83
6efd877d
KR
84symbolS *
85symbol_new (name, segment, value, frag)
2b68b820 86 CONST char *name; /* It is copied, the caller can destroy/modify */
6efd877d 87 segT segment; /* Segment identifier (SEG_<something>) */
2b68b820 88 valueT value; /* Symbol value */
6efd877d 89 fragS *frag; /* Associated fragment */
fecd2382 90{
6efd877d
KR
91 unsigned int name_length;
92 char *preserved_copy_of_name;
93 symbolS *symbolP;
94
95 name_length = strlen (name) + 1; /* +1 for \0 */
96 obstack_grow (&notes, name, name_length);
97 preserved_copy_of_name = obstack_finish (&notes);
2b68b820
KR
98#ifdef STRIP_UNDERSCORE
99 if (preserved_copy_of_name[0] == '_')
100 preserved_copy_of_name++;
101#endif
6efd877d
KR
102 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
103
104 /* symbol must be born in some fixed state. This seems as good as any. */
105 memset (symbolP, 0, sizeof (symbolS));
106
2b68b820
KR
107
108#ifdef BFD_ASSEMBLER
109 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
110 assert (symbolP->bsym != 0);
111 symbolP->bsym->udata = (PTR) symbolP;
112#endif
6efd877d 113 S_SET_NAME (symbolP, preserved_copy_of_name);
6efd877d
KR
114
115 S_SET_SEGMENT (symbolP, segment);
116 S_SET_VALUE (symbolP, value);
2b68b820 117 symbol_clear_list_pointers(symbolP);
6efd877d
KR
118
119 symbolP->sy_frag = frag;
2b68b820 120#ifndef BFD_ASSEMBLER
6efd877d
KR
121 symbolP->sy_number = ~0;
122 symbolP->sy_name_offset = ~0;
2b68b820 123#endif
6efd877d
KR
124
125 /*
2b68b820
KR
126 * Link to end of symbol chain.
127 */
6efd877d
KR
128 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
129
130 obj_symbol_new_hook (symbolP);
131
fecd2382 132#ifdef DEBUG
2b68b820 133 verify_symbol_chain(symbol_rootP, symbol_lastP);
fecd2382 134#endif /* DEBUG */
fecd2382 135
2b68b820
KR
136 return symbolP;
137}
fecd2382 138\f
6efd877d 139
fecd2382
RP
140/*
141 * colon()
142 *
143 * We have just seen "<name>:".
144 * Creates a struct symbol unless it already exists.
145 *
146 * Gripes if we are redefining a symbol incompatibly (and ignores it).
147 *
148 */
6efd877d
KR
149void
150colon (sym_name) /* just seen "x:" - rattle symbols & frags */
151 register char *sym_name; /* symbol name, as a cannonical string */
152 /* We copy this string: OK to alter later. */
fecd2382 153{
6efd877d
KR
154 register symbolS *symbolP; /* symbol we are working with */
155
fecd2382 156#ifdef LOCAL_LABELS_DOLLAR
2b68b820
KR
157 /* Sun local labels go out of scope whenever a non-local symbol is
158 defined. */
6efd877d
KR
159
160 if (*sym_name != 'L')
161 dollar_label_clear ();
162#endif /* LOCAL_LABELS_DOLLAR */
163
fecd2382 164#ifndef WORKING_DOT_WORD
6efd877d
KR
165 if (new_broken_words)
166 {
167 struct broken_word *a;
168 int possible_bytes;
169 fragS *frag_tmp;
170 char *frag_opcode;
171
2b68b820
KR
172 extern const int md_short_jump_size;
173 extern const int md_long_jump_size;
174 possible_bytes = (md_short_jump_size
175 + new_broken_words * md_long_jump_size);
6efd877d
KR
176
177 frag_tmp = frag_now;
178 frag_opcode = frag_var (rs_broken_word,
179 possible_bytes,
180 possible_bytes,
181 (relax_substateT) 0,
182 (symbolS *) broken_words,
183 0L,
184 NULL);
185
186 /* We want to store the pointer to where to insert the jump table in the
2b68b820
KR
187 fr_opcode of the rs_broken_word frag. This requires a little
188 hackery. */
189 while (frag_tmp
190 && (frag_tmp->fr_type != rs_broken_word
191 || frag_tmp->fr_opcode))
6efd877d
KR
192 frag_tmp = frag_tmp->fr_next;
193 know (frag_tmp);
194 frag_tmp->fr_opcode = frag_opcode;
195 new_broken_words = 0;
196
197 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
198 a->dispfrag = frag_tmp;
199 }
200#endif /* WORKING_DOT_WORD */
201
202 if ((symbolP = symbol_find (sym_name)) != 0)
203 {
2b68b820
KR
204#ifdef RESOLVE_SYMBOL_REDEFINITION
205 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
6efd877d 206 return;
2b68b820 207#endif
6efd877d 208 /*
2b68b820
KR
209 * Now check for undefined symbols
210 */
6efd877d
KR
211 if (!S_IS_DEFINED (symbolP))
212 {
213 if (S_GET_VALUE (symbolP) == 0)
214 {
215 symbolP->sy_frag = frag_now;
2b68b820
KR
216#ifdef OBJ_VMS
217 S_GET_OTHER(symbolP) = const_flag;
fecd2382 218#endif
2b68b820 219 S_SET_VALUE (symbolP, (valueT) ((char*)obstack_next_free (&frags) - frag_now->fr_literal));
6efd877d 220 S_SET_SEGMENT (symbolP, now_seg);
fecd2382 221#ifdef N_UNDF
6efd877d 222 know (N_UNDF == 0);
fecd2382 223#endif /* if we have one, it better be zero. */
6efd877d
KR
224
225 }
226 else
227 {
228 /*
2b68b820
KR
229 * There are still several cases to check:
230 * A .comm/.lcomm symbol being redefined as
231 * initialized data is OK
232 * A .comm/.lcomm symbol being redefined with
233 * a larger size is also OK
234 *
235 * This only used to be allowed on VMS gas, but Sun cc
236 * on the sparc also depends on it.
237 */
238
239 if (((!S_IS_DEBUG (symbolP)
240 && !S_IS_DEFINED (symbolP)
241 && S_IS_EXTERNAL (symbolP))
242 || S_GET_SEGMENT (symbolP) == bss_section)
243 && (now_seg == data_section
244 || now_seg == S_GET_SEGMENT (symbolP)))
6efd877d
KR
245 {
246 /*
2b68b820
KR
247 * Select which of the 2 cases this is
248 */
249 if (now_seg != data_section)
6efd877d
KR
250 {
251 /*
2b68b820
KR
252 * New .comm for prev .comm symbol.
253 * If the new size is larger we just
254 * change its value. If the new size
255 * is smaller, we ignore this symbol
256 */
6efd877d 257 if (S_GET_VALUE (symbolP)
2b68b820 258 < ((unsigned) frag_now_fix ()))
6efd877d 259 {
2b68b820 260 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d
KR
261 }
262 }
263 else
264 {
85825401
ILT
265 /* It is a .comm/.lcomm being converted to initialized
266 data. */
6efd877d 267 symbolP->sy_frag = frag_now;
2b68b820
KR
268#ifdef OBJ_VMS
269 S_GET_OTHER(symbolP) = const_flag;
270#endif /* OBJ_VMS */
271 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d
KR
272 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
273 }
274 }
275 else
276 {
2b68b820
KR
277#if defined (S_GET_OTHER) && defined (S_GET_DESC)
278 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
6efd877d
KR
279 sym_name,
280 segment_name (S_GET_SEGMENT (symbolP)),
2b68b820
KR
281 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
282 (long) S_GET_VALUE (symbolP));
283#else
284 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.",
6efd877d
KR
285 sym_name,
286 segment_name (S_GET_SEGMENT (symbolP)),
6efd877d 287 S_GET_VALUE (symbolP));
2b68b820 288#endif
6efd877d
KR
289 }
290 } /* if the undefined symbol has no value */
291 }
292 else
293 {
294 /* Don't blow up if the definition is the same */
295 if (!(frag_now == symbolP->sy_frag
2b68b820 296 && S_GET_VALUE (symbolP) == (char*)obstack_next_free (&frags) - frag_now->fr_literal
6efd877d
KR
297 && S_GET_SEGMENT (symbolP) == now_seg))
298 as_fatal ("Symbol %s already defined.", sym_name);
299 } /* if this symbol is not yet defined */
300
301 }
302 else
303 {
304 symbolP = symbol_new (sym_name,
305 now_seg,
2b68b820 306 (valueT) ((char*)obstack_next_free (&frags) - frag_now->fr_literal),
6efd877d 307 frag_now);
2b68b820 308#ifdef OBJ_VMS
6efd877d 309 S_SET_OTHER (symbolP, const_flag);
2b68b820 310#endif /* OBJ_VMS */
fecd2382 311
6efd877d
KR
312 symbol_table_insert (symbolP);
313 } /* if we have seen this symbol before */
1e9cf565
ILT
314
315#ifdef tc_frob_label
316 tc_frob_label (symbolP);
2b68b820 317#endif
6efd877d
KR
318
319 return;
320} /* colon() */
fecd2382 321\f
6efd877d 322
fecd2382
RP
323/*
324 * symbol_table_insert()
325 *
326 * Die if we can't insert the symbol.
327 *
328 */
329
6efd877d
KR
330void
331symbol_table_insert (symbolP)
332 symbolS *symbolP;
fecd2382 333{
6efd877d
KR
334 register char *error_string;
335
336 know (symbolP);
337 know (S_GET_NAME (symbolP));
338
339 if (*(error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (char *) symbolP)))
340 {
341 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
342 S_GET_NAME (symbolP), error_string);
343 } /* on error */
344} /* symbol_table_insert() */
fecd2382
RP
345\f
346/*
347 * symbol_find_or_make()
348 *
349 * If a symbol name does not exist, create it as undefined, and insert
350 * it into the symbol table. Return a pointer to it.
351 */
6efd877d
KR
352symbolS *
353symbol_find_or_make (name)
354 char *name;
fecd2382 355{
6efd877d
KR
356 register symbolS *symbolP;
357
358 symbolP = symbol_find (name);
359
360 if (symbolP == NULL)
361 {
362 symbolP = symbol_make (name);
363
364 symbol_table_insert (symbolP);
365 } /* if symbol wasn't found */
366
367 return (symbolP);
368} /* symbol_find_or_make() */
369
370symbolS *
371symbol_make (name)
2b68b820 372 CONST char *name;
fecd2382 373{
6efd877d
KR
374 symbolS *symbolP;
375
376 /* Let the machine description default it, e.g. for register names. */
2b68b820 377 symbolP = md_undefined_symbol ((char *) name);
6efd877d
KR
378
379 if (!symbolP)
2b68b820 380 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
6efd877d
KR
381
382 return (symbolP);
383} /* symbol_make() */
fecd2382
RP
384
385/*
386 * symbol_find()
6efd877d 387 *
fecd2382
RP
388 * Implement symbol table lookup.
389 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
390 * Out: NULL if the name was not in the symbol table, else the address
391 * of a struct symbol associated with that name.
392 */
393
6efd877d
KR
394symbolS *
395symbol_find (name)
2b68b820 396 CONST char *name;
fecd2382 397{
a6c6eaf8 398#ifdef STRIP_UNDERSCORE
6efd877d 399 return (symbol_find_base (name, 1));
a6c6eaf8 400#else /* STRIP_UNDERSCORE */
6efd877d 401 return (symbol_find_base (name, 0));
fecd2382 402#endif /* STRIP_UNDERSCORE */
6efd877d 403} /* symbol_find() */
fecd2382 404
6efd877d
KR
405symbolS *
406symbol_find_base (name, strip_underscore)
2b68b820 407 CONST char *name;
6efd877d 408 int strip_underscore;
fecd2382 409{
6efd877d
KR
410 if (strip_underscore && *name == '_')
411 name++;
412 return ((symbolS *) hash_find (sy_hash, name));
fecd2382
RP
413}
414
415/*
416 * Once upon a time, symbols were kept in a singly linked list. At
417 * least coff needs to be able to rearrange them from time to time, for
418 * which a doubly linked list is much more convenient. Loic did these
419 * as macros which seemed dangerous to me so they're now functions.
420 * xoxorich.
421 */
422
423/* Link symbol ADDME after symbol TARGET in the chain. */
6efd877d
KR
424void
425symbol_append (addme, target, rootPP, lastPP)
426 symbolS *addme;
427 symbolS *target;
428 symbolS **rootPP;
429 symbolS **lastPP;
fecd2382 430{
6efd877d
KR
431 if (target == NULL)
432 {
433 know (*rootPP == NULL);
434 know (*lastPP == NULL);
435 *rootPP = addme;
436 *lastPP = addme;
437 return;
438 } /* if the list is empty */
439
440 if (target->sy_next != NULL)
441 {
fecd2382 442#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 443 target->sy_next->sy_previous = addme;
fecd2382 444#endif /* SYMBOLS_NEED_BACKPOINTERS */
6efd877d
KR
445 }
446 else
447 {
448 know (*lastPP == target);
449 *lastPP = addme;
450 } /* if we have a next */
451
452 addme->sy_next = target->sy_next;
453 target->sy_next = addme;
454
fecd2382 455#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 456 addme->sy_previous = target;
fecd2382 457#endif /* SYMBOLS_NEED_BACKPOINTERS */
2b68b820 458}
fecd2382
RP
459
460#ifdef SYMBOLS_NEED_BACKPOINTERS
461/* Remove SYMBOLP from the list. */
6efd877d
KR
462void
463symbol_remove (symbolP, rootPP, lastPP)
464 symbolS *symbolP;
465 symbolS **rootPP;
466 symbolS **lastPP;
fecd2382 467{
6efd877d
KR
468 if (symbolP == *rootPP)
469 {
470 *rootPP = symbolP->sy_next;
471 } /* if it was the root */
472
473 if (symbolP == *lastPP)
474 {
475 *lastPP = symbolP->sy_previous;
476 } /* if it was the tail */
477
478 if (symbolP->sy_next != NULL)
479 {
480 symbolP->sy_next->sy_previous = symbolP->sy_previous;
481 } /* if not last */
482
483 if (symbolP->sy_previous != NULL)
484 {
485 symbolP->sy_previous->sy_next = symbolP->sy_next;
486 } /* if not first */
487
fecd2382 488#ifdef DEBUG
6efd877d 489 verify_symbol_chain (*rootPP, *lastPP);
fecd2382 490#endif /* DEBUG */
2b68b820 491}
fecd2382
RP
492
493/* Set the chain pointers of SYMBOL to null. */
6efd877d
KR
494void
495symbol_clear_list_pointers (symbolP)
496 symbolS *symbolP;
fecd2382 497{
6efd877d
KR
498 symbolP->sy_next = NULL;
499 symbolP->sy_previous = NULL;
2b68b820 500}
fecd2382
RP
501
502/* Link symbol ADDME before symbol TARGET in the chain. */
6efd877d
KR
503void
504symbol_insert (addme, target, rootPP, lastPP)
505 symbolS *addme;
506 symbolS *target;
507 symbolS **rootPP;
508 symbolS **lastPP;
fecd2382 509{
6efd877d
KR
510 if (target->sy_previous != NULL)
511 {
512 target->sy_previous->sy_next = addme;
513 }
514 else
515 {
516 know (*rootPP == target);
517 *rootPP = addme;
518 } /* if not first */
519
520 addme->sy_previous = target->sy_previous;
521 target->sy_previous = addme;
522 addme->sy_next = target;
523
fecd2382 524#ifdef DEBUG
6efd877d 525 verify_symbol_chain (*rootPP, *lastPP);
fecd2382 526#endif /* DEBUG */
2b68b820 527}
6efd877d 528
fecd2382
RP
529#endif /* SYMBOLS_NEED_BACKPOINTERS */
530
6efd877d
KR
531void
532verify_symbol_chain (rootP, lastP)
533 symbolS *rootP;
534 symbolS *lastP;
fecd2382 535{
6efd877d
KR
536 symbolS *symbolP = rootP;
537
538 if (symbolP == NULL)
2b68b820 539 return;
6efd877d
KR
540
541 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
542 {
fecd2382 543#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 544 know (symbolP->sy_next->sy_previous == symbolP);
2b68b820
KR
545#else
546 /* Walk the list anyways, to make sure pointers are still good. */
547 *symbolP;
fecd2382 548#endif /* SYMBOLS_NEED_BACKPOINTERS */
2b68b820 549 }
6efd877d 550
2b68b820
KR
551 assert (lastP == symbolP);
552}
6efd877d 553
2b68b820
KR
554void
555verify_symbol_chain_2 (sym)
556 symbolS *sym;
557{
558 symbolS *p = sym, *n = sym;
559#ifdef SYMBOLS_NEED_BACKPOINTERS
560 while (symbol_previous (p))
561 p = symbol_previous (p);
562#endif
563 while (symbol_next (n))
564 n = symbol_next (n);
565 verify_symbol_chain (p, n);
566}
6efd877d 567
5868b1fe
ILT
568/* Resolve the value of a symbol. This is called during the final
569 pass over the symbol table to resolve any symbols with complex
570 values. */
571
572void
573resolve_symbol_value (symp)
574 symbolS *symp;
575{
576 if (symp->sy_resolved)
577 return;
578
579 if (symp->sy_resolving)
580 {
581 as_bad ("Symbol definition loop encountered at %s",
582 S_GET_NAME (symp));
583 S_SET_VALUE (symp, (valueT) 0);
584 }
585 else
586 {
5ac34ac3
ILT
587 offsetT left, right, val;
588 segT seg_left, seg_right;
589
5868b1fe
ILT
590 symp->sy_resolving = 1;
591
5ac34ac3 592 switch (symp->sy_value.X_op)
5868b1fe 593 {
5ac34ac3
ILT
594 case O_absent:
595 S_SET_VALUE (symp, 0);
596 /* Fall through. */
597 case O_constant:
598 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
599 if (S_GET_SEGMENT (symp) == expr_section)
600 S_SET_SEGMENT (symp, absolute_section);
601 break;
602
603 case O_symbol:
5868b1fe
ILT
604 resolve_symbol_value (symp->sy_value.X_add_symbol);
605
606#ifdef obj_frob_forward_symbol
607 /* Some object formats need to forward the segment. */
608 obj_frob_forward_symbol (symp);
609#endif
610
611 S_SET_VALUE (symp,
612 (symp->sy_value.X_add_number
613 + symp->sy_frag->fr_address
614 + S_GET_VALUE (symp->sy_value.X_add_symbol)));
5ac34ac3
ILT
615 if (S_GET_SEGMENT (symp) == expr_section
616 || S_GET_SEGMENT (symp) == undefined_section)
617 S_SET_SEGMENT (symp,
618 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
619 break;
620
621 case O_uminus:
622 case O_bit_not:
623 resolve_symbol_value (symp->sy_value.X_add_symbol);
624 if (symp->sy_value.X_op == O_uminus)
625 val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
626 else
627 val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
628 S_SET_VALUE (symp,
629 (val
630 + symp->sy_value.X_add_number
631 + symp->sy_frag->fr_address));
632 if (S_GET_SEGMENT (symp) == expr_section
633 || S_GET_SEGMENT (symp) == undefined_section)
634 S_SET_SEGMENT (symp, absolute_section);
635 break;
636
637 case O_multiply:
638 case O_divide:
639 case O_modulus:
640 case O_left_shift:
641 case O_right_shift:
642 case O_bit_inclusive_or:
643 case O_bit_or_not:
644 case O_bit_exclusive_or:
645 case O_bit_and:
646 case O_add:
647 case O_subtract:
c978e704 648 resolve_symbol_value (symp->sy_value.X_add_symbol);
5ac34ac3
ILT
649 resolve_symbol_value (symp->sy_value.X_op_symbol);
650 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
651 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
652 if (seg_left != seg_right
653 && seg_left != undefined_section
654 && seg_right != undefined_section)
655 as_bad ("%s is operation on symbols in different sections",
c978e704 656 S_GET_NAME (symp));
5ac34ac3
ILT
657 if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
658 != absolute_section)
659 && symp->sy_value.X_op != O_subtract)
660 as_bad ("%s is illegal operation on non-absolute symbols",
661 S_GET_NAME (symp));
662 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
663 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
664 switch (symp->sy_value.X_op)
665 {
666 case O_multiply: val = left * right; break;
667 case O_divide: val = left / right; break;
668 case O_modulus: val = left % right; break;
669 case O_left_shift: val = left << right; break;
670 case O_right_shift: val = left >> right; break;
671 case O_bit_inclusive_or: val = left | right; break;
672 case O_bit_or_not: val = left |~ right; break;
673 case O_bit_exclusive_or: val = left ^ right; break;
674 case O_bit_and: val = left & right; break;
675 case O_add: val = left + right; break;
676 case O_subtract: val = left - right; break;
677 default: abort ();
678 }
c978e704
ILT
679 S_SET_VALUE (symp,
680 (symp->sy_value.X_add_number
681 + symp->sy_frag->fr_address
5ac34ac3
ILT
682 + val));
683 if (S_GET_SEGMENT (symp) == expr_section
684 || S_GET_SEGMENT (symp) == undefined_section)
685 S_SET_SEGMENT (symp, absolute_section);
686 break;
687
688 case O_register:
689 case O_big:
690 case O_illegal:
691 as_bad ("bad value for symbol \"%s\"", S_GET_NAME (symp));
692 break;
5868b1fe
ILT
693 }
694 }
695
696 symp->sy_resolved = 1;
697}
698
6efd877d
KR
699#ifdef LOCAL_LABELS_DOLLAR
700
701/* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
702 They are *really* local. That is, they go out of scope whenever we see a
703 label that isn't local. Also, like fb labels, there can be multiple
704 instances of a dollar label. Therefor, we name encode each instance with
705 the instance number, keep a list of defined symbols separate from the real
706 symbol table, and we treat these buggers as a sparse array. */
707
2b68b820
KR
708static long *dollar_labels;
709static long *dollar_label_instances;
710static char *dollar_label_defines;
711static long dollar_label_count;
712static long dollar_label_max;
6efd877d
KR
713
714int
715dollar_label_defined (label)
716 long label;
717{
718 long *i;
719
720 know ((dollar_labels != NULL) || (dollar_label_count == 0));
721
722 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
723 if (*i == label)
724 return dollar_label_defines[i - dollar_labels];
6efd877d
KR
725
726 /* if we get here, label isn't defined */
2b68b820 727 return 0;
6efd877d
KR
728} /* dollar_label_defined() */
729
730static int
731dollar_label_instance (label)
732 long label;
733{
734 long *i;
735
736 know ((dollar_labels != NULL) || (dollar_label_count == 0));
737
738 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
739 if (*i == label)
740 return (dollar_label_instances[i - dollar_labels]);
6efd877d 741
2b68b820
KR
742 /* If we get here, we haven't seen the label before, therefore its instance
743 count is zero. */
744 return 0;
745}
6efd877d
KR
746
747void
748dollar_label_clear ()
749{
750 memset (dollar_label_defines, '\0', dollar_label_count);
2b68b820 751}
6efd877d
KR
752
753#define DOLLAR_LABEL_BUMP_BY 10
754
755void
756define_dollar_label (label)
757 long label;
758{
759 long *i;
760
761 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
762 if (*i == label)
763 {
764 ++dollar_label_instances[i - dollar_labels];
765 dollar_label_defines[i - dollar_labels] = 1;
766 return;
767 }
6efd877d
KR
768
769 /* if we get to here, we don't have label listed yet. */
770
771 if (dollar_labels == NULL)
772 {
773 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
774 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
775 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
776 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
777 dollar_label_count = 0;
6efd877d
KR
778 }
779 else if (dollar_label_count == dollar_label_max)
780 {
781 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
782 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
783 dollar_label_max * sizeof (long));
784 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
785 dollar_label_max * sizeof (long));
786 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
787 } /* if we needed to grow */
788
789 dollar_labels[dollar_label_count] = label;
790 dollar_label_instances[dollar_label_count] = 1;
791 dollar_label_defines[dollar_label_count] = 1;
792 ++dollar_label_count;
2b68b820 793}
6efd877d
KR
794
795/*
796 * dollar_label_name()
797 *
798 * Caller must copy returned name: we re-use the area for the next name.
799 *
2b68b820
KR
800 * The mth occurence of label n: is turned into the symbol "Ln^Am"
801 * where n is the label number and m is the instance number. "L" makes
802 * it a label discarded unless debugging and "^A"('\1') ensures no
803 * ordinary symbol SHOULD get the same name as a local label
804 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
6efd877d
KR
805 *
806 * fb labels get the same treatment, except that ^B is used in place of ^A.
807 */
808
809char * /* Return local label name. */
810dollar_label_name (n, augend)
811 register long n; /* we just saw "n$:" : n a number */
812 register int augend; /* 0 for current instance, 1 for new instance */
813{
814 long i;
815 /* Returned to caller, then copied. used for created names ("4f") */
816 static char symbol_name_build[24];
817 register char *p;
818 register char *q;
819 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
820
821 know (n >= 0);
822 know (augend == 0 || augend == 1);
823 p = symbol_name_build;
824 *p++ = 'L';
825
826 /* Next code just does sprintf( {}, "%d", n); */
827 /* label number */
828 q = symbol_name_temporary;
829 for (*q++ = 0, i = n; i; ++q)
830 {
831 *q = i % 10 + '0';
832 i /= 10;
833 }
834 while ((*p = *--q) != '\0')
835 ++p;
836
837 *p++ = 1; /* ^A */
838
839 /* instance number */
840 q = symbol_name_temporary;
841 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
842 {
843 *q = i % 10 + '0';
844 i /= 10;
845 }
846 while ((*p++ = *--q) != '\0');;
847
848 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2b68b820
KR
849 return symbol_name_build;
850}
6efd877d
KR
851
852#endif /* LOCAL_LABELS_DOLLAR */
853
854#ifdef LOCAL_LABELS_FB
855
856/*
857 * Sombody else's idea of local labels. They are made by "n:" where n
858 * is any decimal digit. Refer to them with
859 * "nb" for previous (backward) n:
860 * or "nf" for next (forward) n:.
861 *
862 * We do a little better and let n be any number, not just a single digit, but
863 * since the other guy's assembler only does ten, we treat the first ten
864 * specially.
865 *
866 * Like someone else's assembler, we have one set of local label counters for
867 * entire assembly, not one set per (sub)segment like in most assemblers. This
868 * implies that one can refer to a label in another segment, and indeed some
869 * crufty compilers have done just that.
870 *
871 * Since there could be a LOT of these things, treat them as a sparse array.
872 */
873
874#define FB_LABEL_SPECIAL (10)
875
876static long fb_low_counter[FB_LABEL_SPECIAL];
877static long *fb_labels;
878static long *fb_label_instances;
879static long fb_label_count = 0;
880static long fb_label_max = 0;
881
882/* this must be more than FB_LABEL_SPECIAL */
883#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
884
885static void
886fb_label_init ()
887{
888 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
6efd877d
KR
889} /* fb_label_init() */
890
891/* add one to the instance number of this fb label */
892void
893fb_label_instance_inc (label)
894 long label;
895{
896 long *i;
897
898 if (label < FB_LABEL_SPECIAL)
899 {
900 ++fb_low_counter[label];
901 return;
902 }
903
e154ecf4 904 if (fb_labels != NULL)
6efd877d 905 {
e154ecf4
ILT
906 for (i = fb_labels + FB_LABEL_SPECIAL;
907 i < fb_labels + fb_label_count; ++i)
6efd877d 908 {
e154ecf4
ILT
909 if (*i == label)
910 {
911 ++fb_label_instances[i - fb_labels];
912 return;
913 } /* if we find it */
914 } /* for each existing label */
915 }
6efd877d
KR
916
917 /* if we get to here, we don't have label listed yet. */
918
919 if (fb_labels == NULL)
920 {
921 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
922 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
923 fb_label_max = FB_LABEL_BUMP_BY;
924 fb_label_count = FB_LABEL_SPECIAL;
925
926 }
927 else if (fb_label_count == fb_label_max)
928 {
929 fb_label_max += FB_LABEL_BUMP_BY;
930 fb_labels = (long *) xrealloc ((char *) fb_labels,
931 fb_label_max * sizeof (long));
932 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
933 fb_label_max * sizeof (long));
934 } /* if we needed to grow */
935
936 fb_labels[fb_label_count] = label;
937 fb_label_instances[fb_label_count] = 1;
938 ++fb_label_count;
939 return;
940} /* fb_label_instance_inc() */
941
942static long
943fb_label_instance (label)
944 long label;
945{
946 long *i;
947
948 if (label < FB_LABEL_SPECIAL)
949 {
950 return (fb_low_counter[label]);
951 }
952
e154ecf4 953 if (fb_labels != NULL)
6efd877d 954 {
e154ecf4
ILT
955 for (i = fb_labels + FB_LABEL_SPECIAL;
956 i < fb_labels + fb_label_count; ++i)
6efd877d 957 {
e154ecf4
ILT
958 if (*i == label)
959 {
960 return (fb_label_instances[i - fb_labels]);
961 } /* if we find it */
962 } /* for each existing label */
963 }
6efd877d 964
e154ecf4
ILT
965 /* We didn't find the label, so this must be a reference to the
966 first instance. */
967 return 0;
2b68b820 968}
6efd877d
KR
969
970/*
971 * fb_label_name()
972 *
973 * Caller must copy returned name: we re-use the area for the next name.
974 *
2b68b820
KR
975 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
976 * where n is the label number and m is the instance number. "L" makes
977 * it a label discarded unless debugging and "^B"('\2') ensures no
978 * ordinary symbol SHOULD get the same name as a local label
979 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
6efd877d 980 *
2b68b820 981 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
6efd877d
KR
982
983char * /* Return local label name. */
984fb_label_name (n, augend)
985 long n; /* we just saw "n:", "nf" or "nb" : n a number */
986 long augend; /* 0 for nb, 1 for n:, nf */
987{
988 long i;
989 /* Returned to caller, then copied. used for created names ("4f") */
990 static char symbol_name_build[24];
991 register char *p;
992 register char *q;
993 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
994
995 know (n >= 0);
996 know (augend == 0 || augend == 1);
997 p = symbol_name_build;
998 *p++ = 'L';
999
1000 /* Next code just does sprintf( {}, "%d", n); */
1001 /* label number */
1002 q = symbol_name_temporary;
1003 for (*q++ = 0, i = n; i; ++q)
1004 {
1005 *q = i % 10 + '0';
1006 i /= 10;
1007 }
1008 while ((*p = *--q) != '\0')
1009 ++p;
1010
1011 *p++ = 2; /* ^B */
1012
1013 /* instance number */
1014 q = symbol_name_temporary;
1015 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1016 {
1017 *q = i % 10 + '0';
1018 i /= 10;
1019 }
1020 while ((*p++ = *--q) != '\0');;
1021
1022 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1023 return (symbol_name_build);
1024} /* fb_label_name() */
1025
1026#endif /* LOCAL_LABELS_FB */
fecd2382
RP
1027
1028
a6c6eaf8 1029/*
6efd877d
KR
1030 * decode name that may have been generated by foo_label_name() above. If
1031 * the name wasn't generated by foo_label_name(), then return it unaltered.
a6c6eaf8
RP
1032 * This is used for error messages.
1033 */
a39116f1 1034
6efd877d
KR
1035char *
1036decode_local_label_name (s)
1037 char *s;
a6c6eaf8 1038{
6efd877d
KR
1039 char *p;
1040 char *symbol_decode;
1041 int label_number;
1042 int instance_number;
1043 char *type;
1044 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1045
1046 if (s[0] != 'L')
1047 return (s);
1048
1049 for (label_number = 0, p = s + 1; isdigit (*p); ++p)
1050 {
1051 label_number = (10 * label_number) + *p - '0';
1052 }
1053
1054 if (*p == 1)
1055 {
1056 type = "dollar";
1057 }
1058 else if (*p == 2)
1059 {
1060 type = "fb";
1061 }
1062 else
1063 {
1064 return (s);
1065 }
1066
1067 for (instance_number = 0, p = s + 1; isdigit (*p); ++p)
1068 {
1069 instance_number = (10 * instance_number) + *p - '0';
1070 }
1071
1072 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1073 (void) sprintf (symbol_decode, message_format, label_number,
1074 instance_number, type);
1075
1076 return (symbol_decode);
1077} /* decode_local_label_name() */
a6c6eaf8 1078
85051959
ILT
1079/* Get the value of a symbol. */
1080
1081valueT
1082S_GET_VALUE (s)
1083 symbolS *s;
1084{
5ac34ac3 1085 if (s->sy_value.X_op != O_constant)
5868b1fe 1086 as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s));
85051959
ILT
1087 return (valueT) s->sy_value.X_add_number;
1088}
1089
1090/* Set the value of a symbol. */
1091
1092void
1093S_SET_VALUE (s, val)
1094 symbolS *s;
1095 valueT val;
1096{
5ac34ac3 1097 s->sy_value.X_op = O_constant;
85051959
ILT
1098 s->sy_value.X_add_number = (offsetT) val;
1099}
1100
2b68b820
KR
1101#ifdef BFD_ASSEMBLER
1102
1103int
1104S_IS_EXTERNAL (s)
1105 symbolS *s;
1106{
1107 flagword flags = s->bsym->flags;
1108
1109 /* sanity check */
1110 if (flags & BSF_LOCAL && flags & (BSF_EXPORT | BSF_GLOBAL))
1111 abort ();
1112
1113 return (flags & (BSF_EXPORT | BSF_GLOBAL)) != 0;
1114}
1115
1116int
1117S_IS_COMMON (s)
1118 symbolS *s;
1119{
1120 return s->bsym->section == &bfd_com_section;
1121}
1122
1123int
1124S_IS_DEFINED (s)
1125 symbolS *s;
1126{
1127 return s->bsym->section != undefined_section;
1128}
1129
1130int
1131S_IS_DEBUG (s)
1132 symbolS *s;
1133{
1134 if (s->bsym->flags & BSF_DEBUGGING)
1135 return 1;
1136 return 0;
1137}
1138
1139int
1140S_IS_LOCAL (s)
1141 symbolS *s;
1142{
1143 flagword flags = s->bsym->flags;
1144
1145 /* sanity check */
1146 if (flags & BSF_LOCAL && flags & (BSF_EXPORT | BSF_GLOBAL))
1147 abort ();
1148
1149 return (S_GET_NAME (s)
1150 && ! S_IS_DEBUG (s)
1151 && (strchr (S_GET_NAME (s), '\001')
1152 || strchr (S_GET_NAME (s), '\002')
1153 || (S_LOCAL_NAME (s)
1154 && !flagseen['L'])));
1155}
1156
1157int
1158S_IS_EXTERN (s)
1159 symbolS *s;
1160{
1161 return S_IS_EXTERNAL (s);
1162}
1163
1164int
1165S_IS_STABD (s)
1166 symbolS *s;
1167{
1168 return S_GET_NAME (s) == 0;
1169}
1170
2b68b820
KR
1171CONST char *
1172S_GET_NAME (s)
1173 symbolS *s;
1174{
1175 return s->bsym->name;
1176}
1177
1178segT
1179S_GET_SEGMENT (s)
1180 symbolS *s;
1181{
1182 return s->bsym->section;
1183}
1184
2b68b820
KR
1185void
1186S_SET_SEGMENT (s, seg)
1187 symbolS *s;
1188 segT seg;
1189{
1190 s->bsym->section = seg;
1191}
1192
1193void
1194S_SET_EXTERNAL (s)
1195 symbolS *s;
1196{
1197 s->bsym->flags |= BSF_EXPORT | BSF_GLOBAL;
1198 s->bsym->flags &= ~BSF_LOCAL;
1199}
1200
1201void
1202S_CLEAR_EXTERNAL (s)
1203 symbolS *s;
1204{
1205 s->bsym->flags |= BSF_LOCAL;
1206 s->bsym->flags &= ~(BSF_EXPORT | BSF_GLOBAL);
1207}
1208
1209void
1210S_SET_NAME (s, name)
1211 symbolS *s;
1212 char *name;
1213{
1214 s->bsym->name = name;
1215}
1216#endif /* BFD_ASSEMBLER */
1217
8b228fe9 1218/* end of symbols.c */
This page took 0.129143 seconds and 4 git commands to generate.