[ipxe-devel] [PATCH 1/1] [arm] add -mno-unaligned-access compiler flag

Mark Rutland mark.rutland at arm.com
Thu Apr 19 15:26:41 UTC 2018


On Thu, Apr 19, 2018 at 04:00:47PM +0100, Mark Rutland wrote:
> On Thu, Apr 19, 2018 at 12:33:10AM +0100, Michael Brown wrote:
> > On 18/04/18 20:21, Heinrich Schuchardt wrote:
> For better or worse, I don't think that it is sufficient to pass the
> compiler -mno-unaligned-accesses, even if it happens to mask the problem
> in this specific case. The compiler can validly assume structures have
> their usual alignment, and hence the LDRD should not be misaligned.

I've just verified that this is the case:

----
struct foo {
	int a;
	int b;
};

int foo_cmp (struct foo *f1)
{
	return (f1->a == f1->b);
};
----

... when compiled with:

  arm-linux-gnueabihf-gcc -mcpu=cortex-a15 -mabi=aapcs -mno-unaligned-access -O2 -c test.c

... yields:

00000000 <foo_cmp>:
   0:	e9d0 2000 	ldrd	r2, r0, [r0]
   4:	eba2 0000 	sub.w	r0, r2, r0
   8:	fab0 f080 	clz	r0, r0
   c:	ea4f 1050 	mov.w	r0, r0, lsr #5
  10:	4770      	bx	lr
  12:	bf00      	nop


... whereas using packed (rather than aligned(1), which I was wrong
about):

----
struct foo {
	int a;
	int b;
} __attribute__ ((packed));

int foo_cmp (struct foo *f1)
{
	return (f1->a == f1->b);
};
----

... when compiled allowing unaligned accesses with:

  arm-linux-gnueabihf-gcc -mcpu=cortex-a15 -mabi=aapcs -O2 -c test.c 

... yields:

00000000 <foo_cmp>:
   0:	6802      	ldr	r2, [r0, #0]
   2:	6840      	ldr	r0, [r0, #4]
   4:	eba2 0000 	sub.w	r0, r2, r0
   8:	fab0 f080 	clz	r0, r0
   c:	ea4f 1050 	mov.w	r0, r0, lsr #5
  10:	4770      	bx	lr
  12:	bf00      	nop

... which should avoid alignment traps so long as SCTLR.A is clear to
permit unaligned LDR.

Thanks,
Mark.



More information about the ipxe-devel mailing list