我细读glibc的时候我通过套接字code来了,可有人解释这是怎么回事? [英] I was perusing glibc when I came across the socket code, can someone explain what is going on?
问题描述
下面是我在浏览来源:<一个href=\"http://sourceware.org/git/?p=glibc.git;a=tree;f=socket;h=8217353b094378659dae035c0619f6495172523c;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f\">glibc来源。我特别的问题是从这一组特定的功能:<一href=\"http://sourceware.org/git/?p=glibc.git;a=tree;f=socket;h=8217353b094378659dae035c0619f6495172523c;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f\">socket库。
Here is the source I was browsing: glibc source. My particular question arises from this particular set of functions: socket library.
例如(大部分功能都设置了这种方式)的插座/ bind.c
来源:
For example(most of the functions are set up this way) socket/bind.c
's source is:
19 #include <errno.h>
20 #include <sys/socket.h>
21
22 /* Give the socket FD the local address ADDR (which is LEN bytes long). */
23 int
24 __bind (fd, addr, len)
25 int fd;
26 __CONST_SOCKADDR_ARG addr;
27 socklen_t len;
28 {
29 __set_errno (ENOSYS);
30 return -1;
31 }
32
33 weak_alias (__bind, bind)
34
35 stub_warning (bind)
36 #include <stub-tag.h>
我承认我没有花很多的时间,但究竟哪里是code的实际功能,这是怎么回事?这是一个很好用的模式?
I admit I didn't spend a lot of time, but where exactly is the code for the actual function and what is going on? Is this a well used paradigm?
推荐答案
的 __绑定
函数是一个的存根:它看起来像外部真实的东西(相同的原型),但不进行必要的函数的函数
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.
的<一个href=\"http://sourceware.org/git/?p=glibc.git;a=blob;f=include/libc-symbols.h;h=252141eb0111269d721c501b1bcac0cd94c3305a;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f#l110\"><$c$c>weak_alias$c$c>宏告诉链接器绑定
是要为 __绑定
弱别名。也就是说,绑定
的定义是弱符号。如果有一个名为绑定
符号,没有其他的定义,这个定义代表;如果有绑定
的另一个(非弱)的定义则该非弱定义站和弱定义将被忽略。弱别名是一个弱符号是另一个符号的别名(而不是让在自己的权利的定义)。在<一个href=\"http://sourceware.org/git/?p=glibc.git;a=blob;f=include/libc-symbols.h;h=252141eb0111269d721c501b1bcac0cd94c3305a;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f#l280\"><$c$c>stub_warning$c$c>宏将导致链接器,如果使用了弱别名发出警告。
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.
真正落实绑定
的依赖于操作系统的Glibc编译为上。在赫德,它<$c$c>sysdeps/mach/hurd/bind.c$c$c>.在Linux上,绑定
是一个系统调用:有Glibc源码,只有组装code没有C code吧。 绑定
在<一个提供href=\"http://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/bind.S;h=7719ad0be8bb6c2ad6c2722393b44f148a19de1c;hb=2543fef229599e8a6e4feeea65ca2dd3f984154f\"><$c$c>sysdeps/unix/sysv/linux/bind.S$c$c>其中重用插座
的体系结构相关的定义 sysdeps / UNIX / SYSV / Linux的/ ** / socket.S
或端口/ sysdeps / UNIX / SYSV / Linux的/ * / socket.S
。这些定义都是围绕着底层的系统调用的所有包装薄,注意该参数和返回值复制到适当的寄存器。
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.
这篇关于我细读glibc的时候我通过套接字code来了,可有人解释这是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!