Search code examples
pluginsadagnat

GNAT examples / plugins: lib function call without dlsym()?


README.testing says

Then 'demo' will use Plugins.Call to

  1. retrieve an access to a symbol in the dynamic library using the dlsym() routine.

Plugins.Call is the only subprogram which calls dlsym. Now:

  • grep doesn't find "Call" in any source file besides the Plugins sources
  • A Put_Line placed at the end of Call never produces any output
  • A Put_Line placed at the end of Plugins.Load produces output, so I am fumbling in the right package (plugins_unix.adb).

In short: It works, but I don't know why. How can a function from a library, loaded at run time, be called without using dlsym?


Solution

  • Finally I understood the scheme.

    The trick is that the main program never calls plugin's functions! It only loads the plugin (libraries) with dlopen.

    All plugins are descendents of an abstract base type, and each plugin has 2 global variables declared in its package body:

    1. one of class wide type (to the base type)
    2. a controlled variable (Limited_Controlled with null record)

    The respective overriding Initialize procedure:

    1. calls a constructor and assigns the result to the first variable
    2. calls a registration subprogram from the main program which introduces this new object.

    The main program can call then all the object's methods, which it knows from the abstract base type.

    So in short, the plugin generates an object when the library is loaded, and the main program acts like "We won't call you, call us."