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