【问题标题】:How can I know if an ELF file is for Cortex-A or Cortex-M?我如何知道 ELF 文件是用于 Cortex-A 还是 Cortex-M?
【发布时间】:2022-01-01 09:39:41
【问题描述】:

我在进行二进制分析时有一个问题。对于已为 ARM 架构识别的给定 ELF 文件 (hello.elf),我如何快速知道此 ELF 是用于 Cortex-A 还是 Cortex-M? 更具体地说,我' m 试图识别 Cortex-M 的整个裸机映像(或 RTOS 映像,如 FreeRTOS)。

来自file hello.elf的结果:

% file hello.elf
hello.elf: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, with debug_info, not stripped

我们只能看到这个ELF是给ARM的。 而从readelf -h ./hello.elf的结果来看:

% readelf -h ./hello.elf
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:               0xcb5
  Start of program headers:          52 (bytes into file)
  Start of section headers:          150896 (bytes into file)
  Flags:                             0x5000200, Version5 EABI, soft-float ABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         5
  Size of section headers:           40 (bytes)
  Number of section headers:         19
  Section header string table index: 17

它也仅显示此文件适用于 ARM 架构。 那么有没有其他方法可以快速识别ELF文件的目标架构呢?

【问题讨论】:

  • 是什么让您认为它必须只适用于其中一个?您可以通过检查存在的部分名称或使用 objdump 反汇编并查找向量表来轻松判断它是否是 Cortex-M 的完整裸机映像,但在操作系统下运行的可执行文件可能更难以区分甚至兼容两者都有。
  • @TomV 你能给我更多关于如何做前一个的细节吗?我正在寻找 Cortex-M 的整个裸机映像(或 RTOS 映像,如 FreeRTOS)。
  • arm-none-eabi-objdump -h hello.elf 查找名为 .vectors 或类似名称的部分。 arm-none-eabi-nm hello.elf 也可能会告诉您一些有用的信息。

标签: arm executable elf cortex-m cortex-a


【解决方案1】:

如果您的二进制文件遵循 Arm ELF 规范,则有一个属性部分包含有关 cpu 架构和(如果适用)架构配置文件的信息。该信息可以通过 readelf 提取。请注意,这些信息是编译器和链接器对事物的看法,有时可能是错误的。

下面的第一个示例来自为 Cortex-A8 构建的二进制文件,第二个示例来自为 Cortex-M33 构建的二进制文件。两者都是使用 IAR 工具链构建的。

> readelf -A cortex-a8.out                                                       
Attribute Section: aeabi
File Attributes
  Tag_conformance: "2.10"
  Tag_CPU_arch: v7
  Tag_CPU_arch_profile: Application
  Tag_ARM_ISA_use: Yes
  Tag_THUMB_ISA_use: Thumb-2
  Tag_PCS_config: Bare platform
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: small
  Tag_ABI_VFP_args: compatible
  Tag_CPU_unaligned_access: v6
  Tag_DIV_use: Not allowed

 > readelf -A cortex-m33.out
Attribute Section: aeabi
File Attributes
  Tag_conformance: "2.10"
  Tag_CPU_arch: v8-M.mainline
  Tag_CPU_arch_profile: Microcontroller
  Tag_THUMB_ISA_use: Yes
  Tag_FP_arch: FPv5/FP-D16 for ARMv8
  Tag_PCS_config: Bare platform
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: forced to int
  Tag_ABI_HardFP_use: SP only
  Tag_ABI_VFP_args: compatible
  Tag_CPU_unaligned_access: v6
  Tag_DIV_use: Not allowed

【讨论】:

  • 是的,这正是我要找的。但是,这个-A 参数只适用于那些简单的应用程序。对于 Cortex-M 上编译为两个可执行文件(例如,FreeRTOSDemo_s.axfFreeRTOSDemo_ns.axf)的多世界项目,此参数不会给出任何输出。对此有何建议?谢谢。
  • @AndyMa 此方法要求 .ARM.attributes 部分存在于 elf 文件中。如果缺少此部分,那么您就不走运了,因为 Cortex-A 和 Cortex-M 的 elf 文件之间没有根本区别。
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 2019-11-13
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 2012-10-17
  • 2017-06-07
  • 2015-03-10
相关资源
最近更新 更多