Structure alignment with GCC

Christopher Bahns chris@bahns.com
Fri Aug 11 12:48:00 GMT 2000


Here's what my manual says for -fpack-struct...

    -fpack-struct
    Pack all structure members together without holes. Usually you would not
    want to use this option, since it makes the code suboptimal, and the offsets of
    structure members won't agree with system libraries.

I don't think I'm concerned about structure "packing", am I? I have specifically been
looking for documentation/options related to structure "alignment". I don't think I have
any issues with how the compiler places structure fields in relation to other fields in the
same structure. My concern is the placement of the structure itself in relation to what
comes before it, which, as far as I can tell, is not related to whether or how the fields
in the structure are packed. Naturally if someone thinks that messing around with structure
packing might affect my alignment problem, I'd appreciate the info.

Thanks
Chris

Greg Shubin wrote:

> I checked tha man page and couldn't find anything about a command line switch. Then I
> checked /usr/doc/egcs-1.1.2/gcc/NEWS and found "-fpack-struct". I tried it on a command
> line and it didn't complain. I have not checked the code it produces to see if it
> actually works. The comment applies to GCC 2.7.0. Here's what it says:
>
> A new option '-fpack-struct' was added to automatically pack all structure
> members together without holes.
>
> I apologize if I offended anyone with my previous reply, but it's one of those hot
> buttons with me. In fact, here's another excerpt from GCC man page.
>
>  -mwarn-passed-structs
>               Warn when a function passes a struct as an argument
>               or  result.   Structure-passing  conventions   have
>               changed during the evolution of the C language, and
>               are often the source of portability  problems.   By
>               default, GCC issues no such warning.
>
> Those PalmOS developers should be given a fair trial, then be taken out and shot.
>
> Michael Sokolov wrote:
>
> > gshubin <gshubin@wco.com> wrote:
> >
> > > Thanks, now I have a good example of why it is always a bad idea to use structures
> > > for passing data. I usually use the example of inter-processor communication, but
> > > now you have given me and example of inter-compiler communication [...]
> >
> > Unfortunately, you don't always get to choose. Sometimes GCC has no other
> > choice but to mirror the ABI of some proprietary compiler in aspects like
> > alignment and calling convention, however wrong it is. Consider PalmOS, for
> > example. (It's a popular handheld embedded OS for the Motorola 68328 Dragonball
> > chip.) It's designers were clearly assembly-phobiac C fans who wrote the whole
> > OS in C, compiled it with one particular proprietary compiler, and effectively
> > made that compiler's ABI, which apparently isn't even documented or formally
> > set down anywhere, the de facto ABI for their OS. PalmOS has no traditional
> > system calls or separation between the system and applications, instead OS
> > routines in the ROM and application routines that application programmers write
> > just freely call each other, and everything is built on the assumption that
> > everything is written in C, with the C ABI being that of the proprietary
> > compiler used to compile the OS. Ouch. This is the kind of stuff that no
> > programmer should ever see in his/her worst nightmare. I don't recall even
> > Microsoft doing something as bad. (But then I've had the fortune of complete
> > isolation from Microsoft since early 1993, so I have no idea what else they've
> > done since then.)
> >
> > As for PalmOS, there are patched versions of gcc 2.7.2.2 and 2.95.2 that mimic
> > the right ABI, more or less. More or less because so far I haven't found any
> > formal spec or in fact any documentation whatsoever for it, so it's all pure
> > guesswork so far. This will surely make my life interesting in a perverse way
> > when I get to integrating this stuff into the current GCC.
> >
> > --
> > Michael Sokolov         Harhan Engineering Laboratory
> > Public Service Agent    International Free Computing Task Force
> >                         International Engineering and Science Task Force
> >                         615 N GOOD LATIMER EXPY STE #4
> >                         DALLAS TX 75204-5852 USA
> >
> > Phone: +1-214-824-7693 (Harhan Eng Lab office)
> > E-mail: msokolov@ivan.Harhan.ORG (ARPA TCP/SMTP) (UUCP coming soon)
> >
> > ------
> > Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
> > Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com
>
> ------
> Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
> Want to unsubscribe? Send a note to crossgcc-unsubscribe@sourceware.cygnus.com


More information about the crossgcc mailing list