* remote.c, remote-mon.c, remote-utils.c, remote-utils.h,
[deliverable/binutils-gdb.git] / gas / symbols.c
CommitLineData
fecd2382 1/* symbols.c -symbol table-
2b68b820 2 Copyright (C) 1987, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
6efd877d 3
a39116f1 4 This file is part of GAS, the GNU Assembler.
6efd877d 5
a39116f1
RP
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
6efd877d 10
a39116f1
RP
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
6efd877d 15
a39116f1
RP
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
fecd2382 19
58d4951d 20/* #define DEBUG_SYMS / * to debug symbol list maintenance */
2b68b820 21
6efd877d
KR
22#include <ctype.h>
23
fecd2382
RP
24#include "as.h"
25
26#include "obstack.h" /* For "symbols.h" */
27#include "subsegs.h"
28
29#ifndef WORKING_DOT_WORD
30extern int new_broken_words;
31#endif
fecd2382 32
2b68b820
KR
33/* symbol-name => struct symbol pointer */
34static struct hash_control *sy_hash;
fecd2382 35
a39116f1 36/* Below are commented in "symbols.h". */
6efd877d
KR
37symbolS *symbol_rootP;
38symbolS *symbol_lastP;
39symbolS abs_symbol;
fecd2382 40
6efd877d 41struct obstack notes;
fecd2382 42
85825401 43static void fb_label_init PARAMS ((void));
fecd2382 44
fecd2382
RP
45/*
46 * symbol_new()
47 *
48 * Return a pointer to a new symbol.
49 * Die if we can't make a new symbol.
50 * Fill in the symbol's values.
51 * Add symbol to end of symbol chain.
52 *
53 *
54 * Please always call this to create a new symbol.
55 *
56 * Changes since 1985: Symbol names may not contain '\0'. Sigh.
57 * 2nd argument is now a SEG rather than a TYPE. The mapping between
58 * segments and types is mostly encapsulated herein (actually, we inherit it
59 * from macros in struc-symbol.h).
60 */
61
6efd877d 62symbolS *
604633ae 63symbol_new (name, segment, valu, frag)
2b68b820 64 CONST char *name; /* It is copied, the caller can destroy/modify */
6efd877d 65 segT segment; /* Segment identifier (SEG_<something>) */
604633ae 66 valueT valu; /* Symbol value */
6efd877d 67 fragS *frag; /* Associated fragment */
fecd2382 68{
6efd877d
KR
69 unsigned int name_length;
70 char *preserved_copy_of_name;
71 symbolS *symbolP;
72
73 name_length = strlen (name) + 1; /* +1 for \0 */
74 obstack_grow (&notes, name, name_length);
75 preserved_copy_of_name = obstack_finish (&notes);
2b68b820
KR
76#ifdef STRIP_UNDERSCORE
77 if (preserved_copy_of_name[0] == '_')
78 preserved_copy_of_name++;
79#endif
6efd877d
KR
80 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
81
82 /* symbol must be born in some fixed state. This seems as good as any. */
83 memset (symbolP, 0, sizeof (symbolS));
84
2b68b820
KR
85
86#ifdef BFD_ASSEMBLER
87 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
88 assert (symbolP->bsym != 0);
89 symbolP->bsym->udata = (PTR) symbolP;
90#endif
6efd877d 91 S_SET_NAME (symbolP, preserved_copy_of_name);
6efd877d
KR
92
93 S_SET_SEGMENT (symbolP, segment);
604633ae 94 S_SET_VALUE (symbolP, valu);
2b68b820 95 symbol_clear_list_pointers(symbolP);
6efd877d
KR
96
97 symbolP->sy_frag = frag;
2b68b820 98#ifndef BFD_ASSEMBLER
6efd877d 99 symbolP->sy_number = ~0;
0cafaab1 100 symbolP->sy_name_offset = (unsigned int) ~0;
2b68b820 101#endif
6efd877d
KR
102
103 /*
2b68b820
KR
104 * Link to end of symbol chain.
105 */
6efd877d
KR
106 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
107
108 obj_symbol_new_hook (symbolP);
109
1ecd6c4a 110#ifdef DEBUG_SYMS
2b68b820 111 verify_symbol_chain(symbol_rootP, symbol_lastP);
1ecd6c4a 112#endif /* DEBUG_SYMS */
fecd2382 113
2b68b820
KR
114 return symbolP;
115}
fecd2382 116\f
6efd877d 117
fecd2382
RP
118/*
119 * colon()
120 *
121 * We have just seen "<name>:".
122 * Creates a struct symbol unless it already exists.
123 *
124 * Gripes if we are redefining a symbol incompatibly (and ignores it).
125 *
126 */
6efd877d
KR
127void
128colon (sym_name) /* just seen "x:" - rattle symbols & frags */
129 register char *sym_name; /* symbol name, as a cannonical string */
130 /* We copy this string: OK to alter later. */
fecd2382 131{
6efd877d
KR
132 register symbolS *symbolP; /* symbol we are working with */
133
fecd2382 134#ifdef LOCAL_LABELS_DOLLAR
2b68b820
KR
135 /* Sun local labels go out of scope whenever a non-local symbol is
136 defined. */
6efd877d
KR
137
138 if (*sym_name != 'L')
139 dollar_label_clear ();
140#endif /* LOCAL_LABELS_DOLLAR */
141
fecd2382 142#ifndef WORKING_DOT_WORD
6efd877d
KR
143 if (new_broken_words)
144 {
145 struct broken_word *a;
146 int possible_bytes;
147 fragS *frag_tmp;
148 char *frag_opcode;
149
2b68b820
KR
150 extern const int md_short_jump_size;
151 extern const int md_long_jump_size;
152 possible_bytes = (md_short_jump_size
153 + new_broken_words * md_long_jump_size);
6efd877d
KR
154
155 frag_tmp = frag_now;
156 frag_opcode = frag_var (rs_broken_word,
157 possible_bytes,
158 possible_bytes,
159 (relax_substateT) 0,
160 (symbolS *) broken_words,
161 0L,
162 NULL);
163
164 /* We want to store the pointer to where to insert the jump table in the
2b68b820
KR
165 fr_opcode of the rs_broken_word frag. This requires a little
166 hackery. */
167 while (frag_tmp
168 && (frag_tmp->fr_type != rs_broken_word
169 || frag_tmp->fr_opcode))
6efd877d
KR
170 frag_tmp = frag_tmp->fr_next;
171 know (frag_tmp);
172 frag_tmp->fr_opcode = frag_opcode;
173 new_broken_words = 0;
174
175 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
176 a->dispfrag = frag_tmp;
177 }
178#endif /* WORKING_DOT_WORD */
179
180 if ((symbolP = symbol_find (sym_name)) != 0)
181 {
2b68b820
KR
182#ifdef RESOLVE_SYMBOL_REDEFINITION
183 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
6efd877d 184 return;
2b68b820 185#endif
6efd877d 186 /*
2b68b820
KR
187 * Now check for undefined symbols
188 */
6efd877d
KR
189 if (!S_IS_DEFINED (symbolP))
190 {
191 if (S_GET_VALUE (symbolP) == 0)
192 {
193 symbolP->sy_frag = frag_now;
2b68b820
KR
194#ifdef OBJ_VMS
195 S_GET_OTHER(symbolP) = const_flag;
fecd2382 196#endif
2b68b820 197 S_SET_VALUE (symbolP, (valueT) ((char*)obstack_next_free (&frags) - frag_now->fr_literal));
6efd877d 198 S_SET_SEGMENT (symbolP, now_seg);
fecd2382 199#ifdef N_UNDF
6efd877d 200 know (N_UNDF == 0);
fecd2382 201#endif /* if we have one, it better be zero. */
6efd877d
KR
202
203 }
204 else
205 {
206 /*
2b68b820
KR
207 * There are still several cases to check:
208 * A .comm/.lcomm symbol being redefined as
209 * initialized data is OK
210 * A .comm/.lcomm symbol being redefined with
211 * a larger size is also OK
212 *
213 * This only used to be allowed on VMS gas, but Sun cc
214 * on the sparc also depends on it.
215 */
216
217 if (((!S_IS_DEBUG (symbolP)
218 && !S_IS_DEFINED (symbolP)
219 && S_IS_EXTERNAL (symbolP))
220 || S_GET_SEGMENT (symbolP) == bss_section)
221 && (now_seg == data_section
222 || now_seg == S_GET_SEGMENT (symbolP)))
6efd877d
KR
223 {
224 /*
2b68b820
KR
225 * Select which of the 2 cases this is
226 */
227 if (now_seg != data_section)
6efd877d
KR
228 {
229 /*
2b68b820
KR
230 * New .comm for prev .comm symbol.
231 * If the new size is larger we just
232 * change its value. If the new size
233 * is smaller, we ignore this symbol
234 */
6efd877d 235 if (S_GET_VALUE (symbolP)
2b68b820 236 < ((unsigned) frag_now_fix ()))
6efd877d 237 {
2b68b820 238 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d
KR
239 }
240 }
241 else
242 {
85825401
ILT
243 /* It is a .comm/.lcomm being converted to initialized
244 data. */
6efd877d 245 symbolP->sy_frag = frag_now;
2b68b820
KR
246#ifdef OBJ_VMS
247 S_GET_OTHER(symbolP) = const_flag;
248#endif /* OBJ_VMS */
249 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
6efd877d
KR
250 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
251 }
252 }
253 else
254 {
2b68b820
KR
255#if defined (S_GET_OTHER) && defined (S_GET_DESC)
256 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
6efd877d
KR
257 sym_name,
258 segment_name (S_GET_SEGMENT (symbolP)),
2b68b820
KR
259 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
260 (long) S_GET_VALUE (symbolP));
261#else
58d4951d 262 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
6efd877d
KR
263 sym_name,
264 segment_name (S_GET_SEGMENT (symbolP)),
58d4951d 265 (long) S_GET_VALUE (symbolP));
2b68b820 266#endif
6efd877d
KR
267 }
268 } /* if the undefined symbol has no value */
269 }
270 else
271 {
272 /* Don't blow up if the definition is the same */
273 if (!(frag_now == symbolP->sy_frag
2b68b820 274 && S_GET_VALUE (symbolP) == (char*)obstack_next_free (&frags) - frag_now->fr_literal
6efd877d
KR
275 && S_GET_SEGMENT (symbolP) == now_seg))
276 as_fatal ("Symbol %s already defined.", sym_name);
277 } /* if this symbol is not yet defined */
278
279 }
280 else
281 {
282 symbolP = symbol_new (sym_name,
283 now_seg,
2b68b820 284 (valueT) ((char*)obstack_next_free (&frags) - frag_now->fr_literal),
6efd877d 285 frag_now);
2b68b820 286#ifdef OBJ_VMS
6efd877d 287 S_SET_OTHER (symbolP, const_flag);
2b68b820 288#endif /* OBJ_VMS */
fecd2382 289
6efd877d
KR
290 symbol_table_insert (symbolP);
291 } /* if we have seen this symbol before */
1e9cf565
ILT
292
293#ifdef tc_frob_label
294 tc_frob_label (symbolP);
2b68b820 295#endif
1ecd6c4a 296}
fecd2382 297\f
6efd877d 298
fecd2382
RP
299/*
300 * symbol_table_insert()
301 *
302 * Die if we can't insert the symbol.
303 *
304 */
305
6efd877d
KR
306void
307symbol_table_insert (symbolP)
308 symbolS *symbolP;
fecd2382 309{
604633ae 310 register const char *error_string;
6efd877d
KR
311
312 know (symbolP);
313 know (S_GET_NAME (symbolP));
314
0cafaab1 315 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
6efd877d
KR
316 {
317 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
318 S_GET_NAME (symbolP), error_string);
319 } /* on error */
320} /* symbol_table_insert() */
fecd2382
RP
321\f
322/*
323 * symbol_find_or_make()
324 *
325 * If a symbol name does not exist, create it as undefined, and insert
326 * it into the symbol table. Return a pointer to it.
327 */
6efd877d
KR
328symbolS *
329symbol_find_or_make (name)
330 char *name;
fecd2382 331{
6efd877d
KR
332 register symbolS *symbolP;
333
334 symbolP = symbol_find (name);
335
336 if (symbolP == NULL)
337 {
338 symbolP = symbol_make (name);
339
340 symbol_table_insert (symbolP);
341 } /* if symbol wasn't found */
342
343 return (symbolP);
344} /* symbol_find_or_make() */
345
346symbolS *
347symbol_make (name)
2b68b820 348 CONST char *name;
fecd2382 349{
6efd877d
KR
350 symbolS *symbolP;
351
352 /* Let the machine description default it, e.g. for register names. */
2b68b820 353 symbolP = md_undefined_symbol ((char *) name);
6efd877d
KR
354
355 if (!symbolP)
2b68b820 356 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
6efd877d
KR
357
358 return (symbolP);
359} /* symbol_make() */
fecd2382
RP
360
361/*
362 * symbol_find()
6efd877d 363 *
fecd2382
RP
364 * Implement symbol table lookup.
365 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
366 * Out: NULL if the name was not in the symbol table, else the address
367 * of a struct symbol associated with that name.
368 */
369
6efd877d
KR
370symbolS *
371symbol_find (name)
2b68b820 372 CONST char *name;
fecd2382 373{
a6c6eaf8 374#ifdef STRIP_UNDERSCORE
6efd877d 375 return (symbol_find_base (name, 1));
a6c6eaf8 376#else /* STRIP_UNDERSCORE */
6efd877d 377 return (symbol_find_base (name, 0));
fecd2382 378#endif /* STRIP_UNDERSCORE */
6efd877d 379} /* symbol_find() */
fecd2382 380
6efd877d
KR
381symbolS *
382symbol_find_base (name, strip_underscore)
2b68b820 383 CONST char *name;
6efd877d 384 int strip_underscore;
fecd2382 385{
6efd877d
KR
386 if (strip_underscore && *name == '_')
387 name++;
388 return ((symbolS *) hash_find (sy_hash, name));
fecd2382
RP
389}
390
391/*
392 * Once upon a time, symbols were kept in a singly linked list. At
393 * least coff needs to be able to rearrange them from time to time, for
394 * which a doubly linked list is much more convenient. Loic did these
395 * as macros which seemed dangerous to me so they're now functions.
396 * xoxorich.
397 */
398
399/* Link symbol ADDME after symbol TARGET in the chain. */
6efd877d
KR
400void
401symbol_append (addme, target, rootPP, lastPP)
402 symbolS *addme;
403 symbolS *target;
404 symbolS **rootPP;
405 symbolS **lastPP;
fecd2382 406{
6efd877d
KR
407 if (target == NULL)
408 {
409 know (*rootPP == NULL);
410 know (*lastPP == NULL);
411 *rootPP = addme;
412 *lastPP = addme;
413 return;
414 } /* if the list is empty */
415
416 if (target->sy_next != NULL)
417 {
fecd2382 418#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 419 target->sy_next->sy_previous = addme;
fecd2382 420#endif /* SYMBOLS_NEED_BACKPOINTERS */
6efd877d
KR
421 }
422 else
423 {
424 know (*lastPP == target);
425 *lastPP = addme;
426 } /* if we have a next */
427
428 addme->sy_next = target->sy_next;
429 target->sy_next = addme;
430
fecd2382 431#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 432 addme->sy_previous = target;
fecd2382 433#endif /* SYMBOLS_NEED_BACKPOINTERS */
2b68b820 434}
fecd2382
RP
435
436#ifdef SYMBOLS_NEED_BACKPOINTERS
437/* Remove SYMBOLP from the list. */
6efd877d
KR
438void
439symbol_remove (symbolP, rootPP, lastPP)
440 symbolS *symbolP;
441 symbolS **rootPP;
442 symbolS **lastPP;
fecd2382 443{
6efd877d
KR
444 if (symbolP == *rootPP)
445 {
446 *rootPP = symbolP->sy_next;
447 } /* if it was the root */
448
449 if (symbolP == *lastPP)
450 {
451 *lastPP = symbolP->sy_previous;
452 } /* if it was the tail */
453
454 if (symbolP->sy_next != NULL)
455 {
456 symbolP->sy_next->sy_previous = symbolP->sy_previous;
457 } /* if not last */
458
459 if (symbolP->sy_previous != NULL)
460 {
461 symbolP->sy_previous->sy_next = symbolP->sy_next;
462 } /* if not first */
463
1ecd6c4a 464#ifdef DEBUG_SYMS
6efd877d 465 verify_symbol_chain (*rootPP, *lastPP);
1ecd6c4a 466#endif /* DEBUG_SYMS */
2b68b820 467}
fecd2382
RP
468
469/* Set the chain pointers of SYMBOL to null. */
6efd877d
KR
470void
471symbol_clear_list_pointers (symbolP)
472 symbolS *symbolP;
fecd2382 473{
6efd877d
KR
474 symbolP->sy_next = NULL;
475 symbolP->sy_previous = NULL;
2b68b820 476}
fecd2382
RP
477
478/* Link symbol ADDME before symbol TARGET in the chain. */
6efd877d
KR
479void
480symbol_insert (addme, target, rootPP, lastPP)
481 symbolS *addme;
482 symbolS *target;
483 symbolS **rootPP;
484 symbolS **lastPP;
fecd2382 485{
6efd877d
KR
486 if (target->sy_previous != NULL)
487 {
488 target->sy_previous->sy_next = addme;
489 }
490 else
491 {
492 know (*rootPP == target);
493 *rootPP = addme;
494 } /* if not first */
495
496 addme->sy_previous = target->sy_previous;
497 target->sy_previous = addme;
498 addme->sy_next = target;
499
1ecd6c4a 500#ifdef DEBUG_SYMS
6efd877d 501 verify_symbol_chain (*rootPP, *lastPP);
1ecd6c4a 502#endif /* DEBUG_SYMS */
2b68b820 503}
6efd877d 504
fecd2382
RP
505#endif /* SYMBOLS_NEED_BACKPOINTERS */
506
6efd877d
KR
507void
508verify_symbol_chain (rootP, lastP)
509 symbolS *rootP;
510 symbolS *lastP;
fecd2382 511{
6efd877d
KR
512 symbolS *symbolP = rootP;
513
514 if (symbolP == NULL)
2b68b820 515 return;
6efd877d
KR
516
517 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
518 {
fecd2382 519#ifdef SYMBOLS_NEED_BACKPOINTERS
6efd877d 520 know (symbolP->sy_next->sy_previous == symbolP);
2b68b820
KR
521#else
522 /* Walk the list anyways, to make sure pointers are still good. */
604633ae 523 ;
fecd2382 524#endif /* SYMBOLS_NEED_BACKPOINTERS */
2b68b820 525 }
6efd877d 526
2b68b820
KR
527 assert (lastP == symbolP);
528}
6efd877d 529
2b68b820
KR
530void
531verify_symbol_chain_2 (sym)
532 symbolS *sym;
533{
534 symbolS *p = sym, *n = sym;
535#ifdef SYMBOLS_NEED_BACKPOINTERS
536 while (symbol_previous (p))
537 p = symbol_previous (p);
538#endif
539 while (symbol_next (n))
540 n = symbol_next (n);
541 verify_symbol_chain (p, n);
542}
6efd877d 543
5868b1fe
ILT
544/* Resolve the value of a symbol. This is called during the final
545 pass over the symbol table to resolve any symbols with complex
546 values. */
547
548void
549resolve_symbol_value (symp)
550 symbolS *symp;
551{
0cafaab1
ILT
552 int resolved;
553
5868b1fe
ILT
554 if (symp->sy_resolved)
555 return;
556
0cafaab1
ILT
557 resolved = 0;
558
5868b1fe
ILT
559 if (symp->sy_resolving)
560 {
561 as_bad ("Symbol definition loop encountered at %s",
562 S_GET_NAME (symp));
563 S_SET_VALUE (symp, (valueT) 0);
0cafaab1 564 resolved = 1;
5868b1fe
ILT
565 }
566 else
567 {
5ac34ac3
ILT
568 offsetT left, right, val;
569 segT seg_left, seg_right;
570
5868b1fe
ILT
571 symp->sy_resolving = 1;
572
5ac34ac3 573 switch (symp->sy_value.X_op)
5868b1fe 574 {
5ac34ac3
ILT
575 case O_absent:
576 S_SET_VALUE (symp, 0);
577 /* Fall through. */
578 case O_constant:
579 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
580 if (S_GET_SEGMENT (symp) == expr_section)
581 S_SET_SEGMENT (symp, absolute_section);
0cafaab1 582 resolved = 1;
5ac34ac3
ILT
583 break;
584
585 case O_symbol:
5868b1fe
ILT
586 resolve_symbol_value (symp->sy_value.X_add_symbol);
587
588#ifdef obj_frob_forward_symbol
589 /* Some object formats need to forward the segment. */
590 obj_frob_forward_symbol (symp);
591#endif
592
593 S_SET_VALUE (symp,
594 (symp->sy_value.X_add_number
595 + symp->sy_frag->fr_address
596 + S_GET_VALUE (symp->sy_value.X_add_symbol)));
5ac34ac3
ILT
597 if (S_GET_SEGMENT (symp) == expr_section
598 || S_GET_SEGMENT (symp) == undefined_section)
599 S_SET_SEGMENT (symp,
600 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
0cafaab1
ILT
601
602 resolved = symp->sy_value.X_add_symbol->sy_resolved;
5ac34ac3
ILT
603 break;
604
605 case O_uminus:
606 case O_bit_not:
607 resolve_symbol_value (symp->sy_value.X_add_symbol);
608 if (symp->sy_value.X_op == O_uminus)
609 val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
610 else
611 val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
612 S_SET_VALUE (symp,
613 (val
614 + symp->sy_value.X_add_number
615 + symp->sy_frag->fr_address));
616 if (S_GET_SEGMENT (symp) == expr_section
617 || S_GET_SEGMENT (symp) == undefined_section)
618 S_SET_SEGMENT (symp, absolute_section);
0cafaab1 619 resolved = symp->sy_value.X_add_symbol->sy_resolved;
5ac34ac3
ILT
620 break;
621
622 case O_multiply:
623 case O_divide:
624 case O_modulus:
625 case O_left_shift:
626 case O_right_shift:
627 case O_bit_inclusive_or:
628 case O_bit_or_not:
629 case O_bit_exclusive_or:
630 case O_bit_and:
631 case O_add:
632 case O_subtract:
c978e704 633 resolve_symbol_value (symp->sy_value.X_add_symbol);
5ac34ac3
ILT
634 resolve_symbol_value (symp->sy_value.X_op_symbol);
635 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
636 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
637 if (seg_left != seg_right
638 && seg_left != undefined_section
639 && seg_right != undefined_section)
640 as_bad ("%s is operation on symbols in different sections",
c978e704 641 S_GET_NAME (symp));
5ac34ac3
ILT
642 if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
643 != absolute_section)
644 && symp->sy_value.X_op != O_subtract)
645 as_bad ("%s is illegal operation on non-absolute symbols",
646 S_GET_NAME (symp));
647 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
648 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
649 switch (symp->sy_value.X_op)
650 {
651 case O_multiply: val = left * right; break;
652 case O_divide: val = left / right; break;
653 case O_modulus: val = left % right; break;
654 case O_left_shift: val = left << right; break;
655 case O_right_shift: val = left >> right; break;
656 case O_bit_inclusive_or: val = left | right; break;
657 case O_bit_or_not: val = left |~ right; break;
658 case O_bit_exclusive_or: val = left ^ right; break;
659 case O_bit_and: val = left & right; break;
660 case O_add: val = left + right; break;
661 case O_subtract: val = left - right; break;
662 default: abort ();
663 }
c978e704
ILT
664 S_SET_VALUE (symp,
665 (symp->sy_value.X_add_number
666 + symp->sy_frag->fr_address
5ac34ac3
ILT
667 + val));
668 if (S_GET_SEGMENT (symp) == expr_section
669 || S_GET_SEGMENT (symp) == undefined_section)
670 S_SET_SEGMENT (symp, absolute_section);
0cafaab1
ILT
671 resolved = (symp->sy_value.X_add_symbol->sy_resolved
672 && symp->sy_value.X_op_symbol->sy_resolved);
5ac34ac3
ILT
673 break;
674
675 case O_register:
676 case O_big:
677 case O_illegal:
0cafaab1
ILT
678 /* Give an error (below) if not in expr_section. We don't
679 want to worry about expr_section symbols, because they
680 are fictional (they are created as part of expression
681 resolution), and any problems may not actually mean
682 anything. */
5ac34ac3 683 break;
5868b1fe
ILT
684 }
685 }
686
0cafaab1
ILT
687 /* Don't worry if we can't resolve an expr_section symbol. */
688 if (resolved)
689 symp->sy_resolved = 1;
690 else if (S_GET_SEGMENT (symp) != expr_section)
691 {
692 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp));
693 symp->sy_resolved = 1;
694 }
5868b1fe
ILT
695}
696
6efd877d
KR
697#ifdef LOCAL_LABELS_DOLLAR
698
699/* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
700 They are *really* local. That is, they go out of scope whenever we see a
701 label that isn't local. Also, like fb labels, there can be multiple
702 instances of a dollar label. Therefor, we name encode each instance with
703 the instance number, keep a list of defined symbols separate from the real
704 symbol table, and we treat these buggers as a sparse array. */
705
2b68b820
KR
706static long *dollar_labels;
707static long *dollar_label_instances;
708static char *dollar_label_defines;
709static long dollar_label_count;
604633ae 710static unsigned long dollar_label_max;
6efd877d
KR
711
712int
713dollar_label_defined (label)
714 long label;
715{
716 long *i;
717
718 know ((dollar_labels != NULL) || (dollar_label_count == 0));
719
720 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
721 if (*i == label)
722 return dollar_label_defines[i - dollar_labels];
6efd877d
KR
723
724 /* if we get here, label isn't defined */
2b68b820 725 return 0;
6efd877d
KR
726} /* dollar_label_defined() */
727
728static int
729dollar_label_instance (label)
730 long label;
731{
732 long *i;
733
734 know ((dollar_labels != NULL) || (dollar_label_count == 0));
735
736 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
737 if (*i == label)
738 return (dollar_label_instances[i - dollar_labels]);
6efd877d 739
2b68b820
KR
740 /* If we get here, we haven't seen the label before, therefore its instance
741 count is zero. */
742 return 0;
743}
6efd877d
KR
744
745void
746dollar_label_clear ()
747{
604633ae 748 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
2b68b820 749}
6efd877d
KR
750
751#define DOLLAR_LABEL_BUMP_BY 10
752
753void
754define_dollar_label (label)
755 long label;
756{
757 long *i;
758
759 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
2b68b820
KR
760 if (*i == label)
761 {
762 ++dollar_label_instances[i - dollar_labels];
763 dollar_label_defines[i - dollar_labels] = 1;
764 return;
765 }
6efd877d
KR
766
767 /* if we get to here, we don't have label listed yet. */
768
769 if (dollar_labels == NULL)
770 {
771 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
772 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
773 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
774 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
775 dollar_label_count = 0;
6efd877d
KR
776 }
777 else if (dollar_label_count == dollar_label_max)
778 {
779 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
780 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
781 dollar_label_max * sizeof (long));
782 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
783 dollar_label_max * sizeof (long));
784 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
785 } /* if we needed to grow */
786
787 dollar_labels[dollar_label_count] = label;
788 dollar_label_instances[dollar_label_count] = 1;
789 dollar_label_defines[dollar_label_count] = 1;
790 ++dollar_label_count;
2b68b820 791}
6efd877d
KR
792
793/*
794 * dollar_label_name()
795 *
796 * Caller must copy returned name: we re-use the area for the next name.
797 *
2b68b820
KR
798 * The mth occurence of label n: is turned into the symbol "Ln^Am"
799 * where n is the label number and m is the instance number. "L" makes
800 * it a label discarded unless debugging and "^A"('\1') ensures no
801 * ordinary symbol SHOULD get the same name as a local label
802 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
6efd877d
KR
803 *
804 * fb labels get the same treatment, except that ^B is used in place of ^A.
805 */
806
807char * /* Return local label name. */
808dollar_label_name (n, augend)
809 register long n; /* we just saw "n$:" : n a number */
810 register int augend; /* 0 for current instance, 1 for new instance */
811{
812 long i;
813 /* Returned to caller, then copied. used for created names ("4f") */
814 static char symbol_name_build[24];
815 register char *p;
816 register char *q;
817 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
818
819 know (n >= 0);
820 know (augend == 0 || augend == 1);
821 p = symbol_name_build;
822 *p++ = 'L';
823
824 /* Next code just does sprintf( {}, "%d", n); */
825 /* label number */
826 q = symbol_name_temporary;
827 for (*q++ = 0, i = n; i; ++q)
828 {
829 *q = i % 10 + '0';
830 i /= 10;
831 }
832 while ((*p = *--q) != '\0')
833 ++p;
834
835 *p++ = 1; /* ^A */
836
837 /* instance number */
838 q = symbol_name_temporary;
839 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
840 {
841 *q = i % 10 + '0';
842 i /= 10;
843 }
844 while ((*p++ = *--q) != '\0');;
845
846 /* The label, as a '\0' ended string, starts at symbol_name_build. */
2b68b820
KR
847 return symbol_name_build;
848}
6efd877d
KR
849
850#endif /* LOCAL_LABELS_DOLLAR */
851
852#ifdef LOCAL_LABELS_FB
853
854/*
855 * Sombody else's idea of local labels. They are made by "n:" where n
856 * is any decimal digit. Refer to them with
857 * "nb" for previous (backward) n:
858 * or "nf" for next (forward) n:.
859 *
860 * We do a little better and let n be any number, not just a single digit, but
861 * since the other guy's assembler only does ten, we treat the first ten
862 * specially.
863 *
864 * Like someone else's assembler, we have one set of local label counters for
865 * entire assembly, not one set per (sub)segment like in most assemblers. This
866 * implies that one can refer to a label in another segment, and indeed some
867 * crufty compilers have done just that.
868 *
869 * Since there could be a LOT of these things, treat them as a sparse array.
870 */
871
872#define FB_LABEL_SPECIAL (10)
873
874static long fb_low_counter[FB_LABEL_SPECIAL];
875static long *fb_labels;
876static long *fb_label_instances;
eec0de3f
KR
877static long fb_label_count;
878static long fb_label_max;
6efd877d
KR
879
880/* this must be more than FB_LABEL_SPECIAL */
881#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
882
883static void
884fb_label_init ()
885{
886 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
6efd877d
KR
887} /* fb_label_init() */
888
889/* add one to the instance number of this fb label */
890void
891fb_label_instance_inc (label)
892 long label;
893{
894 long *i;
895
896 if (label < FB_LABEL_SPECIAL)
897 {
898 ++fb_low_counter[label];
899 return;
900 }
901
e154ecf4 902 if (fb_labels != NULL)
6efd877d 903 {
e154ecf4
ILT
904 for (i = fb_labels + FB_LABEL_SPECIAL;
905 i < fb_labels + fb_label_count; ++i)
6efd877d 906 {
e154ecf4
ILT
907 if (*i == label)
908 {
909 ++fb_label_instances[i - fb_labels];
910 return;
911 } /* if we find it */
912 } /* for each existing label */
913 }
6efd877d
KR
914
915 /* if we get to here, we don't have label listed yet. */
916
917 if (fb_labels == NULL)
918 {
919 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
920 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
921 fb_label_max = FB_LABEL_BUMP_BY;
922 fb_label_count = FB_LABEL_SPECIAL;
923
924 }
925 else if (fb_label_count == fb_label_max)
926 {
927 fb_label_max += FB_LABEL_BUMP_BY;
928 fb_labels = (long *) xrealloc ((char *) fb_labels,
929 fb_label_max * sizeof (long));
930 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
931 fb_label_max * sizeof (long));
932 } /* if we needed to grow */
933
934 fb_labels[fb_label_count] = label;
935 fb_label_instances[fb_label_count] = 1;
936 ++fb_label_count;
1ecd6c4a 937}
6efd877d
KR
938
939static long
940fb_label_instance (label)
941 long label;
942{
943 long *i;
944
945 if (label < FB_LABEL_SPECIAL)
946 {
947 return (fb_low_counter[label]);
948 }
949
e154ecf4 950 if (fb_labels != NULL)
6efd877d 951 {
e154ecf4
ILT
952 for (i = fb_labels + FB_LABEL_SPECIAL;
953 i < fb_labels + fb_label_count; ++i)
6efd877d 954 {
e154ecf4
ILT
955 if (*i == label)
956 {
957 return (fb_label_instances[i - fb_labels]);
958 } /* if we find it */
959 } /* for each existing label */
960 }
6efd877d 961
e154ecf4
ILT
962 /* We didn't find the label, so this must be a reference to the
963 first instance. */
964 return 0;
2b68b820 965}
6efd877d
KR
966
967/*
968 * fb_label_name()
969 *
970 * Caller must copy returned name: we re-use the area for the next name.
971 *
2b68b820
KR
972 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
973 * where n is the label number and m is the instance number. "L" makes
974 * it a label discarded unless debugging and "^B"('\2') ensures no
975 * ordinary symbol SHOULD get the same name as a local label
976 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
6efd877d 977 *
2b68b820 978 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
6efd877d
KR
979
980char * /* Return local label name. */
981fb_label_name (n, augend)
982 long n; /* we just saw "n:", "nf" or "nb" : n a number */
983 long augend; /* 0 for nb, 1 for n:, nf */
984{
985 long i;
986 /* Returned to caller, then copied. used for created names ("4f") */
987 static char symbol_name_build[24];
988 register char *p;
989 register char *q;
990 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
991
992 know (n >= 0);
993 know (augend == 0 || augend == 1);
994 p = symbol_name_build;
995 *p++ = 'L';
996
997 /* Next code just does sprintf( {}, "%d", n); */
998 /* label number */
999 q = symbol_name_temporary;
1000 for (*q++ = 0, i = n; i; ++q)
1001 {
1002 *q = i % 10 + '0';
1003 i /= 10;
1004 }
1005 while ((*p = *--q) != '\0')
1006 ++p;
1007
1008 *p++ = 2; /* ^B */
1009
1010 /* instance number */
1011 q = symbol_name_temporary;
1012 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1013 {
1014 *q = i % 10 + '0';
1015 i /= 10;
1016 }
1017 while ((*p++ = *--q) != '\0');;
1018
1019 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1020 return (symbol_name_build);
1021} /* fb_label_name() */
1022
1023#endif /* LOCAL_LABELS_FB */
fecd2382
RP
1024
1025
a6c6eaf8 1026/*
6efd877d
KR
1027 * decode name that may have been generated by foo_label_name() above. If
1028 * the name wasn't generated by foo_label_name(), then return it unaltered.
a6c6eaf8
RP
1029 * This is used for error messages.
1030 */
a39116f1 1031
6efd877d
KR
1032char *
1033decode_local_label_name (s)
1034 char *s;
a6c6eaf8 1035{
6efd877d
KR
1036 char *p;
1037 char *symbol_decode;
1038 int label_number;
1039 int instance_number;
1040 char *type;
1041 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1042
1043 if (s[0] != 'L')
1044 return (s);
1045
1046 for (label_number = 0, p = s + 1; isdigit (*p); ++p)
1047 {
1048 label_number = (10 * label_number) + *p - '0';
1049 }
1050
1051 if (*p == 1)
1052 {
1053 type = "dollar";
1054 }
1055 else if (*p == 2)
1056 {
1057 type = "fb";
1058 }
1059 else
1060 {
1061 return (s);
1062 }
1063
1064 for (instance_number = 0, p = s + 1; isdigit (*p); ++p)
1065 {
1066 instance_number = (10 * instance_number) + *p - '0';
1067 }
1068
1069 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1070 (void) sprintf (symbol_decode, message_format, label_number,
1071 instance_number, type);
1072
1073 return (symbol_decode);
1074} /* decode_local_label_name() */
a6c6eaf8 1075
85051959
ILT
1076/* Get the value of a symbol. */
1077
1078valueT
1079S_GET_VALUE (s)
1080 symbolS *s;
1081{
5ac34ac3 1082 if (s->sy_value.X_op != O_constant)
5868b1fe 1083 as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s));
85051959
ILT
1084 return (valueT) s->sy_value.X_add_number;
1085}
1086
1087/* Set the value of a symbol. */
1088
1089void
1090S_SET_VALUE (s, val)
1091 symbolS *s;
1092 valueT val;
1093{
5ac34ac3 1094 s->sy_value.X_op = O_constant;
85051959 1095 s->sy_value.X_add_number = (offsetT) val;
0cafaab1 1096 s->sy_value.X_unsigned = 0;
85051959
ILT
1097}
1098
2b68b820
KR
1099#ifdef BFD_ASSEMBLER
1100
1101int
1102S_IS_EXTERNAL (s)
1103 symbolS *s;
1104{
1105 flagword flags = s->bsym->flags;
1106
1107 /* sanity check */
0cafaab1 1108 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
2b68b820
KR
1109 abort ();
1110
0cafaab1 1111 return (flags & BSF_GLOBAL) != 0;
2b68b820
KR
1112}
1113
1114int
1115S_IS_COMMON (s)
1116 symbolS *s;
1117{
0cafaab1 1118 return bfd_is_com_section (s->bsym->section);
2b68b820
KR
1119}
1120
1121int
1122S_IS_DEFINED (s)
1123 symbolS *s;
1124{
1125 return s->bsym->section != undefined_section;
1126}
1127
1128int
1129S_IS_DEBUG (s)
1130 symbolS *s;
1131{
1132 if (s->bsym->flags & BSF_DEBUGGING)
1133 return 1;
1134 return 0;
1135}
1136
1137int
1138S_IS_LOCAL (s)
1139 symbolS *s;
1140{
1141 flagword flags = s->bsym->flags;
1142
1143 /* sanity check */
0cafaab1 1144 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
2b68b820
KR
1145 abort ();
1146
1147 return (S_GET_NAME (s)
1148 && ! S_IS_DEBUG (s)
1149 && (strchr (S_GET_NAME (s), '\001')
1150 || strchr (S_GET_NAME (s), '\002')
1151 || (S_LOCAL_NAME (s)
1152 && !flagseen['L'])));
1153}
1154
1155int
1156S_IS_EXTERN (s)
1157 symbolS *s;
1158{
1159 return S_IS_EXTERNAL (s);
1160}
1161
1162int
1163S_IS_STABD (s)
1164 symbolS *s;
1165{
1166 return S_GET_NAME (s) == 0;
1167}
1168
2b68b820
KR
1169CONST char *
1170S_GET_NAME (s)
1171 symbolS *s;
1172{
1173 return s->bsym->name;
1174}
1175
1176segT
1177S_GET_SEGMENT (s)
1178 symbolS *s;
1179{
1180 return s->bsym->section;
1181}
1182
2b68b820
KR
1183void
1184S_SET_SEGMENT (s, seg)
1185 symbolS *s;
1186 segT seg;
1187{
1188 s->bsym->section = seg;
1189}
1190
1191void
1192S_SET_EXTERNAL (s)
1193 symbolS *s;
1194{
1ecd6c4a
KR
1195 s->bsym->flags |= BSF_GLOBAL;
1196 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
2b68b820
KR
1197}
1198
1199void
1200S_CLEAR_EXTERNAL (s)
1201 symbolS *s;
1202{
1203 s->bsym->flags |= BSF_LOCAL;
1ecd6c4a
KR
1204 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1205}
1206
1207void
1208S_SET_WEAK (s)
1209 symbolS *s;
1210{
1211 s->bsym->flags |= BSF_WEAK;
1212 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
2b68b820
KR
1213}
1214
1215void
1216S_SET_NAME (s, name)
1217 symbolS *s;
1218 char *name;
1219{
1220 s->bsym->name = name;
1221}
1222#endif /* BFD_ASSEMBLER */
1223
eec0de3f
KR
1224void
1225symbol_begin ()
1226{
1227 symbol_lastP = NULL;
1228 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
1229 sy_hash = hash_new ();
1230 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1231#ifdef BFD_ASSEMBLER
1232 abs_symbol.bsym = bfd_abs_section.symbol;
1233#else
1234 /* Can't initialise a union. Sigh. */
1235 S_SET_SEGMENT (&abs_symbol, absolute_section);
1236#endif
1237#ifdef LOCAL_LABELS_FB
1238 fb_label_init ();
1239#endif /* LOCAL_LABELS_FB */
1240}
1241
8b228fe9 1242/* end of symbols.c */
This page took 0.159123 seconds and 4 git commands to generate.