【发布时间】:2017-06-06 09:56:05
【问题描述】:
在glibc(我相信还有EGLIBC)中,libc.so 库有一个main() 方法:
$ /lib/i386-linux-gnu/libc.so.6
GNU C Library (Debian GLIBC 2.19-18+deb8u1) stable release version 2.19, by Roland McGrath et al.
Copyright (C) 2014 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.8.4.
Compiled on a Linux 3.16.7 system on 2015-08-30.
Available extensions:
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
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<http://www.debian.org/Bugs/>.
现在我想对以下简约示例做同样的事情(即我希望我自己的 SO 也有一个 main()):
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello, World!\n");
return 0;
}
作为常规可执行文件链接:
$ gcc -g -O0 -Wall -c test.c
$ gcc -o test test.o
$ ./test
Hello, World!
现在作为共享对象链接:
$ gcc -g -O0 -Wall -c test.c
$ gcc -shared -o libtest.so test.o
$ ./libtest.so
Segmentation fault
-
nm表示存在main符号 (000004f5 T main) - 调试时,
gdb没有显示任何有意义的回溯。 - 在 gcc 命令行中添加
-fpic或-fPIC没有帮助。
我做错了什么?
【问题讨论】:
-
@Someprogrammerdude 我没有特别想解决的问题。我完全理解在 SO 中拥有
main()几乎是没用的。我只是想知道它是如何在 glibc 中完成的。
标签: c linux linker shared-libraries