Executable shared libraries

蹲街弑〆低调 提交于 2019-11-30 15:10:40

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

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.

The answer lies in the name "Executable and Linking Format". The same file format is used for both executables and libraries.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!