Coverage Report

Created: 2026-11-21 09:00

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
15.0k
{
185
15.0k
  symbol_entry_t *entry = (symbol_entry_t *) e;
186
15.0k
  if (entry->sy.hash == 0)
187
8.88k
    entry->sy.hash = htab_hash_string (entry->sy.name);
188
189
15.0k
  return entry->sy.hash;
190
15.0k
}
191
192
/* Equality function for a symbol_entry.  */
193
194
static int
195
eq_symbol_entry (const void *a, const void *b)
196
93.1k
{
197
93.1k
  const symbol_entry_t *ea = a;
198
93.1k
  const symbol_entry_t *eb = b;
199
200
93.1k
  return (ea->sy.hash == eb->sy.hash
201
89.3k
    && strcmp (ea->sy.name, eb->sy.name) == 0);
202
93.1k
}
203
204
static void *
205
symbol_entry_find (htab_t table, const char *name)
206
95.6k
{
207
95.6k
  hashval_t hash = htab_hash_string (name);
208
95.6k
  symbol_entry_t needle = { { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
209
95.6k
            hash, name, 0, 0, 0 } };
210
95.6k
  return htab_find_with_hash (table, &needle, hash);
211
95.6k
}
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
14.9k
#define debug_verify_symchain(root, last) ((void) 0)
236
#endif
237
238
4
#define DOLLAR_LABEL_CHAR '\001'
239
4.84k
#define LOCAL_LABEL_CHAR  '\002'
240
241
#ifndef TC_LABEL_IS_LOCAL
242
4
#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.34M
{
254
1.34M
  return obstack_alloc (&notes, size);
255
1.34M
}
256
257
void *
258
notes_calloc (size_t n, size_t size)
259
1.21M
{
260
1.21M
  size_t amt;
261
1.21M
  void *ret;
262
1.21M
  if (_bfd_mul_overflow (n, size, &amt))
263
0
    {
264
0
      obstack_alloc_failed_handler ();
265
0
      abort ();
266
0
    }
267
1.21M
  ret = notes_alloc (amt);
268
1.21M
  memset (ret, 0, amt);
269
1.21M
  return ret;
270
1.21M
}
271
272
void *
273
notes_memdup (const void *src, size_t copy_size, size_t alloc_size)
274
70.5k
{
275
70.5k
  void *ret = obstack_alloc (&notes, alloc_size);
276
70.5k
  memcpy (ret, src, copy_size);
277
70.5k
  if (alloc_size > copy_size)
278
0
    memset ((char *) ret + copy_size, 0, alloc_size - copy_size);
279
70.5k
  return ret;
280
70.5k
}
281
282
char *
283
notes_strdup (const char *str)
284
70.5k
{
285
70.5k
  size_t len = strlen (str) + 1;
286
70.5k
  return notes_memdup (str, len, len);
287
70.5k
}
288
289
char *
290
notes_concat (const char *first, ...)
291
35
{
292
35
  va_list args;
293
35
  const char *str;
294
295
35
  va_start (args, first);
296
105
  for (str = first; str; str = va_arg (args, const char *))
297
70
    {
298
70
      size_t size = strlen (str);
299
70
      obstack_grow (&notes, str, size);
300
70
    }
301
35
  va_end (args);
302
35
  obstack_1grow (&notes, 0);
303
35
  return obstack_finish (&notes);
304
35
}
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.5k
{
333
10.5k
  symbolS *symbolP = symbol_create (name, segment, frag, valu);
334
335
  /* Link to end of symbol chain.  */
336
10.5k
  symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
337
338
10.5k
  return symbolP;
339
10.5k
}
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
70.5k
{
347
70.5k
  char *ret;
348
349
70.5k
  gas_assert (name != NULL);
350
70.5k
  ret = notes_strdup (name);
351
352
#ifdef tc_canonicalize_symbol_name
353
  ret = tc_canonicalize_symbol_name (ret);
354
#endif
355
356
70.5k
  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
70.5k
  return ret;
365
70.5k
}
366
367
static void
368
symbol_init (symbolS *symbolP, const char *name, asection *sec,
369
       fragS *frag, valueT valu)
370
65.3k
{
371
65.3k
  symbolP->frag = frag;
372
65.3k
  symbolP->bsym = bfd_make_empty_symbol (stdoutput);
373
65.3k
  if (symbolP->bsym == NULL)
374
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
375
65.3k
  symbolP->bsym->name = name;
376
65.3k
  symbolP->bsym->section = sec;
377
378
65.3k
  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
65.3k
  S_SET_VALUE (symbolP, valu);
391
65.3k
  if (sec == reg_section)
392
2.38k
    symbolP->x->value.X_op = O_register;
393
394
65.3k
  symbol_clear_list_pointers (symbolP);
395
396
65.3k
  obj_symbol_new_hook (symbolP);
397
398
#ifdef tc_symbol_new_hook
399
  tc_symbol_new_hook (symbolP);
400
#endif
401
65.3k
}
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
65.3k
{
408
65.3k
  const char *preserved_copy_of_name;
409
65.3k
  symbolS *symbolP;
410
65.3k
  size_t size;
411
412
65.3k
  preserved_copy_of_name = save_symbol_name (name);
413
414
65.3k
  size = sizeof (symbolS) + sizeof (struct xsymbol);
415
65.3k
  symbolP = notes_alloc (size);
416
417
  /* symbol must be born in some fixed state.  This seems as good as any.  */
418
65.3k
  memset (symbolP, 0, size);
419
65.3k
  symbolP->name = preserved_copy_of_name;
420
65.3k
  symbolP->x = (struct xsymbol *) (symbolP + 1);
421
422
65.3k
  symbol_init (symbolP, preserved_copy_of_name, segment, frag, valu);
423
424
65.3k
  return symbolP;
425
65.3k
}
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.21k
{
441
5.21k
  const char *name_copy;
442
5.21k
  struct local_symbol *ret;
443
5.21k
  struct symbol_flags flags = { .local_symbol = 1, .resolved = 0 };
444
445
5.21k
  ++local_symbol_count;
446
447
5.21k
  name_copy = save_symbol_name (name);
448
449
5.21k
  ret = notes_alloc (sizeof *ret);
450
5.21k
  ret->flags = flags;
451
5.21k
  ret->hash = 0;
452
5.21k
  ret->name = name_copy;
453
5.21k
  ret->frag = frag;
454
5.21k
  ret->section = section;
455
5.21k
  ret->value = val;
456
457
5.21k
  htab_insert (sy_hash, ret, 1);
458
459
5.21k
  return ret;
460
5.21k
}
461
462
/* Convert a local symbol into a real symbol.  */
463
464
static symbolS *
465
local_symbol_convert (void *sym)
466
16
{
467
16
  symbol_entry_t *ent = sym;
468
16
  struct xsymbol *xtra;
469
16
  valueT val;
470
471
16
  gas_assert (ent->lsy.flags.local_symbol);
472
473
16
  ++local_symbol_conversion_count;
474
475
16
  xtra = notes_alloc (sizeof (*xtra));
476
16
  memset (xtra, 0, sizeof (*xtra));
477
16
  val = ent->lsy.value;
478
16
  ent->sy.x = xtra;
479
480
  /* Local symbols are always either defined or used.  */
481
16
  ent->sy.flags.used = 1;
482
16
  ent->sy.flags.local_symbol = 0;
483
484
16
  symbol_init (&ent->sy, ent->lsy.name, ent->lsy.section, ent->lsy.frag, val);
485
16
  symbol_append (&ent->sy, symbol_lastP, &symbol_rootP, &symbol_lastP);
486
487
16
  return &ent->sy;
488
16
}
489

490
static void
491
define_sym_at_dot (symbolS *symbolP)
492
646
{
493
646
  symbolP->frag = frag_now;
494
646
  S_SET_VALUE (symbolP, frag_now_fix ());
495
646
  S_SET_SEGMENT (symbolP, now_seg);
496
646
}
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.74k
{
508
8.74k
  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.74k
  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.74k
  if ((symbolP = symbol_find (sym_name)) != 0)
558
3.81k
    {
559
3.81k
      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
3.81k
      if (symbolP->flags.local_symbol)
566
1.41k
  {
567
1.41k
    struct local_symbol *locsym = (struct local_symbol *) symbolP;
568
569
1.41k
    if (locsym->section != undefined_section
570
1.41k
        && (locsym->frag != frag_now
571
1.40k
      || locsym->section != now_seg
572
1.40k
      || locsym->value != frag_now_fix ()))
573
12
      {
574
12
        as_bad (_("symbol `%s' is already defined"), sym_name);
575
12
        return symbolP;
576
12
      }
577
578
1.40k
    locsym->section = now_seg;
579
1.40k
    locsym->frag = frag_now;
580
1.40k
    locsym->value = frag_now_fix ();
581
1.40k
  }
582
2.39k
      else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
583
2.39k
         || S_IS_COMMON (symbolP)
584
2.17k
         || S_IS_VOLATILE (symbolP))
585
225
  {
586
225
    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
225
    if (S_GET_VALUE (symbolP) == 0)
593
10
      {
594
10
        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
10
      }
600
215
    else
601
215
      {
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
215
        if (((!S_IS_DEBUG (symbolP)
614
215
        && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
615
215
        && S_IS_EXTERNAL (symbolP))
616
0
       || S_GET_SEGMENT (symbolP) == bss_section)
617
215
      && (now_seg == data_section
618
215
          || now_seg == bss_section
619
211
          || now_seg == S_GET_SEGMENT (symbolP)))
620
4
    {
621
      /* Select which of the 2 cases this is.  */
622
4
      if (now_seg != data_section)
623
4
        {
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
4
          if (S_GET_VALUE (symbolP) < frag_now_fix ())
630
0
      S_SET_VALUE (symbolP, frag_now_fix ());
631
4
        }
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
4
    }
639
211
        else
640
211
    {
641
211
#if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT))
642
211
      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
211
      as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
652
211
          sym_name,
653
211
          segment_name (S_GET_SEGMENT (symbolP)),
654
211
          od_buf,
655
211
          (long) S_GET_VALUE (symbolP));
656
211
    }
657
215
      }      /* if the undefined symbol has no value  */
658
225
  }
659
2.17k
      else
660
2.17k
  {
661
    /* Don't blow up if the definition is the same.  */
662
2.17k
    if (!(frag_now == symbolP->frag
663
1.82k
    && S_GET_VALUE (symbolP) == frag_now_fix ()
664
1.53k
    && S_GET_SEGMENT (symbolP) == now_seg))
665
636
      {
666
636
        as_bad (_("symbol `%s' is already defined"), sym_name);
667
636
        symbolP = symbol_clone (symbolP, 0);
668
636
        define_sym_at_dot (symbolP);
669
636
      }
670
2.17k
  }
671
672
3.81k
    }
673
4.93k
  else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
674
4.79k
    {
675
4.79k
      symbolP = (symbolS *) local_symbol_make (sym_name, now_seg, frag_now,
676
4.79k
                 frag_now_fix ());
677
4.79k
    }
678
145
  else
679
145
    {
680
145
      symbolP = symbol_new (sym_name, now_seg, frag_now, frag_now_fix ());
681
682
145
      symbol_table_insert (symbolP);
683
145
    }
684
685
8.73k
  if (mri_common_symbol != NULL)
686
229
    {
687
      /* This symbol is actually being defined within an MRI common
688
   section.  This requires special handling.  */
689
229
      if (symbolP->flags.local_symbol)
690
1
  symbolP = local_symbol_convert (symbolP);
691
229
      symbolP->x->value.X_op = O_symbol;
692
229
      symbolP->x->value.X_add_symbol = mri_common_symbol;
693
229
      symbolP->x->value.X_add_number = S_GET_VALUE (mri_common_symbol);
694
229
      symbolP->frag = &zero_address_frag;
695
229
      S_SET_SEGMENT (symbolP, expr_section);
696
229
      symbolP->flags.mri_common = 1;
697
229
    }
698
699
#ifdef tc_frob_label
700
  tc_frob_label (symbolP);
701
#endif
702
8.73k
#ifdef obj_frob_label
703
8.73k
  obj_frob_label (symbolP);
704
8.73k
#endif
705
8.73k
  if (flag_synth_cfi)
706
0
    ginsn_frob_label (symbolP);
707
708
8.73k
  return symbolP;
709
8.74k
}
710

711
/* Die if we can't insert the symbol.  */
712
713
void
714
symbol_table_insert (symbolS *symbolP)
715
8.30k
{
716
8.30k
  know (symbolP);
717
718
8.30k
  htab_insert (sy_hash, symbolP, 1);
719
8.30k
}
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
65.9k
{
727
65.9k
  symbolS *symbolP;
728
729
65.9k
  symbolP = symbol_find (name);
730
731
65.9k
  if (symbolP == NULL)
732
1.64k
    {
733
1.64k
      if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
734
52
  {
735
52
    symbolP = md_undefined_symbol ((char *) name);
736
52
    if (symbolP != NULL)
737
0
      return symbolP;
738
739
52
    symbolP = (symbolS *) local_symbol_make (name, undefined_section,
740
52
               &zero_address_frag, 0);
741
52
    return symbolP;
742
52
  }
743
744
1.59k
      symbolP = symbol_make (name);
745
746
1.59k
      symbol_table_insert (symbolP);
747
1.59k
    }        /* if symbol wasn't found */
748
749
65.9k
  return symbolP;
750
65.9k
}
751
752
symbolS *
753
symbol_make (const char *name)
754
2.04k
{
755
2.04k
  symbolS *symbolP;
756
757
  /* Let the machine description default it, e.g. for register names.  */
758
2.04k
  symbolP = md_undefined_symbol ((char *) name);
759
760
2.04k
  if (!symbolP)
761
2.02k
    symbolP = symbol_new (name, undefined_section, &zero_address_frag, 0);
762
763
2.04k
  return symbolP;
764
2.04k
}
765
766
symbolS *
767
symbol_clone (symbolS *orgsymP, int replace)
768
5.27k
{
769
5.27k
  symbolS *newsymP;
770
5.27k
  asymbol *bsymorg, *bsymnew;
771
772
  /* Make sure we never clone the dot special symbol.  */
773
5.27k
  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
5.27k
  if (orgsymP->flags.local_symbol)
780
0
    orgsymP = local_symbol_convert (orgsymP);
781
5.27k
  bsymorg = orgsymP->bsym;
782
783
5.27k
  newsymP = notes_alloc (sizeof (symbolS) + sizeof (struct xsymbol));
784
5.27k
  *newsymP = *orgsymP;
785
5.27k
  newsymP->x = (struct xsymbol *) (newsymP + 1);
786
5.27k
  *newsymP->x = *orgsymP->x;
787
5.27k
  bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
788
5.27k
  if (bsymnew == NULL)
789
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
790
5.27k
  newsymP->bsym = bsymnew;
791
5.27k
  bsymnew->name = bsymorg->name;
792
5.27k
  bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
793
5.27k
  bsymnew->section = bsymorg->section;
794
5.27k
  bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), &orgsymP->bsym,
795
5.27k
        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
5.27k
  if (replace)
806
4.64k
    {
807
4.64k
      if (orgsymP->x->previous != NULL)
808
4.64k
  orgsymP->x->previous->x->next = newsymP;
809
0
      else
810
0
  symbol_rootP = newsymP;
811
4.64k
      if (orgsymP->x->next != NULL)
812
4.06k
  orgsymP->x->next->x->previous = newsymP;
813
573
      else
814
573
  symbol_lastP = newsymP;
815
816
      /* Symbols that won't be output can't be external.  */
817
4.64k
      S_CLEAR_EXTERNAL (orgsymP);
818
4.64k
      orgsymP->x->previous = orgsymP->x->next = orgsymP;
819
4.64k
      debug_verify_symchain (symbol_rootP, symbol_lastP);
820
821
4.64k
      symbol_table_insert (newsymP);
822
4.64k
    }
823
636
  else
824
636
    {
825
      /* Symbols that won't be output can't be external.  */
826
636
      S_CLEAR_EXTERNAL (newsymP);
827
636
      newsymP->x->previous = newsymP->x->next = newsymP;
828
636
    }
829
830
5.27k
  return newsymP;
831
5.27k
}
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
484k
{
841
484k
  if (symbolP
842
101k
      && !symbolP->flags.local_symbol
843
100k
      && !symbolP->flags.forward_resolved)
844
9.63k
    {
845
9.63k
      symbolS *orig_add_symbol = symbolP->x->value.X_add_symbol;
846
9.63k
      symbolS *orig_op_symbol = symbolP->x->value.X_op_symbol;
847
9.63k
      symbolS *add_symbol = orig_add_symbol;
848
9.63k
      symbolS *op_symbol = orig_op_symbol;
849
850
9.63k
      if (symbolP->flags.forward_ref)
851
0
  is_forward = 1;
852
853
9.63k
      if (is_forward)
854
0
  {
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
0
    if (add_symbol && S_IS_VOLATILE (add_symbol))
859
0
      add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
860
0
    if (op_symbol && S_IS_VOLATILE (op_symbol))
861
0
      op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
862
0
  }
863
864
      /* Re-using resolving here, as this routine cannot get called from
865
   symbol resolution code.  */
866
9.63k
      if ((symbolP->bsym->section == expr_section
867
6.50k
     || symbolP->flags.forward_ref)
868
3.13k
    && !symbolP->flags.resolving)
869
3.13k
  {
870
3.13k
    symbolP->flags.resolving = 1;
871
3.13k
    add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
872
3.13k
    op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
873
3.13k
    symbolP->flags.resolving = 0;
874
3.13k
  }
875
876
9.63k
      if (symbolP->flags.forward_ref
877
9.63k
    || add_symbol != orig_add_symbol
878
9.63k
    || op_symbol != orig_op_symbol)
879
0
  {
880
0
    if (symbolP != &dot_symbol)
881
0
      {
882
0
        symbolP = symbol_clone (symbolP, 0);
883
0
        symbolP->flags.resolving = 0;
884
0
      }
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
0
  }
893
894
9.63k
      symbolP->x->value.X_add_symbol = add_symbol;
895
9.63k
      symbolP->x->value.X_op_symbol = op_symbol;
896
9.63k
      symbolP->flags.forward_resolved = 1;
897
9.63k
    }
898
899
484k
  return symbolP;
900
484k
}
901
902
symbolS *
903
symbol_temp_new (segT seg, fragS *frag, valueT ofs)
904
7.04k
{
905
7.04k
  return symbol_new (FAKE_LABEL_NAME, seg, frag, ofs);
906
7.04k
}
907
908
symbolS *
909
symbol_temp_new_now (void)
910
3.90k
{
911
3.90k
  return symbol_temp_new (now_seg, frag_now, frag_now_fix ());
912
3.90k
}
913
914
symbolS *
915
symbol_temp_new_now_octets (void)
916
190
{
917
190
  return symbol_temp_new (now_seg, frag_now, frag_now_fix_octets ());
918
190
}
919
920
symbolS *
921
symbol_temp_make (void)
922
444
{
923
444
  return symbol_make (FAKE_LABEL_NAME);
924
444
}
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
95.6k
{
940
95.6k
  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
95.6k
  if (sym && ! noref)
948
84.6k
    S_CLEAR_WEAKREFD (sym);
949
950
95.6k
  return sym;
951
95.6k
}
952
953
symbolS *
954
symbol_find (const char *name)
955
95.6k
{
956
95.6k
  return symbol_find_noref (name, 0);
957
95.6k
}
958
959
symbolS *
960
symbol_find_noref (const char *name, int noref)
961
95.6k
{
962
95.6k
  symbolS * result;
963
95.6k
  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
95.6k
  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
95.6k
  result = symbol_find_exact_noref (name, noref);
992
95.6k
  free (copy);
993
95.6k
  return result;
994
95.6k
}
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
10.5k
{
1008
10.5k
  extern int symbol_table_frozen;
1009
10.5k
  if (symbol_table_frozen)
1010
0
    abort ();
1011
10.5k
  if (addme->flags.local_symbol)
1012
0
    abort ();
1013
10.5k
  if (target != NULL && target->flags.local_symbol)
1014
0
    abort ();
1015
1016
10.5k
  if (target == NULL)
1017
368
    {
1018
368
      know (*rootPP == NULL);
1019
368
      know (*lastPP == NULL);
1020
368
      addme->x->next = NULL;
1021
368
      addme->x->previous = NULL;
1022
368
      *rootPP = addme;
1023
368
      *lastPP = addme;
1024
368
      return;
1025
368
    }
1026
1027
10.1k
  if (target->x->next != NULL)
1028
0
    target->x->next->x->previous = addme;
1029
10.1k
  else
1030
10.1k
    *lastPP = addme;
1031
1032
10.1k
  addme->x->next = target->x->next;
1033
10.1k
  target->x->next = addme;
1034
10.1k
  addme->x->previous = target;
1035
1036
10.1k
  debug_verify_symchain (symbol_rootP, symbol_lastP);
1037
10.1k
}
1038
1039
/* Set the chain pointers of SYMBOL to null.  */
1040
1041
void
1042
symbol_clear_list_pointers (symbolS *symbolP)
1043
65.3k
{
1044
65.3k
  if (symbolP->flags.local_symbol)
1045
0
    abort ();
1046
65.3k
  symbolP->x->next = NULL;
1047
65.3k
  symbolP->x->previous = NULL;
1048
65.3k
}
1049
1050
/* Remove SYMBOLP from the list.  */
1051
1052
void
1053
symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
1054
30
{
1055
30
  if (symbolP->flags.local_symbol)
1056
0
    abort ();
1057
1058
30
  if (symbolP->x->previous != NULL)
1059
30
    symbolP->x->previous->x->next = symbolP->x->next;
1060
0
  else
1061
0
    *rootPP = symbolP->x->next;
1062
1063
30
  if (symbolP->x->next != NULL)
1064
0
    symbolP->x->next->x->previous = symbolP->x->previous;
1065
30
  else
1066
30
    *lastPP = symbolP->x->previous;
1067
1068
30
  debug_verify_symchain (*rootPP, *lastPP);
1069
30
}
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
30
{
1077
30
  extern int symbol_table_frozen;
1078
30
  if (symbol_table_frozen)
1079
0
    abort ();
1080
30
  if (addme->flags.local_symbol)
1081
0
    abort ();
1082
30
  if (target->flags.local_symbol)
1083
0
    abort ();
1084
1085
30
  if (target->x->previous != NULL)
1086
0
    target->x->previous->x->next = addme;
1087
30
  else
1088
30
    *rootPP = addme;
1089
1090
30
  addme->x->previous = target->x->previous;
1091
30
  target->x->previous = addme;
1092
30
  addme->x->next = target;
1093
1094
30
  debug_verify_symchain (*rootPP, *lastPP);
1095
30
}
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
34.9k
{
1253
34.9k
  int resolved;
1254
34.9k
  valueT final_val;
1255
34.9k
  segT final_seg;
1256
1257
34.9k
  if (symp->flags.local_symbol)
1258
20
    {
1259
20
      struct local_symbol *locsym = (struct local_symbol *) symp;
1260
1261
20
      final_val = locsym->value;
1262
20
      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
20
      if (locsym->section->flags & SEC_OCTETS)
1268
0
  final_val += locsym->frag->fr_address;
1269
20
      else
1270
20
  final_val += locsym->frag->fr_address / OCTETS_PER_BYTE;
1271
1272
20
      if (finalize_syms)
1273
0
  {
1274
0
    locsym->value = final_val;
1275
0
    locsym->flags.resolved = 1;
1276
0
  }
1277
1278
20
      return final_val;
1279
20
    }
1280
1281
34.8k
  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
34.8k
  resolved = 0;
1305
34.8k
  final_seg = S_GET_SEGMENT (symp);
1306
1307
34.8k
  if (symp->flags.resolving)
1308
284
    {
1309
284
      if (finalize_syms)
1310
0
  as_bad (_("symbol definition loop encountered at `%s'"),
1311
0
    S_GET_NAME (symp));
1312
284
      final_val = 0;
1313
284
      resolved = 1;
1314
284
    }
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
34.6k
  else
1364
34.6k
    {
1365
34.6k
      symbolS *add_symbol, *op_symbol;
1366
34.6k
      offsetT left, right;
1367
34.6k
      segT seg_left, seg_right;
1368
34.6k
      operatorT op;
1369
34.6k
      int move_seg_ok;
1370
1371
34.6k
      symp->flags.resolving = 1;
1372
1373
      /* Help out with CSE.  */
1374
34.6k
      add_symbol = symp->x->value.X_add_symbol;
1375
34.6k
      op_symbol = symp->x->value.X_op_symbol;
1376
34.6k
      final_val = symp->x->value.X_add_number;
1377
34.6k
      op = symp->x->value.X_op;
1378
1379
34.6k
      switch (op)
1380
34.6k
  {
1381
0
  default:
1382
0
    BAD_CASE (op);
1383
0
    break;
1384
1385
0
  case O_md1:
1386
0
  case O_md2:
1387
0
  case O_md3:
1388
0
  case O_md4:
1389
0
  case O_md5:
1390
0
  case O_md6:
1391
0
  case O_md7:
1392
0
  case O_md8:
1393
0
  case O_md9:
1394
0
  case O_md10:
1395
0
  case O_md11:
1396
0
  case O_md12:
1397
0
  case O_md13:
1398
0
  case O_md14:
1399
0
  case O_md15:
1400
0
  case O_md16:
1401
0
  case O_md17:
1402
0
  case O_md18:
1403
0
  case O_md19:
1404
0
  case O_md20:
1405
0
  case O_md21:
1406
0
  case O_md22:
1407
0
  case O_md23:
1408
0
  case O_md24:
1409
0
  case O_md25:
1410
0
  case O_md26:
1411
0
  case O_md27:
1412
0
  case O_md28:
1413
0
  case O_md29:
1414
0
  case O_md30:
1415
0
  case O_md31:
1416
0
  case O_md32:
1417
#ifdef md_resolve_symbol
1418
    resolved = md_resolve_symbol (symp, &final_val, &final_seg);
1419
    if (resolved)
1420
      break;
1421
#endif
1422
0
    goto exit_dont_set_value;
1423
1424
0
  case O_absent:
1425
0
    final_val = 0;
1426
    /* Fall through.  */
1427
1428
33.9k
  case O_constant:
1429
    /* Symbols whose section has SEC_ELF_OCTETS set,
1430
       resolve to octets instead of target bytes. */
1431
33.9k
    if (symp->bsym->section->flags & SEC_OCTETS)
1432
5
      final_val += symp->frag->fr_address;
1433
33.9k
    else
1434
33.9k
      final_val += symp->frag->fr_address / OCTETS_PER_BYTE;
1435
33.9k
    if (final_seg == expr_section)
1436
0
      final_seg = absolute_section;
1437
    /* Fall through.  */
1438
1439
33.9k
  case O_register:
1440
33.9k
    resolved = 1;
1441
33.9k
    break;
1442
1443
319
  case O_symbol:
1444
319
  case O_symbol_rva:
1445
319
  case O_secidx:
1446
319
    left = resolve_symbol_value (add_symbol);
1447
319
    seg_left = S_GET_SEGMENT (add_symbol);
1448
319
    if (finalize_syms)
1449
0
      symp->x->value.X_op_symbol = NULL;
1450
1451
326
  do_symbol:
1452
326
    if (S_IS_WEAKREFR (symp))
1453
98
      {
1454
98
        gas_assert (final_val == 0);
1455
98
        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
98
      }
1464
1465
326
    if (symp->flags.mri_common)
1466
215
      {
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
215
        resolved = symbol_resolved_p (add_symbol);
1471
215
        break;
1472
215
      }
1473
1474
    /* Don't leave symbol loops.  */
1475
111
    if (finalize_syms
1476
0
        && !add_symbol->flags.local_symbol
1477
0
        && add_symbol->flags.resolving)
1478
0
      break;
1479
1480
111
    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
111
        )
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
111
    if (seg_left == undefined_section
1504
106
        || bfd_is_com_section (seg_left)
1505
#if defined (OBJ_COFF) && defined (TE_PE)
1506
        || S_IS_WEAK (add_symbol)
1507
#endif
1508
106
        || (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
5
      {
1514
5
        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
5
        final_seg = seg_left;
1523
5
        final_val += symp->frag->fr_address + left;
1524
5
        resolved = symbol_resolved_p (add_symbol);
1525
5
        symp->flags.resolving = 0;
1526
1527
5
        if (op == O_secidx && seg_left != undefined_section)
1528
0
    {
1529
0
      final_val = 0;
1530
0
      break;
1531
0
    }
1532
1533
5
        goto exit_dont_set_value;
1534
5
      }
1535
106
    else
1536
106
      {
1537
106
        final_val += symp->frag->fr_address + left;
1538
106
        if (final_seg == expr_section || final_seg == undefined_section)
1539
6
    final_seg = seg_left;
1540
106
      }
1541
1542
106
    resolved = symbol_resolved_p (add_symbol);
1543
106
    if (S_IS_WEAKREFR (symp))
1544
94
      {
1545
94
        symp->flags.resolving = 0;
1546
94
        goto exit_dont_set_value;
1547
94
      }
1548
12
    break;
1549
1550
12
  case O_uminus:
1551
0
  case O_bit_not:
1552
0
  case O_logical_not:
1553
0
    left = resolve_symbol_value (add_symbol);
1554
0
    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
0
    if (op != O_logical_not && seg_left != absolute_section
1561
0
        && finalize_syms)
1562
0
      report_op_error (symp, NULL, op, add_symbol);
1563
1564
0
    if (final_seg == expr_section || final_seg == undefined_section)
1565
0
      final_seg = absolute_section;
1566
1567
0
    if (op == O_uminus)
1568
0
      left = -(valueT) left;
1569
0
    else if (op == O_logical_not)
1570
0
      left = !left;
1571
0
    else
1572
0
      left = ~left;
1573
1574
0
    final_val += left + symp->frag->fr_address;
1575
1576
0
    resolved = symbol_resolved_p (add_symbol);
1577
0
    break;
1578
1579
1
  case O_multiply:
1580
5
  case O_divide:
1581
11
  case O_modulus:
1582
11
  case O_left_shift:
1583
18
  case O_right_shift:
1584
18
  case O_bit_inclusive_or:
1585
18
  case O_bit_or_not:
1586
18
  case O_bit_exclusive_or:
1587
18
  case O_bit_and:
1588
22
  case O_add:
1589
138
  case O_subtract:
1590
162
  case O_eq:
1591
162
  case O_ne:
1592
162
  case O_lt:
1593
208
  case O_le:
1594
208
  case O_ge:
1595
301
  case O_gt:
1596
309
  case O_logical_and:
1597
309
  case O_logical_or:
1598
309
    left = resolve_symbol_value (add_symbol);
1599
309
    right = resolve_symbol_value (op_symbol);
1600
309
    seg_left = S_GET_SEGMENT (add_symbol);
1601
309
    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
309
    if (op == O_add)
1606
4
      {
1607
4
        if (seg_right == absolute_section)
1608
4
    {
1609
4
      final_val += right;
1610
4
      goto do_symbol;
1611
4
    }
1612
0
        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
4
      }
1621
305
    else if (op == O_subtract)
1622
116
      {
1623
116
        if (seg_right == absolute_section)
1624
3
    {
1625
3
      final_val -= right;
1626
3
      goto do_symbol;
1627
3
    }
1628
116
      }
1629
1630
302
    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
302
    if (!(seg_left == absolute_section
1639
37
    && seg_right == absolute_section)
1640
297
        && !(op == O_eq || op == O_ne)
1641
278
        && !((op == O_subtract
1642
165
        || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1643
252
       && seg_left == seg_right
1644
78
       && (seg_left != undefined_section
1645
78
           || add_symbol == op_symbol)))
1646
278
      {
1647
        /* Don't emit messages unless we're finalizing the symbol value,
1648
     otherwise we may get the same message multiple times.  */
1649
278
        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
278
        else
1656
278
    move_seg_ok = 0;
1657
278
      }
1658
1659
302
    if (move_seg_ok
1660
24
        && (final_seg == expr_section || final_seg == undefined_section))
1661
8
      final_seg = absolute_section;
1662
1663
    /* Check for division by zero.  */
1664
302
    if ((op == O_divide || op == O_modulus) && right == 0)
1665
3
      {
1666
        /* If seg_right is not absolute_section, then we've
1667
     already issued a warning about using a bad symbol.  */
1668
3
        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
3
        right = 1;
1681
3
      }
1682
302
    if ((op == O_left_shift || op == O_right_shift)
1683
7
        && (valueT) right >= sizeof (valueT) * CHAR_BIT)
1684
2
      {
1685
2
        as_warn_value_out_of_range (_("shift count"), right, 0,
1686
2
            sizeof (valueT) * CHAR_BIT - 1,
1687
2
            NULL, 0);
1688
2
        left = right = 0;
1689
2
      }
1690
1691
302
    switch (symp->x->value.X_op)
1692
302
      {
1693
      /* See expr() for reasons of the use of valueT casts here.  */
1694
1
      case O_multiply:    left *= (valueT) right; break;
1695
1696
      /* See expr() for reasons of the special casing.  */
1697
4
      case O_divide:
1698
4
        if (right == 1)
1699
1
    break;
1700
3
        if (right == -1)
1701
0
    {
1702
0
      left = -(valueT) left;
1703
0
      break;
1704
0
    }
1705
3
        left /= right;
1706
3
        break;
1707
1708
      /* Again, see expr() for reasons of the special casing.  */
1709
6
      case O_modulus:
1710
6
        if (right == 1 || right == -1)
1711
5
    left = 0;
1712
1
        else
1713
1
    left %= right;
1714
6
        break;
1715
1716
0
      case O_left_shift:
1717
0
        left = (valueT) left << (valueT) right; break;
1718
7
      case O_right_shift:
1719
7
        left = (valueT) left >> (valueT) right; break;
1720
0
      case O_bit_inclusive_or:  left |= right; break;
1721
0
      case O_bit_or_not:    left |= ~right; break;
1722
0
      case O_bit_exclusive_or:  left ^= right; break;
1723
0
      case O_bit_and:   left &= right; break;
1724
0
      case O_add:     left += (valueT) right; break;
1725
113
      case O_subtract:    left -= (valueT) right; break;
1726
24
      case O_eq:
1727
24
      case O_ne:
1728
24
        left = (left == right && seg_left == seg_right
1729
0
          && (seg_left != undefined_section
1730
0
        || add_symbol == op_symbol)
1731
24
          ? ~ (offsetT) 0 : 0);
1732
24
        if (symp->x->value.X_op == O_ne)
1733
0
    left = ~left;
1734
24
        break;
1735
0
      case O_lt:  left = left <  right ? ~ (offsetT) 0 : 0; break;
1736
46
      case O_le:  left = left <= right ? ~ (offsetT) 0 : 0; break;
1737
0
      case O_ge:  left = left >= right ? ~ (offsetT) 0 : 0; break;
1738
93
      case O_gt:  left = left >  right ? ~ (offsetT) 0 : 0; break;
1739
8
      case O_logical_and: left = left && right; break;
1740
0
      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
302
      }
1752
1753
302
    final_val += symp->frag->fr_address + left;
1754
302
    if (final_seg == expr_section || final_seg == undefined_section)
1755
278
      {
1756
278
        if (seg_left == undefined_section
1757
65
      || seg_right == undefined_section)
1758
249
    final_seg = undefined_section;
1759
29
        else if (seg_left == absolute_section)
1760
4
    final_seg = seg_right;
1761
25
        else
1762
25
    final_seg = seg_left;
1763
278
      }
1764
302
    resolved = (symbol_resolved_p (add_symbol)
1765
0
          && symbol_resolved_p (op_symbol));
1766
302
    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
34.6k
  }
1777
1778
34.5k
      symp->flags.resolving = 0;
1779
34.5k
    }
1780
1781
34.7k
  if (finalize_syms)
1782
0
    S_SET_VALUE (symp, final_val);
1783
1784
34.8k
 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
34.8k
    S_SET_SEGMENT (symp, final_seg);
1788
1789
  /* Don't worry if we can't resolve an expr_section symbol.  */
1790
34.8k
  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
34.8k
  return final_val;
1803
34.7k
}
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
59.9k
{
1831
59.9k
  symbolS *symbolP = *symbolPP;
1832
1833
59.9k
  if (symbolP->flags.local_symbol)
1834
199
    {
1835
199
      struct local_symbol *locsym = (struct local_symbol *) symbolP;
1836
1837
199
      *valueP = locsym->value;
1838
199
      *segP = locsym->section;
1839
199
      *fragPP = locsym->frag;
1840
199
    }
1841
59.7k
  else
1842
59.7k
    {
1843
59.7k
      expressionS exp = symbolP->x->value;
1844
1845
59.7k
      if (!symbolP->flags.resolved && exp.X_op != O_illegal)
1846
59.7k
  {
1847
59.7k
    int resolved;
1848
1849
59.7k
    if (symbolP->flags.resolving)
1850
6
      return 0;
1851
59.7k
    symbolP->flags.resolving = 1;
1852
59.7k
    resolved = resolve_expression (&exp);
1853
59.7k
    symbolP->flags.resolving = 0;
1854
59.7k
    if (!resolved)
1855
11.9k
      return 0;
1856
1857
47.7k
    switch (exp.X_op)
1858
47.7k
      {
1859
46.1k
      case O_constant:
1860
46.3k
      case O_register:
1861
46.3k
        if (!symbol_equated_p (symbolP))
1862
46.3k
    break;
1863
        /* Fallthru.  */
1864
1.42k
      case O_symbol:
1865
1.42k
      case O_symbol_rva:
1866
1.42k
        symbolP = exp.X_add_symbol;
1867
1.42k
        break;
1868
0
      default:
1869
0
        return 0;
1870
47.7k
      }
1871
47.7k
  }
1872
1873
47.7k
      *symbolPP = symbolP;
1874
47.7k
      *valueP = exp.X_add_number;
1875
1876
      /* We may have picked up a local symbol above: Check again.  */
1877
47.7k
      if (symbolP->flags.local_symbol)
1878
1
  {
1879
1
    struct local_symbol *locsym = (struct local_symbol *) symbolP;
1880
1881
1
    if (locsym->section == expr_section
1882
1
        || locsym->section == absolute_section
1883
1
        || locsym->section == reg_section)
1884
0
      {
1885
0
        switch (exp.X_op)
1886
0
    {
1887
0
    case O_constant:
1888
0
    case O_register:
1889
0
      *valueP += locsym->value;
1890
0
      break;
1891
0
    default:
1892
0
      break;
1893
0
    }
1894
0
      }
1895
1
    *segP = locsym->section;
1896
1
    *fragPP = locsym->frag;
1897
1
  }
1898
47.7k
      else
1899
47.7k
  {
1900
47.7k
    *segP = symbolP->bsym->section;
1901
47.7k
    *fragPP = symbolP->frag;
1902
47.7k
  }
1903
1904
47.7k
      if (*segP == expr_section)
1905
435
  switch (exp.X_op)
1906
435
    {
1907
435
    case O_constant: *segP = absolute_section; break;
1908
0
    case O_register: *segP = reg_section; break;
1909
0
    default: break;
1910
435
    }
1911
47.7k
    }
1912
1913
47.9k
  return 1;
1914
59.9k
}
1915
1916
/* Dollar labels look like a number followed by a dollar sign.  Eg, "42$".
1917
   They are *really* local.  That is, they go out of scope whenever we see a
1918
   label that isn't local.  Also, like fb labels, there can be multiple
1919
   instances of a dollar label.  Therefor, we name encode each instance with
1920
   the instance number, keep a list of defined symbols separate from the real
1921
   symbol table, and we treat these buggers as a sparse array.  */
1922
1923
typedef unsigned int dollar_ent;
1924
static dollar_ent *dollar_labels;
1925
static dollar_ent *dollar_label_instances;
1926
static char *dollar_label_defines;
1927
static size_t dollar_label_count;
1928
static size_t dollar_label_max;
1929
1930
int
1931
dollar_label_defined (unsigned int label)
1932
0
{
1933
0
  dollar_ent *i;
1934
1935
0
  know ((dollar_labels != NULL) || (dollar_label_count == 0));
1936
1937
0
  for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1938
0
    if (*i == label)
1939
0
      return dollar_label_defines[i - dollar_labels];
1940
1941
  /* If we get here, label isn't defined.  */
1942
0
  return 0;
1943
0
}
1944
1945
static unsigned int
1946
dollar_label_instance (unsigned int label)
1947
0
{
1948
0
  dollar_ent *i;
1949
1950
0
  know ((dollar_labels != NULL) || (dollar_label_count == 0));
1951
1952
0
  for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1953
0
    if (*i == label)
1954
0
      return (dollar_label_instances[i - dollar_labels]);
1955
1956
  /* If we get here, we haven't seen the label before.
1957
     Therefore its instance count is zero.  */
1958
0
  return 0;
1959
0
}
1960
1961
void
1962
dollar_label_clear (void)
1963
0
{
1964
0
  if (dollar_label_count)
1965
0
    memset (dollar_label_defines, '\0', dollar_label_count);
1966
0
}
1967
1968
0
#define DOLLAR_LABEL_BUMP_BY 10
1969
1970
void
1971
define_dollar_label (unsigned int label)
1972
0
{
1973
0
  dollar_ent *i;
1974
1975
0
  for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1976
0
    if (*i == label)
1977
0
      {
1978
0
  ++dollar_label_instances[i - dollar_labels];
1979
0
  dollar_label_defines[i - dollar_labels] = 1;
1980
0
  return;
1981
0
      }
1982
1983
  /* If we get to here, we don't have label listed yet.  */
1984
1985
0
  if (dollar_labels == NULL)
1986
0
    {
1987
0
      dollar_labels = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1988
0
      dollar_label_instances = XNEWVEC (dollar_ent, DOLLAR_LABEL_BUMP_BY);
1989
0
      dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
1990
0
      dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1991
0
      dollar_label_count = 0;
1992
0
    }
1993
0
  else if (dollar_label_count == dollar_label_max)
1994
0
    {
1995
0
      dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1996
0
      dollar_labels = XRESIZEVEC (dollar_ent, dollar_labels,
1997
0
          dollar_label_max);
1998
0
      dollar_label_instances = XRESIZEVEC (dollar_ent,
1999
0
             dollar_label_instances,
2000
0
             dollar_label_max);
2001
0
      dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
2002
0
           dollar_label_max);
2003
0
    }       /* if we needed to grow  */
2004
2005
0
  dollar_labels[dollar_label_count] = label;
2006
0
  dollar_label_instances[dollar_label_count] = 1;
2007
0
  dollar_label_defines[dollar_label_count] = 1;
2008
0
  ++dollar_label_count;
2009
0
}
2010
2011
/* Caller must copy returned name: we re-use the area for the next name.
2012
2013
   The mth occurrence of label n: is turned into the symbol "Ln^Am"
2014
   where n is the label number and m is the instance number. "L" makes
2015
   it a label discarded unless debugging and "^A"('\1') ensures no
2016
   ordinary symbol SHOULD get the same name as a local label
2017
   symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
2018
2019
   fb labels get the same treatment, except that ^B is used in place
2020
   of ^A.
2021
2022
   AUGEND is 0 for current instance, 1 for new instance.  */
2023
2024
char *
2025
dollar_label_name (unsigned int n, unsigned int augend)
2026
0
{
2027
  /* Returned to caller, then copied.  Used for created names ("4f").  */
2028
0
  static char symbol_name_build[24];
2029
0
  char *p = symbol_name_build;
2030
2031
0
#ifdef LOCAL_LABEL_PREFIX
2032
0
  *p++ = LOCAL_LABEL_PREFIX;
2033
0
#endif
2034
0
  sprintf (p, "L%u%c%u",
2035
0
     n, DOLLAR_LABEL_CHAR, dollar_label_instance (n) + augend);
2036
0
  return symbol_name_build;
2037
0
}
2038
2039
/* Somebody else's idea of local labels. They are made by "n:" where n
2040
   is any decimal digit. Refer to them with
2041
    "nb" for previous (backward) n:
2042
   or "nf" for next (forward) n:.
2043
2044
   We do a little better and let n be any number, not just a single digit, but
2045
   since the other guy's assembler only does ten, we treat the first ten
2046
   specially.
2047
2048
   Like someone else's assembler, we have one set of local label counters for
2049
   entire assembly, not one set per (sub)segment like in most assemblers. This
2050
   implies that one can refer to a label in another segment, and indeed some
2051
   crufty compilers have done just that.
2052
2053
   Since there could be a LOT of these things, treat them as a sparse
2054
   array.  */
2055
2056
15.4k
#define FB_LABEL_SPECIAL (10)
2057
2058
typedef unsigned int fb_ent;
2059
static fb_ent fb_low_counter[FB_LABEL_SPECIAL];
2060
static fb_ent *fb_labels;
2061
static fb_ent *fb_label_instances;
2062
static size_t fb_label_count;
2063
static size_t fb_label_max;
2064
2065
/* This must be more than FB_LABEL_SPECIAL.  */
2066
38
#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
2067
2068
static void
2069
fb_label_init (void)
2070
368
{
2071
368
  memset (fb_low_counter, 0, sizeof (fb_low_counter));
2072
368
}
2073
2074
/* Add one to the instance number of this fb label.  */
2075
2076
void
2077
fb_label_instance_inc (unsigned int label)
2078
4.77k
{
2079
4.77k
  fb_ent *i;
2080
2081
4.77k
  if (label < FB_LABEL_SPECIAL)
2082
1.88k
    {
2083
1.88k
      ++fb_low_counter[label];
2084
1.88k
      return;
2085
1.88k
    }
2086
2087
2.89k
  if (fb_labels != NULL)
2088
2.89k
    {
2089
2.89k
      for (i = fb_labels + FB_LABEL_SPECIAL;
2090
405k
     i < fb_labels + fb_label_count; ++i)
2091
404k
  {
2092
404k
    if (*i == label)
2093
2.30k
      {
2094
2.30k
        ++fb_label_instances[i - fb_labels];
2095
2.30k
        return;
2096
2.30k
      }      /* if we find it  */
2097
404k
  }      /* for each existing label  */
2098
2.89k
    }
2099
2100
  /* If we get to here, we don't have label listed yet.  */
2101
2102
592
  if (fb_labels == NULL)
2103
1
    {
2104
1
      fb_labels = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
2105
1
      fb_label_instances = XNEWVEC (fb_ent, FB_LABEL_BUMP_BY);
2106
1
      fb_label_max = FB_LABEL_BUMP_BY;
2107
1
      fb_label_count = FB_LABEL_SPECIAL;
2108
2109
1
    }
2110
591
  else if (fb_label_count == fb_label_max)
2111
37
    {
2112
37
      fb_label_max += FB_LABEL_BUMP_BY;
2113
37
      fb_labels = XRESIZEVEC (fb_ent, fb_labels, fb_label_max);
2114
37
      fb_label_instances = XRESIZEVEC (fb_ent, fb_label_instances,
2115
37
               fb_label_max);
2116
37
    }        /* if we needed to grow  */
2117
2118
592
  fb_labels[fb_label_count] = label;
2119
592
  fb_label_instances[fb_label_count] = 1;
2120
592
  ++fb_label_count;
2121
592
}
2122
2123
static unsigned int
2124
fb_label_instance (unsigned int label)
2125
4.83k
{
2126
4.83k
  fb_ent *i;
2127
2128
4.83k
  if (label < FB_LABEL_SPECIAL)
2129
1.90k
    return (fb_low_counter[label]);
2130
2131
2.93k
  if (fb_labels != NULL)
2132
2.93k
    {
2133
2.93k
      for (i = fb_labels + FB_LABEL_SPECIAL;
2134
430k
     i < fb_labels + fb_label_count; ++i)
2135
430k
  {
2136
430k
    if (*i == label)
2137
2.89k
      return (fb_label_instances[i - fb_labels]);
2138
430k
  }
2139
2.93k
    }
2140
2141
  /* We didn't find the label, so this must be a reference to the
2142
     first instance.  */
2143
43
  return 0;
2144
2.93k
}
2145
2146
/* Caller must copy returned name: we re-use the area for the next name.
2147
2148
   The mth occurrence of label n: is turned into the symbol "Ln^Bm"
2149
   where n is the label number and m is the instance number. "L" makes
2150
   it a label discarded unless debugging and "^B"('\2') ensures no
2151
   ordinary symbol SHOULD get the same name as a local label
2152
   symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
2153
2154
   dollar labels get the same treatment, except that ^A is used in
2155
   place of ^B.
2156
2157
   AUGEND is 0 for nb, 1 for n:, nf.  */
2158
2159
char *
2160
fb_label_name (unsigned int n, unsigned int augend)
2161
4.83k
{
2162
  /* Returned to caller, then copied.  Used for created names ("4f").  */
2163
4.83k
  static char symbol_name_build[24];
2164
4.83k
  char *p = symbol_name_build;
2165
2166
#ifdef TC_MMIX
2167
  know (augend <= 2 /* See mmix_fb_label.  */);
2168
#else
2169
4.83k
  know (augend <= 1);
2170
4.83k
#endif
2171
2172
4.83k
#ifdef LOCAL_LABEL_PREFIX
2173
4.83k
  *p++ = LOCAL_LABEL_PREFIX;
2174
4.83k
#endif
2175
4.83k
  sprintf (p, "L%u%c%u",
2176
4.83k
     n, LOCAL_LABEL_CHAR, fb_label_instance (n) + augend);
2177
4.83k
  return symbol_name_build;
2178
4.83k
}
2179
2180
/* Decode name that may have been generated by foo_label_name() above.
2181
   If the name wasn't generated by foo_label_name(), then return it
2182
   unaltered.  This is used for error messages.  */
2183
2184
const char *
2185
decode_local_label_name (const char *s)
2186
0
{
2187
0
  const char *p;
2188
0
  char *symbol_decode;
2189
0
  unsigned int label_number;
2190
0
  unsigned int instance_number;
2191
0
  const char *type;
2192
0
  const char *message_format;
2193
0
  unsigned int lindex = 0;
2194
2195
0
#ifdef LOCAL_LABEL_PREFIX
2196
0
  if (s[lindex] == LOCAL_LABEL_PREFIX)
2197
0
    ++lindex;
2198
0
#endif
2199
2200
0
  if (s[lindex] != 'L')
2201
0
    return s;
2202
2203
0
  for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
2204
0
    label_number = (10 * label_number) + *p - '0';
2205
2206
0
  if (*p == DOLLAR_LABEL_CHAR)
2207
0
    type = "dollar";
2208
0
  else if (*p == LOCAL_LABEL_CHAR)
2209
0
    type = "fb";
2210
0
  else
2211
0
    return s;
2212
2213
0
  for (instance_number = 0, p++; ISDIGIT (*p); ++p)
2214
0
    instance_number = (10 * instance_number) + *p - '0';
2215
2216
0
  message_format = _("\"%u\" (instance number %u of a %s label)");
2217
0
  symbol_decode = notes_alloc (strlen (message_format) + 30);
2218
0
  sprintf (symbol_decode, message_format, label_number, instance_number, type);
2219
2220
0
  return symbol_decode;
2221
0
}
2222
2223
/* Get the value of a symbol.  */
2224
2225
valueT
2226
S_GET_VALUE_WHERE (symbolS *s, const char * file, unsigned int line)
2227
6.78k
{
2228
6.78k
  if (s->flags.local_symbol)
2229
19
    return resolve_symbol_value (s);
2230
2231
6.76k
  if (!s->flags.resolved)
2232
6.76k
    {
2233
6.76k
      valueT val = resolve_symbol_value (s);
2234
6.76k
      if (!finalize_syms)
2235
6.76k
  return val;
2236
6.76k
    }
2237
0
  if (S_IS_WEAKREFR (s))
2238
0
    return S_GET_VALUE (s->x->value.X_add_symbol);
2239
2240
0
  if (s->x->value.X_op != O_constant)
2241
0
    {
2242
0
      if (! s->flags.resolved
2243
0
    || s->x->value.X_op != O_symbol
2244
0
    || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
2245
0
  {
2246
0
    if (strcmp (S_GET_NAME (s), FAKE_LABEL_NAME) == 0)
2247
0
      as_bad_where (file, line, _("expression is too complex to be resolved or converted into relocations"));
2248
0
    else if (file != NULL)
2249
0
      as_bad_where (file, line, _("attempt to get value of unresolved symbol `%s'"),
2250
0
        S_GET_NAME (s));
2251
0
    else
2252
0
      as_bad (_("attempt to get value of unresolved symbol `%s'"),
2253
0
        S_GET_NAME (s));
2254
0
  }
2255
0
    }
2256
0
  return s->x->value.X_add_number;
2257
0
}
2258
2259
valueT
2260
S_GET_VALUE (symbolS *s)
2261
6.78k
{
2262
6.78k
  return S_GET_VALUE_WHERE (s, NULL, 0);
2263
6.78k
}
2264
2265
/* Set the value of a symbol.  */
2266
2267
void
2268
S_SET_VALUE (symbolS *s, valueT val)
2269
963k
{
2270
963k
  if (s->flags.local_symbol)
2271
1
    {
2272
1
      ((struct local_symbol *) s)->value = val;
2273
1
      return;
2274
1
    }
2275
2276
963k
  s->x->value.X_op = O_constant;
2277
963k
  s->x->value.X_add_number = (offsetT) val;
2278
963k
  s->x->value.X_unsigned = 0;
2279
963k
  S_CLEAR_WEAKREFR (s);
2280
963k
}
2281
2282
void
2283
copy_symbol_attributes (symbolS *dest, symbolS *src)
2284
1.56k
{
2285
1.56k
  if (dest->flags.local_symbol)
2286
0
    dest = local_symbol_convert (dest);
2287
1.56k
  if (src->flags.local_symbol)
2288
4
    src = local_symbol_convert (src);
2289
2290
  /* In an expression, transfer the settings of these flags.
2291
     The user can override later, of course.  */
2292
1.56k
#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2293
1.56k
       | BSF_GNU_INDIRECT_FUNCTION)
2294
1.56k
  dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2295
2296
1.56k
#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2297
1.56k
  OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2298
1.56k
#endif
2299
2300
#ifdef TC_COPY_SYMBOL_ATTRIBUTES
2301
  TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2302
#endif
2303
1.56k
}
2304
2305
int
2306
S_IS_FUNCTION (const symbolS *s)
2307
3.53k
{
2308
3.53k
  flagword flags;
2309
2310
3.53k
  if (s->flags.local_symbol)
2311
0
    return 0;
2312
2313
3.53k
  flags = s->bsym->flags;
2314
2315
3.53k
  return (flags & BSF_FUNCTION) != 0;
2316
3.53k
}
2317
2318
int
2319
S_IS_EXTERNAL (const symbolS *s)
2320
260
{
2321
260
  flagword flags;
2322
2323
260
  if (s->flags.local_symbol)
2324
0
    return 0;
2325
2326
260
  flags = s->bsym->flags;
2327
2328
  /* Sanity check.  */
2329
260
  if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2330
0
    abort ();
2331
2332
260
  return (flags & BSF_GLOBAL) != 0;
2333
260
}
2334
2335
int
2336
S_IS_WEAK (const symbolS *s)
2337
31
{
2338
31
  if (s->flags.local_symbol)
2339
0
    return 0;
2340
  /* Conceptually, a weakrefr is weak if the referenced symbol is.  We
2341
     could probably handle a WEAKREFR as always weak though.  E.g., if
2342
     the referenced symbol has lost its weak status, there's no reason
2343
     to keep handling the weakrefr as if it was weak.  */
2344
31
  if (S_IS_WEAKREFR (s))
2345
0
    return S_IS_WEAK (s->x->value.X_add_symbol);
2346
31
  return (s->bsym->flags & BSF_WEAK) != 0;
2347
31
}
2348
2349
int
2350
S_IS_WEAKREFR (const symbolS *s)
2351
192k
{
2352
192k
  if (s->flags.local_symbol)
2353
0
    return 0;
2354
192k
  return s->flags.weakrefr != 0;
2355
192k
}
2356
2357
int
2358
S_IS_WEAKREFD (const symbolS *s)
2359
0
{
2360
0
  if (s->flags.local_symbol)
2361
0
    return 0;
2362
0
  return s->flags.weakrefd != 0;
2363
0
}
2364
2365
int
2366
S_IS_COMMON (const symbolS *s)
2367
3.54k
{
2368
3.54k
  if (s->flags.local_symbol)
2369
1
    return 0;
2370
3.54k
  return bfd_is_com_section (s->bsym->section);
2371
3.54k
}
2372
2373
int
2374
S_IS_DEFINED (const symbolS *s)
2375
20.6k
{
2376
20.6k
  if (s->flags.local_symbol)
2377
11
    return ((const struct local_symbol *) s)->section != undefined_section;
2378
20.6k
  return s->bsym->section != undefined_section;
2379
20.6k
}
2380
2381
2382
#ifndef EXTERN_FORCE_RELOC
2383
#define EXTERN_FORCE_RELOC IS_ELF
2384
#endif
2385
2386
/* Return true for symbols that should not be reduced to section
2387
   symbols or eliminated from expressions, because they may be
2388
   overridden by the linker.  */
2389
int
2390
S_FORCE_RELOC (const symbolS *s, int strict)
2391
745
{
2392
745
  segT sec;
2393
745
  if (s->flags.local_symbol)
2394
10
    sec = ((const struct local_symbol *) s)->section;
2395
735
  else
2396
735
    {
2397
735
      if ((strict
2398
0
     && ((s->bsym->flags & BSF_WEAK) != 0
2399
0
         || (EXTERN_FORCE_RELOC
2400
0
       && (s->bsym->flags & BSF_GLOBAL) != 0)))
2401
735
    || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2402
0
  return true;
2403
735
      sec = s->bsym->section;
2404
735
    }
2405
745
  return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2406
745
}
2407
2408
int
2409
S_IS_DEBUG (const symbolS *s)
2410
219
{
2411
219
  if (s->flags.local_symbol)
2412
0
    return 0;
2413
219
  if (s->bsym->flags & BSF_DEBUGGING)
2414
0
    return 1;
2415
219
  return 0;
2416
219
}
2417
2418
int
2419
S_IS_LOCAL (const symbolS *s)
2420
14
{
2421
14
  flagword flags;
2422
14
  const char *name;
2423
2424
14
  if (s->flags.local_symbol)
2425
0
    return 1;
2426
2427
14
  if (S_IS_EXTERNAL (s))
2428
10
    return 0;
2429
2430
4
  if (bfd_asymbol_section (s->bsym) == reg_section)
2431
0
    return 1;
2432
2433
4
  flags = s->bsym->flags;
2434
2435
4
  if (flag_strip_local_absolute > 0
2436
      /* Keep BSF_FILE symbols in order to allow debuggers to identify
2437
   the source file even when the object file is stripped.  */
2438
0
      && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2439
0
      && bfd_asymbol_section (s->bsym) == absolute_section)
2440
0
    return 1;
2441
2442
4
  name = S_GET_NAME (s);
2443
4
  return (name != NULL
2444
4
    && ! S_IS_DEBUG (s)
2445
4
    && (strchr (name, DOLLAR_LABEL_CHAR)
2446
4
        || strchr (name, LOCAL_LABEL_CHAR)
2447
#if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2448
        || strchr (name, FAKE_LABEL_CHAR)
2449
#endif
2450
0
        || TC_LABEL_IS_LOCAL (name)
2451
4
        || (! flag_keep_locals
2452
4
      && (bfd_is_local_label (stdoutput, s->bsym)
2453
4
          || (flag_mri
2454
3
        && name[0] == '?'
2455
0
        && name[1] == '?')))));
2456
4
}
2457
2458
int
2459
S_IS_STABD (const symbolS *s)
2460
0
{
2461
0
  return S_GET_NAME (s) == 0;
2462
0
}
2463
2464
int
2465
S_CAN_BE_REDEFINED (const symbolS *s)
2466
5.76k
{
2467
5.76k
  if (s->flags.local_symbol)
2468
0
    return (((const struct local_symbol *) s)->frag
2469
0
      == &predefined_address_frag);
2470
  /* Permit register names to be redefined.  */
2471
5.76k
  return s->x->value.X_op == O_register;
2472
5.76k
}
2473
2474
int
2475
S_IS_VOLATILE (const symbolS *s)
2476
11.9k
{
2477
11.9k
  if (s->flags.local_symbol)
2478
0
    return 0;
2479
11.9k
  return s->flags.volatil;
2480
11.9k
}
2481
2482
int
2483
S_IS_FORWARD_REF (const symbolS *s)
2484
6.61k
{
2485
6.61k
  if (s->flags.local_symbol)
2486
0
    return 0;
2487
6.61k
  return s->flags.forward_ref;
2488
6.61k
}
2489
2490
const char *
2491
S_GET_NAME (const symbolS *s)
2492
1.14k
{
2493
1.14k
  return s->name;
2494
1.14k
}
2495
2496
segT
2497
S_GET_SEGMENT (const symbolS *s)
2498
228k
{
2499
228k
  if (s->flags.local_symbol)
2500
665
    return ((const struct local_symbol *) s)->section;
2501
228k
  return s->bsym->section;
2502
228k
}
2503
2504
void
2505
S_SET_SEGMENT (symbolS *s, segT seg)
2506
936k
{
2507
936k
  if (s->flags.local_symbol)
2508
0
    {
2509
0
      ((struct local_symbol *) s)->section = seg;
2510
0
      return;
2511
0
    }
2512
2513
  /* Don't reassign section symbols.  The direct reason is to prevent seg
2514
     faults assigning back to const global symbols such as *ABS*, but it
2515
     shouldn't happen anyway.  */
2516
936k
  if (s->bsym->flags & BSF_SECTION_SYM)
2517
12
    {
2518
12
      if (s->bsym->section != seg)
2519
0
  abort ();
2520
12
    }
2521
936k
  else
2522
936k
    {
2523
936k
      if (multibyte_handling == multibyte_warn_syms
2524
0
    && ! s->flags.local_symbol
2525
0
    && seg != undefined_section
2526
0
    && ! s->flags.multibyte_warned
2527
0
    && scan_for_multibyte_characters ((const unsigned char *) s->name,
2528
0
              (const unsigned char *) s->name + strlen (s->name),
2529
0
              false))
2530
0
  {
2531
0
    as_warn (_("symbol '%s' contains multibyte characters"), s->name);
2532
0
    s->flags.multibyte_warned = 1;
2533
0
  }
2534
2535
936k
      s->bsym->section = seg;
2536
936k
    }
2537
936k
}
2538
2539
void
2540
S_SET_EXTERNAL (symbolS *s)
2541
883
{
2542
883
  if (s->flags.local_symbol)
2543
5
    s = local_symbol_convert (s);
2544
883
  if ((s->bsym->flags & BSF_WEAK) != 0)
2545
0
    {
2546
      /* Let .weak override .global.  */
2547
0
      return;
2548
0
    }
2549
883
  if (s->bsym->flags & BSF_SECTION_SYM)
2550
0
    {
2551
      /* Do not reassign section symbols.  */
2552
0
      as_warn (_("can't make section symbol global"));
2553
0
      return;
2554
0
    }
2555
883
#ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2556
883
  if (S_GET_SEGMENT (s) == reg_section)
2557
0
    {
2558
0
      as_bad (_("can't make register symbol global"));
2559
0
      return;
2560
0
    }
2561
883
#endif
2562
883
  s->bsym->flags |= BSF_GLOBAL;
2563
883
  s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2564
2565
#ifdef TE_PE
2566
  if (! an_external_name && S_GET_NAME(s)[0] != '.')
2567
    an_external_name = S_GET_NAME (s);
2568
#endif
2569
883
}
2570
2571
void
2572
S_CLEAR_EXTERNAL (symbolS *s)
2573
7.27k
{
2574
7.27k
  if (s->flags.local_symbol)
2575
0
    return;
2576
7.27k
  if ((s->bsym->flags & BSF_WEAK) != 0)
2577
37
    {
2578
      /* Let .weak override.  */
2579
37
      return;
2580
37
    }
2581
7.24k
  s->bsym->flags |= BSF_LOCAL;
2582
7.24k
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2583
7.24k
}
2584
2585
void
2586
S_SET_WEAK (symbolS *s)
2587
23
{
2588
23
  if (s->flags.local_symbol)
2589
0
    s = local_symbol_convert (s);
2590
#ifdef obj_set_weak_hook
2591
  obj_set_weak_hook (s);
2592
#endif
2593
23
  s->bsym->flags |= BSF_WEAK;
2594
23
  s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2595
23
}
2596
2597
void
2598
S_SET_WEAKREFR (symbolS *s)
2599
8
{
2600
8
  if (s->flags.local_symbol)
2601
0
    s = local_symbol_convert (s);
2602
8
  s->flags.weakrefr = 1;
2603
  /* If the alias was already used, make sure we mark the target as
2604
     used as well, otherwise it might be dropped from the symbol
2605
     table.  This may have unintended side effects if the alias is
2606
     later redirected to another symbol, such as keeping the unused
2607
     previous target in the symbol table.  Since it will be weak, it's
2608
     not a big deal.  */
2609
8
  if (s->flags.used)
2610
2
    symbol_mark_used (s->x->value.X_add_symbol);
2611
8
}
2612
2613
void
2614
S_CLEAR_WEAKREFR (symbolS *s)
2615
1.93M
{
2616
1.93M
  if (s->flags.local_symbol)
2617
1.41k
    return;
2618
1.92M
  s->flags.weakrefr = 0;
2619
1.92M
}
2620
2621
void
2622
S_SET_WEAKREFD (symbolS *s)
2623
5
{
2624
5
  if (s->flags.local_symbol)
2625
0
    s = local_symbol_convert (s);
2626
5
  s->flags.weakrefd = 1;
2627
5
  S_SET_WEAK (s);
2628
5
}
2629
2630
void
2631
S_CLEAR_WEAKREFD (symbolS *s)
2632
84.6k
{
2633
84.6k
  if (s->flags.local_symbol)
2634
1.67k
    return;
2635
82.9k
  if (s->flags.weakrefd)
2636
3
    {
2637
3
      s->flags.weakrefd = 0;
2638
      /* If a weakref target symbol is weak, then it was never
2639
   referenced directly before, not even in a .global directive,
2640
   so decay it to local.  If it remains undefined, it will be
2641
   later turned into a global, like any other undefined
2642
   symbol.  */
2643
3
      if (s->bsym->flags & BSF_WEAK)
2644
3
  {
2645
#ifdef obj_clear_weak_hook
2646
    obj_clear_weak_hook (s);
2647
#endif
2648
3
    s->bsym->flags &= ~BSF_WEAK;
2649
3
    s->bsym->flags |= BSF_LOCAL;
2650
3
  }
2651
3
    }
2652
82.9k
}
2653
2654
void
2655
S_SET_THREAD_LOCAL (symbolS *s)
2656
0
{
2657
0
  if (s->flags.local_symbol)
2658
0
    s = local_symbol_convert (s);
2659
0
  if (bfd_is_com_section (s->bsym->section)
2660
0
      && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2661
0
    return;
2662
0
  s->bsym->flags |= BSF_THREAD_LOCAL;
2663
0
  if ((s->bsym->flags & BSF_FUNCTION) != 0)
2664
0
    as_bad (_("Accessing function `%s' as thread-local object"),
2665
0
      S_GET_NAME (s));
2666
0
  else if (! bfd_is_und_section (s->bsym->section)
2667
0
     && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2668
0
    as_bad (_("Accessing `%s' as thread-local object"),
2669
0
      S_GET_NAME (s));
2670
0
}
2671
2672
void
2673
S_SET_NAME (symbolS *s, const char *name)
2674
0
{
2675
0
  s->name = name;
2676
0
  if (s->flags.local_symbol)
2677
0
    return;
2678
0
  s->bsym->name = name;
2679
0
}
2680
2681
void
2682
S_SET_VOLATILE (symbolS *s)
2683
4.91k
{
2684
4.91k
  if (s->flags.local_symbol)
2685
4
    s = local_symbol_convert (s);
2686
4.91k
  s->flags.volatil = 1;
2687
4.91k
}
2688
2689
void
2690
S_CLEAR_VOLATILE (symbolS *s)
2691
5
{
2692
5
  if (!s->flags.local_symbol)
2693
5
    s->flags.volatil = 0;
2694
5
}
2695
2696
void
2697
S_SET_FORWARD_REF (symbolS *s)
2698
29
{
2699
29
  if (s->flags.local_symbol)
2700
1
    s = local_symbol_convert (s);
2701
29
  s->flags.forward_ref = 1;
2702
29
}
2703
2704
/* Return the previous symbol in a chain.  */
2705
2706
symbolS *
2707
symbol_previous (const symbolS *s)
2708
0
{
2709
0
  if (s->flags.local_symbol)
2710
0
    abort ();
2711
0
  return s->x->previous;
2712
0
}
2713
2714
/* Return the next symbol in a chain.  */
2715
2716
symbolS *
2717
symbol_next (const symbolS *s)
2718
3.41k
{
2719
3.41k
  if (s->flags.local_symbol)
2720
0
    abort ();
2721
3.41k
  return s->x->next;
2722
3.41k
}
2723
2724
/* Return a pointer to the value of a symbol as an expression.  */
2725
2726
expressionS *
2727
symbol_get_value_expression (symbolS *s)
2728
30.7k
{
2729
30.7k
  if (s->flags.local_symbol)
2730
0
    s = local_symbol_convert (s);
2731
30.7k
  return &s->x->value;
2732
30.7k
}
2733
2734
/* Set the value of a symbol to an expression.  */
2735
2736
void
2737
symbol_set_value_expression (symbolS *s, const expressionS *exp)
2738
57.5k
{
2739
57.5k
  if (s->flags.local_symbol)
2740
0
    s = local_symbol_convert (s);
2741
57.5k
  s->x->value = *exp;
2742
57.5k
  S_CLEAR_WEAKREFR (s);
2743
57.5k
}
2744
2745
/* Return whether 2 symbols are the same.  */
2746
2747
int
2748
symbol_same_p (const symbolS *s1, const symbolS *s2)
2749
13.9k
{
2750
13.9k
  return s1 == s2;
2751
13.9k
}
2752
2753
/* Return a pointer to the X_add_number component of a symbol.  */
2754
2755
offsetT *
2756
symbol_X_add_number (const symbolS *s)
2757
2
{
2758
2
  if (s->flags.local_symbol)
2759
0
    return (offsetT *) &((struct local_symbol *) s)->value;
2760
2761
2
  return &s->x->value.X_add_number;
2762
2
}
2763
2764
/* Set the value of SYM to the current position in the current segment.  */
2765
2766
void
2767
symbol_set_value_now (symbolS *sym)
2768
895k
{
2769
895k
  S_SET_SEGMENT (sym, now_seg);
2770
895k
  S_SET_VALUE (sym, frag_now_fix ());
2771
895k
  symbol_set_frag (sym, frag_now);
2772
895k
}
2773
2774
/* Set the frag of a symbol.  */
2775
2776
void
2777
symbol_set_frag (symbolS *s, fragS *f)
2778
900k
{
2779
900k
  if (s->flags.local_symbol)
2780
0
    {
2781
0
      ((struct local_symbol *) s)->frag = f;
2782
0
      return;
2783
0
    }
2784
900k
  s->frag = f;
2785
900k
  S_CLEAR_WEAKREFR (s);
2786
900k
}
2787
2788
/* Return the frag of a symbol.  */
2789
2790
fragS *
2791
symbol_get_frag (const symbolS *s)
2792
9.75k
{
2793
9.75k
  if (s->flags.local_symbol)
2794
19
    return ((struct local_symbol *) s)->frag;
2795
9.73k
  return s->frag;
2796
9.75k
}
2797
2798
/* Return the frag of a symbol and the symbol's offset into that frag.  */
2799
2800
fragS *symbol_get_frag_and_value (const symbolS *s, addressT *value)
2801
5.45k
{
2802
5.45k
  if (s->flags.local_symbol)
2803
0
    {
2804
0
      const struct local_symbol *locsym = (const struct local_symbol *) s;
2805
2806
0
      *value = locsym->value;
2807
0
      return locsym->frag;
2808
0
    }
2809
2810
5.45k
  gas_assert (s->x->value.X_op == O_constant);
2811
5.45k
  *value = s->x->value.X_add_number;
2812
5.45k
  return s->frag;
2813
5.45k
}
2814
2815
/* Mark a symbol as having been used.  */
2816
2817
void
2818
symbol_mark_used (symbolS *s)
2819
192k
{
2820
192k
  if (s->flags.local_symbol)
2821
607
    return;
2822
191k
  s->flags.used = 1;
2823
191k
  if (S_IS_WEAKREFR (s))
2824
25
    symbol_mark_used (s->x->value.X_add_symbol);
2825
191k
}
2826
2827
/* Clear the mark of whether a symbol has been used.  */
2828
2829
void
2830
symbol_clear_used (symbolS *s)
2831
0
{
2832
0
  if (s->flags.local_symbol)
2833
0
    s = local_symbol_convert (s);
2834
0
  s->flags.used = 0;
2835
0
}
2836
2837
/* Return whether a symbol has been used.  */
2838
2839
int
2840
symbol_used_p (const symbolS *s)
2841
0
{
2842
0
  if (s->flags.local_symbol)
2843
0
    return 1;
2844
0
  return s->flags.used;
2845
0
}
2846
2847
/* Mark a symbol as having been used in a reloc.  */
2848
2849
void
2850
symbol_mark_used_in_reloc (symbolS *s)
2851
23
{
2852
23
  if (s->flags.local_symbol)
2853
0
    s = local_symbol_convert (s);
2854
23
  s->flags.used_in_reloc = 1;
2855
23
}
2856
2857
/* Clear the mark of whether a symbol has been used in a reloc.  */
2858
2859
void
2860
symbol_clear_used_in_reloc (symbolS *s)
2861
0
{
2862
0
  if (s->flags.local_symbol)
2863
0
    return;
2864
0
  s->flags.used_in_reloc = 0;
2865
0
}
2866
2867
/* Return whether a symbol has been used in a reloc.  */
2868
2869
int
2870
symbol_used_in_reloc_p (const symbolS *s)
2871
0
{
2872
0
  if (s->flags.local_symbol)
2873
0
    return 0;
2874
0
  return s->flags.used_in_reloc;
2875
0
}
2876
2877
/* Mark a symbol as an MRI common symbol.  */
2878
2879
void
2880
symbol_mark_mri_common (symbolS *s)
2881
0
{
2882
0
  if (s->flags.local_symbol)
2883
0
    s = local_symbol_convert (s);
2884
0
  s->flags.mri_common = 1;
2885
0
}
2886
2887
/* Clear the mark of whether a symbol is an MRI common symbol.  */
2888
2889
void
2890
symbol_clear_mri_common (symbolS *s)
2891
0
{
2892
0
  if (s->flags.local_symbol)
2893
0
    return;
2894
0
  s->flags.mri_common = 0;
2895
0
}
2896
2897
/* Return whether a symbol is an MRI common symbol.  */
2898
2899
int
2900
symbol_mri_common_p (const symbolS *s)
2901
0
{
2902
0
  if (s->flags.local_symbol)
2903
0
    return 0;
2904
0
  return s->flags.mri_common;
2905
0
}
2906
2907
/* Mark a symbol as having been written.  */
2908
2909
void
2910
symbol_mark_written (symbolS *s)
2911
0
{
2912
0
  if (s->flags.local_symbol)
2913
0
    return;
2914
0
  s->flags.written = 1;
2915
0
}
2916
2917
/* Clear the mark of whether a symbol has been written.  */
2918
2919
void
2920
symbol_clear_written (symbolS *s)
2921
0
{
2922
0
  if (s->flags.local_symbol)
2923
0
    return;
2924
0
  s->flags.written = 0;
2925
0
}
2926
2927
/* Return whether a symbol has been written.  */
2928
2929
int
2930
symbol_written_p (const symbolS *s)
2931
0
{
2932
0
  if (s->flags.local_symbol)
2933
0
    return 0;
2934
0
  return s->flags.written;
2935
0
}
2936
2937
/* Mark a symbol as to be removed.  */
2938
2939
void
2940
symbol_mark_removed (symbolS *s)
2941
0
{
2942
0
  if (s->flags.local_symbol)
2943
0
    return;
2944
0
  s->flags.removed = 1;
2945
0
}
2946
2947
/* Return whether a symbol has been marked to be removed.  */
2948
2949
int
2950
symbol_removed_p (const symbolS *s)
2951
0
{
2952
0
  if (s->flags.local_symbol)
2953
0
    return 0;
2954
0
  return s->flags.removed;
2955
0
}
2956
2957
/* Mark a symbol as having been resolved.  */
2958
2959
void
2960
symbol_mark_resolved (symbolS *s)
2961
0
{
2962
0
  s->flags.resolved = 1;
2963
0
}
2964
2965
/* Return whether a symbol has been resolved.  */
2966
2967
int
2968
symbol_resolved_p (const symbolS *s)
2969
628
{
2970
628
  return s->flags.resolved;
2971
628
}
2972
2973
/* Mark a symbol as being resolved.  */
2974
2975
void
2976
symbol_mark_resolving (symbolS *s)
2977
10.2k
{
2978
10.2k
  s->flags.resolving = 1;
2979
10.2k
}
2980
2981
void
2982
symbol_clear_resolving (symbolS *s)
2983
10.2k
{
2984
10.2k
  s->flags.resolving = 0;
2985
10.2k
}
2986
2987
/* Return whether a symbol is being resolved.  */
2988
2989
int
2990
symbol_resolving_p (const symbolS *s)
2991
40.8k
{
2992
40.8k
  return s->flags.resolving;
2993
40.8k
}
2994
2995
/* Return whether a symbol is a section symbol.  */
2996
2997
int
2998
symbol_section_p (const symbolS *s)
2999
6.23k
{
3000
6.23k
  if (s->flags.local_symbol)
3001
0
    return 0;
3002
6.23k
  return (s->bsym->flags & BSF_SECTION_SYM) != 0;
3003
6.23k
}
3004
3005
/* Return whether a symbol is equated to another symbol.  */
3006
3007
int
3008
symbol_equated_p (const symbolS *s)
3009
96.2k
{
3010
96.2k
  if (s->flags.local_symbol)
3011
85
    return 0;
3012
96.1k
  return s->x->value.X_op == O_symbol;
3013
96.2k
}
3014
3015
/* Return whether a symbol is equated to another symbol, and should be
3016
   treated specially when writing out relocs.  */
3017
3018
int
3019
symbol_equated_reloc_p (const symbolS *s)
3020
0
{
3021
0
  if (s->flags.local_symbol)
3022
0
    return 0;
3023
  /* X_op_symbol, normally not used for O_symbol, is set by
3024
     resolve_symbol_value to flag expression syms that have been
3025
     equated.  */
3026
0
  return (s->x->value.X_op == O_symbol
3027
#if defined (OBJ_COFF) && defined (TE_PE)
3028
    && ! S_IS_WEAK (s)
3029
#endif
3030
0
    && ((s->flags.resolved && s->x->value.X_op_symbol != NULL)
3031
0
        || ! S_IS_DEFINED (s)
3032
0
        || S_IS_COMMON (s)));
3033
0
}
3034
3035
/* Return the final symbol in a chain of equated symbols, and the offset
3036
   from that symbol.  If the chain has a loop, return NULL.
3037
   For a non-equated SYM, return SYM.  */
3038
3039
symbolS *
3040
symbol_equated_to (symbolS *sym, offsetT *off)
3041
47.3k
{
3042
47.3k
  valueT add = 0;
3043
47.3k
  symbolS *ret = sym;
3044
50.6k
  while (ret && symbol_equated_p (ret))
3045
3.35k
    {
3046
3.35k
      const expressionS *e = symbol_get_value_expression (ret);
3047
3.35k
      add += e->X_add_number;
3048
3.35k
      ret->flags.resolving = 1;
3049
3.35k
      ret = e->X_add_symbol;
3050
3.35k
      if (ret->flags.resolving)
3051
0
  ret = NULL;
3052
3.35k
    }
3053
50.6k
  while (sym && sym->flags.resolving)
3054
3.35k
    {
3055
3.35k
      const expressionS *e = symbol_get_value_expression (sym);
3056
3.35k
      sym->flags.resolving = 0;
3057
3.35k
      sym = e->X_add_symbol;
3058
3.35k
    }
3059
47.3k
  *off = add;
3060
47.3k
  return ret;
3061
47.3k
}
3062
3063
/* Return whether a symbol has a constant value.  */
3064
3065
int
3066
symbol_constant_p (const symbolS *s)
3067
1.15k
{
3068
1.15k
  if (s->flags.local_symbol)
3069
0
    return 1;
3070
1.15k
  return s->x->value.X_op == O_constant;
3071
1.15k
}
3072
3073
/* Return whether a symbol was cloned and thus removed from the global
3074
   symbol list.  */
3075
3076
int
3077
symbol_shadow_p (const symbolS *s)
3078
0
{
3079
0
  if (s->flags.local_symbol)
3080
0
    return 0;
3081
0
  return s->x->next == s;
3082
0
}
3083
3084
/* If S is a struct symbol return S, otherwise return NULL.  */
3085
3086
symbolS *
3087
symbol_symbolS (symbolS *s)
3088
0
{
3089
0
  if (s->flags.local_symbol)
3090
0
    return NULL;
3091
0
  return s;
3092
0
}
3093
3094
/* Return the BFD symbol for a symbol.  */
3095
3096
asymbol *
3097
symbol_get_bfdsym (symbolS *s)
3098
14.3k
{
3099
14.3k
  if (s->flags.local_symbol)
3100
0
    s = local_symbol_convert (s);
3101
14.3k
  return s->bsym;
3102
14.3k
}
3103
3104
/* Set the BFD symbol for a symbol.  */
3105
3106
void
3107
symbol_set_bfdsym (symbolS *s, asymbol *bsym)
3108
1.25k
{
3109
1.25k
  if (s->flags.local_symbol)
3110
0
    s = local_symbol_convert (s);
3111
  /* Usually, it is harmless to reset a symbol to a BFD section
3112
     symbol. For example, obj_elf_change_section sets the BFD symbol
3113
     of an old symbol with the newly created section symbol. But when
3114
     we have multiple sections with the same name, the newly created
3115
     section may have the same name as an old section. We check if the
3116
     old symbol has been already marked as a section symbol before
3117
     resetting it.  */
3118
1.25k
  if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
3119
1.25k
    s->bsym = bsym;
3120
  /* else XXX - What do we do now ?  */
3121
1.25k
}
3122
3123
#ifdef OBJ_SYMFIELD_TYPE
3124
3125
/* Get a pointer to the object format information for a symbol.  */
3126
3127
OBJ_SYMFIELD_TYPE *
3128
symbol_get_obj (symbolS *s)
3129
69.2k
{
3130
69.2k
  if (s->flags.local_symbol)
3131
1
    s = local_symbol_convert (s);
3132
69.2k
  return &s->x->obj;
3133
69.2k
}
3134
3135
/* Set the object format information for a symbol.  */
3136
3137
void
3138
symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
3139
0
{
3140
0
  if (s->flags.local_symbol)
3141
0
    s = local_symbol_convert (s);
3142
0
  s->x->obj = *o;
3143
0
}
3144
3145
#endif /* OBJ_SYMFIELD_TYPE */
3146
3147
#ifdef TC_SYMFIELD_TYPE
3148
3149
/* Get a pointer to the processor information for a symbol.  */
3150
3151
TC_SYMFIELD_TYPE *
3152
symbol_get_tc (symbolS *s)
3153
{
3154
  if (s->flags.local_symbol)
3155
    s = local_symbol_convert (s);
3156
  return &s->x->tc;
3157
}
3158
3159
/* Set the processor information for a symbol.  */
3160
3161
void
3162
symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
3163
{
3164
  if (s->flags.local_symbol)
3165
    s = local_symbol_convert (s);
3166
  s->x->tc = *o;
3167
}
3168
3169
#endif /* TC_SYMFIELD_TYPE */
3170
3171
void
3172
symbol_begin (void)
3173
368
{
3174
368
  symbol_lastP = NULL;
3175
368
  symbol_rootP = NULL;   /* In case we have 0 symbols (!!)  */
3176
368
  sy_hash = htab_create_alloc (1024, hash_symbol_entry, eq_symbol_entry,
3177
368
             NULL, xcalloc, free);
3178
3179
368
#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
3180
368
  abs_symbol.bsym = bfd_abs_section_ptr->symbol;
3181
368
#endif
3182
368
  abs_symbol.x = &abs_symbol_x;
3183
368
  abs_symbol.x->value.X_op = O_constant;
3184
368
  abs_symbol.frag = &zero_address_frag;
3185
3186
368
  if (LOCAL_LABELS_FB)
3187
368
    fb_label_init ();
3188
368
}
3189
3190
void
3191
symbol_end (void)
3192
368
{
3193
368
  if (ENABLE_LEAK_CHECK)
3194
368
    htab_delete (sy_hash);
3195
368
}
3196
3197
void
3198
dot_symbol_init (void)
3199
368
{
3200
368
  dot_symbol.name = ".";
3201
368
  dot_symbol.flags.forward_ref = 1;
3202
368
  dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
3203
368
  if (dot_symbol.bsym == NULL)
3204
0
    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
3205
368
  dot_symbol.bsym->name = ".";
3206
368
  dot_symbol.x = &dot_symbol_x;
3207
368
  dot_symbol.x->value.X_op = O_constant;
3208
368
}
3209

3210
int indent_level;
3211
3212
/* Maximum indent level.
3213
   Available for modification inside a gdb session.  */
3214
static int max_indent_level = 8;
3215
3216
void
3217
print_symbol_value_1 (FILE *file, symbolS *sym)
3218
0
{
3219
0
  const char *name = S_GET_NAME (sym);
3220
0
  if (!name || !name[0])
3221
0
    name = "(unnamed)";
3222
0
  fprintf (file, "sym %p %s", sym, name);
3223
3224
0
  if (sym->flags.local_symbol)
3225
0
    {
3226
0
      struct local_symbol *locsym = (struct local_symbol *) sym;
3227
3228
0
      if (locsym->frag != &zero_address_frag
3229
0
    && locsym->frag != NULL)
3230
0
  fprintf (file, " frag %p", locsym->frag);
3231
0
      if (locsym->flags.resolved)
3232
0
  fprintf (file, " resolved");
3233
0
      fprintf (file, " locsym");
3234
0
    }
3235
0
  else
3236
0
    {
3237
0
      if (sym->frag != &zero_address_frag)
3238
0
  fprintf (file, " frag %p", sym->frag);
3239
0
      if (sym->flags.written)
3240
0
  fprintf (file, " written");
3241
0
      if (sym->flags.resolved)
3242
0
  fprintf (file, " resolved");
3243
0
      else if (sym->flags.resolving)
3244
0
  fprintf (file, " resolving");
3245
0
      if (sym->flags.used_in_reloc)
3246
0
  fprintf (file, " used-in-reloc");
3247
0
      if (sym->flags.used)
3248
0
  fprintf (file, " used");
3249
0
      if (S_IS_LOCAL (sym))
3250
0
  fprintf (file, " local");
3251
0
      if (S_IS_EXTERNAL (sym))
3252
0
  fprintf (file, " extern");
3253
0
      if (S_IS_WEAK (sym))
3254
0
  fprintf (file, " weak");
3255
0
      if (S_IS_DEBUG (sym))
3256
0
  fprintf (file, " debug");
3257
0
      if (S_IS_DEFINED (sym))
3258
0
  fprintf (file, " defined");
3259
0
    }
3260
0
  if (S_IS_WEAKREFR (sym))
3261
0
    fprintf (file, " weakrefr");
3262
0
  if (S_IS_WEAKREFD (sym))
3263
0
    fprintf (file, " weakrefd");
3264
0
  fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
3265
0
  if (symbol_resolved_p (sym))
3266
0
    {
3267
0
      segT s = S_GET_SEGMENT (sym);
3268
3269
0
      if (s != undefined_section
3270
0
    && s != expr_section)
3271
0
  fprintf (file, " %"PRIx64, (uint64_t) S_GET_VALUE (sym));
3272
0
    }
3273
0
  else if (indent_level < max_indent_level
3274
0
     && S_GET_SEGMENT (sym) != undefined_section)
3275
0
    {
3276
0
      indent_level++;
3277
0
      fprintf (file, "\n%*s<", indent_level * 4, "");
3278
0
      if (sym->flags.local_symbol)
3279
0
  fprintf (file, "constant %"PRIx64,
3280
0
     (uint64_t) ((struct local_symbol *) sym)->value);
3281
0
      else
3282
0
  print_expr_1 (file, &sym->x->value);
3283
0
      fprintf (file, ">");
3284
0
      indent_level--;
3285
0
    }
3286
0
  fflush (file);
3287
0
}
3288
3289
void
3290
print_symbol_value (symbolS *sym)
3291
0
{
3292
0
  indent_level = 0;
3293
0
  print_symbol_value_1 (stderr, sym);
3294
0
  fprintf (stderr, "\n");
3295
0
}
3296
3297
static void
3298
print_binary (FILE *file, const char *name, expressionS *exp)
3299
0
{
3300
0
  indent_level++;
3301
0
  fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
3302
0
  print_symbol_value_1 (file, exp->X_add_symbol);
3303
0
  fprintf (file, ">\n%*s<", indent_level * 4, "");
3304
0
  print_symbol_value_1 (file, exp->X_op_symbol);
3305
0
  fprintf (file, ">");
3306
0
  indent_level--;
3307
0
}
3308
3309
void
3310
print_expr_1 (FILE *file, expressionS *exp)
3311
0
{
3312
0
  fprintf (file, "expr %p ", exp);
3313
0
  switch (exp->X_op)
3314
0
    {
3315
0
    case O_illegal:
3316
0
      fprintf (file, "illegal");
3317
0
      break;
3318
0
    case O_absent:
3319
0
      fprintf (file, "absent");
3320
0
      break;
3321
0
    case O_constant:
3322
0
      fprintf (file, "constant %" PRIx64, (uint64_t) exp->X_add_number);
3323
0
      break;
3324
0
    case O_symbol:
3325
0
      indent_level++;
3326
0
      fprintf (file, "symbol\n%*s<", indent_level * 4, "");
3327
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3328
0
      fprintf (file, ">");
3329
0
    maybe_print_addnum:
3330
0
      if (exp->X_add_number)
3331
0
  fprintf (file, "\n%*s%" PRIx64, indent_level * 4, "",
3332
0
     (uint64_t) exp->X_add_number);
3333
0
      indent_level--;
3334
0
      break;
3335
0
    case O_register:
3336
0
      fprintf (file, "register #%d", (int) exp->X_add_number);
3337
0
      break;
3338
0
    case O_big:
3339
0
      fprintf (file, "big");
3340
0
      break;
3341
0
    case O_uminus:
3342
0
      fprintf (file, "uminus -<");
3343
0
      indent_level++;
3344
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3345
0
      fprintf (file, ">");
3346
0
      goto maybe_print_addnum;
3347
0
    case O_bit_not:
3348
0
      fprintf (file, "bit_not");
3349
0
      break;
3350
0
    case O_multiply:
3351
0
      print_binary (file, "multiply", exp);
3352
0
      break;
3353
0
    case O_divide:
3354
0
      print_binary (file, "divide", exp);
3355
0
      break;
3356
0
    case O_modulus:
3357
0
      print_binary (file, "modulus", exp);
3358
0
      break;
3359
0
    case O_left_shift:
3360
0
      print_binary (file, "lshift", exp);
3361
0
      break;
3362
0
    case O_right_shift:
3363
0
      print_binary (file, "rshift", exp);
3364
0
      break;
3365
0
    case O_bit_inclusive_or:
3366
0
      print_binary (file, "bit_ior", exp);
3367
0
      break;
3368
0
    case O_bit_exclusive_or:
3369
0
      print_binary (file, "bit_xor", exp);
3370
0
      break;
3371
0
    case O_bit_and:
3372
0
      print_binary (file, "bit_and", exp);
3373
0
      break;
3374
0
    case O_eq:
3375
0
      print_binary (file, "eq", exp);
3376
0
      break;
3377
0
    case O_ne:
3378
0
      print_binary (file, "ne", exp);
3379
0
      break;
3380
0
    case O_lt:
3381
0
      print_binary (file, "lt", exp);
3382
0
      break;
3383
0
    case O_le:
3384
0
      print_binary (file, "le", exp);
3385
0
      break;
3386
0
    case O_ge:
3387
0
      print_binary (file, "ge", exp);
3388
0
      break;
3389
0
    case O_gt:
3390
0
      print_binary (file, "gt", exp);
3391
0
      break;
3392
0
    case O_logical_and:
3393
0
      print_binary (file, "logical_and", exp);
3394
0
      break;
3395
0
    case O_logical_or:
3396
0
      print_binary (file, "logical_or", exp);
3397
0
      break;
3398
0
    case O_add:
3399
0
      indent_level++;
3400
0
      fprintf (file, "add\n%*s<", indent_level * 4, "");
3401
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3402
0
      fprintf (file, ">\n%*s<", indent_level * 4, "");
3403
0
      print_symbol_value_1 (file, exp->X_op_symbol);
3404
0
      fprintf (file, ">");
3405
0
      goto maybe_print_addnum;
3406
0
    case O_subtract:
3407
0
      indent_level++;
3408
0
      fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3409
0
      print_symbol_value_1 (file, exp->X_add_symbol);
3410
0
      fprintf (file, ">\n%*s<", indent_level * 4, "");
3411
0
      print_symbol_value_1 (file, exp->X_op_symbol);
3412
0
      fprintf (file, ">");
3413
0
      goto maybe_print_addnum;
3414
0
    case O_index:
3415
0
      indent_level++;
3416
0
      fprintf (file, "index\n%*s<", indent_level * 4, "");
3417
0
      if (exp->X_add_symbol != NULL)
3418
0
  print_symbol_value_1 (file, exp->X_add_symbol);
3419
0
      fprintf (file, ">\n%*s<", indent_level * 4, "");
3420
0
      print_symbol_value_1 (file, exp->X_op_symbol);
3421
0
      fprintf (file, ">");
3422
0
      indent_level--;
3423
0
      break;
3424
0
    default:
3425
0
      fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3426
0
      break;
3427
0
    }
3428
0
  fflush (stdout);
3429
0
}
3430
3431
void
3432
print_expr (expressionS *exp)
3433
0
{
3434
0
  print_expr_1 (stderr, exp);
3435
0
  fprintf (stderr, "\n");
3436
0
}
3437
3438
void
3439
symbol_print_statistics (FILE *file)
3440
0
{
3441
0
  htab_print_statistics (file, "symbol table", sy_hash);
3442
0
  fprintf (file, "%lu mini local symbols created, %lu converted\n",
3443
0
     local_symbol_count, local_symbol_conversion_count);
3444
0
}
3445
3446
#ifdef OBJ_COMPLEX_RELC
3447
3448
/* Convert given symbol to a new complex-relocation symbol name.  This
3449
   may be a recursive function, since it might be called for non-leaf
3450
   nodes (plain symbols) in the expression tree.  The caller owns the
3451
   returning string, so should free it eventually.  Errors are
3452
   indicated via as_bad and a NULL return value.  The given symbol
3453
   is marked with used_in_reloc.  */
3454
3455
char *
3456
symbol_relc_make_sym (symbolS * sym)
3457
{
3458
  char * terminal = NULL;
3459
  const char * sname;
3460
  char typetag;
3461
  int sname_len;
3462
3463
  gas_assert (sym != NULL);
3464
3465
  /* Recurse to symbol_relc_make_expr if this symbol
3466
     is defined as an expression or a plain value.  */
3467
  if (   S_GET_SEGMENT (sym) == expr_section
3468
      || S_GET_SEGMENT (sym) == absolute_section)
3469
    return symbol_relc_make_expr (symbol_get_value_expression (sym));
3470
3471
  /* This may be a "fake symbol", referring to ".".
3472
     Write out a special null symbol to refer to this position.  */
3473
  if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3474
    return xstrdup (".");
3475
3476
  /* We hope this is a plain leaf symbol.  Construct the encoding
3477
     as {S,s}II...:CCCCCCC....
3478
     where 'S'/'s' means section symbol / plain symbol
3479
     III is decimal for the symbol name length
3480
     CCC is the symbol name itself.  */
3481
  symbol_mark_used_in_reloc (sym);
3482
3483
  sname = S_GET_NAME (sym);
3484
  sname_len = strlen (sname);
3485
  typetag = symbol_section_p (sym) ? 'S' : 's';
3486
3487
  terminal = XNEWVEC (char, (1 /* S or s */
3488
           + 8 /* sname_len in decimal */
3489
           + 1 /* _ spacer */
3490
           + sname_len /* name itself */
3491
           + 1 /* \0 */ ));
3492
3493
  sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3494
  return terminal;
3495
}
3496
3497
/* Convert given value to a new complex-relocation symbol name.  This
3498
   is a non-recursive function, since it is be called for leaf nodes
3499
   (plain values) in the expression tree.  The caller owns the
3500
   returning string, so should free() it eventually.  No errors.  */
3501
3502
char *
3503
symbol_relc_make_value (offsetT val)
3504
{
3505
  char * terminal = XNEWVEC (char, 28);  /* Enough for long long.  */
3506
3507
  terminal[0] = '#';
3508
  bfd_sprintf_vma (stdoutput, terminal + 1, val);
3509
  return terminal;
3510
}
3511
3512
/* Convert given expression to a new complex-relocation symbol name.
3513
   This is a recursive function, since it traverses the entire given
3514
   expression tree.  The caller owns the returning string, so should
3515
   free() it eventually.  Errors are indicated via as_bad() and a NULL
3516
   return value.  */
3517
3518
char *
3519
symbol_relc_make_expr (expressionS * exp)
3520
{
3521
  const char * opstr = NULL; /* Operator prefix string.  */
3522
  int    arity = 0;    /* Arity of this operator.  */
3523
  char * operands[3];  /* Up to three operands.  */
3524
  char * concat_string = NULL;
3525
3526
  operands[0] = operands[1] = operands[2] = NULL;
3527
3528
  gas_assert (exp != NULL);
3529
3530
  /* Match known operators -> fill in opstr, arity, operands[] and fall
3531
     through to construct subexpression fragments; may instead return
3532
     string directly for leaf nodes.  */
3533
3534
  /* See expr.h for the meaning of all these enums.  Many operators
3535
     have an unnatural arity (X_add_number implicitly added).  The
3536
     conversion logic expands them to explicit "+" subexpressions.   */
3537
3538
  switch (exp->X_op)
3539
    {
3540
    default:
3541
      as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3542
      break;
3543
3544
      /* Leaf nodes.  */
3545
    case O_constant:
3546
      return symbol_relc_make_value (exp->X_add_number);
3547
3548
    case O_symbol:
3549
      if (exp->X_add_number)
3550
  {
3551
    arity = 2;
3552
    opstr = "+";
3553
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3554
    operands[1] = symbol_relc_make_value (exp->X_add_number);
3555
    break;
3556
  }
3557
      else
3558
  return symbol_relc_make_sym (exp->X_add_symbol);
3559
3560
      /* Helper macros for nesting nodes.  */
3561
3562
#define HANDLE_XADD_OPT1(str_)            \
3563
      if (exp->X_add_number)            \
3564
  {               \
3565
    arity = 2;              \
3566
    opstr = "+:" str_;            \
3567
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3568
    operands[1] = symbol_relc_make_value (exp->X_add_number); \
3569
    break;              \
3570
  }               \
3571
      else                \
3572
  {               \
3573
    arity = 1;              \
3574
    opstr = str_;             \
3575
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3576
  }               \
3577
      break
3578
3579
#define HANDLE_XADD_OPT2(str_)            \
3580
      if (exp->X_add_number)            \
3581
  {               \
3582
    arity = 3;              \
3583
    opstr = "+:" str_;            \
3584
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3585
    operands[1] = symbol_relc_make_sym (exp->X_op_symbol);  \
3586
    operands[2] = symbol_relc_make_value (exp->X_add_number); \
3587
  }               \
3588
      else                \
3589
  {               \
3590
    arity = 2;              \
3591
    opstr = str_;             \
3592
    operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3593
    operands[1] = symbol_relc_make_sym (exp->X_op_symbol);  \
3594
  }               \
3595
      break
3596
3597
      /* Nesting nodes.  */
3598
3599
    case O_uminus:    HANDLE_XADD_OPT1 ("0-");
3600
    case O_bit_not:   HANDLE_XADD_OPT1 ("~");
3601
    case O_logical_not:   HANDLE_XADD_OPT1 ("!");
3602
    case O_multiply:    HANDLE_XADD_OPT2 ("*");
3603
    case O_divide:    HANDLE_XADD_OPT2 ("/");
3604
    case O_modulus:   HANDLE_XADD_OPT2 ("%");
3605
    case O_left_shift:    HANDLE_XADD_OPT2 ("<<");
3606
    case O_right_shift:   HANDLE_XADD_OPT2 (">>");
3607
    case O_bit_inclusive_or:  HANDLE_XADD_OPT2 ("|");
3608
    case O_bit_exclusive_or:  HANDLE_XADD_OPT2 ("^");
3609
    case O_bit_and:   HANDLE_XADD_OPT2 ("&");
3610
    case O_add:     HANDLE_XADD_OPT2 ("+");
3611
    case O_subtract:    HANDLE_XADD_OPT2 ("-");
3612
    case O_eq:      HANDLE_XADD_OPT2 ("==");
3613
    case O_ne:      HANDLE_XADD_OPT2 ("!=");
3614
    case O_lt:      HANDLE_XADD_OPT2 ("<");
3615
    case O_le:      HANDLE_XADD_OPT2 ("<=");
3616
    case O_ge:      HANDLE_XADD_OPT2 (">=");
3617
    case O_gt:      HANDLE_XADD_OPT2 (">");
3618
    case O_logical_and:   HANDLE_XADD_OPT2 ("&&");
3619
    case O_logical_or:    HANDLE_XADD_OPT2 ("||");
3620
    }
3621
3622
  /* Validate & reject early.  */
3623
  if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3624
    opstr = NULL;
3625
  if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3626
    opstr = NULL;
3627
  if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3628
    opstr = NULL;
3629
3630
  if (opstr == NULL)
3631
    concat_string = NULL;
3632
  else if (arity == 0)
3633
    concat_string = xstrdup (opstr);
3634
  else if (arity == 1)
3635
    concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3636
  else if (arity == 2)
3637
    concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3638
          (char *) NULL);
3639
  else
3640
    concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3641
          operands[2], (char *) NULL);
3642
3643
  /* Free operand strings (not opstr).  */
3644
  if (arity >= 1) xfree (operands[0]);
3645
  if (arity >= 2) xfree (operands[1]);
3646
  if (arity >= 3) xfree (operands[2]);
3647
3648
  return concat_string;
3649
}
3650
3651
#endif