2003-05-01 Andrew Cagney <cagney@redhat.com>
[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,
1d3b2b27 3 1999, 2000, 2001, 2002, 2003
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 }
1d3b2b27 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
1d3b2b27 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;
1d3b2b27 850
0909233e
AM
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 }
1d3b2b27 873
0909233e
AM
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);
1d3b2b27 1070
0909233e
AM
1071 if (final_seg == expr_section || final_seg == undefined_section)
1072 final_seg = absolute_section;
1d3b2b27 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);
1d3b2b27 1157
0909233e
AM
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
1d3b2b27
AM
1684 may call S_GET_VALUE. We use a static symbol to avoid the
1685 immediate recursion. */
252b5132
RH
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
ae6063d4 1810S_FORCE_RELOC (s, strict)
a161fe53 1811 symbolS *s;
ae6063d4 1812 int strict;
a161fe53
AM
1813{
1814 if (LOCAL_SYMBOL_CHECK (s))
1815 return ((struct local_symbol *) s)->lsy_section == undefined_section;
1816
ae6063d4
AM
1817 return ((strict
1818 && ((s->bsym->flags & BSF_WEAK) != 0
1819 || (EXTERN_FORCE_RELOC
1820 && (s->bsym->flags & BSF_GLOBAL) != 0)))
a161fe53
AM
1821 || s->bsym->section == undefined_section
1822 || bfd_is_com_section (s->bsym->section));
1823}
1824
252b5132
RH
1825int
1826S_IS_DEBUG (s)
1827 symbolS *s;
1828{
49309057
ILT
1829 if (LOCAL_SYMBOL_CHECK (s))
1830 return 0;
252b5132
RH
1831 if (s->bsym->flags & BSF_DEBUGGING)
1832 return 1;
1833 return 0;
1834}
1835
1836int
1837S_IS_LOCAL (s)
1838 symbolS *s;
1839{
49309057 1840 flagword flags;
252b5132
RH
1841 const char *name;
1842
49309057
ILT
1843 if (LOCAL_SYMBOL_CHECK (s))
1844 return 1;
1845
1846 flags = s->bsym->flags;
1847
7c743825 1848 /* Sanity check. */
252b5132
RH
1849 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1850 abort ();
1851
1852 if (bfd_get_section (s->bsym) == reg_section)
1853 return 1;
1854
1855 if (flag_strip_local_absolute
1856 && (flags & BSF_GLOBAL) == 0
1857 && bfd_get_section (s->bsym) == absolute_section)
1858 return 1;
1859
1860 name = S_GET_NAME (s);
1861 return (name != NULL
1862 && ! S_IS_DEBUG (s)
aa257fcd
NC
1863 && (strchr (name, DOLLAR_LABEL_CHAR)
1864 || strchr (name, LOCAL_LABEL_CHAR)
252b5132
RH
1865 || (! flag_keep_locals
1866 && (bfd_is_local_label (stdoutput, s->bsym)
1867 || (flag_mri
1868 && name[0] == '?'
1869 && name[1] == '?')))));
1870}
1871
1872int
1873S_IS_EXTERN (s)
1874 symbolS *s;
1875{
1876 return S_IS_EXTERNAL (s);
1877}
1878
1879int
1880S_IS_STABD (s)
1881 symbolS *s;
1882{
1883 return S_GET_NAME (s) == 0;
1884}
1885
9758f3fc 1886const char *
252b5132
RH
1887S_GET_NAME (s)
1888 symbolS *s;
1889{
49309057
ILT
1890 if (LOCAL_SYMBOL_CHECK (s))
1891 return ((struct local_symbol *) s)->lsy_name;
252b5132
RH
1892 return s->bsym->name;
1893}
1894
1895segT
1896S_GET_SEGMENT (s)
1897 symbolS *s;
1898{
49309057
ILT
1899 if (LOCAL_SYMBOL_CHECK (s))
1900 return ((struct local_symbol *) s)->lsy_section;
252b5132
RH
1901 return s->bsym->section;
1902}
1903
1904void
1905S_SET_SEGMENT (s, seg)
1906 symbolS *s;
1907 segT seg;
1908{
1909 /* Don't reassign section symbols. The direct reason is to prevent seg
1910 faults assigning back to const global symbols such as *ABS*, but it
1911 shouldn't happen anyway. */
1912
49309057
ILT
1913 if (LOCAL_SYMBOL_CHECK (s))
1914 {
1915 if (seg == reg_section)
1916 s = local_symbol_convert ((struct local_symbol *) s);
1917 else
1918 {
1919 ((struct local_symbol *) s)->lsy_section = seg;
1920 return;
1921 }
1922 }
1923
252b5132
RH
1924 if (s->bsym->flags & BSF_SECTION_SYM)
1925 {
1926 if (s->bsym->section != seg)
7c743825 1927 abort ();
252b5132
RH
1928 }
1929 else
1930 s->bsym->section = seg;
1931}
1932
1933void
1934S_SET_EXTERNAL (s)
1935 symbolS *s;
1936{
49309057
ILT
1937 if (LOCAL_SYMBOL_CHECK (s))
1938 s = local_symbol_convert ((struct local_symbol *) s);
252b5132
RH
1939 if ((s->bsym->flags & BSF_WEAK) != 0)
1940 {
1941 /* Let .weak override .global. */
1942 return;
1943 }
4d7c34bf
NC
1944 if (s->bsym->flags & BSF_SECTION_SYM)
1945 {
1946 char * file;
1947 unsigned int line;
d1a6c242 1948
4d7c34bf
NC
1949 /* Do not reassign section symbols. */
1950 as_where (& file, & line);
1951 as_warn_where (file, line,
0e389e77 1952 _("section symbols are already global"));
4d7c34bf
NC
1953 return;
1954 }
252b5132 1955 s->bsym->flags |= BSF_GLOBAL;
7c743825 1956 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
252b5132
RH
1957}
1958
1959void
1960S_CLEAR_EXTERNAL (s)
1961 symbolS *s;
1962{
49309057
ILT
1963 if (LOCAL_SYMBOL_CHECK (s))
1964 return;
252b5132
RH
1965 if ((s->bsym->flags & BSF_WEAK) != 0)
1966 {
1967 /* Let .weak override. */
1968 return;
1969 }
1970 s->bsym->flags |= BSF_LOCAL;
7c743825 1971 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
252b5132
RH
1972}
1973
1974void
1975S_SET_WEAK (s)
1976 symbolS *s;
1977{
49309057
ILT
1978 if (LOCAL_SYMBOL_CHECK (s))
1979 s = local_symbol_convert ((struct local_symbol *) s);
252b5132 1980 s->bsym->flags |= BSF_WEAK;
7c743825 1981 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
252b5132
RH
1982}
1983
00f7efb6
JJ
1984void
1985S_SET_THREAD_LOCAL (s)
1986 symbolS *s;
1987{
1988 if (LOCAL_SYMBOL_CHECK (s))
1989 s = local_symbol_convert ((struct local_symbol *) s);
1990 if (bfd_is_com_section (s->bsym->section)
1991 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
1992 return;
1993 s->bsym->flags |= BSF_THREAD_LOCAL;
1994 if ((s->bsym->flags & BSF_FUNCTION) != 0)
1995 as_bad (_("Accessing function `%s' as thread-local object"),
1996 S_GET_NAME (s));
1997 else if (! bfd_is_und_section (s->bsym->section)
1998 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
1999 as_bad (_("Accessing `%s' as thread-local object"),
2000 S_GET_NAME (s));
2001}
2002
252b5132
RH
2003void
2004S_SET_NAME (s, name)
2005 symbolS *s;
2006 char *name;
2007{
49309057
ILT
2008 if (LOCAL_SYMBOL_CHECK (s))
2009 {
2010 ((struct local_symbol *) s)->lsy_name = name;
2011 return;
2012 }
252b5132
RH
2013 s->bsym->name = name;
2014}
2015#endif /* BFD_ASSEMBLER */
2016
49309057
ILT
2017#ifdef SYMBOLS_NEED_BACKPOINTERS
2018
2019/* Return the previous symbol in a chain. */
2020
2021symbolS *
2022symbol_previous (s)
2023 symbolS *s;
2024{
2025 if (LOCAL_SYMBOL_CHECK (s))
2026 abort ();
2027 return s->sy_previous;
2028}
2029
2030#endif /* SYMBOLS_NEED_BACKPOINTERS */
2031
2032/* Return the next symbol in a chain. */
2033
2034symbolS *
2035symbol_next (s)
2036 symbolS *s;
2037{
2038 if (LOCAL_SYMBOL_CHECK (s))
2039 abort ();
2040 return s->sy_next;
2041}
2042
2043/* Return a pointer to the value of a symbol as an expression. */
2044
2045expressionS *
2046symbol_get_value_expression (s)
2047 symbolS *s;
2048{
2049 if (LOCAL_SYMBOL_CHECK (s))
2050 s = local_symbol_convert ((struct local_symbol *) s);
2051 return &s->sy_value;
2052}
2053
2054/* Set the value of a symbol to an expression. */
2055
2056void
2057symbol_set_value_expression (s, exp)
2058 symbolS *s;
2059 const expressionS *exp;
2060{
2061 if (LOCAL_SYMBOL_CHECK (s))
2062 s = local_symbol_convert ((struct local_symbol *) s);
2063 s->sy_value = *exp;
2064}
2065
2066/* Set the frag of a symbol. */
2067
2068void
2069symbol_set_frag (s, f)
2070 symbolS *s;
2071 fragS *f;
2072{
a1605869 2073#ifdef BFD_ASSEMBLER
49309057
ILT
2074 if (LOCAL_SYMBOL_CHECK (s))
2075 {
2076 local_symbol_set_frag ((struct local_symbol *) s, f);
2077 return;
2078 }
a1605869 2079#endif
49309057
ILT
2080 s->sy_frag = f;
2081}
2082
2083/* Return the frag of a symbol. */
2084
2085fragS *
2086symbol_get_frag (s)
2087 symbolS *s;
2088{
a1605869 2089#ifdef BFD_ASSEMBLER
49309057
ILT
2090 if (LOCAL_SYMBOL_CHECK (s))
2091 return local_symbol_get_frag ((struct local_symbol *) s);
a1605869 2092#endif
49309057
ILT
2093 return s->sy_frag;
2094}
2095
2096/* Mark a symbol as having been used. */
2097
2098void
2099symbol_mark_used (s)
2100 symbolS *s;
2101{
2102 if (LOCAL_SYMBOL_CHECK (s))
2103 return;
2104 s->sy_used = 1;
2105}
2106
2107/* Clear the mark of whether a symbol has been used. */
2108
2109void
2110symbol_clear_used (s)
2111 symbolS *s;
2112{
2113 if (LOCAL_SYMBOL_CHECK (s))
2114 s = local_symbol_convert ((struct local_symbol *) s);
2115 s->sy_used = 0;
2116}
2117
2118/* Return whether a symbol has been used. */
2119
2120int
2121symbol_used_p (s)
2122 symbolS *s;
2123{
2124 if (LOCAL_SYMBOL_CHECK (s))
2125 return 1;
2126 return s->sy_used;
2127}
2128
2129/* Mark a symbol as having been used in a reloc. */
2130
2131void
2132symbol_mark_used_in_reloc (s)
2133 symbolS *s;
2134{
2135 if (LOCAL_SYMBOL_CHECK (s))
2136 s = local_symbol_convert ((struct local_symbol *) s);
2137 s->sy_used_in_reloc = 1;
2138}
2139
2140/* Clear the mark of whether a symbol has been used in a reloc. */
2141
2142void
2143symbol_clear_used_in_reloc (s)
2144 symbolS *s;
2145{
2146 if (LOCAL_SYMBOL_CHECK (s))
2147 return;
2148 s->sy_used_in_reloc = 0;
2149}
2150
2151/* Return whether a symbol has been used in a reloc. */
2152
2153int
2154symbol_used_in_reloc_p (s)
2155 symbolS *s;
2156{
2157 if (LOCAL_SYMBOL_CHECK (s))
2158 return 0;
2159 return s->sy_used_in_reloc;
2160}
2161
2162/* Mark a symbol as an MRI common symbol. */
2163
2164void
2165symbol_mark_mri_common (s)
2166 symbolS *s;
2167{
2168 if (LOCAL_SYMBOL_CHECK (s))
2169 s = local_symbol_convert ((struct local_symbol *) s);
2170 s->sy_mri_common = 1;
2171}
2172
2173/* Clear the mark of whether a symbol is an MRI common symbol. */
2174
2175void
2176symbol_clear_mri_common (s)
2177 symbolS *s;
2178{
2179 if (LOCAL_SYMBOL_CHECK (s))
2180 return;
2181 s->sy_mri_common = 0;
2182}
2183
2184/* Return whether a symbol is an MRI common symbol. */
2185
2186int
2187symbol_mri_common_p (s)
2188 symbolS *s;
2189{
2190 if (LOCAL_SYMBOL_CHECK (s))
2191 return 0;
2192 return s->sy_mri_common;
2193}
2194
2195/* Mark a symbol as having been written. */
2196
2197void
2198symbol_mark_written (s)
2199 symbolS *s;
2200{
2201 if (LOCAL_SYMBOL_CHECK (s))
2202 return;
2203 s->written = 1;
2204}
2205
2206/* Clear the mark of whether a symbol has been written. */
2207
2208void
2209symbol_clear_written (s)
2210 symbolS *s;
2211{
2212 if (LOCAL_SYMBOL_CHECK (s))
2213 return;
2214 s->written = 0;
2215}
2216
2217/* Return whether a symbol has been written. */
2218
2219int
2220symbol_written_p (s)
2221 symbolS *s;
2222{
2223 if (LOCAL_SYMBOL_CHECK (s))
2224 return 0;
2225 return s->written;
2226}
2227
2228/* Mark a symbol has having been resolved. */
2229
2230void
2231symbol_mark_resolved (s)
2232 symbolS *s;
2233{
a1605869 2234#ifdef BFD_ASSEMBLER
49309057
ILT
2235 if (LOCAL_SYMBOL_CHECK (s))
2236 {
2237 local_symbol_mark_resolved ((struct local_symbol *) s);
2238 return;
2239 }
a1605869 2240#endif
49309057
ILT
2241 s->sy_resolved = 1;
2242}
2243
2244/* Return whether a symbol has been resolved. */
2245
2246int
2247symbol_resolved_p (s)
2248 symbolS *s;
2249{
a1605869 2250#ifdef BFD_ASSEMBLER
49309057
ILT
2251 if (LOCAL_SYMBOL_CHECK (s))
2252 return local_symbol_resolved_p ((struct local_symbol *) s);
a1605869 2253#endif
49309057
ILT
2254 return s->sy_resolved;
2255}
2256
2257/* Return whether a symbol is a section symbol. */
2258
2259int
2260symbol_section_p (s)
a04b544b 2261 symbolS *s ATTRIBUTE_UNUSED;
49309057
ILT
2262{
2263 if (LOCAL_SYMBOL_CHECK (s))
2264 return 0;
2265#ifdef BFD_ASSEMBLER
2266 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2267#else
7c743825 2268 /* FIXME. */
49309057
ILT
2269 return 0;
2270#endif
2271}
2272
2273/* Return whether a symbol is equated to another symbol. */
2274
2275int
2276symbol_equated_p (s)
2277 symbolS *s;
2278{
2279 if (LOCAL_SYMBOL_CHECK (s))
2280 return 0;
2281 return s->sy_value.X_op == O_symbol;
2282}
2283
e0890092
AM
2284/* Return whether a symbol is equated to another symbol, and should be
2285 treated specially when writing out relocs. */
2286
2287int
2288symbol_equated_reloc_p (s)
2289 symbolS *s;
2290{
2291 if (LOCAL_SYMBOL_CHECK (s))
2292 return 0;
2293 /* X_op_symbol, normally not used for O_symbol, is set by
2294 resolve_symbol_value to flag expression syms that have been
2295 equated. */
2296 return (s->sy_value.X_op == O_symbol
2297 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2298 || ! S_IS_DEFINED (s)
2299 || S_IS_COMMON (s)));
2300}
2301
49309057
ILT
2302/* Return whether a symbol has a constant value. */
2303
2304int
2305symbol_constant_p (s)
2306 symbolS *s;
2307{
2308 if (LOCAL_SYMBOL_CHECK (s))
2309 return 1;
2310 return s->sy_value.X_op == O_constant;
2311}
2312
2313#ifdef BFD_ASSEMBLER
2314
2315/* Return the BFD symbol for a symbol. */
2316
2317asymbol *
2318symbol_get_bfdsym (s)
2319 symbolS *s;
2320{
2321 if (LOCAL_SYMBOL_CHECK (s))
2322 s = local_symbol_convert ((struct local_symbol *) s);
2323 return s->bsym;
2324}
2325
2326/* Set the BFD symbol for a symbol. */
2327
2328void
2329symbol_set_bfdsym (s, bsym)
2330 symbolS *s;
2331 asymbol *bsym;
2332{
2333 if (LOCAL_SYMBOL_CHECK (s))
2334 s = local_symbol_convert ((struct local_symbol *) s);
2335 s->bsym = bsym;
2336}
2337
2338#endif /* BFD_ASSEMBLER */
2339
2340#ifdef OBJ_SYMFIELD_TYPE
2341
2342/* Get a pointer to the object format information for a symbol. */
2343
2344OBJ_SYMFIELD_TYPE *
2345symbol_get_obj (s)
2346 symbolS *s;
2347{
2348 if (LOCAL_SYMBOL_CHECK (s))
2349 s = local_symbol_convert ((struct local_symbol *) s);
2350 return &s->sy_obj;
2351}
2352
2353/* Set the object format information for a symbol. */
2354
2355void
2356symbol_set_obj (s, o)
2357 symbolS *s;
2358 OBJ_SYMFIELD_TYPE *o;
2359{
2360 if (LOCAL_SYMBOL_CHECK (s))
2361 s = local_symbol_convert ((struct local_symbol *) s);
2362 s->sy_obj = *o;
2363}
2364
2365#endif /* OBJ_SYMFIELD_TYPE */
2366
2367#ifdef TC_SYMFIELD_TYPE
2368
2369/* Get a pointer to the processor information for a symbol. */
2370
2371TC_SYMFIELD_TYPE *
2372symbol_get_tc (s)
2373 symbolS *s;
2374{
2375 if (LOCAL_SYMBOL_CHECK (s))
2376 s = local_symbol_convert ((struct local_symbol *) s);
2377 return &s->sy_tc;
2378}
2379
2380/* Set the processor information for a symbol. */
2381
2382void
bf27257e 2383symbol_set_tc (s, o)
49309057
ILT
2384 symbolS *s;
2385 TC_SYMFIELD_TYPE *o;
2386{
2387 if (LOCAL_SYMBOL_CHECK (s))
2388 s = local_symbol_convert ((struct local_symbol *) s);
2389 s->sy_tc = *o;
2390}
2391
2392#endif /* TC_SYMFIELD_TYPE */
2393
252b5132
RH
2394void
2395symbol_begin ()
2396{
2397 symbol_lastP = NULL;
7c743825 2398 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
252b5132 2399 sy_hash = hash_new ();
49309057
ILT
2400#ifdef BFD_ASSEMBLER
2401 local_hash = hash_new ();
2402#endif
252b5132
RH
2403
2404 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2405#ifdef BFD_ASSEMBLER
2406#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2407 abs_symbol.bsym = bfd_abs_section.symbol;
2408#endif
2409#else
7c743825 2410 /* Can't initialise a union. Sigh. */
252b5132
RH
2411 S_SET_SEGMENT (&abs_symbol, absolute_section);
2412#endif
2413 abs_symbol.sy_value.X_op = O_constant;
2414 abs_symbol.sy_frag = &zero_address_frag;
2415
2416 if (LOCAL_LABELS_FB)
2417 fb_label_init ();
2418}
252b5132
RH
2419\f
2420int indent_level;
2421
2422/* Maximum indent level.
2423 Available for modification inside a gdb session. */
2424int max_indent_level = 8;
2425
2426#if 0
2427
2428static void
2429indent ()
2430{
2431 printf ("%*s", indent_level * 4, "");
2432}
2433
2434#endif
2435
2436void
2437print_symbol_value_1 (file, sym)
2438 FILE *file;
2439 symbolS *sym;
2440{
2441 const char *name = S_GET_NAME (sym);
2442 if (!name || !name[0])
2443 name = "(unnamed)";
2444 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
49309057
ILT
2445
2446 if (LOCAL_SYMBOL_CHECK (sym))
2447 {
a1605869 2448#ifdef BFD_ASSEMBLER
49309057
ILT
2449 struct local_symbol *locsym = (struct local_symbol *) sym;
2450 if (local_symbol_get_frag (locsym) != &zero_address_frag
2451 && local_symbol_get_frag (locsym) != NULL)
2452 fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2453 if (local_symbol_resolved_p (locsym))
2454 fprintf (file, " resolved");
2455 fprintf (file, " local");
a1605869 2456#endif
49309057
ILT
2457 }
2458 else
2459 {
2460 if (sym->sy_frag != &zero_address_frag)
2461 fprintf (file, " frag %lx", (long) sym->sy_frag);
2462 if (sym->written)
2463 fprintf (file, " written");
2464 if (sym->sy_resolved)
2465 fprintf (file, " resolved");
2466 else if (sym->sy_resolving)
2467 fprintf (file, " resolving");
2468 if (sym->sy_used_in_reloc)
2469 fprintf (file, " used-in-reloc");
2470 if (sym->sy_used)
2471 fprintf (file, " used");
2472 if (S_IS_LOCAL (sym))
2473 fprintf (file, " local");
2474 if (S_IS_EXTERN (sym))
2475 fprintf (file, " extern");
2476 if (S_IS_DEBUG (sym))
2477 fprintf (file, " debug");
2478 if (S_IS_DEFINED (sym))
2479 fprintf (file, " defined");
2480 }
252b5132 2481 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
49309057 2482 if (symbol_resolved_p (sym))
252b5132
RH
2483 {
2484 segT s = S_GET_SEGMENT (sym);
2485
2486 if (s != undefined_section
411863a4 2487 && s != expr_section)
252b5132
RH
2488 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2489 }
2490 else if (indent_level < max_indent_level
2491 && S_GET_SEGMENT (sym) != undefined_section)
2492 {
2493 indent_level++;
2494 fprintf (file, "\n%*s<", indent_level * 4, "");
a1605869 2495#ifdef BFD_ASSEMBLER
49309057
ILT
2496 if (LOCAL_SYMBOL_CHECK (sym))
2497 fprintf (file, "constant %lx",
bd780143 2498 (long) ((struct local_symbol *) sym)->lsy_value);
49309057 2499 else
a1605869 2500#endif
49309057 2501 print_expr_1 (file, &sym->sy_value);
252b5132
RH
2502 fprintf (file, ">");
2503 indent_level--;
2504 }
2505 fflush (file);
2506}
2507
2508void
2509print_symbol_value (sym)
2510 symbolS *sym;
2511{
2512 indent_level = 0;
2513 print_symbol_value_1 (stderr, sym);
2514 fprintf (stderr, "\n");
2515}
2516
2517static void
2518print_binary (file, name, exp)
2519 FILE *file;
7c743825 2520 const char *name;
252b5132
RH
2521 expressionS *exp;
2522{
2523 indent_level++;
2524 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2525 print_symbol_value_1 (file, exp->X_add_symbol);
2526 fprintf (file, ">\n%*s<", indent_level * 4, "");
2527 print_symbol_value_1 (file, exp->X_op_symbol);
2528 fprintf (file, ">");
2529 indent_level--;
2530}
2531
2532void
2533print_expr_1 (file, exp)
2534 FILE *file;
2535 expressionS *exp;
2536{
2537 fprintf (file, "expr %lx ", (long) exp);
2538 switch (exp->X_op)
2539 {
2540 case O_illegal:
2541 fprintf (file, "illegal");
2542 break;
2543 case O_absent:
2544 fprintf (file, "absent");
2545 break;
2546 case O_constant:
2547 fprintf (file, "constant %lx", (long) exp->X_add_number);
2548 break;
2549 case O_symbol:
2550 indent_level++;
2551 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2552 print_symbol_value_1 (file, exp->X_add_symbol);
2553 fprintf (file, ">");
2554 maybe_print_addnum:
2555 if (exp->X_add_number)
2556 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2557 (long) exp->X_add_number);
2558 indent_level--;
2559 break;
2560 case O_register:
2561 fprintf (file, "register #%d", (int) exp->X_add_number);
2562 break;
2563 case O_big:
2564 fprintf (file, "big");
2565 break;
2566 case O_uminus:
2567 fprintf (file, "uminus -<");
2568 indent_level++;
2569 print_symbol_value_1 (file, exp->X_add_symbol);
2570 fprintf (file, ">");
2571 goto maybe_print_addnum;
2572 case O_bit_not:
2573 fprintf (file, "bit_not");
2574 break;
2575 case O_multiply:
2576 print_binary (file, "multiply", exp);
2577 break;
2578 case O_divide:
2579 print_binary (file, "divide", exp);
2580 break;
2581 case O_modulus:
2582 print_binary (file, "modulus", exp);
2583 break;
2584 case O_left_shift:
2585 print_binary (file, "lshift", exp);
2586 break;
2587 case O_right_shift:
2588 print_binary (file, "rshift", exp);
2589 break;
2590 case O_bit_inclusive_or:
2591 print_binary (file, "bit_ior", exp);
2592 break;
2593 case O_bit_exclusive_or:
2594 print_binary (file, "bit_xor", exp);
2595 break;
2596 case O_bit_and:
2597 print_binary (file, "bit_and", exp);
2598 break;
2599 case O_eq:
2600 print_binary (file, "eq", exp);
2601 break;
2602 case O_ne:
2603 print_binary (file, "ne", exp);
2604 break;
2605 case O_lt:
2606 print_binary (file, "lt", exp);
2607 break;
2608 case O_le:
2609 print_binary (file, "le", exp);
2610 break;
2611 case O_ge:
2612 print_binary (file, "ge", exp);
2613 break;
2614 case O_gt:
2615 print_binary (file, "gt", exp);
2616 break;
2617 case O_logical_and:
2618 print_binary (file, "logical_and", exp);
2619 break;
2620 case O_logical_or:
2621 print_binary (file, "logical_or", exp);
2622 break;
2623 case O_add:
2624 indent_level++;
2625 fprintf (file, "add\n%*s<", indent_level * 4, "");
2626 print_symbol_value_1 (file, exp->X_add_symbol);
2627 fprintf (file, ">\n%*s<", indent_level * 4, "");
2628 print_symbol_value_1 (file, exp->X_op_symbol);
2629 fprintf (file, ">");
2630 goto maybe_print_addnum;
2631 case O_subtract:
2632 indent_level++;
2633 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2634 print_symbol_value_1 (file, exp->X_add_symbol);
2635 fprintf (file, ">\n%*s<", indent_level * 4, "");
2636 print_symbol_value_1 (file, exp->X_op_symbol);
2637 fprintf (file, ">");
2638 goto maybe_print_addnum;
2639 default:
2640 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2641 break;
2642 }
2643 fflush (stdout);
2644}
2645
2646void
2647print_expr (exp)
2648 expressionS *exp;
2649{
2650 print_expr_1 (stderr, exp);
2651 fprintf (stderr, "\n");
2652}
2653
2654void
2655symbol_print_statistics (file)
2656 FILE *file;
2657{
2658 hash_print_statistics (file, "symbol table", sy_hash);
49309057
ILT
2659#ifdef BFD_ASSEMBLER
2660 hash_print_statistics (file, "mini local symbol table", local_hash);
2661 fprintf (file, "%lu mini local symbols created, %lu converted\n",
2662 local_symbol_count, local_symbol_conversion_count);
2663#endif
252b5132 2664}
This page took 0.284071 seconds and 4 git commands to generate.