arm-coff-gcc: -fpack-struct and non-word-aligned ints?

Toralf Lund toralf@procaptura.com
Mon Jul 5 13:13:00 GMT 2004


Richard Earnshaw wrote:

>On Mon, 2004-07-05 at 13:45, Toralf Lund wrote:
>  
>
>>I'm building some code with -fpack-struct on ARM cross-gcc because an 
>>old API expects it. This mostly works fine, but it turns out that I 
>>sometimes get word-alignment issues that I never saw on the platform I'm 
>>porting from (M68k, a completely different compiler.) I sometimes get 
>>int-type struct-members at addresses that are not word-aligned, although 
>>they are always *halfword*-aligned. In some, but not all (I'm not sure 
>>about the exact conditions) cases where this happens, I get into trouble 
>>if I assign a value to the variable via it's address. Simply put, it 
>>looks like the address is rounded down to the nearest word-boundary, so 
>>that if I do something like
>>
>>struct somestruct {
>>    short a;
>>    int b;
>>};
>>[ ... ]
>>
>>struct somestruct value;
>>
>>[ ... ]
>>int *bPtr=&value.b;
>>
>>*bPtr=-100;
>>
>>the assignment will (at least under certain conditions) not actually 
>>write to all 4 bytes of value.b's memory area, but rather value.a and 
>>the two first bytes of value.b.
>>
>>Is this expected to happen? 
>>    
>>
>
>Yes.  You can't take the address of a packed structure member and then
>dereference it as a normal pointer.  If the underlying hardware/ABI only
>supports aligned types then only pointers that are correctly aligned can
>be dereferenced.  
>  
>
Fair enough.

I guess that it's logical that it would work on the 68K, then, as 32-bit 
accesses only need to be aligned to 16-bit words there, as far as I 
remember.


>>Is there another option or something that 
>>might make the problem go away?
>>    
>>
>
>No.  GCC doesn't support pointer to unaligned fundamental type.
>  
>
OK. I also found out why I thought it sometimes worked; in fact I didn't 
take the address of a basic type in those cases, but of the struct 
itself instead. In other words, in my above example

struct somestruct *ptr=&value;

ptr->b=-100;


would probably do the right thing. I'm I right in assuming that this 
will always be allowed?

- T




------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sources.redhat.com



More information about the crossgcc mailing list