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