Page 1 of 3
I need help with my c++ kernel..
Posted: Mon Apr 05, 2004 5:57 pm
by EclipseOS
Hey
I am new to OS Development and I read almost all tutorials and docs that I can find on the internet about it. I have found one tutorial called "Writing a kernel in C++". It sounded great to me but I went through it and I couldn't get the kernel to boot. I am using Mandrake Linux 10, G++ compiler, NASM compiler, and ld Linker. When I just straight copy and paste the code that I am givin, then I try to link it and it says "in loader.o - undefined refence to _main". So I edited loader.asm and changed [extern _main] and call _main to [extern main] and call main, and it gets rid of this error, but I still can't get it to boot. I've tried grub but it just says "unsupported executable format" . Please could someone figure out what is going on. Thanx
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 12:54 am
by Solar
I'd strongly advise building a dedicated cross-compiler instead of using the stock one shipped with your distro. Gets you around a shipload of similar issues.
A how-to on building a cross-compiler is
in the FAQ.
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 2:01 am
by Pype.Clicker
check you use the proper binary format (sounds like the tutorial is for ELF and you're using COFF).
I typed a nicer reply, but your **** website crashed my **** mozilla.
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 6:38 am
by ineo
Solar wrote:
I'd strongly advise building a dedicated cross-compiler instead of using the stock one shipped with your distro. Gets you around a shipload of similar issues.
No need to have a cross-compiler, the one you have is just enough if you're starting. I'm using it
I guess the tutorial you read was a bit old (I think I read the same some months ago). I suggest you look at
http://www.invalidsoftware.net, subscribe to get the last version of the code. You will find a c++ dedicated forum there.
Good luck !
I-Neo
[attachment deleted by admin]
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 7:19 am
by Pype.Clicker
but whyme's tutorial is Djgpp-specific concerning .ctor and .dtor sections ...
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 7:21 am
by Solar
@ ineo:
Smart one. ;D invalidsoftware.net was my starting point, both for the code and the development toolchain. And after I got through the initial stages of confusion, I wrote the very tutorial in the OS FAQ here, and I do recommend building a cross-compiler...
Sure, you can set options to the linker or to objcpy or whatever trickery required to get the toolchain to produce what you want instead of what it defaults to.
But the advantage of setting up a cross-compiler is that the very same procedure is the same regardless of your platform and environment, you don't drag in any files from your dev environment (like, starting files, library headers etc.), and in the end you produce the same code as everybody else using the same approach, making further helping much easier.
That being said, of course it's your decision. All I wanted to say is that I had only minimal hassles once I had my dedicated OS compiler set up.
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 8:16 am
by Pype.Clicker
a few things that may help ...
1. g++ seems to have a -fabi-version=n option which allows the user to toggle between the now-standard gcc 3.x abi and the gcc-specific 2.9x.y abi (version = 0) it doesn't appear to offer any changes on my g++ version, though ...
2. using g++/3.3, the 'main() { Video vid; }' didn't generate any __builtin_delete() in kernel.o nor in video.o ... i'm using whyme's flags (but the compiler complains that -ffreestanding is not valid for C++)
3. putting Video object outside of main() added a .ctors section and several weird symbols:
0000002c l F .text 0000003e _Z41__static_initialization_and_destruction_0ii
0000006a l F .text 00000018 __tcf_0
00000082 l F .text 0000001a _GLOBAL__I_vid
00000000 l d .ctors 00000000
0000000 *UND* 00000000 _ZN5Video5writeEPc
00000000 *UND* 00000000 _ZN5VideoC1Ev
00000000 *UND* 00000000 __dso_handle
00000000 *UND* 00000000 __cxa_atexit
00000000 *UND* 00000000 _ZN5VideoD1Ev
Code: Select all
0000002c <_Z41__static_initialization_and_destruction_0ii>:
2c: 55 push %ebp
2d: 89 e5 mov %esp,%ebp
2f: 83 ec 08 sub $0x8,%esp
32: 81 7d 0c ff ff 00 00 cmpl $0xffff,0xc(%ebp)
39: 75 2d jne 68 <_Z41__static_initialization_and_destr
uction_0ii+0x3c>
3b: 83 7d 08 01 cmpl $0x1,0x8(%ebp)
3f: 75 27 jne 68 <_Z41__static_initialization_and_destr
uction_0ii+0x3c>
41: 83 ec 0c sub $0xc,%esp
44: 68 00 00 00 00 push $0x0
45: R_386_32 vid
49: e8 fc ff ff ff call 4a <_Z41__static_initialization_and_destr
uction_0ii+0x1e>
4a: R_386_PC32 _ZN5VideoC1Ev
4e: 83 c4 10 add $0x10,%esp
51: 83 ec 04 sub $0x4,%esp
54: 68 00 00 00 00 push $0x0
55: R_386_32 __dso_handle
59: 6a 00 push $0x0
5b: 68 6a 00 00 00 push $0x6a
5c: R_386_32 .text
60: e8 fc ff ff ff call 61 <_Z41__static_initialization_and_destr
uction_0ii+0x35>
61: R_386_PC32 __cxa_atexit
65: 83 c4 10 add $0x10,%esp
68: c9 leave
69: c3 ret
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 8:27 am
by pini
one need to set the three "multiboot" fields in order to boot with grub.
I'm currently at work, so I can't give them right here, but googling about multiboot should give the answer.
When I added these fields to my kernel, I was able to boot with grub without making any other change.
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 12:51 pm
by EclipseOS
Thanx for all your help guys, I finially got it working. What I did was add a multiboot header to the loader.asm file, then change the link.ld output format to elf32-i386 and it works fine with GRUB. But there should also be a way to do it in a flat binary. (Not with GRUB). Could someone show a good bootloader that will load my flat binary? Another problem taht I have is getting things like cout, cin, endl, ect.. Any ideas?
Thanx
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 1:00 pm
by Neo
I wrote a simple bootloader in NASM for a flat binary kernel. The link to the primary is
here and the secondary stage is attached here. Hope you find them useful.
<edit>
The secondary is actually named boot2.asm which assembles to boot2.bin as this is the file the primary searches for(you could change this). I renamed the file here because i couldn't attach it with that name here</edit>
[attachment deleted by admin]
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 1:14 pm
by EclipseOS
Thanx for the bootloader, I'll test it out. Man, I need to learn assembly! Looks good.
Re:I need help with my c++ kernel..
Posted: Tue Apr 06, 2004 5:01 pm
by EclipseOS
OK.
I got it to boot as a flat binary under Neo's bootloader. But now I'm not sure how to get standard fuctions like cout, endl, cin, ect.. Could someone show me how I could get these into my kernel? Thanx for the help before.
Re:I need help with my c++ kernel..
Posted: Wed Apr 07, 2004 12:49 am
by Solar
EclipseOS wrote:
But now I'm not sure how to get standard fuctions like cout, endl, cin, ect.. Could someone show me how I could get these into my kernel?
You either have to implement your own standard libraries, or port existing ones (e.g. glibc, newlib). Existing ones depend on certain OS primitives like open(), close(), write(), sbrk() etc. for their standard functions fopen(), fclose(), fwrite(), malloc() etc. to function.
Welcome to kernel space.
Re:I need help with my c++ kernel..
Posted: Wed Apr 07, 2004 6:37 am
by ineo
@Solar
Well I agree with what you said about the cross-compiler & toolchain, however I think it is not really pedagogic to start from.
Once you have played with your kernel and you want to go to the next step, it seems obvious that a cross-compiler is needed.
I'll have a look at your tutorial as I'm interested in it too ;D
I-Neo
Re:I need help with my c++ kernel..
Posted: Wed Apr 07, 2004 6:54 am
by Solar
ineo wrote:
@Solar
Well I agree with what you said about the cross-compiler & toolchain, however I think it is not really pedagogic to start from.
I'm not sure what you mean with "pedagogic", but for me it was a necessity since I was working with Cygwin at that time (which produces PE binaries by default when I wanted ELF ones)...