2

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?

2 Answers 2

2

Finally i'm using libffi .

1

The Wikipedia page has a decent example.

This is not the solution you are looking for. makecontext doesn't take an array but a variable argument list. So, in order to call it you need a function to convert an array to an argument list. Since that is what you want makecontext for, by the time you can use it you have already solved your problem.

I don't know what the solution is, but this is a dead end.

2
  • Ok, but HOW do i insert ulong values onto the stack? Mar 26, 2010 at 0:44
  • Ok got it, but the problem is that makecontext wants the arguments of the functions and i do not know their number, they could be 2 arguments as none, or 4 ... Mar 26, 2010 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.