Bug 11885 - [3.3 Regression] Problem with bitfields in packed structs
Summary: [3.3 Regression] Problem with bitfields in packed structs
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.3.1
: P1 critical
Target Milestone: 3.3.2
Assignee: Jason Merrill
Keywords: wrong-code
Depends on:
Reported: 2003-08-11 21:09 UTC by Stan Rost
Modified: 2003-10-14 18:51 UTC (History)
3 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu avr-unknown-unknown
Known to work:
Known to fail:
Last reconfirmed: 2003-08-11 21:14:39


Note You need to log in before you can comment on or make changes to this bug.
Description Stan Rost 2003-08-11 21:09:52 UTC

I have been hacking for Berkley Motes using nesC, which produces code later
compiled by AVR-GCC.  There is a problem with bitfields within packed
structures, which is also reproducible on the native platform (i686-pc-linux-gnu).

The bug is illustrated by the following simple program:

include <stdio.h>
#include <stdint.h>

typedef struct {
    uint8_t flag1:2;
    uint8_t flag2:1;
    uint8_t flag3:1;
    uint8_t flag4;

} __attribute__ ((packed)) MyType;

int main(void) {

    MyType a;
    MyType *b = &a;

    b->flag1 = 0;
    b->flag2 = 0;
    b->flag3 = 0;

    b->flag4 = 0;

    if (b->flag1)



This is clearly an error because flag4 should be allocated its own byte
and not packed into the byte with other variables.

Please look into this, as the current methods of circumvention force me
to pad the structures with "reserved" bitfields.


Stan Rost
Comment 1 Andrew Pinski 2003-08-11 21:14:39 UTC
I can reproduce this in 3.3.1 (20030707).  It is already fixed on the mainline and also in 3.2.3 so 
making this a regression.
Comment 2 Andrew Pinski 2003-08-11 21:21:43 UTC
Can you try the patch in PR 11662?
Comment 3 Stan Rost 2003-08-11 23:27:08 UTC
Just tried the patch, recompiled and reinstalled, but the patch
you suggested did not fix the problem.

Additionally, I double-checked everything so that the last sentence
is not in error.
Comment 4 Andrew Pinski 2003-08-11 23:40:37 UTC
Thanks for trying this patch, this helps us figure out which patch fixes this on the mainline.
 can you run your regression finder on this one as it looks like a bad regression (which is already 
fixed on the mainline) as prevents any one to use reserved fields in packed structors with bitfields.
Comment 5 janis187 2003-08-12 00:02:49 UTC
Subject: Re:  [3.3 Regression] Problem with bitfields in packed structs

This looked like an interesting bug so I started a hunt on the 3.3
branch a couple of hours ago, and it's now down to a single day
(2003-03-31).  When I identify where it broke on the branch I'll
check to see if it was also broken on mainline, and then what fixed
it on mainline.
Comment 6 janis187 2003-08-12 16:51:25 UTC
The regression in PR 11885 was introduced or exposed on the 3.3 branch
and mainline by this patch:

--- gcc/gcc/ChangeLog ---

2003-03-31  Jason Merrill  <jason@redhat.com>

        * stor-layout.c (update_alignment_for_field): Respect
        DECL_USER_ALIGN for zero-length bitfields, too.
        * c-decl.c (finish_struct): Don't set DECL_ALIGN for normal

--- gcc/gcc/cp/ChangeLog ---

2003-03-31  Jason Merrill  <jason@redhat.com>

        * class.c (check_field_decl): Don't set DECL_ALIGN.

The test case passes for mainline after this large patch:

--- gcc/gcc/ChangeLog ---

2003-04-03  Jason Merrill  <jason@redhat.com>

        * stor-layout.c (do_type_align): New fn, split out from...
        (layout_decl): ...here.  Do all alignment calculations for
        FIELD_DECLs here.
        (update_alignment_for_field): Not here.
        (start_record_layout, debug_rli): Remove unpadded_align.
        * tree.h (struct record_layout_info_s): Remove unpadded_align.
        * c-decl.c (finish_enum): Don't set DECL_SIZE, DECL_ALIGN
        or DECL_MODE on the CONST_DECLs.
        (finish_struct): Don't mess with DECL_ALIGN.

--- gcc/gcc/cp/ChangeLog ---

2003-04-03  Jason Merrill  <jason@redhat.com>

        * class.c (build_vtable): Set DECL_ALIGN here.
        (get_vtable_decl): Not here.
        (layout_vtable_decl): Or here.
        (create_vtable_ptr): Or here.
        (layout_class_type): Or here.
        (check_bitfield_decl): Don't mess with field alignment.

The hunts took place on i686-pc-linux-gnu using the submitter's test case.
Comment 7 CVS Commits 2003-10-14 18:43:13 UTC
Subject: Bug 11885

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	jason@gcc.gnu.org	2003-10-14 18:43:09

Modified files:
	gcc            : ChangeLog stor-layout.c 

Log message:
	PR c/11885
	* stor-layout.c (update_alignment_for_field): Packed non-bit-fields
	get byte alignment.


Comment 8 Andrew Pinski 2003-10-14 18:51:01 UTC
Fixed for 3.3.2.