March 01
https://issues.dlang.org/show_bug.cgi?id=24427

          Issue ID: 24427
           Summary: Crash when dlopen'ed library not dlclose'd explicitly
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: sturtivant@gmail.com

Compiling pure C (no D source) with dmd and using dynamic loading (not dynamic
linking) causes a crash on program exit under certain conditions, where a gcc
compilation of the same source works fine.

Rewriting the C in D and compiling with dmd with identical switches causes the same crash.

The following two examples are attached, using dlfcn.h from C source and core.sys.posix.dlfcn from D source. Both crash.

A.
====
dmd -c main.c
dmd -of=main -L-E main.o -L-ldl

dmd -c -fPIC dynamic_lib.c
dmd -of=dynamic_lib.so -shared -fPIC dynamic_lib.o
====

B.
====
dmd -c main.d
dmd -of=main -L-E main.o -L-ldl

dmd -c -fPIC dynamic_lib.d
dmd -of=dynamic_lib.so -shared -fPIC dynamic_lib.o
====

A. and B.
* main dynamically loads dynamic_lib.so
* main calls a function in dynamic_lib.so which calls a function in main
* main is linked with a switch to export all symbols so this works

When A. is built with gcc it works fine.

Failure to call dlclose before main exits causes the crash; main runs correctly before that. (If the commented out call of dlclose is made then everything works.)

Crash:
====
$ main
1
2
3
4
Aborting from src/rt/sections_elf_shared.d(512) DSO being unregistered isn't
current last one.Aborted (core dumped)
====

This crash occurs only if

1.
Both main and dynamic_lib are built with dmd.
(either from C source or D source)
(If one of main, dynamic_lib.so is built with gcc, everything works.)

2.
Main is NOT linked with -defaultlib=libphobos2.so
(irrespective of how dynamic_lib is linked to phobos)

--