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