This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Constructing function calls
- From: Jean-Sebastien Legare <jslegare at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 21 Jul 2005 18:51:29 -0400
- Subject: Constructing function calls
- Reply-to: Jean-Sebastien Legare <jslegare at gmail dot com>
Hi
I have a special task I would like to accomplish regarding construction of
function calls, without (preferably) using __asm__ sections.
I would like to pass a block of data containing arguments to a function from
which I don't know the type nor number of arguments. The only thing I know
is the address of the function pointer and the size of the argument block
(number of 32bit words).
That is, I would like to fill this function:
void call_func( (void*)func(), void* args, int size) {
1. push the contents of args to the stack;
2. call func() with no args (func will recuperate them from the stack);
3. return
}
For instance, I would like to be able to call :
void print3ints(int a, int b, int c) {
printf(" %d %d %d\n",a,b,c);
}
by doing :
{
int* args = malloc( 3 *sizeof(int));
args[0] = 1; args[1] = 2; args[2] = 3;
call_func(print3ints,args,3);
}
I looked at "__builtin_apply" but it requires calling "__builtin_apply_args"
beforehand, which I cannot do.
I successfully managed to do that without using __asm__ on a x86.
I filled a local int array in call_func with the passed argument block and then
I called the passed function (stack bashing).
However, the method breaks when compiling in -O3 mode and it would certainly
not work on other systems where some arguments are passed inside
registers.
What are my best options ?