Page 1 of 1
[C++] Virtual methods
Posted: Fri Mar 27, 2009 1:01 pm
by AlfaOmega08
I have recently improved my C++ knowledge, learning how to use virtual methods.
But when calling a virtual method, my kernel does a Page Fault with CR2 = 0x0
Also calling the inherited method I have a Page Fault. Removing the virtual keyword, everything works fine, except the fact that I cannot use the overwriting of methods in inherited classes.
Re: [C++] Virtual methods
Posted: Fri Mar 27, 2009 1:05 pm
by neonek
Maybe our wiki entry will help you
click.
Re: [C++] Virtual methods
Posted: Fri Mar 27, 2009 1:21 pm
by AlfaOmega08
Ok. One step over. I read the wiki and there is written that using the flag -fno-rtti, we cannot use virtual functions. I removed the flag, and now I got those two errors from linker:
Code: Select all
src/arch/x86/console.o:(.rodata+0x1bc): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
src/arch/x86/console.o:(.rodata._ZTI10CharDevice[_ZTI10CharDevice]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
src/kernel/dmesg.o:(.rodata+0x54): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
Re: [C++] Virtual methods
Posted: Fri Mar 27, 2009 1:44 pm
by skyking
AlfaOmega08 wrote:Ok. One step over. I read the wiki and there is written that using the flag -fno-rtti, we cannot use virtual functions. I removed the flag, and now I got those two errors from linker:
Code: Select all
src/arch/x86/console.o:(.rodata+0x1bc): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
src/arch/x86/console.o:(.rodata._ZTI10CharDevice[_ZTI10CharDevice]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
src/kernel/dmesg.o:(.rodata+0x54): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
No, the wiki says virtual functions should work even with RTTI disabled. Have you run the constructor properly for the object? Or more precisely does the vtable pointer point to the vtable?
Re: [C++] Virtual methods
Posted: Fri Mar 27, 2009 3:27 pm
by JamesM
skyking wrote:AlfaOmega08 wrote:Ok. One step over. I read the wiki and there is written that using the flag -fno-rtti, we cannot use virtual functions. I removed the flag, and now I got those two errors from linker:
Code: Select all
src/arch/x86/console.o:(.rodata+0x1bc): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
src/arch/x86/console.o:(.rodata._ZTI10CharDevice[_ZTI10CharDevice]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
src/kernel/dmesg.o:(.rodata+0x54): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
IIRC the VTable is where the compiler puts the pointers to the virtual functions. So what's next?
No, the wiki says virtual functions should work even with RTTI disabled. Have you run the constructor properly for the object? Or more precisely does the vtable pointer point to the vtable?
And is the this pointer correct... post the code.
Re: [C++] Virtual methods
Posted: Sat Mar 28, 2009 3:08 am
by xenos
A CR2 value of 0x0 means you are trying to access memory at virtual address 0x0. If this happens when you call a virtual function, that probably means that the function pointer in the VTable is illegal (it points to 0). I think this happens if the constructor is not called properly.
BTW, using -fno-rtti and virtual functions works, since VTable and RTTI are two different things.
Re: [C++] Virtual methods
Posted: Sat Mar 28, 2009 4:06 am
by Creature
Note that you need a few supporting functions (like it says on the wiki), like for when a pure virtual call cannot be made (I believe you have to define __cxa_pure_virtual or something equivalent [check the wiki] and make it plainly return 0). There's also some other support code needed.
As far as I know, virtual functions can work perfectly without RTTI (what else would I've been using in my OS?), I haven't tried pure virtual functions properly yet, though.
Re: [C++] Virtual methods
Posted: Sat Mar 28, 2009 4:33 am
by skyking
[quote="Creature"]Note that you need a few supporting functions (like it says on the wiki), like for when a pure virtual call cannot be made (I believe you have to define __cxa_pure_virtual or something equivalent [check the wiki] and make it plainly return 0). There's also some other support code needed.[/qoute]
Yes check the wiki. No you don't need any supporting functions for just ordinary virtual functions, it's only pure virtual that it is needed for, and no __cxa_pure_virtual is not supposed to be called - it's an error and you'd be better of if you don't return 0 (you'd be better of if you do something completely different like generating a trap screen or something).
I guess that the original poster did not use pure virtual functions and did not force the link to succeed despite unresolved symbols.