Struct/Union alignement problem with Crossworks
2005-09-20 by nourson54
Yahoo Groups archive
Index last updated: 2026-04-28 23:31 UTC
Thread
2005-09-20 by nourson54
Hello, I'm trying a fat16 library that use an union for storing the bootsector data, and it seems the compiler doesn't handle correctly the width of types in memory. So I searched some help on crossworks (gcc) compiler and found that I have to use this attribute with my concerned unions : __attribute__ ((__packed__)) But if the compiler doesn't shout at me when compiling , it seems it has no effect : my union is badly aligned. Have any idea ?
2005-09-20 by Paul Curtis
If you have a union of two (or more) structures, those structures must also have a packed attribute. -- Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk CrossWorks for MSP430, ARM, AVR and now MAXQ processors
> -----Original Message----- > From: nourson54 [mailto:nourson54@...] > Sent: 20 September 2005 14:54 > To: lpc2000@yahoogroups.com > Subject: [lpc2000] Struct/Union alignement problem with Crossworks > > Hello, > > I'm trying a fat16 library that use an union for storing the > bootsector data, and it seems the compiler doesn't handle > correctly the width of types in memory. > So I searched some help on crossworks (gcc) compiler and > found that I have to use this attribute with my concerned unions : > __attribute__ ((__packed__)) > > But if the compiler doesn't shout at me when compiling , it > seems it has no effect : my union is badly aligned. > > Have any idea ? > > > > > > ------------------------ Yahoo! Groups Sponsor > --------------------~--> Fair play? Video games influencing > politics. Click and talk back! > http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/dN_tlB/TM > -------------------------------------------------------------- > ------~-> > > > Yahoo! Groups Links > > > > > > > >
2005-09-20 by Angel Sanchez
In a similar situation I use: #pragma pack (2) ... Structures, etc #pragma pack () and this work (align at word boundaries) Regards Angel Sanchez --- In lpc2000@yahoogroups.com, "nourson54" <nourson54@y...> wrote: > Hello, > > I'm trying a fat16 library that use an union for storing the > bootsector data, and it seems the compiler doesn't handle correctly > the width of types in memory. > So I searched some help on crossworks (gcc) compiler and found that I
> have to use this attribute with my concerned unions : > __attribute__ ((__packed__)) > > But if the compiler doesn't shout at me when compiling , it seems it > has no effect : my union is badly aligned. > > Have any idea ?
2005-09-20 by nourson54
I tried to set the attibute on all structs/unions but at compiling time , gcc told : "`__packed__' attribute ignored" for each attribute ... --- In lpc2000@yahoogroups.com, "Paul Curtis" <plc@r...> wrote:
> If you have a union of two (or more) structures, those structures must > also have a packed attribute. > > -- > Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk > CrossWorks for MSP430, ARM, AVR and now MAXQ processors > > > -----Original Message----- > > From: nourson54 [mailto:nourson54@y...] > > Sent: 20 September 2005 14:54 > > To: lpc2000@yahoogroups.com > > Subject: [lpc2000] Struct/Union alignement problem with Crossworks > > > > Hello, > > > > I'm trying a fat16 library that use an union for storing the > > bootsector data, and it seems the compiler doesn't handle > > correctly the width of types in memory. > > So I searched some help on crossworks (gcc) compiler and > > found that I have to use this attribute with my concerned unions : > > __attribute__ ((__packed__)) > > > > But if the compiler doesn't shout at me when compiling , it > > seems it has no effect : my union is badly aligned. > > > > Have any idea ? > > > > > > > > > > > > ------------------------ Yahoo! Groups Sponsor > > --------------------~--> Fair play? Video games influencing > > politics. Click and talk back! > > http://us.click.yahoo.com/T8sf5C/tzNLAA/TtwFAA/dN_tlB/TM > > -------------------------------------------------------------- > > ------~-> > > > > > > Yahoo! Groups Links > > > > > > > > > > > > > > > >
2005-09-20 by nourson54
here's a part of code :
typedef struct
{
uint8_t bsjmpBoot[3];
char bsOEMName[8];
uint16_t bsBytesPerSec;
...
}BootSector;
and a buffer FAT16_buffer[512] where I put data frome SD card in.
for example , if I declare a Bootsector struct at same address of
FAT16_Buffer, and read the first sector of my SDcard, I coul read
directly the field of my struct.
bsjmpBoot is 3 bytes and is OK
BsOEMName is 8 bytes and OK
bsByteBytesPerSec is 2 bytes is not OK because bsOEMBoot+bsOEMName =
11 bytes : 1 bytes is 'filled' between bsOEMName and bsBytesPerSec.
So data becomes buggy.
with the #pramga pack (x) , Can I 'byte align' each field of my struct ?
--- In lpc2000@yahoogroups.com, "Angel Sanchez" <angelsp@a...> wrote:> In a similar situation I use: > > #pragma pack (2) > > ... > Structures, etc > > #pragma pack () > > and this work (align at word boundaries) > > Regards > Angel Sanchez > > > --- In lpc2000@yahoogroups.com, "nourson54" <nourson54@y...> wrote: > > Hello, > > > > I'm trying a fat16 library that use an union for storing the > > bootsector data, and it seems the compiler doesn't handle correctly > > the width of types in memory. > > So I searched some help on crossworks (gcc) compiler and found that > I > > have to use this attribute with my concerned unions : > > __attribute__ ((__packed__)) > > > > But if the compiler doesn't shout at me when compiling , it seems it > > has no effect : my union is badly aligned. > > > > Have any idea ?
2005-09-20 by Paul Curtis
You would need:
typedef struct
{
uint8_t bsjmpBoot[3];
char bsOEMName[8];
uint16_t bsBytesPerSec;
...
} __attribute__((packed)) BootSector;
Given...
BootSector bs;
...then...
BS.bsBytesPerSec = 1
...generates...
BS.bsBytesPerSec = 1;
E59F200C ldr r2, [pc, #12]
E3843001 orr r3, r4, #0x00000001
E5C2300B strb r3, [r2, #11]
E5C2400C strb r4, [r2, #12]
So, bsBytesPerSec is accessed as bytes at offsets 11 and 12, which is
just what you wanted, isn't it?
--
Paul Curtis, Rowley Associates Ltd http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and now MAXQ processors> -----Original Message-----
> From: nourson54 [mailto:nourson54@...]
> Sent: 20 September 2005 16:31
> To: lpc2000@yahoogroups.com
> Subject: [lpc2000] Re: Struct/Union alignement problem with Crossworks
>
> here's a part of code :
> typedef struct
> {
> uint8_t bsjmpBoot[3];
> char bsOEMName[8];
> uint16_t bsBytesPerSec;
> ...
> }BootSector;
>
> and a buffer FAT16_buffer[512] where I put data frome SD card in.
>
> for example , if I declare a Bootsector struct at same
> address of FAT16_Buffer, and read the first sector of my
> SDcard, I coul read directly the field of my struct.
>
> bsjmpBoot is 3 bytes and is OK
> BsOEMName is 8 bytes and OK
> bsByteBytesPerSec is 2 bytes is not OK because bsOEMBoot+bsOEMName =
> 11 bytes : 1 bytes is 'filled' between bsOEMName and bsBytesPerSec.
> So data becomes buggy.
>
>
> with the #pramga pack (x) , Can I 'byte align' each field of
> my struct ?
>
>
>
>
>
>
>
>
>
> --- In lpc2000@yahoogroups.com, "Angel Sanchez" <angelsp@a...> wrote:
> > In a similar situation I use:
> >
> > #pragma pack (2)
> >
> > ...
> > Structures, etc
> >
> > #pragma pack ()
> >
> > and this work (align at word boundaries)
> >
> > Regards
> > Angel Sanchez
> >
> >
> > --- In lpc2000@yahoogroups.com, "nourson54" <nourson54@y...> wrote:
> > > Hello,
> > >
> > > I'm trying a fat16 library that use an union for storing the
> > > bootsector data, and it seems the compiler doesn't handle
> correctly
> > > the width of types in memory.
> > > So I searched some help on crossworks (gcc) compiler and
> found that
> > I
> > > have to use this attribute with my concerned unions :
> > > __attribute__ ((__packed__))
> > >
> > > But if the compiler doesn't shout at me when compiling ,
> it seems it
> > > has no effect : my union is badly aligned.
> > >
> > > Have any idea ?
>
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~--> Most low income households are not
> online. Help bridge the digital divide today!
> http://us.click.yahoo.com/cd_AJB/QnQLAA/TtwFAA/dN_tlB/TM
> --------------------------------------------------------------
> ------~->
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>