2002-08-19 Elena Zannoni <ezannoni@redhat.com>
[deliverable/binutils-gdb.git] / gas / symbols.c
index 1f4e98816ecb182c91732f04e4163ff05d3843b5..63b4d47a2a459924874c983601250b53a0ed5170 100644 (file)
@@ -1,6 +1,6 @@
 /* symbols.c -symbol table-
    Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001
+   1999, 2000, 2001, 2002
    Free Software Foundation, Inc.
 
    This file is part of GAS, the GNU Assembler.
 
 /* #define DEBUG_SYMS / * to debug symbol list maintenance.  */
 
-#include <ctype.h>
-
 #include "as.h"
 
+#include "safe-ctype.h"
 #include "obstack.h"           /* For "symbols.h" */
 #include "subsegs.h"
 
@@ -124,11 +123,10 @@ save_symbol_name (name)
 
   if (! symbols_case_sensitive)
     {
-      unsigned char *s;
+      char *s;
 
-      for (s = (unsigned char *) ret; *s != '\0'; s++)
-       if (islower (*s))
-         *s = toupper (*s);
+      for (s = ret; *s != '\0'; s++)
+       *s = TOUPPER (*s);
     }
 
   return ret;
@@ -441,9 +439,9 @@ colon (sym_name)            /* Just seen "x:" - rattle symbols & frags.  */
 #ifdef BFD_ASSEMBLER
                  if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
 #endif
-                   sprintf(od_buf, "%d.%d.",
-                           S_GET_OTHER (symbolP),
-                           S_GET_DESC (symbolP));
+                   sprintf (od_buf, "%d.%d.",
+                            S_GET_OTHER (symbolP),
+                            S_GET_DESC (symbolP));
 #endif
                  as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
                            sym_name,
@@ -571,7 +569,7 @@ symbol_find_or_make (name)
 
 symbolS *
 symbol_make (name)
-     CONST char *name;
+     const char *name;
 {
   symbolS *symbolP;
 
@@ -591,7 +589,7 @@ symbol_make (name)
 
 symbolS *
 symbol_find (name)
-     CONST char *name;
+     const char *name;
 {
 #ifdef STRIP_UNDERSCORE
   return (symbol_find_base (name, 1));
@@ -600,9 +598,26 @@ symbol_find (name)
 #endif /* STRIP_UNDERSCORE */
 }
 
+symbolS *
+symbol_find_exact (name)
+     const char *name;
+{
+#ifdef BFD_ASSEMBLER
+  {
+    struct local_symbol *locsym;
+
+    locsym = (struct local_symbol *) hash_find (local_hash, name);
+    if (locsym != NULL)
+      return (symbolS *) locsym;
+  }
+#endif
+
+  return ((symbolS *) hash_find (sy_hash, name));
+}
+
 symbolS *
 symbol_find_base (name, strip_underscore)
-     CONST char *name;
+     const char *name;
      int strip_underscore;
 {
   if (strip_underscore && *name == '_')
@@ -630,24 +645,12 @@ symbol_find_base (name, strip_underscore)
 
       while ((c = *orig++) != '\0')
        {
-         if (islower (c))
-           c = toupper (c);
-         *copy++ = c;
+         *copy++ = TOUPPER (c);
        }
       *copy = '\0';
     }
 
-#ifdef BFD_ASSEMBLER
-  {
-    struct local_symbol *locsym;
-
-    locsym = (struct local_symbol *) hash_find (local_hash, name);
-    if (locsym != NULL)
-      return (symbolS *) locsym;
-  }
-#endif
-
-  return ((symbolS *) hash_find (sy_hash, name));
+  return symbol_find_exact (name);
 }
 
 /* Once upon a time, symbols were kept in a singly linked list.  At
@@ -836,7 +839,7 @@ resolve_symbol_value (symp)
      symbolS *symp;
 {
   int resolved;
-  valueT final_val;
+  valueT final_val = 0;
   segT final_seg;
 
 #ifdef BFD_ASSEMBLER
@@ -914,13 +917,16 @@ resolve_symbol_value (symp)
        case O_symbol:
        case O_symbol_rva:
          left = resolve_symbol_value (add_symbol);
-       do_symbol:
+         seg_left = S_GET_SEGMENT (add_symbol);
+         if (finalize_syms)
+           symp->sy_value.X_op_symbol = NULL;
 
+       do_symbol:
          if (symp->sy_mri_common)
            {
              /* This is a symbol inside an MRI common section.  The
-                 relocation routines are going to handle it specially.
-                 Don't change the value.  */
+                relocation routines are going to handle it specially.
+                Don't change the value.  */
              resolved = symbol_resolved_p (add_symbol);
              break;
            }
@@ -933,31 +939,51 @@ resolve_symbol_value (symp)
              copy_symbol_attributes (symp, 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 we have equated this symbol to an undefined or common
+            symbol, keep X_op set to O_symbol, and 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 (add_symbol) || S_IS_COMMON (add_symbol))
            {
              if (finalize_syms)
                {
-                 final_seg = S_GET_SEGMENT (add_symbol);
                  symp->sy_value.X_op = O_symbol;
                  symp->sy_value.X_add_symbol = add_symbol;
                  symp->sy_value.X_add_number = final_val;
+                 /* Use X_op_symbol as a flag.  */
+                 symp->sy_value.X_op_symbol = add_symbol;
+                 final_seg = seg_left;
                }
              final_val = 0;
              resolved = symbol_resolved_p (add_symbol);
              symp->sy_resolving = 0;
              goto exit_dont_set_value;
            }
+         else if (finalize_syms && final_seg == expr_section
+                  && seg_left != expr_section)
+           {
+             /* If the symbol is an expression symbol, do similarly
+                as for undefined and common syms above.  Handles
+                "sym +/- expr" where "expr" cannot be evaluated
+                immediately, and we want relocations to be against
+                "sym", eg. because it is weak.  */
+             symp->sy_value.X_op = O_symbol;
+             symp->sy_value.X_add_symbol = add_symbol;
+             symp->sy_value.X_add_number = final_val;
+             symp->sy_value.X_op_symbol = add_symbol;
+             final_seg = seg_left;
+             final_val += symp->sy_frag->fr_address + left;
+             resolved = symbol_resolved_p (add_symbol);
+             symp->sy_resolving = 0;
+             goto exit_dont_set_value;
+           }
          else
            {
              final_val += symp->sy_frag->fr_address + left;
              if (final_seg == expr_section || final_seg == undefined_section)
-               final_seg = S_GET_SEGMENT (add_symbol);
+               final_seg = seg_left;
            }
 
          resolved = symbol_resolved_p (add_symbol);
@@ -967,6 +993,7 @@ resolve_symbol_value (symp)
        case O_bit_not:
        case O_logical_not:
          left = resolve_symbol_value (add_symbol);
+         seg_left = S_GET_SEGMENT (add_symbol);
 
          if (op == O_uminus)
            left = -left;
@@ -977,7 +1004,7 @@ resolve_symbol_value (symp)
 
          final_val += left + symp->sy_frag->fr_address;
          if (final_seg == expr_section || final_seg == undefined_section)
-           final_seg = absolute_section;
+           final_seg = seg_left;
 
          resolved = symbol_resolved_p (add_symbol);
          break;
@@ -1008,43 +1035,54 @@ resolve_symbol_value (symp)
 
          /* Simplify addition or subtraction of a constant by folding the
             constant into X_add_number.  */
-         if (op == O_add || op == O_subtract)
+         if (op == O_add)
            {
              if (seg_right == absolute_section)
                {
-                 if (op == O_add)
-                   final_val += right;
-                 else
-                   final_val -= right;
-                 op = O_symbol;
-                 op_symbol = NULL;
+                 final_val += right;
                  goto do_symbol;
                }
-             else if (seg_left == absolute_section && op == O_add)
+             else if (seg_left == absolute_section)
                {
-                 op = O_symbol;
                  final_val += left;
                  add_symbol = op_symbol;
                  left = right;
-                 op_symbol = NULL;
+                 seg_left = seg_right;
+                 goto do_symbol;
+               }
+           }
+         else if (op == O_subtract)
+           {
+             if (seg_right == absolute_section)
+               {
+                 final_val -= right;
                  goto do_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.  */
-         /* Don't emit messages unless we're finalizing the symbol value,
+         /* Equality and non-equality tests are permitted on anything.
+            Subtraction, and other comparison operators are 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.
+
+            Don't emit messages unless we're finalizing the symbol value,
             otherwise we may get the same message multiple times.  */
-         if ((seg_left != absolute_section
-              || seg_right != absolute_section)
-             && (op != O_subtract
-                 || seg_left != seg_right
-                 || seg_left == undefined_section)
-             && finalize_syms)
+         if ((op == O_eq || op == O_ne)
+             || ((op == O_subtract
+                  || op == O_lt || op == O_le || op == O_ge || op == O_gt)
+                 && seg_left == seg_right
+                 && (seg_left != undefined_section
+                     || add_symbol == op_symbol))
+             || (seg_left == absolute_section
+                 && seg_right == absolute_section))
+           {
+             if (final_seg == expr_section || final_seg == undefined_section)
+               final_seg = absolute_section;
+           }
+         else if (finalize_syms)
            {
              char *file;
              unsigned int line;
@@ -1079,13 +1117,16 @@ resolve_symbol_value (symp)
                    as_bad (_("invalid section for operation setting `%s'"),
                            S_GET_NAME (symp));
                }
+             /* Prevent the error propagating.  */
+             if (final_seg == expr_section || final_seg == undefined_section)
+               final_seg = absolute_section;
            }
 
          /* Check for division by zero.  */
          if ((op == O_divide || op == O_modulus) && right == 0)
            {
              /* If seg_right is not absolute_section, then we've
-                 already issued a warning about using a bad symbol.  */
+                already issued a warning about using a bad symbol.  */
              if (seg_right == absolute_section && finalize_syms)
                {
                  char *file;
@@ -1114,8 +1155,15 @@ resolve_symbol_value (symp)
            case O_bit_and:             left &= right; break;
            case O_add:                 left += right; break;
            case O_subtract:            left -= right; break;
-           case O_eq:  left = left == right ? ~ (offsetT) 0 : 0; break;
-           case O_ne:  left = left != right ? ~ (offsetT) 0 : 0; break;
+           case O_eq:
+           case O_ne:
+             left = (left == right && seg_left == seg_right
+                     && (seg_left != undefined_section
+                         || add_symbol == op_symbol)
+                     ? ~ (offsetT) 0 : 0);
+             if (symp->sy_value.X_op == O_ne)
+               left = ~left;
+             break;
            case O_lt:  left = left <  right ? ~ (offsetT) 0 : 0; break;
            case O_le:  left = left <= right ? ~ (offsetT) 0 : 0; break;
            case O_ge:  left = left >= right ? ~ (offsetT) 0 : 0; break;
@@ -1127,7 +1175,15 @@ resolve_symbol_value (symp)
 
          final_val += symp->sy_frag->fr_address + left;
          if (final_seg == expr_section || final_seg == undefined_section)
-           final_seg = absolute_section;
+           {
+             if (seg_left == undefined_section
+                 || seg_right == undefined_section)
+               final_seg = undefined_section;
+             else if (seg_left == absolute_section)
+               final_seg = seg_right;
+             else
+               final_seg = seg_left;
+           }
          resolved = (symbol_resolved_p (add_symbol)
                      && symbol_resolved_p (op_symbol));
          break;
@@ -1547,7 +1603,7 @@ decode_local_label_name (s)
   if (s[index] != 'L')
     return s;
 
-  for (label_number = 0, p = s + index + 1; isdigit ((unsigned char) *p); ++p)
+  for (label_number = 0, p = s + index + 1; ISDIGIT (*p); ++p)
     label_number = (10 * label_number) + *p - '0';
 
   if (*p == DOLLAR_LABEL_CHAR)
@@ -1557,7 +1613,7 @@ decode_local_label_name (s)
   else
     return s;
 
-  for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
+  for (instance_number = 0, p++; ISDIGIT (*p); ++p)
     instance_number = (10 * instance_number) + *p - '0';
 
   message_format = _("\"%d\" (instance number %d of a %s label)");
@@ -1767,7 +1823,7 @@ S_IS_STABD (s)
   return S_GET_NAME (s) == 0;
 }
 
-CONST char *
+const char *
 S_GET_NAME (s)
      symbolS *s;
 {
@@ -1829,7 +1885,7 @@ S_SET_EXTERNAL (s)
     {
       char * file;
       unsigned int line;
-      
+
       /* Do not reassign section symbols.  */
       as_where (& file, & line);
       as_warn_where (file, line,
@@ -2146,6 +2202,24 @@ symbol_equated_p (s)
   return s->sy_value.X_op == O_symbol;
 }
 
+/* Return whether a symbol is equated to another symbol, and should be
+   treated specially when writing out relocs.  */
+
+int
+symbol_equated_reloc_p (s)
+     symbolS *s;
+{
+  if (LOCAL_SYMBOL_CHECK (s))
+    return 0;
+  /* X_op_symbol, normally not used for O_symbol, is set by
+     resolve_symbol_value to flag expression syms that have been
+     equated.  */
+  return (s->sy_value.X_op == O_symbol
+         && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
+             || ! S_IS_DEFINED (s)
+             || S_IS_COMMON (s)));
+}
+
 /* Return whether a symbol has a constant value.  */
 
 int
@@ -2331,7 +2405,7 @@ print_symbol_value_1 (file, sym)
       segT s = S_GET_SEGMENT (sym);
 
       if (s != undefined_section
-          && s != expr_section)
+         && s != expr_section)
        fprintf (file, " %lx", (long) S_GET_VALUE (sym));
     }
   else if (indent_level < max_indent_level
This page took 0.028771 seconds and 4 git commands to generate.