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