Page 1 of 1

cygwin and ld linker

Posted: Sat Jul 13, 2002 3:32 am
by gedeon
Hello
I have some problem with ld
I use cygwin and when i link my program with ld it stop and say
undefined reference to _alloca
undefined reference to __main
etc ...
gcc version is 3.1.1
i made some test
on mandrake 8.2 and gcc 3.x.x it work fine
with djgpp it's ok
could someone help me ?

Re:cygwin and ld linker

Posted: Sun Jul 14, 2002 6:59 am
by Pype.Clicker
Seems that you are missing the *essential* libgcc or something alike ... You can either try to setup GCC to make it learn that it should *not* rely on some pre-existing functions (-fno-builtin should do it) or try to include libgcc statically in your kernel .

Re:cygwin and ld linker

Posted: Sun Jul 14, 2002 11:55 am
by gedeon
it doesn't work at all
i made this test

Code: Select all

void start(){
   main();
}

int main(){
   return;
}
gcc -c -fno-builtin test.c
it's fine
----------------------------
ld test.o
produce this
test.o(.text+0x23):test.c: undefined reference to `_alloca'
test.o(.text+0x28):test.c: undefined reference to `__main'
----------------------------
ld -L/lib/gcc-lib/i686-pc-cygwin/3.1.1/ libgcc.a test.o
produce the same thing

In fact there is a reference to alloca in the .o even if i use the -fno-builtin option

I'm lost , please help me !!

Re:cygwin and ld linker

Posted: Mon Jul 15, 2002 2:27 am
by Pype.Clicker
very weird this is ... could you try objdump -d test.o >test.dis and post attach the .dis file so that we can see what is the generated code and what function troubles you ?

Re:cygwin and ld linker

Posted: Mon Jul 15, 2002 4:47 am
by gedeon
[attachment deleted by admin]

Re:cygwin and ld linker

Posted: Mon Jul 15, 2002 5:54 am
by Pype.Clicker
22: e8 00 00 00 00

hmm .. well, this is typical from a not-yet-linked binary file : the target address of external functions calls is left blank (0). In order to find out where they are pointing to, you now need to run objdump -x and browse the relocations list. At some place, you should find something looking like

<symbol-name> :
<reloc-mode> : 0x000023

telling that the address of <symbol-name> is to be put into 0x0000023 (the place we're looking for) using <reloc-mode>.

__main is probably the code cygwin calls by default for setting up some part of its runtime, and _alloca is usually a function that provides allocation on the stack (a portable way to write sub esp, x ;) ) If it still bugs you, i think you can safely create a small assembly program that will have those two symbols declared as empty functions (just a "ret" opcode ;) ) and link that file with your own work... Not really clean, but at least it *will* link

Re:cygwin and ld linker

Posted: Mon Jul 15, 2002 7:52 am
by gedeon
Arrrrrrrrrrrrrghhhhhhhhhhhhhh !!!

The solution was :
int main(void) ----> int mainCRTStartup(void)

now it's work

the name of the main function is not very important
but when i want to produce some binary file

"ld --oformat binary test.o" for instance
ld answer :
ld: PE operations on non PE file.

It's very tired , isn't it.

Does ld with cygnus is able to produce binary format ?

Re:cygwin and ld linker

Posted: Mon Jul 15, 2002 7:53 am
by gedeon
Arrrrrrrrrrrrrghhhhhhhhhhhhhh !!!

The solution was :
int main(void) ----> int mainCRTStartup(void)

now it's work

the name of the main function is not very important
but when i want to produce some binary file

"ld --oformat binary test.o" for instance
ld answer :
ld: PE operations on non PE file.

It's very tiring , isn't it ?

Does ld with cygnus is able to produce binary format ?

Re:cygwin and ld linker

Posted: Tue Jul 16, 2002 7:36 am
by gedeon
Ok i've got my answer
Tim Robinson wrote this in the tutorial "Writing a Kernel in C" :

Note that, at the time of writing, Cygwin?s flat-binary output is broken.

So the best to use gcc is under linux

exit windowsXP(cygwin, gcc, VC++)
call Mandrake8.2(gcc, kdevelop, make)

Another way to dev with windows and GCC
WindowsXP(VMWare(Mandrake8.2(gcc, kdevelop, make)))) ;D ;D

Thanks for your help