Implementation of __builtin_va_start(v,l)

☆樱花仙子☆ 提交于 2019-12-21 12:14:40

问题


Going down the rabbit hole of variadic macros in glibc, I’ve reached /usr/lib/gcc/x86_64-linux-gnu/4.8.2/include/stdarg.h where, for example, the va_start macro is defined as:

#define va_start(v,l) __builtin_va_start(v,l)

But I’ve been trying to look for the actual implementation of __builtin_va_start(v,l) without success. I’ve googled and grepped for it, and the furthest I’ve gotten to is Microsoft’s implementation for Visual Studio, which I suppose isn’t radically different.

Does anybody know where glibc implementation is?

TIA.


回答1:


To look in the source code of gcc, download the matching version from http://www.netgull.com/gcc/releases/ For example, the 4.8.2 version is at http://www.netgull.com/gcc/releases/gcc-4.8.2/ (82 MB).

The builtin keyword is handled at line 4169 of gcc/builtins.c




回答2:


In general, to find how gcc expands the built-in gcc function whose name is '__builtin_foo', look in the gcc source for the declaration of the function 'expand_builtin_foo'.




回答3:


have a look at stdarg.h in kernel 0.01 linux for an idea - va_start is a macro that initializes ap with as an increment starting at the first argument plus its size (rounded to machine word size); va_arg sets ap as the type given, and increments further the ap in the same way (rounding the type to machine words)

#define __va_rounded_size(TYPE)  \
  ( ( (sizeof (TYPE) + sizeof (int) - 1) / sizeof (int) ) * sizeof (int)  )

#define va_start(AP, LASTARG)                       \
 (AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))

#define va_arg(AP, TYPE)                        \
  (AP += __va_rounded_size (TYPE),                  \
  *((TYPE *) (AP - __va_rounded_size (TYPE)) ))


来源:https://stackoverflow.com/questions/22642222/implementation-of-builtin-va-startv-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!