在ARM上交叉编译Go和Goc时出现“symbol not found”错误消息。

huangapple go评论101阅读模式
英文:

Cross-compilation Go and Goc "symbol not found" error message on ARM

问题

我无处可问,一切都很具体,搜索和谷歌都没有用。我在这上面花了几十个小时。

我正在尝试为我的LinkSys MR8300 V1.1路由器构建一个Go程序,该路由器具有armv7l处理器(uname -a输出),即ARM v7,32位CPU。我已经在路由器上安装了OpenWrt操作系统。

这个应用程序的想法是连接USB硬币接收器,并在投入硬币时启用30分钟的互联网。我在Ubuntu桌面上有一个可以理解该设备的工作应用程序,桌面上一切正常。

然而,路由器是32位的,而且是ARM架构,所以事情有点不同和更复杂。

我使用交叉编译,因为路由器只有19MB的磁盘空间(并且并非所有开发包都可用)。

为了进行交叉编译,我使用x86_64 Ubuntu桌面和OpenWrt提供的工具链。我已经过了需要配置和制作工具链的阶段。我能够在我的路由器上编译和执行基本的C程序,例如:

#include <stdio.h>

int main()
{
  printf("Hello World");
  return 0;
}

甚至可以编译和执行Go程序,例如:

package main

func main() {
        println("Coin acceptor device control program")
}

当我将二进制文件上传到路由器时,它们可以正常工作,没有任何问题。

但是,当我尝试在路由器内部使用USB库时,问题就出现了。由于这是交叉编译,我应该提到我拥有的环境变量:

STAGING_DIR=/home/ro/work/openwrt/staging_dir
TOOLCHAIN_DIR=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi
LDCFLAGS=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
LD_LIBRARY_PATH=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
LDFLAGS=-L/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
CGO_LDFLAGS=-L/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
CFLAGS=-I/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/include
CC=arm-openwrt-linux-gcc
GOARCH=arm
CROSS_COMPILE=arm-openwrt-linux-gcc
CGO_ENABLED=1
GOOS=linux
GOARM=7

使用这些环境变量,go get github.com/karalabe/hid命令按预期工作(在我的/home/ro/go/pkg/linux_arm/github.com/karalabe文件夹中有hid.a二进制文件)。

这个URL https://github.com/karalabe/hid 是我用于USB的库,标题上写着“Gopher Interface Devices (USB HID)”。然而,它依赖于一些C库...所以这就是GOC发挥作用的地方。该库说:

这两个依赖项都直接嵌入到存储库中,并使用CGO进行封装,使hid包自包含且可通过go get获取。

正如我之前提到的,我可以在我的桌面上编译简单的应用程序并将它们上传到路由器上,这些应用程序可以正常工作!然而,当我尝试使用该库时,事情有点偏离脚本。

这是我拥有的应用程序:

main.go

package main

import (
        "fmt"
)

func main() {
        println("Hello")

        devices := FindAll(0x0079)

        if len(devices) == 0 {
                fmt.Println("No coin acceptor found")
                return
        }

        println("Found coin acceptor")
}

device.go

package main

import (
	"errors"
	"github.com/karalabe/hid"
)

type Device struct {
	info hid.DeviceInfo
	dev  *hid.Device

	Name string
	PID  uint16
}

func (device *Device) Open() error {
	if device.dev != nil {
		return errors.New("device: Device handle is not null, but Open() called")
	}

	var err error
	device.dev, err = device.info.Open()

	return err
}

func (device *Device) Close() error {
	if device.dev == nil {
		return errors.New("device: Close() called for Device with null handle")
	}

	err := device.dev.Close()
	device.dev = nil

	return err
}

// Find all products with the given product ID.
func FindAll(product uint16) []Device {
	deviceInfo := hid.Enumerate(0x1e7d, product)

	if len(deviceInfo) == 0 {
		return []Device{}
	}

	devices := make([]Device, len(deviceInfo))

	for i, info := range deviceInfo {
		devices[i] = Device{
			info: info,
			Name: info.Product,
			PID:  product,
		}
	}

	return devices
}

我用来构建的命令:

go build ./main.go ./device.go

二进制文件生成了,一切看起来正常。直到我将二进制文件上传并在路由器上运行它。它基本上显示:

Error relocating ./main: __pthread_cond_timedwait_time64: symbol not found
Error relocating ./main: __nanosleep_time64: symbol not found
Error relocating ./main: __stat_time64: symbol not found
Error relocating ./main: __clock_gettime64: symbol not found

就是这样。它甚至没有打印“Hello”。

需要注意的一件事是,由于某种原因,它引用(并抱怨)64位函数。所以,总结一下:

  • 这是32位ARM CPU。
  • 我可以运行简单的C和Go程序,所以我假设我的C和Go编译器为32位CPU提供了正确的二进制文件。
  • 当我尝试连接库时,没有编译器(我假设该应用程序使用Go和Goc进行编译)抱怨使用64位函数。
  • 二进制文件本身抱怨64位函数。

我想知道我漏掉了什么?我应该向编译器指定哪些标志,以便不使用64位函数?也许这些USB库中有一些用C编写的东西?我已经没有任何想法,基本上陷入了困境,因为我无法在嵌入式OpenWrt设备上使用我的USB设备。

objdump -p main输出:

objdump -p main

main:     file format elf32-little

Program Header:
    PHDR off    0x00000034 vaddr 0x00010034 paddr 0x00010034 align 2**2
         filesz 0x00000120 memsz 0x00000120 flags r--
  INTERP off    0x00000154 vaddr 0x00010154 paddr 0x00010154 align 2**0
         filesz 0x00000018 memsz 0x00000018 flags r--
    LOAD off    0x00000000 vaddr 0x00010000 paddr 0x00010000 align 2**16
         filesz 0x001164ec memsz 0x001164ec flags r-x
    LOAD off    0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**16
         filesz 0x00013cc4 memsz 0x00029c94 flags rw-
 DYNAMIC off    0x00116f08 vaddr 0x00136f08 paddr 0x00136f08 align 2**2
         filesz 0x000000f8 memsz 0x000000f8 flags rw-
    NOTE off    0x0000016c vaddr 0x0001016c paddr 0x0001016c align 2**2
         filesz 0x00000064 memsz 0x00000064 flags r--
     TLS off    0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**2
         filesz 0x00000000 memsz 0x00000008 flags r--
   STACK off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**4
         filesz 0x00000000 memsz 0x00000000 flags rw-
   RELRO off    0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**0
         filesz 0x00000294 memsz 0x00000294 flags r--

Dynamic Section:
  NEEDED               libgcc_s.so.1
  NEEDED               libc.so
  INIT                 0x0001437c
  FINI                 0x000a7abc
  INIT_ARRAY           0x00136d6c
  INIT_ARRAYSZ         0x00000004
  FINI_ARRAY           0x00136d70
  FINI_ARRAYSZ         0x00000004
  HASH                 0x000101d0
  GNU_HASH             0x00010acc
  STRTAB               0x00012820
  SYMTAB               0x000114c0
  STRSZ                0x00001568
  SYMENT               0x00000010
  DEBUG                0x00000000
  PLTGOT               0x00137000
  PLTRELSZ             0x00000360
  PLTREL               0x00000011
  JMPREL               0x0001401c
  REL                  0x00014014
  RELSZ                0x00000008
  RELENT               0x00000008
  VERNEED              0x00013ff4
  VERNEEDNUM           0x00000001
  VERSYM               0x00013d88

Version References:
  required from libgcc_s.so.1:
    0x0b792655 0x00 02 GCC_3.5

file main输出:

main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-armhf.so.1, Go BuildID=VEoRmm2_70EkDLdXjTm0/APdquCT6lGlorzOlV3Un/jnQVIeEEaRXWGrmNQ-mE/Bjx-ldvZODg_GFcJuIgW, not stripped
英文:

I have nowhere to ask, everything is so specific, search and googling doesn't work. Spent tens of hours on this.

I'm trying to build a Go program for my LinkSys MR8300 V1.1 router, which has armv7l processor (uname -a output), which is ARM v7, 32-bit CPU. I have OpenWrt operating system installed on the router.

The idea of the app is to connect USB coin acceptor, and enable internet for 30 minutes when you drop a coin. I have working app on the Ubuntu desktop which understands the device, and on desktop everything's fine.

However, the router is 32 bit, and ARM, so things are little bit different and more complicated.

cat /proc/cpuinfo on my router gives something like this:

processor       : 0
model name      : ARMv7 Processor rev 5 (v7l)
BogoMIPS        : 26.81
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 5

I'm using cross-compilation, since the router has only 19MB of disk space (and not all of the development packages are available).

For cross-compilation I'm using x86_64 Ubuntu desktop, and toolchain provided by OpenWrt. I'm already past the point when you need to configure and make the toolchain. I was able to compile and execute the basic C programs on my router, like:

#include &lt;stdio.h&gt;

int main()
{
  printf(&quot;Hello World&quot;);
  return 0;
}

And even Go programs like:

package main

func main() {
        println(&quot;Coin acceptor device control program&quot;)
}

When I upload binaries to the router they work, there is no any problem with that.

Problems start when I try to use USB library inside the router. Since it's cross-compilation, I should mention environment variables I have:

STAGING_DIR=/home/ro/work/openwrt/staging_dir
TOOLCHAIN_DIR=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi
LDCFLAGS=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
LD_LIBRARY_PATH=/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
LDFLAGS=-L/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
CGO_LDFLAGS=-L/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/usr/lib
CFLAGS=-I/home/ro/work/openwrt/staging_dir/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi/include
CC=arm-openwrt-linux-gcc
GOARCH=arm
CROSS_COMPILE=arm-openwrt-linux-gcc
CGO_ENABLED=1
GOOS=linux
GOARM=7

With these environment variables go get github.com/karalabe/hid command works as expected (there is hid.a binary in my /home/ro/go/pkg/linux_arm/github.com/karalabe folder).

This url https://github.com/karalabe/hid is the library I use for USB, the title says "Gopher Interface Devices (USB HID)". However, it has dependencies on some C libraries... So here is where GOC comes into play I guess. The library says:

> Both of these dependencies are vendored directly into the repository and wrapped using CGO, making the hid package self-contained and go-gettable

As I mentioned before, I can compile simple apps on my Desktop and upload them to the router, and these apps work! However, when I try to use the library, things go little bit off the script.

Here is the app I have:

main.go

package main

import (
        &quot;fmt&quot;
)

func main() {
        println(&quot;Hello&quot;)

        devices := FindAll(0x0079)

        if len(devices) == 0 {
                fmt.Println(&quot;No coin acceptor found&quot;)
                return
        }

        println(&quot;Found coin acceptor&quot;)
}

device.go

package main

import (
	&quot;errors&quot;
	&quot;github.com/karalabe/hid&quot;
)

type Device struct {
	info hid.DeviceInfo
	dev  *hid.Device

	Name string
	PID  uint16
}

func (device *Device) Open() error {
	if device.dev != nil {
		return errors.New(&quot;device: Device handle is not null, but Open() called&quot;)
	}

	var err error
	device.dev, err = device.info.Open()

	return err
}

func (device *Device) Close() error {
	if device.dev == nil {
		return errors.New(&quot;device: Close() called for Device with null handle&quot;)
	}

	err := device.dev.Close()
	device.dev = nil

	return err
}

// Find all products with the given product ID.
func FindAll(product uint16) []Device {
	deviceInfo := hid.Enumerate(0x1e7d, product)

	if len(deviceInfo) == 0 {
		return []Device{}
	}

	devices := make([]Device, len(deviceInfo))

	for i, info := range deviceInfo {
		devices[i] = Device{
			info: info,
			Name: info.Product,
			PID:  product,
		}
	}

	return devices
}

Command I use to build a thing:

go build ./main.go ./device.go

The binary gets produces, and things look normal. Until the point when I upload and run the binary on the router. It pretty much says:

Error relocating ./main: __pthread_cond_timedwait_time64: symbol not found
Error relocating ./main: __nanosleep_time64: symbol not found
Error relocating ./main: __stat_time64: symbol not found
Error relocating ./main: __clock_gettime64: symbol not found

And that's it. It doesn't even print "Hello".

One thing to notice is that it has references to (and complains about) 64-bit functions for some reason. So, to sum this up:

  • This is 32-bit ARM CPU.
  • I can run simple C and Go programs, so I assume that my C and Go compiles provide correct binaries for the 32-bit CPU.
  • When I try to attach the library none of the compilers (I assume the app gets compiled using Go and Goc) complains about using 64-bit functions.
  • The binary itself complains about 64-bit functions.

I'm wondering what am I missing? What flags should I specify to the compiler(s) so there is no 64-bit functions usage? Maybe there is something inside these USB libraries written in C? I kinda ran out of any ideas and pretty much stuck at this point, since I cannot use my USB device with embedded OpenWrt device.

objdump -p main output:

objdump -p main

main:     file format elf32-little

Program Header:
    PHDR off    0x00000034 vaddr 0x00010034 paddr 0x00010034 align 2**2
         filesz 0x00000120 memsz 0x00000120 flags r--
  INTERP off    0x00000154 vaddr 0x00010154 paddr 0x00010154 align 2**0
         filesz 0x00000018 memsz 0x00000018 flags r--
    LOAD off    0x00000000 vaddr 0x00010000 paddr 0x00010000 align 2**16
         filesz 0x001164ec memsz 0x001164ec flags r-x
    LOAD off    0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**16
         filesz 0x00013cc4 memsz 0x00029c94 flags rw-
 DYNAMIC off    0x00116f08 vaddr 0x00136f08 paddr 0x00136f08 align 2**2
         filesz 0x000000f8 memsz 0x000000f8 flags rw-
    NOTE off    0x0000016c vaddr 0x0001016c paddr 0x0001016c align 2**2
         filesz 0x00000064 memsz 0x00000064 flags r--
     TLS off    0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**2
         filesz 0x00000000 memsz 0x00000008 flags r--
   STACK off    0x00000000 vaddr 0x00000000 paddr 0x00000000 align 2**4
         filesz 0x00000000 memsz 0x00000000 flags rw-
   RELRO off    0x00116d6c vaddr 0x00136d6c paddr 0x00136d6c align 2**0
         filesz 0x00000294 memsz 0x00000294 flags r--

Dynamic Section:
  NEEDED               libgcc_s.so.1
  NEEDED               libc.so
  INIT                 0x0001437c
  FINI                 0x000a7abc
  INIT_ARRAY           0x00136d6c
  INIT_ARRAYSZ         0x00000004
  FINI_ARRAY           0x00136d70
  FINI_ARRAYSZ         0x00000004
  HASH                 0x000101d0
  GNU_HASH             0x00010acc
  STRTAB               0x00012820
  SYMTAB               0x000114c0
  STRSZ                0x00001568
  SYMENT               0x00000010
  DEBUG                0x00000000
  PLTGOT               0x00137000
  PLTRELSZ             0x00000360
  PLTREL               0x00000011
  JMPREL               0x0001401c
  REL                  0x00014014
  RELSZ                0x00000008
  RELENT               0x00000008
  VERNEED              0x00013ff4
  VERNEEDNUM           0x00000001
  VERSYM               0x00013d88

Version References:
  required from libgcc_s.so.1:
    0x0b792655 0x00 02 GCC_3.5

file main output:

main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-armhf.so.1, Go BuildID=VEoRmm2_70EkDLdXjTm0/APdquCT6lGlorzOlV3Un/jnQVIeEEaRXWGrmNQ-mE/Bjx-ldvZODg_GFcJuIgW, not stripped

答案1

得分: 1

终于成功了!

我尝试重新构建OpenWrt工具链,但这次我切换到与我的路由器openwrt-21.02类似的分支,然后再次执行了make menuconfigmake命令。这本身并没有起作用,但我稍微改变了我的环境变量,如下所示:

export STAGING_DIR=/home/ro/work/openwrt/staging_dir
export TOOLCHAIN_DIR=$STAGING_DIR/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi
export LDCFLAGS=$TOOLCHAIN_DIR/lib
export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/lib
export LDFLAGS="-L${TOOLCHAIN_DIR}/lib"
export CGO_LDFLAGS="-Xlinker -rpath=${TOOLCHAIN_DIR}/lib/libc.so -static"
export CFLAGS="-I${TOOLCHAIN_DIR}/include"
export CC=arm-openwrt-linux-gcc-11.3.0
export PATH=$TOOLCHAIN_DIR/bin:$PATH
export GOARCH=arm
export CROSS_COMPILE=arm-openwrt-linux-gcc
export CGO_ENABLED=1
export GOOS=linux
export GOARM=7

CGO_LDFLAGS指定了对libc进行静态链接。最终一切都正常工作了,我可以从嵌入式设备访问USB设备!

英文:

Finally!

I tried to re-build OpenWrt toolchain, but this time I switched to a branch similar to my router openwrt-21.02, did make menuconfig and make again. It didn't help by itself, but I slightly changed my env variables this way:

export STAGING_DIR=/home/ro/work/openwrt/staging_dir
export TOOLCHAIN_DIR=$STAGING_DIR/toolchain-arm_cortex-a7+neon-vfpv4_gcc-11.3.0_musl_eabi
export LDCFLAGS=$TOOLCHAIN_DIR/lib
export LD_LIBRARY_PATH=$TOOLCHAIN_DIR/lib
export LDFLAGS=&quot;-L${TOOLCHAIN_DIR}/lib&quot;
export CGO_LDFLAGS=&quot;-Xlinker -rpath=${TOOLCHAIN_DIR}/lib/libc.so -static&quot;
export CFLAGS=&quot;-I${TOOLCHAIN_DIR}/include&quot;
export CC=arm-openwrt-linux-gcc-11.3.0
export PATH=$TOOLCHAIN_DIR/bin:$PATH
export GOARCH=arm
export CROSS_COMPILE=arm-openwrt-linux-gcc
export CGO_ENABLED=1
export GOOS=linux
export GOARM=7

The CGO_LDFLAGS specifies static linking to libc. Finally it all works, and I can access USB device from the embedded device!

huangapple
  • 本文由 发表于 2022年7月20日 03:12:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73042358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定