Providing C++ Runtime support for Kernel

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
whyme_t

Providing C++ Runtime support for Kernel

Post by whyme_t »

Firstly, I would like to say hi to everyone here, and introduce myself.
Secondly, I would like to say that I have done a lot of research before posting here.
I've read the http://www.mega-tokyo.com/os/, searched www.google.co.uk, read most of this forum, and found many usefully links for OS dev.

I have spent the past month implementing a simple C kernel with simple video and keyboard support, using grub as boot loader.

However, I couldn't find much on how to actually implement c++ runtime support at kernel level. I don't want to debate the whole C vs C++ thing, I want to use C++ because I prefer the OO methodology, and believe an OS could be nicely encapsulated into objects.

Am I correct in assuming you can write the C++ Runtime libraries with your OS specific implementation, then link them to gcc (with c++ support), and then compile c++ source into a flat binary file which could be jumped to in PMode?
Can anyone provide a link or two to a c++ runtime library specification? or source (yeah-right)?

I understand that a lot of work needs to be done to provide the C++ support code, but I would like some ideas on how to actually go about this.
Just to clarify, I will only be using C++ for the kernel and user programs, Support code will be coded in C and ASM where applicable.

I'm not looking to write a highly efficient usable operating system, I'm just one programmer looking for a challenge ;)

thanx for your time :)
Tom

Re:Providing C++ Runtime support for Kernel

Post by Tom »

I like C++ better too, and after I get a keyboard IRQ working i'm at the same prob you have....
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Providing C++ Runtime support for Kernel

Post by Pype.Clicker »

a.f.a.i.k. the C++ runtime library is compiler-specific (even 2 different versions of GCC are not compatible about this ... so the best you can do is to link statically your kernel.o file with libg++.a included with your compiler (or some other runtime library if you're not using GCC :)
whyme_t

Re:Providing C++ Runtime support for Kernel

Post by whyme_t »

ok, I'm using DJGPP 2.03 on Windows XP. I couldn't find libg++.a but from reading the manual, I believe that is the UNIX name for libgpp.a. But I couldn't find that library either. I tried searching the web, but found very few useful hits, could anyone point me in the correct direction?

I've compiled and tested my mini c++ kernel on bochs 1.4.1, and everything works except for the calling of my global objects ctors and dtors, obviously.

I read on the web that I should look in crt0.c to see how my compiler implements this, but I couldn't find the source file for djgpp's crt0.c. Is this source available on-line? I tried the djgpp web site, but couldn't find any info about it there.

to compile my kernel I'm using

Code: Select all

gxx -c kernel.cpp -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions
and then using ld to link my asm code which calls my kernel main.

According to C++ Programming language third edition,
Except for the new, delete, typeid, dynamic_cast, and throw operators and the try-block, individual C++ expressions and statements need no run-time support
Are these the only off-limit parts of C++ for the kernel until support code is added?

thanks in advance.
Tim

Re:Providing C++ Runtime support for Kernel

Post by Tim »

Static (global) constructors and destructors are another area. Static constructors get called by the startup code in a compiler-specific way (check the libc source code), and destructors usually get called via atexit.
whyme_t

Re:Providing C++ Runtime support for Kernel

Post by whyme_t »

Would anyone be interested in some tutorials on writing a simple c++ kernel?

If there is some interest, I'll happily write some. :)
Tom

Re:Providing C++ Runtime support for Kernel

Post by Tom »

my C Kernel works with a C++ Compiler (I think)
You can test with that
PlayOS

Re:Providing C++ Runtime support for Kernel

Post by PlayOS »

I would like to see a C++ kernel tutorial. thanks.
Slasher

Re:Providing C++ Runtime support for Kernel

Post by Slasher »

Hi,
I did like to see a tutorial on writing a C++ kernel!
Tim

Re:Providing C++ Runtime support for Kernel

Post by Tim »

If you want to see a C++ tutorial, write one! :) That's why I wrote my own tutorials.

I might write a C++ one eventually, but don't hold your breath (I don't have a lot of time for OS dev at the moment).
whyme_t

Re:Providing C++ Runtime support for Kernel

Post by whyme_t »

ok, I'm going to write one.

I've got my simple c++ kernel working with a simple output stream, and just this very second got my support code to call global/static ctors and dtors... :D
Tom

Re:Providing C++ Runtime support for Kernel

Post by Tom »

FritzOS in pk0.7 (out this week, I hope ) will have some simple C++ stuff in it.
whyme_t

Re:Providing C++ Runtime support for Kernel

Post by whyme_t »

Does anyone know if the .ctors and .dtors sections are compiler specific to gcc?

I've used them to create a list of all the global / static objects in my linker script, and added my own __main and __atexit to walk the list calling the ctors / dotrs, without linking to any other library (libg++.a etc) :)

I just wanted to know how portable this solution is?

It works perfectly for me. I've also got my own versions of std::cout working, and std::cin is very close to working, just ironing out polling the keyboard and inserting chars into the stream.
Tim

Re:Providing C++ Runtime support for Kernel

Post by Tim »

whyme_t wrote:I just wanted to know how portable this solution is?
Not at all portable :|. In fact, not all versions of gcc are the same -- Cygwin gcc doesn't use .ctor or .dtor sections.
whyme_t

Re:Providing C++ Runtime support for Kernel

Post by whyme_t »

ah. ok.

How about this for an idea.

I assume that any given C++ compiler will use it's own way to mangle the names, and that it will do so uniformly.

Having looked at the disassembled object code for a compiled c++ program, it appears there is a way to identify which of the names refer to ctors, and which refer to dtors, of all global / static objects.

e.g __ZNSt7OStreamD1Ev is the mangled name of one of my global objects OStream. The constructor is the same except a C instead of a D.

Is there any reason why a C program couldn't be written to parse the object files, looking for the ctors and dtors, and either add .ctor .dtor sections, or add a ctor_list and dtor_list to the .data section? (assuming you told the program the mangled prefix to search for?)

and lastly, if this were possible, would this then be portable? :)
Post Reply