Hi, 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; b->flag4++; if (b->flag1) printf("Set\n"); else printf("Clear\n"); } -------------------------------------------------------------- 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. Respectfully, Stan Rost
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.
Can you try the patch in PR 11662? <http://gcc.gnu.org/bugzilla/attachment.cgi?id=4479&action=view>
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.
Thanks for trying this patch, this helps us figure out which patch fixes this on the mainline. Janis, 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.
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.
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 fields. --- 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.
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. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.780&r2=1.16114.2.781 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/stor-layout.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.134.2.4&r2=1.134.2.5
Fixed for 3.3.2.