[SOLVED] Linker and undefined function references

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.
luiscubal
Posts: 13
Joined: Tue Jul 28, 2009 3:59 pm

Re: Linker and undefined function references

Post by luiscubal »

Just for testing, I came up with this sample Asm/C program:

Code: Select all

segment .data

segment .bss

segment .text
	global happy
happy:
	push ebp
	mov ebp, esp
	
	mov eax, 5

	pop ebp
	ret

Code: Select all

#include <stdio.h>

extern long happy();

int main(){
	printf("I got: %d\n", happy());

	return 0;
}
And compiling it with

Code: Select all

nasm test.asm -o test.o -f elf32
gcc -m32 main.c test.o -o experiment
works perfectly...

However,

Code: Select all

segment .data

segment .bss

segment .text

	extern global_var
	global happy
happy:
	push ebp
	mov ebp, esp
	
	mov eax, 5
	add eax, [global_var]

	pop ebp
	ret

Code: Select all

#include <stdio.h>

extern long happy();

int global_var = 3;

int main(){
	printf("I got: %d\n", happy());

	return 0;
}
does not, due to: "test.asm:(.text+0xa): undefined reference to `global_var'"...

I think I'm missing something about C-assembly interoperability...
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: Linker and undefined function references

Post by Combuster »

objdump? -fno-leading-underscores? The reason the first example works is because there are zero references to other files.
"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 ]
luiscubal
Posts: 13
Joined: Tue Jul 28, 2009 3:59 pm

Re: Linker and undefined function references

Post by luiscubal »

weird. Now I tried the exact same file and the same command and it worked.
I don't understand. I used the exact same commands and this time it worked... I wonder what happened the first time...
(the assembly test, not the kernel)
luiscubal
Posts: 13
Joined: Tue Jul 28, 2009 3:59 pm

Re: Linker and undefined function references

Post by luiscubal »

I asked someone else that was also making a kernel and he sent me his hello world kernel.
I've replaced my assembly and linker script by his. Although they look very similar(nearly identical), his seems to work. I'm not sure what happened here but I'm glad it's solved.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Linker and undefined function references

Post by Solar »

luiscubal wrote:I'm not sure what happened here but I'm glad it's solved.
It's not "solved" until you understand it..-
Every good solution is obvious once you've found it.
Post Reply