I am running into a similar problem to this post: Global constructor call not in .init_array section
I will re-use his example, but I think this is a problem specific to the RISCV environment, which is why I am creating a new post.
class foobar
{
int i;
public:
foobar()
{
i = 100;
}
void inc()
{
i++;
}
};
foobar foo;
int main()
{
foo.inc();
for (;;);
}
The issue is that the constructor to foo is never called. Which I put a breakpoint on the first increment, the value of i is 0, not the expected 100.
Here is the output of: riscv64-unknown-elf-objdump.exe -j .init_array -x objs/main.o
objs/main.o: file format elf32-littleriscv objs/main.o architecture: riscv:rv32, flags 0x00000011: HAS_RELOC, HAS_SYMS start address 0x00000000
Sections: Idx Name Size VMA LMA File off Algn 8 .init_array 00000004 00000000 00000000 00000154 2**2 CONTENTS, ALLOC, LOAD, RELOC, DATA SYMBOL TABLE: 00000000 l d .init_array 00000000 .init_array
RELOCATION RECORDS FOR [.init_array]: OFFSET TYPE VALUE 00000000 R_RISCV_32 _GLOBAL__sub_I_foo
Which leads me to believe that it should be called? I have also tested standard C variables declared globally, which do get assigned and also show up on the .init_array.
My _init() looks like this:
void _init(void)
{
copy_section(&__sdata_load, &__sdata_start, &__sdata_end);
copy_section(&__data_load, &__data_start, &__data_end);
zero_section(&__sbss_start, &__sbss_end);
zero_section(&__bss_start, &__bss_end);
exit(main());
}
It looks like other RISCV based projects also do not leverage libc initialization primitives (like __libc_init_array()), as a bonus question I was wondering why that is?