cf24215
2014-01-16  Nick Clifton  <nickc@redhat.com>
cf24215
cf24215
	PR middle-end/28865
cf24215
	* varasm.c (output_constant): Return the number of bytes actually
cf24215
	emitted.
cf24215
	(output_constructor_array_range): Update the field size with the
cf24215
	number of bytes emitted by output_constant.
cf24215
	(output_constructor_regular_field): Likewise.  Also do not
cf24215
	complain if the total number of bytes emitted is now greater
cf24215
	than the expected fieldpos.
cf24215
	* output.h (output_constant): Update prototype and descriptive
cf24215
	comment.
cf24215
cf24215
	* gcc.c-torture/compile/pr28865.c: New.
cf24215
	* gcc.c-torture/execute/pr28865.c: New.
cf24215
cf24215
--- gcc/varasm.c	(revision 206660)
cf24215
+++ gcc/varasm.c	(revision 206661)
cf24215
@@ -4474,8 +4474,10 @@ static unsigned HOST_WIDE_INT
cf24215
    This includes the pseudo-op such as ".int" or ".byte", and a newline.
cf24215
    Assumes output_addressed_constants has been done on EXP already.
cf24215
 
cf24215
-   Generate exactly SIZE bytes of assembler data, padding at the end
cf24215
-   with zeros if necessary.  SIZE must always be specified.
cf24215
+   Generate at least SIZE bytes of assembler data, padding at the end
cf24215
+   with zeros if necessary.  SIZE must always be specified.  The returned
cf24215
+   value is the actual number of bytes of assembler data generated, which
cf24215
+   may be bigger than SIZE if the object contains a variable length field.
cf24215
 
cf24215
    SIZE is important for structure constructors,
cf24215
    since trailing members may have been omitted from the constructor.
cf24215
@@ -4490,14 +4492,14 @@ static unsigned HOST_WIDE_INT
cf24215
 
cf24215
    ALIGN is the alignment of the data in bits.  */
cf24215
 
cf24215
-void
cf24215
+unsigned HOST_WIDE_INT
cf24215
 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
cf24215
 {
cf24215
   enum tree_code code;
cf24215
   unsigned HOST_WIDE_INT thissize;
cf24215
 
cf24215
   if (size == 0 || flag_syntax_only)
cf24215
-    return;
cf24215
+    return size;
cf24215
 
cf24215
   /* See if we're trying to initialize a pointer in a non-default mode
cf24215
      to the address of some declaration somewhere.  If the target says
cf24215
@@ -4562,7 +4564,7 @@ output_constant (tree exp, unsigned HOST
cf24215
       && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
cf24215
     {
cf24215
       assemble_zeros (size);
cf24215
-      return;
cf24215
+      return size;
cf24215
     }
cf24215
 
cf24215
   if (TREE_CODE (exp) == FDESC_EXPR)
cf24215
@@ -4574,7 +4576,7 @@ output_constant (tree exp, unsigned HOST
cf24215
 #else
cf24215
       gcc_unreachable ();
cf24215
 #endif
cf24215
-      return;
cf24215
+      return size;
cf24215
     }
cf24215
 
cf24215
   /* Now output the underlying data.  If we've handling the padding, return.
cf24215
@@ -4612,8 +4614,7 @@ output_constant (tree exp, unsigned HOST
cf24215
       switch (TREE_CODE (exp))
cf24215
 	{
cf24215
 	case CONSTRUCTOR:
cf24215
-	    output_constructor (exp, size, align, NULL);
cf24215
-	  return;
cf24215
+	  return output_constructor (exp, size, align, NULL);
cf24215
 	case STRING_CST:
cf24215
 	  thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
cf24215
 			  size);
cf24215
@@ -4648,11 +4649,10 @@ output_constant (tree exp, unsigned HOST
cf24215
     case RECORD_TYPE:
cf24215
     case UNION_TYPE:
cf24215
       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
cf24215
-      output_constructor (exp, size, align, NULL);
cf24215
-      return;
cf24215
+      return output_constructor (exp, size, align, NULL);
cf24215
 
cf24215
     case ERROR_MARK:
cf24215
-      return;
cf24215
+      return 0;
cf24215
 
cf24215
     default:
cf24215
       gcc_unreachable ();
cf24215
@@ -4660,6 +4660,8 @@ output_constant (tree exp, unsigned HOST
cf24215
 
cf24215
   if (size > thissize)
cf24215
     assemble_zeros (size - thissize);
cf24215
+
cf24215
+  return size;
cf24215
 }
cf24215
 
cf24215
 
cf24215
@@ -4759,7 +4761,7 @@ output_constructor_array_range (oc_local
cf24215
       if (local->val == NULL_TREE)
cf24215
 	assemble_zeros (fieldsize);
cf24215
       else
cf24215
-	output_constant (local->val, fieldsize, align2);
cf24215
+	fieldsize = output_constant (local->val, fieldsize, align2);
cf24215
 
cf24215
       /* Count its size.  */
cf24215
       local->total_bytes += fieldsize;
cf24215
@@ -4808,9 +4810,8 @@ output_constructor_regular_field (oc_loc
cf24215
      Note no alignment needed in an array, since that is guaranteed
cf24215
      if each element has the proper size.  */
cf24215
   if ((local->field != NULL_TREE || local->index != NULL_TREE)
cf24215
-      && fieldpos != local->total_bytes)
cf24215
+      && fieldpos > local->total_bytes)
cf24215
     {
cf24215
-      gcc_assert (fieldpos >= local->total_bytes);
cf24215
       assemble_zeros (fieldpos - local->total_bytes);
cf24215
       local->total_bytes = fieldpos;
cf24215
     }
cf24215
@@ -4847,7 +4848,7 @@ output_constructor_regular_field (oc_loc
cf24215
   if (local->val == NULL_TREE)
cf24215
     assemble_zeros (fieldsize);
cf24215
   else
cf24215
-    output_constant (local->val, fieldsize, align2);
cf24215
+    fieldsize = output_constant (local->val, fieldsize, align2);
cf24215
 
cf24215
   /* Count its size.  */
cf24215
   local->total_bytes += fieldsize;
cf24215
--- gcc/output.h	(revision 206660)
cf24215
+++ gcc/output.h	(revision 206661)
cf24215
@@ -294,11 +294,13 @@ extern void output_quoted_string (FILE *
cf24215
    This includes the pseudo-op such as ".int" or ".byte", and a newline.
cf24215
    Assumes output_addressed_constants has been done on EXP already.
cf24215
 
cf24215
-   Generate exactly SIZE bytes of assembler data, padding at the end
cf24215
-   with zeros if necessary.  SIZE must always be specified.
cf24215
+   Generate at least SIZE bytes of assembler data, padding at the end
cf24215
+   with zeros if necessary.  SIZE must always be specified.  The returned
cf24215
+   value is the actual number of bytes of assembler data generated, which
cf24215
+   may be bigger than SIZE if the object contains a variable length field.
cf24215
 
cf24215
    ALIGN is the alignment in bits that may be assumed for the data.  */
cf24215
-extern void output_constant (tree, unsigned HOST_WIDE_INT, unsigned int);
cf24215
+extern unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT, unsigned int);
cf24215
 
cf24215
 /* When outputting delayed branch sequences, this rtx holds the
cf24215
    sequence being output.  It is null when no delayed branch
cf24215
--- gcc/testsuite/gcc.c-torture/execute/pr28865.c	(revision 0)
cf24215
+++ gcc/testsuite/gcc.c-torture/execute/pr28865.c	(revision 206661)
cf24215
@@ -0,0 +1,21 @@
cf24215
+struct A { int a; char b[]; };
cf24215
+union B { struct A a; char b[sizeof (struct A) + 31]; };
cf24215
+union B b = { { 1, "123456789012345678901234567890" } };
cf24215
+union B c = { { 2, "123456789012345678901234567890" } };
cf24215
+
cf24215
+__attribute__((noinline, noclone)) void
cf24215
+foo (int *x[2])
cf24215
+{
cf24215
+  x[0] = &b.a.a;
cf24215
+  x[1] = &c.a.a;
cf24215
+}
cf24215
+
cf24215
+int
cf24215
+main ()
cf24215
+{
cf24215
+  int *x[2];
cf24215
+  foo (x);
cf24215
+  if (*x[0] != 1 || *x[1] != 2)
cf24215
+    __builtin_abort ();
cf24215
+  return 0;
cf24215
+}
cf24215
--- gcc/testsuite/gcc.c-torture/compile/pr28865.c	(revision 0)
cf24215
+++ gcc/testsuite/gcc.c-torture/compile/pr28865.c	(revision 206661)
cf24215
@@ -0,0 +1,16 @@
cf24215
+struct var_len
cf24215
+{
cf24215
+  int field1;
cf24215
+  const char field2[];
cf24215
+};
cf24215
+
cf24215
+/* Note - strictly speaking this array declaration is illegal
cf24215
+   since each element has a variable length.  GCC allows it
cf24215
+   (for the moment) because it is used in existing code, such
cf24215
+   as glibc.  */
cf24215
+static const struct var_len var_array[] = 
cf24215
+{
cf24215
+  { 1, "Long exposure noise reduction" },
cf24215
+  { 2, "Shutter/AE lock buttons" },
cf24215
+  { 3, "Mirror lockup" }
cf24215
+};