WSL环境下Arm程序链接出现__aeabi_unwind_cpp_pr0错误求助
Hey there, let's break down why your linking step is failing and how to fix it. The core issue here is likely a mismatch between the toolchain you're using and the bare-metal Cortex-M4 environment you're targeting—plus a few common pitfalls with Ada on microcontrollers.
First: Stop Using arm-linux-gnueabi-ld
The gnat-5-arm-linux-gnueabi package you installed is built for Linux-running ARM devices, not bare-metal microcontrollers like the Cortex-M4. That linker expects to link against Linux system libraries and kernel interfaces, which don't exist on your MCU. Even if you installed gcc-arm-none-eabi, you might still be using the wrong linker binary.
Switch entirely to the bare-metal toolchain's linker: use arm-none-eabi-ld instead of arm-linux-gnueabi-ld for your linking step.
Check Your Linker Script (flash.ld)
Bare-metal MCUs rely entirely on the linker script to define memory layout, and a misconfigured script will throw errors every time. Verify these key points:
- Memory regions: Make sure you've correctly defined your MCU's FLASH and RAM addresses/sizes (e.g.,
FLASH : ORIGIN = 0x08000000, LENGTH = 1024Kfor a typical STM32F4). - Entry point: Your script should specify the reset handler (usually
Reset_Handler) as the entry point, matching the symbol in your assembly startup file. - Section placement: Ensure
.text(code) goes to FLASH,.data(initialized variables) is loaded to FLASH and copied to RAM at startup, and.bss(uninitialized variables) is placed in RAM and zeroed out.
Add Critical Startup Files & Ada Runtime Libraries
Ada programs need more than just your application code to run on bare metal:
- Assembly startup file: You need a small assembly file to initialize the stack pointer, set up the interrupt vector table, and jump to your Ada
mainprocedure. This file is MCU-specific (e.g.,startup_stm32f4xx.sfor STM32F4 chips). - Bare-metal Ada runtime: The standard Ada runtime is too heavy for microcontrollers—you need a minimal runtime tailored for Cortex-M4. When using
arm-none-eabi-gcc, make sure you link against the bare-metal versions of the Ada runtime, along with GCC support libraries.
Example Correct Workflow
Instead of manually calling gcc and ld, use GNAT's build tools to handle runtime linking automatically. Here's a step-by-step:
# 1. Compile your Ada program with the bare-metal toolchain arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb program.adb # 2. Compile your MCU-specific startup file arm-none-eabi-as -mcpu=cortex-m4 -mthumb startup.s -o startup.o # 3. Link everything together with the correct linker and script arm-none-eabi-ld -T flash.ld startup.o program.o -o program.elf -lc -lgcc -lgnat
The -lc, -lgcc, and -lgnat flags pull in the bare-metal C library, GCC support routines, and minimal Ada runtime—all required for your program to run.
Verify Your Toolchain Has Ada Support
Note: Some pre-built gcc-arm-none-eabi packages don't include Ada support by default. If you get errors about missing libgnat.a, you'll need to either:
- Install a GNAT-specific bare-metal toolchain (like GNAT Community for ARM Bareboard)
- Compile the Ada cross-toolchain from source for
arm-none-eabiin WSL
Final Check
After linking, use arm-none-eabi-objdump to inspect the ELF file and confirm the entry point and memory sections are correctly set:
arm-none-eabi-objdump -h program.elf arm-none-eabi-objdump -d program.elf | grep Reset_Handler
内容的提问来源于stack exchange,提问作者micmys

