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