(GNU) `dlopen` a dynamic library multiple times

  1. The same file handle is returned multiple times.
    • If the path provided to dlopen differs (even when the absolute path is the same), different file handles are returned.
  2. A reference count is maintained by the dl library, so a dynamic library needs to be closed as many times using dlclose as it has been opened using dlopen.
    • The dynamic library is unloaded only when the reference count drops to 0 (+ no other loaded libraries use its symbol).
  3. dlopen with RTLD_NOLOAD flag can be used to dlopen with RTLD_NOLOAD returns NULL if the target library is not resident on memory, or the handle of the library if it is resident. This can be used to:
    • avoid dlopen from opening the same library more than once
    • check whether the reference count has dropped to 0. E.g.:
      // NOTE: Below code has not been tested
      while ((void *handle = dlopen(TARGET_LIB_PATH, RTLD_NOLOAD)) != NULL)
      {
          dlclose(handle);
      }
      

References:

Written on March 9, 2021