今日看nginx源码,有个environ变量,在没有赋值的情况下就用了,翻遍源码也没看到是在哪儿负的值,于是想到可能是系统中的变量,或者宏或者函数。在系统头文件中找到有关于他的信息,但是还是没有找到他的本意,于是在网上搜索
https://blog.csdn.net/tom601/article/details/70036701?utm_source=blogkpcl8
这篇文章说明了
environ变量定义为全局变量,位于glibc源代码posix/environ.c
中
#include <unistd.h>
#include <stddef.h>
/* This must be initialized; we cannot have a weak alias into bss. */
char **__environ = NULL;
weak_alias (__environ, environ)
/* The SVR4 ABI says `_environ' will be the name to use
in case the user overrides the weak alias `environ'. */
weak_alias (__environ, _environ)
继续追寻weak_alias
https://blog.csdn.net/weixin_39455835/article/details/80958105
weak-alias
是一个宏,其目的是为函数添加一个”弱”别名,与”强”符号函数名区分。
说明, 如果调用函数无对应的函数无”强”符号对应的函数,则会调用该别名对应的函数
C/C++ 函数调用是以编译后的”符号”做索引调用,详情了解 <符号表>
//macro weak_alias
#define weak_alias(name, aliasname) _mweak_alias(name, aliasname)
#define _weak_alias(name, aliasname) \
extern __typeof(name) aliasname __attribute__((weak, alias(#name)));
//macro weak_alias end12345
所谓”强”符号函数名就是,普通声明定义的函数对应的函数名
例如:
//strong.c
#include <stdio.h>
void print_hello(const char *s)
{
printf("print_hello: %s\n", s);
}
//strong.c end1234567
print_hello 就是一个”强”函数符号
以下测试两种情况
__weak_hello 有两个”弱符号”
print_hello 有对应的实体函数 而 print_world 则无
//weak.c
#include <stdio.h>
void print_hello(const char *s) __attribute__((weak, alias("__weak_hello")));
void print_world(const char *s) __attribute__((weak, alias("__weak_hello")));
void __weak_hello(const char *)
{
printf("__weak_hello: %s\n", s);
}
//weak.c end123456789
//main.c
int main(void)
{
print_hello("main test print_hello");
print_world("main test print_world");
return 0;
}
//main.c end12345678
编译运行测试
$ gcc strong.c weak.c main.c && ./a.out
print_hello: main test print_hello
__weak: main test print_world
$
可见其是一个弱符号,至于源头还是没有查询得到。