Page 1 of 1

Help regarding CPP kernel

Posted: Sat Feb 26, 2011 6:31 am
by pranjas
Hi,

I have some startup C code that is able to do booting and initialization of pages. Now i tried to add some CPP support to it so that from now on i'll be able to code with CPP and link my existing thing to the newer things in CPP i'm developing.

Ok so here's the problem, i get this weird error

Code: Select all

cppinit.o: In function `__static_initialization_and_destruction_0(int, int)':
cppinit.cpp:(.text+0x120): undefined reference to `__dso_handle'
cppinit.cpp:(.text+0x130): undefined reference to `__cxa_atexit'
Ok now i've this as my common Makefile.

Code: Select all

CC = gcc
CPP= g++
NASMFLAGS = -f elf
NASM = nasm
INCLUDE_DIR = ../include
SRC_DIR := ../src ../src/asm ../src/x86 ../src/kmm ../src/cpp
LIBDIR = ../lib
BINARY_DIR = ../binary
CFLAGS = -c -nostartfiles -nostdlib -nodefaultlibs -fno-builtin -Wall -ffreestanding
CPPFLAGS= -c -nostartfiles -nostdlib -nodefaultlibs -fno-builtin -Wall -ffreestanding -fno-rtti -fno-exceptions -nostdinc++
PROGNAME := kernel.bin
DISK_IMAGE=../test_disks

.SUFFIXES: .s .c .o .cpp

.c.o:
        $(CC) $(CFLAGS) -I $(INCLUDE_DIR) $< -o $@;
        @echo "Copied  $@ to $(LIBDIR)/$@";
        cp $@ $(LIBDIR)/$@;
.s.o:
        $(NASM) $(NASMFLAGS) $< -o $@;
        @echo "Copying $@ to $(LIBDIR)/$@";
        cp $@ $(LIBDIR)/$@;
.cpp.o:
        $(CPP) $(CPPFLAGS) -I $(INCLUDE_DIR) $< -o $@;
        @echo "Copying $@ to $(LIBDIR)/$@";
        cp $@ $(LIBDIR)/$@;

And I've this as as linker script

Code: Select all

OUTPUT_FORMAT("binary");                              
ENTRY(_boot_entry);                                   
codestart = 0x100000;                                 
/*codestart = 0xC0000000;*/                           
virtual = 0xC0000000;                                 
. = codestart;    /* you need to set this up to the kernel text. */
SECTIONS                                                           
{
.text : {
code = . ;
*(.text);
/*I took this section straight from Red Hat documentation for the ld. Added SORT as mentioned on url*/

__CTOR_LIST__ = .;
LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
*(SORT(.ctors));
LONG(0)
__CTOR_END__ = .;
__DTOR_LIST__ = .;
LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
*(SORT(.dtors));
LONG(0)
__DTOR_END__ = .;

/**(.rodata);*/

. = ALIGN(4096);
 codeend = ABSOLUTE(.); /* make sure you use absolute otherwise it'll be relative to current section which is text section */
}

/*. = codeend ; you need to set the current location couter to the absolute value of location counter where the code ended */

.data :{  /* ADDR returns the absolute Virtual Memory Address (VMA) of a named section*/
data = ABSOLUTE(.);
*(.data);
. = ALIGN(4096);
dataend = ABSOLUTE(.);
_dataend = dataend;
}



/*. = dataend ;  same way setup location counter for the bss segment */
.bss :
{
*(.COMMON*);
bss = ABSOLUTE(.);
*(.bss);
. = ALIGN(4096);
bssend = ABSOLUTE(.);
}
end = ABSOLUTE(.);
}
Any help on this? I tried to google couldn't find anything that would suit my needs. Here is the link to the section for ctors and dtors, http://docs.redhat.com/docs/en-US/Red_H ... tions.html.

Also see below issue please,although not such a big issue but i would be glad to have it resolved :D
Another thing is, if i add rodata section separately that is not along with .text section then due to the size, it exceeds into more than 16KB. The problem being my data section goes back beyond 8kb and thus it no longer is bootable by grub. So I was wondering any solution to that before i start with disk driver?

Re: Help regarding CPP kernel

Posted: Sat Feb 26, 2011 10:26 am
by pranjas
Got it working now,

Defined

Code: Select all

 void *__dso_handle=(void*)0;
And another one as

Code: Select all

extern "C" int __cxa_atexit(void (*func) (void *), void * arg, void * dso_handle);
I've currently an empty implementation of the above function as

Code: Select all

int __cxa_atexit(void (*func) (void *), void * arg, void * dso_handle)
{
//currently do nothing.
return 0;
}
As and when i add more code that requires proper shutdown, i'll code this thing up. I hope it helps to anyone else facing same problem. I've not made these functions in a header file straight in CPP code so that it can be found easily by linker only.

The problem i faced however was that when i created global object (Just a single!) the final output came out to be more than 1.1 MB without the -ffreestanding option and just over 12 Kb using -ffreestanding option. I read on this page http://wiki.osdev.org/C_PlusPlus in the new and delete sections that i can't use this option when using g++ however it's supposedly working?

I guess i'll have to write up that disk driver soon cuz i'm not able to boot a kernel of size more than 8Kb.

Any other ideas on how to work with this 8Kb issue?

Re: Help regarding CPP kernel

Posted: Sun Feb 27, 2011 7:14 am
by Combuster
Use a GCC Cross-Compiler instead of dragging in (linux) code into your OS