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