Go ARM

519 views
Skip to first unread message

Stephen Illingworth

unread,
Nov 15, 2023, 2:30:33 PM11/15/23
to golang-nuts
Hello,

I'm trying to build a project on the Raspberry Pi, natively.

Using "go env" I can see that Go has the following value for GOGCCFLAGS

GOGCCFLAGS='-fPIC -marm -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build745518569=/tmp/go-build -gno-record-gcc-switches'

However, the native compiler (gcc 12.2) does not have the -marm flag. The compilation of the project fails.

I know you can't edit the GOGCCFLAGS environment variable directly but it's not clear to me how to remove this unneeded flag.

I've tried the CGO_CFLAGS_DISALLOW environment variable but that doesn't seem work. But maybe I'm misusing it.

This is a brand new installation of Raspbian. The only other software I've installed is Go for Arm from the official Go distribution site.

Help
Stephen

Jan Mercl

unread,
Nov 15, 2023, 3:05:59 PM11/15/23
to Stephen Illingworth, golang-nuts
On Wed, Nov 15, 2023 at 8:30 PM Stephen Illingworth <stephen.i...@gmail.com> wrote:
> I'm trying to build a project on the Raspberry Pi, natively.
>
> Using "go env" I can see that Go has the following value for GOGCCFLAGS
>
> GOGCCFLAGS='-fPIC -marm -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build745518569=/tmp/go-build -gno-record-gcc-switches'
>
> However, the native compiler (gcc 12.2) does not have the -marm flag. The compilation of the project fails.

From the above I guess you might be running gcc for aarch64 as the arm version accepts the -marm flag here:

jnml@pi32:~/tmp/gcc$ go version
go version go1.21.4 linux/arm
jnml@pi32:~/tmp/gcc$ cat main.c
#include <stdio.h>

int main() {
printf("Hello, world!\n");
}
jnml@pi32:~/tmp/gcc$ rm -f a.out ; gcc -marm main.c && ./a.out && echo ok
Hello, world!
ok
jnml@pi32:~/tmp/gcc$ gcc --version
gcc (Raspbian 10.2.1-6+rpi1) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

jnml@pi32:~/tmp/gcc$

I tried the solution from https://jensd.be/1126/linux/cross-compiling-for-arm-or-aarch64-on-debian-or-ubuntu and it seems to work fine:

jnml@pi64:~/tmp/gcc$ jnml@pi64:~/tmp/gcc$ go version
go version go1.21.4 linux/arm64
jnml@pi64:~/tmp/gcc$ cat main.c
#include <stdio.h>

int main() {
printf("Hello, world!\n");
}
jnml@pi64:~/tmp/gcc$ rm -f a.out ; gcc main.c && ./a.out && echo ok
Hello, world!
ok
jnml@pi64:~/tmp/gcc$ rm -f a.out ; gcc -marm main.c && ./a.out && echo ok
gcc: error: unrecognized command-line option ‘-marm’
jnml@pi64:~/tmp/gcc$ sudo apt install gcc-arm-linux-gnueabi binutils-arm-linux-gnueabi
...
jnml@pi64:~/tmp/gcc$ rm -f a.out ; arm-linux-gnueabi-gcc -marm main.c && file a.out
a.out: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, BuildID[sha1]=2191a16290d46b039bfae26fd5918106dff99749, for GNU/Linux 3.2.0, not stripped
jnml@pi64:~/tmp/gcc$

HTH

Stephen Illingworth

unread,
Nov 15, 2023, 3:58:43 PM11/15/23
to golang-nuts
That works better although not perfectly for my purposes. More work required from me.

I'm curious though, about the -marm flag. How can I remove it from the GOGCCFLAGS variable? Or are we saying we can't use the aarch64 compiler in conjunction with cgo?

Thanks for the help

Jan Mercl

unread,
Nov 15, 2023, 4:09:47 PM11/15/23
to Stephen Illingworth, golang-nuts
On Wed, Nov 15, 2023 at 9:59 PM Stephen Illingworth
<stephen.i...@gmail.com> wrote:
>
> That works better although not perfectly for my purposes. More work required from me.
>
> I'm curious though, about the -marm flag. How can I remove it from the GOGCCFLAGS variable? Or are we saying we can't use the aarch64 compiler in conjunction with cgo?

Please share the output of '$ go version' and the output of '$ gcc
-dumpmachine'.

Def Ceb

unread,
Nov 15, 2023, 4:25:37 PM11/15/23
to golan...@googlegroups.com
I downloaded and ran `go env` with both the 32-bit and 64-bit Go
toolchains from go.dev, and the GOGCCFLAGS results were:

64-bit: GOGCCFLAGS='-fPIC -pthread -Wl,--no-gc-sections
-fmessage-length=0
-ffile-prefix-map=/tmp/go-build2828356582=/tmp/go-build
-gno-record-gcc-switches'

32-bit: GOGCCFLAGS='-fPIC -marm -pthread -Wl,--no-gc-sections
-fmessage-length=0
-ffile-prefix-map=/tmp/go-build3860201885=/tmp/go-build
-gno-record-gcc-switches'

Since you're on a newer Raspberry Pi with a 64-bit ARM environment, you
should download the 64-bit Go toolchain, not the 32-bit one.

Stephen Illingworth:
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to golang-nuts...@googlegroups.com
> <mailto:golang-nuts...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/82a69c96-5cbd-4aa2-a911-8141071b2610n%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/82a69c96-5cbd-4aa2-a911-8141071b2610n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Stephen Illingworth

unread,
Nov 15, 2023, 5:52:36 PM11/15/23
to golang-nuts
On Wednesday, 15 November 2023 at 21:25:37 UTC Def Ceb wrote:
I downloaded and ran `go env` with both the 32-bit and 64-bit Go
toolchains from go.dev, and the GOGCCFLAGS results were:

Since you're on a newer Raspberry Pi with a 64-bit ARM environment, you
should download the 64-bit Go toolchain, not the 32-bit one.

That's it! Thank you. Not sure why I didn't spot the ARM64 build on the download page.
Reply all
Reply to author
Forward
0 new messages