Page 1 of 1

New to OSDev - already have an error

Posted: Thu Oct 08, 2009 10:12 pm
by bgraybr
----
I've been programming for about two years, mostly higher level stuff (like Python, PHP, web design). I know a little bit of Java and I'm starting to learn C++. I use Linux. I decided to learn how to program operating systems because I want to learn lower level programming, how computers work, etc. I've read all of the beginner articles in the OSDev Wiki and done some (very basic) programs in assembly.
----

Anyways, I'm at this tutorial.

- loader.s assembles without any errors

- in kernel.c I'm unsure about this line:

Code: Select all

/* You could either use multiboot.h */
/* (http://www.gnu.org/software/grub/manual/multiboot/multiboot.html#multiboot_002eh) */
/* or do your offsets yourself. The following is merely an example. */ 
char * boot_loader_name = (char *)mbd + 64;
So I left it as is. This is probably my problem (I think), but then again I googled it and found some example codes that did the same thing so I don't know.

- when I link the files using linker.ld it gives me an error: "Segmentation fault", but it still worked and I now have the file kernel.bin so I decided to try it out.

Grub works fine, but I get "Error 13: Invalid or unsupported executable format...".

I know this is a simple tutorial but I honestly can't figure it out

Re: New to OSDev - already have an error

Posted: Thu Oct 08, 2009 11:00 pm
by Hangin10
1) Did you build a cross compiler? If so, are you sure it is being used rather than your distribution's compiler?
EDIT: Could you post the command line you're using to invoke the linker?

2) That line is actually wrong. The location (char*)mbd + 64 contains a 32bit pointer to the string. I'd do something like (char*) *(long*)( ((char*) mbd) + 64).

Re: New to OSDev - already have an error

Posted: Fri Oct 09, 2009 11:00 am
by Daevius
As Hangin10 said, sure you use a cross-compiler? I had the same problem 2 days ago (just started as well), here's the thread + my fix: http://forum.osdev.org/viewtopic.php?f=1&t=21033 (supposing that was the problem).
Hangin10 wrote:2) That line is actually wrong. The location (char*)mbd + 64 contains a 32bit pointer to the string. I'd do something like (char*) *(long*)( ((char*) mbd) + 64).
Hmm, are you sure that is correct? The pointer to mbd + 64 bytes points to a string of characters (mbd + 64 is a char*). A char* and long* both have the same size, namely 32bits on a 32bit system (like all pointers on a 32bit system). How else can a string of characters be pointed to if it were at the end of the memory.
Also, you cast mbd to a char*, add 64 bytes, then cast to long*. Then you take the value to where the pointer points to (the first character of the name) and cast it to a char _pointer_! Isn't it a character, not a pointer?

Re: New to OSDev - already have an error

Posted: Fri Oct 09, 2009 11:10 am
by Hangin10
Daevius wrote:Hmm, are you sure that is correct? The pointer to mbd + 64 bytes points to a string of characters (mbd + 64 is a char*). A char* and long* both have the same size, namely 32bits on a 32bit system (like all pointers on a 32bit system). How else can a string of characters be pointed to if it were at the end of the memory.
Also, you cast mbd to a char*, add 64 bytes, then cast to long*. Then you take the value to where the pointer points to (the first character of the name) and cast it to a char _pointer_! Isn't it a character, not a pointer?
Yes, I am sure that is correct. mbd is a pointer to the multiboot struct. It starts out void*, then cast to char*. At this point it is still the same pointer, just with a different type. Then 64 is added to it. So now it points to offset 64 in the struct. At that location is a pointer to a string, not a string, so you need the 32bit data value there (ie the cast of mbd + 64 to long pointer and the dereference) as a char pointer (ie the cast of the 32bit value to char*). Reread the multiboot spec.

Re: New to OSDev - already have an error

Posted: Fri Oct 09, 2009 11:33 am
by Daevius
Oh right, mdb is a pointer to the struct. You are correct :)

Re: New to OSDev - already have an error

Posted: Fri Oct 09, 2009 5:50 pm
by bgraybr
Hangin10 wrote:1) Did you build a cross compiler? If so, are you sure it is being used rather than your distribution's compiler?
No, I didn't. I used GCC. I didn't think it was necessary?

EDIT: Could you post the command line you're using to invoke the linker?

Code: Select all

ld -T linker.ld -o kernel.bin loader.o kernel.o
2) That line is actually wrong. The location (char*)mbd + 64 contains a 32bit pointer to the string. I'd do something like (char*) *(long*)( ((char*) mbd) + 64).
[/quote]
Ah, ok. I'll change it and see what happens.

Also, do I have to have an actual floppy disk? My computer doesn't have a floppy drive so I just used the image.

Re: New to OSDev - already have an error

Posted: Fri Oct 09, 2009 10:26 pm
by Love4Boobies
Hi bgraybr and welcome to the OSDev.org community.

My guess is that you are programming under Windows - where the default executable format is PE (Portable Executable). PE is a format that cannot be handled by GRUB, unlike ELF (Executable and Linking Format) which is basically the de facto standard for hobby OSes, it seems (probably because of the toolchains available). If your compiler produces ELFs then be sure to use the -ffreestanding flag.

As for the floppy disk, no, you do not need one. There are several ways of testing your code on your development machine. For example you could use an emulator (e.g., Bochs) or a virtual machine (e.g., QEMU) with your image. If you want to make your life simpler, once you've implemented a file system you could use a tool to emulate the floppy drive on your host machine and then use Windows or Linux to access it directly (provided you don't roll your own type of file system, in which case you will also have to write a Windows or Linux driver, respectively).

Re: New to OSDev - already have an error

Posted: Sun Oct 11, 2009 7:10 pm
by bgraybr
:|

I had a capitol letter O where the number 0 should have been in my linker.ld file...

Re: New to OSDev - already have an error

Posted: Sun Oct 11, 2009 7:17 pm
by Love4Boobies
Since you didn't provide us with neither the linker script it would have been hard for us to spot that :) Just out of curiosity, where did this typo occur? It's sort of weird that the linker would spit out errors about executable formats. Did you build your cross-compiler yet?

Re: New to OSDev - already have an error

Posted: Sun Oct 11, 2009 8:01 pm
by bgraybr
Love4Boobies wrote:Since you didn't provide us with neither the linker script it would have been hard for us to spot that :) Just out of curiosity, where did this typo occur? It's sort of weird that the linker would spit out errors about executable formats. Did you build your cross-compiler yet?
The linker gave me the error "Segmentation fault" but it still created an empty file named "kernel.bin", so I didn't think that anything was wrong. So when I tried to boot it grub complained about executable formats.

I got my operating system to work using my usual compiler. I built a cross-compiler but I haven't tried using it yet.

Re: New to OSDev - already have an error

Posted: Sun Oct 11, 2009 10:26 pm
by neon
If any empty file is created, it is good chances that it will fail to boot :)

I am surprised something as simple as that though can cause a seg fault in LD...