5

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 ?

Charles
  • 50,943
  • 13
  • 104
  • 142
sp497
  • 2,363
  • 7
  • 25
  • 43
  • 1
    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. – sgarizvi Jan 17 '13 at 09:37
  • @sgar91 Can u elaborate more on what does "initial work on dll load" mean ? It would be helpful for me to understand :) – sp497 Jan 17 '13 at 09:40
  • 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. – sgarizvi Jan 17 '13 at 09:48
  • Sorry I don't have much knowledge of Linux. – sgarizvi Jan 17 '13 at 09:49
  • 3
    @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`. – Anton Kovalenko Jan 17 '13 at 09:54
  • @sgarizvi Any clue for QNX (entry and exit points in dynamic link library)? – parasrish Jan 01 '18 at 07:13
  • @AntonKovalenko Any clues for QNX platform? – parasrish Jan 01 '18 at 07:14

1 Answers1

2

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

Spooky
  • 2,966
  • 8
  • 27
  • 41
parasrish
  • 3,864
  • 26
  • 32
  • 1
    @yugr This is unrelated. – Johan Boulé Apr 07 '19 at 02:36
  • @JohanBoulé You mean suggesting to use standard language mechanisms instead of custom compiler extensions is unrelated? – yugr Apr 08 '19 at 06:47
  • 1
    @yugr 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. – Johan Boulé Apr 08 '19 at 07:21
  • @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... – yugr Apr 08 '19 at 09:14
  • @yugr 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? – Johan Boulé Apr 08 '19 at 09:47
  • @JohanBoulé They definitely are on Linux but I read they are not called in DLLs. This indeed making my comment above incorrect. – yugr Apr 08 '19 at 11:14
  • @yugr Well, that actually makes your comment relevant on linux at least :) You've taught something new to me. – Johan Boulé Apr 08 '19 at 11:28