Page 1 of 1

Standardized GCC ABI?[SOLVED]

Posted: Sun Dec 12, 2010 12:53 am
by CodeJunkie
Ok, so im chuggin along writing nice assembly code for some bits of my toy-kernel and realize something VERY interesting ... it seems that when I build a '.c' code file with:

Code: Select all

int some3( int im_expectin_int ){ return im_expectin_int; }
gcc -m32 -g -ffreestanding -nostdlib -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -nostdinc -fno-builtin -nostartfiles -nodefaultlibs -S -O0 -c file.c -o file.S
I expect the resulting code to be:

Code: Select all

.globl some3
	.type	some3, @function
some3:
	pushl	%ebp
	movl	%esp, %ebp

        // Expecting the %edi register to be the standard 1st argument, like always
	movl	%edi, %eax

        // Cleanup
        movl %ebp, %esp
	popl	%ebp
	ret
But something strange happens and I get this:

Code: Select all

.globl some3
	.type	some3, @function
some3:
	pushl	%ebp
	movl	%esp, %ebp
        // Wait WHAAAAT?
	movl	8(%ebp), %eax  
	popl	%ebp
	ret
:shock: It seems that GCC is applying one ABI standard when compiling programs for your native operating system; consequently, when writing for another OS ... my kernel ... it acts like its never had the previous ABI and it starts using the minuscule amount of stack space I have for passing arguments! What the heck?! Is there a way to use the same ABI specs for cross compiling? #-o

.... edit ....
You know what, now that I think about it ... it kinda makes sense. When doing 64-Bit assembly you have 16 GP registers and in 32-Bit you only have 8 and some are for specific uses. But does anybody know where to find a website that actually documents the the way functions communicate with each other in 32-bit mode?

Re: Standardized GCC ABI?[SOLVED]

Posted: Sun Dec 12, 2010 2:20 am
by xenos
I guess you need to look for function call conventions like stdcall (pass arguments on the stack), fastcall (put some arguments into registers) and so on:

http://en.wikipedia.org/wiki/X86_calling_conventions

Re: Standardized GCC ABI?[SOLVED]

Posted: Sun Dec 12, 2010 4:13 am
by gerryg400
I'm really confused about what you are doing. Are you trying to compile 32 or 64 bit code ?

Re: Standardized GCC ABI?[SOLVED]

Posted: Sun Dec 12, 2010 3:30 pm
by CodeJunkie
XenOS wrote:I guess you need to look for function call conventions like stdcall (pass arguments on the stack), fastcall (put some arguments into registers) and so on:

http://en.wikipedia.org/wiki/X86_calling_conventions
Thank-you that is exactly the kind of thing I was looking for, I just didn't know what exactly to look for. The conventions were lazily called ABI Spec. in my assembly class. And they were teaching x86-64 ASM, I just assumed that they build the calling conventions on the previous architecture like they did for the entirety of the x86 line. I know realize that it is a compiler level convention.
gerryg400 wrote:I'm really confused about what you are doing. Are you trying to compile 32 or 64 bit code ?
If you check the GCC line i included at the top .... -m32 ... 32-bit. I have really only used 64-bit calling conventions until this point. I had no clue that gcc used the stack to pass arguments for 32-bit. And even the fast call is different than 64-bit as 64-bit uses %edi and %esi or %rdi and %rsi for the first two arguments.
berkus wrote:You're using -O0.
Thats just optimization stuff ... it shouldn't really effect the function calling conventions(ABI?) that I am dealing with. But I appreciate the help. ^_^

Re: Standardized GCC ABI?[SOLVED]

Posted: Sun Dec 12, 2010 4:15 pm
by gerryg400
If you check the GCC line i included at the top .... -m32 ... 32-bit. I have really only used 64-bit calling conventions until this point. I had no clue that gcc used the stack to pass arguments for 32-bit. And even the fast call is different than 64-bit as 64-bit uses %edi and %esi or %rdi and %rsi for the first two arguments.
So you can see why I was confused. You were using -m32 but also using the 64-bit calling convention.

Re: Standardized GCC ABI?[SOLVED]

Posted: Sun Dec 12, 2010 11:58 pm
by CodeJunkie
gerryg400 wrote:
If you check the GCC line i included at the top .... -m32 ... 32-bit. I have really only used 64-bit calling conventions until this point. I had no clue that gcc used the stack to pass arguments for 32-bit. And even the fast call is different than 64-bit as 64-bit uses %edi and %esi or %rdi and %rsi for the first two arguments.
So you can see why I was confused. You were using -m32 but also using the 64-bit calling convention.
Thats exactly it. Being naive got me into this #-o ... I just thought the 32-bit calling conventions were similar/identical to the 64-Bit ones ... it was just confusing at first. The extremely useful wiki article posted by XenOS, ty :D, about this particular topic definitely pointed me into the right direction ->http://en.wikipedia.org/wiki/X86_calling_convention. I think I will spend a little bit of time working with 16/32-bit as working at this low level is still fairly new to me. Thank you all for taking the time to reply.