linux -/usr/lib64/libc.so是怎么生成的?

标签 linux glibc ld

[root@xx test]# cat /usr/lib64/libc.so
/* GNU ld script
   Use the shared library, but some functions are only in
   the static library, so try that secondarily.  */
OUTPUT_FORMAT(elf64-x86-64)
GROUP ( /lib64/libc.so.6 /usr/lib64/libc_nonshared.a  AS_NEEDED ( /lib64/ld-linux-x86-64.so.2 ) )

有人知道这种东西是怎么产生的吗?

最佳答案

这是在使用 Make 实用程序编译 glibc 时生成的。

glibc 的 Makefile 中有一条规则(由 make install 启动),它只是将需要的行回显到某个临时文件 $@.new 中:

(echo '/* GNU ld script';\
 echo '   Use the shared library, but some functions are only in';\
 echo '   the static library, so try that secondarily.  */';\
 cat $<; \
 echo 'GROUP ( $(slibdir)/libc.so$(libc.so-version)' \
      '$(libdir)/$(patsubst %,$(libtype.oS),$(libprefix)$(libc-name))'\
      ' AS_NEEDED (' $(slibdir)/$(rtld-installed-name) ') )' \
) > $@.new

然后这个文件重命名为libc.so

mv -f $@.new $@

这是来自 Makefile 的注释,它解释了一点:

# What we install as libc.so for programs to link against is in fact a
# link script.  It contains references for the various libraries we need.
# The libc.so object is not complete since some functions are only defined
# in libc_nonshared.a.
# We need to use absolute paths since otherwise local copies (if they exist)
# of the files are taken by the linker.

我的理解是:libc.so.6 不完整,需要一些不能存储在共享库中的东西。因此,glibc 开发人员将此内容移至 glibc 的静态部分 - libc_nonshared.a。为了强制始终链接 libc.so.6libc_nonstared.a,他们创建了一个特殊的链接脚本,指示 ld 链接器在请求 时使用两者-lc (libc)

非共享部分是什么?让我们检查一下:

$ objdump -t /usr/lib/libc_nonshared.a |grep " F "|grep -v __
00000000 g     F .text  00000058 .hidden atexit
00000000  w    F .text  00000050 .hidden stat
00000000  w    F .text  00000050 .hidden fstat
00000000  w    F .text  00000050 .hidden lstat
00000000 g     F .text  00000050 .hidden stat64
00000000 g     F .text  00000050 .hidden fstat64
00000000 g     F .text  00000050 .hidden lstat64
00000000 g     F .text  00000050 .hidden fstatat
00000000 g     F .text  00000050 .hidden fstatat64
00000000  w    F .text  00000058 .hidden mknod
00000000 g     F .text  00000050 .hidden mknodat
00000000 l     F .text  00000001 nop

atexit()*stat*()mknod函数。为什么?真的不知道,但这是 glibc 的一个事实。

这里有一些很长的解释http://giraffe-data.com/~bryanh/giraffehome/d/note/proglib我引用它的开头:

The stat() family of functions and mknod() are special.  Their
interfaces are tied so tightly to the underlying operating system that
they change occasionally. 

关于linux -/usr/lib64/libc.so是怎么生成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5582688/

相关文章:

gcc - GCC 和 LD 链接有什么区别?

linux - 在 Linux 中与 i2c 设备通信时遇到问题

macos - 如何在Mac OS X 10.6上安装GNU LD?

Linux、GNU GCC、ld、版本脚本和 ELF 二进制格式——它是如何工作的?

c - 为什么 valgrind 报告 glibc tsearch() 随机泄漏内存?

c - 使用 sscanf 解析十进制字符串

c - 自由(): invalid next size (fast) string too long?

linux - 在 Linux : E: The "https://download.docker.com/linux/ubuntu una Release" repository does not have a Release file 上安装 Docker 时出现问题

python - Amazon Linux AMI EC2 - librrd.so.2 依赖性问题

linux - kdevelop智能感知