bfd/
[deliverable/binutils-gdb.git] / gas / symbols.c
1 /* symbols.c -symbol table-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011 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 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
24
25 #include "as.h"
26
27 #include "safe-ctype.h"
28 #include "obstack.h" /* For "symbols.h" */
29 #include "subsegs.h"
30
31 #include "struc-symbol.h"
32
33 /* This is non-zero if symbols are case sensitive, which is the
34 default. */
35 int symbols_case_sensitive = 1;
36
37 #ifndef WORKING_DOT_WORD
38 extern int new_broken_words;
39 #endif
40
41 /* symbol-name => struct symbol pointer */
42 static struct hash_control *sy_hash;
43
44 /* Table of local symbols. */
45 static struct hash_control *local_hash;
46
47 /* Below are commented in "symbols.h". */
48 symbolS *symbol_rootP;
49 symbolS *symbol_lastP;
50 symbolS abs_symbol;
51 symbolS dot_symbol;
52
53 #ifdef DEBUG_SYMS
54 #define debug_verify_symchain verify_symbol_chain
55 #else
56 #define debug_verify_symchain(root, last) ((void) 0)
57 #endif
58
59 #define DOLLAR_LABEL_CHAR '\001'
60 #define LOCAL_LABEL_CHAR '\002'
61
62 #ifndef TC_LABEL_IS_LOCAL
63 #define TC_LABEL_IS_LOCAL(name) 0
64 #endif
65
66 struct obstack notes;
67 #ifdef TE_PE
68 /* The name of an external symbol which is
69 used to make weak PE symbol names unique. */
70 const char * an_external_name;
71 #endif
72
73 static char *save_symbol_name (const char *);
74 static void fb_label_init (void);
75 static long dollar_label_instance (long);
76 static long fb_label_instance (long);
77
78 static void print_binary (FILE *, const char *, expressionS *);
79
80 /* Return a pointer to a new symbol. Die if we can't make a new
81 symbol. Fill in the symbol's values. Add symbol to end of symbol
82 chain.
83
84 This function should be called in the general case of creating a
85 symbol. However, if the output file symbol table has already been
86 set, and you are certain that this symbol won't be wanted in the
87 output file, you can call symbol_create. */
88
89 symbolS *
90 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
91 {
92 symbolS *symbolP = symbol_create (name, segment, valu, frag);
93
94 /* Link to end of symbol chain. */
95 {
96 extern int symbol_table_frozen;
97 if (symbol_table_frozen)
98 abort ();
99 }
100 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
101
102 return symbolP;
103 }
104
105 /* Save a symbol name on a permanent obstack, and convert it according
106 to the object file format. */
107
108 static char *
109 save_symbol_name (const char *name)
110 {
111 unsigned int name_length;
112 char *ret;
113
114 name_length = strlen (name) + 1; /* +1 for \0. */
115 obstack_grow (&notes, name, name_length);
116 ret = (char *) obstack_finish (&notes);
117
118 #ifdef tc_canonicalize_symbol_name
119 ret = tc_canonicalize_symbol_name (ret);
120 #endif
121
122 if (! symbols_case_sensitive)
123 {
124 char *s;
125
126 for (s = ret; *s != '\0'; s++)
127 *s = TOUPPER (*s);
128 }
129
130 return ret;
131 }
132
133 symbolS *
134 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
135 segT segment, /* Segment identifier (SEG_<something>). */
136 valueT valu, /* Symbol value. */
137 fragS *frag /* Associated fragment. */)
138 {
139 char *preserved_copy_of_name;
140 symbolS *symbolP;
141
142 preserved_copy_of_name = save_symbol_name (name);
143
144 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
145
146 /* symbol must be born in some fixed state. This seems as good as any. */
147 memset (symbolP, 0, sizeof (symbolS));
148
149 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
150 if (symbolP->bsym == NULL)
151 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
152 S_SET_NAME (symbolP, preserved_copy_of_name);
153
154 S_SET_SEGMENT (symbolP, segment);
155 S_SET_VALUE (symbolP, valu);
156 symbol_clear_list_pointers (symbolP);
157
158 symbolP->sy_frag = frag;
159
160 obj_symbol_new_hook (symbolP);
161
162 #ifdef tc_symbol_new_hook
163 tc_symbol_new_hook (symbolP);
164 #endif
165
166 return symbolP;
167 }
168 \f
169
170 /* Local symbol support. If we can get away with it, we keep only a
171 small amount of information for local symbols. */
172
173 static symbolS *local_symbol_convert (struct local_symbol *);
174
175 /* Used for statistics. */
176
177 static unsigned long local_symbol_count;
178 static unsigned long local_symbol_conversion_count;
179
180 /* This macro is called with a symbol argument passed by reference.
181 It returns whether this is a local symbol. If necessary, it
182 changes its argument to the real symbol. */
183
184 #define LOCAL_SYMBOL_CHECK(s) \
185 (s->bsym == NULL \
186 ? (local_symbol_converted_p ((struct local_symbol *) s) \
187 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
188 0) \
189 : 1) \
190 : 0)
191
192 /* Create a local symbol and insert it into the local hash table. */
193
194 static struct local_symbol *
195 local_symbol_make (const char *name, segT section, valueT val, fragS *frag)
196 {
197 char *name_copy;
198 struct local_symbol *ret;
199
200 ++local_symbol_count;
201
202 name_copy = save_symbol_name (name);
203
204 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
205 ret->lsy_marker = NULL;
206 ret->lsy_name = name_copy;
207 ret->lsy_section = section;
208 local_symbol_set_frag (ret, frag);
209 ret->lsy_value = val;
210
211 hash_jam (local_hash, name_copy, (void *) ret);
212
213 return ret;
214 }
215
216 /* Convert a local symbol into a real symbol. Note that we do not
217 reclaim the space used by the local symbol. */
218
219 static symbolS *
220 local_symbol_convert (struct local_symbol *locsym)
221 {
222 symbolS *ret;
223
224 gas_assert (locsym->lsy_marker == NULL);
225 if (local_symbol_converted_p (locsym))
226 return local_symbol_get_real_symbol (locsym);
227
228 ++local_symbol_conversion_count;
229
230 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
231 local_symbol_get_frag (locsym));
232
233 if (local_symbol_resolved_p (locsym))
234 ret->sy_resolved = 1;
235
236 /* Local symbols are always either defined or used. */
237 ret->sy_used = 1;
238
239 #ifdef TC_LOCAL_SYMFIELD_CONVERT
240 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
241 #endif
242
243 symbol_table_insert (ret);
244
245 local_symbol_mark_converted (locsym);
246 local_symbol_set_real_symbol (locsym, ret);
247
248 hash_jam (local_hash, locsym->lsy_name, NULL);
249
250 return ret;
251 }
252 \f
253 static void
254 define_sym_at_dot (symbolS *symbolP)
255 {
256 symbolP->sy_frag = frag_now;
257 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
258 S_SET_SEGMENT (symbolP, now_seg);
259 }
260
261 /* We have just seen "<name>:".
262 Creates a struct symbol unless it already exists.
263
264 Gripes if we are redefining a symbol incompatibly (and ignores it). */
265
266 symbolS *
267 colon (/* Just seen "x:" - rattle symbols & frags. */
268 const char *sym_name /* Symbol name, as a cannonical string. */
269 /* We copy this string: OK to alter later. */)
270 {
271 register symbolS *symbolP; /* Symbol we are working with. */
272
273 /* Sun local labels go out of scope whenever a non-local symbol is
274 defined. */
275 if (LOCAL_LABELS_DOLLAR
276 && !bfd_is_local_label_name (stdoutput, sym_name))
277 dollar_label_clear ();
278
279 #ifndef WORKING_DOT_WORD
280 if (new_broken_words)
281 {
282 struct broken_word *a;
283 int possible_bytes;
284 fragS *frag_tmp;
285 char *frag_opcode;
286
287 if (now_seg == absolute_section)
288 {
289 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
290 return NULL;
291 }
292
293 possible_bytes = (md_short_jump_size
294 + new_broken_words * md_long_jump_size);
295
296 frag_tmp = frag_now;
297 frag_opcode = frag_var (rs_broken_word,
298 possible_bytes,
299 possible_bytes,
300 (relax_substateT) 0,
301 (symbolS *) broken_words,
302 (offsetT) 0,
303 NULL);
304
305 /* We want to store the pointer to where to insert the jump
306 table in the fr_opcode of the rs_broken_word frag. This
307 requires a little hackery. */
308 while (frag_tmp
309 && (frag_tmp->fr_type != rs_broken_word
310 || frag_tmp->fr_opcode))
311 frag_tmp = frag_tmp->fr_next;
312 know (frag_tmp);
313 frag_tmp->fr_opcode = frag_opcode;
314 new_broken_words = 0;
315
316 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
317 a->dispfrag = frag_tmp;
318 }
319 #endif /* WORKING_DOT_WORD */
320
321 if ((symbolP = symbol_find (sym_name)) != 0)
322 {
323 S_CLEAR_WEAKREFR (symbolP);
324 #ifdef RESOLVE_SYMBOL_REDEFINITION
325 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
326 return symbolP;
327 #endif
328 /* Now check for undefined symbols. */
329 if (LOCAL_SYMBOL_CHECK (symbolP))
330 {
331 struct local_symbol *locsym = (struct local_symbol *) symbolP;
332
333 if (locsym->lsy_section != undefined_section
334 && (local_symbol_get_frag (locsym) != frag_now
335 || locsym->lsy_section != now_seg
336 || locsym->lsy_value != frag_now_fix ()))
337 {
338 as_bad (_("symbol `%s' is already defined"), sym_name);
339 return symbolP;
340 }
341
342 locsym->lsy_section = now_seg;
343 local_symbol_set_frag (locsym, frag_now);
344 locsym->lsy_value = frag_now_fix ();
345 }
346 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
347 || S_IS_COMMON (symbolP)
348 || S_IS_VOLATILE (symbolP))
349 {
350 if (S_IS_VOLATILE (symbolP))
351 {
352 symbolP = symbol_clone (symbolP, 1);
353 S_SET_VALUE (symbolP, 0);
354 S_CLEAR_VOLATILE (symbolP);
355 }
356 if (S_GET_VALUE (symbolP) == 0)
357 {
358 define_sym_at_dot (symbolP);
359 #ifdef N_UNDF
360 know (N_UNDF == 0);
361 #endif /* if we have one, it better be zero. */
362
363 }
364 else
365 {
366 /* There are still several cases to check:
367
368 A .comm/.lcomm symbol being redefined as initialized
369 data is OK
370
371 A .comm/.lcomm symbol being redefined with a larger
372 size is also OK
373
374 This only used to be allowed on VMS gas, but Sun cc
375 on the sparc also depends on it. */
376
377 if (((!S_IS_DEBUG (symbolP)
378 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
379 && S_IS_EXTERNAL (symbolP))
380 || S_GET_SEGMENT (symbolP) == bss_section)
381 && (now_seg == data_section
382 || now_seg == bss_section
383 || now_seg == S_GET_SEGMENT (symbolP)))
384 {
385 /* Select which of the 2 cases this is. */
386 if (now_seg != data_section)
387 {
388 /* New .comm for prev .comm symbol.
389
390 If the new size is larger we just change its
391 value. If the new size is smaller, we ignore
392 this symbol. */
393 if (S_GET_VALUE (symbolP)
394 < ((unsigned) frag_now_fix ()))
395 {
396 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
397 }
398 }
399 else
400 {
401 /* It is a .comm/.lcomm being converted to initialized
402 data. */
403 define_sym_at_dot (symbolP);
404 }
405 }
406 else
407 {
408 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
409 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
410 static const char *od_buf = "";
411 #else
412 char od_buf[100];
413 od_buf[0] = '\0';
414 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
415 sprintf (od_buf, "%d.%d.",
416 S_GET_OTHER (symbolP),
417 S_GET_DESC (symbolP));
418 #endif
419 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
420 sym_name,
421 segment_name (S_GET_SEGMENT (symbolP)),
422 od_buf,
423 (long) S_GET_VALUE (symbolP));
424 }
425 } /* if the undefined symbol has no value */
426 }
427 else
428 {
429 /* Don't blow up if the definition is the same. */
430 if (!(frag_now == symbolP->sy_frag
431 && S_GET_VALUE (symbolP) == frag_now_fix ()
432 && S_GET_SEGMENT (symbolP) == now_seg))
433 {
434 as_bad (_("symbol `%s' is already defined"), sym_name);
435 symbolP = symbol_clone (symbolP, 0);
436 define_sym_at_dot (symbolP);
437 }
438 }
439
440 }
441 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
442 {
443 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
444 (valueT) frag_now_fix (),
445 frag_now);
446 }
447 else
448 {
449 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
450 frag_now);
451
452 symbol_table_insert (symbolP);
453 }
454
455 if (mri_common_symbol != NULL)
456 {
457 /* This symbol is actually being defined within an MRI common
458 section. This requires special handling. */
459 if (LOCAL_SYMBOL_CHECK (symbolP))
460 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
461 symbolP->sy_value.X_op = O_symbol;
462 symbolP->sy_value.X_add_symbol = mri_common_symbol;
463 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
464 symbolP->sy_frag = &zero_address_frag;
465 S_SET_SEGMENT (symbolP, expr_section);
466 symbolP->sy_mri_common = 1;
467 }
468
469 #ifdef tc_frob_label
470 tc_frob_label (symbolP);
471 #endif
472 #ifdef obj_frob_label
473 obj_frob_label (symbolP);
474 #endif
475
476 return symbolP;
477 }
478 \f
479 /* Die if we can't insert the symbol. */
480
481 void
482 symbol_table_insert (symbolS *symbolP)
483 {
484 register const char *error_string;
485
486 know (symbolP);
487 know (S_GET_NAME (symbolP));
488
489 if (LOCAL_SYMBOL_CHECK (symbolP))
490 {
491 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
492 (void *) symbolP);
493 if (error_string != NULL)
494 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
495 S_GET_NAME (symbolP), error_string);
496 return;
497 }
498
499 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (void *) symbolP)))
500 {
501 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
502 S_GET_NAME (symbolP), error_string);
503 } /* on error */
504 }
505 \f
506 /* If a symbol name does not exist, create it as undefined, and insert
507 it into the symbol table. Return a pointer to it. */
508
509 symbolS *
510 symbol_find_or_make (const char *name)
511 {
512 register symbolS *symbolP;
513
514 symbolP = symbol_find (name);
515
516 if (symbolP == NULL)
517 {
518 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
519 {
520 symbolP = md_undefined_symbol ((char *) name);
521 if (symbolP != NULL)
522 return symbolP;
523
524 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
525 (valueT) 0,
526 &zero_address_frag);
527 return symbolP;
528 }
529
530 symbolP = symbol_make (name);
531
532 symbol_table_insert (symbolP);
533 } /* if symbol wasn't found */
534
535 return (symbolP);
536 }
537
538 symbolS *
539 symbol_make (const char *name)
540 {
541 symbolS *symbolP;
542
543 /* Let the machine description default it, e.g. for register names. */
544 symbolP = md_undefined_symbol ((char *) name);
545
546 if (!symbolP)
547 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
548
549 return (symbolP);
550 }
551
552 symbolS *
553 symbol_clone (symbolS *orgsymP, int replace)
554 {
555 symbolS *newsymP;
556 asymbol *bsymorg, *bsymnew;
557
558 /* Make sure we never clone the dot special symbol. */
559 gas_assert (orgsymP != &dot_symbol);
560
561 /* Running local_symbol_convert on a clone that's not the one currently
562 in local_hash would incorrectly replace the hash entry. Thus the
563 symbol must be converted here. Note that the rest of the function
564 depends on not encountering an unconverted symbol. */
565 if (LOCAL_SYMBOL_CHECK (orgsymP))
566 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
567 bsymorg = orgsymP->bsym;
568
569 newsymP = (symbolS *) obstack_alloc (&notes, sizeof (*newsymP));
570 *newsymP = *orgsymP;
571 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
572 if (bsymnew == NULL)
573 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
574 newsymP->bsym = bsymnew;
575 bsymnew->name = bsymorg->name;
576 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
577 bsymnew->section = bsymorg->section;
578 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
579 bfd_asymbol_bfd (bsymnew), bsymnew);
580
581 #ifdef obj_symbol_clone_hook
582 obj_symbol_clone_hook (newsymP, orgsymP);
583 #endif
584
585 #ifdef tc_symbol_clone_hook
586 tc_symbol_clone_hook (newsymP, orgsymP);
587 #endif
588
589 if (replace)
590 {
591 if (symbol_rootP == orgsymP)
592 symbol_rootP = newsymP;
593 else if (orgsymP->sy_previous)
594 {
595 orgsymP->sy_previous->sy_next = newsymP;
596 orgsymP->sy_previous = NULL;
597 }
598 if (symbol_lastP == orgsymP)
599 symbol_lastP = newsymP;
600 else if (orgsymP->sy_next)
601 orgsymP->sy_next->sy_previous = newsymP;
602
603 /* Symbols that won't be output can't be external. */
604 S_CLEAR_EXTERNAL (orgsymP);
605 orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
606 debug_verify_symchain (symbol_rootP, symbol_lastP);
607
608 symbol_table_insert (newsymP);
609 }
610 else
611 {
612 /* Symbols that won't be output can't be external. */
613 S_CLEAR_EXTERNAL (newsymP);
614 newsymP->sy_previous = newsymP->sy_next = newsymP;
615 }
616
617 return newsymP;
618 }
619
620 /* Referenced symbols, if they are forward references, need to be cloned
621 (without replacing the original) so that the value of the referenced
622 symbols at the point of use . */
623
624 #undef symbol_clone_if_forward_ref
625 symbolS *
626 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
627 {
628 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
629 {
630 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
631 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
632
633 if (symbolP->sy_forward_ref)
634 is_forward = 1;
635
636 if (is_forward)
637 {
638 /* assign_symbol() clones volatile symbols; pre-existing expressions
639 hold references to the original instance, but want the current
640 value. Just repeat the lookup. */
641 if (add_symbol && S_IS_VOLATILE (add_symbol))
642 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
643 if (op_symbol && S_IS_VOLATILE (op_symbol))
644 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
645 }
646
647 /* Re-using sy_resolving here, as this routine cannot get called from
648 symbol resolution code. */
649 if ((symbolP->bsym->section == expr_section || symbolP->sy_forward_ref)
650 && !symbolP->sy_resolving)
651 {
652 symbolP->sy_resolving = 1;
653 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
654 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
655 symbolP->sy_resolving = 0;
656 }
657
658 if (symbolP->sy_forward_ref
659 || add_symbol != symbolP->sy_value.X_add_symbol
660 || op_symbol != symbolP->sy_value.X_op_symbol)
661 {
662 if (symbolP != &dot_symbol)
663 {
664 symbolP = symbol_clone (symbolP, 0);
665 symbolP->sy_resolving = 0;
666 }
667 else
668 {
669 symbolP = symbol_temp_new_now ();
670 #ifdef tc_new_dot_label
671 tc_new_dot_label (symbolP);
672 #endif
673 }
674 }
675
676 symbolP->sy_value.X_add_symbol = add_symbol;
677 symbolP->sy_value.X_op_symbol = op_symbol;
678 }
679
680 return symbolP;
681 }
682
683 symbolS *
684 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
685 {
686 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
687 }
688
689 symbolS *
690 symbol_temp_new_now (void)
691 {
692 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
693 }
694
695 symbolS *
696 symbol_temp_make (void)
697 {
698 return symbol_make (FAKE_LABEL_NAME);
699 }
700
701 /* Implement symbol table lookup.
702 In: A symbol's name as a string: '\0' can't be part of a symbol name.
703 Out: NULL if the name was not in the symbol table, else the address
704 of a struct symbol associated with that name. */
705
706 symbolS *
707 symbol_find_exact (const char *name)
708 {
709 return symbol_find_exact_noref (name, 0);
710 }
711
712 symbolS *
713 symbol_find_exact_noref (const char *name, int noref)
714 {
715 struct local_symbol *locsym;
716 symbolS* sym;
717
718 locsym = (struct local_symbol *) hash_find (local_hash, name);
719 if (locsym != NULL)
720 return (symbolS *) locsym;
721
722 sym = ((symbolS *) hash_find (sy_hash, name));
723
724 /* Any references to the symbol, except for the reference in
725 .weakref, must clear this flag, such that the symbol does not
726 turn into a weak symbol. Note that we don't have to handle the
727 local_symbol case, since a weakrefd is always promoted out of the
728 local_symbol table when it is turned into a weak symbol. */
729 if (sym && ! noref)
730 S_CLEAR_WEAKREFD (sym);
731
732 return sym;
733 }
734
735 symbolS *
736 symbol_find (const char *name)
737 {
738 return symbol_find_noref (name, 0);
739 }
740
741 symbolS *
742 symbol_find_noref (const char *name, int noref)
743 {
744 #ifdef tc_canonicalize_symbol_name
745 {
746 char *copy;
747 size_t len = strlen (name) + 1;
748
749 copy = (char *) alloca (len);
750 memcpy (copy, name, len);
751 name = tc_canonicalize_symbol_name (copy);
752 }
753 #endif
754
755 if (! symbols_case_sensitive)
756 {
757 char *copy;
758 const char *orig;
759 unsigned char c;
760
761 orig = name;
762 name = copy = (char *) alloca (strlen (name) + 1);
763
764 while ((c = *orig++) != '\0')
765 {
766 *copy++ = TOUPPER (c);
767 }
768 *copy = '\0';
769 }
770
771 return symbol_find_exact_noref (name, noref);
772 }
773
774 /* Once upon a time, symbols were kept in a singly linked list. At
775 least coff needs to be able to rearrange them from time to time, for
776 which a doubly linked list is much more convenient. Loic did these
777 as macros which seemed dangerous to me so they're now functions.
778 xoxorich. */
779
780 /* Link symbol ADDME after symbol TARGET in the chain. */
781
782 void
783 symbol_append (symbolS *addme, symbolS *target,
784 symbolS **rootPP, symbolS **lastPP)
785 {
786 if (LOCAL_SYMBOL_CHECK (addme))
787 abort ();
788 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
789 abort ();
790
791 if (target == NULL)
792 {
793 know (*rootPP == NULL);
794 know (*lastPP == NULL);
795 addme->sy_next = NULL;
796 addme->sy_previous = NULL;
797 *rootPP = addme;
798 *lastPP = addme;
799 return;
800 } /* if the list is empty */
801
802 if (target->sy_next != NULL)
803 {
804 target->sy_next->sy_previous = addme;
805 }
806 else
807 {
808 know (*lastPP == target);
809 *lastPP = addme;
810 } /* if we have a next */
811
812 addme->sy_next = target->sy_next;
813 target->sy_next = addme;
814 addme->sy_previous = target;
815
816 debug_verify_symchain (symbol_rootP, symbol_lastP);
817 }
818
819 /* Set the chain pointers of SYMBOL to null. */
820
821 void
822 symbol_clear_list_pointers (symbolS *symbolP)
823 {
824 if (LOCAL_SYMBOL_CHECK (symbolP))
825 abort ();
826 symbolP->sy_next = NULL;
827 symbolP->sy_previous = NULL;
828 }
829
830 /* Remove SYMBOLP from the list. */
831
832 void
833 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
834 {
835 if (LOCAL_SYMBOL_CHECK (symbolP))
836 abort ();
837
838 if (symbolP == *rootPP)
839 {
840 *rootPP = symbolP->sy_next;
841 } /* if it was the root */
842
843 if (symbolP == *lastPP)
844 {
845 *lastPP = symbolP->sy_previous;
846 } /* if it was the tail */
847
848 if (symbolP->sy_next != NULL)
849 {
850 symbolP->sy_next->sy_previous = symbolP->sy_previous;
851 } /* if not last */
852
853 if (symbolP->sy_previous != NULL)
854 {
855 symbolP->sy_previous->sy_next = symbolP->sy_next;
856 } /* if not first */
857
858 debug_verify_symchain (*rootPP, *lastPP);
859 }
860
861 /* Link symbol ADDME before symbol TARGET in the chain. */
862
863 void
864 symbol_insert (symbolS *addme, symbolS *target,
865 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
866 {
867 if (LOCAL_SYMBOL_CHECK (addme))
868 abort ();
869 if (LOCAL_SYMBOL_CHECK (target))
870 abort ();
871
872 if (target->sy_previous != NULL)
873 {
874 target->sy_previous->sy_next = addme;
875 }
876 else
877 {
878 know (*rootPP == target);
879 *rootPP = addme;
880 } /* if not first */
881
882 addme->sy_previous = target->sy_previous;
883 target->sy_previous = addme;
884 addme->sy_next = target;
885
886 debug_verify_symchain (*rootPP, *lastPP);
887 }
888
889 void
890 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
891 {
892 symbolS *symbolP = rootP;
893
894 if (symbolP == NULL)
895 return;
896
897 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
898 {
899 gas_assert (symbolP->bsym != NULL);
900 gas_assert (symbolP->sy_next->sy_previous == symbolP);
901 }
902
903 gas_assert (lastP == symbolP);
904 }
905
906 #ifdef OBJ_COMPLEX_RELC
907
908 static int
909 use_complex_relocs_for (symbolS * symp)
910 {
911 switch (symp->sy_value.X_op)
912 {
913 case O_constant:
914 return 0;
915
916 case O_symbol:
917 case O_symbol_rva:
918 case O_uminus:
919 case O_bit_not:
920 case O_logical_not:
921 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
922 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
923 &&
924 (S_IS_DEFINED (symp->sy_value.X_add_symbol)
925 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section))
926 return 0;
927 break;
928
929 case O_multiply:
930 case O_divide:
931 case O_modulus:
932 case O_left_shift:
933 case O_right_shift:
934 case O_bit_inclusive_or:
935 case O_bit_or_not:
936 case O_bit_exclusive_or:
937 case O_bit_and:
938 case O_add:
939 case O_subtract:
940 case O_eq:
941 case O_ne:
942 case O_lt:
943 case O_le:
944 case O_ge:
945 case O_gt:
946 case O_logical_and:
947 case O_logical_or:
948
949 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
950 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
951 &&
952 (S_IS_COMMON (symp->sy_value.X_op_symbol)
953 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
954
955 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
956 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
957 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section
958 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
959 return 0;
960 break;
961
962 default:
963 break;
964 }
965 return 1;
966 }
967 #endif
968
969 static void
970 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
971 {
972 char *file;
973 unsigned int line;
974 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
975 segT seg_right = S_GET_SEGMENT (right);
976 const char *opname;
977
978 switch (op)
979 {
980 default:
981 abort ();
982 return;
983
984 case O_uminus: opname = "-"; break;
985 case O_bit_not: opname = "~"; break;
986 case O_logical_not: opname = "!"; break;
987 case O_multiply: opname = "*"; break;
988 case O_divide: opname = "/"; break;
989 case O_modulus: opname = "%"; break;
990 case O_left_shift: opname = "<<"; break;
991 case O_right_shift: opname = ">>"; break;
992 case O_bit_inclusive_or: opname = "|"; break;
993 case O_bit_or_not: opname = "|~"; break;
994 case O_bit_exclusive_or: opname = "^"; break;
995 case O_bit_and: opname = "&"; break;
996 case O_add: opname = "+"; break;
997 case O_subtract: opname = "-"; break;
998 case O_eq: opname = "=="; break;
999 case O_ne: opname = "!="; break;
1000 case O_lt: opname = "<"; break;
1001 case O_le: opname = "<="; break;
1002 case O_ge: opname = ">="; break;
1003 case O_gt: opname = ">"; break;
1004 case O_logical_and: opname = "&&"; break;
1005 case O_logical_or: opname = "||"; break;
1006 }
1007
1008 if (expr_symbol_where (symp, &file, &line))
1009 {
1010 if (left)
1011 as_bad_where (file, line,
1012 _("invalid operands (%s and %s sections) for `%s'"),
1013 seg_left->name, seg_right->name, opname);
1014 else
1015 as_bad_where (file, line,
1016 _("invalid operand (%s section) for `%s'"),
1017 seg_right->name, opname);
1018 }
1019 else
1020 {
1021 const char *sname = S_GET_NAME (symp);
1022
1023 if (left)
1024 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1025 seg_left->name, seg_right->name, opname, sname);
1026 else
1027 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1028 seg_right->name, opname, sname);
1029 }
1030 }
1031
1032 /* Resolve the value of a symbol. This is called during the final
1033 pass over the symbol table to resolve any symbols with complex
1034 values. */
1035
1036 valueT
1037 resolve_symbol_value (symbolS *symp)
1038 {
1039 int resolved;
1040 valueT final_val = 0;
1041 segT final_seg;
1042
1043 if (LOCAL_SYMBOL_CHECK (symp))
1044 {
1045 struct local_symbol *locsym = (struct local_symbol *) symp;
1046
1047 final_val = locsym->lsy_value;
1048 if (local_symbol_resolved_p (locsym))
1049 return final_val;
1050
1051 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1052
1053 if (finalize_syms)
1054 {
1055 locsym->lsy_value = final_val;
1056 local_symbol_mark_resolved (locsym);
1057 }
1058
1059 return final_val;
1060 }
1061
1062 if (symp->sy_resolved)
1063 {
1064 if (symp->sy_value.X_op == O_constant)
1065 return (valueT) symp->sy_value.X_add_number;
1066 else
1067 return 0;
1068 }
1069
1070 resolved = 0;
1071 final_seg = S_GET_SEGMENT (symp);
1072
1073 if (symp->sy_resolving)
1074 {
1075 if (finalize_syms)
1076 as_bad (_("symbol definition loop encountered at `%s'"),
1077 S_GET_NAME (symp));
1078 final_val = 0;
1079 resolved = 1;
1080 }
1081 #ifdef OBJ_COMPLEX_RELC
1082 else if (final_seg == expr_section
1083 && use_complex_relocs_for (symp))
1084 {
1085 symbolS * relc_symbol = NULL;
1086 char * relc_symbol_name = NULL;
1087
1088 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1089
1090 /* For debugging, print out conversion input & output. */
1091 #ifdef DEBUG_SYMS
1092 print_expr (& symp->sy_value);
1093 if (relc_symbol_name)
1094 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1095 #endif
1096
1097 if (relc_symbol_name != NULL)
1098 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1099 0, & zero_address_frag);
1100
1101 if (relc_symbol == NULL)
1102 {
1103 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1104 S_GET_NAME (symp));
1105 resolved = 0;
1106 }
1107 else
1108 {
1109 symbol_table_insert (relc_symbol);
1110
1111 /* S_CLEAR_EXTERNAL (relc_symbol); */
1112 if (symp->bsym->flags & BSF_SRELC)
1113 relc_symbol->bsym->flags |= BSF_SRELC;
1114 else
1115 relc_symbol->bsym->flags |= BSF_RELC;
1116 /* symp->bsym->flags |= BSF_RELC; */
1117 copy_symbol_attributes (symp, relc_symbol);
1118 symp->sy_value.X_op = O_symbol;
1119 symp->sy_value.X_add_symbol = relc_symbol;
1120 symp->sy_value.X_add_number = 0;
1121 resolved = 1;
1122 }
1123
1124 final_seg = undefined_section;
1125 goto exit_dont_set_value;
1126 }
1127 #endif
1128 else
1129 {
1130 symbolS *add_symbol, *op_symbol;
1131 offsetT left, right;
1132 segT seg_left, seg_right;
1133 operatorT op;
1134 int move_seg_ok;
1135
1136 symp->sy_resolving = 1;
1137
1138 /* Help out with CSE. */
1139 add_symbol = symp->sy_value.X_add_symbol;
1140 op_symbol = symp->sy_value.X_op_symbol;
1141 final_val = symp->sy_value.X_add_number;
1142 op = symp->sy_value.X_op;
1143
1144 switch (op)
1145 {
1146 default:
1147 BAD_CASE (op);
1148 break;
1149
1150 case O_absent:
1151 final_val = 0;
1152 /* Fall through. */
1153
1154 case O_constant:
1155 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1156 if (final_seg == expr_section)
1157 final_seg = absolute_section;
1158 /* Fall through. */
1159
1160 case O_register:
1161 resolved = 1;
1162 break;
1163
1164 case O_symbol:
1165 case O_symbol_rva:
1166 left = resolve_symbol_value (add_symbol);
1167 seg_left = S_GET_SEGMENT (add_symbol);
1168 if (finalize_syms)
1169 symp->sy_value.X_op_symbol = NULL;
1170
1171 do_symbol:
1172 if (S_IS_WEAKREFR (symp))
1173 {
1174 gas_assert (final_val == 0);
1175 if (S_IS_WEAKREFR (add_symbol))
1176 {
1177 gas_assert (add_symbol->sy_value.X_op == O_symbol
1178 && add_symbol->sy_value.X_add_number == 0);
1179 add_symbol = add_symbol->sy_value.X_add_symbol;
1180 gas_assert (! S_IS_WEAKREFR (add_symbol));
1181 symp->sy_value.X_add_symbol = add_symbol;
1182 }
1183 }
1184
1185 if (symp->sy_mri_common)
1186 {
1187 /* This is a symbol inside an MRI common section. The
1188 relocation routines are going to handle it specially.
1189 Don't change the value. */
1190 resolved = symbol_resolved_p (add_symbol);
1191 break;
1192 }
1193
1194 if (finalize_syms && final_val == 0)
1195 {
1196 if (LOCAL_SYMBOL_CHECK (add_symbol))
1197 add_symbol = local_symbol_convert ((struct local_symbol *)
1198 add_symbol);
1199 copy_symbol_attributes (symp, add_symbol);
1200 }
1201
1202 /* If we have equated this symbol to an undefined or common
1203 symbol, keep X_op set to O_symbol, and don't change
1204 X_add_number. This permits the routine which writes out
1205 relocation to detect this case, and convert the
1206 relocation to be against the symbol to which this symbol
1207 is equated. */
1208 if (! S_IS_DEFINED (add_symbol)
1209 #if defined (OBJ_COFF) && defined (TE_PE)
1210 || S_IS_WEAK (add_symbol)
1211 #endif
1212 || S_IS_COMMON (add_symbol))
1213 {
1214 if (finalize_syms)
1215 {
1216 symp->sy_value.X_op = O_symbol;
1217 symp->sy_value.X_add_symbol = add_symbol;
1218 symp->sy_value.X_add_number = final_val;
1219 /* Use X_op_symbol as a flag. */
1220 symp->sy_value.X_op_symbol = add_symbol;
1221 }
1222 final_seg = seg_left;
1223 final_val = 0;
1224 resolved = symbol_resolved_p (add_symbol);
1225 symp->sy_resolving = 0;
1226 goto exit_dont_set_value;
1227 }
1228 else if (finalize_syms
1229 && ((final_seg == expr_section && seg_left != expr_section)
1230 || symbol_shadow_p (symp)))
1231 {
1232 /* If the symbol is an expression symbol, do similarly
1233 as for undefined and common syms above. Handles
1234 "sym +/- expr" where "expr" cannot be evaluated
1235 immediately, and we want relocations to be against
1236 "sym", eg. because it is weak. */
1237 symp->sy_value.X_op = O_symbol;
1238 symp->sy_value.X_add_symbol = add_symbol;
1239 symp->sy_value.X_add_number = final_val;
1240 symp->sy_value.X_op_symbol = add_symbol;
1241 final_seg = seg_left;
1242 final_val += symp->sy_frag->fr_address + left;
1243 resolved = symbol_resolved_p (add_symbol);
1244 symp->sy_resolving = 0;
1245 goto exit_dont_set_value;
1246 }
1247 else
1248 {
1249 final_val += symp->sy_frag->fr_address + left;
1250 if (final_seg == expr_section || final_seg == undefined_section)
1251 final_seg = seg_left;
1252 }
1253
1254 resolved = symbol_resolved_p (add_symbol);
1255 if (S_IS_WEAKREFR (symp))
1256 goto exit_dont_set_value;
1257 break;
1258
1259 case O_uminus:
1260 case O_bit_not:
1261 case O_logical_not:
1262 left = resolve_symbol_value (add_symbol);
1263 seg_left = S_GET_SEGMENT (add_symbol);
1264
1265 /* By reducing these to the relevant dyadic operator, we get
1266 !S -> S == 0 permitted on anything,
1267 -S -> 0 - S only permitted on absolute
1268 ~S -> S ^ ~0 only permitted on absolute */
1269 if (op != O_logical_not && seg_left != absolute_section
1270 && finalize_syms)
1271 report_op_error (symp, NULL, op, add_symbol);
1272
1273 if (final_seg == expr_section || final_seg == undefined_section)
1274 final_seg = absolute_section;
1275
1276 if (op == O_uminus)
1277 left = -left;
1278 else if (op == O_logical_not)
1279 left = !left;
1280 else
1281 left = ~left;
1282
1283 final_val += left + symp->sy_frag->fr_address;
1284
1285 resolved = symbol_resolved_p (add_symbol);
1286 break;
1287
1288 case O_multiply:
1289 case O_divide:
1290 case O_modulus:
1291 case O_left_shift:
1292 case O_right_shift:
1293 case O_bit_inclusive_or:
1294 case O_bit_or_not:
1295 case O_bit_exclusive_or:
1296 case O_bit_and:
1297 case O_add:
1298 case O_subtract:
1299 case O_eq:
1300 case O_ne:
1301 case O_lt:
1302 case O_le:
1303 case O_ge:
1304 case O_gt:
1305 case O_logical_and:
1306 case O_logical_or:
1307 left = resolve_symbol_value (add_symbol);
1308 right = resolve_symbol_value (op_symbol);
1309 seg_left = S_GET_SEGMENT (add_symbol);
1310 seg_right = S_GET_SEGMENT (op_symbol);
1311
1312 /* Simplify addition or subtraction of a constant by folding the
1313 constant into X_add_number. */
1314 if (op == O_add)
1315 {
1316 if (seg_right == absolute_section)
1317 {
1318 final_val += right;
1319 goto do_symbol;
1320 }
1321 else if (seg_left == absolute_section)
1322 {
1323 final_val += left;
1324 add_symbol = op_symbol;
1325 left = right;
1326 seg_left = seg_right;
1327 goto do_symbol;
1328 }
1329 }
1330 else if (op == O_subtract)
1331 {
1332 if (seg_right == absolute_section)
1333 {
1334 final_val -= right;
1335 goto do_symbol;
1336 }
1337 }
1338
1339 move_seg_ok = 1;
1340 /* Equality and non-equality tests are permitted on anything.
1341 Subtraction, and other comparison operators are permitted if
1342 both operands are in the same section. Otherwise, both
1343 operands must be absolute. We already handled the case of
1344 addition or subtraction of a constant above. This will
1345 probably need to be changed for an object file format which
1346 supports arbitrary expressions, such as IEEE-695. */
1347 if (!(seg_left == absolute_section
1348 && seg_right == absolute_section)
1349 && !(op == O_eq || op == O_ne)
1350 && !((op == O_subtract
1351 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1352 && seg_left == seg_right
1353 && (seg_left != undefined_section
1354 || add_symbol == op_symbol)))
1355 {
1356 /* Don't emit messages unless we're finalizing the symbol value,
1357 otherwise we may get the same message multiple times. */
1358 if (finalize_syms)
1359 report_op_error (symp, add_symbol, op, op_symbol);
1360 /* However do not move the symbol into the absolute section
1361 if it cannot currently be resolved - this would confuse
1362 other parts of the assembler into believing that the
1363 expression had been evaluated to zero. */
1364 else
1365 move_seg_ok = 0;
1366 }
1367
1368 if (move_seg_ok
1369 && (final_seg == expr_section || final_seg == undefined_section))
1370 final_seg = absolute_section;
1371
1372 /* Check for division by zero. */
1373 if ((op == O_divide || op == O_modulus) && right == 0)
1374 {
1375 /* If seg_right is not absolute_section, then we've
1376 already issued a warning about using a bad symbol. */
1377 if (seg_right == absolute_section && finalize_syms)
1378 {
1379 char *file;
1380 unsigned int line;
1381
1382 if (expr_symbol_where (symp, &file, &line))
1383 as_bad_where (file, line, _("division by zero"));
1384 else
1385 as_bad (_("division by zero when setting `%s'"),
1386 S_GET_NAME (symp));
1387 }
1388
1389 right = 1;
1390 }
1391
1392 switch (symp->sy_value.X_op)
1393 {
1394 case O_multiply: left *= right; break;
1395 case O_divide: left /= right; break;
1396 case O_modulus: left %= right; break;
1397 case O_left_shift: left <<= right; break;
1398 case O_right_shift: left >>= right; break;
1399 case O_bit_inclusive_or: left |= right; break;
1400 case O_bit_or_not: left |= ~right; break;
1401 case O_bit_exclusive_or: left ^= right; break;
1402 case O_bit_and: left &= right; break;
1403 case O_add: left += right; break;
1404 case O_subtract: left -= right; break;
1405 case O_eq:
1406 case O_ne:
1407 left = (left == right && seg_left == seg_right
1408 && (seg_left != undefined_section
1409 || add_symbol == op_symbol)
1410 ? ~ (offsetT) 0 : 0);
1411 if (symp->sy_value.X_op == O_ne)
1412 left = ~left;
1413 break;
1414 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1415 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1416 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1417 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1418 case O_logical_and: left = left && right; break;
1419 case O_logical_or: left = left || right; break;
1420 default: abort ();
1421 }
1422
1423 final_val += symp->sy_frag->fr_address + left;
1424 if (final_seg == expr_section || final_seg == undefined_section)
1425 {
1426 if (seg_left == undefined_section
1427 || seg_right == undefined_section)
1428 final_seg = undefined_section;
1429 else if (seg_left == absolute_section)
1430 final_seg = seg_right;
1431 else
1432 final_seg = seg_left;
1433 }
1434 resolved = (symbol_resolved_p (add_symbol)
1435 && symbol_resolved_p (op_symbol));
1436 break;
1437
1438 case O_big:
1439 case O_illegal:
1440 /* Give an error (below) if not in expr_section. We don't
1441 want to worry about expr_section symbols, because they
1442 are fictional (they are created as part of expression
1443 resolution), and any problems may not actually mean
1444 anything. */
1445 break;
1446 }
1447
1448 symp->sy_resolving = 0;
1449 }
1450
1451 if (finalize_syms)
1452 S_SET_VALUE (symp, final_val);
1453
1454 exit_dont_set_value:
1455 /* Always set the segment, even if not finalizing the value.
1456 The segment is used to determine whether a symbol is defined. */
1457 S_SET_SEGMENT (symp, final_seg);
1458
1459 /* Don't worry if we can't resolve an expr_section symbol. */
1460 if (finalize_syms)
1461 {
1462 if (resolved)
1463 symp->sy_resolved = 1;
1464 else if (S_GET_SEGMENT (symp) != expr_section)
1465 {
1466 as_bad (_("can't resolve value for symbol `%s'"),
1467 S_GET_NAME (symp));
1468 symp->sy_resolved = 1;
1469 }
1470 }
1471
1472 return final_val;
1473 }
1474
1475 static void resolve_local_symbol (const char *, void *);
1476
1477 /* A static function passed to hash_traverse. */
1478
1479 static void
1480 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1481 {
1482 if (value != NULL)
1483 resolve_symbol_value ((symbolS *) value);
1484 }
1485
1486 /* Resolve all local symbols. */
1487
1488 void
1489 resolve_local_symbol_values (void)
1490 {
1491 hash_traverse (local_hash, resolve_local_symbol);
1492 }
1493
1494 /* Obtain the current value of a symbol without changing any
1495 sub-expressions used. */
1496
1497 int
1498 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1499 {
1500 symbolS *symbolP = *symbolPP;
1501
1502 if (LOCAL_SYMBOL_CHECK (symbolP))
1503 {
1504 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1505
1506 *valueP = locsym->lsy_value;
1507 *segP = locsym->lsy_section;
1508 *fragPP = local_symbol_get_frag (locsym);
1509 }
1510 else
1511 {
1512 expressionS exp = symbolP->sy_value;
1513
1514 if (!symbolP->sy_resolved && exp.X_op != O_illegal)
1515 {
1516 int resolved;
1517
1518 if (symbolP->sy_resolving)
1519 return 0;
1520 symbolP->sy_resolving = 1;
1521 resolved = resolve_expression (&exp);
1522 symbolP->sy_resolving = 0;
1523 if (!resolved)
1524 return 0;
1525
1526 switch (exp.X_op)
1527 {
1528 case O_constant:
1529 case O_register:
1530 if (!symbol_equated_p (symbolP))
1531 break;
1532 /* Fall thru. */
1533 case O_symbol:
1534 case O_symbol_rva:
1535 symbolP = exp.X_add_symbol;
1536 break;
1537 default:
1538 return 0;
1539 }
1540 }
1541
1542 *symbolPP = symbolP;
1543 *valueP = exp.X_add_number;
1544 *segP = symbolP->bsym->section;
1545 *fragPP = symbolP->sy_frag;
1546
1547 if (*segP == expr_section)
1548 switch (exp.X_op)
1549 {
1550 case O_constant: *segP = absolute_section; break;
1551 case O_register: *segP = reg_section; break;
1552 default: break;
1553 }
1554 }
1555
1556 return 1;
1557 }
1558
1559 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1560 They are *really* local. That is, they go out of scope whenever we see a
1561 label that isn't local. Also, like fb labels, there can be multiple
1562 instances of a dollar label. Therefor, we name encode each instance with
1563 the instance number, keep a list of defined symbols separate from the real
1564 symbol table, and we treat these buggers as a sparse array. */
1565
1566 static long *dollar_labels;
1567 static long *dollar_label_instances;
1568 static char *dollar_label_defines;
1569 static unsigned long dollar_label_count;
1570 static unsigned long dollar_label_max;
1571
1572 int
1573 dollar_label_defined (long label)
1574 {
1575 long *i;
1576
1577 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1578
1579 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1580 if (*i == label)
1581 return dollar_label_defines[i - dollar_labels];
1582
1583 /* If we get here, label isn't defined. */
1584 return 0;
1585 }
1586
1587 static long
1588 dollar_label_instance (long label)
1589 {
1590 long *i;
1591
1592 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1593
1594 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1595 if (*i == label)
1596 return (dollar_label_instances[i - dollar_labels]);
1597
1598 /* If we get here, we haven't seen the label before.
1599 Therefore its instance count is zero. */
1600 return 0;
1601 }
1602
1603 void
1604 dollar_label_clear (void)
1605 {
1606 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1607 }
1608
1609 #define DOLLAR_LABEL_BUMP_BY 10
1610
1611 void
1612 define_dollar_label (long label)
1613 {
1614 long *i;
1615
1616 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1617 if (*i == label)
1618 {
1619 ++dollar_label_instances[i - dollar_labels];
1620 dollar_label_defines[i - dollar_labels] = 1;
1621 return;
1622 }
1623
1624 /* If we get to here, we don't have label listed yet. */
1625
1626 if (dollar_labels == NULL)
1627 {
1628 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1629 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1630 dollar_label_defines = (char *) xmalloc (DOLLAR_LABEL_BUMP_BY);
1631 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1632 dollar_label_count = 0;
1633 }
1634 else if (dollar_label_count == dollar_label_max)
1635 {
1636 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1637 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1638 dollar_label_max * sizeof (long));
1639 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1640 dollar_label_max * sizeof (long));
1641 dollar_label_defines = (char *) xrealloc (dollar_label_defines, dollar_label_max);
1642 } /* if we needed to grow */
1643
1644 dollar_labels[dollar_label_count] = label;
1645 dollar_label_instances[dollar_label_count] = 1;
1646 dollar_label_defines[dollar_label_count] = 1;
1647 ++dollar_label_count;
1648 }
1649
1650 /* Caller must copy returned name: we re-use the area for the next name.
1651
1652 The mth occurence of label n: is turned into the symbol "Ln^Am"
1653 where n is the label number and m is the instance number. "L" makes
1654 it a label discarded unless debugging and "^A"('\1') ensures no
1655 ordinary symbol SHOULD get the same name as a local label
1656 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1657
1658 fb labels get the same treatment, except that ^B is used in place
1659 of ^A. */
1660
1661 char * /* Return local label name. */
1662 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1663 register int augend /* 0 for current instance, 1 for new instance. */)
1664 {
1665 long i;
1666 /* Returned to caller, then copied. Used for created names ("4f"). */
1667 static char symbol_name_build[24];
1668 register char *p;
1669 register char *q;
1670 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1671
1672 know (n >= 0);
1673 know (augend == 0 || augend == 1);
1674 p = symbol_name_build;
1675 #ifdef LOCAL_LABEL_PREFIX
1676 *p++ = LOCAL_LABEL_PREFIX;
1677 #endif
1678 *p++ = 'L';
1679
1680 /* Next code just does sprintf( {}, "%d", n); */
1681 /* Label number. */
1682 q = symbol_name_temporary;
1683 for (*q++ = 0, i = n; i; ++q)
1684 {
1685 *q = i % 10 + '0';
1686 i /= 10;
1687 }
1688 while ((*p = *--q) != '\0')
1689 ++p;
1690
1691 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1692
1693 /* Instance number. */
1694 q = symbol_name_temporary;
1695 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1696 {
1697 *q = i % 10 + '0';
1698 i /= 10;
1699 }
1700 while ((*p++ = *--q) != '\0');;
1701
1702 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1703 return symbol_name_build;
1704 }
1705
1706 /* Somebody else's idea of local labels. They are made by "n:" where n
1707 is any decimal digit. Refer to them with
1708 "nb" for previous (backward) n:
1709 or "nf" for next (forward) n:.
1710
1711 We do a little better and let n be any number, not just a single digit, but
1712 since the other guy's assembler only does ten, we treat the first ten
1713 specially.
1714
1715 Like someone else's assembler, we have one set of local label counters for
1716 entire assembly, not one set per (sub)segment like in most assemblers. This
1717 implies that one can refer to a label in another segment, and indeed some
1718 crufty compilers have done just that.
1719
1720 Since there could be a LOT of these things, treat them as a sparse
1721 array. */
1722
1723 #define FB_LABEL_SPECIAL (10)
1724
1725 static long fb_low_counter[FB_LABEL_SPECIAL];
1726 static long *fb_labels;
1727 static long *fb_label_instances;
1728 static long fb_label_count;
1729 static long fb_label_max;
1730
1731 /* This must be more than FB_LABEL_SPECIAL. */
1732 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1733
1734 static void
1735 fb_label_init (void)
1736 {
1737 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1738 }
1739
1740 /* Add one to the instance number of this fb label. */
1741
1742 void
1743 fb_label_instance_inc (long label)
1744 {
1745 long *i;
1746
1747 if (label < FB_LABEL_SPECIAL)
1748 {
1749 ++fb_low_counter[label];
1750 return;
1751 }
1752
1753 if (fb_labels != NULL)
1754 {
1755 for (i = fb_labels + FB_LABEL_SPECIAL;
1756 i < fb_labels + fb_label_count; ++i)
1757 {
1758 if (*i == label)
1759 {
1760 ++fb_label_instances[i - fb_labels];
1761 return;
1762 } /* if we find it */
1763 } /* for each existing label */
1764 }
1765
1766 /* If we get to here, we don't have label listed yet. */
1767
1768 if (fb_labels == NULL)
1769 {
1770 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1771 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1772 fb_label_max = FB_LABEL_BUMP_BY;
1773 fb_label_count = FB_LABEL_SPECIAL;
1774
1775 }
1776 else if (fb_label_count == fb_label_max)
1777 {
1778 fb_label_max += FB_LABEL_BUMP_BY;
1779 fb_labels = (long *) xrealloc ((char *) fb_labels,
1780 fb_label_max * sizeof (long));
1781 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1782 fb_label_max * sizeof (long));
1783 } /* if we needed to grow */
1784
1785 fb_labels[fb_label_count] = label;
1786 fb_label_instances[fb_label_count] = 1;
1787 ++fb_label_count;
1788 }
1789
1790 static long
1791 fb_label_instance (long label)
1792 {
1793 long *i;
1794
1795 if (label < FB_LABEL_SPECIAL)
1796 {
1797 return (fb_low_counter[label]);
1798 }
1799
1800 if (fb_labels != NULL)
1801 {
1802 for (i = fb_labels + FB_LABEL_SPECIAL;
1803 i < fb_labels + fb_label_count; ++i)
1804 {
1805 if (*i == label)
1806 {
1807 return (fb_label_instances[i - fb_labels]);
1808 } /* if we find it */
1809 } /* for each existing label */
1810 }
1811
1812 /* We didn't find the label, so this must be a reference to the
1813 first instance. */
1814 return 0;
1815 }
1816
1817 /* Caller must copy returned name: we re-use the area for the next name.
1818
1819 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1820 where n is the label number and m is the instance number. "L" makes
1821 it a label discarded unless debugging and "^B"('\2') ensures no
1822 ordinary symbol SHOULD get the same name as a local label
1823 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1824
1825 dollar labels get the same treatment, except that ^A is used in
1826 place of ^B. */
1827
1828 char * /* Return local label name. */
1829 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1830 long augend /* 0 for nb, 1 for n:, nf. */)
1831 {
1832 long i;
1833 /* Returned to caller, then copied. Used for created names ("4f"). */
1834 static char symbol_name_build[24];
1835 register char *p;
1836 register char *q;
1837 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1838
1839 know (n >= 0);
1840 #ifdef TC_MMIX
1841 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1842 #else
1843 know ((unsigned long) augend <= 1);
1844 #endif
1845 p = symbol_name_build;
1846 #ifdef LOCAL_LABEL_PREFIX
1847 *p++ = LOCAL_LABEL_PREFIX;
1848 #endif
1849 *p++ = 'L';
1850
1851 /* Next code just does sprintf( {}, "%d", n); */
1852 /* Label number. */
1853 q = symbol_name_temporary;
1854 for (*q++ = 0, i = n; i; ++q)
1855 {
1856 *q = i % 10 + '0';
1857 i /= 10;
1858 }
1859 while ((*p = *--q) != '\0')
1860 ++p;
1861
1862 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1863
1864 /* Instance number. */
1865 q = symbol_name_temporary;
1866 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1867 {
1868 *q = i % 10 + '0';
1869 i /= 10;
1870 }
1871 while ((*p++ = *--q) != '\0');;
1872
1873 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1874 return (symbol_name_build);
1875 }
1876
1877 /* Decode name that may have been generated by foo_label_name() above.
1878 If the name wasn't generated by foo_label_name(), then return it
1879 unaltered. This is used for error messages. */
1880
1881 char *
1882 decode_local_label_name (char *s)
1883 {
1884 char *p;
1885 char *symbol_decode;
1886 int label_number;
1887 int instance_number;
1888 char *type;
1889 const char *message_format;
1890 int lindex = 0;
1891
1892 #ifdef LOCAL_LABEL_PREFIX
1893 if (s[lindex] == LOCAL_LABEL_PREFIX)
1894 ++lindex;
1895 #endif
1896
1897 if (s[lindex] != 'L')
1898 return s;
1899
1900 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
1901 label_number = (10 * label_number) + *p - '0';
1902
1903 if (*p == DOLLAR_LABEL_CHAR)
1904 type = "dollar";
1905 else if (*p == LOCAL_LABEL_CHAR)
1906 type = "fb";
1907 else
1908 return s;
1909
1910 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1911 instance_number = (10 * instance_number) + *p - '0';
1912
1913 message_format = _("\"%d\" (instance number %d of a %s label)");
1914 symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
1915 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1916
1917 return symbol_decode;
1918 }
1919
1920 /* Get the value of a symbol. */
1921
1922 valueT
1923 S_GET_VALUE (symbolS *s)
1924 {
1925 if (LOCAL_SYMBOL_CHECK (s))
1926 return resolve_symbol_value (s);
1927
1928 if (!s->sy_resolved)
1929 {
1930 valueT val = resolve_symbol_value (s);
1931 if (!finalize_syms)
1932 return val;
1933 }
1934 if (S_IS_WEAKREFR (s))
1935 return S_GET_VALUE (s->sy_value.X_add_symbol);
1936
1937 if (s->sy_value.X_op != O_constant)
1938 {
1939 if (! s->sy_resolved
1940 || s->sy_value.X_op != O_symbol
1941 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1942 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1943 S_GET_NAME (s));
1944 }
1945 return (valueT) s->sy_value.X_add_number;
1946 }
1947
1948 /* Set the value of a symbol. */
1949
1950 void
1951 S_SET_VALUE (symbolS *s, valueT val)
1952 {
1953 if (LOCAL_SYMBOL_CHECK (s))
1954 {
1955 ((struct local_symbol *) s)->lsy_value = val;
1956 return;
1957 }
1958
1959 s->sy_value.X_op = O_constant;
1960 s->sy_value.X_add_number = (offsetT) val;
1961 s->sy_value.X_unsigned = 0;
1962 S_CLEAR_WEAKREFR (s);
1963 }
1964
1965 void
1966 copy_symbol_attributes (symbolS *dest, symbolS *src)
1967 {
1968 if (LOCAL_SYMBOL_CHECK (dest))
1969 dest = local_symbol_convert ((struct local_symbol *) dest);
1970 if (LOCAL_SYMBOL_CHECK (src))
1971 src = local_symbol_convert ((struct local_symbol *) src);
1972
1973 /* In an expression, transfer the settings of these flags.
1974 The user can override later, of course. */
1975 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1976 | BSF_GNU_INDIRECT_FUNCTION)
1977 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1978
1979 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1980 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1981 #endif
1982
1983 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1984 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
1985 #endif
1986 }
1987
1988 int
1989 S_IS_FUNCTION (symbolS *s)
1990 {
1991 flagword flags;
1992
1993 if (LOCAL_SYMBOL_CHECK (s))
1994 return 0;
1995
1996 flags = s->bsym->flags;
1997
1998 return (flags & BSF_FUNCTION) != 0;
1999 }
2000
2001 int
2002 S_IS_EXTERNAL (symbolS *s)
2003 {
2004 flagword flags;
2005
2006 if (LOCAL_SYMBOL_CHECK (s))
2007 return 0;
2008
2009 flags = s->bsym->flags;
2010
2011 /* Sanity check. */
2012 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2013 abort ();
2014
2015 return (flags & BSF_GLOBAL) != 0;
2016 }
2017
2018 int
2019 S_IS_WEAK (symbolS *s)
2020 {
2021 if (LOCAL_SYMBOL_CHECK (s))
2022 return 0;
2023 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2024 could probably handle a WEAKREFR as always weak though. E.g., if
2025 the referenced symbol has lost its weak status, there's no reason
2026 to keep handling the weakrefr as if it was weak. */
2027 if (S_IS_WEAKREFR (s))
2028 return S_IS_WEAK (s->sy_value.X_add_symbol);
2029 return (s->bsym->flags & BSF_WEAK) != 0;
2030 }
2031
2032 int
2033 S_IS_WEAKREFR (symbolS *s)
2034 {
2035 if (LOCAL_SYMBOL_CHECK (s))
2036 return 0;
2037 return s->sy_weakrefr != 0;
2038 }
2039
2040 int
2041 S_IS_WEAKREFD (symbolS *s)
2042 {
2043 if (LOCAL_SYMBOL_CHECK (s))
2044 return 0;
2045 return s->sy_weakrefd != 0;
2046 }
2047
2048 int
2049 S_IS_COMMON (symbolS *s)
2050 {
2051 if (LOCAL_SYMBOL_CHECK (s))
2052 return 0;
2053 return bfd_is_com_section (s->bsym->section);
2054 }
2055
2056 int
2057 S_IS_DEFINED (symbolS *s)
2058 {
2059 if (LOCAL_SYMBOL_CHECK (s))
2060 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2061 return s->bsym->section != undefined_section;
2062 }
2063
2064
2065 #ifndef EXTERN_FORCE_RELOC
2066 #define EXTERN_FORCE_RELOC IS_ELF
2067 #endif
2068
2069 /* Return true for symbols that should not be reduced to section
2070 symbols or eliminated from expressions, because they may be
2071 overridden by the linker. */
2072 int
2073 S_FORCE_RELOC (symbolS *s, int strict)
2074 {
2075 if (LOCAL_SYMBOL_CHECK (s))
2076 return ((struct local_symbol *) s)->lsy_section == undefined_section;
2077
2078 return ((strict
2079 && ((s->bsym->flags & BSF_WEAK) != 0
2080 || (EXTERN_FORCE_RELOC
2081 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2082 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0
2083 || s->bsym->section == undefined_section
2084 || bfd_is_com_section (s->bsym->section));
2085 }
2086
2087 int
2088 S_IS_DEBUG (symbolS *s)
2089 {
2090 if (LOCAL_SYMBOL_CHECK (s))
2091 return 0;
2092 if (s->bsym->flags & BSF_DEBUGGING)
2093 return 1;
2094 return 0;
2095 }
2096
2097 int
2098 S_IS_LOCAL (symbolS *s)
2099 {
2100 flagword flags;
2101 const char *name;
2102
2103 if (LOCAL_SYMBOL_CHECK (s))
2104 return 1;
2105
2106 flags = s->bsym->flags;
2107
2108 /* Sanity check. */
2109 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2110 abort ();
2111
2112 if (bfd_get_section (s->bsym) == reg_section)
2113 return 1;
2114
2115 if (flag_strip_local_absolute
2116 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2117 the source file even when the object file is stripped. */
2118 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2119 && bfd_get_section (s->bsym) == absolute_section)
2120 return 1;
2121
2122 name = S_GET_NAME (s);
2123 return (name != NULL
2124 && ! S_IS_DEBUG (s)
2125 && (strchr (name, DOLLAR_LABEL_CHAR)
2126 || strchr (name, LOCAL_LABEL_CHAR)
2127 || TC_LABEL_IS_LOCAL (name)
2128 || (! flag_keep_locals
2129 && (bfd_is_local_label (stdoutput, s->bsym)
2130 || (flag_mri
2131 && name[0] == '?'
2132 && name[1] == '?')))));
2133 }
2134
2135 int
2136 S_IS_STABD (symbolS *s)
2137 {
2138 return S_GET_NAME (s) == 0;
2139 }
2140
2141 int
2142 S_IS_VOLATILE (const symbolS *s)
2143 {
2144 if (LOCAL_SYMBOL_CHECK (s))
2145 return 0;
2146 return s->sy_volatile;
2147 }
2148
2149 int
2150 S_IS_FORWARD_REF (const symbolS *s)
2151 {
2152 if (LOCAL_SYMBOL_CHECK (s))
2153 return 0;
2154 return s->sy_forward_ref;
2155 }
2156
2157 const char *
2158 S_GET_NAME (symbolS *s)
2159 {
2160 if (LOCAL_SYMBOL_CHECK (s))
2161 return ((struct local_symbol *) s)->lsy_name;
2162 return s->bsym->name;
2163 }
2164
2165 segT
2166 S_GET_SEGMENT (symbolS *s)
2167 {
2168 if (LOCAL_SYMBOL_CHECK (s))
2169 return ((struct local_symbol *) s)->lsy_section;
2170 return s->bsym->section;
2171 }
2172
2173 void
2174 S_SET_SEGMENT (symbolS *s, segT seg)
2175 {
2176 /* Don't reassign section symbols. The direct reason is to prevent seg
2177 faults assigning back to const global symbols such as *ABS*, but it
2178 shouldn't happen anyway. */
2179
2180 if (LOCAL_SYMBOL_CHECK (s))
2181 {
2182 if (seg == reg_section)
2183 s = local_symbol_convert ((struct local_symbol *) s);
2184 else
2185 {
2186 ((struct local_symbol *) s)->lsy_section = seg;
2187 return;
2188 }
2189 }
2190
2191 if (s->bsym->flags & BSF_SECTION_SYM)
2192 {
2193 if (s->bsym->section != seg)
2194 abort ();
2195 }
2196 else
2197 s->bsym->section = seg;
2198 }
2199
2200 void
2201 S_SET_EXTERNAL (symbolS *s)
2202 {
2203 if (LOCAL_SYMBOL_CHECK (s))
2204 s = local_symbol_convert ((struct local_symbol *) s);
2205 if ((s->bsym->flags & BSF_WEAK) != 0)
2206 {
2207 /* Let .weak override .global. */
2208 return;
2209 }
2210 if (s->bsym->flags & BSF_SECTION_SYM)
2211 {
2212 char * file;
2213 unsigned int line;
2214
2215 /* Do not reassign section symbols. */
2216 as_where (& file, & line);
2217 as_warn_where (file, line,
2218 _("section symbols are already global"));
2219 return;
2220 }
2221 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2222 if (S_GET_SEGMENT (s) == reg_section)
2223 {
2224 as_bad ("can't make register symbol `%s' global",
2225 S_GET_NAME (s));
2226 return;
2227 }
2228 #endif
2229 s->bsym->flags |= BSF_GLOBAL;
2230 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2231
2232 #ifdef TE_PE
2233 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2234 an_external_name = S_GET_NAME (s);
2235 #endif
2236 }
2237
2238 void
2239 S_CLEAR_EXTERNAL (symbolS *s)
2240 {
2241 if (LOCAL_SYMBOL_CHECK (s))
2242 return;
2243 if ((s->bsym->flags & BSF_WEAK) != 0)
2244 {
2245 /* Let .weak override. */
2246 return;
2247 }
2248 s->bsym->flags |= BSF_LOCAL;
2249 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2250 }
2251
2252 void
2253 S_SET_WEAK (symbolS *s)
2254 {
2255 if (LOCAL_SYMBOL_CHECK (s))
2256 s = local_symbol_convert ((struct local_symbol *) s);
2257 #ifdef obj_set_weak_hook
2258 obj_set_weak_hook (s);
2259 #endif
2260 s->bsym->flags |= BSF_WEAK;
2261 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2262 }
2263
2264 void
2265 S_SET_WEAKREFR (symbolS *s)
2266 {
2267 if (LOCAL_SYMBOL_CHECK (s))
2268 s = local_symbol_convert ((struct local_symbol *) s);
2269 s->sy_weakrefr = 1;
2270 /* If the alias was already used, make sure we mark the target as
2271 used as well, otherwise it might be dropped from the symbol
2272 table. This may have unintended side effects if the alias is
2273 later redirected to another symbol, such as keeping the unused
2274 previous target in the symbol table. Since it will be weak, it's
2275 not a big deal. */
2276 if (s->sy_used)
2277 symbol_mark_used (s->sy_value.X_add_symbol);
2278 }
2279
2280 void
2281 S_CLEAR_WEAKREFR (symbolS *s)
2282 {
2283 if (LOCAL_SYMBOL_CHECK (s))
2284 return;
2285 s->sy_weakrefr = 0;
2286 }
2287
2288 void
2289 S_SET_WEAKREFD (symbolS *s)
2290 {
2291 if (LOCAL_SYMBOL_CHECK (s))
2292 s = local_symbol_convert ((struct local_symbol *) s);
2293 s->sy_weakrefd = 1;
2294 S_SET_WEAK (s);
2295 }
2296
2297 void
2298 S_CLEAR_WEAKREFD (symbolS *s)
2299 {
2300 if (LOCAL_SYMBOL_CHECK (s))
2301 return;
2302 if (s->sy_weakrefd)
2303 {
2304 s->sy_weakrefd = 0;
2305 /* If a weakref target symbol is weak, then it was never
2306 referenced directly before, not even in a .global directive,
2307 so decay it to local. If it remains undefined, it will be
2308 later turned into a global, like any other undefined
2309 symbol. */
2310 if (s->bsym->flags & BSF_WEAK)
2311 {
2312 #ifdef obj_clear_weak_hook
2313 obj_clear_weak_hook (s);
2314 #endif
2315 s->bsym->flags &= ~BSF_WEAK;
2316 s->bsym->flags |= BSF_LOCAL;
2317 }
2318 }
2319 }
2320
2321 void
2322 S_SET_THREAD_LOCAL (symbolS *s)
2323 {
2324 if (LOCAL_SYMBOL_CHECK (s))
2325 s = local_symbol_convert ((struct local_symbol *) s);
2326 if (bfd_is_com_section (s->bsym->section)
2327 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2328 return;
2329 s->bsym->flags |= BSF_THREAD_LOCAL;
2330 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2331 as_bad (_("Accessing function `%s' as thread-local object"),
2332 S_GET_NAME (s));
2333 else if (! bfd_is_und_section (s->bsym->section)
2334 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2335 as_bad (_("Accessing `%s' as thread-local object"),
2336 S_GET_NAME (s));
2337 }
2338
2339 void
2340 S_SET_NAME (symbolS *s, const char *name)
2341 {
2342 if (LOCAL_SYMBOL_CHECK (s))
2343 {
2344 ((struct local_symbol *) s)->lsy_name = name;
2345 return;
2346 }
2347 s->bsym->name = name;
2348 }
2349
2350 void
2351 S_SET_VOLATILE (symbolS *s)
2352 {
2353 if (LOCAL_SYMBOL_CHECK (s))
2354 s = local_symbol_convert ((struct local_symbol *) s);
2355 s->sy_volatile = 1;
2356 }
2357
2358 void
2359 S_CLEAR_VOLATILE (symbolS *s)
2360 {
2361 if (!LOCAL_SYMBOL_CHECK (s))
2362 s->sy_volatile = 0;
2363 }
2364
2365 void
2366 S_SET_FORWARD_REF (symbolS *s)
2367 {
2368 if (LOCAL_SYMBOL_CHECK (s))
2369 s = local_symbol_convert ((struct local_symbol *) s);
2370 s->sy_forward_ref = 1;
2371 }
2372
2373 /* Return the previous symbol in a chain. */
2374
2375 symbolS *
2376 symbol_previous (symbolS *s)
2377 {
2378 if (LOCAL_SYMBOL_CHECK (s))
2379 abort ();
2380 return s->sy_previous;
2381 }
2382
2383 /* Return the next symbol in a chain. */
2384
2385 symbolS *
2386 symbol_next (symbolS *s)
2387 {
2388 if (LOCAL_SYMBOL_CHECK (s))
2389 abort ();
2390 return s->sy_next;
2391 }
2392
2393 /* Return a pointer to the value of a symbol as an expression. */
2394
2395 expressionS *
2396 symbol_get_value_expression (symbolS *s)
2397 {
2398 if (LOCAL_SYMBOL_CHECK (s))
2399 s = local_symbol_convert ((struct local_symbol *) s);
2400 return &s->sy_value;
2401 }
2402
2403 /* Set the value of a symbol to an expression. */
2404
2405 void
2406 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2407 {
2408 if (LOCAL_SYMBOL_CHECK (s))
2409 s = local_symbol_convert ((struct local_symbol *) s);
2410 s->sy_value = *exp;
2411 S_CLEAR_WEAKREFR (s);
2412 }
2413
2414 /* Return whether 2 symbols are the same. */
2415
2416 int
2417 symbol_same_p (symbolS *s1, symbolS *s2)
2418 {
2419 if (s1->bsym == NULL
2420 && local_symbol_converted_p ((struct local_symbol *) s1))
2421 s1 = local_symbol_get_real_symbol ((struct local_symbol *) s1);
2422 if (s2->bsym == NULL
2423 && local_symbol_converted_p ((struct local_symbol *) s2))
2424 s2 = local_symbol_get_real_symbol ((struct local_symbol *) s2);
2425 return s1 == s2;
2426 }
2427
2428 /* Return a pointer to the X_add_number component of a symbol. */
2429
2430 offsetT *
2431 symbol_X_add_number (symbolS *s)
2432 {
2433 if (LOCAL_SYMBOL_CHECK (s))
2434 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2435
2436 return &s->sy_value.X_add_number;
2437 }
2438
2439 /* Set the value of SYM to the current position in the current segment. */
2440
2441 void
2442 symbol_set_value_now (symbolS *sym)
2443 {
2444 S_SET_SEGMENT (sym, now_seg);
2445 S_SET_VALUE (sym, frag_now_fix ());
2446 symbol_set_frag (sym, frag_now);
2447 }
2448
2449 /* Set the frag of a symbol. */
2450
2451 void
2452 symbol_set_frag (symbolS *s, fragS *f)
2453 {
2454 if (LOCAL_SYMBOL_CHECK (s))
2455 {
2456 local_symbol_set_frag ((struct local_symbol *) s, f);
2457 return;
2458 }
2459 s->sy_frag = f;
2460 S_CLEAR_WEAKREFR (s);
2461 }
2462
2463 /* Return the frag of a symbol. */
2464
2465 fragS *
2466 symbol_get_frag (symbolS *s)
2467 {
2468 if (LOCAL_SYMBOL_CHECK (s))
2469 return local_symbol_get_frag ((struct local_symbol *) s);
2470 return s->sy_frag;
2471 }
2472
2473 /* Mark a symbol as having been used. */
2474
2475 void
2476 symbol_mark_used (symbolS *s)
2477 {
2478 if (LOCAL_SYMBOL_CHECK (s))
2479 return;
2480 s->sy_used = 1;
2481 if (S_IS_WEAKREFR (s))
2482 symbol_mark_used (s->sy_value.X_add_symbol);
2483 }
2484
2485 /* Clear the mark of whether a symbol has been used. */
2486
2487 void
2488 symbol_clear_used (symbolS *s)
2489 {
2490 if (LOCAL_SYMBOL_CHECK (s))
2491 s = local_symbol_convert ((struct local_symbol *) s);
2492 s->sy_used = 0;
2493 }
2494
2495 /* Return whether a symbol has been used. */
2496
2497 int
2498 symbol_used_p (symbolS *s)
2499 {
2500 if (LOCAL_SYMBOL_CHECK (s))
2501 return 1;
2502 return s->sy_used;
2503 }
2504
2505 /* Mark a symbol as having been used in a reloc. */
2506
2507 void
2508 symbol_mark_used_in_reloc (symbolS *s)
2509 {
2510 if (LOCAL_SYMBOL_CHECK (s))
2511 s = local_symbol_convert ((struct local_symbol *) s);
2512 s->sy_used_in_reloc = 1;
2513 }
2514
2515 /* Clear the mark of whether a symbol has been used in a reloc. */
2516
2517 void
2518 symbol_clear_used_in_reloc (symbolS *s)
2519 {
2520 if (LOCAL_SYMBOL_CHECK (s))
2521 return;
2522 s->sy_used_in_reloc = 0;
2523 }
2524
2525 /* Return whether a symbol has been used in a reloc. */
2526
2527 int
2528 symbol_used_in_reloc_p (symbolS *s)
2529 {
2530 if (LOCAL_SYMBOL_CHECK (s))
2531 return 0;
2532 return s->sy_used_in_reloc;
2533 }
2534
2535 /* Mark a symbol as an MRI common symbol. */
2536
2537 void
2538 symbol_mark_mri_common (symbolS *s)
2539 {
2540 if (LOCAL_SYMBOL_CHECK (s))
2541 s = local_symbol_convert ((struct local_symbol *) s);
2542 s->sy_mri_common = 1;
2543 }
2544
2545 /* Clear the mark of whether a symbol is an MRI common symbol. */
2546
2547 void
2548 symbol_clear_mri_common (symbolS *s)
2549 {
2550 if (LOCAL_SYMBOL_CHECK (s))
2551 return;
2552 s->sy_mri_common = 0;
2553 }
2554
2555 /* Return whether a symbol is an MRI common symbol. */
2556
2557 int
2558 symbol_mri_common_p (symbolS *s)
2559 {
2560 if (LOCAL_SYMBOL_CHECK (s))
2561 return 0;
2562 return s->sy_mri_common;
2563 }
2564
2565 /* Mark a symbol as having been written. */
2566
2567 void
2568 symbol_mark_written (symbolS *s)
2569 {
2570 if (LOCAL_SYMBOL_CHECK (s))
2571 return;
2572 s->written = 1;
2573 }
2574
2575 /* Clear the mark of whether a symbol has been written. */
2576
2577 void
2578 symbol_clear_written (symbolS *s)
2579 {
2580 if (LOCAL_SYMBOL_CHECK (s))
2581 return;
2582 s->written = 0;
2583 }
2584
2585 /* Return whether a symbol has been written. */
2586
2587 int
2588 symbol_written_p (symbolS *s)
2589 {
2590 if (LOCAL_SYMBOL_CHECK (s))
2591 return 0;
2592 return s->written;
2593 }
2594
2595 /* Mark a symbol has having been resolved. */
2596
2597 void
2598 symbol_mark_resolved (symbolS *s)
2599 {
2600 if (LOCAL_SYMBOL_CHECK (s))
2601 {
2602 local_symbol_mark_resolved ((struct local_symbol *) s);
2603 return;
2604 }
2605 s->sy_resolved = 1;
2606 }
2607
2608 /* Return whether a symbol has been resolved. */
2609
2610 int
2611 symbol_resolved_p (symbolS *s)
2612 {
2613 if (LOCAL_SYMBOL_CHECK (s))
2614 return local_symbol_resolved_p ((struct local_symbol *) s);
2615 return s->sy_resolved;
2616 }
2617
2618 /* Return whether a symbol is a section symbol. */
2619
2620 int
2621 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2622 {
2623 if (LOCAL_SYMBOL_CHECK (s))
2624 return 0;
2625 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2626 }
2627
2628 /* Return whether a symbol is equated to another symbol. */
2629
2630 int
2631 symbol_equated_p (symbolS *s)
2632 {
2633 if (LOCAL_SYMBOL_CHECK (s))
2634 return 0;
2635 return s->sy_value.X_op == O_symbol;
2636 }
2637
2638 /* Return whether a symbol is equated to another symbol, and should be
2639 treated specially when writing out relocs. */
2640
2641 int
2642 symbol_equated_reloc_p (symbolS *s)
2643 {
2644 if (LOCAL_SYMBOL_CHECK (s))
2645 return 0;
2646 /* X_op_symbol, normally not used for O_symbol, is set by
2647 resolve_symbol_value to flag expression syms that have been
2648 equated. */
2649 return (s->sy_value.X_op == O_symbol
2650 #if defined (OBJ_COFF) && defined (TE_PE)
2651 && ! S_IS_WEAK (s)
2652 #endif
2653 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2654 || ! S_IS_DEFINED (s)
2655 || S_IS_COMMON (s)));
2656 }
2657
2658 /* Return whether a symbol has a constant value. */
2659
2660 int
2661 symbol_constant_p (symbolS *s)
2662 {
2663 if (LOCAL_SYMBOL_CHECK (s))
2664 return 1;
2665 return s->sy_value.X_op == O_constant;
2666 }
2667
2668 /* Return whether a symbol was cloned and thus removed from the global
2669 symbol list. */
2670
2671 int
2672 symbol_shadow_p (symbolS *s)
2673 {
2674 if (LOCAL_SYMBOL_CHECK (s))
2675 return 0;
2676 return s->sy_next == s;
2677 }
2678
2679 /* Return the BFD symbol for a symbol. */
2680
2681 asymbol *
2682 symbol_get_bfdsym (symbolS *s)
2683 {
2684 if (LOCAL_SYMBOL_CHECK (s))
2685 s = local_symbol_convert ((struct local_symbol *) s);
2686 return s->bsym;
2687 }
2688
2689 /* Set the BFD symbol for a symbol. */
2690
2691 void
2692 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2693 {
2694 if (LOCAL_SYMBOL_CHECK (s))
2695 s = local_symbol_convert ((struct local_symbol *) s);
2696 /* Usually, it is harmless to reset a symbol to a BFD section
2697 symbol. For example, obj_elf_change_section sets the BFD symbol
2698 of an old symbol with the newly created section symbol. But when
2699 we have multiple sections with the same name, the newly created
2700 section may have the same name as an old section. We check if the
2701 old symbol has been already marked as a section symbol before
2702 resetting it. */
2703 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2704 s->bsym = bsym;
2705 /* else XXX - What do we do now ? */
2706 }
2707
2708 #ifdef OBJ_SYMFIELD_TYPE
2709
2710 /* Get a pointer to the object format information for a symbol. */
2711
2712 OBJ_SYMFIELD_TYPE *
2713 symbol_get_obj (symbolS *s)
2714 {
2715 if (LOCAL_SYMBOL_CHECK (s))
2716 s = local_symbol_convert ((struct local_symbol *) s);
2717 return &s->sy_obj;
2718 }
2719
2720 /* Set the object format information for a symbol. */
2721
2722 void
2723 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2724 {
2725 if (LOCAL_SYMBOL_CHECK (s))
2726 s = local_symbol_convert ((struct local_symbol *) s);
2727 s->sy_obj = *o;
2728 }
2729
2730 #endif /* OBJ_SYMFIELD_TYPE */
2731
2732 #ifdef TC_SYMFIELD_TYPE
2733
2734 /* Get a pointer to the processor information for a symbol. */
2735
2736 TC_SYMFIELD_TYPE *
2737 symbol_get_tc (symbolS *s)
2738 {
2739 if (LOCAL_SYMBOL_CHECK (s))
2740 s = local_symbol_convert ((struct local_symbol *) s);
2741 return &s->sy_tc;
2742 }
2743
2744 /* Set the processor information for a symbol. */
2745
2746 void
2747 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2748 {
2749 if (LOCAL_SYMBOL_CHECK (s))
2750 s = local_symbol_convert ((struct local_symbol *) s);
2751 s->sy_tc = *o;
2752 }
2753
2754 #endif /* TC_SYMFIELD_TYPE */
2755
2756 void
2757 symbol_begin (void)
2758 {
2759 symbol_lastP = NULL;
2760 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2761 sy_hash = hash_new ();
2762 local_hash = hash_new ();
2763
2764 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2765 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2766 abs_symbol.bsym = bfd_abs_section.symbol;
2767 #endif
2768 abs_symbol.sy_value.X_op = O_constant;
2769 abs_symbol.sy_frag = &zero_address_frag;
2770
2771 if (LOCAL_LABELS_FB)
2772 fb_label_init ();
2773 }
2774
2775 void
2776 dot_symbol_init (void)
2777 {
2778 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
2779 if (dot_symbol.bsym == NULL)
2780 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2781 dot_symbol.bsym->name = ".";
2782 dot_symbol.sy_forward_ref = 1;
2783 dot_symbol.sy_value.X_op = O_constant;
2784 }
2785 \f
2786 int indent_level;
2787
2788 /* Maximum indent level.
2789 Available for modification inside a gdb session. */
2790 static int max_indent_level = 8;
2791
2792 void
2793 print_symbol_value_1 (FILE *file, symbolS *sym)
2794 {
2795 const char *name = S_GET_NAME (sym);
2796 if (!name || !name[0])
2797 name = "(unnamed)";
2798 fprintf (file, "sym ");
2799 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
2800 fprintf (file, " %s", name);
2801
2802 if (LOCAL_SYMBOL_CHECK (sym))
2803 {
2804 struct local_symbol *locsym = (struct local_symbol *) sym;
2805
2806 if (local_symbol_get_frag (locsym) != & zero_address_frag
2807 && local_symbol_get_frag (locsym) != NULL)
2808 {
2809 fprintf (file, " frag ");
2810 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
2811 }
2812 if (local_symbol_resolved_p (locsym))
2813 fprintf (file, " resolved");
2814 fprintf (file, " local");
2815 }
2816 else
2817 {
2818 if (sym->sy_frag != &zero_address_frag)
2819 {
2820 fprintf (file, " frag ");
2821 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
2822 }
2823 if (sym->written)
2824 fprintf (file, " written");
2825 if (sym->sy_resolved)
2826 fprintf (file, " resolved");
2827 else if (sym->sy_resolving)
2828 fprintf (file, " resolving");
2829 if (sym->sy_used_in_reloc)
2830 fprintf (file, " used-in-reloc");
2831 if (sym->sy_used)
2832 fprintf (file, " used");
2833 if (S_IS_LOCAL (sym))
2834 fprintf (file, " local");
2835 if (S_IS_EXTERNAL (sym))
2836 fprintf (file, " extern");
2837 if (S_IS_WEAK (sym))
2838 fprintf (file, " weak");
2839 if (S_IS_DEBUG (sym))
2840 fprintf (file, " debug");
2841 if (S_IS_DEFINED (sym))
2842 fprintf (file, " defined");
2843 }
2844 if (S_IS_WEAKREFR (sym))
2845 fprintf (file, " weakrefr");
2846 if (S_IS_WEAKREFD (sym))
2847 fprintf (file, " weakrefd");
2848 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2849 if (symbol_resolved_p (sym))
2850 {
2851 segT s = S_GET_SEGMENT (sym);
2852
2853 if (s != undefined_section
2854 && s != expr_section)
2855 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
2856 }
2857 else if (indent_level < max_indent_level
2858 && S_GET_SEGMENT (sym) != undefined_section)
2859 {
2860 indent_level++;
2861 fprintf (file, "\n%*s<", indent_level * 4, "");
2862 if (LOCAL_SYMBOL_CHECK (sym))
2863 fprintf (file, "constant %lx",
2864 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
2865 else
2866 print_expr_1 (file, &sym->sy_value);
2867 fprintf (file, ">");
2868 indent_level--;
2869 }
2870 fflush (file);
2871 }
2872
2873 void
2874 print_symbol_value (symbolS *sym)
2875 {
2876 indent_level = 0;
2877 print_symbol_value_1 (stderr, sym);
2878 fprintf (stderr, "\n");
2879 }
2880
2881 static void
2882 print_binary (FILE *file, const char *name, expressionS *exp)
2883 {
2884 indent_level++;
2885 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2886 print_symbol_value_1 (file, exp->X_add_symbol);
2887 fprintf (file, ">\n%*s<", indent_level * 4, "");
2888 print_symbol_value_1 (file, exp->X_op_symbol);
2889 fprintf (file, ">");
2890 indent_level--;
2891 }
2892
2893 void
2894 print_expr_1 (FILE *file, expressionS *exp)
2895 {
2896 fprintf (file, "expr ");
2897 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
2898 fprintf (file, " ");
2899 switch (exp->X_op)
2900 {
2901 case O_illegal:
2902 fprintf (file, "illegal");
2903 break;
2904 case O_absent:
2905 fprintf (file, "absent");
2906 break;
2907 case O_constant:
2908 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
2909 break;
2910 case O_symbol:
2911 indent_level++;
2912 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2913 print_symbol_value_1 (file, exp->X_add_symbol);
2914 fprintf (file, ">");
2915 maybe_print_addnum:
2916 if (exp->X_add_number)
2917 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2918 (unsigned long) exp->X_add_number);
2919 indent_level--;
2920 break;
2921 case O_register:
2922 fprintf (file, "register #%d", (int) exp->X_add_number);
2923 break;
2924 case O_big:
2925 fprintf (file, "big");
2926 break;
2927 case O_uminus:
2928 fprintf (file, "uminus -<");
2929 indent_level++;
2930 print_symbol_value_1 (file, exp->X_add_symbol);
2931 fprintf (file, ">");
2932 goto maybe_print_addnum;
2933 case O_bit_not:
2934 fprintf (file, "bit_not");
2935 break;
2936 case O_multiply:
2937 print_binary (file, "multiply", exp);
2938 break;
2939 case O_divide:
2940 print_binary (file, "divide", exp);
2941 break;
2942 case O_modulus:
2943 print_binary (file, "modulus", exp);
2944 break;
2945 case O_left_shift:
2946 print_binary (file, "lshift", exp);
2947 break;
2948 case O_right_shift:
2949 print_binary (file, "rshift", exp);
2950 break;
2951 case O_bit_inclusive_or:
2952 print_binary (file, "bit_ior", exp);
2953 break;
2954 case O_bit_exclusive_or:
2955 print_binary (file, "bit_xor", exp);
2956 break;
2957 case O_bit_and:
2958 print_binary (file, "bit_and", exp);
2959 break;
2960 case O_eq:
2961 print_binary (file, "eq", exp);
2962 break;
2963 case O_ne:
2964 print_binary (file, "ne", exp);
2965 break;
2966 case O_lt:
2967 print_binary (file, "lt", exp);
2968 break;
2969 case O_le:
2970 print_binary (file, "le", exp);
2971 break;
2972 case O_ge:
2973 print_binary (file, "ge", exp);
2974 break;
2975 case O_gt:
2976 print_binary (file, "gt", exp);
2977 break;
2978 case O_logical_and:
2979 print_binary (file, "logical_and", exp);
2980 break;
2981 case O_logical_or:
2982 print_binary (file, "logical_or", exp);
2983 break;
2984 case O_add:
2985 indent_level++;
2986 fprintf (file, "add\n%*s<", indent_level * 4, "");
2987 print_symbol_value_1 (file, exp->X_add_symbol);
2988 fprintf (file, ">\n%*s<", indent_level * 4, "");
2989 print_symbol_value_1 (file, exp->X_op_symbol);
2990 fprintf (file, ">");
2991 goto maybe_print_addnum;
2992 case O_subtract:
2993 indent_level++;
2994 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2995 print_symbol_value_1 (file, exp->X_add_symbol);
2996 fprintf (file, ">\n%*s<", indent_level * 4, "");
2997 print_symbol_value_1 (file, exp->X_op_symbol);
2998 fprintf (file, ">");
2999 goto maybe_print_addnum;
3000 default:
3001 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3002 break;
3003 }
3004 fflush (stdout);
3005 }
3006
3007 void
3008 print_expr (expressionS *exp)
3009 {
3010 print_expr_1 (stderr, exp);
3011 fprintf (stderr, "\n");
3012 }
3013
3014 void
3015 symbol_print_statistics (FILE *file)
3016 {
3017 hash_print_statistics (file, "symbol table", sy_hash);
3018 hash_print_statistics (file, "mini local symbol table", local_hash);
3019 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3020 local_symbol_count, local_symbol_conversion_count);
3021 }
3022
3023 #ifdef OBJ_COMPLEX_RELC
3024
3025 /* Convert given symbol to a new complex-relocation symbol name. This
3026 may be a recursive function, since it might be called for non-leaf
3027 nodes (plain symbols) in the expression tree. The caller owns the
3028 returning string, so should free it eventually. Errors are
3029 indicated via as_bad and a NULL return value. The given symbol
3030 is marked with sy_used_in_reloc. */
3031
3032 char *
3033 symbol_relc_make_sym (symbolS * sym)
3034 {
3035 char * terminal = NULL;
3036 const char * sname;
3037 char typetag;
3038 int sname_len;
3039
3040 gas_assert (sym != NULL);
3041
3042 /* Recurse to symbol_relc_make_expr if this symbol
3043 is defined as an expression or a plain value. */
3044 if ( S_GET_SEGMENT (sym) == expr_section
3045 || S_GET_SEGMENT (sym) == absolute_section)
3046 return symbol_relc_make_expr (& sym->sy_value);
3047
3048 /* This may be a "fake symbol" L0\001, referring to ".".
3049 Write out a special null symbol to refer to this position. */
3050 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3051 return xstrdup (".");
3052
3053 /* We hope this is a plain leaf symbol. Construct the encoding
3054 as {S,s}II...:CCCCCCC....
3055 where 'S'/'s' means section symbol / plain symbol
3056 III is decimal for the symbol name length
3057 CCC is the symbol name itself. */
3058 symbol_mark_used_in_reloc (sym);
3059
3060 sname = S_GET_NAME (sym);
3061 sname_len = strlen (sname);
3062 typetag = symbol_section_p (sym) ? 'S' : 's';
3063
3064 terminal = xmalloc (1 /* S or s */
3065 + 8 /* sname_len in decimal */
3066 + 1 /* _ spacer */
3067 + sname_len /* name itself */
3068 + 1 /* \0 */ );
3069
3070 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3071 return terminal;
3072 }
3073
3074 /* Convert given value to a new complex-relocation symbol name. This
3075 is a non-recursive function, since it is be called for leaf nodes
3076 (plain values) in the expression tree. The caller owns the
3077 returning string, so should free() it eventually. No errors. */
3078
3079 char *
3080 symbol_relc_make_value (offsetT val)
3081 {
3082 char * terminal = xmalloc (28); /* Enough for long long. */
3083
3084 terminal[0] = '#';
3085 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3086 return terminal;
3087 }
3088
3089 /* Convert given expression to a new complex-relocation symbol name.
3090 This is a recursive function, since it traverses the entire given
3091 expression tree. The caller owns the returning string, so should
3092 free() it eventually. Errors are indicated via as_bad() and a NULL
3093 return value. */
3094
3095 char *
3096 symbol_relc_make_expr (expressionS * exp)
3097 {
3098 char * opstr = NULL; /* Operator prefix string. */
3099 int arity = 0; /* Arity of this operator. */
3100 char * operands[3]; /* Up to three operands. */
3101 char * concat_string = NULL;
3102
3103 operands[0] = operands[1] = operands[2] = NULL;
3104
3105 gas_assert (exp != NULL);
3106
3107 /* Match known operators -> fill in opstr, arity, operands[] and fall
3108 through to construct subexpression fragments; may instead return
3109 string directly for leaf nodes. */
3110
3111 /* See expr.h for the meaning of all these enums. Many operators
3112 have an unnatural arity (X_add_number implicitly added). The
3113 conversion logic expands them to explicit "+" subexpressions. */
3114
3115 switch (exp->X_op)
3116 {
3117 default:
3118 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3119 break;
3120
3121 /* Leaf nodes. */
3122 case O_constant:
3123 return symbol_relc_make_value (exp->X_add_number);
3124
3125 case O_symbol:
3126 if (exp->X_add_number)
3127 {
3128 arity = 2;
3129 opstr = "+";
3130 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3131 operands[1] = symbol_relc_make_value (exp->X_add_number);
3132 break;
3133 }
3134 else
3135 return symbol_relc_make_sym (exp->X_add_symbol);
3136
3137 /* Helper macros for nesting nodes. */
3138
3139 #define HANDLE_XADD_OPT1(str_) \
3140 if (exp->X_add_number) \
3141 { \
3142 arity = 2; \
3143 opstr = "+:" str_; \
3144 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3145 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3146 break; \
3147 } \
3148 else \
3149 { \
3150 arity = 1; \
3151 opstr = str_; \
3152 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3153 } \
3154 break
3155
3156 #define HANDLE_XADD_OPT2(str_) \
3157 if (exp->X_add_number) \
3158 { \
3159 arity = 3; \
3160 opstr = "+:" str_; \
3161 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3162 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3163 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3164 } \
3165 else \
3166 { \
3167 arity = 2; \
3168 opstr = str_; \
3169 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3170 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3171 } \
3172 break
3173
3174 /* Nesting nodes. */
3175
3176 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3177 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3178 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3179 case O_multiply: HANDLE_XADD_OPT2 ("*");
3180 case O_divide: HANDLE_XADD_OPT2 ("/");
3181 case O_modulus: HANDLE_XADD_OPT2 ("%");
3182 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3183 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3184 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3185 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3186 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3187 case O_add: HANDLE_XADD_OPT2 ("+");
3188 case O_subtract: HANDLE_XADD_OPT2 ("-");
3189 case O_eq: HANDLE_XADD_OPT2 ("==");
3190 case O_ne: HANDLE_XADD_OPT2 ("!=");
3191 case O_lt: HANDLE_XADD_OPT2 ("<");
3192 case O_le: HANDLE_XADD_OPT2 ("<=");
3193 case O_ge: HANDLE_XADD_OPT2 (">=");
3194 case O_gt: HANDLE_XADD_OPT2 (">");
3195 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3196 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3197 }
3198
3199 /* Validate & reject early. */
3200 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3201 opstr = NULL;
3202 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3203 opstr = NULL;
3204 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3205 opstr = NULL;
3206
3207 if (opstr == NULL)
3208 concat_string = NULL;
3209 else
3210 {
3211 /* Allocate new string; include inter-operand padding gaps etc. */
3212 concat_string = xmalloc (strlen (opstr)
3213 + 1
3214 + (arity >= 1 ? (strlen (operands[0]) + 1 ) : 0)
3215 + (arity >= 2 ? (strlen (operands[1]) + 1 ) : 0)
3216 + (arity >= 3 ? (strlen (operands[2]) + 0 ) : 0)
3217 + 1);
3218 gas_assert (concat_string != NULL);
3219
3220 /* Format the thing. */
3221 sprintf (concat_string,
3222 (arity == 0 ? "%s" :
3223 arity == 1 ? "%s:%s" :
3224 arity == 2 ? "%s:%s:%s" :
3225 /* arity == 3 */ "%s:%s:%s:%s"),
3226 opstr, operands[0], operands[1], operands[2]);
3227 }
3228
3229 /* Free operand strings (not opstr). */
3230 if (arity >= 1) xfree (operands[0]);
3231 if (arity >= 2) xfree (operands[1]);
3232 if (arity >= 3) xfree (operands[2]);
3233
3234 return concat_string;
3235 }
3236
3237 #endif
This page took 0.1236 seconds and 4 git commands to generate.