Skip to content
systemcoding.dev
Embedded Linux

Identify a Binary's Architecture and ABI With file and readelf

Read a Linux binary's CPU architecture, bit width, float ABI and loader with file, readelf -h, readelf -A and xxd, before you copy it to a board.

7 min read
Summarize in AI
Flat illustration of source code passing through a compiler and linker into a binary, with the Linux, GNU and C++ logos.

Most cross-compiling mistakes can be caught in ten seconds without touching the board, because a Linux program announces what it was built for in its first few bytes. The tools to read that announcement are already on your computer. file gives a summary, readelf prints the details, and xxd shows the raw bytes if you want to see where the information comes from. This post uses all three on binaries built for 32-bit Arm, 64-bit Arm and x86-64, so you can tell at a glance whether a file will run on your board. It assumes GCC 13 cross compilers and binutils 2.42, and every command is safe to run, because none of them executes the program.

The quick answer from file

file reads the first part of a file and names what it is. For a program built on your own computer:

Shell
file ./sysinfo
Text
./sysinfo: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=2c1f..., for GNU/Linux 3.2.0, not stripped

And for the same program built for a 32-bit Arm board and a 64-bit Arm board:

Text
./sysinfo-armhf: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=5d0e..., for GNU/Linux 3.2.0, not stripped
./sysinfo-arm64: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=8be2..., for GNU/Linux 3.2.0, not stripped

Break one down. ELF is the executable format Linux uses. 32-bit or 64-bit is the word size. LSB means little-endian, the byte order. pie executable means position-independent, the default for modern GCC. The architecture follows: x86-64, ARM or ARM aarch64. EABI5 is the Arm application binary interface, which defines how functions pass arguments. dynamically linked and the interpreter path tell you the program needs a dynamic loader. stripped or not stripped says whether the symbol table is still there.

For a first check, three fields matter: the word size, the architecture and the interpreter. If those match your board, the program is a candidate to run. If they do not, it will not.

The details from readelf

file summarizes, and readelf -h prints the real header fields. The cross toolchain has its own readelf, but any recent binutils reads any architecture's ELF files, so the host's works too:

Shell
readelf -h sysinfo-armhf
Text
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:                              DYN (Position-Independent Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x4d1
  Flags:                             0x5000400, Version5 EABI, hard-float ABI

Three lines carry most of the value. Class is ELF32 or ELF64. Machine is the processor family: ARM, AArch64, Advanced Micro Devices X86-64 or RISC-V. And Flags on 32-bit Arm names the floating-point ABI: hard-float ABI means floating-point values are passed in FPU registers, and soft-float ABI means they are passed in integer registers. A hard-float program does not run on a system built for soft float, and the error is the confusing not found from the shell when the loader is missing. The entry point address and the exact flags value on your build will differ.

Where the answer comes from in the bytes

The facts above are stored in a fixed header at the start of the file, so you can read them with xxd. The first four bytes are the magic number 7f 45 4c 46, which is the byte 7f followed by the letters ELF. The fifth byte is the class, 01 for 32-bit and 02 for 64-bit. The sixth is the byte order, 01 for little-endian. And the two bytes at offset 18 are the machine type:

Shell
xxd -s 0 -l 6 sysinfo-armhf
xxd -s 18 -l 2 sysinfo-armhf
xxd -s 18 -l 2 sysinfo-arm64
xxd -s 18 -l 2 sysinfo
Text
00000000: 7f45 4c46 0101                           .ELF..
00000012: 2800                                     (.
00000012: b700                                     ..
00000012: 3e00                                     >.

The machine numbers are 0x28 (40) for Arm, 0xb7 (183) for AArch64 and 0x3e (62) for x86-64, stored little-endian, which is why the low byte comes first. RISC-V is 0xf3 (243). You will rarely need the raw bytes, but knowing where file gets its answer explains why it is reliable: it reads the header, not the file name.

The float ABI and CPU level with readelf -A

For Arm, the compiler records which instruction set and floating-point conventions it used in a section of attributes, and readelf -A prints them. This is the closest thing to a label on a 32-bit Arm binary that says what processor it expects:

Shell
readelf -A sysinfo-armhf
Text
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_enum_size: int
  Tag_ABI_VFP_args: VFP registers
  Tag_CPU_unaligned_access: v6

Read the lines that matter for a board. Tag_CPU_arch: v7 means the code needs Armv7 or later, so it will not run on an Armv6 board such as the original Raspberry Pi. Tag_FP_arch: VFPv3-D16 is the floating-point hardware assumed, and Tag_ABI_VFP_args: VFP registers is the hard-float calling convention. If your board's CPU is older than the tag says, the program dies with Illegal instruction. This output explains why before you spend an hour in a debugger.

On AArch64 there is no such section for the basics, since the 64-bit instruction set has a floating-point unit and a single calling convention. readelf -h is enough.

The dynamic loader and the libraries it needs

A dynamically linked program asks for a loader and a set of shared libraries. readelf -l shows the loader, and readelf -d the libraries:

Shell
readelf -l sysinfo-armhf | grep interpreter
readelf -d sysinfo-armhf | grep -E 'NEEDED|RPATH|RUNPATH'
Text
      [Requesting program interpreter: /lib/ld-linux-armhf.so.3]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]

The interpreter path must exist on the board. The NEEDED list must be satisfiable from the board's library directories. A static binary has neither, and file reports it as statically linked. ldd is the tool people reach for here, but ldd works by running the loader, so it does not work on a foreign architecture binary, and on a hostile binary it is unsafe. Use readelf, which only reads.

A one-line survey of a directory

When you have a whole root filesystem or a release folder, check everything at once. This pipeline lists each distinct kind of binary and how many there are:

Shell
find stage -type f -perm -u+x -exec file -b {} + | cut -d, -f1-2 | sort | uniq -c | sort -rn
Text
     41 ELF 32-bit LSB shared object, ARM
     12 ELF 32-bit LSB pie executable, ARM
      3 POSIX shell script
      1 ELF 64-bit LSB pie executable, x86-64

The last line is the one to chase: one x86-64 executable in a tree that should be all Arm. It slipped in from a host tool run during the install. Finding it takes one command, where finding it on the board takes a failed boot.

What to do with each answer

What you seeWhat it tells youWhat to check next
ELF 64-bit ... x86-64Built for your computerCC and --host settings
ELF 32-bit ... ARM on a 64-bit-only systemWrong word size for a pure 64-bit userlandUse the aarch64 toolchain
hard-float ABI on a soft-float boardABI mismatchRebuild with the board's toolchain
Tag_CPU_arch: v7 on an Armv6 boardCPU level too highLower -march or -mcpu
Interpreter not on the boardLoader mismatchCompare with ls /lib/ld-* on the board

A script that checks a binary against the board

Once you know the three facts your board needs, turn them into a check that runs in the build. This script takes a binary and compares its class, machine and float ABI with the expected values, and exits with a non-zero status on a mismatch, so a release job fails before packaging:

check-arch.shShell
#!/bin/sh
set -eu

bin="$1"
class=$(readelf -h "$bin" | awk '/Class:/ {print $2}')
machine=$(readelf -h "$bin" | awk -F: '/Machine:/ {gsub(/^ +/, "", $2); print $2}')
flags=$(readelf -h "$bin" | awk -F: '/Flags:/ {print $2}')

[ "$class" = "ELF32" ] || { echo "FAIL $bin: class $class, want ELF32"; exit 1; }
[ "$machine" = "ARM" ] || { echo "FAIL $bin: machine $machine, want ARM"; exit 1; }
case "$flags" in
  *hard-float*) ;;
  *) echo "FAIL $bin: not a hard-float binary ($flags)"; exit 1 ;;
esac
echo "ok $bin: $class $machine hard-float"
Shell
sh check-arch.sh stage/usr/bin/sysinfo
sh check-arch.sh /usr/bin/ls
Text
ok stage/usr/bin/sysinfo: ELF32 ARM hard-float
FAIL /usr/bin/ls: class ELF64, want ELF32

The second run shows the failure mode the script is for: a host binary that ended up in the staging tree. Run the script over every executable in the tree with find stage -type f -perm -u+x -print0 | xargs -0 -n1 sh check-arch.sh and the whole release is checked in one line.

Remember to set the expected values for your own board. A 64-bit board wants ELF64 and AArch64, and a soft-float board wants the opposite float check.

The primary sources for this post are the ELF specification and Arm AAPCS32 document.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and GNU logos.
Kernels

Write a Pin Controller Driver With pinctrl

Write a Linux 6.12 pin controller driver: pinctrl_desc, pins, groups and functions, the set_mux callback, and the device tree states that consumers select.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.