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