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