I wrote a riscv emulator that loads riscv .elf files compiled with riscv64 unknown elf toolchain compiled with newlib support and c++ support. It is able to run programs that don't use any c/c++ stl functions. However I would like to be able to use stl functions like printf, cout, vector and malloc and stuff.
I however am unsure how to properly do this. Removing -nostdlib from my g++/gcc command works and allows me to compile programs that make use of stl features, however they don't work properly. Mild usage seems to be mostly ok, but heavier usage causes it to start trashing program storage(.text at 0, usage of certain stl features causes 0 writes to address 0x00000004 and up). My guess is that's happening because I'm missing things in my init function like initializing constructors, providing things heap and such.
Now, I know I probably need those(and probably more things), but I have no idea how to add those. So I'd like some help. This is my current linker script:
ENTRY(riscvcoreinitfunction);
MEMORY
{
ram (rwx) : ORIGIN = 0x00000000, LENGTH = 0x003E8000
}
STACK_SIZE = 0xFFFF;
SECTIONS
{
.text :
{
KEEP(*(.vector*))
*(.text*)
} > ram
.bss (NOLOAD) :
{
*(.bss*)
*(COMMON)
} > ram
.data :
{
*(.data*);
} > ram
.stack (NOLOAD) :
{
. = ALIGN(8);
. = . + STACK_SIZE;
. = ALIGN(8);
} > ram
__global_pointer$ =. + 0x10000;
}
And my current init function:
extern "C"
{
void riscvcoreinitfunction()
{
asm volatile
(" .option push \t\n\
.option norelax \t\n\
1:auipc gp, %pcrel_hi(__global_pointer$) \t\n\
addi gp, gp, %pcrel_lo(1b) \t\n\
.option pop \t\n\
");
asm volatile("" ::: "memory");
main();
}
}
And this is what I use to call g++:
riscv64-unknown-elf/bin/riscv64-unknown-elf-g++ -nostartfiles -march=rv32i -mabi=ilp32 -T linkerscript.ld -o bin.elf
I found a tutorial online for implementing newlib in my platform, but it doesn't really work for me and lacks details like what variables I need in my linkerscript. Also in the tutorial things like sbrk are undefined, where in my program they already have an implementation somehow for some reason.(So how do I replace them if needed?) Could anyone assist me in adding support for all the basic c/c++ features required and stl features?
Thanks!
User contributions licensed under CC BY-SA 3.0