* symbols.c (symbol_new): Don't call debug_verify_symchain.
[deliverable/binutils-gdb.git] / gas / symbols.c
CommitLineData
fecd2382 1/* symbols.c -symbol table-
e95ef7c1 2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
dacf29ea 3 Free Software Foundation, Inc.
6efd877d 4
a39116f1 5 This file is part of GAS, the GNU Assembler.
6efd877d 6
a39116f1
RP
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.
6efd877d 11
a39116f1
RP
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.
6efd877d 16
a39116f1 17 You should have received a copy of the GNU General Public License
e95ef7c1
ILT
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
fecd2382 21
58d4951d 22/* #define DEBUG_SYMS / * to debug symbol list maintenance */
2b68b820 23
6efd877d
KR
24#include <ctype.h>
25
fecd2382
RP
26#include "as.h"
27
28#include "obstack.h" /* For "symbols.h" */
29#include "subsegs.h"
30
1356d77d
ILT
31/* This is non-zero if symbols are case sensitive, which is the
32 default. */
33int symbols_case_sensitive = 1;
34
fecd2382
RP
35#ifndef WORKING_DOT_WORD
36extern int new_broken_words;
37#endif
fecd2382 38
2b68b820
KR
39/* symbol-name => struct symbol pointer */
40static struct hash_control *sy_hash;
fecd2382 41
a39116f1 42/* Below are commented in "symbols.h". */
6efd877d
KR
43symbolS *symbol_rootP;
44symbolS *symbol_lastP;
45symbolS abs_symbol;
fecd2382 46
dacf29ea
KR
47#ifdef DEBUG_SYMS
48#define debug_verify_symchain verify_symbol_chain
49#else
8095b665 50#define debug_verify_symchain(root, last) ((void) 0)
dacf29ea
KR
51#endif
52
6efd877d 53struct obstack notes;
fecd2382 54
85825401 55static void fb_label_init PARAMS ((void));
fecd2382 56
dacf29ea
KR
57/* symbol_new()
58
59 Return a pointer to a new symbol. Die if we can't make a new
60 symbol. Fill in the symbol's values. Add symbol to end of symbol
61 chain.
62
63 This function should be called in the general case of creating a
64 symbol. However, if the output file symbol table has already been
65 set, and you are certain that this symbol won't be wanted in the
66 output file, you can call symbol_create. */
fecd2382 67
6efd877d 68symbolS *
604633ae 69symbol_new (name, segment, valu, frag)
dacf29ea
KR
70 const char *name;
71 segT segment;
72 valueT valu;
73 fragS *frag;
74{
75 symbolS *symbolP = symbol_create (name, segment, valu, frag);
76
77 /*
78 * Link to end of symbol chain.
79 */
80#ifdef BFD_ASSEMBLER
81 {
82 extern int symbol_table_frozen;
83 if (symbol_table_frozen)
84 abort ();
85 }
86#endif
87 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
dacf29ea
KR
88
89 return symbolP;
90}
91
92symbolS *
93symbol_create (name, segment, valu, frag)
94 const char *name; /* It is copied, the caller can destroy/modify */
6efd877d 95 segT segment; /* Segment identifier (SEG_<something>) */
604633ae 96 valueT valu; /* Symbol value */
6efd877d 97 fragS *frag; /* Associated fragment */
fecd2382 98{
6efd877d
KR
99 unsigned int name_length;
100 char *preserved_copy_of_name;
101 symbolS *symbolP;
102
103 name_length = strlen (name) + 1; /* +1 for \0 */
104 obstack_grow (&notes, name, name_length);
105 preserved_copy_of_name = obstack_finish (&notes);
2b68b820
KR
106#ifdef STRIP_UNDERSCORE
107 if (preserved_copy_of_name[0] == '_')
108 preserved_copy_of_name++;
109#endif
dacf29ea
KR
110
111#ifdef tc_canonicalize_symbol_name
112 preserved_copy_of_name =
113 tc_canonicalize_symbol_name (preserved_copy_of_name);
114#endif
115
1356d77d
ILT
116 if (! symbols_case_sensitive)
117 {
118 unsigned char *s;
119
120 for (s = (unsigned char *) preserved_copy_of_name; *s != '\0'; s++)
121 if (islower (*s))
122 *s = toupper (*s);
123 }
124
6efd877d
KR
125 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
126
127 /* symbol must be born in some fixed state. This seems as good as any. */
128 memset (symbolP, 0, sizeof (symbolS));
129
2b68b820
KR
130#ifdef BFD_ASSEMBLER
131 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
943bdfdc
ILT
132 if (symbolP->bsym == NULL)
133 as_perror ("%s", "bfd_make_empty_symbol");
87e48495 134 symbolP->bsym->udata.p = (PTR) symbolP;
2b68b820 135#endif
6efd877d 136 S_SET_NAME (symbolP, preserved_copy_of_name);
6efd877d
KR
137
138 S_SET_SEGMENT (symbolP, segment);
604633ae 139 S_SET_VALUE (symbolP, valu);
dacf29ea 140 symbol_clear_list_pointers (symbolP);
6efd877d
KR
141
142 symbolP->sy_frag = frag;
2b68b820 143#ifndef BFD_ASSEMBLER
6efd877d 144 symbolP->sy_number = ~0;
0cafaab1 145 symbolP->sy_name_offset = (unsigned int) ~0;
2b68b820 146#endif
6efd877d 147
6efd877d
KR
148 obj_symbol_new_hook (symbolP);
149
dacf29ea
KR
150#ifdef tc_symbol_new_hook
151 tc_symbol_new_hook (symbolP);
152#endif
fecd2382 153
2b68b820
KR
154 return symbolP;
155}
fecd2382 156\f
6efd877d 157
fecd2382
RP
158/*
159 * colon()
160 *
161 * We have just seen "<name>:".
162 * Creates a struct symbol unless it already exists.
163 *
164 * Gripes if we are redefining a symbol incompatibly (and ignores it).
165 *
166 */
1356d77d 167symbolS *
6efd877d 168colon (sym_name) /* just seen "x:" - rattle symbols & frags */
2a0f64a5 169 const char *sym_name; /* symbol name, as a cannonical string */
6efd877d 170 /* We copy this string: OK to alter later. */
fecd2382 171{
6efd877d
KR
172 register symbolS *symbolP; /* symbol we are working with */
173
2b68b820
KR
174 /* Sun local labels go out of scope whenever a non-local symbol is
175 defined. */
e95ef7c1
ILT
176 if (LOCAL_LABELS_DOLLAR)
177 {
178 int local;
179
180#ifdef BFD_ASSEMBLER
181 local = bfd_is_local_label_name (stdoutput, sym_name);
182#else
183 local = LOCAL_LABEL (sym_name);
184#endif
185
186 if (! local)
187 dollar_label_clear ();
188 }
6efd877d 189
fecd2382 190#ifndef WORKING_DOT_WORD
6efd877d
KR
191 if (new_broken_words)
192 {
193 struct broken_word *a;
194 int possible_bytes;
195 fragS *frag_tmp;
196 char *frag_opcode;
197
2b68b820
KR
198 extern const int md_short_jump_size;
199 extern const int md_long_jump_size;
200 possible_bytes = (md_short_jump_size
201 + new_broken_words * md_long_jump_size);
6efd877d
KR
202
203 frag_tmp = frag_now;
204 frag_opcode = frag_var (rs_broken_word,
205 possible_bytes,
206 possible_bytes,
207 (relax_substateT) 0,
208 (symbolS *) broken_words,
209 0L,
210 NULL);
211
212 /* We want to store the pointer to where to insert the jump table in the
2b68b820
KR
213 fr_opcode of the rs_broken_word frag. This requires a little
214 hackery. */
215 while (frag_tmp
216 && (frag_tmp->fr_type != rs_broken_word
217 || frag_tmp->fr_opcode))
6efd877d
KR
218 frag_tmp = frag_tmp->fr_next;
219 know (frag_tmp);
220 frag_tmp->fr_opcode = frag_opcode;
221 new_broken_words = 0;
222
223 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
224 a->dispfrag = frag_tmp;
225 }
226#endif /* WORKING_DOT_WORD */
227
228 if ((symbolP = symbol_find (sym_name)) != 0)
229 {
2b68b820
KR
230#ifdef RESOLVE_SYMBOL_REDEFINITION
231 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
1356d77d 232 return symbolP;
2b68b820 233#endif
6efd877d 234 /*
2b68b820
KR
235 * Now check for undefined symbols
236 */
6efd877d
KR
237 if (!S_IS_DEFINED (symbolP))
238 {
239 if (S_GET_VALUE (symbolP) == 0)
240 {
241 symbolP->sy_frag = frag_now;
2b68b820 242#ifdef OBJ_VMS
214f540d 243 S_SET_OTHER(symbolP, const_flag);
fecd2382 244#endif
87e48495 245 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d 246 S_SET_SEGMENT (symbolP, now_seg);
fecd2382 247#ifdef N_UNDF
6efd877d 248 know (N_UNDF == 0);
fecd2382 249#endif /* if we have one, it better be zero. */
6efd877d
KR
250
251 }
252 else
253 {
254 /*
2b68b820
KR
255 * There are still several cases to check:
256 * A .comm/.lcomm symbol being redefined as
257 * initialized data is OK
258 * A .comm/.lcomm symbol being redefined with
259 * a larger size is also OK
260 *
261 * This only used to be allowed on VMS gas, but Sun cc
262 * on the sparc also depends on it.
263 */
264
265 if (((!S_IS_DEBUG (symbolP)
266 && !S_IS_DEFINED (symbolP)
267 && S_IS_EXTERNAL (symbolP))
268 || S_GET_SEGMENT (symbolP) == bss_section)
269 && (now_seg == data_section
270 || now_seg == S_GET_SEGMENT (symbolP)))
6efd877d
KR
271 {
272 /*
2b68b820
KR
273 * Select which of the 2 cases this is
274 */
275 if (now_seg != data_section)
6efd877d
KR
276 {
277 /*
2b68b820
KR
278 * New .comm for prev .comm symbol.
279 * If the new size is larger we just
280 * change its value. If the new size
281 * is smaller, we ignore this symbol
282 */
6efd877d 283 if (S_GET_VALUE (symbolP)
2b68b820 284 < ((unsigned) frag_now_fix ()))
6efd877d 285 {
2b68b820 286 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d
KR
287 }
288 }
289 else
290 {
85825401
ILT
291 /* It is a .comm/.lcomm being converted to initialized
292 data. */
6efd877d 293 symbolP->sy_frag = frag_now;
2b68b820 294#ifdef OBJ_VMS
214f540d
KR
295 S_SET_OTHER(symbolP, const_flag);
296#endif
2b68b820 297 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d
KR
298 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
299 }
300 }
301 else
302 {
2b68b820
KR
303#if defined (S_GET_OTHER) && defined (S_GET_DESC)
304 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
6efd877d
KR
305 sym_name,
306 segment_name (S_GET_SEGMENT (symbolP)),
2b68b820
KR
307 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
308 (long) S_GET_VALUE (symbolP));
309#else
58d4951d 310 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
6efd877d
KR
311 sym_name,
312 segment_name (S_GET_SEGMENT (symbolP)),
58d4951d 313 (long) S_GET_VALUE (symbolP));
2b68b820 314#endif
6efd877d
KR
315 }
316 } /* if the undefined symbol has no value */
317 }
318 else
319 {
320 /* Don't blow up if the definition is the same */
321 if (!(frag_now == symbolP->sy_frag
87e48495 322 && S_GET_VALUE (symbolP) == frag_now_fix ()
6efd877d
KR
323 && S_GET_SEGMENT (symbolP) == now_seg))
324 as_fatal ("Symbol %s already defined.", sym_name);
325 } /* if this symbol is not yet defined */
326
327 }
328 else
329 {
87e48495 330 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
6efd877d 331 frag_now);
2b68b820 332#ifdef OBJ_VMS
6efd877d 333 S_SET_OTHER (symbolP, const_flag);
2b68b820 334#endif /* OBJ_VMS */
fecd2382 335
6efd877d
KR
336 symbol_table_insert (symbolP);
337 } /* if we have seen this symbol before */
1e9cf565 338
1356d77d
ILT
339 if (mri_common_symbol != NULL)
340 {
341 /* This symbol is actually being defined within an MRI common
342 section. This requires special handling. */
343 symbolP->sy_value.X_op = O_symbol;
344 symbolP->sy_value.X_add_symbol = mri_common_symbol;
345 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
346 symbolP->sy_frag = &zero_address_frag;
347 S_SET_SEGMENT (symbolP, expr_section);
348 symbolP->sy_mri_common = 1;
349 }
350
1e9cf565
ILT
351#ifdef tc_frob_label
352 tc_frob_label (symbolP);
2b68b820 353#endif
1356d77d
ILT
354
355 return symbolP;
1ecd6c4a 356}
fecd2382 357\f
6efd877d 358
fecd2382
RP
359/*
360 * symbol_table_insert()
361 *
362 * Die if we can't insert the symbol.
363 *
364 */
365
6efd877d
KR
366void
367symbol_table_insert (symbolP)
368 symbolS *symbolP;
fecd2382 369{
604633ae 370 register const char *error_string;
6efd877d
KR
371
372 know (symbolP);
373 know (S_GET_NAME (symbolP));
374
0cafaab1 375 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
6efd877d
KR
376 {
377 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
378 S_GET_NAME (symbolP), error_string);
379 } /* on error */
380} /* symbol_table_insert() */
fecd2382
RP
381\f
382/*
383 * symbol_find_or_make()
384 *
385 * If a symbol name does not exist, create it as undefined, and insert
386 * it into the symbol table. Return a pointer to it.
387 */
6efd877d
KR
388symbolS *
389symbol_find_or_make (name)
73255941 390 const char *name;
fecd2382 391{
6efd877d
KR
392 register symbolS *symbolP;
393
394 symbolP = symbol_find (name);
395
396 if (symbolP == NULL)
397 {
398 symbolP = symbol_make (name);
399
400 symbol_table_insert (symbolP);
401 } /* if symbol wasn't found */
402
403 return (symbolP);
404} /* symbol_find_or_make() */
405
406symbolS *
407symbol_make (name)
2b68b820 408 CONST char *name;
fecd2382 409{
6efd877d
KR
410 symbolS *symbolP;
411
412 /* Let the machine description default it, e.g. for register names. */
2b68b820 413 symbolP = md_undefined_symbol ((char *) name);
6efd877d
KR
414
415 if (!symbolP)
2b68b820 416 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
6efd877d
KR
417
418 return (symbolP);
419} /* symbol_make() */
fecd2382
RP
420
421/*
422 * symbol_find()
6efd877d 423 *
fecd2382
RP
424 * Implement symbol table lookup.
425 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
426 * Out: NULL if the name was not in the symbol table, else the address
427 * of a struct symbol associated with that name.
428 */
429
6efd877d
KR
430symbolS *
431symbol_find (name)
2b68b820 432 CONST char *name;
fecd2382 433{
a6c6eaf8 434#ifdef STRIP_UNDERSCORE
6efd877d 435 return (symbol_find_base (name, 1));
a6c6eaf8 436#else /* STRIP_UNDERSCORE */
6efd877d 437 return (symbol_find_base (name, 0));
fecd2382 438#endif /* STRIP_UNDERSCORE */
6efd877d 439} /* symbol_find() */
fecd2382 440
6efd877d
KR
441symbolS *
442symbol_find_base (name, strip_underscore)
2b68b820 443 CONST char *name;
6efd877d 444 int strip_underscore;
fecd2382 445{
6efd877d
KR
446 if (strip_underscore && *name == '_')
447 name++;
dacf29ea
KR
448
449#ifdef tc_canonicalize_symbol_name
450 {
451 char *copy;
452
453 copy = (char *) alloca (strlen (name) + 1);
454 strcpy (copy, name);
455 name = tc_canonicalize_symbol_name (copy);
456 }
457#endif
458
1356d77d
ILT
459 if (! symbols_case_sensitive)
460 {
461 unsigned char *copy;
462
463 copy = (unsigned char *) alloca (strlen (name) + 1);
464 name = (const char *) copy;
465 for (; *copy != '\0'; copy++)
466 if (islower (*copy))
467 *copy = toupper (*copy);
468 }
469
6efd877d 470 return ((symbolS *) hash_find (sy_hash, name));
fecd2382
RP
471}
472
473/*
474 * Once upon a time, symbols were kept in a singly linked list. At
475 * least coff needs to be able to rearrange them from time to time, for
476 * which a doubly linked list is much more convenient. Loic did these
477 * as macros which seemed dangerous to me so they're now functions.
478 * xoxorich.
479 */
480
481/* Link symbol ADDME after symbol TARGET in the chain. */
6efd877d
KR
482void
483symbol_append (addme, target, rootPP, lastPP)
484 symbolS *addme;
485 symbolS *target;
486 symbolS **rootPP;
487 symbolS **lastPP;
fecd2382 488{
6efd877d
KR
489 if (target == NULL)
490 {
491 know (*rootPP == NULL);
492 know (*lastPP == NULL);
e95ef7c1
ILT
493 addme->sy_next = NULL;
494#ifdef SYMBOLS_NEED_BACKPOINTERS
495 addme->sy_previous = NULL;
496#endif
6efd877d
KR
497 *rootPP = addme;
498 *lastPP = addme;
499 return;
500 } /* if the list is empty */
501
502 if (target->sy_next != NULL)
503 {
fecd2382 504#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 505 target->sy_next->sy_previous = addme;
fecd2382 506#endif /* SYMBOLS_NEED_BACKPOINTERS */
6efd877d
KR
507 }
508 else
509 {
510 know (*lastPP == target);
511 *lastPP = addme;
512 } /* if we have a next */
513
514 addme->sy_next = target->sy_next;
515 target->sy_next = addme;
516
fecd2382 517#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 518 addme->sy_previous = target;
fecd2382 519#endif /* SYMBOLS_NEED_BACKPOINTERS */
e95ef7c1
ILT
520
521 debug_verify_symchain (symbol_rootP, symbol_lastP);
2b68b820 522}
fecd2382 523
dacf29ea
KR
524/* Set the chain pointers of SYMBOL to null. */
525void
526symbol_clear_list_pointers (symbolP)
527 symbolS *symbolP;
528{
529 symbolP->sy_next = NULL;
530#ifdef SYMBOLS_NEED_BACKPOINTERS
531 symbolP->sy_previous = NULL;
532#endif
533}
534
fecd2382
RP
535#ifdef SYMBOLS_NEED_BACKPOINTERS
536/* Remove SYMBOLP from the list. */
6efd877d
KR
537void
538symbol_remove (symbolP, rootPP, lastPP)
539 symbolS *symbolP;
540 symbolS **rootPP;
541 symbolS **lastPP;
fecd2382 542{
6efd877d
KR
543 if (symbolP == *rootPP)
544 {
545 *rootPP = symbolP->sy_next;
546 } /* if it was the root */
547
548 if (symbolP == *lastPP)
549 {
550 *lastPP = symbolP->sy_previous;
551 } /* if it was the tail */
552
553 if (symbolP->sy_next != NULL)
554 {
555 symbolP->sy_next->sy_previous = symbolP->sy_previous;
556 } /* if not last */
557
558 if (symbolP->sy_previous != NULL)
559 {
560 symbolP->sy_previous->sy_next = symbolP->sy_next;
561 } /* if not first */
562
dacf29ea 563 debug_verify_symchain (*rootPP, *lastPP);
2b68b820 564}
fecd2382
RP
565
566/* Link symbol ADDME before symbol TARGET in the chain. */
6efd877d
KR
567void
568symbol_insert (addme, target, rootPP, lastPP)
569 symbolS *addme;
570 symbolS *target;
571 symbolS **rootPP;
572 symbolS **lastPP;
fecd2382 573{
6efd877d
KR
574 if (target->sy_previous != NULL)
575 {
576 target->sy_previous->sy_next = addme;
577 }
578 else
579 {
580 know (*rootPP == target);
581 *rootPP = addme;
582 } /* if not first */
583
584 addme->sy_previous = target->sy_previous;
585 target->sy_previous = addme;
586 addme->sy_next = target;
587
dacf29ea 588 debug_verify_symchain (*rootPP, *lastPP);
2b68b820 589}
6efd877d 590
fecd2382
RP
591#endif /* SYMBOLS_NEED_BACKPOINTERS */
592
6efd877d
KR
593void
594verify_symbol_chain (rootP, lastP)
595 symbolS *rootP;
596 symbolS *lastP;
fecd2382 597{
6efd877d
KR
598 symbolS *symbolP = rootP;
599
600 if (symbolP == NULL)
2b68b820 601 return;
6efd877d
KR
602
603 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
604 {
fecd2382 605#ifdef SYMBOLS_NEED_BACKPOINTERS
e95ef7c1 606 assert (symbolP->sy_next->sy_previous == symbolP);
2b68b820
KR
607#else
608 /* Walk the list anyways, to make sure pointers are still good. */
604633ae 609 ;
fecd2382 610#endif /* SYMBOLS_NEED_BACKPOINTERS */
2b68b820 611 }
6efd877d 612
2b68b820
KR
613 assert (lastP == symbolP);
614}
6efd877d 615
2b68b820
KR
616void
617verify_symbol_chain_2 (sym)
618 symbolS *sym;
619{
620 symbolS *p = sym, *n = sym;
621#ifdef SYMBOLS_NEED_BACKPOINTERS
622 while (symbol_previous (p))
623 p = symbol_previous (p);
624#endif
625 while (symbol_next (n))
626 n = symbol_next (n);
627 verify_symbol_chain (p, n);
628}
6efd877d 629
5868b1fe
ILT
630/* Resolve the value of a symbol. This is called during the final
631 pass over the symbol table to resolve any symbols with complex
632 values. */
633
634void
635resolve_symbol_value (symp)
636 symbolS *symp;
637{
0cafaab1
ILT
638 int resolved;
639
5868b1fe
ILT
640 if (symp->sy_resolved)
641 return;
642
0cafaab1
ILT
643 resolved = 0;
644
5868b1fe
ILT
645 if (symp->sy_resolving)
646 {
647 as_bad ("Symbol definition loop encountered at %s",
648 S_GET_NAME (symp));
649 S_SET_VALUE (symp, (valueT) 0);
0cafaab1 650 resolved = 1;
5868b1fe
ILT
651 }
652 else
653 {
5ac34ac3
ILT
654 offsetT left, right, val;
655 segT seg_left, seg_right;
656
5868b1fe
ILT
657 symp->sy_resolving = 1;
658
3569064f
ILT
659 /* Simplify addition or subtraction of a constant by folding the
660 constant into X_add_number. */
661 if (symp->sy_value.X_op == O_add
662 || symp->sy_value.X_op == O_subtract)
663 {
664 resolve_symbol_value (symp->sy_value.X_add_symbol);
665 resolve_symbol_value (symp->sy_value.X_op_symbol);
666 if (S_GET_SEGMENT (symp->sy_value.X_op_symbol) == absolute_section)
667 {
668 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
669 if (symp->sy_value.X_op == O_add)
670 symp->sy_value.X_add_number += right;
671 else
672 symp->sy_value.X_add_number -= right;
673 symp->sy_value.X_op = O_symbol;
674 symp->sy_value.X_op_symbol = NULL;
675 }
676 else if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
677 == absolute_section)
678 && symp->sy_value.X_op == O_add)
679 {
680 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
681 symp->sy_value.X_add_symbol = symp->sy_value.X_op_symbol;
682 symp->sy_value.X_add_number += left;
683 symp->sy_value.X_op = O_symbol;
684 symp->sy_value.X_op_symbol = NULL;
685 }
686 }
687
5ac34ac3 688 switch (symp->sy_value.X_op)
5868b1fe 689 {
5ac34ac3
ILT
690 case O_absent:
691 S_SET_VALUE (symp, 0);
692 /* Fall through. */
693 case O_constant:
694 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
695 if (S_GET_SEGMENT (symp) == expr_section)
696 S_SET_SEGMENT (symp, absolute_section);
0cafaab1 697 resolved = 1;
5ac34ac3
ILT
698 break;
699
700 case O_symbol:
5868b1fe
ILT
701 resolve_symbol_value (symp->sy_value.X_add_symbol);
702
1356d77d
ILT
703 if (symp->sy_mri_common)
704 {
705 /* This is a symbol inside an MRI common section. The
706 relocation routines are going to handle it specially.
707 Don't change the value. */
708 S_SET_VALUE (symp, symp->sy_value.X_add_number);
709 resolved = symp->sy_value.X_add_symbol->sy_resolved;
710 break;
711 }
712
943bdfdc
ILT
713 if (symp->sy_value.X_add_number == 0)
714 copy_symbol_attributes (symp, symp->sy_value.X_add_symbol);
715
943bdfdc 716 /* If we have equated this symbol to an undefined symbol, we
3569064f
ILT
717 keep X_op set to O_symbol, and we don't change
718 X_add_number. This permits the routine which writes out
719 relocation to detect this case, and convert the
720 relocation to be against the symbol to which this symbol
721 is equated. */
722 if (! S_IS_DEFINED (symp->sy_value.X_add_symbol)
723 || S_IS_COMMON (symp->sy_value.X_add_symbol))
e95ef7c1
ILT
724 {
725 symp->sy_value.X_op = O_symbol;
726 S_SET_SEGMENT (symp,
727 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
728 }
3569064f
ILT
729 else
730 {
731 S_SET_VALUE (symp,
732 (symp->sy_value.X_add_number
733 + symp->sy_frag->fr_address
734 + S_GET_VALUE (symp->sy_value.X_add_symbol)));
735 if (S_GET_SEGMENT (symp) == expr_section
736 || S_GET_SEGMENT (symp) == undefined_section)
737 S_SET_SEGMENT (symp,
738 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
739 }
943bdfdc 740
0cafaab1 741 resolved = symp->sy_value.X_add_symbol->sy_resolved;
5ac34ac3
ILT
742 break;
743
744 case O_uminus:
745 case O_bit_not:
943bdfdc 746 case O_logical_not:
5ac34ac3
ILT
747 resolve_symbol_value (symp->sy_value.X_add_symbol);
748 if (symp->sy_value.X_op == O_uminus)
749 val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
943bdfdc
ILT
750 else if (symp->sy_value.X_op == O_logical_not)
751 val = ! S_GET_VALUE (symp->sy_value.X_add_symbol);
5ac34ac3
ILT
752 else
753 val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
754 S_SET_VALUE (symp,
755 (val
756 + symp->sy_value.X_add_number
757 + symp->sy_frag->fr_address));
758 if (S_GET_SEGMENT (symp) == expr_section
759 || S_GET_SEGMENT (symp) == undefined_section)
760 S_SET_SEGMENT (symp, absolute_section);
0cafaab1 761 resolved = symp->sy_value.X_add_symbol->sy_resolved;
5ac34ac3
ILT
762 break;
763
764 case O_multiply:
765 case O_divide:
766 case O_modulus:
767 case O_left_shift:
768 case O_right_shift:
769 case O_bit_inclusive_or:
770 case O_bit_or_not:
771 case O_bit_exclusive_or:
772 case O_bit_and:
3569064f 773 case O_add:
5ac34ac3 774 case O_subtract:
1356d77d
ILT
775 case O_eq:
776 case O_ne:
777 case O_lt:
778 case O_le:
779 case O_ge:
780 case O_gt:
73255941
ILT
781 case O_logical_and:
782 case O_logical_or:
c978e704 783 resolve_symbol_value (symp->sy_value.X_add_symbol);
5ac34ac3
ILT
784 resolve_symbol_value (symp->sy_value.X_op_symbol);
785 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
786 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
3569064f
ILT
787 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
788 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
c0b34702 789
3569064f
ILT
790 /* Subtraction is permitted if both operands are in the same
791 section. Otherwise, both operands must be absolute. We
792 already handled the case of addition or subtraction of a
793 constant above. This will probably need to be changed
794 for an object file format which supports arbitrary
795 expressions, such as IEEE-695. */
796 if ((seg_left != absolute_section
797 || seg_right != absolute_section)
798 && (symp->sy_value.X_op != O_subtract
799 || seg_left != seg_right))
c0b34702
ILT
800 {
801 char *file;
802 unsigned int line;
803
804 if (expr_symbol_where (symp, &file, &line))
3569064f 805 {
e95ef7c1 806 if (seg_left == undefined_section)
3569064f
ILT
807 as_bad_where (file, line,
808 "undefined symbol %s in operation",
e95ef7c1
ILT
809 S_GET_NAME (symp->sy_value.X_add_symbol));
810 if (seg_right == undefined_section)
811 as_bad_where (file, line,
812 "undefined symbol %s in operation",
813 S_GET_NAME (symp->sy_value.X_op_symbol));
814 if (seg_left != undefined_section
815 && seg_right != undefined_section)
3569064f
ILT
816 as_bad_where (file, line, "invalid section for operation");
817 }
c0b34702 818 else
3569064f 819 {
e95ef7c1 820 if (seg_left == undefined_section)
3569064f 821 as_bad ("undefined symbol %s in operation setting %s",
e95ef7c1 822 S_GET_NAME (symp->sy_value.X_add_symbol),
3569064f 823 S_GET_NAME (symp));
e95ef7c1
ILT
824 if (seg_right == undefined_section)
825 as_bad ("undefined symbol %s in operation setting %s",
826 S_GET_NAME (symp->sy_value.X_op_symbol),
827 S_GET_NAME (symp));
828 if (seg_left != undefined_section
829 && seg_right != undefined_section)
3569064f
ILT
830 as_bad ("invalid section for operation setting %s",
831 S_GET_NAME (symp));
832 }
c0b34702 833 }
3569064f 834
5ac34ac3
ILT
835 switch (symp->sy_value.X_op)
836 {
837 case O_multiply: val = left * right; break;
838 case O_divide: val = left / right; break;
839 case O_modulus: val = left % right; break;
840 case O_left_shift: val = left << right; break;
841 case O_right_shift: val = left >> right; break;
842 case O_bit_inclusive_or: val = left | right; break;
843 case O_bit_or_not: val = left |~ right; break;
844 case O_bit_exclusive_or: val = left ^ right; break;
845 case O_bit_and: val = left & right; break;
846 case O_add: val = left + right; break;
847 case O_subtract: val = left - right; break;
1356d77d
ILT
848 case O_eq: val = left == right ? ~ (offsetT) 0 : 0;
849 case O_ne: val = left != right ? ~ (offsetT) 0 : 0;
850 case O_lt: val = left < right ? ~ (offsetT) 0 : 0;
851 case O_le: val = left <= right ? ~ (offsetT) 0 : 0;
852 case O_ge: val = left >= right ? ~ (offsetT) 0 : 0;
853 case O_gt: val = left > right ? ~ (offsetT) 0 : 0;
73255941
ILT
854 case O_logical_and: val = left && right; break;
855 case O_logical_or: val = left || right; break;
5ac34ac3
ILT
856 default: abort ();
857 }
c978e704
ILT
858 S_SET_VALUE (symp,
859 (symp->sy_value.X_add_number
860 + symp->sy_frag->fr_address
5ac34ac3
ILT
861 + val));
862 if (S_GET_SEGMENT (symp) == expr_section
863 || S_GET_SEGMENT (symp) == undefined_section)
864 S_SET_SEGMENT (symp, absolute_section);
0cafaab1
ILT
865 resolved = (symp->sy_value.X_add_symbol->sy_resolved
866 && symp->sy_value.X_op_symbol->sy_resolved);
5ac34ac3
ILT
867 break;
868
869 case O_register:
870 case O_big:
871 case O_illegal:
0cafaab1
ILT
872 /* Give an error (below) if not in expr_section. We don't
873 want to worry about expr_section symbols, because they
874 are fictional (they are created as part of expression
875 resolution), and any problems may not actually mean
876 anything. */
5ac34ac3 877 break;
5868b1fe
ILT
878 }
879 }
880
0cafaab1
ILT
881 /* Don't worry if we can't resolve an expr_section symbol. */
882 if (resolved)
883 symp->sy_resolved = 1;
884 else if (S_GET_SEGMENT (symp) != expr_section)
885 {
886 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp));
887 symp->sy_resolved = 1;
888 }
5868b1fe
ILT
889}
890
6efd877d
KR
891/* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
892 They are *really* local. That is, they go out of scope whenever we see a
893 label that isn't local. Also, like fb labels, there can be multiple
894 instances of a dollar label. Therefor, we name encode each instance with
895 the instance number, keep a list of defined symbols separate from the real
896 symbol table, and we treat these buggers as a sparse array. */
897
2b68b820
KR
898static long *dollar_labels;
899static long *dollar_label_instances;
900static char *dollar_label_defines;
901static long dollar_label_count;
604633ae 902static unsigned long dollar_label_max;
6efd877d
KR
903
904int
905dollar_label_defined (label)
906 long label;
907{
908 long *i;
909
910 know ((dollar_labels != NULL) || (dollar_label_count == 0));
911
912 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
913 if (*i == label)
914 return dollar_label_defines[i - dollar_labels];
6efd877d
KR
915
916 /* if we get here, label isn't defined */
2b68b820 917 return 0;
6efd877d
KR
918} /* dollar_label_defined() */
919
920static int
921dollar_label_instance (label)
922 long label;
923{
924 long *i;
925
926 know ((dollar_labels != NULL) || (dollar_label_count == 0));
927
928 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
929 if (*i == label)
930 return (dollar_label_instances[i - dollar_labels]);
6efd877d 931
2b68b820
KR
932 /* If we get here, we haven't seen the label before, therefore its instance
933 count is zero. */
934 return 0;
935}
6efd877d
KR
936
937void
938dollar_label_clear ()
939{
604633ae 940 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
2b68b820 941}
6efd877d
KR
942
943#define DOLLAR_LABEL_BUMP_BY 10
944
945void
946define_dollar_label (label)
947 long label;
948{
949 long *i;
950
951 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
952 if (*i == label)
953 {
954 ++dollar_label_instances[i - dollar_labels];
955 dollar_label_defines[i - dollar_labels] = 1;
956 return;
957 }
6efd877d
KR
958
959 /* if we get to here, we don't have label listed yet. */
960
961 if (dollar_labels == NULL)
962 {
963 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
964 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
965 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
966 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
967 dollar_label_count = 0;
6efd877d
KR
968 }
969 else if (dollar_label_count == dollar_label_max)
970 {
971 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
972 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
973 dollar_label_max * sizeof (long));
974 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
975 dollar_label_max * sizeof (long));
976 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
977 } /* if we needed to grow */
978
979 dollar_labels[dollar_label_count] = label;
980 dollar_label_instances[dollar_label_count] = 1;
981 dollar_label_defines[dollar_label_count] = 1;
982 ++dollar_label_count;
2b68b820 983}
6efd877d
KR
984
985/*
986 * dollar_label_name()
987 *
988 * Caller must copy returned name: we re-use the area for the next name.
989 *
2b68b820
KR
990 * The mth occurence of label n: is turned into the symbol "Ln^Am"
991 * where n is the label number and m is the instance number. "L" makes
992 * it a label discarded unless debugging and "^A"('\1') ensures no
993 * ordinary symbol SHOULD get the same name as a local label
994 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
6efd877d
KR
995 *
996 * fb labels get the same treatment, except that ^B is used in place of ^A.
997 */
998
999char * /* Return local label name. */
1000dollar_label_name (n, augend)
1001 register long n; /* we just saw "n$:" : n a number */
1002 register int augend; /* 0 for current instance, 1 for new instance */
1003{
1004 long i;
1005 /* Returned to caller, then copied. used for created names ("4f") */
1006 static char symbol_name_build[24];
1007 register char *p;
1008 register char *q;
1009 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1010
1011 know (n >= 0);
1012 know (augend == 0 || augend == 1);
1013 p = symbol_name_build;
1014 *p++ = 'L';
1015
1016 /* Next code just does sprintf( {}, "%d", n); */
1017 /* label number */
1018 q = symbol_name_temporary;
1019 for (*q++ = 0, i = n; i; ++q)
1020 {
1021 *q = i % 10 + '0';
1022 i /= 10;
1023 }
1024 while ((*p = *--q) != '\0')
1025 ++p;
1026
1027 *p++ = 1; /* ^A */
1028
1029 /* instance number */
1030 q = symbol_name_temporary;
1031 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1032 {
1033 *q = i % 10 + '0';
1034 i /= 10;
1035 }
1036 while ((*p++ = *--q) != '\0');;
1037
1038 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2b68b820
KR
1039 return symbol_name_build;
1040}
6efd877d 1041
6efd877d
KR
1042/*
1043 * Sombody else's idea of local labels. They are made by "n:" where n
1044 * is any decimal digit. Refer to them with
1045 * "nb" for previous (backward) n:
1046 * or "nf" for next (forward) n:.
1047 *
1048 * We do a little better and let n be any number, not just a single digit, but
1049 * since the other guy's assembler only does ten, we treat the first ten
1050 * specially.
1051 *
1052 * Like someone else's assembler, we have one set of local label counters for
1053 * entire assembly, not one set per (sub)segment like in most assemblers. This
1054 * implies that one can refer to a label in another segment, and indeed some
1055 * crufty compilers have done just that.
1056 *
1057 * Since there could be a LOT of these things, treat them as a sparse array.
1058 */
1059
1060#define FB_LABEL_SPECIAL (10)
1061
1062static long fb_low_counter[FB_LABEL_SPECIAL];
1063static long *fb_labels;
1064static long *fb_label_instances;
eec0de3f
KR
1065static long fb_label_count;
1066static long fb_label_max;
6efd877d
KR
1067
1068/* this must be more than FB_LABEL_SPECIAL */
1069#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1070
1071static void
1072fb_label_init ()
1073{
1074 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
6efd877d
KR
1075} /* fb_label_init() */
1076
1077/* add one to the instance number of this fb label */
1078void
1079fb_label_instance_inc (label)
1080 long label;
1081{
1082 long *i;
1083
1084 if (label < FB_LABEL_SPECIAL)
1085 {
1086 ++fb_low_counter[label];
1087 return;
1088 }
1089
e154ecf4 1090 if (fb_labels != NULL)
6efd877d 1091 {
e154ecf4
ILT
1092 for (i = fb_labels + FB_LABEL_SPECIAL;
1093 i < fb_labels + fb_label_count; ++i)
6efd877d 1094 {
e154ecf4
ILT
1095 if (*i == label)
1096 {
1097 ++fb_label_instances[i - fb_labels];
1098 return;
1099 } /* if we find it */
1100 } /* for each existing label */
1101 }
6efd877d
KR
1102
1103 /* if we get to here, we don't have label listed yet. */
1104
1105 if (fb_labels == NULL)
1106 {
1107 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1108 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1109 fb_label_max = FB_LABEL_BUMP_BY;
1110 fb_label_count = FB_LABEL_SPECIAL;
1111
1112 }
1113 else if (fb_label_count == fb_label_max)
1114 {
1115 fb_label_max += FB_LABEL_BUMP_BY;
1116 fb_labels = (long *) xrealloc ((char *) fb_labels,
1117 fb_label_max * sizeof (long));
1118 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1119 fb_label_max * sizeof (long));
1120 } /* if we needed to grow */
1121
1122 fb_labels[fb_label_count] = label;
1123 fb_label_instances[fb_label_count] = 1;
1124 ++fb_label_count;
1ecd6c4a 1125}
6efd877d
KR
1126
1127static long
1128fb_label_instance (label)
1129 long label;
1130{
1131 long *i;
1132
1133 if (label < FB_LABEL_SPECIAL)
1134 {
1135 return (fb_low_counter[label]);
1136 }
1137
e154ecf4 1138 if (fb_labels != NULL)
6efd877d 1139 {
e154ecf4
ILT
1140 for (i = fb_labels + FB_LABEL_SPECIAL;
1141 i < fb_labels + fb_label_count; ++i)
6efd877d 1142 {
e154ecf4
ILT
1143 if (*i == label)
1144 {
1145 return (fb_label_instances[i - fb_labels]);
1146 } /* if we find it */
1147 } /* for each existing label */
1148 }
6efd877d 1149
e154ecf4
ILT
1150 /* We didn't find the label, so this must be a reference to the
1151 first instance. */
1152 return 0;
2b68b820 1153}
6efd877d
KR
1154
1155/*
1156 * fb_label_name()
1157 *
1158 * Caller must copy returned name: we re-use the area for the next name.
1159 *
2b68b820
KR
1160 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1161 * where n is the label number and m is the instance number. "L" makes
1162 * it a label discarded unless debugging and "^B"('\2') ensures no
1163 * ordinary symbol SHOULD get the same name as a local label
1164 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
6efd877d 1165 *
2b68b820 1166 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
6efd877d
KR
1167
1168char * /* Return local label name. */
1169fb_label_name (n, augend)
1170 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1171 long augend; /* 0 for nb, 1 for n:, nf */
1172{
1173 long i;
1174 /* Returned to caller, then copied. used for created names ("4f") */
1175 static char symbol_name_build[24];
1176 register char *p;
1177 register char *q;
1178 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1179
1180 know (n >= 0);
1181 know (augend == 0 || augend == 1);
1182 p = symbol_name_build;
1183 *p++ = 'L';
1184
1185 /* Next code just does sprintf( {}, "%d", n); */
1186 /* label number */
1187 q = symbol_name_temporary;
1188 for (*q++ = 0, i = n; i; ++q)
1189 {
1190 *q = i % 10 + '0';
1191 i /= 10;
1192 }
1193 while ((*p = *--q) != '\0')
1194 ++p;
1195
1196 *p++ = 2; /* ^B */
1197
1198 /* instance number */
1199 q = symbol_name_temporary;
1200 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1201 {
1202 *q = i % 10 + '0';
1203 i /= 10;
1204 }
1205 while ((*p++ = *--q) != '\0');;
1206
1207 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1208 return (symbol_name_build);
1209} /* fb_label_name() */
1210
a6c6eaf8 1211/*
6efd877d
KR
1212 * decode name that may have been generated by foo_label_name() above. If
1213 * the name wasn't generated by foo_label_name(), then return it unaltered.
a6c6eaf8
RP
1214 * This is used for error messages.
1215 */
a39116f1 1216
6efd877d
KR
1217char *
1218decode_local_label_name (s)
1219 char *s;
a6c6eaf8 1220{
6efd877d
KR
1221 char *p;
1222 char *symbol_decode;
1223 int label_number;
1224 int instance_number;
1225 char *type;
1226 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1227
1228 if (s[0] != 'L')
56dc989a 1229 return s;
6efd877d
KR
1230
1231 for (label_number = 0, p = s + 1; isdigit (*p); ++p)
56dc989a 1232 label_number = (10 * label_number) + *p - '0';
6efd877d
KR
1233
1234 if (*p == 1)
56dc989a 1235 type = "dollar";
6efd877d 1236 else if (*p == 2)
56dc989a 1237 type = "fb";
6efd877d 1238 else
56dc989a 1239 return s;
6efd877d 1240
56dc989a
ILT
1241 for (instance_number = 0, p++; isdigit (*p); ++p)
1242 instance_number = (10 * instance_number) + *p - '0';
6efd877d
KR
1243
1244 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
56dc989a 1245 sprintf (symbol_decode, message_format, label_number, instance_number, type);
6efd877d 1246
56dc989a
ILT
1247 return symbol_decode;
1248}
a6c6eaf8 1249
85051959
ILT
1250/* Get the value of a symbol. */
1251
1252valueT
1253S_GET_VALUE (s)
1254 symbolS *s;
1255{
e702f6e6
KR
1256 if (!s->sy_resolved && !s->sy_resolving && s->sy_value.X_op != O_constant)
1257 resolve_symbol_value (s);
5ac34ac3 1258 if (s->sy_value.X_op != O_constant)
943bdfdc
ILT
1259 {
1260 static symbolS *recur;
1261
1262 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1263 may call S_GET_VALUE. We use a static symbol to avoid the
1264 immediate recursion. */
1265 if (recur == s)
1266 return (valueT) s->sy_value.X_add_number;
1267 recur = s;
1268 if (! s->sy_resolved
1269 || s->sy_value.X_op != O_symbol
1270 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1271 as_bad ("Attempt to get value of unresolved symbol %s",
1272 S_GET_NAME (s));
1273 recur = NULL;
1274 }
85051959
ILT
1275 return (valueT) s->sy_value.X_add_number;
1276}
1277
1278/* Set the value of a symbol. */
1279
1280void
1281S_SET_VALUE (s, val)
1282 symbolS *s;
1283 valueT val;
1284{
5ac34ac3 1285 s->sy_value.X_op = O_constant;
85051959 1286 s->sy_value.X_add_number = (offsetT) val;
0cafaab1 1287 s->sy_value.X_unsigned = 0;
85051959
ILT
1288}
1289
e702f6e6
KR
1290void
1291copy_symbol_attributes (dest, src)
1292 symbolS *dest, *src;
1293{
1294#ifdef BFD_ASSEMBLER
1295 /* In an expression, transfer the settings of these flags.
1296 The user can override later, of course. */
1297#define COPIED_SYMFLAGS (BSF_FUNCTION)
1298 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1299#endif
1300
1301#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1302 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1303#endif
1304}
1305
2b68b820
KR
1306#ifdef BFD_ASSEMBLER
1307
1308int
1309S_IS_EXTERNAL (s)
1310 symbolS *s;
1311{
1312 flagword flags = s->bsym->flags;
1313
1314 /* sanity check */
0cafaab1 1315 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
2b68b820
KR
1316 abort ();
1317
0cafaab1 1318 return (flags & BSF_GLOBAL) != 0;
2b68b820
KR
1319}
1320
943bdfdc
ILT
1321int
1322S_IS_WEAK (s)
1323 symbolS *s;
1324{
1325 return (s->bsym->flags & BSF_WEAK) != 0;
1326}
1327
2b68b820
KR
1328int
1329S_IS_COMMON (s)
1330 symbolS *s;
1331{
0cafaab1 1332 return bfd_is_com_section (s->bsym->section);
2b68b820
KR
1333}
1334
1335int
1336S_IS_DEFINED (s)
1337 symbolS *s;
1338{
1339 return s->bsym->section != undefined_section;
1340}
1341
1342int
1343S_IS_DEBUG (s)
1344 symbolS *s;
1345{
1346 if (s->bsym->flags & BSF_DEBUGGING)
1347 return 1;
1348 return 0;
1349}
1350
1351int
1352S_IS_LOCAL (s)
1353 symbolS *s;
1354{
1355 flagword flags = s->bsym->flags;
1356d77d 1356 const char *name;
2b68b820
KR
1357
1358 /* sanity check */
0cafaab1 1359 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
2b68b820
KR
1360 abort ();
1361
2a0f64a5
ILT
1362 if (bfd_get_section (s->bsym) == reg_section)
1363 return 1;
1364
1356d77d
ILT
1365 name = S_GET_NAME (s);
1366 return (name != NULL
2b68b820 1367 && ! S_IS_DEBUG (s)
1356d77d
ILT
1368 && (strchr (name, '\001')
1369 || strchr (name, '\002')
1370 || (! flag_keep_locals
e95ef7c1 1371 && (bfd_is_local_label (stdoutput, s->bsym)
1356d77d
ILT
1372 || (flag_mri
1373 && name[0] == '?'
1374 && name[1] == '?')))));
2b68b820
KR
1375}
1376
1377int
1378S_IS_EXTERN (s)
1379 symbolS *s;
1380{
1381 return S_IS_EXTERNAL (s);
1382}
1383
1384int
1385S_IS_STABD (s)
1386 symbolS *s;
1387{
1388 return S_GET_NAME (s) == 0;
1389}
1390
2b68b820
KR
1391CONST char *
1392S_GET_NAME (s)
1393 symbolS *s;
1394{
1395 return s->bsym->name;
1396}
1397
1398segT
1399S_GET_SEGMENT (s)
1400 symbolS *s;
1401{
1402 return s->bsym->section;
1403}
1404
2b68b820
KR
1405void
1406S_SET_SEGMENT (s, seg)
1407 symbolS *s;
1408 segT seg;
1409{
1410 s->bsym->section = seg;
1411}
1412
1413void
1414S_SET_EXTERNAL (s)
1415 symbolS *s;
1416{
56dc989a 1417 if ((s->bsym->flags & BSF_WEAK) != 0)
c0b34702 1418 {
5ca547dc 1419 /* Let .weak override .global. */
c0b34702
ILT
1420 return;
1421 }
1ecd6c4a
KR
1422 s->bsym->flags |= BSF_GLOBAL;
1423 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
2b68b820
KR
1424}
1425
1426void
1427S_CLEAR_EXTERNAL (s)
1428 symbolS *s;
1429{
56dc989a 1430 if ((s->bsym->flags & BSF_WEAK) != 0)
c0b34702 1431 {
5ca547dc 1432 /* Let .weak override. */
c0b34702
ILT
1433 return;
1434 }
2b68b820 1435 s->bsym->flags |= BSF_LOCAL;
1ecd6c4a
KR
1436 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1437}
1438
1439void
1440S_SET_WEAK (s)
1441 symbolS *s;
1442{
1443 s->bsym->flags |= BSF_WEAK;
1444 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
2b68b820
KR
1445}
1446
1447void
1448S_SET_NAME (s, name)
1449 symbolS *s;
1450 char *name;
1451{
1452 s->bsym->name = name;
1453}
1454#endif /* BFD_ASSEMBLER */
1455
eec0de3f
KR
1456void
1457symbol_begin ()
1458{
1459 symbol_lastP = NULL;
1460 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
1461 sy_hash = hash_new ();
dacf29ea 1462
eec0de3f
KR
1463 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1464#ifdef BFD_ASSEMBLER
dacf29ea 1465#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
eec0de3f 1466 abs_symbol.bsym = bfd_abs_section.symbol;
dacf29ea 1467#endif
eec0de3f
KR
1468#else
1469 /* Can't initialise a union. Sigh. */
1470 S_SET_SEGMENT (&abs_symbol, absolute_section);
1471#endif
dacf29ea 1472 abs_symbol.sy_value.X_op = O_constant;
db317fe7 1473 abs_symbol.sy_frag = &zero_address_frag;
dacf29ea 1474
56dc989a
ILT
1475 if (LOCAL_LABELS_FB)
1476 fb_label_init ();
eec0de3f
KR
1477}
1478
e702f6e6
KR
1479\f
1480int indent_level;
dacf29ea 1481
56dc989a
ILT
1482#if 0
1483
dacf29ea
KR
1484static void
1485indent ()
1486{
1487 printf ("%*s", indent_level * 4, "");
1488}
1489
56dc989a
ILT
1490#endif
1491
dacf29ea
KR
1492void print_expr_1 PARAMS ((FILE *, expressionS *));
1493void print_symbol_value_1 PARAMS ((FILE *, symbolS *));
1494
1495void
1496print_symbol_value_1 (file, sym)
1497 FILE *file;
1498 symbolS *sym;
1499{
1500 const char *name = S_GET_NAME (sym);
1501 if (!name || !name[0])
1502 name = "(unnamed)";
56dc989a 1503 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
e702f6e6
KR
1504 if (sym->sy_frag != &zero_address_frag)
1505 fprintf (file, " frag %lx", (long) sym->sy_frag);
dacf29ea
KR
1506 if (sym->written)
1507 fprintf (file, " written");
1508 if (sym->sy_resolved)
1509 fprintf (file, " resolved");
e702f6e6 1510 else if (sym->sy_resolving)
dacf29ea
KR
1511 fprintf (file, " resolving");
1512 if (sym->sy_used_in_reloc)
1513 fprintf (file, " used-in-reloc");
1514 if (sym->sy_used)
1515 fprintf (file, " used");
87e48495
KR
1516 if (S_IS_LOCAL (sym))
1517 fprintf (file, " local");
1518 if (S_IS_EXTERN (sym))
1519 fprintf (file, " extern");
1520 if (S_IS_DEBUG (sym))
1521 fprintf (file, " debug");
1522 if (S_IS_DEFINED (sym))
1523 fprintf (file, " defined");
e702f6e6 1524 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
dacf29ea
KR
1525 if (sym->sy_resolved)
1526 {
e702f6e6
KR
1527 segT s = S_GET_SEGMENT (sym);
1528
1529 if (s != undefined_section
1530 && s != expr_section)
1531 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
dacf29ea 1532 }
e702f6e6 1533 else if (indent_level < 8 && S_GET_SEGMENT (sym) != undefined_section)
dacf29ea
KR
1534 {
1535 indent_level++;
1536 fprintf (file, "\n%*s<", indent_level * 4, "");
1537 print_expr_1 (file, &sym->sy_value);
1538 fprintf (file, ">");
1539 indent_level--;
1540 }
1541 fflush (file);
1542}
1543
1544void
1545print_symbol_value (sym)
1546 symbolS *sym;
1547{
1548 indent_level = 0;
1549 print_symbol_value_1 (stderr, sym);
1550 fprintf (stderr, "\n");
1551}
1552
1553void
1554print_expr_1 (file, exp)
1555 FILE *file;
1556 expressionS *exp;
1557{
1558 fprintf (file, "expr %lx ", (long) exp);
1559 switch (exp->X_op)
1560 {
1561 case O_illegal:
1562 fprintf (file, "illegal");
1563 break;
1564 case O_absent:
1565 fprintf (file, "absent");
1566 break;
1567 case O_constant:
1568 fprintf (file, "constant %lx", (long) exp->X_add_number);
1569 break;
1570 case O_symbol:
1571 indent_level++;
1572 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
1573 print_symbol_value_1 (file, exp->X_add_symbol);
1574 fprintf (file, ">");
1575 maybe_print_addnum:
dacf29ea 1576 if (exp->X_add_number)
e702f6e6 1577 fprintf (file, "\n%*s%lx", indent_level * 4, "",
dacf29ea 1578 (long) exp->X_add_number);
e702f6e6 1579 indent_level--;
dacf29ea
KR
1580 break;
1581 case O_register:
1582 fprintf (file, "register #%d", (int) exp->X_add_number);
1583 break;
1584 case O_big:
1585 fprintf (file, "big");
1586 break;
1587 case O_uminus:
1588 fprintf (file, "uminus -<");
1589 indent_level++;
1590 print_symbol_value_1 (file, exp->X_add_symbol);
1591 fprintf (file, ">");
1592 goto maybe_print_addnum;
1593 case O_bit_not:
1594 fprintf (file, "bit_not");
1595 break;
1596 case O_multiply:
1597 fprintf (file, "multiply");
1598 break;
1599 case O_divide:
1600 fprintf (file, "divide");
1601 break;
1602 case O_modulus:
1603 fprintf (file, "modulus");
1604 break;
1605 case O_left_shift:
1606 fprintf (file, "lshift");
1607 break;
1608 case O_right_shift:
1609 fprintf (file, "rshift");
1610 break;
1611 case O_bit_inclusive_or:
1612 fprintf (file, "bit_ior");
1613 break;
1614 case O_bit_exclusive_or:
1615 fprintf (file, "bit_xor");
1616 break;
1617 case O_bit_and:
1618 fprintf (file, "bit_and");
1619 break;
1356d77d
ILT
1620 case O_eq:
1621 fprintf (file, "eq");
1622 break;
1623 case O_ne:
1624 fprintf (file, "ne");
1625 break;
1626 case O_lt:
1627 fprintf (file, "lt");
1628 break;
1629 case O_le:
1630 fprintf (file, "le");
1631 break;
1632 case O_ge:
1633 fprintf (file, "ge");
1634 break;
1635 case O_gt:
1636 fprintf (file, "gt");
1637 break;
73255941
ILT
1638 case O_logical_and:
1639 fprintf (file, "logical_and");
1640 break;
1641 case O_logical_or:
1642 fprintf (file, "logical_or");
1643 break;
dacf29ea
KR
1644 case O_add:
1645 indent_level++;
1646 fprintf (file, "add\n%*s<", indent_level * 4, "");
1647 print_symbol_value_1 (file, exp->X_add_symbol);
1648 fprintf (file, ">\n%*s<", indent_level * 4, "");
1649 print_symbol_value_1 (file, exp->X_op_symbol);
1650 fprintf (file, ">");
1651 goto maybe_print_addnum;
1652 case O_subtract:
1653 indent_level++;
1654 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
1655 print_symbol_value_1 (file, exp->X_add_symbol);
1656 fprintf (file, ">\n%*s<", indent_level * 4, "");
1657 print_symbol_value_1 (file, exp->X_op_symbol);
1658 fprintf (file, ">");
1659 goto maybe_print_addnum;
1660 default:
1661 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
1662 break;
1663 }
1664 fflush (stdout);
1665}
1666
1667void
1668print_expr (exp)
1669 expressionS *exp;
1670{
1671 print_expr_1 (stderr, exp);
1672 fprintf (stderr, "\n");
1673}
1674
943bdfdc
ILT
1675void
1676symbol_print_statistics (file)
1677 FILE *file;
1678{
1679 hash_print_statistics (file, "symbol table", sy_hash);
1680}
1681
8b228fe9 1682/* end of symbols.c */
This page took 0.284453 seconds and 4 git commands to generate.