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