As a spin-off of a previous question (sbrk function not found when placed in a static library):
I'm creating a bare-metal application for the stm32f407 microcontroller, which has an ARM Cortex M4 core. I'm trying to create a static library containing basic stuff like cmsis, the HAL functions, and the interrupt vector table, and link that static library to my main application. However, when the interrupt vector table (which is a variable named __isr_vectors
) is placed inside the static library, the variable does not survive linking.
I can piece together what happens: The linker has an object main.o, and a few libraries. It takes all symbols found in main.o
, and it takes all symbols out of the libraries that are used by main.o
or that are used by other symbols that are already deemed necessary. The __isr_vectors
variable is not yet needed at this stage, since no one references this variable explicitly, so it is thrown out by the linker. Then, the linker searches for all input sections named .isr_vector
in order to create the output section .isr_vector
. However, no symbols survived up to this moment, so that output section is left empty, and my binary is left without an interrupt table.
I know of one trick to get the __isr_vector
to survive: reference it in a dummy function in main.c
. However, this is kinda ugly and error-prone (you can forget to do that when building a new application against the same static library). So here's my question: Is there another way to make the __isr_vector
survive, so that I can just link in my static library and be done with it?
These are the files and steps to reproduce the problem:
vectors.c:
__ attribute__ ((section(".isr_vector"), used)) void* __isr_vectors = 0;
main.c:
void _start(void) {}
link.ld:
MEMORY { FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K }
ENTRY(_start)
SECTIONS { .isr_vector : ALIGN(4) { KEEP(*(.isr_vector)) } >FLASH
.text : ALIGN(4) { *(.text .text.*) } >FLASH
}
Commands:
"C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2014q4\bin\arm-none-eabi-gcc.exe" -g -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb -ffunction-sections -std=gnu11 -o main.c.obj -c main.c "C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2014q4\bin\arm-none-eabi-gcc.exe" -g -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb -ffunction-sections -std=gnu11 -o vectors.c.obj -c vectors.c
"C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2014q4\bin\arm-none-eabi-gcc.exe" -g -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb -ffunction-sections -std=gnu11 -g -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb -Wl,--gc-sections -nostartfiles -Wl,-Tlink.ld -Wl,-Map=test_linker.without_library.map main.c.obj vectors.c.obj -o test_linker.without_library.elf
"C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2014q4\bin\arm-none-eabi-ar.exe cq libtest_linker.lib.a vectors.c.obj
"C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2014q4\bin\arm-none-eabi-ranlib.exe" libtest_linker.lib.a
"C:\Program Files (x86)\GNU Tools ARM Embedded\4.9 2014q4\bin\arm-none-eabi-gcc.exe" -g -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb -ffunction-sections -std=gnu11 -g -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -mthumb -Wl,--gc-sections -nostartfiles -Wl,-Tlink.ld -Wl,-Map=test_linker.with_library.map main.c.obj -o test_linker.with_library.elf libtest_linker.lib.a