* gprof.c (main): Correct copyright message.
[deliverable/binutils-gdb.git] / gas / symbols.c
index 3e8d1deb97f1299612bff7a2dd30b7c0f971b84c..161f83b699ae101f690c5c0f7123f0692567b62c 100644 (file)
@@ -1,5 +1,5 @@
 /* symbols.c -symbol table-
-   Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994
+   Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 1997
    Free Software Foundation, Inc.
 
    This file is part of GAS, the GNU Assembler.
@@ -15,8 +15,9 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with GAS; see the file COPYING.  If not, write to
-   the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   along with GAS; see the file COPYING.  If not, write to the Free
+   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.  */
 
 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
 
@@ -46,12 +47,14 @@ symbolS abs_symbol;
 #ifdef DEBUG_SYMS
 #define debug_verify_symchain verify_symbol_chain
 #else
-#define debug_verify_symchain (void)
+#define debug_verify_symchain(root, last) ((void) 0)
 #endif
 
 struct obstack notes;
 
 static void fb_label_init PARAMS ((void));
+static long dollar_label_instance PARAMS ((long));
+static long fb_label_instance PARAMS ((long));
 
 /* symbol_new()
   
@@ -84,7 +87,6 @@ symbol_new (name, segment, valu, frag)
   }
 #endif
   symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
-  debug_verify_symchain (symbol_rootP, symbol_lastP);
 
   return symbolP;
 }
@@ -129,7 +131,8 @@ symbol_create (name, segment, valu, frag)
 
 #ifdef BFD_ASSEMBLER
   symbolP->bsym = bfd_make_empty_symbol (stdoutput);
-  assert (symbolP->bsym != 0);
+  if (symbolP->bsym == NULL)
+    as_perror ("%s", "bfd_make_empty_symbol");
   symbolP->bsym->udata.p = (PTR) symbolP;
 #endif
   S_SET_NAME (symbolP, preserved_copy_of_name);
@@ -165,15 +168,26 @@ symbol_create (name, segment, valu, frag)
  */
 symbolS *
 colon (sym_name)               /* just seen "x:" - rattle symbols & frags */
-     register char *sym_name;  /* symbol name, as a cannonical string */
+     const char *sym_name;     /* symbol name, as a cannonical string */
      /* We copy this string: OK to alter later. */
 {
   register symbolS *symbolP;   /* symbol we are working with */
 
   /* Sun local labels go out of scope whenever a non-local symbol is
      defined.  */
-  if (LOCAL_LABELS_DOLLAR && *sym_name != 'L')
-    dollar_label_clear ();
+  if (LOCAL_LABELS_DOLLAR)
+    {
+      int local;
+
+#ifdef BFD_ASSEMBLER
+      local = bfd_is_local_label_name (stdoutput, sym_name);
+#else
+      local = LOCAL_LABEL (sym_name);
+#endif
+
+      if (! local)
+       dollar_label_clear ();
+    }
 
 #ifndef WORKING_DOT_WORD
   if (new_broken_words)
@@ -194,7 +208,7 @@ colon (sym_name)            /* just seen "x:" - rattle symbols & frags */
                              possible_bytes,
                              (relax_substateT) 0,
                              (symbolS *) broken_words,
-                             0L,
+                             (offsetT) 0,
                              NULL);
 
       /* We want to store the pointer to where to insert the jump table in the
@@ -228,7 +242,7 @@ colon (sym_name)            /* just seen "x:" - rattle symbols & frags */
            {
              symbolP->sy_frag = frag_now;
 #ifdef OBJ_VMS
-             S_GET_OTHER(symbolP) = const_flag;
+             S_SET_OTHER(symbolP, const_flag);
 #endif
              S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
              S_SET_SEGMENT (symbolP, now_seg);
@@ -280,8 +294,8 @@ colon (sym_name)            /* just seen "x:" - rattle symbols & frags */
                         data.  */
                      symbolP->sy_frag = frag_now;
 #ifdef OBJ_VMS
-                     S_GET_OTHER(symbolP) = const_flag;
-#endif /* OBJ_VMS */
+                     S_SET_OTHER(symbolP, const_flag);
+#endif
                      S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
                      S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
                    }
@@ -375,7 +389,7 @@ symbol_table_insert (symbolP)
  */
 symbolS *
 symbol_find_or_make (name)
-     char *name;
+     const char *name;
 {
   register symbolS *symbolP;
 
@@ -478,6 +492,10 @@ symbol_append (addme, target, rootPP, lastPP)
     {
       know (*rootPP == NULL);
       know (*lastPP == NULL);
+      addme->sy_next = NULL;
+#ifdef SYMBOLS_NEED_BACKPOINTERS
+      addme->sy_previous = NULL;
+#endif
       *rootPP = addme;
       *lastPP = addme;
       return;
@@ -501,6 +519,8 @@ symbol_append (addme, target, rootPP, lastPP)
 #ifdef SYMBOLS_NEED_BACKPOINTERS
   addme->sy_previous = target;
 #endif /* SYMBOLS_NEED_BACKPOINTERS */
+
+  debug_verify_symchain (symbol_rootP, symbol_lastP);
 }
 
 /* Set the chain pointers of SYMBOL to null. */
@@ -585,7 +605,7 @@ verify_symbol_chain (rootP, lastP)
   for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
     {
 #ifdef SYMBOLS_NEED_BACKPOINTERS
-      know (symbolP->sy_next->sy_previous == symbolP);
+      assert (symbolP->sy_next->sy_previous == symbolP);
 #else
       /* Walk the list anyways, to make sure pointers are still good.  */
       ;
@@ -638,7 +658,35 @@ resolve_symbol_value (symp)
 
       symp->sy_resolving = 1;
 
-    reduce:
+      /* Simplify addition or subtraction of a constant by folding the
+         constant into X_add_number.  */
+      if (symp->sy_value.X_op == O_add
+         || symp->sy_value.X_op == O_subtract)
+       {
+         resolve_symbol_value (symp->sy_value.X_add_symbol);
+         resolve_symbol_value (symp->sy_value.X_op_symbol);
+         if (S_GET_SEGMENT (symp->sy_value.X_op_symbol) == absolute_section)
+           {
+             right = S_GET_VALUE (symp->sy_value.X_op_symbol);
+             if (symp->sy_value.X_op == O_add)
+               symp->sy_value.X_add_number += right;
+             else
+               symp->sy_value.X_add_number -= right;
+             symp->sy_value.X_op = O_symbol;
+             symp->sy_value.X_op_symbol = NULL;
+           }
+         else if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
+                   == absolute_section)
+                  && symp->sy_value.X_op == O_add)
+           {
+             left = S_GET_VALUE (symp->sy_value.X_add_symbol);
+             symp->sy_value.X_add_symbol = symp->sy_value.X_op_symbol;
+             symp->sy_value.X_add_number += left;
+             symp->sy_value.X_op = O_symbol;
+             symp->sy_value.X_op_symbol = NULL;
+           }
+       }
+
       switch (symp->sy_value.X_op)
        {
        case O_absent:
@@ -664,16 +712,24 @@ resolve_symbol_value (symp)
              break;
            }
 
-#if 0 /* I thought this was needed for some of the i386-svr4 PIC
-        support, but it appears I was wrong, and it breaks rs6000
-        support.  */
-         if (S_GET_SEGMENT (symp->sy_value.X_add_symbol) != undefined_section
-             && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section)
-#endif
+         if (symp->sy_value.X_add_number == 0)
+           copy_symbol_attributes (symp, symp->sy_value.X_add_symbol);
+
+         /* If we have equated this symbol to an undefined symbol, we
+             keep X_op set to O_symbol, and we don't change
+             X_add_number.  This permits the routine which writes out
+             relocation to detect this case, and convert the
+             relocation to be against the symbol to which this symbol
+             is equated.  */
+         if (! S_IS_DEFINED (symp->sy_value.X_add_symbol)
+             || S_IS_COMMON (symp->sy_value.X_add_symbol))
+           {
+             symp->sy_value.X_op = O_symbol;
+             S_SET_SEGMENT (symp,
+                            S_GET_SEGMENT (symp->sy_value.X_add_symbol));
+           }
+         else
            {
-             if (symp->sy_value.X_add_number == 0)
-               copy_symbol_attributes (symp, symp->sy_value.X_add_symbol);
-
              S_SET_VALUE (symp,
                           (symp->sy_value.X_add_number
                            + symp->sy_frag->fr_address
@@ -683,14 +739,18 @@ resolve_symbol_value (symp)
                S_SET_SEGMENT (symp,
                               S_GET_SEGMENT (symp->sy_value.X_add_symbol));
            }
+
          resolved = symp->sy_value.X_add_symbol->sy_resolved;
          break;
 
        case O_uminus:
        case O_bit_not:
+       case O_logical_not:
          resolve_symbol_value (symp->sy_value.X_add_symbol);
          if (symp->sy_value.X_op == O_uminus)
            val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
+         else if (symp->sy_value.X_op == O_logical_not)
+           val = ! S_GET_VALUE (symp->sy_value.X_add_symbol);
          else
            val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
          S_SET_VALUE (symp,
@@ -703,39 +763,6 @@ resolve_symbol_value (symp)
          resolved = symp->sy_value.X_add_symbol->sy_resolved;
          break;
 
-       case O_add:
-         resolve_symbol_value (symp->sy_value.X_add_symbol);
-         resolve_symbol_value (symp->sy_value.X_op_symbol);
-         seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
-         seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
-         /* This case comes up with PIC support.  */
-         {
-           symbolS *s_left = symp->sy_value.X_add_symbol;
-           symbolS *s_right = symp->sy_value.X_op_symbol;
-
-           if (seg_left == absolute_section)
-             {
-               symbolS *t;
-               segT ts;
-               t = s_left;
-               s_left = s_right;
-               s_right = t;
-               ts = seg_left;
-               seg_left = seg_right;
-               seg_right = ts;
-             }
-           if (seg_right == absolute_section
-               && s_right->sy_resolved)
-             {
-               symp->sy_value.X_add_number += S_GET_VALUE (s_right);
-               symp->sy_value.X_op_symbol = 0;
-               symp->sy_value.X_add_symbol = s_left;
-               symp->sy_value.X_op = O_symbol;
-               goto reduce;
-             }
-         }
-         /* fall through */
-
        case O_multiply:
        case O_divide:
        case O_modulus:
@@ -745,6 +772,7 @@ resolve_symbol_value (symp)
        case O_bit_or_not:
        case O_bit_exclusive_or:
        case O_bit_and:
+       case O_add:
        case O_subtract:
        case O_eq:
        case O_ne:
@@ -752,22 +780,60 @@ resolve_symbol_value (symp)
        case O_le:
        case O_ge:
        case O_gt:
+       case O_logical_and:
+       case O_logical_or:
          resolve_symbol_value (symp->sy_value.X_add_symbol);
          resolve_symbol_value (symp->sy_value.X_op_symbol);
          seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
          seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
-         if (seg_left != seg_right
-             && seg_left != undefined_section
-             && seg_right != undefined_section)
-           as_bad ("%s is operation on symbols in different sections",
-                   S_GET_NAME (symp));
-         if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
-              != absolute_section)
-             && symp->sy_value.X_op != O_subtract)
-           as_bad ("%s is illegal operation on non-absolute symbols",
-                   S_GET_NAME (symp));
          left = S_GET_VALUE (symp->sy_value.X_add_symbol);
          right = S_GET_VALUE (symp->sy_value.X_op_symbol);
+
+         /* Subtraction is permitted if both operands are in the same
+            section.  Otherwise, both operands must be absolute.  We
+            already handled the case of addition or subtraction of a
+            constant above.  This will probably need to be changed
+            for an object file format which supports arbitrary
+            expressions, such as IEEE-695.  */
+         if ((seg_left != absolute_section
+              || seg_right != absolute_section)
+             && (symp->sy_value.X_op != O_subtract
+                 || seg_left != seg_right))
+           {
+             char *file;
+             unsigned int line;
+
+             if (expr_symbol_where (symp, &file, &line))
+               {
+                 if (seg_left == undefined_section)
+                   as_bad_where (file, line,
+                                 "undefined symbol %s in operation",
+                                 S_GET_NAME (symp->sy_value.X_add_symbol));
+                 if (seg_right == undefined_section)
+                   as_bad_where (file, line,
+                                 "undefined symbol %s in operation",
+                                 S_GET_NAME (symp->sy_value.X_op_symbol));
+                 if (seg_left != undefined_section
+                     && seg_right != undefined_section)
+                   as_bad_where (file, line, "invalid section for operation");
+               }
+             else
+               {
+                 if (seg_left == undefined_section)
+                   as_bad ("undefined symbol %s in operation setting %s",
+                           S_GET_NAME (symp->sy_value.X_add_symbol),
+                           S_GET_NAME (symp));
+                 if (seg_right == undefined_section)
+                   as_bad ("undefined symbol %s in operation setting %s",
+                           S_GET_NAME (symp->sy_value.X_op_symbol),
+                           S_GET_NAME (symp));
+                 if (seg_left != undefined_section
+                     && seg_right != undefined_section)
+                   as_bad ("invalid section for operation setting %s",
+                           S_GET_NAME (symp));
+               }
+           }
+
          switch (symp->sy_value.X_op)
            {
            case O_multiply:            val = left * right; break;
@@ -787,6 +853,8 @@ resolve_symbol_value (symp)
            case O_le:          val = left <= right ? ~ (offsetT) 0 : 0;
            case O_ge:          val = left >= right ? ~ (offsetT) 0 : 0;
            case O_gt:          val = left >  right ? ~ (offsetT) 0 : 0;
+           case O_logical_and: val = left && right; break;
+           case O_logical_or:  val = left || right; break;
            default:                    abort ();
            }
          S_SET_VALUE (symp,
@@ -851,7 +919,7 @@ dollar_label_defined (label)
   return 0;
 }                              /* dollar_label_defined() */
 
-static int 
+static long
 dollar_label_instance (label)
      long label;
 {
@@ -1190,7 +1258,22 @@ S_GET_VALUE (s)
   if (!s->sy_resolved && !s->sy_resolving && s->sy_value.X_op != O_constant)
     resolve_symbol_value (s);
   if (s->sy_value.X_op != O_constant)
-    as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s));
+    {
+      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;
 }
 
@@ -1237,6 +1320,13 @@ S_IS_EXTERNAL (s)
   return (flags & BSF_GLOBAL) != 0;
 }
 
+int
+S_IS_WEAK (s)
+     symbolS *s;
+{
+  return (s->bsym->flags & BSF_WEAK) != 0;
+}
+
 int
 S_IS_COMMON (s)
      symbolS *s;
@@ -1271,13 +1361,16 @@ S_IS_LOCAL (s)
   if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
     abort ();
 
+  if (bfd_get_section (s->bsym) == reg_section)
+    return 1;
+
   name = S_GET_NAME (s);
   return (name != NULL
          && ! S_IS_DEBUG (s)
          && (strchr (name, '\001')
              || strchr (name, '\002')
              || (! flag_keep_locals
-                 && (LOCAL_LABEL (name)
+                 && (bfd_is_local_label (stdoutput, s->bsym)
                      || (flag_mri
                          && name[0] == '?'
                          && name[1] == '?')))));
@@ -1324,7 +1417,10 @@ S_SET_EXTERNAL (s)
      symbolS *s;
 {
   if ((s->bsym->flags & BSF_WEAK) != 0)
-    as_warn ("%s already declared as weak", S_GET_NAME (s));
+    {
+      /* Let .weak override .global.  */
+      return;
+    }
   s->bsym->flags |= BSF_GLOBAL;
   s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
 }
@@ -1334,7 +1430,10 @@ S_CLEAR_EXTERNAL (s)
      symbolS *s;
 {
   if ((s->bsym->flags & BSF_WEAK) != 0)
-    as_warn ("%s already declared as weak", S_GET_NAME (s));
+    {
+      /* Let .weak override.  */
+      return;
+    }
   s->bsym->flags |= BSF_LOCAL;
   s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
 }
@@ -1343,8 +1442,6 @@ void
 S_SET_WEAK (s)
      symbolS *s;
 {
-  if ((s->bsym->flags & BSF_GLOBAL) != 0)
-    as_warn ("%s already declared as global", S_GET_NAME (s));
   s->bsym->flags |= BSF_WEAK;
   s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
 }
@@ -1375,6 +1472,7 @@ symbol_begin ()
   S_SET_SEGMENT (&abs_symbol, absolute_section);
 #endif
   abs_symbol.sy_value.X_op = O_constant;
+  abs_symbol.sy_frag = &zero_address_frag;
 
   if (LOCAL_LABELS_FB)
     fb_label_init ();
@@ -1393,9 +1491,6 @@ indent ()
 
 #endif
 
-void print_expr_1 PARAMS ((FILE *, expressionS *));
-void print_symbol_value_1 PARAMS ((FILE *, symbolS *));
-
 void
 print_symbol_value_1 (file, sym)
      FILE *file;
@@ -1539,6 +1634,12 @@ print_expr_1 (file, exp)
     case O_gt:
       fprintf (file, "gt");
       break;
+    case O_logical_and:
+      fprintf (file, "logical_and");
+      break;
+    case O_logical_or:
+      fprintf (file, "logical_or");
+      break;
     case O_add:
       indent_level++;
       fprintf (file, "add\n%*s<", indent_level * 4, "");
@@ -1570,4 +1671,11 @@ print_expr (exp)
   fprintf (stderr, "\n");
 }
 
+void
+symbol_print_statistics (file)
+     FILE *file;
+{
+  hash_print_statistics (file, "symbol table", sy_hash);
+}
+
 /* end of symbols.c */
This page took 0.029645 seconds and 4 git commands to generate.