2006-12-13 Paul Brook <paul@codesourcery.com>
[binutils-gdb.git] / gas / symbols.c
index e8c110cb4212845326822278d25e73f07908b44f..41fabc5df0a5987aee1fadbcf2a35fc8c3fec055 100644 (file)
@@ -1,6 +1,6 @@
 /* symbols.c -symbol table-
    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
    This file is part of GAS, the GNU Assembler.
@@ -144,8 +144,7 @@ symbol_create (const char *name, /* It is copied, the caller can destroy/modify.
 
   symbolP->bsym = bfd_make_empty_symbol (stdoutput);
   if (symbolP->bsym == NULL)
-    as_perror ("%s", "bfd_make_empty_symbol");
-  symbolP->bsym->udata.p = (PTR) symbolP;
+    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
   S_SET_NAME (symbolP, preserved_copy_of_name);
 
   S_SET_SEGMENT (symbolP, segment);
@@ -333,8 +332,15 @@ colon (/* Just seen "x:" - rattle symbols & frags.  */
          locsym->lsy_value = frag_now_fix ();
        }
       else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
-              || S_IS_COMMON (symbolP))
+              || S_IS_COMMON (symbolP)
+              || S_IS_VOLATILE (symbolP))
        {
+         if (S_IS_VOLATILE (symbolP))
+           {
+             symbolP = symbol_clone (symbolP, 1);
+             S_SET_VALUE (symbolP, 0);
+             S_CLEAR_VOLATILE (symbolP);
+           }
          if (S_GET_VALUE (symbolP) == 0)
            {
              symbolP->sy_frag = frag_now;
@@ -366,6 +372,7 @@ colon (/* Just seen "x:" - rattle symbols & frags.  */
                    && S_IS_EXTERNAL (symbolP))
                   || S_GET_SEGMENT (symbolP) == bss_section)
                  && (now_seg == data_section
+                     || now_seg == bss_section
                      || now_seg == S_GET_SEGMENT (symbolP)))
                {
                  /* Select which of the 2 cases this is.  */
@@ -421,7 +428,10 @@ colon (/* Just seen "x:" - rattle symbols & frags.  */
          if (!(frag_now == symbolP->sy_frag
                && S_GET_VALUE (symbolP) == frag_now_fix ()
                && S_GET_SEGMENT (symbolP) == now_seg))
-           as_bad (_("symbol `%s' is already defined"), sym_name);
+           {
+             as_bad (_("symbol `%s' is already defined"), sym_name);
+             symbolP = symbol_clone (symbolP, 0);
+           }
        }
 
     }
@@ -559,12 +569,11 @@ symbol_clone (symbolS *orgsymP, int replace)
   *newsymP = *orgsymP;
   bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
   if (bsymnew == NULL)
-    as_perror ("%s", "bfd_make_empty_symbol");
+    as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
   newsymP->bsym = bsymnew;
   bsymnew->name = bsymorg->name;
   bsymnew->flags =  bsymorg->flags;
   bsymnew->section =  bsymorg->section;
-  bsymnew->udata.p = (PTR) newsymP;
   bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
                                bfd_asymbol_bfd (bsymnew), bsymnew);
 
@@ -589,11 +598,13 @@ symbol_clone (symbolS *orgsymP, int replace)
        symbol_lastP = newsymP;
       else if (orgsymP->sy_next)
        orgsymP->sy_next->sy_previous = newsymP;
-      orgsymP->sy_next = NULL;
+      orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
       debug_verify_symchain (symbol_rootP, symbol_lastP);
 
       symbol_table_insert (newsymP);
     }
+  else
+    newsymP->sy_previous = newsymP->sy_next = newsymP;
 
   return newsymP;
 }
@@ -914,13 +925,11 @@ report_op_error (symbolS *symp, symbolS *left, symbolS *right)
          && seg_right != undefined_section)
        {
          if (right)
-           as_bad_where (file, line,
-                         _("invalid sections for operation on `%s' and `%s' setting `%s'"),
-                         S_GET_NAME (left), S_GET_NAME (right), S_GET_NAME (symp));
+           as_bad (_("invalid sections for operation on `%s' and `%s' setting `%s'"),
+                   S_GET_NAME (left), S_GET_NAME (right), S_GET_NAME (symp));
          else
-           as_bad_where (file, line,
-                         _("invalid section for operation on `%s' setting `%s'"),
-                         S_GET_NAME (left), S_GET_NAME (symp));
+           as_bad (_("invalid section for operation on `%s' setting `%s'"),
+                   S_GET_NAME (left), S_GET_NAME (symp));
        }
     }
 }
@@ -1071,8 +1080,9 @@ resolve_symbol_value (symbolS *symp)
              symp->sy_resolving = 0;
              goto exit_dont_set_value;
            }
-         else if (finalize_syms && final_seg == expr_section
-                  && seg_left != expr_section)
+         else if (finalize_syms
+                  && ((final_seg == expr_section && seg_left != expr_section)
+                      || symbol_shadow_p (symp)))
            {
              /* If the symbol is an expression symbol, do similarly
                 as for undefined and common syms above.  Handles
@@ -1341,8 +1351,10 @@ resolve_local_symbol_values (void)
    sub-expressions used.  */
 
 int
-snapshot_symbol (symbolS *symbolP, valueT *valueP, segT *segP, fragS **fragPP)
+snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
 {
+  symbolS *symbolP = *symbolPP;
+
   if (LOCAL_SYMBOL_CHECK (symbolP))
     {
       struct local_symbol *locsym = (struct local_symbol *) symbolP;
@@ -1371,10 +1383,7 @@ snapshot_symbol (symbolS *symbolP, valueT *valueP, segT *segP, fragS **fragPP)
            {
            case O_constant:
            case O_register:
-             /* This check wouldn't be needed if pseudo_set() didn't set
-                symbols equated to bare symbols to undefined_section.  */
-             if (symbolP->bsym->section != undefined_section
-                 || symbolP->sy_value.X_op != O_symbol)
+             if (!symbol_equated_p (symbolP))
                break;
              /* Fall thru.  */
            case O_symbol:
@@ -1386,6 +1395,10 @@ snapshot_symbol (symbolS *symbolP, valueT *valueP, segT *segP, fragS **fragPP)
            }
        }
 
+      /* Never change a defined symbol.  */
+      if (symbolP->bsym->section == undefined_section
+         || symbolP->bsym->section == expr_section)
+       *symbolPP = symbolP;
       *valueP = expr.X_add_number;
       *segP = symbolP->bsym->section;
       *fragPP = symbolP->sy_frag;
@@ -1782,20 +1795,11 @@ S_GET_VALUE (symbolS *s)
 
   if (s->sy_value.X_op != O_constant)
     {
-      static symbolS *recur;
-
-      /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
-        may call S_GET_VALUE.  We use a static symbol to avoid the
-        immediate recursion.  */
-      if (recur == s)
-       return (valueT) s->sy_value.X_add_number;
-      recur = s;
       if (! s->sy_resolved
          || s->sy_value.X_op != O_symbol
          || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
        as_bad (_("attempt to get value of unresolved symbol `%s'"),
                S_GET_NAME (s));
-      recur = NULL;
     }
   return (valueT) s->sy_value.X_add_number;
 }
@@ -2195,6 +2199,13 @@ S_SET_VOLATILE (symbolS *s)
   s->sy_volatile = 1;
 }
 
+void
+S_CLEAR_VOLATILE (symbolS *s)
+{
+  if (!LOCAL_SYMBOL_CHECK (s))
+    s->sy_volatile = 0;
+}
+
 void
 S_SET_FORWARD_REF (symbolS *s)
 {
@@ -2484,6 +2495,17 @@ symbol_constant_p (symbolS *s)
   return s->sy_value.X_op == O_constant;
 }
 
+/* Return whether a symbol was cloned and thus removed from the global
+   symbol list.  */
+
+int
+symbol_shadow_p (symbolS *s)
+{
+  if (LOCAL_SYMBOL_CHECK (s))
+    return 0;
+  return s->sy_next == s;
+}
+
 /* Return the BFD symbol for a symbol.  */
 
 asymbol *