[solved] Problem with linker

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

[solved] Problem with linker

Post 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.
Attachments
kernel_v1.zip
bootstrap, asm header, c code, linker code, and Makefile
(9.36 KiB) Downloaded 34 times
Last edited by Coty on Wed Mar 09, 2011 3:44 pm, edited 1 time in total.
My hero, is Mel.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Problem with linker

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Coty
Member
Member
Posts: 286
Joined: Thu Feb 12, 2009 5:12 pm

Re: [solved] Problem with linker

Post 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!
My hero, is Mel.
Post Reply