【问题标题】:Creating a shared library with main() [duplicate]使用 main() 创建共享库 [重复]
【发布时间】: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


【解决方案1】:

libc.so 库有一个 main() 方法

不,gcc 的 C 库的实现有一个入口点而不是主符号。

您可以在here找到一个示例。

【讨论】:

  • nope 无法执行二进制文件:执行格式错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 2011-03-11
相关资源
最近更新 更多