From patchwork Wed Dec 12 19:34:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Tietz X-Patchwork-Id: 205630 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id EEB0B2C007D for ; Thu, 13 Dec 2012 06:34:52 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1355945693; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:In-Reply-To:References:Date: Message-ID:Subject:From:To:Cc:Content-Type:Mailing-List: Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:Sender:Delivered-To; bh=moeoB6HeyPVl14B13uKtZq2mW7k=; b=ozNjHUY1vEWPm+8Vj36yzqEWmOBXuU6SGSb+j1PuYWPiLIk+LtDT3ggKJLeFUs jnEQu4uvXkLlW3WHCqA4UYhlITo4mCqVzWaf+gk60UET+iGGIcwKjqiXf0ijYm+6 NocK8YnJdA/d8Vdo18biqH3yzEyeqY5VJ7JsYp2g1sp7c= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:MIME-Version:Received:Received:In-Reply-To:References:Date:Message-ID:Subject:From:To:Cc:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=vnv8UpKvnYvbxx4oUkncBnCHoya515eJB5nuKnKA3uE1VWpK7kNIYBt7ovX9HJ n6YieAkmCtMjxszlRpgBrHKv5zh0XUp1vsK986XK2JGhm560IdJClAOom7BifjV7 kvw6EjmvzKJ184oHpXtbXSbOndCKugL5ZDO6K0B0lpEzE=; Received: (qmail 31162 invoked by alias); 12 Dec 2012 19:34:45 -0000 Received: (qmail 31112 invoked by uid 22791); 12 Dec 2012 19:34:44 -0000 X-SWARE-Spam-Status: No, hits=-4.3 required=5.0 tests=AWL, BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, DKIM_VALID, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, KHOP_RCVD_TRUST, KHOP_THREADED, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE X-Spam-Check-By: sourceware.org Received: from mail-ee0-f47.google.com (HELO mail-ee0-f47.google.com) (74.125.83.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 12 Dec 2012 19:34:40 +0000 Received: by mail-ee0-f47.google.com with SMTP id e51so739652eek.20 for ; Wed, 12 Dec 2012 11:34:38 -0800 (PST) MIME-Version: 1.0 Received: by 10.14.194.199 with SMTP id m47mr5395730een.11.1355340878839; Wed, 12 Dec 2012 11:34:38 -0800 (PST) Received: by 10.14.174.7 with HTTP; Wed, 12 Dec 2012 11:34:38 -0800 (PST) In-Reply-To: References: <50C89E96.4090603@redhat.com> Date: Wed, 12 Dec 2012 20:34:38 +0100 Message-ID: Subject: Re: [patch c/c++]: Fix for PR c/52991 issue about ignored packed-attribute for ms-structure-layout From: Kai Tietz To: Richard Biener Cc: Richard Henderson , GCC Patches X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, I added two testcases to this patch. So that we might detect regressions about this issue later more easily. 2012-12-12 Kai Tietz PR c/52991 * stor-layout.c (start_record_layout): Handle packed-attribute for ms-structure-layout. (update_alignment_for_field): Likewise. (place_field): Likewise. 2012-12-12 Kai Tietz PR c/52991 * gcc.dg/attr-ms_struct-packed2.c: New file. * gcc.dg/attr-ms_struct-packed3.c: New file. Ok for apply? Regards, Kai +} Index: gcc/gcc/stor-layout.c =================================================================== --- gcc.orig/gcc/stor-layout.c +++ gcc/gcc/stor-layout.c @@ -756,7 +756,10 @@ start_record_layout (tree t) /* If the type has a minimum specified alignment (via an attribute declaration, for example) use it -- otherwise, start with a one-byte alignment. */ - rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t)); + if (TYPE_PACKED (t)) + rli->record_align = BITS_PER_UNIT; + else + rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t)); rli->unpacked_align = rli->record_align; rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT); @@ -952,15 +955,20 @@ update_alignment_for_field (record_layou meaningless. */ if (targetm.ms_bitfield_layout_p (rli->t)) { + if (rli->t && TYPE_PACKED (rli->t) + && (is_bitfield || !DECL_PACKED (field) + || DECL_SIZE (field) == NULL_TREE + || !integer_zerop (DECL_SIZE (field)))) + desired_align = BITS_PER_UNIT; /* Here, the alignment of the underlying type of a bitfield can affect the alignment of a record; even a zero-sized field can do this. The alignment should be to the alignment of the type, except that for zero-size bitfields this only applies if there was an immediately prior, nonzero-size bitfield. (That's the way it is, experimentally.) */ - if ((!is_bitfield && !DECL_PACKED (field)) - || ((DECL_SIZE (field) == NULL_TREE - || !integer_zerop (DECL_SIZE (field))) + else if ((!is_bitfield && !DECL_PACKED (field)) + || ((DECL_SIZE (field) == NULL_TREE + || !integer_zerop (DECL_SIZE (field))) ? !DECL_PACKED (field) : (rli->prev_field && DECL_BIT_FIELD_TYPE (rli->prev_field) @@ -1414,7 +1422,12 @@ place_field (record_layout_info rli, tre } /* Now align (conventionally) for the new type. */ - type_align = TYPE_ALIGN (TREE_TYPE (field)); + if (!TYPE_PACKED (rli->t)) + { + type_align = TYPE_ALIGN (TREE_TYPE (field)); + if (DECL_PACKED (field)) + type_align = MIN (type_align, BITS_PER_UNIT); + } if (maximum_field_alignment != 0) type_align = MIN (type_align, maximum_field_alignment); Index: gcc/gcc/testsuite/gcc.dg/attr-ms_struct-packed2.c =================================================================== --- /dev/null +++ gcc/gcc/testsuite/gcc.dg/attr-ms_struct-packed2.c @@ -0,0 +1,31 @@ +/* Test for MS structure with packed attribute. */ +/* { dg-do run { target *-*-interix* *-*-mingw* *-*-cygwin* i?86-*-darwin* i?86-*-linux* x86_64-*-linux* } } +/* { dg-options "-std=gnu99" } */ + +extern void abort (); + +struct A { + short s; + struct { int i; } __attribute__((__ms_struct__)); +} __attribute__((__ms_struct__, __packed__)); + +struct B { + short s; + struct { int i; } __attribute__((__ms_struct__, __packed__)); +} __attribute__((__ms_struct__, __packed__)); + +struct C { + struct { int i; } __attribute__((__ms_struct__)); + short s; +} __attribute__((__ms_struct__, __packed__)); + +int +main (void) +{ + if (sizeof (struct C) != sizeof (struct B) + || sizeof (struct A) != sizeof (struct B) + || sizeof (struct B) != 6) + abort (); + + return 0;