VACALL(3)
π VACALL(3) β C functions called with variable arguments π
okay bestie, welcome to the vacall page β it's a Library Functions Manual situation, and honestly it's giving "magic but make it macros" β¨
πΈ NAME πΈ
vacall</purgle> β C functions called with variable arguments. That's the whole vibe. No cap. π¦
β¨ SYNOPSIS β¨
#include <vacall.h>
extern void* vacall_function;
void function (va_alistalist)
{
va_start_type(alist[, return_type]);
arg = va_arg_type(alist[, arg_type]);
va_return_type(alist[[, return_type], return_value]);
}
vacall_function = &function;
val = ((return_type (*) ()) vacall) (arg1,arg2,...);
π DESCRIPTION π
So this little set of macros lets a C function β literally just function β get called with variable arguments AND hand back variable return values. Think stdarg(3) energy but with a glow-up: the return value can be decided at run time. Main character behaviour, honestly. π
Function calling conventions differ considerably on different machines (rude of them, truly), and vacall is out here trying to give you some degree of isolation from those architecture dependencies. π‘οΈ
The function that can be called with any number and any type of arguments, and that will return any type of return value, is vacall. It does some magic β¨ and calls the function stored in the variable vacall_function. And listen β if you wanna use vacall more than once, use the trampoline(3) facility to stash &function into vacall_function just before calling vacall. π
Inside function, these macros are your besties for walking the argument list and specifying a return value:
va_start_type(alist[, return_type]);β starts the walk through the argument list and specifies the return type. πΆβοΈarg = va_arg_type(alist[, arg_type]);β fetches the next argument from the argument list. πva_return_type(alist[[, return_type], return_value]);β ends the walk through the argument list and specifies the return value. π
π the type rules (iykyk)
The type in va_start_type and va_return_type shall be one of void, int, uint, long, ulong, longlong, ulonglong, double, struct, ptr or (for ANSI C calling conventions only) char, schar, uchar, short, ushort, float, depending on the class of return_type. That's the whole menu, no substitutions bestie.
The type specifiers in va_start_type and va_return_type must be the same. The return_type specifiers passed to va_start_type and va_return_type must be the same. π―βοΈ
The type in va_arg_type shall be one of int, uint, long, ulong, longlong, ulonglong, double, struct, ptr or (for ANSI C calling conventions only) char, schar, uchar, short, ushort, float, depending on the class of arg_type.
π§ the splittable situation
In va_start_struct(alist, return_type, splittable); the splittable flag specifies whether the struct return_type can be returned in registers such that every struct field fits entirely in a single register. This needs to be specified for structs of size 2*sizeof(long). For structs of size <= sizeof(long), splittable is ignored and assumed to be 1. For structs of size > 2*sizeof(long), splittable is ignored and assumed to be 0. π«Ά
There are some handy macros for this:
va_word_splittable_1 (type1)
va_word_splittable_2 (type1, type2)
va_word_splittable_3 (type1, type2, type3)
va_word_splittable_4 (type1, type2, type3, type4)
For a struct with three slots
struct { type1 id1; type2 id2; type3 id3; }
you can specify splittable as va_word_splittable_3 (type1, type2, type3) .
π NOTES π
Functions which want to emulate Kernighan & Ritchie style functions (i.e., in ANSI C, functions without a typed argument list) cannot use the type values char, schar, uchar, short, ushort, float. As prescribed by the default K&R C expression promotions, they have to use int instead of char, schar, uchar, short, ushort and double instead of float. Rules are rules, babe. π
The macros va_start_longlong(), va_start_ulonglong(), va_return_longlong(), va_return_ulonglong(), va_arg_longlong() and va_arg_ulonglong() work only if the C compiler has a working long long 64-bit integer type. π₯
β οΈ The struct types used inva_start_struct()andva_struct()must only contain (signed or unsigned)int,long,long longor pointer fields. Struct types containing (signed or unsigned)char,short,float,doubleor other structs are not supported. π
π EXAMPLE π
This example, a possible implementation of execl(3) on top of execv(2) using stdarg(3),
#include <stdarg.h>
#define MAXARGS 100
/* execl is called by execl(file, arg1, arg2, ..., (char *)0); */
int execl (...)
{
va_list ap;
char* file;
char* args[MAXARGS];
int argno = 0;
va_start (ap);
file = va_arg(ap, char*);
while ((args[argno] = va_arg(ap, char*)) != (char *)0)
argno++;
va_end (ap);
return execv(file, args);
}
looks like this using vacall(3):
#include <vacall.h>
#define MAXARGS 100
/* execl is called by vacall(file, arg1, arg2, ..., (char *)0); */
void execl (va_alist ap)
{
char* file;
char* args[MAXARGS];
int argno = 0;
int retval;
va_start_int (ap);
file = va_arg_ptr(ap, char*);
while ((args[argno] = va_arg_ptr(ap, char*)) != (char *)0)
argno++;
retval = execv(file, args);
va_return_int (ap, retval);
}
vacall_function = &execl;
ate that. no crumbs. π₯
π SEE ALSO π
stdarg(3), trampoline(3), callback(3).
π BUGS π
Okay so, the tea β:
- The current implementations have been tested on a selection of common cases but there are probably still many bugs. π
- There are typically built-in limits on the size of the argument-list, which may also include the size of any structure arguments.
- The decision whether a struct is to be returned in registers or in memory considers only the struct's size and alignment. This is inaccurate: for example,
gcconm68k-nextreturnsstruct { char a,b,c; }in registers andstruct { char a[3]; }in memory, although both types have the same size and the same alignment. π - The argument list can only be walked once.
- The use of the global variable
vacall_functionis not reentrant. This is fixed in thecallback(3)package. β¨
π PORTING π
Knowledge about argument passing conventions can be found in the gcc source, file gcc-2.6.3/config/cpu/cpu.h, section "Stack layout; function entry, exit and calling."
The implementation of varargs for gcc can be found in the gcc source, files gcc-2.6.3/ginclude/va*.h.
gcc's __builtin_saveregs() function is defined in the gcc source, file gcc-2.6.3/libgcc2.c.
Deep lore, bestie. Read the source. π¦
π AUTHOR π
Bruno Haible <bruno@clisp.org>
π ACKNOWLEDGEMENTS π
Many ideas and a lot of code were cribbed from the gcc source.
and that's the whole page, bestie β slay π
This was the VACALL(3) page from the Library Functions Manual, dated 1 January 2017, version VACALL(3). go forth and call functions with variable arguments, you absolute icon π«Άβ¨
π¦ where this page comes from
- alpine 3.24: ffcall-doc 2.5-r0 Β· GPL-2.0-or-later
- arch rolling: ffcall 2.5-1 Β· GPL-2.0-or-later
- debian bookworm: libffcall-dev 2.4-2 Β· GPL-2.0-or-later
- debian trixie: libffcall-dev 2.5-2 Β· GPL-2.0-or-later
- nixpkgs unstable: libffcall 2.5
- ubuntu jammy: libffcall-dev 2.4-1 Β· GPL-2.0-or-later
- ubuntu noble: libffcall-dev 2.4-2.1build1 Β· GPL-2.0-or-later
- ubuntu resolute: libffcall-dev 2.5-2 Β· GPL-2.0-or-later
- ubuntu stonking: libffcall-dev 2.5-2ubuntu1 Β· GPL-2.0-or-later
- Copyright (C) 1995-2017 Bruno Haible
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms