Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross compiling for ARM: Floating point exception when using fixed size array declarations

When cross-compiling a C program for the Raspberry Pi, the resulting executable fails with a Floating Point Exception when executed on the Pi. I could narrow it down to this minimal C program which reproduces the issue:

int main() {
    char x[1];
    return 0;
}

The size (1) does not matter either, same problem with 1024 or any size of the array.

The error does not occur if I

  • remove the char x[1] line
  • replace it with x = malloc(1)
  • move the declaration to global space (would imply some issue with stack vs. heap??)

Cross compiling is done with this docker image:

FROM ubuntu:24.04
ENV BUILD_DIR="/build"
ENV CPATH="/usr/local/include"
WORKDIR /root
RUN apt-get update && \
    apt-get install -y \
        cmake \
        git \
        build-essential \
        gcc-arm-linux-gnueabihf \
        gcc-arm-linux-gnueabi \
        g++-arm-linux-gnueabihf \
        g++-arm-linux-gnueabi 

RUN git clone https://github.com/hannenz/pigpio && \
    cd pigpio && \
    make CROSS_PREFIX=arm-linux-gnueabi- && \
    make install

WORKDIR ${BUILD_DIR}
docker run -it \
    -v /home/hannenz/pidev/projects/pi/kiddyblaster:/build \
    -w /build \
    -e BUILD_DIR=/build/build \
    -e SRC_DIRS=/build/src \
    pidev \
    arm-linux-gnueabi-gcc -o test test.c
scp test raspberry-pi:/home/pi/

and execute it on the Raspberry Pi:

pi@kiddyblaster:~ $ ./test
Floating point exception
pi@kiddyblaster:~ $ echo $?
136

The Raspberry Pi is Pi 2 Model B Rev 1.1 with a fresh installed PiOS Lite (Raspian 12):

pi@kiddyblaster:~ $ cat /proc/cpuinfo | tail -n 14
processor       : 3
model name      : ARMv7 Processor rev 5 (v7l)
BogoMIPS        : 38.40
Features        : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 5

Hardware        : BCM2835
Revision        : a01041
Serial          : 000000006a731e3b
Model           : Raspberry Pi 2 Model B Rev 1.1
pi@kiddyblaster:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm

I'd be glad for any hint on where I can start to debug this. Is it something with the cross compile toolchain? Memory management? Little/Big Endian? something ARM-specific? The stack / heap?

like image 434
Johannes Braun Avatar asked Nov 17 '25 17:11

Johannes Braun


1 Answers

I found out what caused the issue: I need to use the gnueabihf variant of the tools (compiler, linker etc., e.g. arm-linux-gnueabihf-gcc), i think the "hf" stands for "hard float" and refers to the missing FPU on the raspberry pi?

I moved to using the docker images from this repo: https://github.com/tttapa/docker-arm-cross-toolchain and everything works fine. Thanks for your comments and replies

like image 171
Johannes Braun Avatar answered Nov 20 '25 06:11

Johannes Braun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!