0

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!

pls_help
  • 71
  • 1
  • 2
  • 9
  • 1
    Isn't this sort of thing usually done in crt0.s? But since it's bare metal you probably need your own startup code. – user253751 Dec 06 '19 at 11:55
  • I’ve tripped over the same problem. There is [link](https://stackoverflow.com/questions/50214840/risc-v-startup-code-for-stack-pointer-initalization#answer-50421199) a **partial** answer to this question. – hermannk May 27 '20 at 09:49

0 Answers0