Do dynamic linked libraries(.dll,.so etc) have an entry point?
Asked Answered
S

1

6

Today I was in a discussion involving that libraries dont have an entry point.Generally the executable loads the libraries and the entry point is the main in the executable itself.

Are there execeptions wherein the libraries themselves can have an entry point ??

Update:

@sgr91 explained that DllMain is the entry point in Windows! What about linux ? Or is it just a feature of Windows ?

Stickler answered 17/1, 2013 at 9:35 Comment(7)
The entry point in DLL is optional. The function DllMain is the entry point of the DLL. If you want to do some initial work on DLL load, you can create the function, else you can skip it.Denudate
@sgar91 Can u elaborate more on what does "initial work on dll load" mean ? It would be helpful for me to understand :)Stickler
e.g you want to call a specific function when a DLL is loaded, you can call the function in DLLMain. Although it is highly discouraged to call an external function in DllMain.Denudate
Sorry I don't have much knowledge of Linux.Denudate
@sgar91 on Linux, there are _init and _fini sections, pointing to functions to run on load/unload. Roughly the same functionality as DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH for DllMain.Bennettbenni
@Denudate Any clue for QNX (entry and exit points in dynamic link library)?Aulic
@AntonKovalenko Any clues for QNX platform?Aulic
A
3

Yes, dynamic libraries do have entry-points. It may be named differently (may or may not be exposed for usage), based on compiler and OS.

For Linux:

void __attribute__ ((constructor)) my_init(void);

void __attribute__ ((destructor)) my_fini(void);

The _init and _fini sections are now obsolete.

Read more

Aulic answered 5/1, 2018 at 3:38 Comment(7)
@yugr This is unrelated.Nevarez
@JohanBoulé You mean suggesting to use standard language mechanisms instead of custom compiler extensions is unrelated?Strut
@Strut You seemed to be confusing these two unrelated concepts. Library entry points are not standardised, so you have to use nonstandard definitions. When you dynamically load or unload a library, these functions are called (a bit like the main entry point of a program). Where do objects and their ctors/dtors fit into this picture? They don't. Show me some equivalent technic with static objects if you think you can obtain the same result.Nevarez
@JohanBoulé "Where do objects and their ctors/dtors fit into this picture" - by allowing one to write portable (de)initializers without resorting to compiler extensions...Strut
@Strut Are you implying, without being explicit at all, that static objects of a library are destroyed/freed (and, IF C++ is used, their dtor called), when we call dlclose/FreeLibrary to unload it ? I might buy your argument about static initialisation talking place before dlopen/LoadLibrary returns, but what about unloading?Nevarez
@JohanBoulé They definitely are on Linux but I read they are not called in DLLs. This indeed making my comment above incorrect.Strut
@Strut Well, that actually makes your comment relevant on linux at least :) You've taught something new to me.Nevarez

© 2022 - 2024 — McMap. All rights reserved.