Coverage Report

Created: 2026-04-04 08:16

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
30.6k
{
185
30.6k
  symbol_entry_t *entry = (symbol_entry_t *) e;
186
30.6k
  if (entry->sy.hash == 0)
187
12.9k
    entry->sy.hash = htab_hash_string (entry->sy.name);
188
189
30.6k
  return entry->sy.hash;
190
30.6k
}
191
192
/* Equality function for a symbol_entry.  */
193
194
static int
195
eq_symbol_entry (const void *a, const void *b)
196
101k
{
197
101k
  const symbol_entry_t *ea = a;
198
101k
  const symbol_entry_t *eb = b;
199
200
101k
  return (ea->sy.hash == eb->sy.hash
201
95.5k
    && strcmp (ea->sy.name, eb->sy.name) == 0);
202
101k
}
203
204
static void *
205
symbol_entry_find (htab_t table, const char *name)
206
94.5k
{
207
94.5k
  hashval_t hash = htab_hash_string (name);
208
94.5k
  symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
209
94.5k
            hash, name, 0, 0, 0 } };
210
94.5k
  return htab_find_with_hash (table, &needle, hash);
211
94.5k
}
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
23.6k
#define debug_verify_symchain(root, last) ((void) 0)
236
#endif
237
238
1
#define DOLLAR_LABEL_CHAR '\001'
239
5.06k
#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
1.95M
{
254
1.95M
  return obstack_alloc (&notes, size);
255
1.95M
}
256
257
void *
258
notes_calloc (size_t n, size_t size)
259
1.83M
{
260
1.83M
  size_t amt;
261
1.83M
  void *ret;
262
1.83M
  if (gas_mul_overflow (n, size, &amt))
263
0
    {
264
0
      obstack_alloc_failed_handler ();
265
0
      abort ();
266
0
    }
267
1.83M
  ret = notes_alloc (amt);
268
1.83M
  memset (ret, 0, amt);
269
1.83M
  return ret;
270
1.83M
}
271
272
void *
273
notes_memdup (const void *src, size_t copy_size, size_t alloc_size)
274
59.0k
{
275
59.0k
  void *ret = obstack_alloc (&notes, alloc_size);
276
59.0k
  memcpy (ret, src, copy_size);
277
59.0k
  if (alloc_size > copy_size)
278
12
    memset ((char *) ret + copy_size, 0, alloc_size - copy_size);
279
59.0k
  return ret;
280
59.0k
}
281
282
char *
283
notes_strdup (const char *str)
284
58.9k
{
285
58.9k
  size_t len = strlen (str) + 1;
286
58.9k
  return notes_memdup (str, len, len);
287
58.9k
}
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
10.9k
{
333
10.9k
  symbolS *symbolP = symbol_create (name, segment, frag, valu);
334
335
  /* Link to end of symbol chain.  */
336
10.9k
  symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
337
338
10.9k
  return symbolP;
339
10.9k
}
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
58.9k
{
347
58.9k
  char *ret;
348
349
58.9k
  gas_assert (name != NULL);
350
58.9k
  ret = notes_strdup (name);
351
352
#ifdef tc_canonicalize_symbol_name
353
  ret = tc_canonicalize_symbol_name (ret);
354
#endif
355
356
58.9k
  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
58.9k
  return ret;
365
58.9k
}
366
367
static void
368
symbol_init (symbolS *symbolP, const char *name, asection *sec,
369
       fragS *frag, valueT valu)
370
53.7k
{
371
53.7k
  symbolP->frag = frag;
372
53.7k
  symbolP->bsym = bfd_make_empty_symbol (stdoutput);
373
53.7k
  if (symbolP->bsym == NULL)
374
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
375
53.7k
  symbolP->bsym->name = name;
376
53.7k
  symbolP->bsym->section = sec;
377
378
53.7k
  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
53.7k
  S_SET_VALUE (symbolP, valu);
391
53.7k
  if (sec == reg_section)
392
990
    symbolP->x->value.X_op = O_register;
393
394
53.7k
  symbol_clear_list_pointers (symbolP);
395
396
53.7k
  obj_symbol_new_hook (symbolP);
397
398
#ifdef tc_symbol_new_hook
399
  tc_symbol_new_hook (symbolP);
400
#endif
401
53.7k
}
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
53.6k
{
408
53.6k
  const char *preserved_copy_of_name;
409
53.6k
  symbolS *symbolP;
410
53.6k
  size_t size;
411
412
53.6k
  preserved_copy_of_name = save_symbol_name (name);
413
414
53.6k
  size = sizeof (symbolS) + sizeof (struct xsymbol);
415
53.6k
  symbolP = notes_alloc (size);
416
417
  /* symbol must be born in some fixed state.  This seems as good as any.  */
418
53.6k
  memset (symbolP, 0, size);
419
53.6k
  symbolP->name = preserved_copy_of_name;
420
53.6k
  symbolP->x = (struct xsymbol *) (symbolP + 1);
421
422
53.6k
  symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
423
424
53.6k
  return symbolP;
425
53.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
5.28k
{
441
5.28k
  const char *name_copy;
442
5.28k
  struct local_symbol *ret;
443
5.28k
  struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
444
445
5.28k
  ++local_symbol_count;
446
447
5.28k
  name_copy = save_symbol_name (name);
448
449
5.28k
  ret = notes_alloc (sizeof *ret);
450
5.28k
  ret->flags = flags;
451
5.28k
  ret->hash = 0;
452
5.28k
  ret->name = name_copy;
453
5.28k
  ret->frag = frag;
454
5.28k
  ret->section = section;
455
5.28k
  ret->value = val;
456
457
5.28k
  htab_insert (sy_hash, ret, 1);
458
459
5.28k
  return ret;
460
5.28k
}
461
462
/* Convert a local symbol into a real symbol.  */
463
464
static symbolS *
465
local_symbol_convert (void *sym)
466
63
{
467
63
  symbol_entry_t *ent = sym;
468
63
  struct xsymbol *xtra;
469
63
  valueT val;
470
471
63
  gas_assert (ent->lsy.flags.local_symbol);
472
473
63
  ++local_symbol_conversion_count;
474
475
63
  xtra = notes_alloc (sizeof (*xtra));
476
63
  memset (xtra, 0, sizeof (*xtra));
477
63
  val = ent->lsy.value;
478
63
  ent->sy.x = xtra;
479
480
  /* Local symbols are always either defined or used.  */
481
63
  ent->sy.flags.used = 1;
482
63
  ent->sy.flags.local_symbol = 0;
483
484
63
  symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
485
63
  symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
486
487
63
  return &ent->sy;
488
63
}
489

490
static void
491
define_sym_at_dot (symbolS *symbolP)
492
2.78k
{
493
2.78k
  symbolP->frag = frag_now;
494
2.78k
  S_SET_VALUE (symbolP, frag_now_fix ());
495
2.78k
  S_SET_SEGMENT (symbolP, now_seg);
496
2.78k
}
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
8.89k
{
508
8.89k
  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
8.89k
  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
8.89k
  if ((symbolP = symbol_find (sym_name)) != 0)
558
4.26k
    {
559
4.26k
      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
4.26k
      if (symbolP->flags.local_symbol)
566
155
  {
567
155
    struct local_symbol *locsym = (struct local_symbol *) symbolP;
568
569
155
    if (locsym->section != undefined_section
570
7
        && (locsym->frag != frag_now
571
1
      || locsym->section != now_seg
572
1
      || locsym->value != frag_now_fix ()))
573
6
      {
574
6
        as_bad (_("symbol `%s' is already defined"), sym_name);
575
6
        return symbolP;
576
6
      }
577
578
149
    locsym->section = now_seg;
579
149
    locsym->frag = frag_now;
580
149
    locsym->value = frag_now_fix ();
581
149
  }
582
4.10k
      else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
583
4.10k
         || S_IS_COMMON (symbolP)
584
4.10k
         || S_IS_VOLATILE (symbolP))
585
5
  {
586
5
    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
5
    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
5
    else
601
5
      {
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
5
        if (((!S_IS_DEBUG (symbolP)
614
5
        && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
615
5
        && S_IS_EXTERNAL (symbolP))
616
0
       || S_GET_SEGMENT (symbolP) == bss_section)
617
5
      && (now_seg == data_section
618
5
          || now_seg == bss_section
619
0
          || now_seg == S_GET_SEGMENT (symbolP)))
620
5
    {
621
      /* Select which of the 2 cases this is.  */
622
5
      if (now_seg != data_section)
623
5
        {
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
5
          if (S_GET_VALUE (symbolP) < frag_now_fix ())
630
3
      S_SET_VALUE (symbolP, frag_now_fix ());
631
5
        }
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
5
    }
639
0
        else
640
0
    {
641
0
#if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
642
0
      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
0
      as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
652
0
          sym_name,
653
0
          segment_name (S_GET_SEGMENT (symbolP)),
654
0
          od_buf,
655
0
          (long) S_GET_VALUE (symbolP));
656
0
    }
657
5
      }     /* if the undefined symbol has no value  */
658
5
  }
659
4.10k
      else
660
4.10k
  {
661
    /* Don't blow up if the definition is the same.  */
662
4.10k
    if (!(frag_now == symbolP->frag
663
1.82k
    && S_GET_VALUE (symbolP) == frag_now_fix ()
664
1.31k
    && S_GET_SEGMENT (symbolP) == now_seg))
665
2.78k
      {
666
2.78k
        as_bad (_("symbol `%s' is already defined"), sym_name);
667
2.78k
        symbolP = symbol_clone (symbolP, 0);
668
2.78k
        define_sym_at_dot (symbolP);
669
2.78k
      }
670
4.10k
  }
671
672
4.26k
    }
673
4.63k
  else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
674
4.47k
    {
675
4.47k
      symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
676
4.47k
                 frag_now_fix ());
677
4.47k
    }
678
155
  else
679
155
    {
680
155
      symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
681
682
155
      symbol_table_insert (symbolP);
683
155
    }
684
685
8.89k
  if (mri_common_symbol != NULL)
686
35
    {
687
      /* This symbol is actually being defined within an MRI common
688
   section.  This requires special handling.  */
689
35
      if (symbolP->flags.local_symbol)
690
22
  symbolP = local_symbol_convert (symbolP);
691
35
      symbolP->x->value.X_op = O_symbol;
692
35
      symbolP->x->value.X_add_symbol = mri_common_symbol;
693
35
      symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
694
35
      symbolP->frag = &zero_address_frag;
695
35
      S_SET_SEGMENT (symbolP, expr_section);
696
35
      symbolP->flags.mri_common = 1;
697
35
    }
698
699
#ifdef tc_frob_label
700
  tc_frob_label (symbolP);
701
#endif
702
8.89k
#ifdef obj_frob_label
703
8.89k
  obj_frob_label (symbolP);
704
8.89k
#endif
705
8.89k
  if (flag_synth_cfi)
706
0
    ginsn_frob_label (symbolP);
707
708
8.89k
  return symbolP;
709
8.89k
}
710

711
/* Die if we can't insert the symbol.  */
712
713
void
714
symbol_table_insert (symbolS *symbolP)
715
20.8k
{
716
20.8k
  know (symbolP);
717
718
20.8k
  htab_insert (sy_hash, symbolP, 1);
719
20.8k
}
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
43.3k
{
727
43.3k
  symbolS *symbolP;
728
729
43.3k
  symbolP = symbol_find (name);
730
731
43.3k
  if (symbolP == NULL)
732
2.62k
    {
733
2.62k
      if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
734
234
  {
735
234
    symbolP = md_undefined_symbol ((char *) name);
736
234
    if (symbolP != NULL)
737
0
      return symbolP;
738
739
234
    symbolP = (symbolS *) local_symbol_make (name, undefined_section,
740
234
               &zero_address_frag, 0);
741
234
    return symbolP;
742
234
  }
743
744
2.38k
      symbolP = symbol_make (name);
745
746
2.38k
      symbol_table_insert (symbolP);
747
2.38k
    }        /* if symbol wasn't found */
748
749
43.1k
  return symbolP;
750
43.3k
}
751
752
symbolS *
753
symbol_make (const char *name)
754
2.94k
{
755
2.94k
  symbolS *symbolP;
756
757
  /* Let the machine description default it, e.g. for register names.  */
758
2.94k
  symbolP = md_undefined_symbol ((char *) name);
759
760
2.94k
  if (!symbolP)
761
2.93k
    symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
762
763
2.94k
  return symbolP;
764
2.94k
}
765
766
symbolS *
767
symbol_clone (symbolS *orgsymP, int replace)
768
15.9k
{
769
15.9k
  symbolS *newsymP;
770
15.9k
  asymbol *bsymorg, *bsymnew;
771
772
  /* Make sure we never clone the dot special symbol.  */
773
15.9k
  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
15.9k
  if (orgsymP->flags.local_symbol)
780
0
    orgsymP = local_symbol_convert (orgsymP);
781
15.9k
  bsymorg = orgsymP->bsym;
782
783
15.9k
  newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol));
784
15.9k
  *newsymP = *orgsymP;
785
15.9k
  newsymP->x = (struct xsymbol *) (newsymP + 1);
786
15.9k
  *newsymP->x = *orgsymP->x;
787
15.9k
  bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
788
15.9k
  if (bsymnew == NULL)
789
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
790
15.9k
  newsymP->bsym = bsymnew;
791
15.9k
  bsymnew->name = bsymorg->name;
792
15.9k
  bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
793
15.9k
  bsymnew->section = bsymorg->section;
794
15.9k
  bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), &orgsymP->bsym,
795
15.9k
        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
15.9k
  if (replace)
806
13.1k
    {
807
13.1k
      if (orgsymP->x->previous != NULL)
808
13.1k
  orgsymP->x->previous->x->next = newsymP;
809
0
      else
810
0
  symbol_rootP = newsymP;
811
13.1k
      if (orgsymP->x->next != NULL)
812
13.0k
  orgsymP->x->next->x->previous = newsymP;
813
97
      else
814
97
  symbol_lastP = newsymP;
815
816
      /* Symbols that won't be output can't be external.  */
817
13.1k
      S_CLEAR_EXTERNAL (orgsymP);
818
13.1k
      orgsymP->x->previous = orgsymP->x->next = orgsymP;
819
13.1k
      debug_verify_symchain (symbol_rootP, symbol_lastP);
820
821
13.1k
      symbol_table_insert (newsymP);
822
13.1k
    }
823
2.79k
  else
824
2.79k
    {
825
      /* Symbols that won't be output can't be external.  */
826
2.79k
      S_CLEAR_EXTERNAL (newsymP);
827
2.79k
      newsymP->x->previous = newsymP->x->next = newsymP;
828
2.79k
    }
829
830
15.9k
  return newsymP;
831
15.9k
}
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
521k
{
841
521k
  if (symbolP
842
47.4k
      && !symbolP->flags.local_symbol
843
46.6k
      && !symbolP->flags.forward_resolved)
844
4.35k
    {
845
4.35k
      symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
846
4.35k
      symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
847
4.35k
      symbolS *add_symbol = orig_add_symbol;
848
4.35k
      symbolS *op_symbol = orig_op_symbol;
849
850
4.35k
      if (symbolP->flags.forward_ref)
851
8
  is_forward = 1;
852
853
4.35k
      if (is_forward)
854
8
  {
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
8
    if (add_symbol && S_IS_VOLATILE (add_symbol))
859
0
      add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
860
8
    if (op_symbol && S_IS_VOLATILE (op_symbol))
861
0
      op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
862
8
  }
863
864
      /* Re-using resolving here, as this routine cannot get called from
865
   symbol resolution code.  */
866
4.35k
      if ((symbolP->bsym->section == expr_section
867
3.65k
     || symbolP->flags.forward_ref)
868
708
    && !symbolP->flags.resolving)
869
704
  {
870
704
    symbolP->flags.resolving = 1;
871
704
    add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
872
704
    op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
873
704
    symbolP->flags.resolving = 0;
874
704
  }
875
876
4.35k
      if (symbolP->flags.forward_ref
877
4.35k
    || add_symbol != orig_add_symbol
878
4.35k
    || op_symbol != orig_op_symbol)
879
8
  {
880
8
    if (symbolP != &dot_symbol)
881
8
      {
882
8
        symbolP = symbol_clone (symbolP, 0);
883
8
        symbolP->flags.resolving = 0;
884
8
      }
885
0
    else
886
0
      {
887
0
        symbolP = symbol_temp_new_now ();
888
#ifdef tc_new_dot_label
889
        tc_new_dot_label (symbolP);
890
#endif
891
0
      }
892
8
  }
893
894
4.35k
      symbolP->x->value.X_add_symbol = add_symbol;
895
4.35k
      symbolP->x->value.X_op_symbol = op_symbol;
896
4.35k
      symbolP->flags.forward_resolved = 1;
897
4.35k
    }
898
899
521k
  return symbolP;
900
521k
}
901
902
symbolS *
903
symbol_temp_new (segT seg, fragS *frag, valueT ofs)
904
3.62k
{
905
3.62k
  return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
906
3.62k
}
907
908
symbolS *
909
symbol_temp_new_now (void)
910
1.90k
{
911
1.90k
  return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
912
1.90k
}
913
914
symbolS *
915
symbol_temp_new_now_octets (void)
916
299
{
917
299
  return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
918
299
}
919
920
symbolS *
921
symbol_temp_make (void)
922
539
{
923
539
  return symbol_make (FAKE_LABEL_NAME);
924
539
}
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
94.5k
{
940
94.5k
  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
94.5k
  if (sym && ! noref)
948
80.2k
    S_CLEAR_WEAKREFD (sym);
949
950
94.5k
  return sym;
951
94.5k
}
952
953
symbolS *
954
symbol_find (const char *name)
955
94.5k
{
956
94.5k
  return symbol_find_noref (name, 0);
957
94.5k
}
958
959
symbolS *
960
symbol_find_noref (const char *name, int noref)
961
94.5k
{
962
94.5k
  symbolS * result;
963
94.5k
  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
94.5k
  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
94.5k
  result = symbol_find_exact_noref (name, noref);
992
94.5k
  free (copy);
993
94.5k
  return result;
994
94.5k
}
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
11.0k
{
1008
11.0k
  extern int symbol_table_frozen;
1009
11.0k
  if (symbol_table_frozen)
1010
0
    abort ();
1011
11.0k
  if (addme->flags.local_symbol)
1012
0
    abort ();
1013
11.0k
  if (target != NULL && target->flags.local_symbol)
1014
0
    abort ();
1015
1016
11.0k
  if (target == NULL)
1017
567
    {
1018
567
      know (*rootPP == NULL);
1019
567
      know (*lastPP == NULL);
1020
567
      addme->x->next = NULL;
1021
567
      addme->x->previous = NULL;
1022
567
      *rootPP = addme;
1023
567
      *lastPP = addme;
1024
567
      return;
1025
567
    }
1026
1027
10.4k
  if (target->x->next != NULL)
1028
0
    target->x->next->x->previous = addme;
1029
10.4k
  else
1030
10.4k
    *lastPP = addme;
1031
1032
10.4k
  addme->x->next = target->x->next;
1033
10.4k
  target->x->next = addme;
1034
10.4k
  addme->x->previous = target;
1035
1036
10.4k
  debug_verify_symchain (symbol_rootP, symbol_lastP);
1037
10.4k
}
1038
1039
/* Set the chain pointers of SYMBOL to null.  */
1040
1041
void
1042
symbol_clear_list_pointers (symbolS *symbolP)
1043
53.7k
{
1044
53.7k
  if (symbolP->flags.local_symbol)
1045
0
    abort ();
1046
53.7k
  symbolP->x->next = NULL;
1047
53.7k
  symbolP->x->previous = NULL;
1048
53.7k
}
1049
1050
/* Remove SYMBOLP from the list.  */
1051
1052
void
1053
symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1054
17
{
1055
17
  if (symbolP->flags.local_symbol)
1056
0
    abort ();
1057
1058
17
  if (symbolP->x->previous != NULL)
1059
17
    symbolP->x->previous->x->next = symbolP->x->next;
1060
0
  else
1061
0
    *rootPP = symbolP->x->next;
1062
1063
17
  if (symbolP->x->next != NULL)
1064
0
    symbolP->x->next->x->previous = symbolP->x->previous;
1065
17
  else
1066
17
    *lastPP = symbolP->x->previous;
1067
1068
17
  debug_verify_symchain (*rootPP, *lastPP);
1069
17
}
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
17
{
1077
17
  extern int symbol_table_frozen;
1078
17
  if (symbol_table_frozen)
1079
0
    abort ();
1080
17
  if (addme->flags.local_symbol)
1081
0
    abort ();
1082
17
  if (target->flags.local_symbol)
1083
0
    abort ();
1084
1085
17
  if (target->x->previous != NULL)
1086
0
    target->x->previous->x->next = addme;
1087
17
  else
1088
17
    *rootPP = addme;
1089
1090
17
  addme->x->previous = target->x->previous;
1091
17
  target->x->previous = addme;
1092
17
  addme->x->next = target;
1093
1094
17
  debug_verify_symchain (*rootPP, *lastPP);
1095
17
}
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
73.5k
{
1253
73.5k
  int resolved;
1254
73.5k
  valueT final_val;
1255
73.5k
  segT final_seg;
1256
1257
73.5k
  if (symp->flags.local_symbol)
1258
959
    {
1259
959
      struct local_symbol *locsym = (struct local_symbol *) symp;
1260
1261
959
      final_val = locsym->value;
1262
959
      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
959
      if (locsym->section->flags & SEC_OCTETS)
1268
0
  final_val += locsym->frag->fr_address;
1269
959
      else
1270
959
  final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1271
1272
959
      if (finalize_syms)
1273
0
  {
1274
0
    locsym->value = final_val;
1275
0
    locsym->flags.resolved = 1;
1276
0
  }
1277
1278
959
      return final_val;
1279
959
    }
1280
1281
72.5k
  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
72.5k
  resolved = 0;
1305
72.5k
  final_seg = S_GET_SEGMENT (symp);
1306
1307
72.5k
  if (symp->flags.resolving)
1308
1.27k
    {
1309
1.27k
      if (finalize_syms)
1310
0
  as_bad (_("symbol definition loop encountered at `%s'"),
1311
0
    S_GET_NAME (symp));
1312
1.27k
      final_val = 0;
1313
1.27k
      resolved = 1;
1314
1.27k
    }
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
71.3k
  else
1364
71.3k
    {
1365
71.3k
      symbolS *add_symbol, *op_symbol;
1366
71.3k
      offsetT left, right;
1367
71.3k
      segT seg_left, seg_right;
1368
71.3k
      operatorT op;
1369
71.3k
      int move_seg_ok;
1370
1371
71.3k
      symp->flags.resolving = 1;
1372
1373
      /* Help out with CSE.  */
1374
71.3k
      add_symbol = symp->x->value.X_add_symbol;
1375
71.3k
      op_symbol = symp->x->value.X_op_symbol;
1376
71.3k
      final_val = symp->x->value.X_add_number;
1377
71.3k
      op = symp->x->value.X_op;
1378
1379
71.3k
      switch (op)
1380
71.3k
  {
1381
0
  default:
1382
0
    BAD_CASE (op);
1383
0
    break;
1384
1385
0
  case O_md1:
1386
93
  case O_md2:
1387
93
  case O_md3:
1388
93
  case O_md4:
1389
93
  case O_md5:
1390
93
  case O_md6:
1391
93
  case O_md7:
1392
93
  case O_md8:
1393
93
  case O_md9:
1394
93
  case O_md10:
1395
93
  case O_md11:
1396
93
  case O_md12:
1397
93
  case O_md13:
1398
93
  case O_md14:
1399
93
  case O_md15:
1400
93
  case O_md16:
1401
93
  case O_md17:
1402
93
  case O_md18:
1403
93
  case O_md19:
1404
93
  case O_md20:
1405
93
  case O_md21:
1406
93
  case O_md22:
1407
93
  case O_md23:
1408
93
  case O_md24:
1409
93
  case O_md25:
1410
93
  case O_md26:
1411
93
  case O_md27:
1412
93
  case O_md28:
1413
93
  case O_md29:
1414
93
  case O_md30:
1415
93
  case O_md31:
1416
93
  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
93
    goto exit_dont_set_value;
1423
1424
0
  case O_absent:
1425
0
    final_val = 0;
1426
    /* Fall through.  */
1427
1428
50.1k
  case O_constant:
1429
    /* Symbols whose section has SEC_ELF_OCTETS set,
1430
       resolve to octets instead of target bytes. */
1431
50.1k
    if (symp->bsym->section->flags & SEC_OCTETS)
1432
2
      final_val += symp->frag->fr_address;
1433
50.1k
    else
1434
50.1k
      final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1435
50.1k
    if (final_seg == expr_section)
1436
0
      final_seg = absolute_section;
1437
    /* Fall through.  */
1438
1439
50.2k
  case O_register:
1440
50.2k
    resolved = 1;
1441
50.2k
    break;
1442
1443
216
  case O_symbol:
1444
216
  case O_symbol_rva:
1445
216
  case O_secidx:
1446
216
    left = resolve_symbol_value (add_symbol);
1447
216
    seg_left = S_GET_SEGMENT (add_symbol);
1448
216
    if (finalize_syms)
1449
0
      symp->x->value.X_op_symbol = NULL;
1450
1451
1.88k
  do_symbol:
1452
1.88k
    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
1.88k
    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
1.88k
    if (finalize_syms
1476
0
        && !add_symbol->flags.local_symbol
1477
0
        && add_symbol->flags.resolving)
1478
0
      break;
1479
1480
1.88k
    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
1.88k
        )
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
1.88k
    if (seg_left == undefined_section
1504
1.03k
        || bfd_is_com_section (seg_left)
1505
#if defined (OBJ_COFF) && defined (TE_PE)
1506
        || S_IS_WEAK (add_symbol)
1507
#endif
1508
1.03k
        || (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
848
      {
1514
848
        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
848
        final_seg = seg_left;
1523
848
        final_val += symp->frag->fr_address + left;
1524
848
        resolved = symbol_resolved_p (add_symbol);
1525
848
        symp->flags.resolving = 0;
1526
1527
848
        if (op == O_secidx && seg_left != undefined_section)
1528
0
    {
1529
0
      final_val = 0;
1530
0
      break;
1531
0
    }
1532
1533
848
        goto exit_dont_set_value;
1534
848
      }
1535
1.03k
    else
1536
1.03k
      {
1537
1.03k
        final_val += symp->frag->fr_address + left;
1538
1.03k
        if (final_seg == expr_section || final_seg == undefined_section)
1539
380
    final_seg = seg_left;
1540
1.03k
      }
1541
1542
1.03k
    resolved = symbol_resolved_p (add_symbol);
1543
1.03k
    if (S_IS_WEAKREFR (symp))
1544
0
      {
1545
0
        symp->flags.resolving = 0;
1546
0
        goto exit_dont_set_value;
1547
0
      }
1548
1.03k
    break;
1549
1550
1.03k
  case O_uminus:
1551
114
  case O_bit_not:
1552
1.36k
  case O_logical_not:
1553
1.36k
    left = resolve_symbol_value (add_symbol);
1554
1.36k
    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
1.36k
    if (op != O_logical_not && seg_left != absolute_section
1561
89
        && finalize_syms)
1562
0
      report_op_error (symp, NULL, op, add_symbol);
1563
1564
1.36k
    if (final_seg == expr_section || final_seg == undefined_section)
1565
501
      final_seg = absolute_section;
1566
1567
1.36k
    if (op == O_uminus)
1568
106
      left = -(valueT) left;
1569
1.25k
    else if (op == O_logical_not)
1570
1.24k
      left = !left;
1571
8
    else
1572
8
      left = ~left;
1573
1574
1.36k
    final_val += left + symp->frag->fr_address;
1575
1576
1.36k
    resolved = symbol_resolved_p (add_symbol);
1577
1.36k
    break;
1578
1579
32
  case O_multiply:
1580
34
  case O_divide:
1581
4.45k
  case O_modulus:
1582
4.45k
  case O_left_shift:
1583
4.50k
  case O_right_shift:
1584
4.50k
  case O_bit_inclusive_or:
1585
6.68k
  case O_bit_or_not:
1586
6.69k
  case O_bit_exclusive_or:
1587
6.70k
  case O_bit_and:
1588
6.77k
  case O_add:
1589
14.6k
  case O_subtract:
1590
15.4k
  case O_eq:
1591
15.4k
  case O_ne:
1592
15.8k
  case O_lt:
1593
17.3k
  case O_le:
1594
17.5k
  case O_ge:
1595
19.3k
  case O_gt:
1596
19.4k
  case O_logical_and:
1597
19.4k
  case O_logical_or:
1598
19.4k
    left = resolve_symbol_value (add_symbol);
1599
19.4k
    right = resolve_symbol_value (op_symbol);
1600
19.4k
    seg_left = S_GET_SEGMENT (add_symbol);
1601
19.4k
    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
19.4k
    if (op == O_add)
1606
66
      {
1607
66
        if (seg_right == absolute_section)
1608
44
    {
1609
44
      final_val += right;
1610
44
      goto do_symbol;
1611
44
    }
1612
22
        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
66
      }
1621
19.3k
    else if (op == O_subtract)
1622
7.90k
      {
1623
7.90k
        if (seg_right == absolute_section)
1624
1.62k
    {
1625
1.62k
      final_val -= right;
1626
1.62k
      goto do_symbol;
1627
1.62k
    }
1628
7.90k
      }
1629
1630
17.7k
    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
17.7k
    if (!(seg_left == absolute_section
1639
6.93k
    && seg_right == absolute_section)
1640
15.7k
        && !(op == O_eq || op == O_ne)
1641
14.9k
        && !((op == O_subtract
1642
8.68k
        || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1643
8.16k
       && seg_left == seg_right
1644
1.59k
       && (seg_left != undefined_section
1645
1.59k
           || add_symbol == op_symbol)))
1646
14.9k
      {
1647
        /* Don't emit messages unless we're finalizing the symbol value,
1648
     otherwise we may get the same message multiple times.  */
1649
14.9k
        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
14.9k
        else
1656
14.9k
    move_seg_ok = 0;
1657
14.9k
      }
1658
1659
17.7k
    if (move_seg_ok
1660
2.82k
        && (final_seg == expr_section || final_seg == undefined_section))
1661
1.12k
      final_seg = absolute_section;
1662
1663
    /* Check for division by zero.  */
1664
17.7k
    if ((op == O_divide || op == O_modulus) && right == 0)
1665
888
      {
1666
        /* If seg_right is not absolute_section, then we've
1667
     already issued a warning about using a bad symbol.  */
1668
888
        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
888
        right = 1;
1681
888
      }
1682
17.7k
    if ((op == O_left_shift || op == O_right_shift)
1683
54
        && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1684
4
      {
1685
4
        as_warn_value_out_of_range (_("shift count"), right, 0,
1686
4
            sizeof (valueT) * CHAR_BIT - 1,
1687
4
            NULL, 0);
1688
4
        left = right = 0;
1689
4
      }
1690
1691
17.7k
    switch (symp->x->value.X_op)
1692
17.7k
      {
1693
      /* See expr() for reasons of the use of valueT casts here.  */
1694
32
      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
4.41k
      case O_modulus:
1710
4.41k
        if (right == 1 || right == -1)
1711
888
    left = 0;
1712
3.52k
        else
1713
3.52k
    left %= right;
1714
4.41k
        break;
1715
1716
0
      case O_left_shift:
1717
0
        left = (valueT) left << (valueT) right; break;
1718
54
      case O_right_shift:
1719
54
        left = (valueT) left >> (valueT) right; break;
1720
4
      case O_bit_inclusive_or:  left |= right; break;
1721
2.17k
      case O_bit_or_not:    left |= ~right; break;
1722
14
      case O_bit_exclusive_or:  left ^= right; break;
1723
8
      case O_bit_and:   left &= right; break;
1724
22
      case O_add:     left += (valueT) right; break;
1725
6.27k
      case O_subtract:    left -= (valueT) right; break;
1726
770
      case O_eq:
1727
778
      case O_ne:
1728
778
        left = (left == right && seg_left == seg_right
1729
277
          && (seg_left != undefined_section
1730
276
        || add_symbol == op_symbol)
1731
778
          ? ~ (offsetT) 0 : 0);
1732
778
        if (symp->x->value.X_op == O_ne)
1733
8
    left = ~left;
1734
778
        break;
1735
368
      case O_lt:  left = left <  right ? ~ (offsetT) 0 : 0; break;
1736
1.56k
      case O_le:  left = left <= right ? ~ (offsetT) 0 : 0; break;
1737
184
      case O_ge:  left = left >= right ? ~ (offsetT) 0 : 0; break;
1738
1.79k
      case O_gt:  left = left >  right ? ~ (offsetT) 0 : 0; break;
1739
54
      case O_logical_and: left = left && right; break;
1740
20
      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
17.7k
      }
1752
1753
17.7k
    final_val += symp->frag->fr_address + left;
1754
17.7k
    if (final_seg == expr_section || final_seg == undefined_section)
1755
14.9k
      {
1756
14.9k
        if (seg_left == undefined_section
1757
5.46k
      || seg_right == undefined_section)
1758
14.7k
    final_seg = undefined_section;
1759
217
        else if (seg_left == absolute_section)
1760
7
    final_seg = seg_right;
1761
210
        else
1762
210
    final_seg = seg_left;
1763
14.9k
      }
1764
17.7k
    resolved = (symbol_resolved_p (add_symbol)
1765
0
          && symbol_resolved_p (op_symbol));
1766
17.7k
    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
71.3k
  }
1777
1778
70.3k
      symp->flags.resolving = 0;
1779
70.3k
    }
1780
1781
71.6k
  if (finalize_syms)
1782
0
    S_SET_VALUE (symp, final_val);
1783
1784
72.5k
 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
72.5k
    S_SET_SEGMENT (symp, final_seg);
1788
1789
  /* Don't worry if we can't resolve an expr_section symbol.  */
1790
72.5k
  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
72.5k
  return final_val;
1803
71.6k
}
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
15.8k
{
1831
15.8k
  symbolS *symbolP = *symbolPP;
1832
1833
15.8k
  if (symbolP->flags.local_symbol)
1834
112
    {
1835
112
      struct local_symbol *locsym = (struct local_symbol *) symbolP;
1836
1837
112
      *valueP = locsym->value;
1838
112
      *segP = locsym->section;
1839
112
      *fragPP = locsym->frag;
1840
112
    }
1841
15.7k
  else
1842
15.7k
    {
1843
15.7k
      expressionS exp = symbolP->x->value;
1844
1845
15.7k
      if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1846
15.7k
  {
1847
15.7k
    int resolved;
1848
1849
15.7k
    if (symbolP->flags.resolving)
1850
0
      return 0;
1851
15.7k
    symbolP->flags.resolving = 1;
1852
15.7k
    resolved = resolve_expression (&exp);
1853
15.7k
    symbolP->flags.resolving = 0;
1854
15.7k
    if (!resolved)
1855
876
      return 0;
1856
1857
14.8k
    switch (exp.X_op)
1858
14.8k
      {
1859
14.0k
      case O_constant:
1860
14.2k
      case O_register:
1861
14.2k
        if (!symbol_equated_p (symbolP))
1862
14.2k
    break;
1863
        /* Fallthru.  */
1864
646
      case O_symbol:
1865
646
      case O_symbol_rva:
1866
646
        symbolP = exp.X_add_symbol;
1867
646
        break;
1868
0
      default:
1869
0
        return 0;
1870
14.8k
      }
1871
14.8k
  }
1872
1873
14.8k
      *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
14.8k
      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
14.8k
      else
1886
14.8k
  {
1887
14.8k
    *valueP = exp.X_add_number;
1888
14.8k
    *segP = symbolP->bsym->section;
1889
14.8k
    *fragPP = symbolP->frag;
1890
14.8k
  }
1891
1892
14.8k
      if (*segP == expr_section)
1893
359
  switch (exp.X_op)
1894
359
    {
1895
323
    case O_constant: *segP = absolute_section; break;
1896
36
    case O_register: *segP = reg_section; break;
1897
0
    default: break;
1898
359
    }
1899
14.8k
    }
1900
1901
15.0k
  return 1;
1902
15.8k
}
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
14.8k
#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
2
#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
2055
2056
static void
2057
fb_label_init (void)
2058
567
{
2059
567
  memset (fb_low_counter, 0, sizeof (fb_low_counter));
2060
567
}
2061
2062
/* Add one to the instance number of this fb label.  */
2063
2064
void
2065
fb_label_instance_inc (unsigned int label)
2066
4.61k
{
2067
4.61k
  fb_ent *i;
2068
2069
4.61k
  if (label < FB_LABEL_SPECIAL)
2070
2.20k
    {
2071
2.20k
      ++fb_low_counter[label];
2072
2.20k
      return;
2073
2.20k
    }
2074
2075
2.41k
  if (fb_labels != NULL)
2076
2.41k
    {
2077
2.41k
      for (i = fb_labels + FB_LABEL_SPECIAL;
2078
22.4k
     i < fb_labels + fb_label_count; ++i)
2079
22.4k
  {
2080
22.4k
    if (*i == label)
2081
2.40k
      {
2082
2.40k
        ++fb_label_instances[i - fb_labels];
2083
2.40k
        return;
2084
2.40k
      }      /* if we find it  */
2085
22.4k
  }      /* for each existing label  */
2086
2.41k
    }
2087
2088
  /* If we get to here, we don't have label listed yet.  */
2089
2090
14
  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
13
  else if (fb_label_count == fb_label_max)
2099
1
    {
2100
1
      fb_label_max += FB_LABEL_BUMP_BY;
2101
1
      fb_labels = XRESIZEVEC (fb_ent, fb_labels, fb_label_max);
2102
1
      fb_label_instances = XRESIZEVEC (fb_ent, fb_label_instances,
2103
1
               fb_label_max);
2104
1
    }        /* if we needed to grow  */
2105
2106
14
  fb_labels[fb_label_count] = label;
2107
14
  fb_label_instances[fb_label_count] = 1;
2108
14
  ++fb_label_count;
2109
14
}
2110
2111
static unsigned int
2112
fb_label_instance (unsigned int label)
2113
5.06k
{
2114
5.06k
  fb_ent *i;
2115
2116
5.06k
  if (label < FB_LABEL_SPECIAL)
2117
2.37k
    return (fb_low_counter[label]);
2118
2119
2.69k
  if (fb_labels != NULL)
2120
2.69k
    {
2121
2.69k
      for (i = fb_labels + FB_LABEL_SPECIAL;
2122
26.0k
     i < fb_labels + fb_label_count; ++i)
2123
25.8k
  {
2124
25.8k
    if (*i == label)
2125
2.42k
      return (fb_label_instances[i - fb_labels]);
2126
25.8k
  }
2127
2.69k
    }
2128
2129
  /* We didn't find the label, so this must be a reference to the
2130
     first instance.  */
2131
271
  return 0;
2132
2.69k
}
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
5.06k
{
2150
  /* Returned to caller, then copied.  Used for created names ("4f").  */
2151
5.06k
  static char symbol_name_build[24];
2152
5.06k
  char *p = symbol_name_build;
2153
2154
#ifdef TC_MMIX
2155
  know (augend <= 2 /* See mmix_fb_label.  */);
2156
#else
2157
5.06k
  know (augend <= 1);
2158
5.06k
#endif
2159
2160
5.06k
#ifdef LOCAL_LABEL_PREFIX
2161
5.06k
  *p++ = LOCAL_LABEL_PREFIX;
2162
5.06k
#endif
2163
5.06k
  sprintf (p, "L%u%c%u",
2164
5.06k
     n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2165
5.06k
  return symbol_name_build;
2166
5.06k
}
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
11.0k
{
2216
11.0k
  if (s->flags.local_symbol)
2217
27
    return resolve_symbol_value (s);
2218
2219
10.9k
  if (!s->flags.resolved)
2220
10.9k
    {
2221
10.9k
      valueT val = resolve_symbol_value (s);
2222
10.9k
      if (!finalize_syms)
2223
10.9k
  return val;
2224
10.9k
    }
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
11.0k
{
2250
11.0k
  return S_GET_VALUE_WHERE (s, NULL, 0);
2251
11.0k
}
2252
2253
/* Set the value of a symbol.  */
2254
2255
void
2256
S_SET_VALUE (symbolS *s, valueT val)
2257
638k
{
2258
638k
  if (s->flags.local_symbol)
2259
0
    {
2260
0
      ((struct local_symbol *) s)->value = val;
2261
0
      return;
2262
0
    }
2263
2264
638k
  s->x->value.X_op = O_constant;
2265
638k
  s->x->value.X_add_number = (offsetT) val;
2266
638k
  s->x->value.X_unsigned = 0;
2267
638k
  S_CLEAR_WEAKREFR (s);
2268
638k
}
2269
2270
void
2271
copy_symbol_attributes (symbolS *dest, symbolS *src)
2272
3.92k
{
2273
3.92k
  if (dest->flags.local_symbol)
2274
0
    dest = local_symbol_convert (dest);
2275
3.92k
  if (src->flags.local_symbol)
2276
12
    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
3.92k
#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2281
3.92k
       | BSF_GNU_INDIRECT_FUNCTION)
2282
3.92k
  dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2283
2284
3.92k
#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2285
3.92k
  OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2286
3.92k
#endif
2287
2288
#ifdef TC_COPY_SYMBOL_ATTRIBUTES
2289
  TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2290
#endif
2291
3.92k
}
2292
2293
int
2294
S_IS_FUNCTION (const symbolS *s)
2295
2.20k
{
2296
2.20k
  flagword flags;
2297
2298
2.20k
  if (s->flags.local_symbol)
2299
0
    return 0;
2300
2301
2.20k
  flags = s->bsym->flags;
2302
2303
2.20k
  return (flags & BSF_FUNCTION) != 0;
2304
2.20k
}
2305
2306
int
2307
S_IS_EXTERNAL (const symbolS *s)
2308
135
{
2309
135
  flagword flags;
2310
2311
135
  if (s->flags.local_symbol)
2312
0
    return 0;
2313
2314
135
  flags = s->bsym->flags;
2315
2316
  /* Sanity check.  */
2317
135
  if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2318
0
    abort ();
2319
2320
135
  return (flags & BSF_GLOBAL) != 0;
2321
135
}
2322
2323
int
2324
S_IS_WEAK (const symbolS *s)
2325
31
{
2326
31
  if (s->flags.local_symbol)
2327
0
    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
31
  if (S_IS_WEAKREFR (s))
2333
0
    return S_IS_WEAK (s->x->value.X_add_symbol);
2334
31
  return (s->bsym->flags & BSF_WEAK) != 0;
2335
31
}
2336
2337
int
2338
S_IS_WEAKREFR (const symbolS *s)
2339
94.6k
{
2340
94.6k
  if (s->flags.local_symbol)
2341
0
    return 0;
2342
94.6k
  return s->flags.weakrefr != 0;
2343
94.6k
}
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.81k
{
2356
4.81k
  if (s->flags.local_symbol)
2357
8
    return 0;
2358
4.80k
  return bfd_is_com_section (s->bsym->section);
2359
4.81k
}
2360
2361
int
2362
S_IS_DEFINED (const symbolS *s)
2363
28.4k
{
2364
28.4k
  if (s->flags.local_symbol)
2365
25
    return ((const struct local_symbol *) s)->section != undefined_section;
2366
28.3k
  return s->bsym->section != undefined_section;
2367
28.4k
}
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
771
{
2380
771
  segT sec;
2381
771
  if (s->flags.local_symbol)
2382
27
    sec = ((const struct local_symbol *) s)->section;
2383
744
  else
2384
744
    {
2385
744
      if ((strict
2386
0
     && ((s->bsym->flags & BSF_WEAK) != 0
2387
0
         || (EXTERN_FORCE_RELOC
2388
0
       && (s->bsym->flags & BSF_GLOBAL) != 0)))
2389
744
    || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2390
0
  return true;
2391
744
      sec = s->bsym->section;
2392
744
    }
2393
771
  return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2394
771
}
2395
2396
int
2397
S_IS_DEBUG (const symbolS *s)
2398
6
{
2399
6
  if (s->flags.local_symbol)
2400
0
    return 0;
2401
6
  if (s->bsym->flags & BSF_DEBUGGING)
2402
0
    return 1;
2403
6
  return 0;
2404
6
}
2405
2406
int
2407
S_IS_LOCAL (const symbolS *s)
2408
11
{
2409
11
  flagword flags;
2410
11
  const char *name;
2411
2412
11
  if (s->flags.local_symbol)
2413
0
    return 1;
2414
2415
11
  if (S_IS_EXTERNAL (s))
2416
10
    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
7.53k
{
2455
7.53k
  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
7.53k
  return s->x->value.X_op == O_register;
2460
7.53k
}
2461
2462
int
2463
S_IS_VOLATILE (const symbolS *s)
2464
30.5k
{
2465
30.5k
  if (s->flags.local_symbol)
2466
0
    return 0;
2467
30.5k
  return s->flags.volatil;
2468
30.5k
}
2469
2470
int
2471
S_IS_FORWARD_REF (const symbolS *s)
2472
20.9k
{
2473
20.9k
  if (s->flags.local_symbol)
2474
2
    return 0;
2475
20.9k
  return s->flags.forward_ref;
2476
20.9k
}
2477
2478
const char *
2479
S_GET_NAME (const symbolS *s)
2480
1.78k
{
2481
1.78k
  return s->name;
2482
1.78k
}
2483
2484
segT
2485
S_GET_SEGMENT (const symbolS *s)
2486
218k
{
2487
218k
  if (s->flags.local_symbol)
2488
2.48k
    return ((const struct local_symbol *) s)->section;
2489
216k
  return s->bsym->section;
2490
218k
}
2491
2492
void
2493
S_SET_SEGMENT (symbolS *s, segT seg)
2494
667k
{
2495
667k
  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
667k
  if (s->bsym->flags & BSF_SECTION_SYM)
2505
12
    {
2506
12
      if (s->bsym->section != seg)
2507
0
  abort ();
2508
12
    }
2509
667k
  else
2510
667k
    {
2511
667k
      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
667k
      s->bsym->section = seg;
2524
667k
    }
2525
667k
}
2526
2527
void
2528
S_SET_EXTERNAL (symbolS *s)
2529
125
{
2530
125
  if (s->flags.local_symbol)
2531
0
    s = local_symbol_convert (s);
2532
125
  if ((s->bsym->flags & BSF_WEAK) != 0)
2533
9
    {
2534
      /* Let .weak override .global.  */
2535
9
      return;
2536
9
    }
2537
116
  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
116
#ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2544
116
  if (S_GET_SEGMENT (s) == reg_section)
2545
10
    {
2546
10
      as_bad (_("can't make register symbol global"));
2547
10
      return;
2548
10
    }
2549
106
#endif
2550
106
  s->bsym->flags |= BSF_GLOBAL;
2551
106
  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
106
}
2558
2559
void
2560
S_CLEAR_EXTERNAL (symbolS *s)
2561
21.1k
{
2562
21.1k
  if (s->flags.local_symbol)
2563
0
    return;
2564
21.1k
  if ((s->bsym->flags & BSF_WEAK) != 0)
2565
0
    {
2566
      /* Let .weak override.  */
2567
0
      return;
2568
0
    }
2569
21.1k
  s->bsym->flags |= BSF_LOCAL;
2570
21.1k
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2571
21.1k
}
2572
2573
void
2574
S_SET_WEAK (symbolS *s)
2575
323
{
2576
323
  if (s->flags.local_symbol)
2577
8
    s = local_symbol_convert (s);
2578
#ifdef obj_set_weak_hook
2579
  obj_set_weak_hook (s);
2580
#endif
2581
323
  s->bsym->flags |= BSF_WEAK;
2582
323
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2583
323
}
2584
2585
void
2586
S_SET_WEAKREFR (symbolS *s)
2587
5
{
2588
5
  if (s->flags.local_symbol)
2589
0
    s = local_symbol_convert (s);
2590
5
  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
5
  if (s->flags.used)
2598
2
    symbol_mark_used (s->x->value.X_add_symbol);
2599
5
}
2600
2601
void
2602
S_CLEAR_WEAKREFR (symbolS *s)
2603
1.29M
{
2604
1.29M
  if (s->flags.local_symbol)
2605
155
    return;
2606
1.29M
  s->flags.weakrefr = 0;
2607
1.29M
}
2608
2609
void
2610
S_SET_WEAKREFD (symbolS *s)
2611
3
{
2612
3
  if (s->flags.local_symbol)
2613
0
    s = local_symbol_convert (s);
2614
3
  s->flags.weakrefd = 1;
2615
3
  S_SET_WEAK (s);
2616
3
}
2617
2618
void
2619
S_CLEAR_WEAKREFD (symbolS *s)
2620
80.2k
{
2621
80.2k
  if (s->flags.local_symbol)
2622
729
    return;
2623
79.5k
  if (s->flags.weakrefd)
2624
1
    {
2625
1
      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
1
      if (s->bsym->flags & BSF_WEAK)
2632
1
  {
2633
#ifdef obj_clear_weak_hook
2634
    obj_clear_weak_hook (s);
2635
#endif
2636
1
    s->bsym->flags &= ~BSF_WEAK;
2637
1
    s->bsym->flags |= BSF_LOCAL;
2638
1
  }
2639
1
    }
2640
79.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
14.1k
{
2672
14.1k
  if (s->flags.local_symbol)
2673
8
    s = local_symbol_convert (s);
2674
14.1k
  s->flags.volatil = 1;
2675
14.1k
}
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
124
{
2687
124
  if (s->flags.local_symbol)
2688
5
    s = local_symbol_convert (s);
2689
124
  s->flags.forward_ref = 1;
2690
124
}
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
2.02k
{
2707
2.02k
  if (s->flags.local_symbol)
2708
0
    abort ();
2709
2.02k
  return s->x->next;
2710
2.02k
}
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
11.3k
{
2717
11.3k
  if (s->flags.local_symbol)
2718
2
    s = local_symbol_convert (s);
2719
11.3k
  return &s->x->value;
2720
11.3k
}
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
50.9k
{
2727
50.9k
  if (s->flags.local_symbol)
2728
0
    s = local_symbol_convert (s);
2729
50.9k
  s->x->value = *exp;
2730
50.9k
  S_CLEAR_WEAKREFR (s);
2731
50.9k
}
2732
2733
/* Return whether 2 symbols are the same.  */
2734
2735
int
2736
symbol_same_p (const symbolS *s1, const symbolS *s2)
2737
6.72k
{
2738
6.72k
  return s1 == s2;
2739
6.72k
}
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
16
{
2746
16
  if (s->flags.local_symbol)
2747
0
    return (offsetT *) &((struct local_symbol *) s)->value;
2748
2749
16
  return &s->x->value.X_add_number;
2750
16
}
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
576k
{
2757
576k
  S_SET_SEGMENT (sym, now_seg);
2758
576k
  S_SET_VALUE (sym, frag_now_fix ());
2759
576k
  symbol_set_frag (sym, frag_now);
2760
576k
}
2761
2762
/* Set the frag of a symbol.  */
2763
2764
void
2765
symbol_set_frag (symbolS *s, fragS *f)
2766
591k
{
2767
591k
  if (s->flags.local_symbol)
2768
2
    {
2769
2
      ((struct local_symbol *) s)->frag = f;
2770
2
      return;
2771
2
    }
2772
591k
  s->frag = f;
2773
591k
  S_CLEAR_WEAKREFR (s);
2774
591k
}
2775
2776
/* Return the frag of a symbol.  */
2777
2778
fragS *
2779
symbol_get_frag (const symbolS *s)
2780
22.6k
{
2781
22.6k
  if (s->flags.local_symbol)
2782
29
    return ((struct local_symbol *) s)->frag;
2783
22.6k
  return s->frag;
2784
22.6k
}
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
2.82k
{
2790
2.82k
  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
2.82k
  gas_assert (s->x->value.X_op == O_constant);
2799
2.82k
  *value = s->x->value.X_add_number;
2800
2.82k
  return s->frag;
2801
2.82k
}
2802
2803
/* Mark a symbol as having been used.  */
2804
2805
void
2806
symbol_mark_used (symbolS *s)
2807
93.2k
{
2808
93.2k
  if (s->flags.local_symbol)
2809
1.57k
    return;
2810
91.6k
  s->flags.used = 1;
2811
91.6k
  if (S_IS_WEAKREFR (s))
2812
158
    symbol_mark_used (s->x->value.X_add_symbol);
2813
91.6k
}
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
566
{
2840
566
  if (s->flags.local_symbol)
2841
0
    s = local_symbol_convert (s);
2842
566
  s->flags.used_in_reloc = 1;
2843
566
}
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
21.0k
{
2958
21.0k
  return s->flags.resolved;
2959
21.0k
}
2960
2961
/* Mark a symbol as being resolved.  */
2962
2963
void
2964
symbol_mark_resolving (symbolS *s)
2965
10.2k
{
2966
10.2k
  s->flags.resolving = 1;
2967
10.2k
}
2968
2969
void
2970
symbol_clear_resolving (symbolS *s)
2971
10.2k
{
2972
10.2k
  s->flags.resolving = 0;
2973
10.2k
}
2974
2975
/* Return whether a symbol is being resolved.  */
2976
2977
int
2978
symbol_resolving_p (const symbolS *s)
2979
10.2k
{
2980
10.2k
  return s->flags.resolving;
2981
10.2k
}
2982
2983
/* Return whether a symbol is a section symbol.  */
2984
2985
int
2986
symbol_section_p (const symbolS *s)
2987
23.0k
{
2988
23.0k
  if (s->flags.local_symbol)
2989
0
    return 0;
2990
23.0k
  return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2991
23.0k
}
2992
2993
/* Return whether a symbol is equated to another symbol.  */
2994
2995
int
2996
symbol_equated_p (const symbolS *s)
2997
23.0k
{
2998
23.0k
  if (s->flags.local_symbol)
2999
17
    return 0;
3000
22.9k
  return s->x->value.X_op == O_symbol;
3001
23.0k
}
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 the final symbol in a chain of equated symbols, and the offset
3024
   from that symbol.  If the chain has a loop, return NULL.
3025
   For a non-equated SYM, return SYM.  */
3026
3027
symbolS *
3028
symbol_equated_to (symbolS *sym, offsetT *off)
3029
6.73k
{
3030
6.73k
  valueT add = 0;
3031
6.73k
  symbolS *ret = sym;
3032
6.80k
  while (ret && symbol_equated_p (ret))
3033
67
    {
3034
67
      const expressionS *e = symbol_get_value_expression (ret);
3035
67
      add += e->X_add_number;
3036
67
      ret->flags.resolving = 1;
3037
67
      ret = e->X_add_symbol;
3038
67
      if (ret->flags.resolving)
3039
0
  ret = NULL;
3040
67
    }
3041
6.80k
  while (sym && sym->flags.resolving)
3042
67
    {
3043
67
      const expressionS *e = symbol_get_value_expression (sym);
3044
67
      sym->flags.resolving = 0;
3045
67
      sym = e->X_add_symbol;
3046
67
    }
3047
6.73k
  *off = add;
3048
6.73k
  return ret;
3049
6.73k
}
3050
3051
/* Return whether a symbol has a constant value.  */
3052
3053
int
3054
symbol_constant_p (const symbolS *s)
3055
36
{
3056
36
  if (s->flags.local_symbol)
3057
0
    return 1;
3058
36
  return s->x->value.X_op == O_constant;
3059
36
}
3060
3061
/* Return whether a symbol was cloned and thus removed from the global
3062
   symbol list.  */
3063
3064
int
3065
symbol_shadow_p (const symbolS *s)
3066
0
{
3067
0
  if (s->flags.local_symbol)
3068
0
    return 0;
3069
0
  return s->x->next == s;
3070
0
}
3071
3072
/* If S is a struct symbol return S, otherwise return NULL.  */
3073
3074
symbolS *
3075
symbol_symbolS (symbolS *s)
3076
0
{
3077
0
  if (s->flags.local_symbol)
3078
0
    return NULL;
3079
0
  return s;
3080
0
}
3081
3082
/* Return the BFD symbol for a symbol.  */
3083
3084
asymbol *
3085
symbol_get_bfdsym (symbolS *s)
3086
26.2k
{
3087
26.2k
  if (s->flags.local_symbol)
3088
4
    s = local_symbol_convert (s);
3089
26.2k
  return s->bsym;
3090
26.2k
}
3091
3092
/* Set the BFD symbol for a symbol.  */
3093
3094
void
3095
symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3096
4.09k
{
3097
4.09k
  if (s->flags.local_symbol)
3098
0
    s = local_symbol_convert (s);
3099
  /* Usually, it is harmless to reset a symbol to a BFD section
3100
     symbol. For example, obj_elf_change_section sets the BFD symbol
3101
     of an old symbol with the newly created section symbol. But when
3102
     we have multiple sections with the same name, the newly created
3103
     section may have the same name as an old section. We check if the
3104
     old symbol has been already marked as a section symbol before
3105
     resetting it.  */
3106
4.09k
  if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3107
4.09k
    s->bsym = bsym;
3108
  /* else XXX - What do we do now ?  */
3109
4.09k
}
3110
3111
#ifdef OBJ_SYMFIELD_TYPE
3112
3113
/* Get a pointer to the object format information for a symbol.  */
3114
3115
OBJ_SYMFIELD_TYPE *
3116
symbol_get_obj (symbolS *s)
3117
61.7k
{
3118
61.7k
  if (s->flags.local_symbol)
3119
2
    s = local_symbol_convert (s);
3120
61.7k
  return &s->x->obj;
3121
61.7k
}
3122
3123
/* Set the object format information for a symbol.  */
3124
3125
void
3126
symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
3127
0
{
3128
0
  if (s->flags.local_symbol)
3129
0
    s = local_symbol_convert (s);
3130
0
  s->x->obj = *o;
3131
0
}
3132
3133
#endif /* OBJ_SYMFIELD_TYPE */
3134
3135
#ifdef TC_SYMFIELD_TYPE
3136
3137
/* Get a pointer to the processor information for a symbol.  */
3138
3139
TC_SYMFIELD_TYPE *
3140
symbol_get_tc (symbolS *s)
3141
{
3142
  if (s->flags.local_symbol)
3143
    s = local_symbol_convert (s);
3144
  return &s->x->tc;
3145
}
3146
3147
/* Set the processor information for a symbol.  */
3148
3149
void
3150
symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
3151
{
3152
  if (s->flags.local_symbol)
3153
    s = local_symbol_convert (s);
3154
  s->x->tc = *o;
3155
}
3156
3157
#endif /* TC_SYMFIELD_TYPE */
3158
3159
void
3160
symbol_begin (void)
3161
567
{
3162
567
  symbol_lastP = NULL;
3163
567
  symbol_rootP = NULL;   /* In case we have 0 symbols (!!)  */
3164
567
  sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3165
567
             NULL, xcalloc, free);
3166
3167
567
#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3168
567
  abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3169
567
#endif
3170
567
  abs_symbol.x = &abs_symbol_x;
3171
567
  abs_symbol.x->value.X_op = O_constant;
3172
567
  abs_symbol.frag = &zero_address_frag;
3173
3174
567
  if (LOCAL_LABELS_FB)
3175
567
    fb_label_init ();
3176
567
}
3177
3178
void
3179
symbol_end (void)
3180
567
{
3181
567
  if (ENABLE_LEAK_CHECK)
3182
567
    htab_delete (sy_hash);
3183
567
}
3184
3185
void
3186
dot_symbol_init (void)
3187
567
{
3188
567
  dot_symbol.name = ".";
3189
567
  dot_symbol.flags.forward_ref = 1;
3190
567
  dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3191
567
  if (dot_symbol.bsym == NULL)
3192
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3193
567
  dot_symbol.bsym->name = ".";
3194
567
  dot_symbol.x = &dot_symbol_x;
3195
567
  dot_symbol.x->value.X_op = O_constant;
3196
567
}
3197

3198
int indent_level;
3199
3200
/* Maximum indent level.
3201
   Available for modification inside a gdb session.  */
3202
static int max_indent_level = 8;
3203
3204
void
3205
print_symbol_value_1 (FILE *file, symbolS *sym)
3206
0
{
3207
0
  const char *name = S_GET_NAME (sym);
3208
0
  if (!name || !name[0])
3209
0
    name = "(unnamed)";
3210
0
  fprintf (file, "sym %p %s", sym, name);
3211
3212
0
  if (sym->flags.local_symbol)
3213
0
    {
3214
0
      struct local_symbol *locsym = (struct local_symbol *) sym;
3215
3216
0
      if (locsym->frag != &zero_address_frag
3217
0
    && locsym->frag != NULL)
3218
0
  fprintf (file, " frag %p", locsym->frag);
3219
0
      if (locsym->flags.resolved)
3220
0
  fprintf (file, " resolved");
3221
0
      fprintf (file, " local");
3222
0
    }
3223
0
  else
3224
0
    {
3225
0
      if (sym->frag != &zero_address_frag)
3226
0
  fprintf (file, " frag %p", sym->frag);
3227
0
      if (sym->flags.written)
3228
0
  fprintf (file, " written");
3229
0
      if (sym->flags.resolved)
3230
0
  fprintf (file, " resolved");
3231
0
      else if (sym->flags.resolving)
3232
0
  fprintf (file, " resolving");
3233
0
      if (sym->flags.used_in_reloc)
3234
0
  fprintf (file, " used-in-reloc");
3235
0
      if (sym->flags.used)
3236
0
  fprintf (file, " used");
3237
0
      if (S_IS_LOCAL (sym))
3238
0
  fprintf (file, " local");
3239
0
      if (S_IS_EXTERNAL (sym))
3240
0
  fprintf (file, " extern");
3241
0
      if (S_IS_WEAK (sym))
3242
0
  fprintf (file, " weak");
3243
0
      if (S_IS_DEBUG (sym))
3244
0
  fprintf (file, " debug");
3245
0
      if (S_IS_DEFINED (sym))
3246
0
  fprintf (file, " defined");
3247
0
    }
3248
0
  if (S_IS_WEAKREFR (sym))
3249
0
    fprintf (file, " weakrefr");
3250
0
  if (S_IS_WEAKREFD (sym))
3251
0
    fprintf (file, " weakrefd");
3252
0
  fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3253
0
  if (symbol_resolved_p (sym))
3254
0
    {
3255
0
      segT s = S_GET_SEGMENT (sym);
3256
3257
0
      if (s != undefined_section
3258
0
    && s != expr_section)
3259
0
  fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
3260
0
    }
3261
0
  else if (indent_level < max_indent_level
3262
0
     && S_GET_SEGMENT (sym) != undefined_section)
3263
0
    {
3264
0
      indent_level++;
3265
0
      fprintf (file, "\n%*s<", indent_level * 4, "");
3266
0
      if (sym->flags.local_symbol)
3267
0
  fprintf (file, "constant %lx",
3268
0
     (unsigned long) ((struct local_symbol *) sym)->value);
3269
0
      else
3270
0
  print_expr_1 (file, &sym->x->value);
3271
0
      fprintf (file, ">");
3272
0
      indent_level--;
3273
0
    }
3274
0
  fflush (file);
3275
0
}
3276
3277
void
3278
print_symbol_value (symbolS *sym)
3279
0
{
3280
0
  indent_level = 0;
3281
0
  print_symbol_value_1 (stderr, sym);
3282
0
  fprintf (stderr, "\n");
3283
0
}
3284
3285
static void
3286
print_binary (FILE *file, const char *name, expressionS *exp)
3287
0
{
3288
0
  indent_level++;
3289
0
  fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3290
0
  print_symbol_value_1 (file, exp->X_add_symbol);
3291
0
  fprintf (file, ">\n%*s<", indent_level * 4, "");
3292
0
  print_symbol_value_1 (file, exp->X_op_symbol);
3293
0
  fprintf (file, ">");
3294
0
  indent_level--;
3295
0
}
3296
3297
void
3298
print_expr_1 (FILE *file, expressionS *exp)
3299
0
{
3300
0
  fprintf (file, "expr %p ", exp);
3301
0
  switch (exp->X_op)
3302
0
    {
3303
0
    case O_illegal:
3304
0
      fprintf (file, "illegal");
3305
0
      break;
3306
0
    case O_absent:
3307
0
      fprintf (file, "absent");
3308
0
      break;
3309
0
    case O_constant:
3310
0
      fprintf (file, "constant %" PRIx64, (uint64_t) exp->X_add_number);
3311
0
      break;
3312
0
    case O_symbol:
3313
0
      indent_level++;
3314
0
      fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3315
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3316
0
      fprintf (file, ">");
3317
0
    maybe_print_addnum:
3318
0
      if (exp->X_add_number)
3319
0
  fprintf (file, "\n%*s%" PRIx64, indent_level * 4, "",
3320
0
     (uint64_t) exp->X_add_number);
3321
0
      indent_level--;
3322
0
      break;
3323
0
    case O_register:
3324
0
      fprintf (file, "register #%d", (int) exp->X_add_number);
3325
0
      break;
3326
0
    case O_big:
3327
0
      fprintf (file, "big");
3328
0
      break;
3329
0
    case O_uminus:
3330
0
      fprintf (file, "uminus -<");
3331
0
      indent_level++;
3332
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3333
0
      fprintf (file, ">");
3334
0
      goto maybe_print_addnum;
3335
0
    case O_bit_not:
3336
0
      fprintf (file, "bit_not");
3337
0
      break;
3338
0
    case O_multiply:
3339
0
      print_binary (file, "multiply", exp);
3340
0
      break;
3341
0
    case O_divide:
3342
0
      print_binary (file, "divide", exp);
3343
0
      break;
3344
0
    case O_modulus:
3345
0
      print_binary (file, "modulus", exp);
3346
0
      break;
3347
0
    case O_left_shift:
3348
0
      print_binary (file, "lshift", exp);
3349
0
      break;
3350
0
    case O_right_shift:
3351
0
      print_binary (file, "rshift", exp);
3352
0
      break;
3353
0
    case O_bit_inclusive_or:
3354
0
      print_binary (file, "bit_ior", exp);
3355
0
      break;
3356
0
    case O_bit_exclusive_or:
3357
0
      print_binary (file, "bit_xor", exp);
3358
0
      break;
3359
0
    case O_bit_and:
3360
0
      print_binary (file, "bit_and", exp);
3361
0
      break;
3362
0
    case O_eq:
3363
0
      print_binary (file, "eq", exp);
3364
0
      break;
3365
0
    case O_ne:
3366
0
      print_binary (file, "ne", exp);
3367
0
      break;
3368
0
    case O_lt:
3369
0
      print_binary (file, "lt", exp);
3370
0
      break;
3371
0
    case O_le:
3372
0
      print_binary (file, "le", exp);
3373
0
      break;
3374
0
    case O_ge:
3375
0
      print_binary (file, "ge", exp);
3376
0
      break;
3377
0
    case O_gt:
3378
0
      print_binary (file, "gt", exp);
3379
0
      break;
3380
0
    case O_logical_and:
3381
0
      print_binary (file, "logical_and", exp);
3382
0
      break;
3383
0
    case O_logical_or:
3384
0
      print_binary (file, "logical_or", exp);
3385
0
      break;
3386
0
    case O_add:
3387
0
      indent_level++;
3388
0
      fprintf (file, "add\n%*s<", indent_level * 4, "");
3389
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3390
0
      fprintf (file, ">\n%*s<", indent_level * 4, "");
3391
0
      print_symbol_value_1 (file, exp->X_op_symbol);
3392
0
      fprintf (file, ">");
3393
0
      goto maybe_print_addnum;
3394
0
    case O_subtract:
3395
0
      indent_level++;
3396
0
      fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3397
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3398
0
      fprintf (file, ">\n%*s<", indent_level * 4, "");
3399
0
      print_symbol_value_1 (file, exp->X_op_symbol);
3400
0
      fprintf (file, ">");
3401
0
      goto maybe_print_addnum;
3402
0
    default:
3403
0
      fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3404
0
      break;
3405
0
    }
3406
0
  fflush (stdout);
3407
0
}
3408
3409
void
3410
print_expr (expressionS *exp)
3411
0
{
3412
0
  print_expr_1 (stderr, exp);
3413
0
  fprintf (stderr, "\n");
3414
0
}
3415
3416
void
3417
symbol_print_statistics (FILE *file)
3418
0
{
3419
0
  htab_print_statistics (file, "symbol table", sy_hash);
3420
0
  fprintf (file, "%lu mini local symbols created, %lu converted\n",
3421
0
     local_symbol_count, local_symbol_conversion_count);
3422
0
}
3423
3424
#ifdef OBJ_COMPLEX_RELC
3425
3426
/* Convert given symbol to a new complex-relocation symbol name.  This
3427
   may be a recursive function, since it might be called for non-leaf
3428
   nodes (plain symbols) in the expression tree.  The caller owns the
3429
   returning string, so should free it eventually.  Errors are
3430
   indicated via as_bad and a NULL return value.  The given symbol
3431
   is marked with used_in_reloc.  */
3432
3433
char *
3434
symbol_relc_make_sym (symbolS * sym)
3435
{
3436
  char * terminal = NULL;
3437
  const char * sname;
3438
  char typetag;
3439
  int sname_len;
3440
3441
  gas_assert (sym != NULL);
3442
3443
  /* Recurse to symbol_relc_make_expr if this symbol
3444
     is defined as an expression or a plain value.  */
3445
  if (   S_GET_SEGMENT (sym) == expr_section
3446
      || S_GET_SEGMENT (sym) == absolute_section)
3447
    return symbol_relc_make_expr (symbol_get_value_expression (sym));
3448
3449
  /* This may be a "fake symbol", referring to ".".
3450
     Write out a special null symbol to refer to this position.  */
3451
  if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3452
    return xstrdup (".");
3453
3454
  /* We hope this is a plain leaf symbol.  Construct the encoding
3455
     as {S,s}II...:CCCCCCC....
3456
     where 'S'/'s' means section symbol / plain symbol
3457
     III is decimal for the symbol name length
3458
     CCC is the symbol name itself.  */
3459
  symbol_mark_used_in_reloc (sym);
3460
3461
  sname = S_GET_NAME (sym);
3462
  sname_len = strlen (sname);
3463
  typetag = symbol_section_p (sym) ? 'S' : 's';
3464
3465
  terminal = XNEWVEC (char, (1 /* S or s */
3466
           + 8 /* sname_len in decimal */
3467
           + 1 /* _ spacer */
3468
           + sname_len /* name itself */
3469
           + 1 /* \0 */ ));
3470
3471
  sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3472
  return terminal;
3473
}
3474
3475
/* Convert given value to a new complex-relocation symbol name.  This
3476
   is a non-recursive function, since it is be called for leaf nodes
3477
   (plain values) in the expression tree.  The caller owns the
3478
   returning string, so should free() it eventually.  No errors.  */
3479
3480
char *
3481
symbol_relc_make_value (offsetT val)
3482
{
3483
  char * terminal = XNEWVEC (char, 28);  /* Enough for long long.  */
3484
3485
  terminal[0] = '#';
3486
  bfd_sprintf_vma (stdoutput, terminal + 1, val);
3487
  return terminal;
3488
}
3489
3490
/* Convert given expression to a new complex-relocation symbol name.
3491
   This is a recursive function, since it traverses the entire given
3492
   expression tree.  The caller owns the returning string, so should
3493
   free() it eventually.  Errors are indicated via as_bad() and a NULL
3494
   return value.  */
3495
3496
char *
3497
symbol_relc_make_expr (expressionS * exp)
3498
{
3499
  const char * opstr = NULL; /* Operator prefix string.  */
3500
  int    arity = 0;    /* Arity of this operator.  */
3501
  char * operands[3];  /* Up to three operands.  */
3502
  char * concat_string = NULL;
3503
3504
  operands[0] = operands[1] = operands[2] = NULL;
3505
3506
  gas_assert (exp != NULL);
3507
3508
  /* Match known operators -> fill in opstr, arity, operands[] and fall
3509
     through to construct subexpression fragments; may instead return
3510
     string directly for leaf nodes.  */
3511
3512
  /* See expr.h for the meaning of all these enums.  Many operators
3513
     have an unnatural arity (X_add_number implicitly added).  The
3514
     conversion logic expands them to explicit "+" subexpressions.   */
3515
3516
  switch (exp->X_op)
3517
    {
3518
    default:
3519
      as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3520
      break;
3521
3522
      /* Leaf nodes.  */
3523
    case O_constant:
3524
      return symbol_relc_make_value (exp->X_add_number);
3525
3526
    case O_symbol:
3527
      if (exp->X_add_number)
3528
  {
3529
    arity = 2;
3530
    opstr = "+";
3531
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3532
    operands[1] = symbol_relc_make_value (exp->X_add_number);
3533
    break;
3534
  }
3535
      else
3536
  return symbol_relc_make_sym (exp->X_add_symbol);
3537
3538
      /* Helper macros for nesting nodes.  */
3539
3540
#define HANDLE_XADD_OPT1(str_)            \
3541
      if (exp->X_add_number)            \
3542
  {               \
3543
    arity = 2;              \
3544
    opstr = "+:" str_;            \
3545
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3546
    operands[1] = symbol_relc_make_value (exp->X_add_number); \
3547
    break;              \
3548
  }               \
3549
      else                \
3550
  {               \
3551
    arity = 1;              \
3552
    opstr = str_;             \
3553
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3554
  }               \
3555
      break
3556
3557
#define HANDLE_XADD_OPT2(str_)            \
3558
      if (exp->X_add_number)            \
3559
  {               \
3560
    arity = 3;              \
3561
    opstr = "+:" str_;            \
3562
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3563
    operands[1] = symbol_relc_make_sym (exp->X_op_symbol);  \
3564
    operands[2] = symbol_relc_make_value (exp->X_add_number); \
3565
  }               \
3566
      else                \
3567
  {               \
3568
    arity = 2;              \
3569
    opstr = str_;             \
3570
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3571
    operands[1] = symbol_relc_make_sym (exp->X_op_symbol);  \
3572
  }               \
3573
      break
3574
3575
      /* Nesting nodes.  */
3576
3577
    case O_uminus:    HANDLE_XADD_OPT1 ("0-");
3578
    case O_bit_not:   HANDLE_XADD_OPT1 ("~");
3579
    case O_logical_not:   HANDLE_XADD_OPT1 ("!");
3580
    case O_multiply:    HANDLE_XADD_OPT2 ("*");
3581
    case O_divide:    HANDLE_XADD_OPT2 ("/");
3582
    case O_modulus:   HANDLE_XADD_OPT2 ("%");
3583
    case O_left_shift:    HANDLE_XADD_OPT2 ("<<");
3584
    case O_right_shift:   HANDLE_XADD_OPT2 (">>");
3585
    case O_bit_inclusive_or:  HANDLE_XADD_OPT2 ("|");
3586
    case O_bit_exclusive_or:  HANDLE_XADD_OPT2 ("^");
3587
    case O_bit_and:   HANDLE_XADD_OPT2 ("&");
3588
    case O_add:     HANDLE_XADD_OPT2 ("+");
3589
    case O_subtract:    HANDLE_XADD_OPT2 ("-");
3590
    case O_eq:      HANDLE_XADD_OPT2 ("==");
3591
    case O_ne:      HANDLE_XADD_OPT2 ("!=");
3592
    case O_lt:      HANDLE_XADD_OPT2 ("<");
3593
    case O_le:      HANDLE_XADD_OPT2 ("<=");
3594
    case O_ge:      HANDLE_XADD_OPT2 (">=");
3595
    case O_gt:      HANDLE_XADD_OPT2 (">");
3596
    case O_logical_and:   HANDLE_XADD_OPT2 ("&&");
3597
    case O_logical_or:    HANDLE_XADD_OPT2 ("||");
3598
    }
3599
3600
  /* Validate & reject early.  */
3601
  if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3602
    opstr = NULL;
3603
  if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3604
    opstr = NULL;
3605
  if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3606
    opstr = NULL;
3607
3608
  if (opstr == NULL)
3609
    concat_string = NULL;
3610
  else if (arity == 0)
3611
    concat_string = xstrdup (opstr);
3612
  else if (arity == 1)
3613
    concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3614
  else if (arity == 2)
3615
    concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3616
          (char *) NULL);
3617
  else
3618
    concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3619
          operands[2], (char *) NULL);
3620
3621
  /* Free operand strings (not opstr).  */
3622
  if (arity >= 1) xfree (operands[0]);
3623
  if (arity >= 2) xfree (operands[1]);
3624
  if (arity >= 3) xfree (operands[2]);
3625
3626
  return concat_string;
3627
}
3628
3629
#endif