This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: dlopen/dlclose problems


On Sat, 27 Nov 2010 10:47:26 -0500
"Carlos O'Donell" <carlos@systemhalted.org> wrote:

> On Sat, Nov 27, 2010 at 10:13 AM, Andreas Fink
> <andreas.fink85@googlemail.com> wrote:
> >> What problem are you trying to solve?
> >>
> >
> > I have a libraries which contain algorithms (for image processing, but that's not important here).
> > And I have a program which can use these algorithms from the libraries. Now I'm developing a new algorithm and recompile one
> > of my libraries. Of course I do not want to close my program, but just tell it, to reload the library.
> >
> > Basically my intention is to reopen a library since it has changed on the harddisk, and use the brand new algorithms in there,
> > without the need of closing my program.
> >
> 
> Please keep libc-help in the CC, it helps others learn.
> 
> What problem are you trying to solve?
> 
> You have described how your application works, but not the problem.
> 
> I just tried a *simple* test application that does the following:
> 
> * dlopen
> * dlsym
> * call symbol, received first result set.
> * dlclose
> * sleep
> 
> While the application slept I changed the shared library implementation.
> 
> * application returns from sleep
> * dlopen
> * dlsym
> * call symbol, received second result set.
> * dlclose
> * exit.
> 
> The dynamic loader correctly reloaded the library after it changed.
> 
> If this basic functionality was broken we would not be able to
> implement plugins on GNU/Linux.
> 
> If you have a problem then you need to describe the problem.
> 
> If you are interested in learning then ask a question :-)
> 
> Cheers,
> Carlos.

Hello Carlos,

That's exactly what I want to do. Unfortunately with the testlib I sent with my first request (and as attachment here again)
it is not working. Since the library is not closed correctly (I can even call dlclose several times on the handle and it always
returns 0, i.e. no error). I don't care if it would still be in memory or not, as long as dlopening it a second time would really
reload it!

Attached is again a test program and a test library which fails for glibc-2.12.1 (glibc-2.9 works). Please can you confirm this
behaviour with your glibc?

Regards
Andreas
// g++ -shared -fPIC -O0 -o libtest.so libtest.cpp

#include <iostream>


template<class T>
void test()
{
  // change this to some other value to see if reloading was successfull...
  static const int N[1] = {5};
  std::cout << "N[0]=" << N[0] << std::endl;
}

extern "C" {
void blubbels()
{
  test<short>();
}
}
// g++ -ldl -o test_dlopen test_dlopen.cpp && ./test_dlopen /path/to/libtest.so

#include <dlfcn.h>
#include <iostream>
#include <cstdio>
#include <link.h>

int cb(struct dl_phdr_info* info, size_t size, void* data)
{
  std::cout << info->dlpi_name << std::endl;
  return 0;
}

typedef void (*test_fct)();

int main(int argc, char** argv)
{
  const char* default_path = "./libtest.so";
  const char* path = default_path;
  if (argc >= 2) {
    path = argv[1];
  }

  void* h = dlopen(path, RTLD_LAZY);
  test_fct t;
  if ( dlsym(h, "blubbels") ) {
    t = (test_fct) dlsym(h, "blubbels");
  }
  else {
    std::cout << "Not found symbol" << std::endl;
    return 1;
  }
  t();
  dlclose(h);

  std::cout << "library should be unloaded --> recompile and press enter" << std::endl;
  getchar();
  
  h = dlopen(path, RTLD_LAZY);
  if ( dlsym(h, "blubbels") ) {
    t = (test_fct) dlsym(h, "blubbels");
  }
  else {
    std::cout << "Not found symbol" << std::endl;
    return 1;
  }
  t();
  dlclose(h);

  return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]