ELF32 Cross-ABI Auditing, VFP Hard-Float Traps & Missing PT_INTERP ENOENT
Inspect raw Elf32_Ehdr fields (ELFCLASS32, EM_ARM=40, EF_ARM_ABI_FLOAT_HARD=0x400) and trace why a missing /lib/ld-linux-armhf.so.3 makes execve() return ENOENT!
What you'll learn
- Audit
52-byteElf32_Ehdrheaders ande_flagsbitmasks (0x05000400EABI5 hard-float vs0x05000200soft-float) before copying binaries to/srv/nfs/rootfs. - Diagnose why
./app: No such file or directory(ENOENT) occurs on an existing, executableARMbinary whenPT_INTERPis absent from the target rootfs. - Verify
DT_NEEDEDshared-library presence andGLIBC_2.xxversion ceilings against the target rootfs sysroot.
The Most Misleading Error Message in Embedded Linux Bring-Up
Picture this rite of passage: you cross-compile hello.c on your workstation, copy hello into /srv/nfs/rootfs/root/, verify ls -l ./hello shows -rwxr-xr-x 1 root root 9824, type ./hello on the ARMv7-A serial console, and the shell prints: -sh: ./hello: not found (or No such file or directory, errno = ENOENT = 2)! Engineers lose hours re-running chmod +x or blaming NFS caching on a file standing right in front of them.
Tracing Linux kernel fs/binfmt_elf.c explains every byte of what happened. First, if you accidentally compiled with host gcc (e_machine = EM_X86_64 = 62), elf_check_arch() rejects the header immediately with -ENOEXEC (Exec format error). Second, if you compiled with arm-linux-gnueabihf-gcc (EM_ARM = 40, EF_ARM_ABI_FLOAT_HARD = 0x400), elf_check_arch() passes, load_elf_binary() reads the PT_INTERP program header (/lib/ld-linux-armhf.so.3), calls open_exec() on that loader path inside your minimal BusyBox rootfs, gets -ENOENT because /lib was left empty, and returns -ENOENT to user space!
Figure 6.1a. execve() returns ENOEXEC when e_machine != EM_ARM, and returns ENOENT when PT_INTERP is missing in rootfs!
mental model: PT_INTERP inside a dynamically linked ELF executable works like a #! /bin/sh shebang line at the top of a script: if /bin/sh itself is missing from the rootfs, the kernel reports No such file or directory even though your script file is right there!
Where it stops working: Unlike text shebangs read as ASCII lines, PT_INTERP is a binary program-header table slice parsed only after fs/binfmt_elf.c validates e_machine (EM_ARM) and ELFCLASS32.
definition (PT_INTERP Program Header): An ELF program-header entry (p_type = 3) in dynamically linked executables storing the NUL-terminated filesystem path of the user-space runtime dynamic linker (such as /lib/ld-linux-armhf.so.3 on 32-bit ARM hard-float Linux) that the kernel maps and transfers initial control to before the application's own _start entry point runs.
- Step 1. Catching EM_X86_64 (62) or soft-float (0x200) on the host takes 5 milliseconds with readelf -h.
- Step 2. Note symlink chains: if ld-linux-armhf.so.3 -> ld-2.35.so, BOTH files must exist in rootfs/lib!
- Step 3. DT_NEEDED lists direct dependencies only (unlike ldd, it never executes foreign code).
- Step 4. Building against the SDK's target sysroot guarantees symbol versions never outrun target libc.so.6.
Figure 6.1b. Verify EM_ARM(40), e_flags 0x400 (hard-float), PT_INTERP in rootfs/lib, and matching sysroot glibc.
Inside the 52-Byte Elf32_Ehdr and VFPv3 Calling Convention
Every 32-bit ARM executable starts with a fixed 52-byte Elf32_Ehdr structure that you can parse directly in C without external libraries: bytes 0..3 hold 0x7F, 'E', 'L', 'F'; byte 4 (EI_CLASS) is 1 (ELFCLASS32); byte 5 (EI_DATA) is 1 (ELFDATA2LSB little-endian); uint16_t e_type at 0x10 is 2 (ET_EXEC) or 3 (ET_DYN PIE); uint16_t e_machine at 0x12 is 40 (EM_ARM); and uint32_t e_flags at 0x24 holds 0x05000400 (EF_ARM_EABI_VER5 | EF_ARM_ABI_FLOAT_HARD).
Why are armel (0x200 soft-float) and armhf (0x400 hard-float) mutually incompatible at the binary interface? Under AAPCS base (soft/softfp), double calc(double a, double b) passes a in integer core registers r0:r1 and b in r2:r3. Under AAPCS-VFP (hard, -mfloat-abi=hard -mfpu=neon-vfpv4 on Cortex-A7), a passes in hardware VFP register d0 and b in d1! If an armhf caller jumped into a softfp libm.so, sin(1.57) would read uninitialized junk from r0:r1 — which is why Debian/Linux assigns them completely separate PT_INTERP paths!
Figure 6.1b. Byte offsets 0x00..0x27 of Elf32_Ehdr expose architecture, endianness, EABI version, and VFP float ABI.
| Build Mistake | Elf32_Ehdr / PT_INTERP State | Target Shell Error | Exact Fix |
|---|---|---|---|
Compiled with gcc instead of $(CROSS_COMPILE)gcc | ELFCLASS64, e_machine = 62 (EM_X86_64) | ./app: Exec format error (ENOEXEC) | Compile with arm-linux-gnueabihf-gcc (CC=$(CROSS_COMPILE)gcc) |
Compiled with arm-linux-gnueabi-gcc (soft) | e_flags & 0x200, PT_INTERP = /lib/ld-linux.so.3 | ./app: No such file or directory (ENOENT) | Switch to arm-linux-gnueabihf-gcc (-mfloat-abi=hard) |
Minimal BusyBox rootfs missing /lib/ld-*.so* | PT_INTERP = /lib/ld-linux-armhf.so.3 absent in /srv/nfs/rootfs | ./app: No such file or directory (ENOENT) | Copy toolchain sysroot lib/ld-linux-armhf.so.3 + libc.so.6 (cp -a preserving symlinks!) or link -static |
Host cross-gcc (glibc 2.39) vs target (glibc 2.35) | .gnu.version_r demands GLIBC_2.38 | libc.so.6: version 'GLIBC_2.38' not found | Compile against the target rootfs SDK --sysroot |
learner@ksa:~/gl-e00/m06-l01$ bat -H 18 -H 38 elf32_armhf_contract.h File: elf32_armhf_contract.h /* SPDX-License-Identifier: GPL-2.0 */ #ifndef KSA_ELF32_ARMHF_CONTRACT_H #define KSA_ELF32_ARMHF_CONTRACT_H #include <stdint.h> #define KSA_ELFCLASS32 1U #define KSA_ELFDATA2LSB 1U #define KSA_ET_REL 1U #define KSA_ET_EXEC 2U #define KSA_ET_DYN 3U #define KSA_EM_ARM 40U #define KSA_EM_X86_64 62U #define KSA_PT_INTERP 3U #define KSA_EF_ARM_EABI_VER5 0x05000000U #define KSA_EF_ARM_ABI_FLOAT_SOFT 0x00000200U #define KSA_EF_ARM_ABI_FLOAT_HARD 0x00000400U #define KSA_ARMHF_INTERP_PATH "/lib/ld-linux-armhf.so.3" struct ksa_elf32_ehdr { uint8_t e_ident[16]; uint16_t e_type; uint16_t e_machine; uint32_t e_version; uint32_t e_entry; uint32_t e_phoff; uint32_t e_shoff; uint32_t e_flags; uint16_t e_ehsize; uint16_t e_phentsize; uint16_t e_phnum; uint16_t e_shentsize; uint16_t e_shnum; uint16_t e_shstrndx; }; _Static_assert(sizeof(struct ksa_elf32_ehdr) == 52U, "Elf32_Ehdr is exactly 52 bytes"); struct ksa_elf32_phdr { uint32_t p_type; uint32_t p_offset; uint32_t p_vaddr; uint32_t p_paddr; uint32_t p_filesz; uint32_t p_memsz; uint32_t p_flags; uint32_t p_align; }; #endif /* KSA_ELF32_ARMHF_CONTRACT_H */- EF_ARM_ABI_FLOAT_HARD (0x400) marks VFP register argument passing paired with /lib/ld-linux-armhf.so.3.
- 52-byte Elf32_Ehdr matches the SVR4 / ARM EABI specification identically on LP64 and ILP32 hosts.
elf(5) / readelf(1)(5) GL-E00 Quick Reference elf(5) / readelf(1)(5) SYNOPSIS arm-linux-gnueabihf-readelf -h -l -d -V target_app file target_app DIAGNOSTIC COMMANDS readelf -h app Verify Class: ELF32, Machine: ARM, Flags: Version5 EABI, hard-float ABI. readelf -l app Inspect INTERP header -> [Requesting program interpreter: /lib/ld-linux-armhf.so.3]. readelf -d app List direct (NEEDED) shared libraries without running host ldd! cp -a sysroot/lib/* Always use -a (archive/no-dereference) when populating rootfs/lib symlinks! Kernel Systems Academy Lesson 6.1 elf(5) / readelf(1)(5) GL-E00 quick reference · full manual page: man 5 elf(5) / readelf(1) Simulating fs/binfmt_elf.c Architecture and PT_INTERP Checks in C11
In verify.c, we synthesize three compact binary ELF images in memory — an EM_X86_64 accidental host build, a soft-float armel build (/lib/ld-linux.so.3), and a clean armhf (0x05000400) build (/lib/ld-linux-armhf.so.3) — and run them through an exact C11 model of fs/binfmt_elf.c's execve() validator against an armhf rootfs.
Watching -ENOEXEC (-8) vs -ENOENT (-2) emerge from 50 lines of C11 makes the No such file or directory mystery unforgettable.
learner@ksa:~/gl-e00/m06-l01$ bat -H 56 -H 63 verify.c File: verify.c #define _POSIX_C_SOURCE 200809L #include <errno.h> #include <stdint.h> #include <stdio.h> #include <string.h> struct mini_ehdr { uint8_t ident[16]; uint16_t type; uint16_t machine; uint32_t version, entry, phoff, shoff, flags; uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstrndx; }; struct mini_phdr { uint32_t type, offset, vaddr, paddr, filesz, memsz, flags, align; }; static size_t build_test_elf(uint8_t *buf, uint8_t cls, uint16_t mach, uint32_t eflags, const char *interp) { struct mini_ehdr eh = {0}; struct mini_phdr ph = {0}; size_t ilen = strlen(interp) + 1U; memcpy(eh.ident, "\x7f" "ELF", 4U); eh.ident[4] = cls; eh.ident[5] = 1U; /* LSB */ eh.type = 2U; /* ET_EXEC */ eh.machine = mach; eh.version = 1U; eh.phoff = (uint32_t)sizeof(eh); eh.flags = eflags; eh.ehsize = (uint16_t)sizeof(eh); eh.phentsize = (uint16_t)sizeof(ph); eh.phnum = 1U; ph.type = 3U; /* PT_INTERP */ ph.offset = (uint32_t)(sizeof(eh) + sizeof(ph)); ph.filesz = (uint32_t)ilen; ph.memsz = (uint32_t)ilen; memcpy(buf, &eh, sizeof(eh)); memcpy(buf + sizeof(eh), &ph, sizeof(ph)); memcpy(buf + sizeof(eh) + sizeof(ph), interp, ilen); return sizeof(eh) + sizeof(ph) + ilen; } static int sim_arm_binfmt_elf(const uint8_t *img, size_t len, const char *rootfs_interp) { struct mini_ehdr eh; struct mini_phdr ph; if (len < sizeof(eh) || memcmp(img, "\x7f" "ELF", 4U) != 0) return -ENOEXEC; memcpy(&eh, img, sizeof(eh)); if (eh.ident[4] != 1U || eh.machine != 40U) return -ENOEXEC; if (eh.phnum >= 1U && eh.phoff + sizeof(ph) <= len) { memcpy(&ph, img + eh.phoff, sizeof(ph)); if (ph.type == 3U && ph.offset + ph.filesz <= len) { const char *req = (const char *)(img + ph.offset); if (strcmp(req, rootfs_interp) != 0) return -ENOENT; /* open_exec(PT_INTERP) failed! */ } } return ((eh.flags & 0x400U) != 0U) ? 0 : -EINVAL; } int main(void) { uint8_t img_x64[160], img_soft[160], img_hf[160]; size_t n1 = build_test_elf(img_x64, 2U, 62U, 0U, "/lib64/ld-linux-x86-64.so.2"); size_t n2 = build_test_elf(img_soft, 1U, 40U, 0x05000200U, "/lib/ld-linux.so.3"); size_t n3 = build_test_elf(img_hf, 1U, 40U, 0x05000400U, "/lib/ld-linux-armhf.so.3"); int r_x64 = sim_arm_binfmt_elf(img_x64, n1, "/lib/ld-linux-armhf.so.3"); int r_soft = sim_arm_binfmt_elf(img_soft, n2, "/lib/ld-linux-armhf.so.3"); int r_hf = sim_arm_binfmt_elf(img_hf, n3, "/lib/ld-linux-armhf.so.3"); if (r_x64 != -ENOEXEC || r_soft != -ENOENT || r_hf != 0) return 1; printf("BINFMT_ELF x86_64_rc=%d(ENOEXEC) armel_rc=%d(ENOENT_missing_interp) armhf_rc=%d(OK)\n", r_x64, r_soft, r_hf); return 0; }- Wrong ELF class or e_machine != 40 (EM_ARM) fails elf_check_arch() immediately with -ENOEXEC (-8).
- Valid EM_ARM header whose PT_INTERP path is absent from rootfs returns -ENOENT (-2) from execve()!
Tracing Exact Return Codes (-ENOEXEC vs -ENOENT)
Notice how cleanly BINFMT_ELF separates -8 (-ENOEXEC, Exec format error) on the EM_X86_64 image from -2 (-ENOENT, No such file or directory) on the EM_ARM image requesting /lib/ld-linux.so.3 on an armhf-only rootfs.
Whenever a target shell reports not found or No such file or directory on a binary you know exists, run readelf -l binary | grep interpreter on your host — 99% of the time, either PT_INTERP or its symlink target (ld-2.xx.so) is missing in rootfs/lib/.
learner@ksa:~/gl-e00/m06-l01gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e00/m06-l01./verifyBINFMT_ELF x86_64_rc=-8(ENOEXEC) armel_rc=-2(ENOENT_missing_interp) armhf_rc=0(OK)learner@ksa:~/gl-e00/m06-l01learner@ksa:~/gl-e00/m06-l01strace -e trace=write,exit_group ./verifywrite(1, "BINFMT_ELF x86_64_rc=-8(ENOEXEC)"..., 83) = 83exit_group(0) = ?+++ exited with 0 +++Symlink Dereferencing and Cross-ldd Security/Diagnostic Traps
When building minimal BusyBox root filesystems (GL-E03), copy shared libraries from your cross-toolchain's libc/lib/ using cp -a (--archive, which implies -d --no-dereference) rather than plain cp -r! Plain cp duplicates multi-megabyte .so files three times (libc.so.6, libc-2.35.so, etc.) or drops relative symlink targets.
Keep one tiny -static diagnostic binary on /bin during early bring-up so you can always test kernel execve() independently of /lib.
Running Host x86_64 ldd on Cross-Compiled ARM Binaries:
- Symptom: Running
ldd ./arm_appon Ubuntu printsnot a dynamic executableeven thoughfile ./arm_appsaysdynamically linked! - Root Cause:
lddworks by invoking the host's dynamic linker (ld-linux-x86-64.so.2) withLD_TRACE_LOADED_OBJECTS=1, which cannot load32-bitEM_ARMbinaries (and is unsafe on untrusted ELFs). - Fix: Never use host
lddon cross-binaries; always usearm-linux-gnueabihf-readelf -d ./arm_appandreadelf -l ./arm_app.
Copying ld-linux-armhf.so.3 Symlink Without Its Underlying Target File:
- Symptom:
ls /srv/nfs/rootfs/lib/ld-linux-armhf.so.3appears in directory listings, yet./appon the target STILL fails withNo such file or directory(ENOENT)! - Root Cause: On toolchains where
ld-linux-armhf.so.3is a symbolic link pointing told-2.35.so(or/usr/arm-linux-gnueabihf/lib/...absolute host path!), a dangling symlink causesopen_exec()inside the target's root namespace to return-ENOENT. - Fix: Inspect
ls -la rootfs/lib/to verify every symlink is relative (-> ld-2.35.so) and its target regular file exists insiderootfs/lib/.
Guided Practice
Extend sim_arm_binfmt_elf() in verify.c to validate buffer-overflow-safe PT_INTERP NUL termination (memchr(img + ph.offset, '\0', ph.filesz) != NULL). What would happen inside a naive C host tool if a corrupted or truncated ELF file had ph.filesz = 24 with no '\0' byte before ph.offset + ph.filesz?
Verify that strcmp(req, ...) without a bounded memchr check reads past img + len, and test your fix with a synthetic non-NUL-terminated PT_INTERP payload.
Independent Practice
Inspect your host's own ./verify binary by writing a 35-line C11 program dump_self_elf.c that opens "./verify" with fopen("./verify", "rb"), reads the first 64 bytes, and prints EI_CLASS, EI_DATA, and uint16_t e_machine at offset 0x12.
Verify that on x86_64 Linux it prints EI_CLASS=2 (ELF64) and e_machine=62 (EM_X86_64), contrasting directly against EI_CLASS=1 and e_machine=40.
Verification Checks
Key takeaways
- A valid
ARMv7-ALinux user-space binary carriesELFmagic0x7F 'E' 'L' 'F',EI_CLASS = ELFCLASS32 (1),e_machine = EM_ARM (40), ande_flagsbitEF_ARM_ABI_FLOAT_HARD (0x400). - Executing an
EM_ARM (40)binary on anx86_64host failself_check_arch()withENOEXEC(Exec format error), whereas executing anarmhfbinary on anARMrootfs missing itsPT_INTERP(/lib/ld-linux-armhf.so.3) makesexecve()returnENOENT(No such file or directory). - Mixing soft-float (
EF_ARM_ABI_FLOAT_SOFT = 0x200,r0..r3float passing) with hard-float (0x400,VFPv3s0..s15/d0..d7registers) breaks calling conventions and requests incompatible/lib/ld-linux.so.3vs/lib/ld-linux-armhf.so.3loaders. DT_NEEDEDentries and.gnu.version_rtags (GLIBC_2.xx) bind binaries to the sysroot's minimumglibcversion; compiling with a newer host cross-toolchain than the target rootfs fails inld.soat launch.