This is the mail archive of the crossgcc@sources.redhat.com mailing list for the crossgcc project.

See the CrossGCC FAQ for lots more information.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On Mon, Jul 05, 2004 at 02:45:58PM +0200, 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.

That just isn't going to work.

> 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.

Yup.

> Is this expected to happen?

Yup.  

On some architectures you get a bus fault or some other error
signal from the bus interface hardware, but on ARM7 (don't know
about 9 or 10) you just end up with 16 bits of the data being
written to the other half of the destination.

> Is there another option or something that might make the
> problem go away?

Nope.  

You'll have to fix the program.

Well, you could modify gcc so that you can declare the pointer
as a "pointer to a packed int", and then modify the ARM back
end to emit code when de-referencing such pointers that is
guaranteed to work for mis-aligned addresses.  

That would be pretty cool.

> Note: I think I know several ways arounf the problem in my
> actual program - in fact I may even be able to rewrite
> everything so that -fpack-struct is not necessary - but I'd
> really like to know what is going on first.

Basically, your code is broken.  

As things are defined now, you can't expect it to work when you
assign the address of a packed struct field to a pointer, and
then de-reference the pointer.

-- 
Grant Edwards
grante@visi.com

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]