链接器问题,而交叉编译皮质-a8设备

时间:2016-04-01 13:51:58

标签: linux linker arm cross-compiling

我正在使用VAR-SOM-AM33板并且想要运行示例代码,例如在设备上运行hello world,它会给出-sh:./ test:not found error

用于编译代码的工具链是gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux。

代码如下

#include <stdio.h>
    int main(){
    printf("hello world");
    return 0;
}

用于交叉编译文件

arm-linux-gnueabihf-gcc test.c -march=armv7-a -marm -mthumb-interwork -mfloat-abi=hard -mfpu=neon -mtune=cortex-a8 -o test

输出二进制显示如下

test: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.31, BuildID[sha1]=2ce1c5b3d97dac2093fe2cd2d340cdaa9989923f, not stripped

将该文件复制到硬件并运行后显示以下错误

root@am335x-evm:~# ./test 
-sh: ./test: not found

文件权限也会改变

root@am335x-evm:~# chmod +x test 

但结果显示同样未找到错误。

在硬件上运行的演示文件,其架构如下

root@am335x-evm:~# readelf -A /usr/bin/hello
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_VFP_arch: VFPv3
Tag_Advanced_SIMD_arch: NEONv1
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align8_needed: Yes
Tag_ABI_align8_preserved: Yes, except leaf SP
Tag_ABI_enum_size: int
Tag_ABI_HardFP_use: SP and DP

和交叉编译的文件,其架构如下

root@am335x-evm:~# readelf -A test 
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_VFP_arch: VFPv3
Tag_Advanced_SIMD_arch: NEONv1
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align8_needed: Yes
Tag_ABI_align8_preserved: Yes, except leaf SP
Tag_ABI_enum_size: int
Tag_ABI_HardFP_use: SP and DP
Tag_ABI_VFP_args: VFP registers
Tag_CPU_unaligned_access: v6
Tag_unknown_44: 1 (0x1)

硬件cpuinfo如下

root@am335x-evm:~# cat /proc/cpuinfo 
Processor   : ARMv7 Processor rev 2 (v7l)
BogoMIPS    : 718.02
Features    : swp half thumb fastmult vfp edsp thumbee neon vfpv3 tls 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x3
CPU part    : 0xc08
CPU revision    : 2

Hardware    : Variscite VAR-SOM-AM33
Revision    : 0000
Serial      : 0000000000000000

我试过在目标设备上运行ldd命令。

root@am335x-evm:~# ldd
-sh: ldd: not found

所以我怀疑这个问题与链接器有关。 如果我只是编译文件,而不链接它。

arm-linux-gnueabihf-gcc -mtune=cortex-a8 -march=armv7  -O -c test.c -o test 

现在,如果我运行此文件,我会收到此错误。

root@am335x-evm:~# chmod +x test 
root@am335x-evm:~# ./test 
./test: line 1: syntax error: word unexpected (expecting ")")

请建议如何解决此问题。

1 个答案:

答案 0 :(得分:0)

感谢解决方案,我发现链接器出错。

现有文件有链接器 ld-linux.so.3 ,交叉编译文件有链接器 ld-linux-armhf.so.3

root@am335x-evm:/usr/bin# readelf -l hello

Elf file type is EXEC (Executable file)
Entry point 0x82fc
There are 8 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  EXIDX          0x00044c 0x0000844c 0x0000844c 0x00008 0x00008 R   0x4
  PHDR           0x000034 0x00008034 0x00008034 0x00100 0x00100 R E 0x4
  INTERP         0x000134 0x00008134 0x00008134 0x00013 0x00013 R   0x1
      [Requesting program interpreter: /lib/ld-linux.so.3]

交叉编译文件程序头

root@am335x-evm:~# readelf -l test 

Elf file type is EXEC (Executable file)
Entry point 0x82f9
There are 8 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  EXIDX          0x000450 0x00008450 0x00008450 0x00008 0x00008 R   0x4
  PHDR           0x000034 0x00008034 0x00008034 0x00100 0x00100 R E 0x4
  INTERP         0x000134 0x00008134 0x00008134 0x00019 0x00019 R   0x1
      [Requesting program interpreter: /lib/ld-linux-armhf.so.3]

在chang之后链接器程序在目标设备上运行。

root@am335x-evm:~# cd /lib/
root@am335x-evm:/lib# ls -l ld-linux.so.3      
lrwxrwxrwx    1 1000     1000           12 Aug  7  2012 ld-linux.so.3 -> ld-2.12.2.so
root@am335x-evm:/lib# ln -s /li ld-2.12.2.so ld-linux-armhf.so.3
/lib/     /linuxrc
root@am335x-evm:/lib# ln -s /lib/ld-2.12.2.so  ld-linux-armhf.so.3
root@am335x-evm:/lib# ldconfig 
root@am335x-evm:/lib# cd
root@am335x-evm:~# ./test 
hello worldroot@am335x-evm:~#