在AllWinner设备上运行交叉编译的二进制文件会出现“未找到”错误。

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

running cross compiled binary on AllWinner device gives not found error

问题

I want to cross compile a C++ application for AllWinner board running OpenWRT. I've found the toolchain and am trying to build a Hello World app using CMake, as described in the CMake docs. So, I have an allwinner.cmake file containing:

# the name of the target operating system
set(CMAKE_SYSTEM_NAME Linux)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER  gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)

# where is the target environment located
set(CMAKE_FIND_ROOT_PATH gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/)

# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Also, I have a CMakeLists.txt with these contents:

project(hello)
add_executable(hello main.cpp)

I am building the app using this command:

cmake -B build -DCMAKE_TOOLCHAIN_FILE=allwinner.cmake
-DCMAKE_BUILD_TYPE=Release . && cmake --build build

After that, I receive the hello binary, copy it to the board in /tmp, and try to run it there as follows:

# cd /tmp
# ./hello
sh: ./hello: not found

I do not understand why I receive this error and what it means. Running ldd hello gives me:

# ldd hello
	/lib/ld-linux-aarch64.so.1 (0x7fb5587000)
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fb543b000)
	libm.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7fb541b000)
	libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)

This output appears to be correct. Could anybody explain what I am doing wrong?

英文:

I want to cross compile a C++ application for AllWinner board runing OpenWRT. I've found the toolchain and trying to build Hello world app using CMake as described in CMake docs. So I have allwinner.cmake file containing

# the name of the target operating system
set(CMAKE_SYSTEM_NAME Linux)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER  gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)

# where is the target environment located
set(CMAKE_FIND_ROOT_PATH gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu/)

# adjust the default behavior of the FIND_XXX() commands:
# search programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# search headers and libraries in the target environment
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Also I have CMakeLists.txt with this contents:

project(hello)
add_executable(hello main.cpp)

I am building the app using this command:

cmake -B build -DCMAKE_TOOLCHAIN_FILE=allwinner.cmake
-DCMAKE_BUILD_TYPE=Release . && cmake --build build

After that i receive hello binary, copy it to the board in /tmp and trying to run it there as follows:

# cd /tmp
# ./hello
sh: ./hello: not found

I do not understand why i receive this error and what does it mean. Running ldd hello gives me:

# ldd hello
	/lib/ld-linux-aarch64.so.1 (0x7fb5587000)
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fb543b000)
	libm.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7fb541b000)
	libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)

This output looks as something correct.

Could anybody explain me what i am doing wrong?

答案1

得分: 0

不允许从 /tmp 运行可执行文件。

您可以使用 mount 命令检查已挂载的文件系统,查看 /tmp 是否以 noexec 属性挂载。

英文:

It is not allowed to run executables from /tmp.

You may check the mounted filesystems with the command mount and see /tmp is mounted with noexec attribute.

答案2

得分: 0

我使用了无效的工具链。当我比较两个ldd输出时,检测到了这个问题。正如我之前所写的,我的二进制文件给出了这个:

# ldd hello
    /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fb543b000)
    libm.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7fb541b000)
    libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)

而系统的ls的ldd输出如下:

# ldd /bin/ls
    /lib/ld-musl-aarch64.so.1 (0x7f7b940000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7f7b920000)
    libc.so => /lib/ld-musl-aarch64.so.1 (0x7f7b940000)

如你所见,区别在于libc。我的二进制文件希望使用ld-linux-aarch64,而ls二进制文件使用ld-musl-aarch64。我已经下载了aarch64-musl交叉编译器,使用它构建了我的二进制文件,现在它可以正常工作了。

英文:

I used invalid toolchain. It was detected when I compared two ldd outputs. As I've wrote earlier my binary gives this:

# ldd hello
	/lib/ld-linux-aarch64.so.1 (0x7fb5587000)
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x7fb543b000)
	libm.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7fb541b000)
	libc.so.6 => /lib/ld-linux-aarch64.so.1 (0x7fb5587000)

And ldd of system's ls gives this:

# ldd /bin/ls
	/lib/ld-musl-aarch64.so.1 (0x7f7b940000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x7f7b920000)
	libc.so => /lib/ld-musl-aarch64.so.1 (0x7f7b940000)

As you can see the difference is in libc. My binary wants to use ld-linux-aarch64 and ls binary use ld-musl-aarch64. I've downloaded aarch64-musl cross-compiler, built my binary using it and voila, it works now.

huangapple
  • 本文由 发表于 2023年5月11日 06:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222887.html
匿名

发表评论

匿名网友

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

确定