I was perusing glibc when I came across the socket code, can someone explain what is going on?

元气小坏坏 提交于 2019-12-03 12:40:47

The __bind function is a stub: it's a function that looks externally like the real thing (same prototype) but does not perform the requisite function.

The weak_alias macro tells the linker that bind is to be a weak alias for __bind. That is, this definition of bind is a weak symbol. If there is no other definition of a symbol called bind, this definition stands; if there is another (non-weak) definition of bind then that non-weak definition stands and the weak definition is ignored. A weak alias is a weak symbol that is an alias of another symbol (as opposed to having a definition in its own right). The stub_warning macro causes the linker to emit a warning if that weak alias is used.

The real implementation of bind depends on the operating system that Glibc is compiled for. On Hurd, it is defined in sysdeps/mach/hurd/bind.c. On Linux, bind is a system call: there is no C code for it in the Glibc source, only assembly code. bind is provided in sysdeps/unix/sysv/linux/bind.S which reuses the architecture-dependent definition of socket in sysdeps/unix/sysv/linux/**/socket.S or ports/sysdeps/unix/sysv/linux/*/socket.S. Those definitions are all thin wrappers around the underlying system call, taking care to copy the argument and the return values into the proper registers.

You're looking at the general bind() implementation which ... just tells you that bind() is not implementet (It just returns an error and sets errno to ENOSYS - syscall not implemented.).

Many platform dependent system calls in glibc work this way - there's a default implementation that just returns an error, and each platform/architector have to provide an implementation of the system call if one exists.

Look in e.g. ./sysdeps/unix/sysv/linux/i386/socket.S for the linux x86 implementation.

Ofcourse, socket() is just a system call, so the actual implementation is in the kernel. Glibc just provides a C wrapper to call it.

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