Problem linking kernel.o and loader.o

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
User avatar
abachler
Member
Member
Posts: 33
Joined: Thu Jan 15, 2009 2:21 pm

Problem linking kernel.o and loader.o

Post by abachler »

Im using GCC and getting an error linking, I am using the barebones code verbatum, I searched and every post says its because I need to set up a cross compiler, but I already have a cross compiler set up...

loader.o: In function 'loader':
loader.s: (.text+0x14): undefined reference to 'kmain'

Code: Select all

nasm -f elf -o loader.o loader.s  
gcc -o kernel.o -c kernel.cpp -Wall -Wextra -Werror -nostdlib -nostartfiles -nodefaultlibs  
ld -T linker.ld -o kernel.bin kernel.o loader.o
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Problem linking kernel.o and loader.o

Post by Hangin10 »

Either add an underscore before kmain in the assembly file, or add -fno-leading-underscore to the flags to GCC.
User avatar
abachler
Member
Member
Posts: 33
Joined: Thu Jan 15, 2009 2:21 pm

Re: Problem linking kernel.o and loader.o

Post by abachler »

I tried -

Code: Select all

extern _kmain
call _kmain
same result

Code: Select all

extern __kmain
call __kmain
same result

Code: Select all

gcc -fno-leading-underscore -o kernel.o -c kernel.c -Wall -Wextra -Werror -nostdlib -nostartfiles -nodefaultlibs
with both of the above and plain kmain, same result
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Problem linking kernel.o and loader.o

Post by Creature »

Can you show us your code? Try putting loader.o before kernel.o by the way (if your OS uses GRUB), because the file with the multi-boot header should be one of the first files to be linked.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: Problem linking kernel.o and loader.o

Post by piranha »

If you tell it (gcc) to use no leading underscores, then maybe not using a leading underscore in the call would work, hmm?

Edit: Nvmd, didnt read the footnote at the bottom of your post...

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
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: Problem linking kernel.o and loader.o

Post by Combuster »

Have you checked that you actually use the crosscompiler and not your system's stock gcc?
(try calling i586-elf-gcc and i586-elf-ld)
"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
abachler
Member
Member
Posts: 33
Joined: Thu Jan 15, 2009 2:21 pm

Re: Problem linking kernel.o and loader.o

Post by abachler »

Creature wrote:Can you show us your code? Try putting loader.o before kernel.o by the way (if your OS uses GRUB), because the file with the multi-boot header should be one of the first files to be linked.
I copied and pasted the code in http://wiki.osdev.org/Bare_bones

updating cygwin now, as soon as it finishes Ill double check that its using the right gcc, I suspect that may be the problem as i586-elf-gcc doesnt exist in my install of gcc

well, even after updating cygwin I dont have that file, or the i686 equivelant, all I have is i686-pc-cygwin-gcc, which gives me the saem error as before.

whoever thought the hardest part of writing an OS would be setting up the toolchain...

turns out, a friend on another site suggested runnign nm kernel.o, and it turns out the compiler is mangling the name fo the function to __Z5kmainv, so after changing the references to that it linked just fine.
Hyperdrive
Member
Member
Posts: 93
Joined: Mon Nov 24, 2008 9:13 am

Re: Problem linking kernel.o and loader.o

Post by Hyperdrive »

abachler wrote:turns out, a friend on another site suggested runnign nm kernel.o, and it turns out the compiler is mangling the name fo the function to __Z5kmainv, so after changing the references to that it linked just fine.
Seems you're putting your file through g++, which does name mangling.

Try adding

Code: Select all

extern "C"
in front of your function. So it will look like this:

Code: Select all

extern "C"
void kmain( void* mbd, unsigned int magic )
{ ....
That turns off the name mangling for kmain.

Regards,
Thilo
sirprize
Posts: 1
Joined: Thu Feb 12, 2009 6:01 am

Re: Problem linking kernel.o and loader.o

Post by sirprize »

Hi Thilo,

I just ran into this exact problem. Thanks for your post, I wasn't aware of the fact that g++ does name mangling. With extern "C" decalaration it works well.

Regards,
Michael
xlq
Member
Member
Posts: 36
Joined: Mon Dec 11, 2006 7:51 am

Re: Problem linking kernel.o and loader.o

Post by xlq »

All C++ compilers do name mangling. It's how it solves operator overloading and scoping issues.
Marionette the flexible kernel
unplygOS
Posts: 3
Joined: Wed Aug 06, 2008 12:27 pm

Re: Problem linking kernel.o and loader.o

Post by unplygOS »

I found that if I have

Code: Select all

 extern kmain
call kmain
in boot.s
and

Code: Select all

void kmain( void* mbd, unsigned int magic )
in kernel.c
and follow the rest of the tutorial. Of course you could always add an underscore to kmain. It worked for me.
xlq
Member
Member
Posts: 36
Joined: Mon Dec 11, 2006 7:51 am

Re: Problem linking kernel.o and loader.o

Post by xlq »

unplygOS wrote:I found that if I have

Code: Select all

 extern kmain
call kmain
in boot.s
and

Code: Select all

void kmain( void* mbd, unsigned int magic )
in kernel.c
and follow the rest of the tutorial. Of course you could always add an underscore to kmain. It worked for me.
Yes, but was your kernel C or C++? There's a big difference.

With the underscore, I'd prefer to get the compiler to output the proper symbol names, (-fno-leading-underscore) rather than putting the underscores in your assembly code.
Marionette the flexible kernel
Post Reply