Calling _main in Bran's Kernel Tutorial

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
Tempestum
Posts: 3
Joined: Sat Dec 19, 2009 3:30 am
Location: Drachten, the Netherlands

Calling _main in Bran's Kernel Tutorial

Post by Tempestum »

Hi everyone,

Normally I don't post much on the forums, but I couldn't find a solution for this problem (or, actually, I am not sure if I am doing this right), so I decided to ask for help here.

I am following Bran's Kernel Tutorial, and when it comes to the section where I have to start using C source files, and calling the main function in the assembly code, I have a problem.

As Brandon suggested in his tutorial, calling the main function from the C source file (main.c) should be done by adding the following lines immediately after 'stublet:'

Code: Select all

extern _main
call _main
The problem is, when I start compiling and linking the whole thing. My makefile returns an error saying:
kernel.o:kernel.o(.text.0x2d) undefined reference to '_main'
I changed '_main' to 'main' in my code, and then it didn't came up with errors anymore.

However, I don't know if I have really 'solved' the problem. Though it does not come up with errors anymore, is using 'main' in assembly a good thing to do? Brandon said that GCC (I use version 4.3.3 by the way) automatically added an underscore before every function in C, so I guess my GCC compiler does not do that? Or is there something else I need to fix?

Thanks in advance,
~Temp.
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: Calling _main in Bran's Kernel Tutorial

Post by zity »

GCC will use underscores depending on the configuration, if you're using the flag -fno-leading-underscore gcc won't add underscores to the function name.

But your solution is pefectly fine :)
Grunt
Member
Member
Posts: 37
Joined: Fri Nov 06, 2009 1:05 am

Re: Calling _main in Bran's Kernel Tutorial

Post by Grunt »

Tempestum wrote:Brandon said that GCC (I use version 4.3.3 by the way) automatically added an underscore before every function in C, so I guess my GCC compiler does not do that?
You should have tried that first.

If that doesn't fix it, then I guess it's the linker script.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Calling _main in Bran's Kernel Tutorial

Post by pcmattman »

To build upon zity's answer...

Some (mainly older) versions of GCC (and some specific compile targets as well) have different underscore rules built into the compiler. Your best bet for the most portable (at least, across GCC versions and targets ;) ) solution is to use -fno-leading-underscore and ensure no underscores are used in your assembly code.
Tempestum
Posts: 3
Joined: Sat Dec 19, 2009 3:30 am
Location: Drachten, the Netherlands

Re: Calling _main in Bran's Kernel Tutorial

Post by Tempestum »

Grunt wrote:
Tempestum wrote:Brandon said that GCC (I use version 4.3.3 by the way) automatically added an underscore before every function in C, so I guess my GCC compiler does not do that?
You should have tried that first.
I actually did try that first:
I changed '_main' to 'main' in my code, and then it didn't came up with errors anymore.
I was asking whether this was actually 'normal', and if it wouldn't get me into trouble later. :wink:

Anyway, thanks everyone! I've just got GRUB working on my floppy and it seems my kernel is working fine.
Post Reply