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