Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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