Page 1 of 1

Basic kernel in C - howto?

Posted: Mon Oct 23, 2006 2:36 pm
by bureX
I have managed to boot my "OS" from a floppy drive and print out a basic command prompt written in asm (but everything is still in real mode). My question is:

How do I manage to get to the point where I can start writing my code in C?
I have tried calling my C kernel like so:

asm (no, it's not the complete code):

Code: Select all

[extern _kernel]
call _kernel
and my C code is:

Code: Select all

int kernel()
{
for (;;);
}
This should make the kernel get stuck in an endless loop... but I can't manage to combine the two...

I am using NASM and GCC on Windows XP. Now, I've tried some methods to link the compiled output with LD by reading some tutorials, but I always fail because of multiple reasons (mostly because LD "doesn't recognize the file input")...

I'm asking for somebody to write a quick and dirty example on how to link compiled output from NASM and GCC (and how to compile it in the first place) in Windows using LD in order to create a basic "Hello world" kernel (where "Hello World" is an endless loop ;) ).

PS: And if somebody could explain which output format should be used (aout, elf etc.), and why, that would be great :)

PS2: I'm using GCC and LD from the bin folder that I got with Bloodshed Dev-Cpp

Posted: Mon Oct 23, 2006 3:17 pm
by Brynet-Inc
It's fairly simple.. You shouldn't have to include the forward slash in MinGW's GCC. Also a friend of mine said using DJGPP's ld command is better.

I used OpenBSD.. So I use nasm's aoutb format for compiling the ASM bit.

ASM side - Command: nasm -f aout stub.asm -o stub.o

Code: Select all

[BITS 32]
[global start]
[extern kernel]  ;kernel function in C file

start:
	call kernel ; Call C function..
	;Disable interrupts using cli could be a good idea..
	jmp $ ;Crash the kernel
C Side - Command: gcc -ffreestanding -c kernel.c -o kernel.o

Code: Select all

int kernel(void)
{
	for (;;); //You'll have to write your own C functions.. (printf etc)
}
You may also want to use a linker script.. (Filename is Link.ld)
Command: ld -T Link.ld stub.o kernel.o -o kernel.bin

Code: Select all

ENTRY(start)
OUTPUT_FORMAT("binary")
SECTIONS
{
    .text 0x100000 :
    {
        code = .; _code = .; __code = .;
        *(.text)
        . = ALIGN(4096);
    }

    .data :
    {
        data = .; _data = .; __data = .;
        *(.data)
        . = ALIGN(4096);
    }


    .bss :
    {
        bss = .; _bss = .; __bss = .;
        *(.bss)
        . = ALIGN(4096);
    }

    end = .; _end = .; __end = .;
}
*.rodata may be required at some point?*

Probably should read some pages at http://www.osdever.net/tutorials.php?cat=0&sort=1 or read the Wiki's.

http://www.osdev.org/osfaq2/
http://www.osdev.org/wiki/

Now thats my quick and dirty example ;)

Posted: Mon Oct 23, 2006 3:48 pm
by bureX
Again... I get the problem I had before... I compiled my code and tried to link it with the configuration file you attached, but I failed:

Code: Select all

G:\Dev-Cpp\bin>ld -T Link.ld ks.o kernel.o -o kernel.bin
ks.o: file not recognized: File format not recognized
I compiled my ASM file like so:

Code: Select all

C:\nasm-0.98.39>nasmw -f aout kern.asm -o ks.o
......

Then I tried to use your example ASM and C code, but ended up with the same problem... So, either my GCC configuration is messed up, or LD is giving me the cold shoulder :(

Any ideas? What should I try next?

BTW: What's the "-ffreestanding" flag about?
BTW2: What does the LD configuration file represent? (sorry if I'm a bit buggy, but my curiosity is sucking me into this ;) )

Very, VERY quick responce though, thanks a million!
Oh, and I've been reading all those tuts and articles from osdever.net for weeks now, so don't worry, I'm not the lazy type ;)

Posted: Mon Oct 23, 2006 4:04 pm
by Combuster
maybe you should use "-f coff" for nasm instead, being that a.out is an *nix thing

about -ffreestanding: this prevents your code being linked to anything it normally would (the runtime, libc)

the ld config tells the linker how it should put the data into the final output. I'm not a guru in ld scripts however

Posted: Mon Oct 23, 2006 4:15 pm
by bureX
k... Will try the coff format...

Posted: Mon Oct 23, 2006 4:18 pm
by bureX
Um...

Code: Select all

G:\Dev-Cpp\bin>ld -T Link.ld ks.o kernel.o -o kernel.bin
ld: PE operations on non PE file.
Thanks for the explanation for -ffreestanding, but doesn't the "-c" command do that anyways?

If you don't have any more ideas (because I sure don't :) ), could you tell me what do you use for OS development on Widnows? Maybe I should try using tools that didn't come bundled with Dev-Cpp... What do you think?

Posted: Mon Oct 23, 2006 4:30 pm
by Combuster
Ack, that message again - There was a workaround for that but i dont know which one... It should be possible anyways.

As for the OS toolchain of my choice: nasm, gcc cross-compiler, freebasic (and you probably didnt want to hear that last one :) ). That said, a GCC Cross-Compiler seems to be pretty standard around here (at least on MT it was), and it isnt really hard work either.

Theres a walkthrough on the wiki for this.

Posted: Mon Oct 23, 2006 4:38 pm
by bureX
I didn't touch Basic for at least 2 years :)

I also didn't know that Basic was even mentioned when it comes to OS development, but then I saw it's description:
FreeBASIC - as the name suggests - is a completely free, open-source, 32-bit BASIC compiler, with the syntax the most compatible possible with MS-QuickBASIC, that adds new features such as pointers, unsigned data types, inline-assembly and many others.
I mean... Pointers, inline ASM - WOW! Sounds like hot stuff!

And off I go to read the wiki...

Thanks for the quick chat ;)

Posted: Mon Oct 23, 2006 4:45 pm
by Combuster
bureX wrote:I didn't touch Basic for at least 2 years :)

I also didn't know that Basic was even mentioned when it comes to OS development, but then I saw it's description:
AFAIK i'm the only one around here with that idea :) Not to mention being criticised about it by my friends (until somebody took a bet and lost his money)

Anyway, to answer one other question i missed the other post: -ffreestanding is indeed pointless when supplying -c, as it is used in the linking stage, which -c disables.

Good luck compiling

Posted: Mon Oct 23, 2006 5:35 pm
by Brynet-Inc
Please try the DJGPP's ld command, and use the aout nasm format.

I had my friend send this, MinGW's ld command does NOT like non-PE formats.

Here is DJGPP's ld command...
http://www.mooload.com/new/file.php?fil ... /ld.tar.gz

Simply put it in the directory with your kernel source files and make sure you run ./ld.exe to use this one instead of MinGW's.

(Or get off Windows and install a true OS like OpenBSD... 8))

Posted: Mon Oct 23, 2006 6:18 pm
by Midas
Brynet-Inc wrote:*.rodata may be required at some point?*
*.rodata is needed to store string literals, certainly in the ELF format using GCC. You start to encounter strange errors (I forget what they were exactly, but as I recall they're related to GCC's desire to store string literals before the function in which they're used - similar to why you can't just use GRUB to load a C binary without an ASM stub. So, you'd want to include it in the linker script.

Posted: Tue Oct 24, 2006 3:12 pm
by bureX
I'm now using LD from the DJGPP package, and it seems like everything is going on track! Off I go to enable pmode and to write my first printf function ;)

Dunno why LD that came with Dev-Cpp kept failing me... But nevermind...

Thanks for the help everybody!

Posted: Tue Oct 24, 2006 4:05 pm
by Brynet-Inc
bureX wrote:I'm now using LD from the DJGPP package, and it seems like everything is going on track! Off I go to enable pmode and to write my first printf function ;)

Dunno why LD that came with Dev-Cpp kept failing me... But nevermind...

Thanks for the help everybody!
Good, Apparently it's because it doesn't include support for aout.