2

I can delay loading of a shared library using dlopen() with RTLD_NOW. But once library is dynamically loaded, still I need to use dlsym to load each symbols individually.

Since my library contains a large number of APIs, I don't want to call dlsym for all of them. Is there any way to make the APIs work same way as normal loadtime linking (where you just call the APIs without needing dlsym) ?

Lunar Mushrooms
  • 8,358
  • 18
  • 66
  • 88
  • You mean without needing to call `dlsym()` *explicitly*? – Ignacio Vazquez-Abrams Sep 12 '13 at 06:51
  • Yes, I just need to load the library dynamically. And API calls from application shall work normally - I dont want to rewrite the code to include dlsym() for every APIs. – Lunar Mushrooms Sep 12 '13 at 06:53
  • 1
    I am unmarking this as a duplicate because Linux's shared library semantics are completely different from Windows's shared library semantics; something that is possible on Windows is not necessarily possible on Linux and vice versa. (For the curious, the difference between [ELF](http://wiki.osdev.org/ELF) and [PE](http://msdn.microsoft.com/en-us/magazine/cc301805.aspx) executable image formats is what controls, here.) – zwol Jun 11 '14 at 15:31
  • https://stackoverflow.com/a/47221988/841108 is an answer to a nearly duplicate question – Basile Starynkevitch Nov 10 '17 at 12:16

3 Answers3

2

You might be looking for RTLD_GLOBAL.

From the page you linked to[1]:

RTLD_GLOBAL
      The symbols defined by this library will be made available for
      symbol resolution of subsequently loaded libraries.

By using this, you should be able to create a "wrapper" library around your API that doesn't need to use dlsym(), and in this way cut down on the total number of dlsym() calls needed. This of course raises the question of whether it's more effort to maintain a wrapper library than to clean up the API.

[1] http://man7.org/linux/man-pages/man3/dlsym.3.html

cha5on
  • 482
  • 4
  • 9
0

As explained here, you could have a plugin which contains an attribute(constructor) function registering many plugin functions into some global data structure provided by the main program (since constructors of plugins are called at the dlopen time of the plugin). So you could have dlopen-ed a plugin and not even use dlsym once to retrieve and use (many) plugin functions.

I need to use dlsym to load each symbols individually.

Wrong. dlsym finds functions (or data) by their symbol (or name). The function is already here (since the entire code segment of the plugin is added into your virtual address space by dlopen).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Probly not relevant anymore but still - you are looking for analog of Windows DLL import libraries which would provide stubs that look like normal functions to your code but call dlopen and dlsym internally.

Linux does not provide import libraries out-of-the-box but you can generate them by hand, via some custom script or use Implib.so tool to do this fully automatically.

yugr
  • 19,769
  • 3
  • 51
  • 96