gpt4 book ai didi

使用 main() 创建共享库

转载 作者:可可西里 更新时间:2023-11-01 11:45:21 28 4
gpt4 key购买 nike

<分区>

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 不显示任何有意义的回溯。
  • -fpic-fPIC 添加到 gcc 命令行没有帮助。

我做错了什么?

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com