ARM Hard-Float EABI, ELF Sections, and Object Inspection
Understand from first principles how compiled C and assembly functions exchange arguments in Cortex-A7 integer and VFPv4 registers under AAPCS32, why cold reset traps floating-point instructions, and how ELF32 separates file-backed sections from zero-filled .bss RAM.
What you'll learn
- Identify ARM machine and floating-point ABI evidence in an ELF object.
- Trace argument, return-value, and preserved-register obligations across an AAPCS call.
- Classify initialized data and zero-initialized storage without confusing file and RAM size.
Why Binary Objects Need a Hardware Calling Convention
Once a C compiler translates float gain(float a, float b); into 32-bit ARMv7-A machine instructions, parameter names, C types, and header files vanish completely. All that exists in silicon is a bank of 32-bit general-purpose registers (r0..r15), an optional Vector Floating-Point / NEON register file (s0..s31 / d0..d31), and raw bytes in RAM. If main.o (compiled on Monday) calls gain.o (compiled into a library on Tuesday), how do those two independently compiled .o files agree on which register holds a, where the return value lands, which registers gain is allowed to overwrite, and how the stack pointer is aligned? That machine-level treaty is the Application Binary Interface (ABI)—specifically the Arm Architecture Procedure Call Standard (AAPCS32 / EABI).
Trace both halves of the Cortex-A7 register contract in Figure 1.2a before reading a line of assembly:
- Core Integer & Pointer Contract (
AAPCS32): The first four32-bit integer or pointer arguments pass inr0..r3, and32-bit results return inr0(64-bit integers use an aligned register pair such asr0:r1). Arguments5and beyond spill onto the stack ([sp, #0],[sp, #4], ...). Crucially, registers split into two ownership classes:r0..r3andr12(IP) are caller-saved scratch (any function you call may destroy them freely), whereasr4..r11andr13(SP) are callee-saved (if a callee touchesr4, it must pushr4on entry and restore it before returning; noter9often carries platform-reserved state in bootloaders, so avoid casual scratch use). At every public call boundary,SPmust be8-byte aligned (SP mod 8 == 0). - Soft-Float vs VFPv4 Hard-Float (
-mfloat-abi=hard): Under the base/soft-float ABI (arm-none-eabi-mfloat-abi=soft),floatvalues are bit-copied into integer registersr0..r3and computed via software routines (or softfp wrappers). Under the hard-float variant (arm-linux-gnueabihf,.eabi_attribute 28, 1),floatanddoublearguments pass directly inVFPv4hardware registerss0..s15/d0..d7(s16..s31/d8..d15are callee-saved). Mixing the two is catastrophic if unchecked: a soft-float caller places1.5f(0x3FC00000) intor0, while a hard-float callee reads uninitializeds0! Equally vital for bare-metal engineers: on a coldCortex-A7reset, theVFPv4/NEONsilicon is powered off/disabled by hardware—executing even a singlevadd.f32or compiler-generatedvldrstruct copy triggers an Undefined Instruction exception until privileged startup enablesCP15CPACR(CP10/CP11) and setsFPEXC.EN = 1!
Figure 1.2a. AAPCS32 register ownership — integer caller/callee split (left) vs VFPv4 hard-float register passing and its bare-metal CPACR/FPEXC reset precondition (right).
mental model: Two independently machined hydraulic manifolds can share an identical bolt pattern (matching C prototypes) yet route pressure into different ports (r0 vs s0), starving the actuator with zero external compiler warning.
Where it stops working: Unlike mechanical ports, .ARM.attributes let linkers and readelf audit register conventions statically, yet even a perfectly matched hard-float binary will fault at runtime until privileged startup enables the VFP coprocessor in .
definition (AAPCS32, Hard-Float EABI & ELF32 Section/Segment Roles): AAPCS32 places the first four 32-bit integer/pointer arguments in r0..r3, requires callees to preserve r4..r11 and 8-byte-aligned SP, and under the hard-float variant (.eabi_attribute 28, 1, Tag_ABI_VFP_args: VFP registers) passes eligible floating-point values in s0..s15 / d0..d7. An Executable and Linkable Format (ELF32) object records EM_ARM (40) and .ARM.attributes metadata alongside .text (machine code), .rodata (constants), .data (initialized globals stored in the file), and SHT_NOBITS .bss (zero-initialized globals occupying zero file bytes whose p_memsz - p_filesz RAM extent startup code must explicitly zero).
Walk through the four-stage contract from C call site to runtime RAM layout below:
- Step 1. Step 1 (C Call Site): Word arguments enter in r0..r3; hard-float variants pass eligible floats in VFPv4 registers s0..s15.
- Step 2. Step 2 (Object Contract): readelf -h -A proves ELFCLASS32, little-endian LSB, EM_ARM (40), cortex-a7, and Tag_ABI_VFP_args.
- Step 3. Step 3 (File Bytes): .text, .rodata, and .data occupy p_filesz bytes on flash/TFTP; raw .bin strips headers and symbols.
- Step 4. Step 4 (Runtime Memory): SHT_NOBITS .bss adds (p_memsz - p_filesz) RAM bytes that bare-metal startup must zero before main().
Figure 1.2b. Never trust a .o or .bin filename: inspect ELF32 machine class, VFP ABI attributes, and .bss expansion before execution.
Inspecting AAPCS32 Leaf Assembly, VFPv4 Attributes, and ELF Headers
Why does an ELF file have both Section Headers (.text, .rodata, .data, .bss, .ARM.attributes) and Program Headers (PT_LOAD), and why do we keep the .elf file even when flashing a stripped .bin? Sections are the linker's view during compilation: relocatable .o files have sections and relocation records (0 program headers) so ld can merge all .text blocks and patch symbol addresses. Program headers (Elf32_Phdr) are the loader's view in a linked executable: each 32-byte PT_LOAD entry tells a loader to copy p_filesz initialized bytes from flash/load address p_paddr (LMA) into p_memsz runtime bytes at p_vaddr (VMA), where the extra p_memsz - p_filesz tail is SHT_NOBITS .bss! Why did ELF designers make .bss NOBITS? Because storing 64 KiB of literal 0x00 bytes for static uint8_t rx_ring[65536]; inside SPI NOR flash or a TFTP packet wastes storage and boot time when a 4-instruction loop in _start can zero RAM in microseconds!
Examine the two Cortex-A7 leaf functions in target.S below. Notice .eabi_attribute 28, 1 (Tag_ABI_VFP_args = 1), which stamps hard-float ABI metadata into .ARM.attributes. Because neither add_words (r0 <- r0 + r1) nor add_floats (s0 <- s0 + s1) touches callee-saved r4..r11 / d8..d15 or calls another subroutine, neither allocates stack space, and both return via bx lr (which branches to LR and sets Thumb state from LR[0]—a subroutine return, distinct from hardware exception returns covered in Module 2).
learner@ksa:~/gl-e01/m01-l02$ bat target.S File: target.S .syntax unified .cpu cortex-a7 .fpu neon-vfpv4 .eabi_attribute 28, 1 .arm .text .global add_words .type add_words, %function add_words: add r0, r0, r1 bx lr .size add_words, .-add_words .global add_floats .type add_floats, %function add_floats: vadd.f32 s0, s0, s1 bx lr .size add_floats, .-add_floatsAuditing 32-Bit ELF Headers and NOBITS Segment Expansion in C11
Compare the exact byte offsets in Figure 1.2b against elf32_phdr_audit.c below. Notice that our x86_64 host tool never casts raw bytes to Elf64_Ehdr or unaligned pointers—it decodes little-endian 16-bit and 32-bit fields explicitly via rd_le16() and rd_le32(). Inside the 52-byte Elf32_Ehdr, bytes 0..3 hold 0x7F 'E' 'L' 'F', byte 4 (EI_CLASS = 1) proves ELF32, byte 5 (EI_DATA = 1) proves two's-complement little-endian, offset +18 (e_machine = 40) proves EM_ARM (rejecting 62 = EM_X86_64!), offset +40 (e_ehsize) equals 52, and offset +42 (e_phentsize) equals 32. Inside the 32-byte Elf32_Phdr, .text (128 B) + .rodata (16 B) + .data (8 B) give p_filesz = 152, while 64 B of .bss gives p_memsz = 216 (bss_tail = 216 - 152 = 64), rejecting both p_memsz < p_filesz (151) and 32-bit p_vaddr wraparound (0xFFFFFF80 + 216).
Figure 1.2b. Exact Elf32_Ehdr / Elf32_Phdr byte offsets (top) and why runtime p_memsz (216 B) exceeds file-backed p_filesz (152 B) by the 64-byte SHT_NOBITS .bss tail (bottom).
learner@ksa:~/gl-e01/m01-l02$ bat -H 14 -H 15 -H 16 -H 17 -H 18 -H 19 -H 20 -H 21 -H 25 -H 26 -H 27 -H 28 -H 29 -H 30 -H 31 -H 32 -H 33 -H 34 elf32_phdr_audit.c File: elf32_phdr_audit.c #include <assert.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> #include <stdio.h> static uint16_t rd_le16(const unsigned char *p) { return (uint16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8)); } static uint32_t rd_le32(const unsigned char *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } static bool valid_arm_ehdr(const unsigned char *eh, size_t n) { return n >= 52U && eh[0] == 0x7fU && eh[1] == 'E' && eh[2] == 'L' && eh[3] == 'F' && eh[4] == 1U && eh[5] == 1U && rd_le16(eh + 18) == 40U && rd_le16(eh + 40) == 52U && rd_le16(eh + 42) == 32U; } static bool valid_pt_load(const unsigned char *ph, uint32_t *bss_tail) { uint32_t p_type = rd_le32(ph + 0), p_vaddr = rd_le32(ph + 8); uint32_t p_paddr = rd_le32(ph + 12), p_filesz = rd_le32(ph + 16); uint32_t p_memsz = rd_le32(ph + 20); if (p_type != 1U || p_memsz < p_filesz || p_memsz == 0U) return false; if (p_memsz - 1U > UINT32_MAX - p_vaddr) return false; if (p_filesz > 0U && p_filesz - 1U > UINT32_MAX - p_paddr) return false; *bss_tail = p_memsz - p_filesz; return true; } static void wr_le32(unsigned char *p, uint32_t v) { for (unsigned i = 0U; i < 4U; ++i) p[i] = (unsigned char)(v >> (8U * i)); } int main(void) { unsigned char eh[52] = {0x7fU, 'E', 'L', 'F', 1U, 1U, 1U}; eh[18] = 40U; eh[40] = 52U; eh[42] = 32U; assert(valid_arm_ehdr(eh, sizeof eh) && !valid_arm_ehdr(eh, 51U)); eh[4] = 2U; assert(!valid_arm_ehdr(eh, sizeof eh)); eh[4] = 1U; eh[18] = 62U; assert(!valid_arm_ehdr(eh, sizeof eh)); eh[18] = 40U; unsigned char ph[32] = {0U}; wr_le32(ph + 0, 1U); /* PT_LOAD */ wr_le32(ph + 8, 0x87800000U); /* p_vaddr */ wr_le32(ph + 12, 0x877ff400U); /* p_paddr */ wr_le32(ph + 16, 152U); /* p_filesz = 128 + 16 + 8 */ wr_le32(ph + 20, 216U); /* p_memsz = 152 + 64 (.bss) */ uint32_t bss = 0U; assert(valid_pt_load(ph, &bss) && bss == 64U); wr_le32(ph + 20, 151U); assert(!valid_pt_load(ph, &bss)); wr_le32(ph + 20, 216U); wr_le32(ph + 8, 0xffffff80U); assert(!valid_pt_load(ph, &bss)); printf("ELF32 EM_ARM=40 ehdr=52B phdr=32B filesz=152 memsz=216 bss=%u\n", bss); return 0; }- Verify ELF magic, ELFCLASS32 (1), little-endian data (1), EM_ARM (40), and 52/32-byte header sizes.
- Require p_memsz >= p_filesz so NOBITS .bss growth is accepted while truncated memory spans fail.
- Check p_memsz - 1 <= UINT32_MAX - p_vaddr to reject 32-bit virtual-address wraparound.
Cross-Assembling for Cortex-A7 and Verifying ELF Metadata
Run the native elf32_phdr_audit verifier first, then cross-assemble target.S with clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm and inspect the real emitted ELF32 header and aeabi attribute section via readelf -h -A target.o. Match Class: ELF32, Machine: ARM, Size of this header: 52 (bytes), Number of program headers: 0 (relocatable .o), and Tag_ABI_VFP_args: VFP registers directly against our byte decoder.
learner@ksa:~/gl-e01/m01-l02gcc -std=c11 -Wall -Wextra -Werror -pedantic elf32_phdr_audit.c -o elf32_phdr_auditlearner@ksa:~/gl-e01/m01-l02./elf32_phdr_auditELF32 EM_ARM=40 ehdr=52B phdr=32B filesz=152 memsz=216 bss=64learner@ksa:~/gl-e01/m01-l02clang --target=armv7a-none-eabi -mcpu=cortex-a7 -marm -c target.S -o target.olearner@ksa:~/gl-e01/m01-l02readelf -h -A target.oELF 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: REL (Relocatable file) Machine: ARM Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 252 (bytes into file) Flags: 0x5000000, Version5 EABI Size of this header: 52 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 40 (bytes) Number of section headers: 5 Section header string table index: 1Attribute Section: aeabiFile Attributes Tag_CPU_name: "cortex-a7" Tag_CPU_arch: v7 Tag_CPU_arch_profile: Application Tag_ARM_ISA_use: Yes Tag_THUMB_ISA_use: Thumb-2 Tag_FP_arch: VFPv4 Tag_Advanced_SIMD_arch: NEONv1 with Fused-MAC Tag_ABI_VFP_args: VFP registers Tag_CPU_unaligned_access: None Tag_MPextension_use: Allowed Tag_DIV_use: Allowed in v7-A with integer division extension Tag_Virtualization_use: TrustZone and Virtualization Extensionslearner@ksa:~/gl-e01/m01-l02learner@ksa:~/gl-e01/m01-l02strace -e trace=write,exit_group \./elf32_phdr_auditwrite(1, "ELF32 EM_ARM=40 ehdr=52B phdr=32"..., 62) = 62exit_group(0) = ?+++ exited with 0 +++File Payload vs Runtime RAM Footprint and Float-ABI Trade-Offs
Always archive the linked .elf and linker .map file beside any objcopy -O binary output! When objcopy -O binary app.elf app.bin strips an executable for U-Boot or the i.MX6ULL Boot ROM, it discards Elf32_Ehdr, Elf32_Phdr, .symtab, .ARM.attributes, and every trace of .bss (p_memsz - p_filesz). Without app.elf, you cannot verify Tag_ABI_VFP_args or map a crash PC (0x878001A4) back to a C line. Equally important: readelf proving Tag_FP_arch: VFPv4 is a static file fact, whereas CPACR + FPEXC.EN enabling vadd.f32 on silicon is a privileged runtime fact.
| Build / Artifact Choice | Register / File Impact | Hardware Precondition | Failure Mode When Mismatched |
|---|---|---|---|
-mfloat-abi=soft (integer EABI) | Floats pass in r0..r3; soft-fp helpers linked | Pure integer Cortex-A7 pipeline (0 VFP setup) | Link error or silent NaN/garbage if mixed with -mfloat-abi=hard objects |
-mfloat-abi=hard (VFPv4) | Floats pass in s0..s15 (0 GPR copy overhead) | CPACR CP10/11 + FPEXC.EN must be enabled first | Undefined Instruction trap on first vldr/vpush/vadd.f32 after reset |
Linked ELF32 executable (.elf) | Retains 52 B Ehdr, Phdr, .symtab, .ARM.attributes | Parsed by host tools / debuggers (readelf, gdb) | Rejected by i.MX6ULL Boot ROM if flashed raw without IVT/.bin extraction |
Stripped raw binary (objcopy -O binary) | p_filesz bytes only (0 .bss bytes, 0 headers) | _start must relocate .data and zero .bss | Silent corruption if .bss goes unzeroed or load address drifts from p_vaddr |
Later: Lesson m01-l03 moves from binary contracts to hardware schematics, deriving active-low CMOS sink currents from Kirchhoff's Voltage Law and mapping package balls to 32-bit GPIO register masks.
Common Production Pitfalls
warning: Symptom: Bare-metal C code compiled with arm-linux-gnueabihf-gcc -O2 crashes with an Undefined Instruction exception inside main() even though the C source contains zero float or double variables! Root Cause: With -mfloat-abi=hard and -mfpu=neon-vfpv4 enabled by default on gnueabihf toolchains, the optimizer auto-vectorizes struct copies and memset loops using 64/128-bit vldr/vstr VFP/NEON instructions before _start enables CPACR (CP10/CP11) and FPEXC.EN. Fix: Compile early bare-metal boot/ code with -mfloat-abi=soft -mno-unaligned-access (or explicitly enable CP10/CP11 + FPEXC.EN in assembly before calling C).
warning: Symptom: A custom bootloader reserves p_filesz bytes (152 B) in OCRAM for an ELF32 PT_LOAD segment, and running the loaded firmware immediately corrupts the stack or descriptor placed at p_vaddr + 152. Root Cause: The loader allocated runtime memory from p_filesz instead of p_memsz (216 B), forgetting the 64-byte SHT_NOBITS .bss region that occupies zero file bytes. Fix: Always reserve p_memsz bytes in target RAM, copy p_filesz bytes from p_offset, and zero [p_vaddr + p_filesz, p_vaddr + p_memsz).
Practice & Retrieval
Guided Practice: Decoding a Bare-Metal OCRAM Segment Header
Step 1 (Little-Endian Decode): Given Elf32_Phdr slices ph[8..11] = {0x00, 0x70, 0x90, 0x00}, ph[16..19] = {0x80, 0x04, 0x00, 0x00}, and ph[20..23] = {0x00, 0x06, 0x00, 0x00}, compute p_vaddr, p_filesz, p_memsz, and the NOBITS .bss byte count (0x00907000, 1152 B, 1536 B, leaving 384 B of .bss). Step 2 (LMA vs VMA): If .data (64 B initialized, 128 B .bss) has p_vaddr = 0x87810000 and p_paddr = 0x87800480, state which address objcopy -O binary uses to position those 64 bytes (p_paddr, the LMA). Step 3 (Attribute Check): Predict how Tag_ABI_VFP_args changes in readelf -A if .eabi_attribute 28, 1 is changed to .eabi_attribute 28, 0.
Independent Practice: Multi-Segment ELF32 Overlap & Entry Verifier (elf32_image_check.c)
Extend elf32_phdr_audit.c into elf32_image_check.c accepting e_entry (Elf32_Ehdr + 24) and an array of Elf32_Phdr structures. Verify that e_entry falls inside a PT_LOAD segment carrying PF_X (0x1 in p_flags at +24), (p_vaddr % p_align) == (p_offset % p_align) holds whenever power-of-two p_align > 1 (+28), and no two PT_LOAD segments overlap in [p_vaddr, p_vaddr + p_memsz) or [p_paddr, p_paddr + p_filesz).
Key takeaways
AAPCS32passes the first four32-bit word arguments in caller-savedr0..r3, requires callees to preserver4..r11and8-byte-alignedSP, and returns32-bit scalars inr0.- The
VFPv4hard-float (-mfloat-abi=hard,.eabi_attribute 28, 1) passes floats ins0..s15/d0..d7, but faults on bare-metalCortex-A7reset untilCPACR(CP10/CP11) andFPEXC.ENare enabled. ELF32stores code and constants in.text/.rodataand initialized variables in.data(p_fileszbytes), whileSHT_NOBITS.bssaddsp_memsz - p_fileszzero-initialized RAM bytes with zero file bloat.- Always retain
.elfartifacts alongside stripped.binimages and verifyELFCLASS32,EM_ARM(40), and.ARM.attributeswithreadelf -h -A. - assigns ordinary word-sized arguments to r0–r3 and requires preserved-register obligations across calls.
- The hard-float procedure-call variant can pass eligible floating-point arguments in VFP registers.
- Inspect class, machine metadata, and relevant ABI attributes.