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