Some questions about dynamic libraries

Hi there,
I’m playing around with shared libraries in Neutrino. I have a couple of
questions:

  • Are dynamic libraries unloaded and when?
  • If a dynamic library is modified, recompiled and linked to the same
    location, when is the new lib loaded?
  • How do I prevent loading the dynamic library to load them later with
    dlopen?
    Thanks
    Markus

In article <8urqqu$rbo$1@inn.qnx.com>,
Markus Loffler <loffler@ces.clemson.edu> wrote:

Hi there,
I’m playing around with shared libraries in Neutrino. I have a couple of
questions:

  • Are dynamic libraries unloaded and when?

Dynamic libraries are mapped in using mmap(). Therefore, the rules
of mmap() apply. When the last reference to the library is removed,
the library itself can be unloaded. i.e. when the last program linked
against the library terminates, it will be unloaded.

  • If a dynamic library is modified, recompiled and linked to the same
    location, when is the new lib loaded?

On first use. By this I mean, the previous incarnation would have to
have been unloaded first. If you run an application that links against
the dynamic library, but a program is still running that linked in
the previous incarnation, the newly run application will be dynamically
linked against the previous incarnation of the library that is currently
in memory.

  • How do I prevent loading the dynamic library to load them later with
    dlopen?

You can’t have any direct references to symbols in the library. If
you have direct references, then the library will be loaded.
One thing you can do is right wrappers for each of the functions you
want, assuming you have a lot of them and the implementation is
substantially larger than the wrappers. Put the wrappers in a library
you link against the application.

e.g. (THREAD-SAFE)

#include <dlfcn.h>
static pthread_once_t once;
static void *lib_handle;

static int initialize_library(const char library) {
/
Where LIBNAME is a manifest constant for the library */
lib_handle = dlopen(LIBNAME, RTLD_GLOBAL|RTLD_NOW);
}

void *xxx_find_library() {
pthread_once(&once, initialize_library);
return lib_handle;
}

void *xxx_find_symbol(const char *sym) {
void *handle = xxx_find_library();
return dlsym(handle, sym);
}

int myFunc() {
int (fp)();
fp = (int (
)())xxx_find_symbol(myFunc);
(*fp)();
}

.
.
.

Thanks
Markus
\

Steve Furr email: furr@qnx.com
QNX Software Systems, Ltd.