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