gcc - alignment

gcc - alignment

Post by Frank Popp » Thu, 23 Dec 1999 04:00:00



Hi all,

I'm porting an old DOS app now to linux, at least some small exe's, for
the use as cgi-scripts to access an some database files. The dos-exes
where compiled with an 1 byte alignment, and now i need to do this in my
gcc-linux program also. the db-structure used is:

typedef struct {
        /*  0  */ UCHAR name[53] __attribute__ ((aligned (1)));
        /*  53 */ ULONG number __attribute__ ((aligned (1)));
        /*  57 */ UCHAR name2[53] __attribute__ ((aligned (1)));
        } mystruct;

But the align-attribute doesn't give the desired results, it will be
aligned on 2 or 4 byte boundaries, so that from name2 always the first
three chars are missing.

Can anyone hlep to acomplish that?

Thanks

 
 
 

gcc - alignment

Post by Anthony Gree » Thu, 23 Dec 1999 04:00:00



> I'm porting an old DOS app now to linux, at least some small exe's, for
> the use as cgi-scripts to access an some database files. The dos-exes
> where compiled with an 1 byte alignment, and now i need to do this in my
> gcc-linux program also. the db-structure used is:

> typedef struct {
>    /*  0  */ UCHAR name[53] __attribute__ ((aligned (1)));
>    /*  53 */ ULONG number __attribute__ ((aligned (1)));
>    /*  57 */ UCHAR name2[53] __attribute__ ((aligned (1)));
>    } mystruct;

> But the align-attribute doesn't give the desired results, it will be
> aligned on 2 or 4 byte boundaries, so that from name2 always the first
> three chars are missing.

Use __attribute__((packed)) instead.  From the GCC manual:



AG

--
Anthony Green                                               Cygnus Solutions
                                                       Sunnyvale, California

 
 
 

gcc - alignment

Post by Norm Dresne » Fri, 24 Dec 1999 04:00:00


#pragma pack(1)
causes no added space for alignment

        Norm






> > > I'm porting an old DOS app now to linux, at least some small exe's,
for
> > > the use as cgi-scripts to access an some database files. The dos-exes
> > > where compiled with an 1 byte alignment, and now i need to do this in
> my
> > > gcc-linux program also. the db-structure used is:

> > > typedef struct {
> > >  /*  0  */ UCHAR name[53] __attribute__ ((aligned (1)));
> > >  /*  53 */ ULONG number __attribute__ ((aligned (1)));
> > >  /*  57 */ UCHAR name2[53] __attribute__ ((aligned (1)));
> > >  } mystruct;

> > > But the align-attribute doesn't give the desired results, it will be
> > > aligned on 2 or 4 byte boundaries, so that from name2 always the
first
> > > three chars are missing.

> #pragma    pack(n)
> changes the alignment to n=2,4 (and maybe 8).
> #pragma    pack()
> resets it to the default

>    Norm

 
 
 

gcc - alignment

Post by Norm Dresne » Fri, 24 Dec 1999 04:00:00





> > I'm porting an old DOS app now to linux, at least some small exe's, for
> > the use as cgi-scripts to access an some database files. The dos-exes
> > where compiled with an 1 byte alignment, and now i need to do this in
my
> > gcc-linux program also. the db-structure used is:

> > typedef struct {
> >       /*  0  */ UCHAR name[53] __attribute__ ((aligned (1)));
> >       /*  53 */ ULONG number __attribute__ ((aligned (1)));
> >       /*  57 */ UCHAR name2[53] __attribute__ ((aligned (1)));
> >       } mystruct;

> > But the align-attribute doesn't give the desired results, it will be
> > aligned on 2 or 4 byte boundaries, so that from name2 always the first
> > three chars are missing.

#pragma pack(n)
changes the alignment to n=2,4 (and maybe 8).
#pragma pack()
resets it to the default

        Norm

 
 
 

gcc - alignment

Post by wim delvau » Wed, 29 Dec 1999 04:00:00


I Wonder if the ULONG will be usable in the application to perform
calculations (if at all necessary)

> #pragma pack(1)
> causes no added space for alignment

>         Norm






> > > > I'm porting an old DOS app now to linux, at least some small exe's,
> for
> > > > the use as cgi-scripts to access an some database files. The dos-exes
> > > > where compiled with an 1 byte alignment, and now i need to do this in
> > my
> > > > gcc-linux program also. the db-structure used is:

> > > > typedef struct {
> > > >   /*  0  */ UCHAR name[53] __attribute__ ((aligned (1)));
> > > >   /*  53 */ ULONG number __attribute__ ((aligned (1)));
> > > >   /*  57 */ UCHAR name2[53] __attribute__ ((aligned (1)));
> > > >   } mystruct;

> > > > But the align-attribute doesn't give the desired results, it will be
> > > > aligned on 2 or 4 byte boundaries, so that from name2 always the
> first
> > > > three chars are missing.

> > #pragma       pack(n)
> > changes the alignment to n=2,4 (and maybe 8).
> > #pragma       pack()
> > resets it to the default

> >       Norm

 
 
 

1. Better test for GCC alignment options

Do the test for -falign-xxx vs. -malign-xxx only once.

--
                                Brian Gerst

[ gcc-align-1 2K ]
diff -urN linux-2.5.62-bk8/arch/i386/Makefile linux/arch/i386/Makefile
--- linux-2.5.62-bk8/arch/i386/Makefile 2003-02-21 20:45:05.000000000 -0500

 # prevent gcc from keeping the stack 16 byte aligned
 CFLAGS += $(call check_gcc,-mpreferred-stack-boundary=2,)

+align := $(subst -functions=0,,$(call check_gcc,-falign-functions=0,-malign-functions=0))
+
 cflags-$(CONFIG_M386)          += -march=i386
 cflags-$(CONFIG_M486)          += -march=i486

 cflags-$(CONFIG_MPENTIUMIII)   += $(call check_gcc,-march=pentium3,-march=i686)
 cflags-$(CONFIG_MPENTIUM4)     += $(call check_gcc,-march=pentium4,-march=i686)
 cflags-$(CONFIG_MK6)           += $(call check_gcc,-march=k6,-march=i586)
-cflags-$(CONFIG_MK7)           += $(call check_gcc,-march=athlon,-march=i686 -malign-functions=4)
-cflags-$(CONFIG_MK8)           += $(call check_gcc,-march=k8,$(call check_gcc,-march=athlon,-march=i686 -malign-functions=4))
-cflags-$(CONFIG_MCRUSOE)       += -march=i686
-cflags-$(CONFIG_MCRUSOE)       += $(call check_gcc,-falign-functions=0 -falign-jumps=0 -falign-loops=0,-malign-functions=0 -malign-jumps=0 -malign-loops=0)
+cflags-$(CONFIG_MK7)           += $(call check_gcc,-march=athlon,-march=i686 $(align)-functions=4)
+cflags-$(CONFIG_MK8)           += $(call check_gcc,-march=k8,$(call check_gcc,-march=athlon,-march=i686 $(align)-functions=4))
+cflags-$(CONFIG_MCRUSOE)       += -march=i686 $(align)-functions=0 $(align)-jumps=0 $(align)-loops=0
 cflags-$(CONFIG_MWINCHIPC6)    += $(call check_gcc,-march=winchip-c6,-march=i586)
 cflags-$(CONFIG_MWINCHIP2)     += $(call check_gcc,-march=winchip2,-march=i586)
 cflags-$(CONFIG_MWINCHIP3D)    += -march=i586
-cflags-$(CONFIG_MCYRIXIII)     += $(call check_gcc,-march=c3,-march=i486)
-cflags-$(CONFIG_MCYRIXIII)     += $(call check_gcc,-falign-functions=0 -falign-jumps=0 -falign-loops=0,-malign-functions=0 -malign-jumps=0 -malign-loops=0)
+cflags-$(CONFIG_MCYRIXIII)     += $(call check_gcc,-march=c3,-march=i486) $(align)-functions=0 $(align)-jumps=0 $(align)-loops=0
 cflags-$(CONFIG_MVIAC3_2)      += $(call check_gcc,-march=c3-2,-march=i686)

 CFLAGS += $(cflags-y)

2. how to create new user under linux Redhat 4.2

3. Alignment problems on solaris & gcc?

4. Connecting to internet

5. GCC data alignment - how to turn off ?

6. Printing to a Novell pint queue

7. PROBLEM WITH GCC DATA ALIGNMENT

8. SCSI cards, AL 500 or Data Cannon

9. GCC and Structure field alignment

10. gcc: How to switch off alignment?

11. gcc and alignment

12. Word/short alignment problem, gcc 2.4.5, Sparc 5 Solaris 2.3

13. Forcing gcc to odd byte alignment?