OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 3:24 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: what are crti crtn, crt0 files?
PostPosted: Sun Aug 04, 2019 12:53 pm 
Offline

Joined: Fri Jul 26, 2019 2:34 pm
Posts: 3
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. And if you could point me to some good resources.
Moreover, I tried to cross-compile for x86_64, but could not. Apparently there is also crt0 for the x86 arch? (I want to target the x86_64 architecture instead of the i386)

This is my first topic post on here, as I'm new in this niche. Although, I study Computer Science in college, OS development is very challenging. Learning a lot though.

If you think I'm missing some key concepts that I should learn about, please point me to the right direction.


Top
 Profile  
 
 Post subject: Re: what are crti crtn, crt0 files?
PostPosted: Sun Aug 04, 2019 3:09 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
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.


Top
 Profile  
 
 Post subject: Re: what are crti crtn, crt0 files?
PostPosted: Mon Aug 05, 2019 2:28 am 
Offline

Joined: Fri Jul 26, 2019 2:34 pm
Posts: 3
I understand better now, thanks.

Quote:
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -MD -c arch/x86_64/crti.S -o arch/x86_64/crti.o -O2 -g -ffreestanding -Wall -Wextra -D__is_kernel -Iinclude
arch/x86_64/crti.S: Assembler messages:
arch/x86_64/crti.S:5: Error: operand type mismatch for `push'
arch/x86_64/crti.S:13: Error: operand type mismatch for `push'
Makefile:66: recipe for target 'arch/x86_64/crti.o' failed
make: *** [arch/x86_64/crti.o] Error 1

This is the part where it fails. Up on cross compiling.
The code of crti.S:
Quote:
.section .init
.global _init
.type _init, @function
_init:
push %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .init section here. */

.section .fini
.global _fini
.type _fini, @function
_fini:
push %ebp
movl %esp, %ebp
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */

There are other flags that I should be using for x86_64 cross compiling?


Top
 Profile  
 
 Post subject: Re: what are crti crtn, crt0 files?
PostPosted: Mon Aug 05, 2019 2:46 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
You need to push the 64-bit registers, not 32-bit. Use "push %rbp".


Top
 Profile  
 
 Post subject: Re: what are crti crtn, crt0 files?
PostPosted: Mon Aug 05, 2019 3:23 am 
Offline

Joined: Fri Jul 26, 2019 2:34 pm
Posts: 3
I did it and it worked, thanks.
Still apparently, myos.kernel failed.
Take a look:
Quote:
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -MD -c arch/x86_64/crti.S -o arch/x86_64/crti.o -O2 -g -ffreestanding -Wall -Wextra -D__is_kernel -Iinclude
OBJ=`x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -O2 -g -ffreestanding -Wall -Wextra -print-file-name=crtbegin.o` && cp "$OBJ" arch/x86_64/crtbegin.o
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -MD -c arch/x86_64/boot.S -o arch/x86_64/boot.o -O2 -g -ffreestanding -Wall -Wextra -D__is_kernel -Iinclude
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -MD -c arch/x86_64/tty.c -o arch/x86_64/tty.o -std=gnu11 -O2 -g -ffreestanding -Wall -Wextra -D__is_kernel -Iinclude
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -MD -c kernel/kernel.c -o kernel/kernel.o -std=gnu11 -O2 -g -ffreestanding -Wall -Wextra -D__is_kernel -Iinclude
OBJ=`x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -O2 -g -ffreestanding -Wall -Wextra -print-file-name=crtend.o` && cp "$OBJ" arch/x86_64/crtend.o
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -MD -c arch/x86_64/crtn.S -o arch/x86_64/crtn.o -O2 -g -ffreestanding -Wall -Wextra -D__is_kernel -Iinclude
x86_64-elf-gcc --sysroot=/home/morchid23mj/meaty-skeleton/sysroot -isystem=/usr/include -T arch/x86_64/linker.ld -o myos.kernel -O2 -g -ffreestanding -Wall -Wextra arch/x86_64/crti.o arch/x86_64/crtbegin.o arch/x86_64/boot.o arch/x86_64/tty.o kernel/kernel.o -nostdlib -lk -lgcc arch/x86_64/crtend.o arch/x86_64/crtn.o
grub-file --is-x86-multiboot myos.kernel
Makefile:56: recipe for target 'myos.kernel' failed
make: *** [myos.kernel] Error 1


Top
 Profile  
 
 Post subject: Re: what are crti crtn, crt0 files?
PostPosted: Mon Aug 05, 2019 3:53 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4591
Location: Chichester, UK
You are compiling a 64-bit kernel. So of course it fails the test:

grub-file --is-x86-multiboot

which is testing for a 32-bit kernel.

Booting a 64-bit kernel is rather more complicated than booting a 32-bit one. You can't just blindly follow instructions aimed at creating a 32-bit kernel.

You might want to look at https://wiki.osdev.org/Creating_a_64-bit_kernel and http://ringzeroandlower.com/2017/08/08/ ... -boot.html . Alternatively, at this stage you might want to work with 32-bit code and only progress to 64-bit code when you are more familiar with the whole process of creating a working kernel. Copy/pasting an existing tutorial with your own modifications is almost certain to fail unless you are quite certain what you are doing. A pre-requisite is to study the Intel Programmer's Manuals.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 167 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group