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