weak_alias 函数的作用及其定义位置

what does the weak_alias function do and where is it defined

所以我正在查看 gcc 编译器的源代码,我在 fork.c:

中找到了这个
int
__fork ()
{
  __set_errno (ENOSYS);
  return -1;
}
libc_hidden_def (__fork)
stub_warning (fork)

weak_alias (__fork, fork)
#include <stub-tag.h>

我想弄清楚 weak_alias 的作用。我在 glibc 源文件中使用 grep 命令查找所有出现的 #define weak_alias:

grep -r "#define weak_alias"

我发现了很多次宏:

#define weak_alias(n, a)

但是宏实际上并没有解释任何东西。他们只是定义了那个声明,他们没有显示它是如何被替换的。例如,在 profil.c:

中出现了一次
/* Turn off the attempt to generate ld aliasing records. */
#undef weak_alias
#define weak_alias(a,b)

那么知道 weak_alias 的作用和定义在哪里吗?

提前致谢

来自 https://github.com/lattera/glibc/blob/master/include/libc-symbols.h

/* Define ALIASNAME as a weak alias for NAME.
   If weak aliases are not available, this defines a strong alias.  */
# define weak_alias(name, aliasname) _weak_alias (name, aliasname)
# define _weak_alias(name, aliasname) \
  extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));

关于弱符号:

https://en.wikipedia.org/wiki/Weak_symbol

这是一个执行以下操作的宏:

它声明了一个弱函数,如果您没有为该函数提供强符号名称,它将调用您指定的函数。例如

int _foo(){ return 1;}

//And weak alias
int __attribute__((weak, alias("_foo"))) foo();

因此,如果您没有为 foo 提供实际实现,它基本上会使用 _foo 和 return 1.