Nim's syntax looks like it would be great for writing a toy x86 kernel after the initial hurdles. I wrote a simple file, kernel.nim:
proc kmain {.exportc.} = discard
And compiled it like this (supposed to be linked later):
nim cc --cc:clang --cpu:i386 --noLinking:on --nimcache:"." --passC:"-m32 -ffreestanding -fno-stack-protector" --os:standalone --noMain:on kernel.nim
I also added a panicoverride.nim with stubs:
proc rawoutput(s: string) = discard
proc panic(s: string) = discard
And a boot assembly file boot.s, which should be irrevelant.
But then when I link it, like this:
ld -m elf_i386 -T linker.ld -o kernel boot.o @mkernel.nim.c.o
I get an error:
ld: @mkernel.nim.c.o: in function `PreMain':
@mkernel.nim.c:(.text+0xa2): undefined reference to `systemInit000'
ld: kernel: hidden symbol `systemInit000' isn't defined
Here's the reference to that in the generated source code:
void PreMainInner() {
systemInit000();
testDatInit000();
}
void PreMain() {
void (*volatile inner)();
systemDatInit000();
inner = PreMainInner;
initStackBottomWith((void *)&inner);
(*inner)();
}
I also had to install lib32-glibc on my Arch system to get it to compile (it needed the gnu/stubs-32.h header) despite not linking libc, curious why.
Any help is appreciated!
This command:
nim cc --cc:clang --cpu:i386 --noLinking:on --nimcache:"." --passC:"-m32 -ffreestanding -fno-stack-protector" --os:any -d:useMalloc -d:StandAloneHeap=1047856 --noMain:on kernel.nim
still asks me to port the memory manager. also, what's that arbitrary number? standaloneheap is an implementation of the heap for freestanding platforms?You may need to choose useMalloc or standAloneHeap. Also, I'd set --gc:arc (though orc should be fine too).
The value in -d:StandAloneHeap=1047856 is a bit over 1megabyte. It's was just a simple array based memory heap that's setup in the executable.
I encounter the same issue not using malloc but using standAloneHeap (with or without arc):
nim cc --cc:clang --cpu:i386 --noLinking:on --nimcache:"." --passC:"-m32 -ffreestanding -fno-stack-protector" --os:any -d:StandAloneHeap=1047856 --gc:arc --noMain:on kernel.nim
I see, so it's just a temporary fixed buffer. That's quite handy