* expr.c (expr): Move code setting "retval" to the end of the loop,
[deliverable/binutils-gdb.git] / gas / symbols.c
CommitLineData
252b5132 1/* symbols.c -symbol table-
f7e42eb4
NC
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001
252b5132
RH
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
22
7c743825 23/* #define DEBUG_SYMS / * to debug symbol list maintenance. */
252b5132
RH
24
25#include <ctype.h>
26
27#include "as.h"
28
29#include "obstack.h" /* For "symbols.h" */
30#include "subsegs.h"
31
49309057
ILT
32#include "struc-symbol.h"
33
252b5132
RH
34/* This is non-zero if symbols are case sensitive, which is the
35 default. */
36int symbols_case_sensitive = 1;
37
38#ifndef WORKING_DOT_WORD
39extern int new_broken_words;
40#endif
41
42/* symbol-name => struct symbol pointer */
43static struct hash_control *sy_hash;
44
49309057
ILT
45/* Table of local symbols. */
46static struct hash_control *local_hash;
47
7c743825 48/* Below are commented in "symbols.h". */
252b5132
RH
49symbolS *symbol_rootP;
50symbolS *symbol_lastP;
51symbolS abs_symbol;
52
53#ifdef DEBUG_SYMS
54#define debug_verify_symchain verify_symbol_chain
55#else
56#define debug_verify_symchain(root, last) ((void) 0)
57#endif
58
aa257fcd
NC
59#define DOLLAR_LABEL_CHAR '\001'
60#define LOCAL_LABEL_CHAR '\002'
61
252b5132
RH
62struct obstack notes;
63
8d28c9d7 64static char *save_symbol_name PARAMS ((const char *));
252b5132
RH
65static void fb_label_init PARAMS ((void));
66static long dollar_label_instance PARAMS ((long));
67static long fb_label_instance PARAMS ((long));
68
69static void print_binary PARAMS ((FILE *, const char *, expressionS *));
70
7c743825 71/* Return a pointer to a new symbol. Die if we can't make a new
252b5132
RH
72 symbol. Fill in the symbol's values. Add symbol to end of symbol
73 chain.
7c743825 74
252b5132
RH
75 This function should be called in the general case of creating a
76 symbol. However, if the output file symbol table has already been
77 set, and you are certain that this symbol won't be wanted in the
78 output file, you can call symbol_create. */
79
80symbolS *
81symbol_new (name, segment, valu, frag)
82 const char *name;
83 segT segment;
84 valueT valu;
85 fragS *frag;
86{
87 symbolS *symbolP = symbol_create (name, segment, valu, frag);
88
7c743825 89 /* Link to end of symbol chain. */
252b5132
RH
90#ifdef BFD_ASSEMBLER
91 {
92 extern int symbol_table_frozen;
93 if (symbol_table_frozen)
94 abort ();
95 }
96#endif
97 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
98
99 return symbolP;
100}
101
49309057
ILT
102/* Save a symbol name on a permanent obstack, and convert it according
103 to the object file format. */
104
105static char *
106save_symbol_name (name)
107 const char *name;
252b5132
RH
108{
109 unsigned int name_length;
49309057 110 char *ret;
252b5132 111
7c743825 112 name_length = strlen (name) + 1; /* +1 for \0. */
252b5132 113 obstack_grow (&notes, name, name_length);
49309057
ILT
114 ret = obstack_finish (&notes);
115
252b5132 116#ifdef STRIP_UNDERSCORE
49309057
ILT
117 if (ret[0] == '_')
118 ++ret;
252b5132
RH
119#endif
120
121#ifdef tc_canonicalize_symbol_name
49309057 122 ret = tc_canonicalize_symbol_name (ret);
252b5132
RH
123#endif
124
125 if (! symbols_case_sensitive)
126 {
127 unsigned char *s;
128
49309057 129 for (s = (unsigned char *) ret; *s != '\0'; s++)
252b5132
RH
130 if (islower (*s))
131 *s = toupper (*s);
132 }
133
49309057
ILT
134 return ret;
135}
136
137symbolS *
138symbol_create (name, segment, valu, frag)
7c743825
KH
139 const char *name; /* It is copied, the caller can destroy/modify. */
140 segT segment; /* Segment identifier (SEG_<something>). */
141 valueT valu; /* Symbol value. */
142 fragS *frag; /* Associated fragment. */
49309057
ILT
143{
144 char *preserved_copy_of_name;
145 symbolS *symbolP;
146
147 preserved_copy_of_name = save_symbol_name (name);
148
252b5132
RH
149 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
150
7c743825 151 /* symbol must be born in some fixed state. This seems as good as any. */
252b5132
RH
152 memset (symbolP, 0, sizeof (symbolS));
153
154#ifdef BFD_ASSEMBLER
155 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
156 if (symbolP->bsym == NULL)
157 as_perror ("%s", "bfd_make_empty_symbol");
158 symbolP->bsym->udata.p = (PTR) symbolP;
159#endif
160 S_SET_NAME (symbolP, preserved_copy_of_name);
161
162 S_SET_SEGMENT (symbolP, segment);
163 S_SET_VALUE (symbolP, valu);
164 symbol_clear_list_pointers (symbolP);
165
166 symbolP->sy_frag = frag;
167#ifndef BFD_ASSEMBLER
168 symbolP->sy_number = ~0;
169 symbolP->sy_name_offset = (unsigned int) ~0;
170#endif
171
172 obj_symbol_new_hook (symbolP);
173
174#ifdef tc_symbol_new_hook
175 tc_symbol_new_hook (symbolP);
176#endif
177
178 return symbolP;
179}
180\f
49309057
ILT
181#ifdef BFD_ASSEMBLER
182
183/* Local symbol support. If we can get away with it, we keep only a
184 small amount of information for local symbols. */
185
186static struct local_symbol *local_symbol_make PARAMS ((const char *, segT,
187 valueT, fragS *));
188static symbolS *local_symbol_convert PARAMS ((struct local_symbol *));
189
190/* Used for statistics. */
191
192static unsigned long local_symbol_count;
193static unsigned long local_symbol_conversion_count;
194
195/* This macro is called with a symbol argument passed by reference.
196 It returns whether this is a local symbol. If necessary, it
197 changes its argument to the real symbol. */
198
199#define LOCAL_SYMBOL_CHECK(s) \
200 (s->bsym == NULL \
201 ? (local_symbol_converted_p ((struct local_symbol *) s) \
202 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
203 0) \
204 : 1) \
205 : 0)
206
207/* Create a local symbol and insert it into the local hash table. */
208
209static struct local_symbol *
bd780143 210local_symbol_make (name, section, value, frag)
49309057
ILT
211 const char *name;
212 segT section;
bd780143 213 valueT value;
49309057
ILT
214 fragS *frag;
215{
216 char *name_copy;
217 struct local_symbol *ret;
218
219 ++local_symbol_count;
220
221 name_copy = save_symbol_name (name);
222
223 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
224 ret->lsy_marker = NULL;
225 ret->lsy_name = name_copy;
226 ret->lsy_section = section;
227 local_symbol_set_frag (ret, frag);
bd780143 228 ret->lsy_value = value;
49309057
ILT
229
230 hash_jam (local_hash, name_copy, (PTR) ret);
231
232 return ret;
233}
234
235/* Convert a local symbol into a real symbol. Note that we do not
236 reclaim the space used by the local symbol. */
237
238static symbolS *
239local_symbol_convert (locsym)
240 struct local_symbol *locsym;
241{
242 symbolS *ret;
243
244 assert (locsym->lsy_marker == NULL);
245 if (local_symbol_converted_p (locsym))
246 return local_symbol_get_real_symbol (locsym);
247
248 ++local_symbol_conversion_count;
249
bd780143 250 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
49309057
ILT
251 local_symbol_get_frag (locsym));
252
253 if (local_symbol_resolved_p (locsym))
254 ret->sy_resolved = 1;
255
256 /* Local symbols are always either defined or used. */
257 ret->sy_used = 1;
258
8c5e1ccd
AO
259#ifdef TC_LOCAL_SYMFIELD_CONVERT
260 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
261#endif
262
49309057
ILT
263 symbol_table_insert (ret);
264
265 local_symbol_mark_converted (locsym);
266 local_symbol_set_real_symbol (locsym, ret);
267
268 hash_jam (local_hash, locsym->lsy_name, NULL);
269
270 return ret;
271}
272
273#else /* ! BFD_ASSEMBLER */
274
275#define LOCAL_SYMBOL_CHECK(s) 0
276#define local_symbol_convert(s) ((symbolS *) s)
277
278#endif /* ! BFD_ASSEMBLER */
279\f
7c743825
KH
280/* We have just seen "<name>:".
281 Creates a struct symbol unless it already exists.
282
283 Gripes if we are redefining a symbol incompatibly (and ignores it). */
252b5132 284
252b5132 285symbolS *
7c743825
KH
286colon (sym_name) /* Just seen "x:" - rattle symbols & frags. */
287 const char *sym_name; /* Symbol name, as a cannonical string. */
288 /* We copy this string: OK to alter later. */
252b5132 289{
7c743825 290 register symbolS *symbolP; /* Symbol we are working with. */
252b5132
RH
291
292 /* Sun local labels go out of scope whenever a non-local symbol is
293 defined. */
294 if (LOCAL_LABELS_DOLLAR)
295 {
296 int local;
297
298#ifdef BFD_ASSEMBLER
299 local = bfd_is_local_label_name (stdoutput, sym_name);
300#else
301 local = LOCAL_LABEL (sym_name);
302#endif
303
304 if (! local)
305 dollar_label_clear ();
306 }
307
308#ifndef WORKING_DOT_WORD
309 if (new_broken_words)
310 {
311 struct broken_word *a;
312 int possible_bytes;
313 fragS *frag_tmp;
314 char *frag_opcode;
315
316 extern const int md_short_jump_size;
317 extern const int md_long_jump_size;
318 possible_bytes = (md_short_jump_size
319 + new_broken_words * md_long_jump_size);
320
321 frag_tmp = frag_now;
322 frag_opcode = frag_var (rs_broken_word,
323 possible_bytes,
324 possible_bytes,
325 (relax_substateT) 0,
326 (symbolS *) broken_words,
327 (offsetT) 0,
328 NULL);
329
7c743825
KH
330 /* We want to store the pointer to where to insert the jump
331 table in the fr_opcode of the rs_broken_word frag. This
332 requires a little hackery. */
252b5132
RH
333 while (frag_tmp
334 && (frag_tmp->fr_type != rs_broken_word
335 || frag_tmp->fr_opcode))
336 frag_tmp = frag_tmp->fr_next;
337 know (frag_tmp);
338 frag_tmp->fr_opcode = frag_opcode;
339 new_broken_words = 0;
340
341 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
342 a->dispfrag = frag_tmp;
343 }
344#endif /* WORKING_DOT_WORD */
345
346 if ((symbolP = symbol_find (sym_name)) != 0)
347 {
348#ifdef RESOLVE_SYMBOL_REDEFINITION
349 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
350 return symbolP;
351#endif
7c743825 352 /* Now check for undefined symbols. */
49309057
ILT
353 if (LOCAL_SYMBOL_CHECK (symbolP))
354 {
a1605869 355#ifdef BFD_ASSEMBLER
49309057
ILT
356 struct local_symbol *locsym = (struct local_symbol *) symbolP;
357
358 if (locsym->lsy_section != undefined_section
359 && (local_symbol_get_frag (locsym) != frag_now
360 || locsym->lsy_section != now_seg
bd780143 361 || locsym->lsy_value != frag_now_fix ()))
49309057 362 {
0e389e77 363 as_bad (_("symbol `%s' is already defined"), sym_name);
49309057
ILT
364 return symbolP;
365 }
366
367 locsym->lsy_section = now_seg;
368 local_symbol_set_frag (locsym, frag_now);
bd780143 369 locsym->lsy_value = frag_now_fix ();
a1605869 370#endif
49309057
ILT
371 }
372 else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
252b5132
RH
373 {
374 if (S_GET_VALUE (symbolP) == 0)
375 {
376 symbolP->sy_frag = frag_now;
377#ifdef OBJ_VMS
7c743825 378 S_SET_OTHER (symbolP, const_flag);
252b5132
RH
379#endif
380 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
381 S_SET_SEGMENT (symbolP, now_seg);
382#ifdef N_UNDF
383 know (N_UNDF == 0);
7c743825 384#endif /* if we have one, it better be zero. */
252b5132
RH
385
386 }
387 else
388 {
7c743825
KH
389 /* There are still several cases to check:
390
391 A .comm/.lcomm symbol being redefined as initialized
392 data is OK
393
394 A .comm/.lcomm symbol being redefined with a larger
395 size is also OK
396
397 This only used to be allowed on VMS gas, but Sun cc
398 on the sparc also depends on it. */
252b5132
RH
399
400 if (((!S_IS_DEBUG (symbolP)
401 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
402 && S_IS_EXTERNAL (symbolP))
403 || S_GET_SEGMENT (symbolP) == bss_section)
404 && (now_seg == data_section
405 || now_seg == S_GET_SEGMENT (symbolP)))
406 {
7c743825 407 /* Select which of the 2 cases this is. */
252b5132
RH
408 if (now_seg != data_section)
409 {
7c743825
KH
410 /* New .comm for prev .comm symbol.
411
412 If the new size is larger we just change its
413 value. If the new size is smaller, we ignore
414 this symbol. */
252b5132
RH
415 if (S_GET_VALUE (symbolP)
416 < ((unsigned) frag_now_fix ()))
417 {
418 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
419 }
420 }
421 else
422 {
423 /* It is a .comm/.lcomm being converted to initialized
424 data. */
425 symbolP->sy_frag = frag_now;
426#ifdef OBJ_VMS
7c743825 427 S_SET_OTHER (symbolP, const_flag);
252b5132
RH
428#endif
429 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
7c743825 430 S_SET_SEGMENT (symbolP, now_seg); /* Keep N_EXT bit. */
252b5132
RH
431 }
432 }
433 else
434 {
4c63da97
AM
435#if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
436 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
437 static const char *od_buf = "";
252b5132 438#else
4c63da97
AM
439 char od_buf[100];
440 od_buf[0] = '\0';
441#ifdef BFD_ASSEMBLER
442 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
443#endif
444 sprintf(od_buf, "%d.%d.",
445 S_GET_OTHER (symbolP),
446 S_GET_DESC (symbolP));
447#endif
0e389e77 448 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
252b5132
RH
449 sym_name,
450 segment_name (S_GET_SEGMENT (symbolP)),
4c63da97 451 od_buf,
252b5132 452 (long) S_GET_VALUE (symbolP));
252b5132 453 }
7c743825 454 } /* if the undefined symbol has no value */
252b5132
RH
455 }
456 else
457 {
7c743825 458 /* Don't blow up if the definition is the same. */
252b5132
RH
459 if (!(frag_now == symbolP->sy_frag
460 && S_GET_VALUE (symbolP) == frag_now_fix ()
461 && S_GET_SEGMENT (symbolP) == now_seg))
0e389e77 462 as_bad (_("symbol `%s' is already defined"), sym_name);
d4887adc 463 }
252b5132
RH
464
465 }
49309057
ILT
466#ifdef BFD_ASSEMBLER
467 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
468 {
469 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
470 (valueT) frag_now_fix (),
471 frag_now);
472 }
473#endif /* BFD_ASSEMBLER */
252b5132
RH
474 else
475 {
476 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
477 frag_now);
478#ifdef OBJ_VMS
479 S_SET_OTHER (symbolP, const_flag);
480#endif /* OBJ_VMS */
481
482 symbol_table_insert (symbolP);
d4887adc 483 }
252b5132
RH
484
485 if (mri_common_symbol != NULL)
486 {
487 /* This symbol is actually being defined within an MRI common
488 section. This requires special handling. */
49309057
ILT
489 if (LOCAL_SYMBOL_CHECK (symbolP))
490 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
252b5132
RH
491 symbolP->sy_value.X_op = O_symbol;
492 symbolP->sy_value.X_add_symbol = mri_common_symbol;
493 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
494 symbolP->sy_frag = &zero_address_frag;
495 S_SET_SEGMENT (symbolP, expr_section);
496 symbolP->sy_mri_common = 1;
497 }
498
499#ifdef tc_frob_label
500 tc_frob_label (symbolP);
501#endif
502#ifdef obj_frob_label
503 obj_frob_label (symbolP);
504#endif
505
506 return symbolP;
507}
508\f
7c743825 509/* Die if we can't insert the symbol. */
252b5132 510
7c743825 511void
252b5132
RH
512symbol_table_insert (symbolP)
513 symbolS *symbolP;
514{
515 register const char *error_string;
516
517 know (symbolP);
518 know (S_GET_NAME (symbolP));
519
49309057
ILT
520 if (LOCAL_SYMBOL_CHECK (symbolP))
521 {
522 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
523 (PTR) symbolP);
524 if (error_string != NULL)
0e389e77 525 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
49309057
ILT
526 S_GET_NAME (symbolP), error_string);
527 return;
528 }
529
252b5132
RH
530 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
531 {
0e389e77 532 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
252b5132 533 S_GET_NAME (symbolP), error_string);
7c743825
KH
534 } /* on error */
535}
252b5132 536\f
7c743825
KH
537/* If a symbol name does not exist, create it as undefined, and insert
538 it into the symbol table. Return a pointer to it. */
539
252b5132
RH
540symbolS *
541symbol_find_or_make (name)
542 const char *name;
543{
544 register symbolS *symbolP;
545
546 symbolP = symbol_find (name);
547
548 if (symbolP == NULL)
549 {
49309057
ILT
550#ifdef BFD_ASSEMBLER
551 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
552 {
553 symbolP = md_undefined_symbol ((char *) name);
554 if (symbolP != NULL)
555 return symbolP;
556
557 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
558 (valueT) 0,
559 &zero_address_frag);
560 return symbolP;
561 }
562#endif
563
252b5132
RH
564 symbolP = symbol_make (name);
565
566 symbol_table_insert (symbolP);
567 } /* if symbol wasn't found */
568
569 return (symbolP);
7c743825 570}
252b5132
RH
571
572symbolS *
573symbol_make (name)
574 CONST char *name;
575{
576 symbolS *symbolP;
577
7c743825 578 /* Let the machine description default it, e.g. for register names. */
252b5132
RH
579 symbolP = md_undefined_symbol ((char *) name);
580
581 if (!symbolP)
582 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
583
584 return (symbolP);
7c743825 585}
252b5132 586
7c743825
KH
587/* Implement symbol table lookup.
588 In: A symbol's name as a string: '\0' can't be part of a symbol name.
589 Out: NULL if the name was not in the symbol table, else the address
590 of a struct symbol associated with that name. */
252b5132
RH
591
592symbolS *
593symbol_find (name)
594 CONST char *name;
595{
596#ifdef STRIP_UNDERSCORE
597 return (symbol_find_base (name, 1));
598#else /* STRIP_UNDERSCORE */
599 return (symbol_find_base (name, 0));
600#endif /* STRIP_UNDERSCORE */
7c743825 601}
252b5132
RH
602
603symbolS *
604symbol_find_base (name, strip_underscore)
605 CONST char *name;
606 int strip_underscore;
607{
608 if (strip_underscore && *name == '_')
609 name++;
610
611#ifdef tc_canonicalize_symbol_name
612 {
613 char *copy;
03987ced 614 size_t len = strlen (name) + 1;
252b5132 615
03987ced
RH
616 copy = (char *) alloca (len);
617 memcpy (copy, name, len);
252b5132
RH
618 name = tc_canonicalize_symbol_name (copy);
619 }
620#endif
621
622 if (! symbols_case_sensitive)
623 {
03987ced
RH
624 char *copy;
625 const char *orig;
626 unsigned char c;
627
628 orig = name;
629 name = copy = (char *) alloca (strlen (name) + 1);
630
631 while ((c = *orig++) != '\0')
632 {
633 if (islower (c))
634 c = toupper (c);
635 *copy++ = c;
636 }
637 *copy = '\0';
252b5132
RH
638 }
639
a1605869
ILT
640#ifdef BFD_ASSEMBLER
641 {
642 struct local_symbol *locsym;
643
644 locsym = (struct local_symbol *) hash_find (local_hash, name);
645 if (locsym != NULL)
646 return (symbolS *) locsym;
647 }
648#endif
49309057 649
252b5132
RH
650 return ((symbolS *) hash_find (sy_hash, name));
651}
652
7c743825
KH
653/* Once upon a time, symbols were kept in a singly linked list. At
654 least coff needs to be able to rearrange them from time to time, for
655 which a doubly linked list is much more convenient. Loic did these
656 as macros which seemed dangerous to me so they're now functions.
657 xoxorich. */
658
659/* Link symbol ADDME after symbol TARGET in the chain. */
252b5132 660
7c743825 661void
252b5132
RH
662symbol_append (addme, target, rootPP, lastPP)
663 symbolS *addme;
664 symbolS *target;
665 symbolS **rootPP;
666 symbolS **lastPP;
667{
49309057
ILT
668 if (LOCAL_SYMBOL_CHECK (addme))
669 abort ();
670 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
671 abort ();
672
252b5132
RH
673 if (target == NULL)
674 {
675 know (*rootPP == NULL);
676 know (*lastPP == NULL);
677 addme->sy_next = NULL;
678#ifdef SYMBOLS_NEED_BACKPOINTERS
679 addme->sy_previous = NULL;
680#endif
681 *rootPP = addme;
682 *lastPP = addme;
683 return;
7c743825 684 } /* if the list is empty */
252b5132
RH
685
686 if (target->sy_next != NULL)
687 {
688#ifdef SYMBOLS_NEED_BACKPOINTERS
689 target->sy_next->sy_previous = addme;
690#endif /* SYMBOLS_NEED_BACKPOINTERS */
691 }
692 else
693 {
694 know (*lastPP == target);
695 *lastPP = addme;
7c743825 696 } /* if we have a next */
252b5132
RH
697
698 addme->sy_next = target->sy_next;
699 target->sy_next = addme;
700
701#ifdef SYMBOLS_NEED_BACKPOINTERS
702 addme->sy_previous = target;
703#endif /* SYMBOLS_NEED_BACKPOINTERS */
704
705 debug_verify_symchain (symbol_rootP, symbol_lastP);
706}
707
7c743825
KH
708/* Set the chain pointers of SYMBOL to null. */
709
710void
252b5132
RH
711symbol_clear_list_pointers (symbolP)
712 symbolS *symbolP;
713{
49309057
ILT
714 if (LOCAL_SYMBOL_CHECK (symbolP))
715 abort ();
252b5132
RH
716 symbolP->sy_next = NULL;
717#ifdef SYMBOLS_NEED_BACKPOINTERS
718 symbolP->sy_previous = NULL;
719#endif
720}
721
722#ifdef SYMBOLS_NEED_BACKPOINTERS
7c743825
KH
723/* Remove SYMBOLP from the list. */
724
725void
252b5132
RH
726symbol_remove (symbolP, rootPP, lastPP)
727 symbolS *symbolP;
728 symbolS **rootPP;
729 symbolS **lastPP;
730{
49309057
ILT
731 if (LOCAL_SYMBOL_CHECK (symbolP))
732 abort ();
733
252b5132
RH
734 if (symbolP == *rootPP)
735 {
736 *rootPP = symbolP->sy_next;
7c743825 737 } /* if it was the root */
252b5132
RH
738
739 if (symbolP == *lastPP)
740 {
741 *lastPP = symbolP->sy_previous;
7c743825 742 } /* if it was the tail */
252b5132
RH
743
744 if (symbolP->sy_next != NULL)
745 {
746 symbolP->sy_next->sy_previous = symbolP->sy_previous;
7c743825 747 } /* if not last */
252b5132
RH
748
749 if (symbolP->sy_previous != NULL)
750 {
751 symbolP->sy_previous->sy_next = symbolP->sy_next;
7c743825 752 } /* if not first */
252b5132
RH
753
754 debug_verify_symchain (*rootPP, *lastPP);
755}
756
7c743825
KH
757/* Link symbol ADDME before symbol TARGET in the chain. */
758
759void
252b5132
RH
760symbol_insert (addme, target, rootPP, lastPP)
761 symbolS *addme;
762 symbolS *target;
763 symbolS **rootPP;
ab9da554 764 symbolS **lastPP ATTRIBUTE_UNUSED;
252b5132 765{
49309057
ILT
766 if (LOCAL_SYMBOL_CHECK (addme))
767 abort ();
768 if (LOCAL_SYMBOL_CHECK (target))
769 abort ();
770
252b5132
RH
771 if (target->sy_previous != NULL)
772 {
773 target->sy_previous->sy_next = addme;
774 }
775 else
776 {
777 know (*rootPP == target);
778 *rootPP = addme;
7c743825 779 } /* if not first */
252b5132
RH
780
781 addme->sy_previous = target->sy_previous;
782 target->sy_previous = addme;
783 addme->sy_next = target;
784
785 debug_verify_symchain (*rootPP, *lastPP);
786}
787
788#endif /* SYMBOLS_NEED_BACKPOINTERS */
789
7c743825 790void
252b5132
RH
791verify_symbol_chain (rootP, lastP)
792 symbolS *rootP;
793 symbolS *lastP;
794{
795 symbolS *symbolP = rootP;
796
797 if (symbolP == NULL)
798 return;
799
800 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
801 {
49309057
ILT
802#ifdef BFD_ASSEMBLER
803 assert (symbolP->bsym != NULL);
804#endif
252b5132
RH
805#ifdef SYMBOLS_NEED_BACKPOINTERS
806 assert (symbolP->sy_next->sy_previous == symbolP);
807#else
808 /* Walk the list anyways, to make sure pointers are still good. */
809 ;
810#endif /* SYMBOLS_NEED_BACKPOINTERS */
811 }
812
813 assert (lastP == symbolP);
814}
815
816void
817verify_symbol_chain_2 (sym)
818 symbolS *sym;
819{
820 symbolS *p = sym, *n = sym;
821#ifdef SYMBOLS_NEED_BACKPOINTERS
822 while (symbol_previous (p))
823 p = symbol_previous (p);
824#endif
825 while (symbol_next (n))
826 n = symbol_next (n);
827 verify_symbol_chain (p, n);
828}
829
830/* Resolve the value of a symbol. This is called during the final
831 pass over the symbol table to resolve any symbols with complex
832 values. */
833
834valueT
6386f3a7 835resolve_symbol_value (symp)
252b5132 836 symbolS *symp;
252b5132
RH
837{
838 int resolved;
839 valueT final_val;
840 segT final_seg;
841
a1605869 842#ifdef BFD_ASSEMBLER
49309057
ILT
843 if (LOCAL_SYMBOL_CHECK (symp))
844 {
845 struct local_symbol *locsym = (struct local_symbol *) symp;
846
bd780143 847 final_val = locsym->lsy_value;
49309057 848 if (local_symbol_resolved_p (locsym))
bd780143 849 return final_val;
49309057 850
bd780143 851 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
49309057 852
6386f3a7 853 if (finalize_syms)
49309057 854 {
bd780143 855 locsym->lsy_value = final_val;
49309057
ILT
856 local_symbol_mark_resolved (locsym);
857 }
858
859 return final_val;
860 }
a1605869 861#endif
49309057 862
252b5132
RH
863 if (symp->sy_resolved)
864 {
865 if (symp->sy_value.X_op == O_constant)
866 return (valueT) symp->sy_value.X_add_number;
867 else
868 return 0;
869 }
870
871 resolved = 0;
872 final_seg = S_GET_SEGMENT (symp);
873
874 if (symp->sy_resolving)
875 {
6386f3a7 876 if (finalize_syms)
0e389e77 877 as_bad (_("symbol definition loop encountered at `%s'"),
7c743825 878 S_GET_NAME (symp));
252b5132
RH
879 final_val = 0;
880 resolved = 1;
881 }
882 else
883 {
884 symbolS *add_symbol, *op_symbol;
885 offsetT left, right;
886 segT seg_left, seg_right;
887 operatorT op;
888
889 symp->sy_resolving = 1;
890
891 /* Help out with CSE. */
892 add_symbol = symp->sy_value.X_add_symbol;
893 op_symbol = symp->sy_value.X_op_symbol;
894 final_val = symp->sy_value.X_add_number;
895 op = symp->sy_value.X_op;
896
897 switch (op)
898 {
899 default:
900 BAD_CASE (op);
901 break;
902
903 case O_absent:
904 final_val = 0;
905 /* Fall through. */
906
907 case O_constant:
bea9907b 908 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
252b5132
RH
909 if (final_seg == expr_section)
910 final_seg = absolute_section;
911 resolved = 1;
912 break;
913
914 case O_symbol:
915 case O_symbol_rva:
6386f3a7 916 left = resolve_symbol_value (add_symbol);
e0890092
AM
917 seg_left = S_GET_SEGMENT (add_symbol);
918 if (finalize_syms)
919 symp->sy_value.X_op_symbol = NULL;
252b5132 920
e0890092 921 do_symbol:
252b5132
RH
922 if (symp->sy_mri_common)
923 {
924 /* This is a symbol inside an MRI common section. The
e0890092
AM
925 relocation routines are going to handle it specially.
926 Don't change the value. */
49309057 927 resolved = symbol_resolved_p (add_symbol);
252b5132
RH
928 break;
929 }
930
6386f3a7 931 if (finalize_syms && final_val == 0)
49309057
ILT
932 {
933 if (LOCAL_SYMBOL_CHECK (add_symbol))
934 add_symbol = local_symbol_convert ((struct local_symbol *)
935 add_symbol);
936 copy_symbol_attributes (symp, add_symbol);
937 }
252b5132 938
e0890092
AM
939 /* If we have equated this symbol to an undefined or common
940 symbol, keep X_op set to O_symbol, and don't change
941 X_add_number. This permits the routine which writes out
942 relocation to detect this case, and convert the
943 relocation to be against the symbol to which this symbol
944 is equated. */
252b5132
RH
945 if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
946 {
6386f3a7 947 if (finalize_syms)
252b5132 948 {
252b5132
RH
949 symp->sy_value.X_op = O_symbol;
950 symp->sy_value.X_add_symbol = add_symbol;
951 symp->sy_value.X_add_number = final_val;
e0890092
AM
952 /* Use X_op_symbol as a flag. */
953 symp->sy_value.X_op_symbol = add_symbol;
954 final_seg = seg_left;
252b5132
RH
955 }
956 final_val = 0;
49309057 957 resolved = symbol_resolved_p (add_symbol);
c89c8534 958 symp->sy_resolving = 0;
252b5132
RH
959 goto exit_dont_set_value;
960 }
e0890092
AM
961 else if (finalize_syms && final_seg == expr_section
962 && seg_left != expr_section)
963 {
964 /* If the symbol is an expression symbol, do similarly
965 as for undefined and common syms above. Handles
966 "sym +/- expr" where "expr" cannot be evaluated
967 immediately, and we want relocations to be against
968 "sym", eg. because it is weak. */
969 symp->sy_value.X_op = O_symbol;
970 symp->sy_value.X_add_symbol = add_symbol;
971 symp->sy_value.X_add_number = final_val;
972 symp->sy_value.X_op_symbol = add_symbol;
973 final_seg = seg_left;
974 final_val += symp->sy_frag->fr_address + left;
975 resolved = symbol_resolved_p (add_symbol);
976 symp->sy_resolving = 0;
977 goto exit_dont_set_value;
978 }
252b5132
RH
979 else
980 {
981 final_val += symp->sy_frag->fr_address + left;
982 if (final_seg == expr_section || final_seg == undefined_section)
e0890092 983 final_seg = seg_left;
252b5132
RH
984 }
985
49309057 986 resolved = symbol_resolved_p (add_symbol);
252b5132
RH
987 break;
988
989 case O_uminus:
990 case O_bit_not:
991 case O_logical_not:
6386f3a7 992 left = resolve_symbol_value (add_symbol);
252b5132
RH
993
994 if (op == O_uminus)
995 left = -left;
996 else if (op == O_logical_not)
997 left = !left;
998 else
999 left = ~left;
1000
1001 final_val += left + symp->sy_frag->fr_address;
1002 if (final_seg == expr_section || final_seg == undefined_section)
1003 final_seg = absolute_section;
1004
49309057 1005 resolved = symbol_resolved_p (add_symbol);
252b5132
RH
1006 break;
1007
1008 case O_multiply:
1009 case O_divide:
1010 case O_modulus:
1011 case O_left_shift:
1012 case O_right_shift:
1013 case O_bit_inclusive_or:
1014 case O_bit_or_not:
1015 case O_bit_exclusive_or:
1016 case O_bit_and:
1017 case O_add:
1018 case O_subtract:
1019 case O_eq:
1020 case O_ne:
1021 case O_lt:
1022 case O_le:
1023 case O_ge:
1024 case O_gt:
1025 case O_logical_and:
1026 case O_logical_or:
6386f3a7
AM
1027 left = resolve_symbol_value (add_symbol);
1028 right = resolve_symbol_value (op_symbol);
252b5132
RH
1029 seg_left = S_GET_SEGMENT (add_symbol);
1030 seg_right = S_GET_SEGMENT (op_symbol);
1031
1032 /* Simplify addition or subtraction of a constant by folding the
1033 constant into X_add_number. */
e0890092 1034 if (op == O_add)
252b5132
RH
1035 {
1036 if (seg_right == absolute_section)
1037 {
e0890092 1038 final_val += right;
252b5132
RH
1039 goto do_symbol;
1040 }
e0890092 1041 else if (seg_left == absolute_section)
252b5132 1042 {
252b5132
RH
1043 final_val += left;
1044 add_symbol = op_symbol;
1045 left = right;
e0890092
AM
1046 seg_left = seg_right;
1047 goto do_symbol;
1048 }
1049 }
1050 else if (op == O_subtract)
1051 {
1052 if (seg_right == absolute_section)
1053 {
1054 final_val -= right;
252b5132
RH
1055 goto do_symbol;
1056 }
1057 }
1058
e0890092
AM
1059 /* Equality and non-equality tests are permitted on anything.
1060 Subtraction, and other comparison operators are permitted if
1061 both operands are in the same section. Otherwise, both
1062 operands must be absolute. We already handled the case of
1063 addition or subtraction of a constant above. This will
1064 probably need to be changed for an object file format which
1065 supports arbitrary expressions, such as IEEE-695.
1066
1067 Don't emit messages unless we're finalizing the symbol value,
252b5132 1068 otherwise we may get the same message multiple times. */
e0890092
AM
1069 if (op != O_eq && op != O_ne
1070 && (seg_left != absolute_section
1071 || seg_right != absolute_section)
1072 && ((op != O_subtract
1073 && op != O_lt && op != O_le && op != O_ge && op != O_gt)
9b4d630b 1074 || seg_left != seg_right
e0890092
AM
1075 || (seg_left == undefined_section
1076 && add_symbol != op_symbol))
6386f3a7 1077 && finalize_syms)
252b5132
RH
1078 {
1079 char *file;
1080 unsigned int line;
1081
1082 if (expr_symbol_where (symp, &file, &line))
1083 {
1084 if (seg_left == undefined_section)
1085 as_bad_where (file, line,
0e389e77 1086 _("undefined symbol `%s' in operation"),
252b5132
RH
1087 S_GET_NAME (symp->sy_value.X_add_symbol));
1088 if (seg_right == undefined_section)
1089 as_bad_where (file, line,
0e389e77 1090 _("undefined symbol `%s' in operation"),
252b5132
RH
1091 S_GET_NAME (symp->sy_value.X_op_symbol));
1092 if (seg_left != undefined_section
1093 && seg_right != undefined_section)
7c743825
KH
1094 as_bad_where (file, line,
1095 _("invalid section for operation"));
252b5132
RH
1096 }
1097 else
1098 {
1099 if (seg_left == undefined_section)
0e389e77 1100 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
252b5132
RH
1101 S_GET_NAME (symp->sy_value.X_add_symbol),
1102 S_GET_NAME (symp));
1103 if (seg_right == undefined_section)
0e389e77 1104 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
252b5132
RH
1105 S_GET_NAME (symp->sy_value.X_op_symbol),
1106 S_GET_NAME (symp));
1107 if (seg_left != undefined_section
1108 && seg_right != undefined_section)
0e389e77 1109 as_bad (_("invalid section for operation setting `%s'"),
252b5132
RH
1110 S_GET_NAME (symp));
1111 }
1112 }
1113
1114 /* Check for division by zero. */
1115 if ((op == O_divide || op == O_modulus) && right == 0)
1116 {
1117 /* If seg_right is not absolute_section, then we've
e0890092 1118 already issued a warning about using a bad symbol. */
6386f3a7 1119 if (seg_right == absolute_section && finalize_syms)
252b5132
RH
1120 {
1121 char *file;
1122 unsigned int line;
1123
1124 if (expr_symbol_where (symp, &file, &line))
1125 as_bad_where (file, line, _("division by zero"));
1126 else
0e389e77 1127 as_bad (_("division by zero when setting `%s'"),
252b5132
RH
1128 S_GET_NAME (symp));
1129 }
1130
1131 right = 1;
1132 }
1133
1134 switch (symp->sy_value.X_op)
1135 {
1136 case O_multiply: left *= right; break;
1137 case O_divide: left /= right; break;
1138 case O_modulus: left %= right; break;
1139 case O_left_shift: left <<= right; break;
1140 case O_right_shift: left >>= right; break;
1141 case O_bit_inclusive_or: left |= right; break;
1142 case O_bit_or_not: left |= ~right; break;
1143 case O_bit_exclusive_or: left ^= right; break;
1144 case O_bit_and: left &= right; break;
1145 case O_add: left += right; break;
1146 case O_subtract: left -= right; break;
e0890092
AM
1147 case O_eq:
1148 case O_ne:
1149 left = (left == right && seg_left == seg_right
1150 && (seg_left != undefined_section
1151 || add_symbol == op_symbol)
1152 ? ~ (offsetT) 0 : 0);
1153 if (symp->sy_value.X_op == O_ne)
1154 left = ~left;
1155 break;
252b5132
RH
1156 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1157 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1158 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1159 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1160 case O_logical_and: left = left && right; break;
1161 case O_logical_or: left = left || right; break;
1162 default: abort ();
1163 }
1164
1165 final_val += symp->sy_frag->fr_address + left;
1166 if (final_seg == expr_section || final_seg == undefined_section)
1167 final_seg = absolute_section;
49309057
ILT
1168 resolved = (symbol_resolved_p (add_symbol)
1169 && symbol_resolved_p (op_symbol));
7c743825 1170 break;
252b5132
RH
1171
1172 case O_register:
1173 case O_big:
1174 case O_illegal:
1175 /* Give an error (below) if not in expr_section. We don't
1176 want to worry about expr_section symbols, because they
1177 are fictional (they are created as part of expression
1178 resolution), and any problems may not actually mean
1179 anything. */
1180 break;
1181 }
1182
1183 symp->sy_resolving = 0;
1184 }
1185
6386f3a7 1186 if (finalize_syms)
05bdb37e 1187 S_SET_VALUE (symp, final_val);
252b5132 1188
05bdb37e
AM
1189exit_dont_set_value:
1190 /* Always set the segment, even if not finalizing the value.
1191 The segment is used to determine whether a symbol is defined. */
252b5132 1192#if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
05bdb37e
AM
1193 /* The old a.out backend does not handle S_SET_SEGMENT correctly
1194 for a stab symbol, so we use this bad hack. */
1195 if (final_seg != S_GET_SEGMENT (symp))
252b5132 1196#endif
05bdb37e 1197 S_SET_SEGMENT (symp, final_seg);
252b5132 1198
252b5132 1199 /* Don't worry if we can't resolve an expr_section symbol. */
6386f3a7 1200 if (finalize_syms)
252b5132
RH
1201 {
1202 if (resolved)
1203 symp->sy_resolved = 1;
1204 else if (S_GET_SEGMENT (symp) != expr_section)
1205 {
0e389e77 1206 as_bad (_("can't resolve value for symbol `%s'"),
7c743825 1207 S_GET_NAME (symp));
252b5132
RH
1208 symp->sy_resolved = 1;
1209 }
1210 }
1211
1212 return final_val;
1213}
1214
49309057
ILT
1215#ifdef BFD_ASSEMBLER
1216
1217static void resolve_local_symbol PARAMS ((const char *, PTR));
1218
1219/* A static function passed to hash_traverse. */
1220
1221static void
1222resolve_local_symbol (key, value)
ab9da554 1223 const char *key ATTRIBUTE_UNUSED;
49309057
ILT
1224 PTR value;
1225{
1226 if (value != NULL)
6386f3a7 1227 resolve_symbol_value (value);
49309057
ILT
1228}
1229
1230#endif
1231
1232/* Resolve all local symbols. */
1233
1234void
1235resolve_local_symbol_values ()
1236{
1237#ifdef BFD_ASSEMBLER
1238 hash_traverse (local_hash, resolve_local_symbol);
1239#endif
1240}
1241
252b5132
RH
1242/* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1243 They are *really* local. That is, they go out of scope whenever we see a
1244 label that isn't local. Also, like fb labels, there can be multiple
1245 instances of a dollar label. Therefor, we name encode each instance with
1246 the instance number, keep a list of defined symbols separate from the real
1247 symbol table, and we treat these buggers as a sparse array. */
1248
1249static long *dollar_labels;
1250static long *dollar_label_instances;
1251static char *dollar_label_defines;
1252static unsigned long dollar_label_count;
1253static unsigned long dollar_label_max;
1254
7c743825 1255int
252b5132
RH
1256dollar_label_defined (label)
1257 long label;
1258{
1259 long *i;
1260
1261 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1262
1263 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1264 if (*i == label)
1265 return dollar_label_defines[i - dollar_labels];
1266
7c743825 1267 /* If we get here, label isn't defined. */
252b5132 1268 return 0;
7c743825 1269}
252b5132
RH
1270
1271static long
1272dollar_label_instance (label)
1273 long label;
1274{
1275 long *i;
1276
1277 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1278
1279 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1280 if (*i == label)
1281 return (dollar_label_instances[i - dollar_labels]);
1282
7c743825
KH
1283 /* If we get here, we haven't seen the label before.
1284 Therefore its instance count is zero. */
252b5132
RH
1285 return 0;
1286}
1287
7c743825 1288void
252b5132
RH
1289dollar_label_clear ()
1290{
1291 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1292}
1293
1294#define DOLLAR_LABEL_BUMP_BY 10
1295
7c743825 1296void
252b5132
RH
1297define_dollar_label (label)
1298 long label;
1299{
1300 long *i;
1301
1302 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1303 if (*i == label)
1304 {
1305 ++dollar_label_instances[i - dollar_labels];
1306 dollar_label_defines[i - dollar_labels] = 1;
1307 return;
1308 }
1309
7c743825 1310 /* If we get to here, we don't have label listed yet. */
252b5132
RH
1311
1312 if (dollar_labels == NULL)
1313 {
1314 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1315 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1316 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1317 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1318 dollar_label_count = 0;
1319 }
1320 else if (dollar_label_count == dollar_label_max)
1321 {
1322 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1323 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1324 dollar_label_max * sizeof (long));
1325 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1326 dollar_label_max * sizeof (long));
1327 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
7c743825 1328 } /* if we needed to grow */
252b5132
RH
1329
1330 dollar_labels[dollar_label_count] = label;
1331 dollar_label_instances[dollar_label_count] = 1;
1332 dollar_label_defines[dollar_label_count] = 1;
1333 ++dollar_label_count;
1334}
1335
7c743825
KH
1336/* Caller must copy returned name: we re-use the area for the next name.
1337
1338 The mth occurence of label n: is turned into the symbol "Ln^Am"
1339 where n is the label number and m is the instance number. "L" makes
1340 it a label discarded unless debugging and "^A"('\1') ensures no
1341 ordinary symbol SHOULD get the same name as a local label
1342 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1343
1344 fb labels get the same treatment, except that ^B is used in place
1345 of ^A. */
1346
1347char * /* Return local label name. */
252b5132 1348dollar_label_name (n, augend)
7c743825
KH
1349 register long n; /* we just saw "n$:" : n a number. */
1350 register int augend; /* 0 for current instance, 1 for new instance. */
252b5132
RH
1351{
1352 long i;
7c743825 1353 /* Returned to caller, then copied. Used for created names ("4f"). */
252b5132
RH
1354 static char symbol_name_build[24];
1355 register char *p;
1356 register char *q;
7c743825 1357 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
252b5132
RH
1358
1359 know (n >= 0);
1360 know (augend == 0 || augend == 1);
1361 p = symbol_name_build;
f84dd1f0
NC
1362#ifdef LOCAL_LABEL_PREFIX
1363 *p++ = LOCAL_LABEL_PREFIX;
1364#endif
252b5132
RH
1365 *p++ = 'L';
1366
7c743825
KH
1367 /* Next code just does sprintf( {}, "%d", n); */
1368 /* Label number. */
252b5132
RH
1369 q = symbol_name_temporary;
1370 for (*q++ = 0, i = n; i; ++q)
1371 {
1372 *q = i % 10 + '0';
1373 i /= 10;
1374 }
1375 while ((*p = *--q) != '\0')
1376 ++p;
1377
aa257fcd 1378 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
252b5132 1379
7c743825 1380 /* Instance number. */
252b5132
RH
1381 q = symbol_name_temporary;
1382 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1383 {
1384 *q = i % 10 + '0';
1385 i /= 10;
1386 }
1387 while ((*p++ = *--q) != '\0');;
1388
7c743825 1389 /* The label, as a '\0' ended string, starts at symbol_name_build. */
252b5132
RH
1390 return symbol_name_build;
1391}
1392
7c743825
KH
1393/* Sombody else's idea of local labels. They are made by "n:" where n
1394 is any decimal digit. Refer to them with
1395 "nb" for previous (backward) n:
1396 or "nf" for next (forward) n:.
1397
1398 We do a little better and let n be any number, not just a single digit, but
1399 since the other guy's assembler only does ten, we treat the first ten
1400 specially.
1401
1402 Like someone else's assembler, we have one set of local label counters for
1403 entire assembly, not one set per (sub)segment like in most assemblers. This
1404 implies that one can refer to a label in another segment, and indeed some
1405 crufty compilers have done just that.
1406
1407 Since there could be a LOT of these things, treat them as a sparse
1408 array. */
252b5132
RH
1409
1410#define FB_LABEL_SPECIAL (10)
1411
1412static long fb_low_counter[FB_LABEL_SPECIAL];
1413static long *fb_labels;
1414static long *fb_label_instances;
1415static long fb_label_count;
1416static long fb_label_max;
1417
7c743825 1418/* This must be more than FB_LABEL_SPECIAL. */
252b5132
RH
1419#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1420
7c743825 1421static void
252b5132
RH
1422fb_label_init ()
1423{
1424 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
7c743825 1425}
252b5132 1426
7c743825
KH
1427/* Add one to the instance number of this fb label. */
1428
1429void
252b5132
RH
1430fb_label_instance_inc (label)
1431 long label;
1432{
1433 long *i;
1434
1435 if (label < FB_LABEL_SPECIAL)
1436 {
1437 ++fb_low_counter[label];
1438 return;
1439 }
1440
1441 if (fb_labels != NULL)
1442 {
1443 for (i = fb_labels + FB_LABEL_SPECIAL;
1444 i < fb_labels + fb_label_count; ++i)
1445 {
1446 if (*i == label)
1447 {
1448 ++fb_label_instances[i - fb_labels];
1449 return;
7c743825
KH
1450 } /* if we find it */
1451 } /* for each existing label */
252b5132
RH
1452 }
1453
7c743825 1454 /* If we get to here, we don't have label listed yet. */
252b5132
RH
1455
1456 if (fb_labels == NULL)
1457 {
1458 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1459 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1460 fb_label_max = FB_LABEL_BUMP_BY;
1461 fb_label_count = FB_LABEL_SPECIAL;
1462
1463 }
1464 else if (fb_label_count == fb_label_max)
1465 {
1466 fb_label_max += FB_LABEL_BUMP_BY;
1467 fb_labels = (long *) xrealloc ((char *) fb_labels,
1468 fb_label_max * sizeof (long));
1469 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1470 fb_label_max * sizeof (long));
7c743825 1471 } /* if we needed to grow */
252b5132
RH
1472
1473 fb_labels[fb_label_count] = label;
1474 fb_label_instances[fb_label_count] = 1;
1475 ++fb_label_count;
1476}
1477
7c743825 1478static long
252b5132
RH
1479fb_label_instance (label)
1480 long label;
1481{
1482 long *i;
1483
1484 if (label < FB_LABEL_SPECIAL)
1485 {
1486 return (fb_low_counter[label]);
1487 }
1488
1489 if (fb_labels != NULL)
1490 {
1491 for (i = fb_labels + FB_LABEL_SPECIAL;
1492 i < fb_labels + fb_label_count; ++i)
1493 {
1494 if (*i == label)
1495 {
1496 return (fb_label_instances[i - fb_labels]);
7c743825
KH
1497 } /* if we find it */
1498 } /* for each existing label */
252b5132
RH
1499 }
1500
1501 /* We didn't find the label, so this must be a reference to the
1502 first instance. */
1503 return 0;
1504}
1505
7c743825
KH
1506/* Caller must copy returned name: we re-use the area for the next name.
1507
1508 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1509 where n is the label number and m is the instance number. "L" makes
1510 it a label discarded unless debugging and "^B"('\2') ensures no
1511 ordinary symbol SHOULD get the same name as a local label
1512 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1513
1514 dollar labels get the same treatment, except that ^A is used in
1515 place of ^B. */
1516
1517char * /* Return local label name. */
252b5132 1518fb_label_name (n, augend)
7c743825
KH
1519 long n; /* We just saw "n:", "nf" or "nb" : n a number. */
1520 long augend; /* 0 for nb, 1 for n:, nf. */
252b5132
RH
1521{
1522 long i;
7c743825 1523 /* Returned to caller, then copied. Used for created names ("4f"). */
252b5132
RH
1524 static char symbol_name_build[24];
1525 register char *p;
1526 register char *q;
7c743825 1527 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
252b5132
RH
1528
1529 know (n >= 0);
1530 know (augend == 0 || augend == 1);
1531 p = symbol_name_build;
aa257fcd
NC
1532#ifdef LOCAL_LABEL_PREFIX
1533 *p++ = LOCAL_LABEL_PREFIX;
1534#endif
252b5132
RH
1535 *p++ = 'L';
1536
7c743825
KH
1537 /* Next code just does sprintf( {}, "%d", n); */
1538 /* Label number. */
252b5132
RH
1539 q = symbol_name_temporary;
1540 for (*q++ = 0, i = n; i; ++q)
1541 {
1542 *q = i % 10 + '0';
1543 i /= 10;
1544 }
1545 while ((*p = *--q) != '\0')
1546 ++p;
1547
aa257fcd 1548 *p++ = LOCAL_LABEL_CHAR; /* ^B */
252b5132 1549
7c743825 1550 /* Instance number. */
252b5132
RH
1551 q = symbol_name_temporary;
1552 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1553 {
1554 *q = i % 10 + '0';
1555 i /= 10;
1556 }
1557 while ((*p++ = *--q) != '\0');;
1558
7c743825 1559 /* The label, as a '\0' ended string, starts at symbol_name_build. */
252b5132 1560 return (symbol_name_build);
7c743825 1561}
252b5132 1562
7c743825
KH
1563/* Decode name that may have been generated by foo_label_name() above.
1564 If the name wasn't generated by foo_label_name(), then return it
1565 unaltered. This is used for error messages. */
252b5132
RH
1566
1567char *
1568decode_local_label_name (s)
1569 char *s;
1570{
1571 char *p;
1572 char *symbol_decode;
1573 int label_number;
1574 int instance_number;
1575 char *type;
d95767bf 1576 const char *message_format;
aa257fcd 1577 int index = 0;
43ad3147 1578
aa257fcd
NC
1579#ifdef LOCAL_LABEL_PREFIX
1580 if (s[index] == LOCAL_LABEL_PREFIX)
1581 ++index;
1582#endif
43ad3147 1583
aa257fcd 1584 if (s[index] != 'L')
252b5132
RH
1585 return s;
1586
aa257fcd 1587 for (label_number = 0, p = s + index + 1; isdigit ((unsigned char) *p); ++p)
252b5132
RH
1588 label_number = (10 * label_number) + *p - '0';
1589
aa257fcd 1590 if (*p == DOLLAR_LABEL_CHAR)
252b5132 1591 type = "dollar";
aa257fcd 1592 else if (*p == LOCAL_LABEL_CHAR)
252b5132
RH
1593 type = "fb";
1594 else
1595 return s;
1596
1597 for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
1598 instance_number = (10 * instance_number) + *p - '0';
1599
d95767bf 1600 message_format = _("\"%d\" (instance number %d of a %s label)");
252b5132
RH
1601 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1602 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1603
1604 return symbol_decode;
1605}
1606
1607/* Get the value of a symbol. */
1608
1609valueT
1610S_GET_VALUE (s)
1611 symbolS *s;
1612{
a1605869 1613#ifdef BFD_ASSEMBLER
49309057 1614 if (LOCAL_SYMBOL_CHECK (s))
ac62c346 1615 return resolve_symbol_value (s);
a1605869 1616#endif
49309057 1617
ac62c346 1618 if (!s->sy_resolved)
e46d99eb 1619 {
6386f3a7
AM
1620 valueT val = resolve_symbol_value (s);
1621 if (!finalize_syms)
e46d99eb
AM
1622 return val;
1623 }
252b5132
RH
1624 if (s->sy_value.X_op != O_constant)
1625 {
1626 static symbolS *recur;
1627
1628 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1629 may call S_GET_VALUE. We use a static symbol to avoid the
1630 immediate recursion. */
1631 if (recur == s)
1632 return (valueT) s->sy_value.X_add_number;
1633 recur = s;
1634 if (! s->sy_resolved
1635 || s->sy_value.X_op != O_symbol
1636 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
0e389e77 1637 as_bad (_("attempt to get value of unresolved symbol `%s'"),
252b5132
RH
1638 S_GET_NAME (s));
1639 recur = NULL;
1640 }
1641 return (valueT) s->sy_value.X_add_number;
1642}
1643
1644/* Set the value of a symbol. */
1645
1646void
1647S_SET_VALUE (s, val)
1648 symbolS *s;
1649 valueT val;
1650{
a1605869 1651#ifdef BFD_ASSEMBLER
49309057
ILT
1652 if (LOCAL_SYMBOL_CHECK (s))
1653 {
bd780143 1654 ((struct local_symbol *) s)->lsy_value = val;
49309057
ILT
1655 return;
1656 }
a1605869 1657#endif
49309057 1658
252b5132
RH
1659 s->sy_value.X_op = O_constant;
1660 s->sy_value.X_add_number = (offsetT) val;
1661 s->sy_value.X_unsigned = 0;
1662}
1663
1664void
1665copy_symbol_attributes (dest, src)
1666 symbolS *dest, *src;
1667{
49309057 1668 if (LOCAL_SYMBOL_CHECK (dest))
7f2f689c 1669 dest = local_symbol_convert ((struct local_symbol *) dest);
49309057 1670 if (LOCAL_SYMBOL_CHECK (src))
7f2f689c 1671 src = local_symbol_convert ((struct local_symbol *) src);
49309057 1672
252b5132
RH
1673#ifdef BFD_ASSEMBLER
1674 /* In an expression, transfer the settings of these flags.
1675 The user can override later, of course. */
1676#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1677 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1678#endif
1679
1680#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1681 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1682#endif
1683}
1684
1685#ifdef BFD_ASSEMBLER
1686
1687int
1688S_IS_FUNCTION (s)
1689 symbolS *s;
1690{
49309057
ILT
1691 flagword flags;
1692
1693 if (LOCAL_SYMBOL_CHECK (s))
1694 return 0;
1695
1696 flags = s->bsym->flags;
252b5132
RH
1697
1698 return (flags & BSF_FUNCTION) != 0;
1699}
1700
1701int
1702S_IS_EXTERNAL (s)
1703 symbolS *s;
1704{
49309057
ILT
1705 flagword flags;
1706
1707 if (LOCAL_SYMBOL_CHECK (s))
1708 return 0;
1709
1710 flags = s->bsym->flags;
252b5132 1711
7c743825 1712 /* Sanity check. */
252b5132
RH
1713 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1714 abort ();
1715
1716 return (flags & BSF_GLOBAL) != 0;
1717}
1718
1719int
1720S_IS_WEAK (s)
1721 symbolS *s;
1722{
49309057
ILT
1723 if (LOCAL_SYMBOL_CHECK (s))
1724 return 0;
252b5132
RH
1725 return (s->bsym->flags & BSF_WEAK) != 0;
1726}
1727
1728int
1729S_IS_COMMON (s)
1730 symbolS *s;
1731{
49309057
ILT
1732 if (LOCAL_SYMBOL_CHECK (s))
1733 return 0;
252b5132
RH
1734 return bfd_is_com_section (s->bsym->section);
1735}
1736
1737int
1738S_IS_DEFINED (s)
1739 symbolS *s;
1740{
49309057
ILT
1741 if (LOCAL_SYMBOL_CHECK (s))
1742 return ((struct local_symbol *) s)->lsy_section != undefined_section;
252b5132
RH
1743 return s->bsym->section != undefined_section;
1744}
1745
1746int
1747S_IS_DEBUG (s)
1748 symbolS *s;
1749{
49309057
ILT
1750 if (LOCAL_SYMBOL_CHECK (s))
1751 return 0;
252b5132
RH
1752 if (s->bsym->flags & BSF_DEBUGGING)
1753 return 1;
1754 return 0;
1755}
1756
1757int
1758S_IS_LOCAL (s)
1759 symbolS *s;
1760{
49309057 1761 flagword flags;
252b5132
RH
1762 const char *name;
1763
49309057
ILT
1764 if (LOCAL_SYMBOL_CHECK (s))
1765 return 1;
1766
1767 flags = s->bsym->flags;
1768
7c743825 1769 /* Sanity check. */
252b5132
RH
1770 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1771 abort ();
1772
1773 if (bfd_get_section (s->bsym) == reg_section)
1774 return 1;
1775
1776 if (flag_strip_local_absolute
1777 && (flags & BSF_GLOBAL) == 0
1778 && bfd_get_section (s->bsym) == absolute_section)
1779 return 1;
1780
1781 name = S_GET_NAME (s);
1782 return (name != NULL
1783 && ! S_IS_DEBUG (s)
aa257fcd
NC
1784 && (strchr (name, DOLLAR_LABEL_CHAR)
1785 || strchr (name, LOCAL_LABEL_CHAR)
252b5132
RH
1786 || (! flag_keep_locals
1787 && (bfd_is_local_label (stdoutput, s->bsym)
1788 || (flag_mri
1789 && name[0] == '?'
1790 && name[1] == '?')))));
1791}
1792
1793int
1794S_IS_EXTERN (s)
1795 symbolS *s;
1796{
1797 return S_IS_EXTERNAL (s);
1798}
1799
1800int
1801S_IS_STABD (s)
1802 symbolS *s;
1803{
1804 return S_GET_NAME (s) == 0;
1805}
1806
1807CONST char *
1808S_GET_NAME (s)
1809 symbolS *s;
1810{
49309057
ILT
1811 if (LOCAL_SYMBOL_CHECK (s))
1812 return ((struct local_symbol *) s)->lsy_name;
252b5132
RH
1813 return s->bsym->name;
1814}
1815
1816segT
1817S_GET_SEGMENT (s)
1818 symbolS *s;
1819{
49309057
ILT
1820 if (LOCAL_SYMBOL_CHECK (s))
1821 return ((struct local_symbol *) s)->lsy_section;
252b5132
RH
1822 return s->bsym->section;
1823}
1824
1825void
1826S_SET_SEGMENT (s, seg)
1827 symbolS *s;
1828 segT seg;
1829{
1830 /* Don't reassign section symbols. The direct reason is to prevent seg
1831 faults assigning back to const global symbols such as *ABS*, but it
1832 shouldn't happen anyway. */
1833
49309057
ILT
1834 if (LOCAL_SYMBOL_CHECK (s))
1835 {
1836 if (seg == reg_section)
1837 s = local_symbol_convert ((struct local_symbol *) s);
1838 else
1839 {
1840 ((struct local_symbol *) s)->lsy_section = seg;
1841 return;
1842 }
1843 }
1844
252b5132
RH
1845 if (s->bsym->flags & BSF_SECTION_SYM)
1846 {
1847 if (s->bsym->section != seg)
7c743825 1848 abort ();
252b5132
RH
1849 }
1850 else
1851 s->bsym->section = seg;
1852}
1853
1854void
1855S_SET_EXTERNAL (s)
1856 symbolS *s;
1857{
49309057
ILT
1858 if (LOCAL_SYMBOL_CHECK (s))
1859 s = local_symbol_convert ((struct local_symbol *) s);
252b5132
RH
1860 if ((s->bsym->flags & BSF_WEAK) != 0)
1861 {
1862 /* Let .weak override .global. */
1863 return;
1864 }
4d7c34bf
NC
1865 if (s->bsym->flags & BSF_SECTION_SYM)
1866 {
1867 char * file;
1868 unsigned int line;
1869
1870 /* Do not reassign section symbols. */
1871 as_where (& file, & line);
1872 as_warn_where (file, line,
0e389e77 1873 _("section symbols are already global"));
4d7c34bf
NC
1874 return;
1875 }
252b5132 1876 s->bsym->flags |= BSF_GLOBAL;
7c743825 1877 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
252b5132
RH
1878}
1879
1880void
1881S_CLEAR_EXTERNAL (s)
1882 symbolS *s;
1883{
49309057
ILT
1884 if (LOCAL_SYMBOL_CHECK (s))
1885 return;
252b5132
RH
1886 if ((s->bsym->flags & BSF_WEAK) != 0)
1887 {
1888 /* Let .weak override. */
1889 return;
1890 }
1891 s->bsym->flags |= BSF_LOCAL;
7c743825 1892 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
252b5132
RH
1893}
1894
1895void
1896S_SET_WEAK (s)
1897 symbolS *s;
1898{
49309057
ILT
1899 if (LOCAL_SYMBOL_CHECK (s))
1900 s = local_symbol_convert ((struct local_symbol *) s);
252b5132 1901 s->bsym->flags |= BSF_WEAK;
7c743825 1902 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
252b5132
RH
1903}
1904
1905void
1906S_SET_NAME (s, name)
1907 symbolS *s;
1908 char *name;
1909{
49309057
ILT
1910 if (LOCAL_SYMBOL_CHECK (s))
1911 {
1912 ((struct local_symbol *) s)->lsy_name = name;
1913 return;
1914 }
252b5132
RH
1915 s->bsym->name = name;
1916}
1917#endif /* BFD_ASSEMBLER */
1918
49309057
ILT
1919#ifdef SYMBOLS_NEED_BACKPOINTERS
1920
1921/* Return the previous symbol in a chain. */
1922
1923symbolS *
1924symbol_previous (s)
1925 symbolS *s;
1926{
1927 if (LOCAL_SYMBOL_CHECK (s))
1928 abort ();
1929 return s->sy_previous;
1930}
1931
1932#endif /* SYMBOLS_NEED_BACKPOINTERS */
1933
1934/* Return the next symbol in a chain. */
1935
1936symbolS *
1937symbol_next (s)
1938 symbolS *s;
1939{
1940 if (LOCAL_SYMBOL_CHECK (s))
1941 abort ();
1942 return s->sy_next;
1943}
1944
1945/* Return a pointer to the value of a symbol as an expression. */
1946
1947expressionS *
1948symbol_get_value_expression (s)
1949 symbolS *s;
1950{
1951 if (LOCAL_SYMBOL_CHECK (s))
1952 s = local_symbol_convert ((struct local_symbol *) s);
1953 return &s->sy_value;
1954}
1955
1956/* Set the value of a symbol to an expression. */
1957
1958void
1959symbol_set_value_expression (s, exp)
1960 symbolS *s;
1961 const expressionS *exp;
1962{
1963 if (LOCAL_SYMBOL_CHECK (s))
1964 s = local_symbol_convert ((struct local_symbol *) s);
1965 s->sy_value = *exp;
1966}
1967
1968/* Set the frag of a symbol. */
1969
1970void
1971symbol_set_frag (s, f)
1972 symbolS *s;
1973 fragS *f;
1974{
a1605869 1975#ifdef BFD_ASSEMBLER
49309057
ILT
1976 if (LOCAL_SYMBOL_CHECK (s))
1977 {
1978 local_symbol_set_frag ((struct local_symbol *) s, f);
1979 return;
1980 }
a1605869 1981#endif
49309057
ILT
1982 s->sy_frag = f;
1983}
1984
1985/* Return the frag of a symbol. */
1986
1987fragS *
1988symbol_get_frag (s)
1989 symbolS *s;
1990{
a1605869 1991#ifdef BFD_ASSEMBLER
49309057
ILT
1992 if (LOCAL_SYMBOL_CHECK (s))
1993 return local_symbol_get_frag ((struct local_symbol *) s);
a1605869 1994#endif
49309057
ILT
1995 return s->sy_frag;
1996}
1997
1998/* Mark a symbol as having been used. */
1999
2000void
2001symbol_mark_used (s)
2002 symbolS *s;
2003{
2004 if (LOCAL_SYMBOL_CHECK (s))
2005 return;
2006 s->sy_used = 1;
2007}
2008
2009/* Clear the mark of whether a symbol has been used. */
2010
2011void
2012symbol_clear_used (s)
2013 symbolS *s;
2014{
2015 if (LOCAL_SYMBOL_CHECK (s))
2016 s = local_symbol_convert ((struct local_symbol *) s);
2017 s->sy_used = 0;
2018}
2019
2020/* Return whether a symbol has been used. */
2021
2022int
2023symbol_used_p (s)
2024 symbolS *s;
2025{
2026 if (LOCAL_SYMBOL_CHECK (s))
2027 return 1;
2028 return s->sy_used;
2029}
2030
2031/* Mark a symbol as having been used in a reloc. */
2032
2033void
2034symbol_mark_used_in_reloc (s)
2035 symbolS *s;
2036{
2037 if (LOCAL_SYMBOL_CHECK (s))
2038 s = local_symbol_convert ((struct local_symbol *) s);
2039 s->sy_used_in_reloc = 1;
2040}
2041
2042/* Clear the mark of whether a symbol has been used in a reloc. */
2043
2044void
2045symbol_clear_used_in_reloc (s)
2046 symbolS *s;
2047{
2048 if (LOCAL_SYMBOL_CHECK (s))
2049 return;
2050 s->sy_used_in_reloc = 0;
2051}
2052
2053/* Return whether a symbol has been used in a reloc. */
2054
2055int
2056symbol_used_in_reloc_p (s)
2057 symbolS *s;
2058{
2059 if (LOCAL_SYMBOL_CHECK (s))
2060 return 0;
2061 return s->sy_used_in_reloc;
2062}
2063
2064/* Mark a symbol as an MRI common symbol. */
2065
2066void
2067symbol_mark_mri_common (s)
2068 symbolS *s;
2069{
2070 if (LOCAL_SYMBOL_CHECK (s))
2071 s = local_symbol_convert ((struct local_symbol *) s);
2072 s->sy_mri_common = 1;
2073}
2074
2075/* Clear the mark of whether a symbol is an MRI common symbol. */
2076
2077void
2078symbol_clear_mri_common (s)
2079 symbolS *s;
2080{
2081 if (LOCAL_SYMBOL_CHECK (s))
2082 return;
2083 s->sy_mri_common = 0;
2084}
2085
2086/* Return whether a symbol is an MRI common symbol. */
2087
2088int
2089symbol_mri_common_p (s)
2090 symbolS *s;
2091{
2092 if (LOCAL_SYMBOL_CHECK (s))
2093 return 0;
2094 return s->sy_mri_common;
2095}
2096
2097/* Mark a symbol as having been written. */
2098
2099void
2100symbol_mark_written (s)
2101 symbolS *s;
2102{
2103 if (LOCAL_SYMBOL_CHECK (s))
2104 return;
2105 s->written = 1;
2106}
2107
2108/* Clear the mark of whether a symbol has been written. */
2109
2110void
2111symbol_clear_written (s)
2112 symbolS *s;
2113{
2114 if (LOCAL_SYMBOL_CHECK (s))
2115 return;
2116 s->written = 0;
2117}
2118
2119/* Return whether a symbol has been written. */
2120
2121int
2122symbol_written_p (s)
2123 symbolS *s;
2124{
2125 if (LOCAL_SYMBOL_CHECK (s))
2126 return 0;
2127 return s->written;
2128}
2129
2130/* Mark a symbol has having been resolved. */
2131
2132void
2133symbol_mark_resolved (s)
2134 symbolS *s;
2135{
a1605869 2136#ifdef BFD_ASSEMBLER
49309057
ILT
2137 if (LOCAL_SYMBOL_CHECK (s))
2138 {
2139 local_symbol_mark_resolved ((struct local_symbol *) s);
2140 return;
2141 }
a1605869 2142#endif
49309057
ILT
2143 s->sy_resolved = 1;
2144}
2145
2146/* Return whether a symbol has been resolved. */
2147
2148int
2149symbol_resolved_p (s)
2150 symbolS *s;
2151{
a1605869 2152#ifdef BFD_ASSEMBLER
49309057
ILT
2153 if (LOCAL_SYMBOL_CHECK (s))
2154 return local_symbol_resolved_p ((struct local_symbol *) s);
a1605869 2155#endif
49309057
ILT
2156 return s->sy_resolved;
2157}
2158
2159/* Return whether a symbol is a section symbol. */
2160
2161int
2162symbol_section_p (s)
a04b544b 2163 symbolS *s ATTRIBUTE_UNUSED;
49309057
ILT
2164{
2165 if (LOCAL_SYMBOL_CHECK (s))
2166 return 0;
2167#ifdef BFD_ASSEMBLER
2168 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2169#else
7c743825 2170 /* FIXME. */
49309057
ILT
2171 return 0;
2172#endif
2173}
2174
2175/* Return whether a symbol is equated to another symbol. */
2176
2177int
2178symbol_equated_p (s)
2179 symbolS *s;
2180{
2181 if (LOCAL_SYMBOL_CHECK (s))
2182 return 0;
2183 return s->sy_value.X_op == O_symbol;
2184}
2185
e0890092
AM
2186/* Return whether a symbol is equated to another symbol, and should be
2187 treated specially when writing out relocs. */
2188
2189int
2190symbol_equated_reloc_p (s)
2191 symbolS *s;
2192{
2193 if (LOCAL_SYMBOL_CHECK (s))
2194 return 0;
2195 /* X_op_symbol, normally not used for O_symbol, is set by
2196 resolve_symbol_value to flag expression syms that have been
2197 equated. */
2198 return (s->sy_value.X_op == O_symbol
2199 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2200 || ! S_IS_DEFINED (s)
2201 || S_IS_COMMON (s)));
2202}
2203
49309057
ILT
2204/* Return whether a symbol has a constant value. */
2205
2206int
2207symbol_constant_p (s)
2208 symbolS *s;
2209{
2210 if (LOCAL_SYMBOL_CHECK (s))
2211 return 1;
2212 return s->sy_value.X_op == O_constant;
2213}
2214
2215#ifdef BFD_ASSEMBLER
2216
2217/* Return the BFD symbol for a symbol. */
2218
2219asymbol *
2220symbol_get_bfdsym (s)
2221 symbolS *s;
2222{
2223 if (LOCAL_SYMBOL_CHECK (s))
2224 s = local_symbol_convert ((struct local_symbol *) s);
2225 return s->bsym;
2226}
2227
2228/* Set the BFD symbol for a symbol. */
2229
2230void
2231symbol_set_bfdsym (s, bsym)
2232 symbolS *s;
2233 asymbol *bsym;
2234{
2235 if (LOCAL_SYMBOL_CHECK (s))
2236 s = local_symbol_convert ((struct local_symbol *) s);
2237 s->bsym = bsym;
2238}
2239
2240#endif /* BFD_ASSEMBLER */
2241
2242#ifdef OBJ_SYMFIELD_TYPE
2243
2244/* Get a pointer to the object format information for a symbol. */
2245
2246OBJ_SYMFIELD_TYPE *
2247symbol_get_obj (s)
2248 symbolS *s;
2249{
2250 if (LOCAL_SYMBOL_CHECK (s))
2251 s = local_symbol_convert ((struct local_symbol *) s);
2252 return &s->sy_obj;
2253}
2254
2255/* Set the object format information for a symbol. */
2256
2257void
2258symbol_set_obj (s, o)
2259 symbolS *s;
2260 OBJ_SYMFIELD_TYPE *o;
2261{
2262 if (LOCAL_SYMBOL_CHECK (s))
2263 s = local_symbol_convert ((struct local_symbol *) s);
2264 s->sy_obj = *o;
2265}
2266
2267#endif /* OBJ_SYMFIELD_TYPE */
2268
2269#ifdef TC_SYMFIELD_TYPE
2270
2271/* Get a pointer to the processor information for a symbol. */
2272
2273TC_SYMFIELD_TYPE *
2274symbol_get_tc (s)
2275 symbolS *s;
2276{
2277 if (LOCAL_SYMBOL_CHECK (s))
2278 s = local_symbol_convert ((struct local_symbol *) s);
2279 return &s->sy_tc;
2280}
2281
2282/* Set the processor information for a symbol. */
2283
2284void
bf27257e 2285symbol_set_tc (s, o)
49309057
ILT
2286 symbolS *s;
2287 TC_SYMFIELD_TYPE *o;
2288{
2289 if (LOCAL_SYMBOL_CHECK (s))
2290 s = local_symbol_convert ((struct local_symbol *) s);
2291 s->sy_tc = *o;
2292}
2293
2294#endif /* TC_SYMFIELD_TYPE */
2295
252b5132
RH
2296void
2297symbol_begin ()
2298{
2299 symbol_lastP = NULL;
7c743825 2300 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
252b5132 2301 sy_hash = hash_new ();
49309057
ILT
2302#ifdef BFD_ASSEMBLER
2303 local_hash = hash_new ();
2304#endif
252b5132
RH
2305
2306 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2307#ifdef BFD_ASSEMBLER
2308#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2309 abs_symbol.bsym = bfd_abs_section.symbol;
2310#endif
2311#else
7c743825 2312 /* Can't initialise a union. Sigh. */
252b5132
RH
2313 S_SET_SEGMENT (&abs_symbol, absolute_section);
2314#endif
2315 abs_symbol.sy_value.X_op = O_constant;
2316 abs_symbol.sy_frag = &zero_address_frag;
2317
2318 if (LOCAL_LABELS_FB)
2319 fb_label_init ();
2320}
252b5132
RH
2321\f
2322int indent_level;
2323
2324/* Maximum indent level.
2325 Available for modification inside a gdb session. */
2326int max_indent_level = 8;
2327
2328#if 0
2329
2330static void
2331indent ()
2332{
2333 printf ("%*s", indent_level * 4, "");
2334}
2335
2336#endif
2337
2338void
2339print_symbol_value_1 (file, sym)
2340 FILE *file;
2341 symbolS *sym;
2342{
2343 const char *name = S_GET_NAME (sym);
2344 if (!name || !name[0])
2345 name = "(unnamed)";
2346 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
49309057
ILT
2347
2348 if (LOCAL_SYMBOL_CHECK (sym))
2349 {
a1605869 2350#ifdef BFD_ASSEMBLER
49309057
ILT
2351 struct local_symbol *locsym = (struct local_symbol *) sym;
2352 if (local_symbol_get_frag (locsym) != &zero_address_frag
2353 && local_symbol_get_frag (locsym) != NULL)
2354 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2355 if (local_symbol_resolved_p (locsym))
2356 fprintf (file, " resolved");
2357 fprintf (file, " local");
a1605869 2358#endif
49309057
ILT
2359 }
2360 else
2361 {
2362 if (sym->sy_frag != &zero_address_frag)
2363 fprintf (file, " frag %lx", (long) sym->sy_frag);
2364 if (sym->written)
2365 fprintf (file, " written");
2366 if (sym->sy_resolved)
2367 fprintf (file, " resolved");
2368 else if (sym->sy_resolving)
2369 fprintf (file, " resolving");
2370 if (sym->sy_used_in_reloc)
2371 fprintf (file, " used-in-reloc");
2372 if (sym->sy_used)
2373 fprintf (file, " used");
2374 if (S_IS_LOCAL (sym))
2375 fprintf (file, " local");
2376 if (S_IS_EXTERN (sym))
2377 fprintf (file, " extern");
2378 if (S_IS_DEBUG (sym))
2379 fprintf (file, " debug");
2380 if (S_IS_DEFINED (sym))
2381 fprintf (file, " defined");
2382 }
252b5132 2383 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
49309057 2384 if (symbol_resolved_p (sym))
252b5132
RH
2385 {
2386 segT s = S_GET_SEGMENT (sym);
2387
2388 if (s != undefined_section
2389 && s != expr_section)
2390 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2391 }
2392 else if (indent_level < max_indent_level
2393 && S_GET_SEGMENT (sym) != undefined_section)
2394 {
2395 indent_level++;
2396 fprintf (file, "\n%*s<", indent_level * 4, "");
a1605869 2397#ifdef BFD_ASSEMBLER
49309057
ILT
2398 if (LOCAL_SYMBOL_CHECK (sym))
2399 fprintf (file, "constant %lx",
bd780143 2400 (long) ((struct local_symbol *) sym)->lsy_value);
49309057 2401 else
a1605869 2402#endif
49309057 2403 print_expr_1 (file, &sym->sy_value);
252b5132
RH
2404 fprintf (file, ">");
2405 indent_level--;
2406 }
2407 fflush (file);
2408}
2409
2410void
2411print_symbol_value (sym)
2412 symbolS *sym;
2413{
2414 indent_level = 0;
2415 print_symbol_value_1 (stderr, sym);
2416 fprintf (stderr, "\n");
2417}
2418
2419static void
2420print_binary (file, name, exp)
2421 FILE *file;
7c743825 2422 const char *name;
252b5132
RH
2423 expressionS *exp;
2424{
2425 indent_level++;
2426 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2427 print_symbol_value_1 (file, exp->X_add_symbol);
2428 fprintf (file, ">\n%*s<", indent_level * 4, "");
2429 print_symbol_value_1 (file, exp->X_op_symbol);
2430 fprintf (file, ">");
2431 indent_level--;
2432}
2433
2434void
2435print_expr_1 (file, exp)
2436 FILE *file;
2437 expressionS *exp;
2438{
2439 fprintf (file, "expr %lx ", (long) exp);
2440 switch (exp->X_op)
2441 {
2442 case O_illegal:
2443 fprintf (file, "illegal");
2444 break;
2445 case O_absent:
2446 fprintf (file, "absent");
2447 break;
2448 case O_constant:
2449 fprintf (file, "constant %lx", (long) exp->X_add_number);
2450 break;
2451 case O_symbol:
2452 indent_level++;
2453 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2454 print_symbol_value_1 (file, exp->X_add_symbol);
2455 fprintf (file, ">");
2456 maybe_print_addnum:
2457 if (exp->X_add_number)
2458 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2459 (long) exp->X_add_number);
2460 indent_level--;
2461 break;
2462 case O_register:
2463 fprintf (file, "register #%d", (int) exp->X_add_number);
2464 break;
2465 case O_big:
2466 fprintf (file, "big");
2467 break;
2468 case O_uminus:
2469 fprintf (file, "uminus -<");
2470 indent_level++;
2471 print_symbol_value_1 (file, exp->X_add_symbol);
2472 fprintf (file, ">");
2473 goto maybe_print_addnum;
2474 case O_bit_not:
2475 fprintf (file, "bit_not");
2476 break;
2477 case O_multiply:
2478 print_binary (file, "multiply", exp);
2479 break;
2480 case O_divide:
2481 print_binary (file, "divide", exp);
2482 break;
2483 case O_modulus:
2484 print_binary (file, "modulus", exp);
2485 break;
2486 case O_left_shift:
2487 print_binary (file, "lshift", exp);
2488 break;
2489 case O_right_shift:
2490 print_binary (file, "rshift", exp);
2491 break;
2492 case O_bit_inclusive_or:
2493 print_binary (file, "bit_ior", exp);
2494 break;
2495 case O_bit_exclusive_or:
2496 print_binary (file, "bit_xor", exp);
2497 break;
2498 case O_bit_and:
2499 print_binary (file, "bit_and", exp);
2500 break;
2501 case O_eq:
2502 print_binary (file, "eq", exp);
2503 break;
2504 case O_ne:
2505 print_binary (file, "ne", exp);
2506 break;
2507 case O_lt:
2508 print_binary (file, "lt", exp);
2509 break;
2510 case O_le:
2511 print_binary (file, "le", exp);
2512 break;
2513 case O_ge:
2514 print_binary (file, "ge", exp);
2515 break;
2516 case O_gt:
2517 print_binary (file, "gt", exp);
2518 break;
2519 case O_logical_and:
2520 print_binary (file, "logical_and", exp);
2521 break;
2522 case O_logical_or:
2523 print_binary (file, "logical_or", exp);
2524 break;
2525 case O_add:
2526 indent_level++;
2527 fprintf (file, "add\n%*s<", indent_level * 4, "");
2528 print_symbol_value_1 (file, exp->X_add_symbol);
2529 fprintf (file, ">\n%*s<", indent_level * 4, "");
2530 print_symbol_value_1 (file, exp->X_op_symbol);
2531 fprintf (file, ">");
2532 goto maybe_print_addnum;
2533 case O_subtract:
2534 indent_level++;
2535 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2536 print_symbol_value_1 (file, exp->X_add_symbol);
2537 fprintf (file, ">\n%*s<", indent_level * 4, "");
2538 print_symbol_value_1 (file, exp->X_op_symbol);
2539 fprintf (file, ">");
2540 goto maybe_print_addnum;
2541 default:
2542 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2543 break;
2544 }
2545 fflush (stdout);
2546}
2547
2548void
2549print_expr (exp)
2550 expressionS *exp;
2551{
2552 print_expr_1 (stderr, exp);
2553 fprintf (stderr, "\n");
2554}
2555
2556void
2557symbol_print_statistics (file)
2558 FILE *file;
2559{
2560 hash_print_statistics (file, "symbol table", sy_hash);
49309057
ILT
2561#ifdef BFD_ASSEMBLER
2562 hash_print_statistics (file, "mini local symbol table", local_hash);
2563 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2564 local_symbol_count, local_symbol_conversion_count);
2565#endif
252b5132 2566}
This page took 0.195391 seconds and 4 git commands to generate.