【发布时间】:2015-08-17 18:05:31
【问题描述】:
在 Linux 中,GNU 标准 C 库 (libc.so) 的共享库不仅是共享库,还可以作为独立的可执行文件运行,它会打印出版本信息:
[me@computer ~]$ /lib/libc.so.6
GNU C Library stable release version 2.12, by Roland McGrath et al.
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.4.7 20120313 (Red Hat 4.4.7-11).
Compiled on a Linux 2.6.32 system on 2015-01-07.
Available extensions:
The C stubs add-on version 2.1.2.
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
RT using linux kernel aio
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.
他们是怎么做到的?我尝试创建一个共享库,该库也恰好有一个 main() 函数,但是当我尝试运行它时它出现了段错误。
我尝试了什么:
/* myso.c */
#include <stdlib.h>
#include <stdio.h>
static void foo(void) {
printf("hello foo\n");
}
int main(int argc, char *argv[]) {
printf("hello world\n");
foo();
return 0;
}
然后:
$ gcc -Wall -fPIC -shared -o myso.so myso.c
$ ./myso.so
Segmentation fault (core dumped)
作为说明:我真的不想出于任何实际目的这样做,我只是想知道 GNU 家伙(和女孩)是如何做到的。
【问题讨论】:
-
有可能,请尝试在您的机器上运行 /lib/libc.so。而且我真的不想这样做,我想知道 GNU 的家伙(和女孩)是如何做到的。
-
有趣,里面有一个
__libc_main()函数。 -
书中运行它的目的是什么?
-
只是为了说明你可以通过运行来了解系统的libc.so的版本和编译信息。
-
尝试将我的
main更改为__libc_main()。虽然仍然存在段错误。
标签: c linux compilation shared-libraries