Coverage Report

Created: 2025-06-24 06:45

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