可执行文件共享库 [英] Executable shared libraries

查看:196
本文介绍了可执行文件共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,当您编译共享库,执行这是毫无意义的,这样做会产生什么有用的:

Most of the time, when you compile a shared library, executing it is meaningless and doing so produces nothing useful:

$ ./libfoobarbaz.so
Segmentation fault

然而,在GNU的人们已经能够在一些输出坚持执行的glibc时:

However, the folks at GNU have been able to stick in some output when glibc is executed:

$ /lib/libc.so.6
GNU C Library (Debian EGLIBC 2.11.2-10) stable release version 2.11.2, by Roland McGrath et al.
Copyright (C) 2009 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.5.
Compiled on a Linux 2.6.32 system on 2011-01-23.
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
For bug reporting instructions, please see:
<http://www.debian.org/Bugs/>.

虽然这个特定的用途似乎是普通的老膨胀对我来说,他们是如何实现决策的共享库,也作为一个工作的可执行文件?

Although this particular use seems like plain old bloat to me, how did they achieve making a shared library that also acts as a working executable?

推荐答案

这里的窍门是,该可执行文件和共享对象库使用名为ELF的相同的格式和 libc.so 的,在code合作发现的的crt0.o 的(在* nixes),编译器的一部分,实际上是负责建立运行时环境,然后调用适当的 INT主(...)功能。如果没有对链接的 libc.so 的crt0.o 的一个程序只 INT主(...)不会执行。技术上讲,它是可以设置的主要的功能是可执行文件入口点,但已开始这样的程序将不会收到命令行参数,没有环境等,这一切都在标准的责任运行时库的 libc.so

The trick here is, that executables and shared object libraries use the same format called ELF and that libc.so, in collaboration with code found in crt0.o (on *nixes), part of the compiler, actually is responsible for setting up the runtime environment and then call the proper int main(...) function. Without linking against libc.so and crt0.o a program with just int main(...) will not execute. Technically it is possible to set the main function to be the executable entry point, but being started like that the program will not recieve command line arguments, no environment, etc. that's all in the responsibility of the standard runtime library libc.so

所以的 libc.so 的,是还负责preparing的 INT主(...)函数的调用可以轻松确定它是否得到了一些其他程序链接,或者如果它是独立。如果该进程得到了通过的 libc.so 的入口点开始,将显示此消息,然后退出。只有当过程是通过一个二进制可执行文件的入口点,它开始通过神奇的二进制临危的的crt0.o 的进程将照常运行。

So libc.so, being also responsible for preparing the call of the int main(...) function can easily determine if it got linked by some other program, or if it is "stand alone". If the process got started through the libc.so entry point in it will display this message, then exit. Only if the process is started through an executables binary entry point, which the binary recieves through that magic crt0.o the process will run as usual.

这篇关于可执行文件共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆