ARM交叉编译

3

我在我的x86 Ubuntu12.04 linux上安装了ARM交叉编译工具链。

现在我有arm-linux-gnueabi-gcc交叉编译器。

我编写了一个简单的hello world程序,如下所示:

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

我使用以下命令进行交叉编译:

arm-linux-gnueabi-gcc -o a a.c -static

编译后的二进制文件如下所示:

$ file a a: ELF 32-bit LSB 可执行文件,ARM,版本 1 (SYSV),静态连接,适用于 GNU/Linux 2.6.31,未剥离

但是,如果我在 QEMU 上的 ARM Debian Linux 上运行此二进制文件时,会出现“非法指令”的错误。

然而,如果我在我的 ARM Debian Linux 上使用 gcc 编译相同的源代码,它可以正常运行。

两个二进制文件的文件结果相同。请问我的交叉编译的 ARM 二进制文件有什么问题吗?

谢谢您的帮助。

meltdown@meltdown-VirtualBox:/var/www$ arm-linux-gnueabi-readelf --file-header --arch-specific a
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x8881
  Start of program headers:          52 (bytes into file)
  Start of section headers:          368052 (bytes into file)
  Flags:                             0x5000002, has entry point, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
      Number of program headers:         7
  Size of section headers:           40 (bytes)
  Number of section headers:         30
  Section header string table index: 27
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_denormal: Needed
  Tag_ABI_FP_exceptions: Needed
  Tag_ABI_FP_number_model: IEEE 754
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: int
  Tag_ABI_HardFP_use: SP and DP
  Tag_CPU_unaligned_access: v6
  Tag_DIV_use: Not allowed
1个回答

4

请运行readelf命令以确定您的交叉编译器针对哪个ARM架构?我们需要查看这个目标是否被Qemu支持。

arm-linux-gnueabi-readelf --file-header --arch-specific a

此外,您可以在模拟系统中检查dmesg的输出。它应该包含非法指令的描述。
此外,您可以在调试器中运行程序,以查看哪个指令失败。

我认为我的交叉编译器制作了ARMv7二进制文件,但我的QEMU正在模拟ARMv5...谢谢你的帮助! - daehee
你能告诉我如何将交叉编译的二进制版本更改为ARMv5,或者如何使用QEMU模拟ARMv7 CPU吗?或者给我一些参考文献? - daehee
如果你想针对armv5,除非你的工具链支持multilib,否则你应该使用另一个工具链。 - Christophe Vu-Brugier
对于Qemu,您应该使用-cpu选项指定要使用的CPU。 - Christophe Vu-Brugier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接