2017-06-06 31 views
2

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並沒有幫助。

我在做什麼錯?

+0

@Someprogrammerdude我沒有特別的問題想解決。我完全理解在SO中有一個'main()'幾乎是無用的。我只是想了解它是如何在glibc中完成的。 – Bass

回答

4

的libc.so庫具有main()方法

否,GCC的C庫的執行有一個入口點不是一個主符號。

你可以找到一個如何做到這一點的例子here