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