cygwin and ld linker

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
gedeon

cygwin and ld linker

Post 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 ?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:cygwin and ld linker

Post 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 .
gedeon

Re:cygwin and ld linker

Post 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 !!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:cygwin and ld linker

Post 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 ?
gedeon

Re:cygwin and ld linker

Post by gedeon »

[attachment deleted by admin]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:cygwin and ld linker

Post 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
gedeon

Re:cygwin and ld linker

Post 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 ?
gedeon

Re:cygwin and ld linker

Post 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 ?
gedeon

Re:cygwin and ld linker

Post 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
Post Reply