gas: consistently emit diagnostics for non-zero data emission to .bss/.struct
[binutils-gdb.git] / gas / read.c
index 172d75f29297f25769956afb11a39da7f2df7d1e..b25ab8dee1a4ef1e29504b4fafd1ac328553b749 100644 (file)
@@ -1337,6 +1337,14 @@ convert_to_bignum (expressionS *exp, int sign)
   exp->X_add_number = i;
 }
 
+static bfd_boolean
+in_bss (void)
+{
+  flagword flags = bfd_get_section_flags (stdoutput, now_seg);
+
+  return (flags & SEC_ALLOC) && !(flags & (SEC_LOAD | SEC_HAS_CONTENTS));
+}
+
 /* For most MRI pseudo-ops, the line actually ends at the first
    nonquoted space.  This function looks for that point, stuffs a null
    in, and sets *STOPCP to the character that used to be there, and
@@ -1402,13 +1410,17 @@ s_abort (int ignore ATTRIBUTE_UNUSED)
 static void
 do_align (int n, char *fill, int len, int max)
 {
-  if (now_seg == absolute_section)
+  if (now_seg == absolute_section || in_bss ())
     {
       if (fill != NULL)
        while (len-- > 0)
          if (*fill++ != '\0')
            {
-             as_warn (_("ignoring fill value in absolute section"));
+             if (now_seg == absolute_section)
+               as_warn (_("ignoring fill value in absolute section"));
+             else
+               as_warn (_("ignoring fill value in section `%s'"),
+                        segment_name (now_seg));
              break;
            }
       fill = NULL;
@@ -2207,6 +2219,20 @@ s_fill (int ignore ATTRIBUTE_UNUSED)
 
   if (size && !need_pass_2)
     {
+      if (now_seg == absolute_section)
+       {
+         if (rep_exp.X_op != O_constant)
+           as_bad (_("non-constant fill count for absolute section"));
+         else if (fill && rep_exp.X_add_number != 0)
+           as_bad (_("attempt to fill absolute section with non-zero value"));
+         abs_section_offset += rep_exp.X_add_number * size;
+       }
+      else if (fill
+              && (rep_exp.X_op != O_constant || rep_exp.X_add_number != 0)
+              && in_bss ())
+       as_bad (_("attempt to fill section `%s' with non-zero value"),
+               segment_name (now_seg));
+
       if (rep_exp.X_op == O_constant)
        {
          p = frag_var (rs_fill, (int) size, (int) size,
@@ -2777,6 +2803,10 @@ do_org (segT segment, expressionS *exp, int fill)
       symbolS *sym = exp->X_add_symbol;
       offsetT off = exp->X_add_number * OCTETS_PER_BYTE;
 
+      if (fill && in_bss ())
+       as_warn (_("ignoring fill value in section `%s'"),
+                segment_name (now_seg));
+
       if (exp->X_op != O_constant && exp->X_op != O_symbol)
        {
          /* Handle complex expressions.  */
@@ -3331,10 +3361,11 @@ s_space (int mult)
       val.X_add_number = 0;
     }
 
-  if (val.X_op != O_constant
-      || val.X_add_number < - 0x80
-      || val.X_add_number > 0xff
-      || (mult != 0 && mult != 1 && val.X_add_number != 0))
+  if ((val.X_op != O_constant
+       || val.X_add_number < - 0x80
+       || val.X_add_number > 0xff
+       || (mult != 0 && mult != 1 && val.X_add_number != 0))
+      && (now_seg != absolute_section && !in_bss ()))
     {
       resolve_expression (&exp);
       if (exp.X_op != O_constant)
@@ -3375,6 +3406,8 @@ s_space (int mult)
          /* If we are in the absolute section, just bump the offset.  */
          if (now_seg == absolute_section)
            {
+             if (val.X_op != O_constant || val.X_add_number != 0)
+               as_warn (_("ignoring fill value in absolute section"));
              abs_section_offset += repeat;
              goto getout;
            }
@@ -3412,7 +3445,10 @@ s_space (int mult)
                          make_expr_symbol (&exp), (offsetT) 0, (char *) 0);
        }
 
-      if (p)
+      if ((val.X_op != O_constant || val.X_add_number != 0) && in_bss ())
+       as_warn (_("ignoring fill value in section `%s'"),
+                segment_name (now_seg));
+      else if (p)
        *p = val.X_add_number;
     }
 
@@ -4213,15 +4249,6 @@ emit_expr_with_reloc (expressionS *exp,
 
   op = exp->X_op;
 
-  /* Allow `.word 0' in the absolute section.  */
-  if (now_seg == absolute_section)
-    {
-      if (op != O_constant || exp->X_add_number != 0)
-       as_bad (_("attempt to store value in absolute section"));
-      abs_section_offset += nbytes;
-      return;
-    }
-
   /* Handle a negative bignum.  */
   if (op == O_uminus
       && exp->X_add_number == 0
@@ -4271,6 +4298,20 @@ emit_expr_with_reloc (expressionS *exp,
       op = O_constant;
     }
 
+  /* Allow `.word 0' in the absolute section.  */
+  if (now_seg == absolute_section)
+    {
+      if (op != O_constant || exp->X_add_number != 0)
+       as_bad (_("attempt to store value in absolute section"));
+      abs_section_offset += nbytes;
+      return;
+    }
+
+  /* Allow `.word 0' in BSS style sections.  */
+  if ((op != O_constant || exp->X_add_number != 0) && in_bss ())
+    as_bad (_("attempt to store non-zero value in section `%s'"),
+           segment_name (now_seg));
+
   p = frag_more ((int) nbytes);
 
   if (reloc != TC_PARSE_CONS_RETURN_NONE)
@@ -4854,6 +4895,21 @@ float_cons (/* Clobbers input_line-pointer, checks end-of-line.  */
       return;
     }
 
+  if (now_seg == absolute_section)
+    {
+      as_bad (_("attempt to store float in absolute section"));
+      ignore_rest_of_line ();
+      return;
+    }
+
+  if (in_bss ())
+    {
+      as_bad (_("attempt to store float in section `%s'"),
+             segment_name (now_seg));
+      ignore_rest_of_line ();
+      return;
+    }
+
 #ifdef md_flush_pending_output
   md_flush_pending_output ();
 #endif
@@ -5186,6 +5242,18 @@ emit_leb128_expr (expressionS *exp, int sign)
       op = O_big;
     }
 
+  if (now_seg == absolute_section)
+    {
+      if (op != O_constant || exp->X_add_number != 0)
+       as_bad (_("attempt to store value in absolute section"));
+      abs_section_offset++;
+      return;
+    }
+
+  if ((op != O_constant || exp->X_add_number != 0) && in_bss ())
+    as_bad (_("attempt to store non-zero value in section `%s'"),
+           segment_name (now_seg));
+
   /* Let check_eh_frame know that data is being emitted.  nbytes == -1 is
      a signal that this is leb128 data.  It shouldn't optimize this away.  */
   nbytes = (unsigned int) -1;
@@ -5255,6 +5323,10 @@ s_leb128 (int sign)
 static void
 stringer_append_char (int c, int bitsize)
 {
+  if (c && in_bss ())
+    as_bad (_("attempt to store non-empty string in section `%s'"),
+           segment_name (now_seg));
+
   if (!target_big_endian)
     FRAG_APPEND_1_CHAR (c);