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