Page 1 of 1

[solved] Problem with linker

Posted: Wed Mar 09, 2011 1:44 pm
by Coty
Hello everyone.

I am fairly new to C, but I know enough to make a sample C kernel. However I am having trouble with linking my kernel.

1) I am sure my ASM code works
2) My C code is simple so I don't think there is a problem there...

My kernel compiles, assemblers, and links without error, but my code never makes it to the C code, infact, it never launches the kernel corectly. It works like this:
  • Boot loader launches, and loads kernel.
  • Boot loader sets 32bit paging (located at 0x00001000), Puts IDT at 0x00000000, Sets PIT, maps PIC, lastly, sets P-Mode and jumps to kernel.
On QEmu I see:

Code: Select all

Loading sectors...               OK!
Installing paging...             OK!
Entering P-mode...
Now this is when we set P-mode and jump to the kernel. The kernel should automatically clear the screen (No "OK!" message should be displayed for PM)

I know my bootstrap is not the one at fault because I wrote it for my ASM kernel and have used it a while. Since it loads raw binary to 0x7E00 I have tried to configure my linker to handle this based from the one in "Bare_Bones". Here is the linker script:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY (start)

SECTIONS{
    . = 0x00007e00;    /* This should tell it were the kernel is loaded.. no?  */

    .text :{
        *(.text)
    }

    .data : {
        *(.data)
    }

    .bss : {
        sbss = .;
        *(COMMON)
        *(.bss)
        ebss = .;
    }
}
	INPUT(head.o kernel.o)
	OUTPUT(kernel.bin)
As far as I can understand it should work, but obviously my understanding is wrong...

All code attached below... ( /!\ Code is built with Linux Makefile! [ bootstrap uses FASM, header uses NASM and kernel uses GCC, havn't taken time to convert bootstrap to NASM yet, sorry...] just run the makefile and it should make a bootible floppy.)

Any guide lines, tips, pointes, help, will be appreciated.

Re: Problem with linker

Posted: Wed Mar 09, 2011 2:22 pm
by Combuster
Your setup is broken by design. The linker is free to reorder whatever code it is fed within the extent of the linker script. It may just skip the header and instead jump straight into main. In fact it probably does just that, because the code in the header assembly is not part of any named section, so the linker will just throw it away.

And then there's the HLT, which will break QEmu if combined with an absence of interrupts - you're not loading IDTR either.

Re: [solved] Problem with linker

Posted: Wed Mar 09, 2011 3:40 pm
by Coty
Awesome! I got it to work! :!:

I did everything you pointed out (ashamed at my self about the LIDT part though!) I didn't have any luck, but then I thought about what you said about jumping to main, so I changed the "main" to "kmain", GCC gave me an error that I fixed by changing:

Code: Select all

gcc kernel.c -o kernel.o
to

Code: Select all

gcc -c kernel.c -o kernel.o
Thanks! I feel a little ashamed I always seem to miss the little things, but I guess its just like the real world, its easier to see my house then to see my wallet!

Thanks again!

---
Cheers!