I'm trying to get my kernel loaded. I finished my stage1 loader that loads stage2 from a FAT12 formatted floppy which now enables A20, switches to protected mode, searches for a kernel image and tries to jump to it after placing it at 0x100000.
The stage2 loader is fully working, because I can get some small assembly code up and running with it.
I'm compiling my Kernel in C++ and linking it in binary format, because I don't want to implement and ELF-Parser in my stage2 loader. Perhaps later. I now want to get started with my kernel.
The problem I have is that not the main function starts at 0x0. It's one of my functions to print strings. If I jump there, the CPU is doing crap.
How can I tell the linker to put the main function directly to 0x0? Even in ELF it's wrong. The entry point in the header stated that it is at 0x1000, which is right in this case. But there was also the print function located. I did an objdump --source to determine this.
Does somebody know how to fix this? Can I do this with binary format? And why is even wrong in the ELF file?
Here is main code that needs to be executed:
Code: Select all
/*
====================================================
entry.cpp
-This is the kernel entry point. This is called
from the boot loader
====================================================
*/
extern int kmain () __attribute__ ((cdecl));
extern void InitializeConstructors() __attribute__ ((cdecl));
extern void Exit () __attribute__ ((cdecl));
int main () {
#ifdef ARCH_X86
asm (
"cli \n\t" // clear interrupts--Do not enable them yet
"mov $0x10, %ax \n\t" // offset 0x10 in gdt for data selector, remember?
"mov %ax, %ds \n\t"
"mov %ax, %es \n\t"
"mov %ax, %fs \n\t"
"mov %ax, %gs \n\t"
"mov %ax, %ss \n\t" // Set up base stack
"mov $0x90000, %esp \n\t"
"mov %esp, %ebp \n\t" // store current stack pointer
"push %ebp "
);
#endif
//! Execute global constructors
InitializeConstructors();
//! Call kernel entry point
return kmain ();
//! Cleanup all dynamic dtors
Exit ();
#ifdef ARCH_X86
asm("cli \n\t");
#endif
for (;;) ;
}
Code: Select all
OUTPUT_FORMAT("binary")
SECTIONS{
. = 0x00100000;
.text :{
*(.text)
}
.rodata ALIGN (0x1000) : {
*(.rodata)
}
.data ALIGN (0x1000) : {
start_ctors = .;
*(.ctor*)
end_ctors = .;
start_dtors = .;
*(.dtor*)
end_dtors = .;
*(.data)
}
.bss : {
sbss = .;
*(COMMON)
*(.bss)
ebss = .;
}
}