Page 2 of 2

Re: Linker and undefined function references

Posted: Sat Aug 01, 2009 9:00 am
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...

Re: Linker and undefined function references

Posted: Sat Aug 01, 2009 9:19 am
by Combuster
objdump? -fno-leading-underscores? The reason the first example works is because there are zero references to other files.

Re: Linker and undefined function references

Posted: Sat Aug 01, 2009 10:38 am
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)

Re: Linker and undefined function references

Posted: Sat Aug 01, 2009 4:25 pm
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.

Re: Linker and undefined function references

Posted: Mon Aug 03, 2009 1:02 am
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..-