[Gc] GC and dynamic libraries

Boehm, Hans hans.boehm at hp.com
Mon Jun 14 15:44:16 PDT 2004


On Linux at least, there shouldn't be an issue with this.  On a
sufficiently recent Linux system, the collector uses dl_iterate_phdr
to enumerate the static data sections associated with all dynamic
libraries, and then treats those as roots.

I think there was an earlier bug report suggesting that if the collector
itself resides in a dynamic library, on some Linux distributions, it
effectively sees a value of _end that refers to the dynamic library
and not the main program.  This will cause a crash.  I believe that by
ELF rules, this is incorrect and thus a linker problem.  But I don't
think it was ever completely resolved.  And I don't think it occurs
in most distributions.

To track this down further, please post the (relevant sections of)
the backtrace at the time of the SIGSEGV that caused the crash, the
output of GC_dump() when called from the debugger at that point, and
the contents of /proc/<pid>/maps of the dying process, as it's stopped
by the debugger.

Hans

> -----Original Message-----
> From: gc-bounces at napali.hpl.hp.com
> [mailto:gc-bounces at napali.hpl.hp.com]On Behalf Of Joris van der Hoeven
> Sent: Thursday, June 10, 2004 4:09 AM
> To: gc at napali.hpl.hp.com
> Cc: Gabriel Dos Reis; vdhoeven at texmacs.org
> Subject: [Gc] GC and dynamic libraries
> 
> 
> 
> Hi,
> 
> This may be a frequently asked question: do I need to do 
> something special
> in order to use GC in combination with dynamically loaded libraries?
> 
> Indeed, I am writing an interpreter which uses GC as its 
> garbage collector,
> and which should be able to dynamically load third party 
> libraries using
> an instruction from within the interpreter. I work under GNU/Linux
> and used the commands
> 
> 	g++ -fPIC -c -I/home/vdhoeven/mathemagix/src/include/ 
> mmx-simple.cc -o mmx-simple.o
> 	g++ -lgc -fPIC -shared mmx-simple.o -o mmx-simple.so
> 
> in order to compile my .so. I also used the code below for
> the dynamic linking from within the interpreter:
> 
> ---------------------------------------------------
> void
> evaluator_rep::kw_use_2 () {
>   CHECK_SYNC (MMX_USE);
>   // Input: (use_2, module_name, cont, ...)
>   string name= as_string (V (car (rem)));
>   string lib = string("mmx-"*name*".so");
>   string rout= "init_" * name; // attention to mangling by C+
>   char* _lib = as_charp (lib);
>   char* _rout= as_charp (rout);
> 
>   void* handle= dlopen (_lib, RTLD_LAZY | RTLD_GLOBAL);
>   if (handle == NULL) {
>     error (car (trace), dlerror ());
>     // cerr << "Dynamic library " << lib << " did not open\n";
>     return;
>   }
>   else {
>     void* f= dlsym (handle, _rout);
>     if (f == NULL) {
>       error (car (trace), "Symbol " * rout * " not defined in 
> " * lib);
>       return;
>     }
>     else {
>       void (*init) (void)= (void (*) (void)) f;
>       init ();
>     }
>   }
>   cur= cadr (rem);
>   rem= cons (VAL_NONE, cddr (rem));
>   // Output: (cont, none, ...)
>   trace= cdr (trace);
> }
> ---------------------------------------------------
> 
> When doing so, I obtain a segmentation fault after a while.
> When I statically link the same code, everything works fine.
> So I probably used a wrong linking option or should I tell
> the GC about dynamically linked libraries?
> 
> Thanks for your time, Joris
> 
> -----------------------------------------------------------
> Joris van der Hoeven <vdhoeven at texmacs.org>
> http://www.texmacs.org: GNU TeXmacs scientific text editor
> http://www.math.u-psud.fr/~vdhoeven: personal homepage
> -----------------------------------------------------------
> 
> _______________________________________________
> Gc mailing list
> Gc at linux.hpl.hp.com
> http://www.hpl.hp.com/hosted/linux/mail-archives/gc/
> 


More information about the Gc mailing list