Skip to content

Instantly share code, notes, and snippets.

@Golevka
Created July 1, 2013 13:45
Show Gist options
  • Save Golevka/5900915 to your computer and use it in GitHub Desktop.
Save Golevka/5900915 to your computer and use it in GitHub Desktop.
Calling C functions with parameter lists, does not play well with structs or doubles.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void *__call_varg_impl(void *f, int n_args, void **argv)
{
unsigned int param[n_args];
int ret;
memcpy(param, argv, n_args * sizeof(void*));
asm volatile ("movl %0, %%esp;" : : "r" (param));
asm volatile ("call %0;" : : "r" (f));
asm volatile ("movl %%eax, %0;" : "=r" (ret));
return ret;
}
struct __test_struct {
int a;
int b;
int c;
};
int testfunc(int a, int b, struct __test_struct *_st0, struct __test_struct *_st1)
{
return a + b + _st0->a + _st0->b + _st0->c +
_st1->a + _st1->b + _st1->c;
}
int main(int argc, char *argv[])
{
struct __test_struct st0 = {1,2,3}, st1 = {4,5,6};
void **arglst[] = {
10, 20, &st0, &st1
};
int res = (int) __call_varg_impl(testfunc, sizeof(arglst), arglst);
printf("%d\n", res);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment