www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10775] New: druntime is not set up to handle dynamically loaded shared libraries in linux

http://d.puremagic.com/issues/show_bug.cgi?id=10775

           Summary: druntime is not set up to handle dynamically loaded
                    shared libraries in linux
           Product: D
           Version: D2
          Platform: All
        OS/Version: Linux
            Status: NEW
          Keywords: dll
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bugzilla digitalmars.com


--- Comment #0 from Walter Bright <bugzilla digitalmars.com> 2013-08-08
00:28:52 PDT ---
main.d:

    import core.stdc.stdio;
    import core.stdc.stdlib;
    import core.sys.posix.dlfcn;

    extern (C) int dll();

    int main()
    {
        printf("+main()\n");

        void *lh = dlopen("libdll.so", RTLD_LAZY);
        if (!lh)
        {
            fprintf(stderr, "dlopen error: %s\n", dlerror());
            exit(1);
        }
        printf("libdll.so is loaded\n");

        int function() fn = cast(int function())dlsym(lh, "dll");
        char *error = dlerror();
        if (error)
        {
            fprintf(stderr, "dlsym error: %s\n", error);
            exit(1);
        }
        printf("dll() function is found\n");

        fn();

        printf("unloading libdll.so\n");
        dlclose(lh);

        printf("-main()\n");
        return 0;
    }

dll.d:

    import core.stdc.stdio;

    extern (C) int dll()
    {
        printf("dll()\n");
        return 0;
    }

    static this()
    {
        printf("libdll.so construction\n");
    }

    static ~this()
    {
        printf("libdll.so destruction\n");
    }

Build:

    dmd -c dll.d -fPIC
    dmd -oflibdll.so dll.o -shared -defaultlib=libphobos2.so
-L-rpath=/home/walter/cbx/mars/phobos/generated/linux/release/64

    dmd -c main.d
    dmd main.o -L-ldl -defaultlib=libphobos2.so
-L-rpath=.:/home/walter/cbx/mars/phobos/generated/linux/release/64
    ./main

Results:

    +main()
    libdll.so is loaded
    dll() function is found
    dll()
    unloading libdll.so
    -main()

Note that the module constructors and destructors are not run. This is a bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 08 2013