morched23mj wrote:
While on the Meaty Skeleton tutorial, I stumbled across crti and crtn files. Unfortunately I could not understand what's their purpose? Googled them, but could not get my head around them. So if you could explain to me those in a tutorial-like tone, I'd be thankful.
The crti file contains the code for the beginning of the "init" and "fini" functions. The crtn file contains the code for the end of those two functions. The rest of these two functions is entirely generated by the compiler; you just provide the bits necessary to call them.
The "init" function, once it's fully pieced together by the linker, will call your program's global constructors. Similarly, the "fini" function calls the global destructors. If you're writing code in C++ you probably will want those.
morched23mj wrote:
Moreover, I tried to cross-compile for x86_64, but could not.
We'll need more detail about that in order to help.
morched23mj wrote:
Apparently there is also crt0 for the x86 arch?
The crt0 file contains the startup code, and you need one for most (if not all) architectures. Its job is to set up the environment before the main function runs, and clean up if the main function returns. A typical userspace example might call init, call main, and then call exit. (What about fini? That's usually called from exit, in case main decides to call exit instead of returning.)
Incidentally, the Meaty Skeleton tutorial does include crt0, but calls it "boot.S" instead. Since it's a kernel, setting up the environment includes preparing the stack pointer, and it assumes it will never need to clean up since the kernel's main function should never return.