Baremetal Cortex-M7 gcc and unwind tables without libunwind

101 views Asked by At

I'm working on a project using an STM32H743 MCU which has a Cortex-M7. I'm building outside of ST's toolchain and IDE with gcc-arm-none-eabi (13.2 Rel 1). My project is written entirely in C and assembly without external libraries (other than libgcc) or exceptions. I had the idea of writing a basic call stack unwinder as part of a debug and assertions framework, using .ARM.extab and .ARM.exidx so I added -funwind-tables to my Makefile.

After this my project wouldn't build, with errors like:

../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-abort.o): in function `abort':
abort.c:(.text.abort+0xa): undefined reference to `_exit'
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0x12): undefined reference to `_kill'
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'

So I added --specs=nosys.specs, after this it builds but with a load of warnings:

../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-closer.o): in function `_close_r':
closer.c:(.text._close_r+0xc): warning: _close is not implemented and will always fail
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): warning: _getpid is not implemented and will always fail
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0x12): warning: _kill is not implemented and will always fail
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-lseekr.o): in function `_lseek_r':
lseekr.c:(.text._lseek_r+0x14): warning: _lseek is not implemented and will always fail
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-readr.o): in function `_read_r':
readr.c:(.text._read_r+0x14): warning: _read is not implemented and will always fail
../arm-none-eabi/lib/thumb/v7e-m+dp/hard/libc.a(libc_a-writer.o): in function `_write_r':
writer.c:(.text._write_r+0x14): warning: _write is not implemented and will always fail

This is still not what I wanted, I understand the cause for the warnings is nosys.specs and the stubs it generates, but in fact I don't want any extra code linked at all. Looking at the symbol table of the elf I see lots of symbols that aren't in my project:

...
00000000 l    df *ABS*  00000000 unwind-arm.c
08003ecc l     F .text  00000012 selfrel_offset31
08003ee0 l     F .text  00000068 search_EIT_table
08003f48 l     F .text  00000028 __gnu_unwind_get_pr_addr
08003f70 l     F .text  00000098 get_eit_entry
08004008 l     F .text  00000058 restore_non_core_regs
08004060 l     F .text  0000000a _Unwind_decode_typeinfo_ptr.constprop.0
0800406c l     F .text  00000002 _Unwind_DebugHook
08004070 l     F .text  0000003a unwind_phase2
080040ac l     F .text  000000d8 unwind_phase2_forced
08004288 l     F .text  0000001a _Unwind_GetGR
080042d8 l     F .text  00000018 _Unwind_SetGR
08004374 l     F .text  000002a4 __gnu_unwind_pr_common
00000000 l    df *ABS*  00000000 pr-support.c
08004920 l     F .text  00000038 next_unwind_byte
...

And so on.

Is there a way to get gcc just to generate the tables and nothing more? I'm happy to deal with everything else myself if I can just get the tables in the elf. My desired output is just .ARM.exidx and .ARM.extab without any supporting code added to my elf. Or maybe I'm misunderstanding something?

My gcc compile command is something like:

arm-none-eabi-gcc -Wall -g3 -Wextra -Wpedantic -O0 -MMD
-ffunction-sections -fdata-sections -Isrc/include -mcpu=cortex-m7
-mfloat-abi=hard -mfpu=fpv5-d16 -mthumb -fno-exceptions
-ffreestanding -nodefaultlibs -nostartfiles -fno-builtin
-funwind-tables -fno-asynchronous-unwind-tables
-fno-omit-frame-pointer --specs=nosys.specs -lnosys <sourcefile>

My gcc ld command looks something like:

arm-none-eabi-gcc -Os -Wl,--gc-sections,--relax -mcpu=cortex-m7
-mfloat-abi=hard -mfpu=fpv5-d16 -mthumb
-Tsrc/devices/stm32h7xx/stm32h743.ld -funwind-tables
-fno-exceptions -fno-asynchronous-unwind-tables
--specs=nosys.specs -lnosys -ffreestanding <objfiles>

I'm fairly sure some of these flags aren't required, I added several in despair. Thanks for any advice.

0

There are 0 answers