Go项目交叉编译ARMv6失败,树莓派Zero W运行报非法指令
为树莓派Zero W(ARMv6l)交叉编译Go程序时的架构不匹配问题
我正尝试为ARMv6l架构交叉编译Go程序,目标是在树莓派Zero W上运行。树莓派Zero W的CPU信息如下:
$ cat /proc/cpuinfo processor : 0 model name : ARMv6-compatible processor rev 7 (v6l) BogoMIPS : 697.95 Features : half thumb fastmult vfp edsp java tls CPU implementer : 0x41 CPU architecture: 7 CPU variant : 0x0 CPU part : 0xb76 CPU revision : 7 Hardware : BCM2835 Revision : 9000c1 Serial : 0000000059e0c5a9 Model : Raspberry Pi Zero W Rev 1.1
我执行了如下命令进行交叉编译:
env CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARM=6 GOARCH=arm go build -o build/dht_sensor_exporter-$(VERSION)-linux-armv6 ./cmd/main.go
执行file dht_sensor_exporter-$(VERSION)-linux-armv6查看结果显示正常:
dht_sensor_exporter-$(VERSION)-linux-armv6: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=d84b6c4b0dc4b77b9c65db7bc30823e5dbe1078c, for GNU/Linux 3.2.0, with debug_info, not stripped
但在树莓派Zero W上运行时提示Illegal instruction。经排查,发现交叉编译生成的文件并非对应正确架构,执行如下命令查看:
$ readelf -A dht_sensor_exporter Attribute Section: aeabi File Attributes Tag_CPU_name: "7-A" Tag_CPU_arch: v7 Tag_CPU_arch_profile: Application Tag_ARM_ISA_use: Yes Tag_THUMB_ISA_use: Thumb-2 Tag_FP_arch: VFPv3-D16 Tag_ABI_PCS_wchar_t: 4 Tag_ABI_FP_rounding: Needed Tag_ABI_FP_denormal: Needed Tag_ABI_FP_exceptions: Needed Tag_ABI_FP_number_model: IEEE 754 Tag_ABI_align_needed: 8-byte Tag_ABI_enum_size: int Tag_ABI_VFP_args: VFP registers Tag_CPU_unaligned_access: v6
从Tag_CPU_arch可见实际编译为ARMv7架构。该程序基于GitHub Actions在最新Ubuntu版本构建,我怀疑是在C/C++编译器(arm-linux-gnueabihf-gcc)的使用上存在错误,恳请帮助。
内容的提问来源于stack exchange,提问作者vernak2539
相关产品推荐
相关产品推荐

