GOARCH=mipsle GOOS=linux go build -o web
如果平台是大端环境,需要export GOARCH=mips 另外,这个环境中没有浮点数运算单元,需要增加软浮点GOMIPS="softfloat"
Netgear R6300 v2 (Arm EABI5 DDWRT)
首先通过查看 /proc/cpuinfo 确定 Netgear R6300 v2 是 ARM 系的 CPU,然后下载一个路由器上的可执行文件,进行file查看,可以看到,是 EABI5 编译的:
============= /proc/cpuinfo =============
model name : ARMv7 Processor rev 0 (v7l)
processor : 0
BogoMIPS : 1594.16
Features : half fastmult edsp tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x3
CPU part : 0xc09
CPU revision : 0
model name : ARMv7 Processor rev 0 (v7l)
processor : 1
BogoMIPS : 1594.16
Features : half fastmult edsp tls
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x3
CPU part : 0xc09
CPU revision : 0
Hardware : Northstar Prototype
Revision : 0000
Serial : 0000000000000000
============= /proc/cpuinfo =============
============= file /bin/sh =============
./sh: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-arm.so.1, corrupted section header size
============= file /bin/sh =============
由上,我们可以确定编译参数为:
export GOOS=linux
export GOARCH=arm
export GOARM=5
Xiaomi MT7620 (MIPSle Openwrt)
确定的方法同上,这里贴出来执行结果:
============= /proc/cpuinfo =============
system type : MT7620
machine : Unknown
processor : 0
cpu model : MIPS 24KEc V5.0
BogoMIPS : 385.02
CPUClock : 580
FlashSize : 8192
wait instruction : yes
microsecond timers : yes
tlb_entries : 32
extra interrupt vector : yes
hardware watchpoint : yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc, 0x0ffb, 0x0ffb]
isa : mips1 mips2 mips32r1 mips32r2
ASEs implemented : mips16 dsp
shadow register sets : 1
kscratch registers : 0
core : 0
VCED exceptions : not available
VCEI exceptions : not available
============= /proc/cpuinfo =============
============= file /bin/sh =============
./sh: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked, interpreter /lib/ld-uClibc.so.0, corrupted section header size
============= file /bin/sh =============
由于MIPS系CPU有大小端区别,这里我们可以看到上面红色部分为LSB,即小端CPU,由此,我们确定出编译参数为:
export GOOS=linux
export GOARCH=mipsle
Netgear WNDR3800 (MIPS Openwrt)
============= /proc/cpuinfo =============
system type : Atheros AR7161 rev 2
machine : NETGEAR WNDR3700/WNDR3800/WNDRMAC
processor : 0
cpu model : MIPS 24Kc V7.4
BogoMIPS : 452.19
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
============= /proc/cpuinfo =============
============= file /bin/sh =============
./sh: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-mips-sf.so.1, stripped
============= file /bin/sh =============
这里我们可以看到上面红色部分为MSB,即大端CPU,由此,我们确定出编译参数为:
export GOOS=linux
export GOARCH=mips
但是这时间编译完成并上传很有可能出现如下错误
root@OpenWrt:~# ./hello
Illegal instruction
这是因为 AR7161 CPU并没有默认支持浮点数运算,所以这里有两种方案
重新编译内核并加入浮点数运算模拟器
这种办法的优点是效率较高,而且不折腾,本人便是采用这种办法
下载 Openwrt 源码,并执行 make kernel_menuconfig
然后选择其中的 Kernel Type 勾选上 MIPS FPU Emulator 即可,如下图所示:
然后重新刷入新系统即可
采用 gomini 的 go-mips32 进行编译
源码开源地址**在此**,编译方式参考 https://github.com/xtaci/kcptun/issues/79,由于本人并没有采用这种办法,所以不在此赘述
小贴士:使用 build 参数 -ldflags “-s -w” 可以减少约 0.5M 体积哦~