Bugs in shared libraries (dlopen / dlclose)..

Bugs in shared libraries (dlopen / dlclose)..

Post by Marius Kjeldah » Sun, 02 Jun 1996 04:00:00

I am building a system which makes extensive use of shared libraries
(an object oriented system where the code for each object will be
located in shared libraries, loaded when needed and supporting "dynamic
upgrades" - upgrades without taking the system down).

In doing so, I have encountered a problem, and have managed to isolate
it as demonstrated in the source (10 code lines) attached to the end of
this message.

The problem is that the program halts with a segment fault after 199
iterations. If I move section 1 & 2 outside the loop (meaning the
library is opened only once, and closed after exiting the for loop),
everything runs fine and dandy.

The system I am running is 1.3.20 with libc 5.0.9 (I think).

So, the questions are:
1) Is this behaviour expected (and acceptible)?
2) How can I avoid it (I NEED to be able to load and unload a lot of
3) Any upgrades that will fix this?

Obviously, since moving section 1&2 out of the foor loop, the "bugs"
must be related to these calls (dlopen and dlclose).

Also, does anyone know of any "diagnostic" tools which can list the
dynamic libraries in use and their usage counts?

Thanks in advance..


#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

#define ITERATIONS 10000

main( int nArgC, char* pArgV[] ) {
  void*  pHandle;
  char*  pzError;
  double ( *pfCosine )( double );
  int    i;

  for ( i = 0; i < ITERATIONS; i++) {
      /* Section 1 start */
      pHandle = dlopen( "/lib/libm.so.5", RTLD_LAZY );
      if (!pHandle) {
         fputs (dlerror(), stderr);
      /* Section 1 end */
      pfCosine = dlsym( pHandle, "cos" );
      if ((pzError = ( char* ) dlerror()) != NULL)  {
         fputs(pzError, stderr);

      printf ("%d:%f\n", i, (*pfCosine)(2.0));
      /* Section 2 start */
      /* Section 2 end */



1. Loading libraries run-time: dlopen, dlsym, dlclose

I was reading through the man pages on my system and came across some
functions which seem to indicate that I can load a library at run-time
without statically linking the load information stubs into my program.
The functions in question are dlopen, dlsym, dlerror and dlclose.
They are especially of interest to me because my application has a
core which is static (ie in development) but which can be extended
without change by loading different libraries at different times.
For example, I may want my application to enter into a module by a
specific function name, and each of these functions has the same
argument type. This will be ideal if, for instance, I have three
different e-mail systems, and I want to be able to tie the three

As I mentioned I found them in the man pages by man -k "dynamic link"
but there were no actual man pages. I found the prototypes in
/usr/include/dlfcn.h, but couldn't find which library to link into.

Does anyone know the library(s) or where I can get the right
information/documentation? It's not in the LPG - I checked.

Thanks in advance.

2. Showing Subdirectories

3. Shared variable between main() and loaded shared library - dlopen()

4. experience with linux on TP380?

5. Shared library - linux glibc6 2.1.3x, dlclose generate segmentation fault

6. PC164 beep code -help please

7. Netatalk vs OSX

8. Where are dlopen, dlclose, etc...

9. dlopen and dlclose

10. Where are dlclose/dlopen?

11. Undefined dlopen, dlclose ..

12. loader errors: dlclose, dlsym,dlopen