setcontext and makecontext to call a generic function pointer

Posted by Simone Margaritelli on Stack Overflow See other posts from Stack Overflow or by Simone Margaritelli
Published on 2010-03-26T00:24:56Z Indexed on 2010/03/26 0:43 UTC
Read the original article Hit count: 349

Filed under:
|
|
|

In another question i had the problem to port the code

unsigned long stack[] = { 1, 23, 33, 43 };

/* save all the registers and the stack pointer */
unsigned long esp;
asm __volatile__ ( "pusha" );
asm __volatile__ ( "mov %%esp, %0" :"=m" (esp));

for( i = 0; i < sizeof(stack); i++ ){
    unsigned long val = stack[i];
    asm __volatile__ ( "push %0" :: "m"(val) );
}

unsigned long ret = function_pointer();

/* restore registers and stack pointer */
asm __volatile__ ( "mov %0, %%esp" :: "m" (esp) );
asm __volatile__ ( "popa" );

To a 64bit platform and many guys told me i should use the setcontext and makecontext functions set instead due to the calling conversion differences between 32 and 64 bits and portability issues.

Well, i really can't find any useful documentation online, or at least not the kind i need to implement this, so, how can i use those functions to push arguments onto the stack, call a generic function pointer, obtain the return value and then restore the registers?

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++