The Problem: When I compile a C program for a bare metal RISC-V environment with GCC, the function __libc_init_array
is using the stack pointer for writes to the memory before the stack pointer is set. The stack pointer is set in an assembly file that also initializes the registers.
The __libc_init_array
function is not included in the created binary if the argument -nostartfiles
is used, but this causes some programs to stop working (I don't know what the issue is there exactly, but I would like to avoid modifying the programs).
As far as I understand the function causing the problem initializes some memory and that there is a way to execute code at that time using __attribute__
e.g. void __attribute__ ((constructor)) premain()
. This is not the exact function I tried, but I cannot find to other one now. Nevertheless, I was not able to set the stack pointer in them (using inline assembly) before it is used by __libc_init_array
.
There seems to be a way to set the init priority, but that is only working in C++ e.g. Some_Class A __attribute__ ((init_priority (2000)));
I also use the arguments -pedantic
and -ffreestanding
.
Any help is greatly appreciated!