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