Going from C Bare Bones to C++ Bare Bones
Posted: Tue Oct 26, 2010 3:27 pm
I have some questions about the process.
When you finish the C Bare Bones kernel (http://wiki.osdev.org/Bare_bones) you have 3 files:
linker.ld -> which is the script that tells linker how to link different object code sections
loader.s -> written in gas (in my case) which passes the control from bootloader to kernel main function initializin kernel stack and other things.
kernel.c -> kernel main function
Then I'm following the C++ Bare Bones Kernel Tutorial (http://wiki.osdev.org/C%2B%2B_Bare_Bones)
I need to override my linker.ld from de C with the C++ (without forgeting the ENRTY (loader) ) one that defines extra rules to link constructor and destructor sections of the ELF and GNU soft linking. Untill here everything is fine.
Then the wiki talks about constructors and destructors and that in C++ you must call a constructor before you use an object and the destructor when you don't need the object any more, and after that you give some sample code.
I have some questions about de C/C++ code, i write it inline:
extern start_ctors, end_ctors, start_dtors, end_dtors;
//my compiler says me that all variables declared in extern have no type
void loader(void)
{
//- call all the static constructors in the list.
//I do not understand where and why this list exists if we need an object I will construt it when we need it
for(unsigned long *constructor(&start_ctors); constructor < &end_ctors; ++constructor)
((void (*) (void)) (*constructor)) ();
//- call kernel proper
main();
//I supose here we have to pass the struct mb_header *header, unsigned magic parameters, but I need the *header //and magic but I don't no from where I get them, I need to use a multiboot.h file or something?
//- call all the static destructors in the list.
//I do not understand where and why this list exists if I don't need an object I will destroy it.
for(unsigned long *destructor(&start_dtors); destructor < &end_dtors; ++destructor)
((void (*) (void)) (*destructor)) ();
}
Thank you for solving my n00b doubts.
When you finish the C Bare Bones kernel (http://wiki.osdev.org/Bare_bones) you have 3 files:
linker.ld -> which is the script that tells linker how to link different object code sections
loader.s -> written in gas (in my case) which passes the control from bootloader to kernel main function initializin kernel stack and other things.
kernel.c -> kernel main function
Then I'm following the C++ Bare Bones Kernel Tutorial (http://wiki.osdev.org/C%2B%2B_Bare_Bones)
I need to override my linker.ld from de C with the C++ (without forgeting the ENRTY (loader) ) one that defines extra rules to link constructor and destructor sections of the ELF and GNU soft linking. Untill here everything is fine.
Then the wiki talks about constructors and destructors and that in C++ you must call a constructor before you use an object and the destructor when you don't need the object any more, and after that you give some sample code.
I have some questions about de C/C++ code, i write it inline:
extern start_ctors, end_ctors, start_dtors, end_dtors;
//my compiler says me that all variables declared in extern have no type
void loader(void)
{
//- call all the static constructors in the list.
//I do not understand where and why this list exists if we need an object I will construt it when we need it
for(unsigned long *constructor(&start_ctors); constructor < &end_ctors; ++constructor)
((void (*) (void)) (*constructor)) ();
//- call kernel proper
main();
//I supose here we have to pass the struct mb_header *header, unsigned magic parameters, but I need the *header //and magic but I don't no from where I get them, I need to use a multiboot.h file or something?
//- call all the static destructors in the list.
//I do not understand where and why this list exists if I don't need an object I will destroy it.
for(unsigned long *destructor(&start_dtors); destructor < &end_dtors; ++destructor)
((void (*) (void)) (*destructor)) ();
}
Thank you for solving my n00b doubts.