go build for mips

1,190 views
Skip to first unread message

luc...@kryptus.com

unread,
Jun 22, 2017, 10:20:51 AM6/22/17
to golang-nuts
Since go1.8 added support for mips32, why can't build a simple hello world for a mips architecture system?

I successfully build programs for arm architecture, but could not do the same for mips.

Ian Lance Taylor

unread,
Jun 22, 2017, 10:37:08 AM6/22/17
to luc...@kryptus.com, golang-nuts
Tell us precisely what you did and precisely what happened.

Ian

luc...@kryptus.com

unread,
Jun 22, 2017, 12:19:47 PM6/22/17
to golang-nuts, luc...@kryptus.com
I try to build a simple hello world with
cgo_enabled=1 goarch=mips goos=linux go build hello.go    | Result: illegal instruction

file information: ELF 32-bit MSB executable, MIPS, MIPS32 version 1 (SYSV), statically linked, not stripped

Also, tried to build with another compile (CC=/path/to/Compiler) instead the one go uses. Also illegal instruction
on my router tp-linkwdr3500.

If anyone could give me a hint, I would be grateful.

Thanks,
Lucas.

Nathan Kerr

unread,
Jun 22, 2017, 2:32:27 PM6/22/17
to golang-nuts, luc...@kryptus.com
What system are you building on (OS and architecture)?

What version of Go are you using?

Is the result of running go build an illegal instruction?

What file did you provide information for?

What is in hello.go?

Harmen

unread,
Jun 22, 2017, 2:34:27 PM6/22/17
to Nathan Kerr, golang-nuts, luc...@kryptus.com
On Thu, Jun 22, 2017 at 11:32:27AM -0700, Nathan Kerr wrote:
> >
> > I try to build a simple hello world with
> > cgo_enabled=1 goarch=mips goos=linux go build hello.go | Result:
> > illegal instruction

They need to be captitals:

$ GOARCH=mips GOOS=linux go build


> >
> > file information: ELF 32-bit MSB executable, MIPS, MIPS32 version 1
> > (SYSV), statically linked, not stripped
> >
> > Also, tried to build with another compile (CC=/path/to/Compiler) instead
> > the one go uses. Also illegal instruction
> > on my router tp-linkwdr3500.
> >
> > If anyone could give me a hint, I would be grateful.
> >
> > Thanks,
> > Lucas.
> >
> >
> > On Thursday, June 22, 2017 at 11:37:08 AM UTC-3, Ian Lance Taylor wrote:
> >>
> >> On Wed, Jun 21, 2017 at 7:48 PM, lucas.w...@gmail.com
> >> <luc...@kryptus.com> wrote:
> >> >
> >> > Since go1.8 added support for mips32, why can't build a simple hello
> >> world
> >> > for a mips architecture system?
> >> >
> >> > I successfully build programs for arm architecture, but could not do
> >> the
> >> > same for mips.
> >>
> >> Tell us precisely what you did and precisely what happened.
> >>
> >> Ian
> >>
> >
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

luc...@kryptus.com

unread,
Jun 22, 2017, 2:59:19 PM6/22/17
to golang-nuts, luc...@kryptus.com
I'm building on linux amd64 system and want to execute the go binary on mips arch.

Go1.8

The binary is successfully compiled, but when I execute it on my specific architecture (mips), I got an illegal instruction.

The file information I provided is about the go binary compiled.

It's a hello world in go --> https://gobyexample.com/hello-world

I was able to run helloworld for mips but I had to build an image based on eglibc (glibc), instead of uClibc (based on this tutorial: http://akagi201.org/post/golang-on-openwrt/)
I would like to be able to run without changing the base image.

Harmen, I build it with capitals like you said (only typed with lowercase here).

Ian Lance Taylor

unread,
Jun 22, 2017, 4:21:57 PM6/22/17
to luc...@kryptus.com, golang-nuts
On Thu, Jun 22, 2017 at 9:19 AM, <luc...@kryptus.com> wrote:
> I try to build a simple hello world with
> cgo_enabled=1 goarch=mips goos=linux go build hello.go | Result: illegal
> instruction
>
> file information: ELF 32-bit MSB executable, MIPS, MIPS32 version 1 (SYSV),
> statically linked, not stripped
>
> Also, tried to build with another compile (CC=/path/to/Compiler) instead the
> one go uses. Also illegal instruction
> on my router tp-linkwdr3500.

Please show us precisely what you did. Don't transcribe--that just
leads to confusion like the use of lower case when you meant upper
case. Cut and paste from the terminal.

Please show us precisely what happened. What was the exact and
complete output of running the program on the MIPS board?

Try running the program on the MIPS board using a debugger to find out
exactly what the illegal instruction was.

Ian

kty...@gmail.com

unread,
Jun 22, 2017, 5:36:37 PM6/22/17
to golang-nuts, luc...@kryptus.com
This can be related to issue #18162
If you compile a normal go program for mips32 with the current compiler and run it on hardware without FPU, this is the error message you see.
The preferred solution is for the compiler to emmit soft float code. It has been implemented but did not make it into go-1.9.
As a work around, if you can recompile the kernel, you can add floating point emulation to the kernel.

luc...@kryptus.com

unread,
Jul 3, 2017, 8:58:38 AM7/3/17
to golang-nuts, luc...@kryptus.com
Sorry for not replying sooner. 

Building with CGO_ENABLED=1 GOARCH=mips GOOS=linux go build hello.go
I got an Illegal Instruction running on my mips board. 

I used a debugger to find out what was going on but it did not gave better 
answers why the illegal instruction was occuring.

I was able to run it by recompiling the kernel with floating point emulation activated.

Thanks,
Lucas.

luc...@kryptus.com

unread,
Aug 10, 2017, 4:46:31 PM8/10/17
to golang-nuts, luc...@kryptus.com
Sorry for my confused responses before. 
The thing was my hardware board lacked fpu, so that's why I could not 
make it work a simple hello world compiled with: GOOS=linux GOARCH=mips go build hello.go
because the go compiler did not emmit softfloat.

For other people who are facing the same problem, it can be done without modifying 
the original image (withouting recompiling the kernel) by using the patch on this issue:

I got it done using the Vladimir patches with the following command:
env GOOS=linux GOARCH=mips GOMIPS=r2softfloat /home/lucas/Desktop/bin/go build -a helloworld.go

It seems ematsu shared his backport also recently, with a newest patch (with some fixes).


I would like to ask one question. I am trying to use c code in a go application. I was able to do the crosscompile 
of the code for an arm board, but not for a mips board. The go build command I am using:

env CGO_ENABLED=1 GOOS=linux GOARCH=mips GOMIPS=r2softfloat CC=/home/lucas/OpenWrt-SDK-ar71xx-generic_gcc-5.3.0_glibc-2.19.Linux-x86_64/staging_dir/toolchain-mips_34kc_gcc-5.3.0_glibc-2.19/bin/mips-openwrt-linux-gccgo CGO_LDFLAGS="-s -w -static-libgo -L/home/lucas/OpenWrt-SDK-ar71xx-generic_gcc-5.3.0_glibc-2.19.Linux-x86_64/staging_dir/target-mips_34kc_glibc-2.19/usr/lib" /home/lucas/Desktop/go-mips32/bin/go build -a -v mypackage/program

It generates a binary but when i try to run on my board, it gives me the following: 'program' not found

When do I set a cross compile (CC), does it overwrite the go build compiler with softfloat ? 
Do I have the compile linking full static? 

I am not sure if this is the best place to ask these questions, so I'm sorry if this forum is not the right place.

Lucas.
Reply all
Reply to author
Forward
0 new messages