Golang on OpenWRT MIPS

I have been tracking Golang for quite a while since I came to know about it I guess about 3 years ago primarily because it is very easy to use and build static binaries that just work about anywhere. And no dealing with memory allocation stuff which often lead to frustrations and segmentation fault bugs soaking up hours of your time to solve those.

As a OpenWRT user running a Go program on OpenWRT had been one of my most desired things. So here it is, finally, a hello world program running my TP Link WR740N (which is a MIPS 32 bit CPU, ar71xx in OpenWRT tree):

system type             : Atheros AR9330 rev 1
machine                 : TP-LINK TL-WR741ND v4
processor               : 0
cpu model               : MIPS 24Kc V7.4
BogoMIPS                : 265.42
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 16
extra interrupt vector  : yes
hardware watchpoint     : yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa                     : mips1 mips2 mips32r1 mips32r2
ASEs implemented        : mips16
shadow register sets    : 1
kscratch registers      : 0
package                 : 0
core                    : 0
VCED exceptions         : not available
VCEI exceptions         : not available
package main

import "fmt"

func main() {
        fmt.Println("hello world")
}

First I built it with GOOS=linux GOARCH=mips go build hello but it did not run and gave error “Illegal Instruction”. Then I tried it with GOOS=linux GOARCH=mipsle go build hello which again, did not work because the CPU of this TP Link is big endian, not little endian. After a bit of searching I came across this GoMips guide on Golang’s Github which builds it using GOMIPS=softfloat. I tried the same and my program works! It will now be easy to build complex stuff that runs on embedded devices without resorting to C/C++.

$ GOOS=linux GOARCH=mips GOMIPS=softfloat go build hello
$ scp hello root@IP:/tmp/

root@740n-2:/tmp# ./hello
hello world
root@740n-2:/tmp#                                                                                        

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: