All of lore.kernel.org
 help / color / mirror / Atom feed
* ARM cross compiler question
@ 2022-06-20 20:42 Stephen.Yu
  2022-06-21 11:32 ` Ross Burton
  0 siblings, 1 reply; 2+ messages in thread
From: Stephen.Yu @ 2022-06-20 20:42 UTC (permalink / raw)
  To: meta-arm

[-- Attachment #1: Type: text/plain, Size: 2805 bytes --]

I have the following C source file arm_cast.c
================================================================

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv)
{
    uint32_t value = 0x1254FEBA;
    uint16_t v1, v2;
    uint16_t a[2];
    int loop;

    v1 = ((uint16_t *)(&value))[0];
    v2 = ((uint16_t *)(&value))[1];

    printf("value: hex -> 0x%x, dec -> %u\n", value, value);
    printf("v1: hex -> 0x%x, dec -> %u\n", v1, v1);
    printf("v2: hex -> 0x%x, dec -> %u\n", v2, v2);

    for (loop = 0; loop < 2; loop++)
        a[loop] = ((uint16_t *)(&value))[loop];

    printf("a[0]: hex -> 0x%x, addr -> %p\n", a[0], &a[0]);
    printf("a[1]: hex -> 0x%x, addr -> %p\n", a[1], &a[1]);

    return 0;
}

Cross compile from x86-64 to ARM
================================================================
syu@ubuntu-virtual-machine:~$ echo $CC
arm-poky-linux-gnueabi-gcc -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/opt/poky/3.1.7/sysroots/cortexa9hf-neon-poky-linux-gnueabi
syu@ubuntu-virtual-machine:~$
syu@ubuntu-virtual-machine:~$ $CC -o arm_cast_o1 -O1 arm_cast.c
syu@ubuntu-virtual-machine:~$
syu@ubuntu-virtual-machine:~$ $CC -o arm_cast_o2 -O2 arm_cast.c
syu@ubuntu-virtual-machine:~$
syu@ubuntu-virtual-machine:~$ ls -lt arm_cast_o1 arm_cast_o2
-rwxrwxr-x 1 syu syu 11636 Jun 20 13:24 arm_cast_o2
-rwxrwxr-x 1 syu syu 11636 Jun 20 13:24 arm_cast_o1

Run the binary on target
================================================================
root@Bev8040D:~# uname -a
Linux Bev8040D 5.4.124-altera #1 SMP Wed Nov 10 15:47:46 UTC 2021 armv7l armv7l armv7l GNU/Linux
root@Bev8040D:~#
root@Bev8040D:~# ls -lt arm_cast_o1 arm_cast_o2
-rwxr-xr-x 1 root root 11636 Dec  7 20:35 arm_cast_o2
-rwxr-xr-x 1 root root 11636 Dec  7 20:35 arm_cast_o1
root@Bev8040D:~#
root@Bev8040D:~# ./arm_cast_o1
value: hex -> 0x1254feba, dec -> 307560122
v1: hex -> 0xfeba, dec -> 65210
v2: hex -> 0x1254, dec -> 4692
a[0]: hex -> 0xfeba, addr -> 0x7ebf3b88
a[1]: hex -> 0x1254, addr -> 0x7ebf3b8a
root@Bev8040D:~#
root@Bev8040D:~# ./arm_cast_o2
value: hex -> 0x1254feba, dec -> 307560122
v1: hex -> 0xfeba, dec -> 65210
v2: hex -> 0x1254, dec -> 4692
a[0]: hex -> 0x0, addr -> 0x7ede7b88
a[1]: hex -> 0x0, addr -> 0x7ede7b8a

The issue was that with -O2, a[0] and a[1] were 0, which seemed incorrect in my opinion. I did the same experiment with $CXX. The result was correct. For both -O1 and -O2, a[0] and a[1]  had the same non-zero value. Note that the target runs Dunfell distro. Again, I tested the Honister and it seemed having the same issue.

Any idea and suggestion?

Thanks,
Stephen


[-- Attachment #2: Type: text/html, Size: 13047 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: ARM cross compiler question
  2022-06-20 20:42 ARM cross compiler question Stephen.Yu
@ 2022-06-21 11:32 ` Ross Burton
  0 siblings, 0 replies; 2+ messages in thread
From: Ross Burton @ 2022-06-21 11:32 UTC (permalink / raw)
  To: stephen.yu, meta-arm

[-- Attachment #1: Type: text/plain, Size: 3850 bytes --]

This is not Arm-specific, as demonstrated here:

https://godbolt.org/z/7bdeYrKxK

The problem is that you’re breaking strict aliasing with your pointer casts:

<source>:12:32: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   12 |     v1 = ((uint16_t *)(&value))[0];

You’ve wandered into undefined behaviour here, so this means that value is effectively uninitialized here:

<source>:12:8: warning: 'value' is used uninitialized [-Wuninitialized]
   12 |     v1 = ((uint16_t *)(&value))[0];
      |     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~

So, it’s zero.  Turn on -Wall and you’ll get the warnings from GCC that this is happening.

Ross

From: meta-arm@lists.yoctoproject.org <meta-arm@lists.yoctoproject.org> on behalf of Stephen Yu (Microchip) via lists.yoctoproject.org <stephen.yu=microchip.com@lists.yoctoproject.org>
Date: Monday, 20 June 2022 at 21:43
To: meta-arm@lists.yoctoproject.org <meta-arm@lists.yoctoproject.org>
Subject: [meta-arm] ARM cross compiler question
I have the following C source file arm_cast.c
================================================================

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv)
{
    uint32_t value = 0x1254FEBA;
    uint16_t v1, v2;
    uint16_t a[2];
    int loop;

    v1 = ((uint16_t *)(&value))[0];
    v2 = ((uint16_t *)(&value))[1];

    printf("value: hex -> 0x%x, dec -> %u\n", value, value);
    printf("v1: hex -> 0x%x, dec -> %u\n", v1, v1);
    printf("v2: hex -> 0x%x, dec -> %u\n", v2, v2);

    for (loop = 0; loop < 2; loop++)
        a[loop] = ((uint16_t *)(&value))[loop];

    printf("a[0]: hex -> 0x%x, addr -> %p\n", a[0], &a[0]);
    printf("a[1]: hex -> 0x%x, addr -> %p\n", a[1], &a[1]);

    return 0;
}

Cross compile from x86-64 to ARM
================================================================
syu@ubuntu-virtual-machine:~$ echo $CC
arm-poky-linux-gnueabi-gcc -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/opt/poky/3.1.7/sysroots/cortexa9hf-neon-poky-linux-gnueabi
syu@ubuntu-virtual-machine:~$
syu@ubuntu-virtual-machine:~$ $CC -o arm_cast_o1 -O1 arm_cast.c
syu@ubuntu-virtual-machine:~$
syu@ubuntu-virtual-machine:~$ $CC -o arm_cast_o2 -O2 arm_cast.c
syu@ubuntu-virtual-machine:~$
syu@ubuntu-virtual-machine:~$ ls -lt arm_cast_o1 arm_cast_o2
-rwxrwxr-x 1 syu syu 11636 Jun 20 13:24 arm_cast_o2
-rwxrwxr-x 1 syu syu 11636 Jun 20 13:24 arm_cast_o1

Run the binary on target
================================================================
root@Bev8040D:~# uname -a
Linux Bev8040D 5.4.124-altera #1 SMP Wed Nov 10 15:47:46 UTC 2021 armv7l armv7l armv7l GNU/Linux
root@Bev8040D:~#
root@Bev8040D:~# ls -lt arm_cast_o1 arm_cast_o2
-rwxr-xr-x 1 root root 11636 Dec  7 20:35 arm_cast_o2
-rwxr-xr-x 1 root root 11636 Dec  7 20:35 arm_cast_o1
root@Bev8040D:~#
root@Bev8040D:~# ./arm_cast_o1
value: hex -> 0x1254feba, dec -> 307560122
v1: hex -> 0xfeba, dec -> 65210
v2: hex -> 0x1254, dec -> 4692
a[0]: hex -> 0xfeba, addr -> 0x7ebf3b88
a[1]: hex -> 0x1254, addr -> 0x7ebf3b8a
root@Bev8040D:~#
root@Bev8040D:~# ./arm_cast_o2
value: hex -> 0x1254feba, dec -> 307560122
v1: hex -> 0xfeba, dec -> 65210
v2: hex -> 0x1254, dec -> 4692
a[0]: hex -> 0x0, addr -> 0x7ede7b88
a[1]: hex -> 0x0, addr -> 0x7ede7b8a

The issue was that with -O2, a[0] and a[1] were 0, which seemed incorrect in my opinion. I did the same experiment with $CXX. The result was correct. For both -O1 and -O2, a[0] and a[1]  had the same non-zero value. Note that the target runs Dunfell distro. Again, I tested the Honister and it seemed having the same issue.

Any idea and suggestion?

Thanks,
Stephen


[-- Attachment #2: Type: text/html, Size: 19459 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-21 11:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-20 20:42 ARM cross compiler question Stephen.Yu
2022-06-21 11:32 ` Ross Burton

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.