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