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