Shared Library Constructor is not executed

青春壹個敷衍的年華 提交于 2019-12-04 03:43:01

This text is meant for reference, but I'm coming over to your office for convenience :)

I'm not an expert on that field but a quick Google search gave me this. Reading just the beginning of the document and if I get it right the problem is this:

Linked statically your program is self-contained at execution time... it has the whole library in it and it's completely loaded into memory when you run it.

Linked dynamically when the library function is called from your program at execution time the linker tries to resolve all unresolved references on functions by looking if it has an implementation in some library. If so it loads this implementation, i.e. just the functions code.

So if I get this right and the dynamic linker just loads portions of libraries, i.e. needed functions, and not the whole library then this would explain why your constructor isn't called when your library is linked dynamically.

Gcc's constructor handling is not the same thing as the ELF constructor handling, rather, it sits on top of it. To work, you need to link in the glue code that is provided in gcc's startup files.

The easiest way to do that is to link using gcc:

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