Coverage Report

Created: 2026-03-10 08:46

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