Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Thread

Misaligned structure fields, works regardless... Structure packing?

Misaligned structure fields, works regardless... Structure packing?

2005-12-22 by Thiadmer Riemersma (ITB CompuPhase)

Hello everyone,

I just saw that I have a couple of structure fields that are not well
aligned. That is, a 4-byte pointer starts at an odd memory address
(instead of at a multiple of 4). The funny thing is, the code runs fine.

What's up? is GNU GCC too smart? Is this a side-effect of packing?

Some background: I have had a couple of nasty bugs in the firmware
that turned out to be caused by mis-alignment. When another nasty bug
came up earlier this week, I thought "alignment" again, and set out
(finally) to write that utility that helps me find misaligned
variables, structure fields, etc. This time, the nasty bug turned out
to be a stack overrun, but I made the utility regardless and ran it on
the code.

I found that in a few instances, structures could start at an odd
address, even though the first element is a pointer. This is what all
mis-aligned structures have in common:
- their first element is a pointer to char
- all members have __attribute((packed))
- the structure itself has __attribute((packed)) too

Other observations:
- both structures in RAM and in ROM can be mis-aligned
- both global and "static local" variables can be mis-aligned

My guess is that the first member being a pointer to char is just
coincidence. So probably this has to do with structure packing. This
implies that GCC must have an alternative method to access the
mis-aligned structure fields, and in fact, it means that GCC must use
this alternative (and probably slower) access method for all accesses
to packed structures.

Can anyone confirm/refute this?

Kind regards,
Thiadmer

Re: [lpc2000] Misaligned structure fields, works regardless... Structure packing?

2005-12-22 by David Hawkins

> Can anyone confirm/refute this?

Hi Thiadmer,

The code generated is dependent on your compiler settings,
so the only 'person' that can answer your question with confidence
is your compiler. So, take a look at the disassembled source code.
It'll show you if a single statement in C code to an unaligned
variable is translating to multiple assembler statements.

Cheers
Dave

Re: [lpc2000] Misaligned structure fields, works regardless... Structure packing?

2005-12-22 by Tom Walsh

Thiadmer Riemersma (ITB CompuPhase) wrote:

>Hello everyone,
>
>I just saw that I have a couple of structure fields that are not well
>aligned. That is, a 4-byte pointer starts at an odd memory address
>(instead of at a multiple of 4). The funny thing is, the code runs fine.
>
>What's up? is GNU GCC too smart? Is this a side-effect of packing?
>
>  
>
No, it is the exact reason you declare a structure to be packed.  As 
David says in the other posting, look at the code being generated via 
listing or JTAG assembly view in your debugger.

Regards,

TomW



>Some background: I have had a couple of nasty bugs in the firmware
>that turned out to be caused by mis-alignment. When another nasty bug
>came up earlier this week, I thought "alignment" again, and set out
>(finally) to write that utility that helps me find misaligned
>variables, structure fields, etc. This time, the nasty bug turned out
>to be a stack overrun, but I made the utility regardless and ran it on
>the code.
>
>I found that in a few instances, structures could start at an odd
>address, even though the first element is a pointer. This is what all
>mis-aligned structures have in common:
>- their first element is a pointer to char
>- all members have __attribute((packed))
>- the structure itself has __attribute((packed)) too
>
>Other observations:
>- both structures in RAM and in ROM can be mis-aligned
>- both global and "static local" variables can be mis-aligned
>
>My guess is that the first member being a pointer to char is just
>coincidence. So probably this has to do with structure packing. This
>implies that GCC must have an alternative method to access the
>mis-aligned structure fields, and in fact, it means that GCC must use
>this alternative (and probably slower) access method for all accesses
>to packed structures.
>
>Can anyone confirm/refute this?
>
>Kind regards,
>Thiadmer
>
>
>
>
>
>
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>  
>


-- 
Tom Walsh - WN3L - Embedded Systems Consultant
http://openhardware.net, http://cyberiansoftware.com
"Windows? No thanks, I have work to do..."
----------------------------------------------------

Re: Misaligned structure fields, works regardless... Structure packing?

2005-12-23 by Thiadmer Riemersma (ITB CompuPhase)

Hello Tom,

> No, it is the exact reason you declare a structure to be packed.

What I meant to do is have all members in the structure to be packed
relative to each other (i.e. no padding between the fields), but
aligning the first member to its "natural alignment".

Most of this is due to libraries that are supposed to be portable and
that deal with file/protocol formats. I guess I started seeing
__attribute__((packed)) and the equivalent on other compilers as a
magic "fix all my problems" solution.

Thanks,
Thiadmer

Re: Misaligned structure fields, works regardless... Structure packing?

2005-12-23 by Thiadmer Riemersma (ITB CompuPhase)

> What I meant to do is have all members in the structure to be packed
> relative to each other (i.e. no padding between the fields), but
> aligning the first member to its "natural alignment".

I just found my error. I was declaring structs as:

	typedef struct tagMYSTRUCT {
		long member1   __attribute__((packed));
		short member2  __attribute__((packed));
		short member3  __attribute__((packed));
	} __attribute__((packed)) MYSTRUCT;

What I *should* have done is to apply the __attribute__((packed)) to
the individual members, bot *not* to the structure as a whole.

Season greetings,
Thiadmer

Re: [lpc2000] Re: Misaligned structure fields, works regardless... Structure packing?

2005-12-23 by Tom Walsh

Thiadmer Riemersma (ITB CompuPhase) wrote:

>>What I meant to do is have all members in the structure to be packed
>>relative to each other (i.e. no padding between the fields), but
>>aligning the first member to its "natural alignment".
>>    
>>
>
>I just found my error. I was declaring structs as:
>
>	typedef struct tagMYSTRUCT {
>		long member1   __attribute__((packed));
>		short member2  __attribute__((packed));
>		short member3  __attribute__((packed));
>	} __attribute__((packed)) MYSTRUCT;
>
>What I *should* have done is to apply the __attribute__((packed)) to
>the individual members, bot *not* to the structure as a whole.
>
>  
>
:-)  Sometimes ya' just gotta look at the code. 

Cheers!

TomW


-- 
Tom Walsh - WN3L - Embedded Systems Consultant
http://openhardware.net, http://cyberiansoftware.com
"Windows? No thanks, I have work to do..."
----------------------------------------------------

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.