Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are void* pointers returned in C?

Basically I need to know what register void* pointers are placed in when they are returned from a C function. I have this code:

void* kmalloc(unsigned int size)
{
    asm("mov %[size], %%esi"
    : /* no outputs */
    : [size] "m" (size)
    : "esi");
    asm("movl $9, %eax");
    asm("int $0x80");

}

which should put an address into EAX. I thought that return values in C were stored in EAX, but apparently not, (oh and I am using GCC BTW). I need to some how return EAX, register int won't work either because of the compiler settings. Is there a register that is used for returning pointers? Or is it like pushed onto the stack or something?

like image 440
user1454902 Avatar asked Jul 10 '26 08:07

user1454902


1 Answers

This is not a valid way to write inline asm. Even if you put the return value in the right register, it could be lost/clobbered by the time the function actually returns, because the compiler can add arbitrary epilogue code. You must use output constraints and a return statement to return the results of inline asm:

void* kmalloc(unsigned int size)
{
    void *p;
    asm("int $0x80" : "=a"(p) : "S"(size), "a"(9));
    return p;
}
like image 143
R.. GitHub STOP HELPING ICE Avatar answered Jul 12 '26 00:07

R.. GitHub STOP HELPING ICE