Problems with references to __alloca and __main.

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
miha
Posts: 4
Joined: Sun Oct 12, 2008 5:25 pm

Problems with references to __alloca and __main.

Post by miha »

Hello.

First, I am new to kernel development. I must say I find it very interesting and fun. But at the same time so frustrating because of these problems.

I know, there were a lot of people with this problem. I know I should build a cross-compiler. I want to avoid that for now.

First, I thought it was my error. But its not. I followed Brans Kernel tutorial step by step exactly. And I get the following problem:

Code: Select all

main.o:main.c:(.text+0x29): undefined reference to `__alloca` 
main.o:main.c:(.text+0x2e): undefined reference to `___main`
I passed -nostdinc -fno-builtin but still GCC thinks its cool that it puts __main and __alloca in my object files. What can I do? I have GCC 3.4.5 (MinGW special). Why does it put these symbols in there if I explicitly say that it should not?

Thank you.
posman
Posts: 19
Joined: Fri Sep 05, 2008 12:55 pm

Re: Problems with references to __alloca and __main.

Post by posman »

I've had the same problem. It gave me a few headaches. I don't remember the exact solution (it was a few months ago). But what I remember is that it was something about the name of the main function. I guess you have source file with "int main(some parameters)" declaration. Could you please try changing that to this:

int _main(some parameters)

Maybe is not the most elegant solution, but I worked for me :D
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: Problems with references to __alloca and __main.

Post by Combuster »

GCC Cross-Compiler and search first next time
"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
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Problems with references to __alloca and __main.

Post by AJ »

IIRC, __alloca is used to allocate a certain amount of space on the stack. It takes a size_t parameter and returns a void*, although this is not guaranteed and this could even vary between GCC versions.

As you suspect and as you will get fed up of hearing - build a cross compiler rather than trying to implement this yourself :)

Cheers,
Adam
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Problems with references to __alloca and __main.

Post by Solar »

__alloca() is a stack-handling function from the Windows runtime. The fact that your compiler is trying to reference that function shows that you are trying to generate a Windows executable. The solution is to either find the correct command line options to turn this off, or (better) to use a compiler that doesn't assume Windows as default target - hence the "use a cross-compiler" sentiment.
Every good solution is obvious once you've found it.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: Problems with references to __alloca and __main.

Post by CodeCat »

It might seem like rubbish to use a cross-compiler, at first I didn't see the sense in it either. But then I tried it and... well it just works.
miha
Posts: 4
Joined: Sun Oct 12, 2008 5:25 pm

Re: Problems with references to __alloca and __main.

Post by miha »

I searched and I found. But not a solution. Build a cross-compiler? Sure, I would be happy to. I also tried it. Its not as easy as it looks in the Wiki. I guess I'll try again today.

But I want to be clear. What kind of exes will the cross-compiler build? I guess if I build GCC on Windows, I will get ELF executables, right?

Ah, guess its back to the cross-compiler. :P
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Problems with references to __alloca and __main.

Post by AJ »

Hi,
miha wrote:But I want to be clear. What kind of exes will the cross-compiler build?
If you follow the tutorial exactly, your target will be i586-pc-elf (ELF32 format). Once you are happy with doing this, it is perfectly possible to adjust the target to, for example, x86_64-pc-elf.

Cheers,
Adam
miha
Posts: 4
Joined: Sun Oct 12, 2008 5:25 pm

Re: Problems with references to __alloca and __main.

Post by miha »

Sorry for not opening a new thread for this, but it just seems that its not worth it. I compiled binutils successfully. But now, when I try to compile GCC 4.0.0 (I tried already with GCC 3.4.5) with the configure line:

Code: Select all

miha@debbie usr/src/build-gcc $ ../gcc-4.0.0/configure --target=$TARGET --prefix=$PREFIX --disable-nls --enable-languages=c,c++ --without-headers 
Everything goes fine except that it doesn't find c++. So only C is supported. Then I try to make gcc-all everything:

Code: Select all

$ make all-gcc
make: *** No rule to make target "/usr/src/gcc/gcc/version.c" needed by 'config.status'. Stop.
What could be causing this?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Problems with references to __alloca and __main.

Post by JamesM »

Firstly, don't use a crusty old version. Use GCC 4.2 at the latest.

Secondly, you need to download the 'C++' tarball as well as the core package in order to enable C++.
miha
Posts: 4
Joined: Sun Oct 12, 2008 5:25 pm

Re: Problems with references to __alloca and __main.

Post by miha »

OK, I got pretty far this time. But somewhere in the late stages of the compilation, this happened:

Image

Hmm.. The paths don't seem right. And I don't know what the -isystem parameter does. :/
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Re: Problems with references to __alloca and __main.

Post by Craze Frog »

mingw is very flaky, you should install gcc through cygwin and build a cross compiler (from normal gcc sources) with the gcc from cygwin. I promise you that will work if you follow the instructions.
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: Problems with references to __alloca and __main.

Post by Combuster »

Also, make sure you remove anything unneeded from the path variable:

Code: Select all

echo $PATH
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/WINDOWS/system32:
      /cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem
(keep only the cygwin directories and windows directories listed by the first command)
"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 ]
Post Reply