ALIGNMENT RUNAROUND

I’m posting this merely as a warning to other QNX4 programmers
so that they might avoid the time time I’ve just wasted. The
default for the GNU compiler seems to be to align variables
in structures according to their size, instead of packing.
The opposite was the case with Watcom 10.6.

I was working on a piece of hardware with a specific memory
mapped structure when this happened.


Mitchell Schoenbrun --------- maschoen@pobox.com

Can you explain this a little more?

Is there a command line option to achieve QNX4 type alignment?


Bill Caroselli - Sattel Global Networks
1-818-709-6201 ext 122



“Mitchell Schoenbrun” <maschoen@pobox.com> wrote in message
news:Voyager.010518145435.654D@schoenbrun.com

I’m posting this merely as a warning to other QNX4 programmers
so that they might avoid the time time I’ve just wasted. The
default for the GNU compiler seems to be to align variables
in structures according to their size, instead of packing.
The opposite was the case with Watcom 10.6.

I was working on a piece of hardware with a specific memory
mapped structure when this happened.


Mitchell Schoenbrun --------- > maschoen@pobox.com

Previously, Bill Caroselli wrote in qdn.public.qnxrtp.porting:

Can you explain this a little more?

Sure,

Lets say you have some memory mapped hardware with the following
structure:




With the Watcom compiler you would create the following
structure.

struct
{
unsigned char function_A;
unsigned short value_A;
};

Now recompile this under GNU and you get the following
layout

Name Offset Size
Function A 0 1
Filler 1 1
Value A 2 2


Note that an extra byte has been inserted to give the
unsigned short an alignment of 2.


Is there a command line option to achieve QNX4 type alignment?

I haven’t found one yet. I’d be interested in knowing of it,
or a “pragma” if there is one.



Mitchell Schoenbrun --------- maschoen@pobox.com

OK. This is what I thought you meant, but your wording was a little
strange.

You said, “The default for the GNU compiler seems to be to align variables
in structures according to their size, instead of packing.”

Where I would have expected to see somethnig a little simpler, like, “The
default for the GNU compiler seems to be to pack on 2 byte boundries.”


Bill Caroselli - Sattel Global Networks
1-818-709-6201 ext 122



“Mitchell Schoenbrun” <maschoen@pobox.com> wrote in message
news:Voyager.010523133332.258F@schoenbrun.com

Note that an extra byte has been inserted to give the
unsigned short an alignment of 2.

Is there a command line option to achieve QNX4 type alignment?

I haven’t found one yet. I’d be interested in knowing of it,
or a “pragma” if there is one.

Previously, Bill Caroselli wrote in qdn.public.qnxrtp.porting:

Where I would have expected to see somethnig a little simpler, like, “The
default for the GNU compiler seems to be to pack on 2 byte boundries.”

I guess we think differently about the terminology. To me packing is
the act of removing extra alignment bytes. The GNU compiler “unpacks”
if anything. As far as it “packing” on 2 byte boundaries, well that
is what it does for 16bit fields. I suspect, but am not sure that
it aligns 32bit fields on 4 byte boundaries. Clearly it does
align 8 bit fields on 1 byte boundaries, thus my statement that it
is size related.

Mitchell Schoenbrun --------- maschoen@pobox.com

On Wed, 23 May 2001 13:33:32 -0700, Mitchell Schoenbrun
<maschoen@pobox.com> wrote:

Previously, Bill Caroselli wrote in qdn.public.qnxrtp.porting:
Can you explain this a little more?

Sure,

Lets say you have some memory mapped hardware with the following
structure:




With the Watcom compiler you would create the following
structure.

struct
{
unsigned char function_A;
unsigned short value_A;
};

Now recompile this under GNU and you get the following
layout

Name Offset Size
Function A 0 1
Filler 1 1
Value A 2 2


Note that an extra byte has been inserted to give the
unsigned short an alignment of 2.


Is there a command line option to achieve QNX4 type alignment?

I haven’t found one yet. I’d be interested in knowing of it,
or a “pragma” if there is one.

The alignment in gcc (among other things) is described in

/usr/info/gcc.info-* files. It may be defined using variables’
attributes, or with different styles of pragmas. There are also
_pack*.h headers in /usr/include. I haven’t found any command
line option yet.

ako

Mitchell Schoenbrun --------- > maschoen@pobox.com

I have to agree with Mitchell here. I always thought the name “pack” was
backwards because any value other than 1 involves inserting extra, hidden
bytes, that is, unpacking.

Mitchell Schoenbrun wrote:

Previously, Bill Caroselli wrote in qdn.public.qnxrtp.porting:

Where I would have expected to see somethnig a little simpler, like, “The
default for the GNU compiler seems to be to pack on 2 byte boundries.”

I guess we think differently about the terminology. To me packing is
the act of removing extra alignment bytes. The GNU compiler “unpacks”
if anything. As far as it “packing” on 2 byte boundaries, well that
is what it does for 16bit fields. I suspect, but am not sure that
it aligns 32bit fields on 4 byte boundaries. Clearly it does
align 8 bit fields on 1 byte boundaries, thus my statement that it
is size related.

Mitchell Schoenbrun --------- > maschoen@pobox.com

“Andrzej Kocon” <ako@box43.gnet.pl> wrote in message
news:3b0cbf9f.8520041@inn.qnx.com

On Wed, 23 May 2001 13:33:32 -0700, Mitchell Schoenbrun
maschoen@pobox.com> > wrote:

Previously, Bill Caroselli wrote in qdn.public.qnxrtp.porting:
Can you explain this a little more?

Sure,

Lets say you have some memory mapped hardware with the following
structure:




With the Watcom compiler you would create the following
structure.

struct
{
unsigned char function_A;
unsigned short value_A;
};

Now recompile this under GNU and you get the following
layout

Name Offset Size
Function A 0 1
Filler 1 1
Value A 2 2


Note that an extra byte has been inserted to give the
unsigned short an alignment of 2.


Is there a command line option to achieve QNX4 type alignment?

I haven’t found one yet. I’d be interested in knowing of it,
or a “pragma” if there is one.

The alignment in gcc (among other things) is described in
/usr/info/gcc.info-* files. It may be defined using variables’
attributes, or with different styles of pragmas. There are also
_pack*.h headers in /usr/include. I haven’t found any command
line option yet.

ako

I think the attribute((packed)) method is the
common way to remove padding from structures in GCC:

struct HDLCFrame_FS
{
unsigned char mLogicChan;
unsigned char mFlags;
unsigned long mTimeStamp;
unsigned char mData[0];

} attribute((packed));



\

Mats Byggmastar
http://www.multi.fi/~mbc

Cute.

Is this ANSI? Is it official C++?

Or is it just a GNU thing?

\

Bill Caroselli - Sattel Global Networks
1-818-709-6201 ext 122



“Mats Byggmastar” <mats.byggmastar@multi.NOJUNK.fi> wrote in message
news:9f07k2$rpi$1@inn.qnx.com

I think the attribute((packed)) method is the
common way to remove padding from structures in GCC:

struct HDLCFrame_FS
{
unsigned char mLogicChan;
unsigned char mFlags;
unsigned long mTimeStamp;
unsigned char mData[0];

} attribute((packed));

Cute.

Is this ANSI? Is it official C++?

Or is it just a GNU thing?

Not sure, but I think it’s just a GNU thing.


\

Mats Byggmastar
http://www.multi.fi/~mbc

On Tue, 29 May 2001 16:20:38 +0300, “Mats Byggmastar”
<mats.byggmastar@multi.NOJUNK.fi> wrote:

“Andrzej Kocon” <> ako@box43.gnet.pl> > wrote in message
news:> 3b0cbf9f.8520041@inn.qnx.com> …

(…)

The alignment in gcc (among other things) is described in
/usr/info/gcc.info-* files. It may be defined using variables’
attributes, or with different styles of pragmas. There are also
^^^^^^^^^^
_pack*.h headers in /usr/include. I haven’t found any command
line option yet.

ako


I think the attribute((packed)) method is the
common way to remove padding from structures in GCC:

Sure it’s very handy (e. g., when specifying individual

alignment for each structure member), but (obviously) labour intensive
for porting purposes - a command line parameter would be nicer,
esp. if the application being ported assumed uniform packing
throughout its code. I think I’ve seen a gcc parameter, but not
for x86. Also, the method may raise problems when porting in the
other direction.

ako

struct HDLCFrame_FS
{
unsigned char mLogicChan;
unsigned char mFlags;
unsigned long mTimeStamp;
unsigned char mData[0];

} attribute((packed));


Mats Byggmastar
http://www.multi.fi/~mbc
\

Previously, Andrzej Kocon wrote in qdn.public.qnxrtp.porting:

Sure it’s very handy (e. g., when specifying individual
alignment for each structure member), but (obviously) labour intensive
for porting purposes - a command line parameter would be nicer,
esp. if the application being ported assumed uniform packing
throughout its code. I think I’ve seen a gcc parameter, but not
for x86. Also, the method may raise problems when porting in the
other direction.

Just my two cents here. I’m quite happy to find that I can
add “attribute ((packed))” to the whole structure just
once rather than to each individual field. Before this example
was posted I was unware of this and had to modify each
field individually. Since there were only a 3 or 4 “hardware”
structures, this worked fine.

struct HDLCFrame_FS
{
unsigned char mLogicChan;

} attribute((packed));

That is, I’m a happy camper now.

\

Mitchell Schoenbrun --------- maschoen@pobox.com

Have you tried passing -fpack-struct to the compiler?

Mitchell Schoenbrun <maschoen@pobox.com> wrote:

I’m posting this merely as a warning to other QNX4 programmers
so that they might avoid the time time I’ve just wasted. The
default for the GNU compiler seems to be to align variables
in structures according to their size, instead of packing.
The opposite was the case with Watcom 10.6.

I was working on a piece of hardware with a specific memory
mapped structure when this happened.


Mitchell Schoenbrun --------- > maschoen@pobox.com


cburgess@qnx.com

On Wed, 30 May 2001 16:07:42 -0700, Mitchell Schoenbrun
<maschoen@pobox.com> wrote:

I wouldn’t dare to post detailed examples
to Mitchell Schoenbrun… BTW, now I’m happy too,
after the Colin Burgess’ post.

ako

Just my two cents here. I’m quite happy to find that I can
add “attribute ((packed))” to the whole structure just
once rather than to each individual field. Before this example
was posted I was unware of this and had to modify each
field individually. Since there were only a 3 or 4 “hardware”
structures, this worked fine.

struct HDLCFrame_FS
{
unsigned char mLogicChan;

} attribute((packed));

That is, I’m a happy camper now.

\

Mitchell Schoenbrun --------- > maschoen@pobox.com

Previously, Colin Burgess wrote in qdn.public.qnxrtp.porting:

Have you tried passing -fpack-struct to the compiler?

Not yet, thanks for the tip.

Mitchell Schoenbrun --------- maschoen@pobox.com

Mitchell…

Did it work for you the -fpack-struct flag?


Thanks…

M.



Mitchell Schoenbrun wrote:

Previously, Colin Burgess wrote in qdn.public.qnxrtp.porting:
Have you tried passing -fpack-struct to the compiler?

Not yet, thanks for the tip.

Mitchell Schoenbrun --------- > maschoen@pobox.com

my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work.

Miguel Simon
Research Engineer
School of Aerospace and Mechanical Engineering
University of Oklahoma
http://www.amerobotics.ou.edu/
http://www.saic.com

Previously, Miguel Simon wrote in qdn.public.qnxrtp.porting:

Mitchell…

Did it work for you the -fpack-struct flag?

Sorry, I haven’t had a chance to try it. No
RTP for another few weeks.


Mitchell Schoenbrun --------- maschoen@pobox.com