1

I want to load the same .so file twice as separate instances. Based on the example, I have created the app with two dlopen commands.

However, I was facing some issues and I understood that dlmopen should be used if I am using multiple instances of a same .so. However, I don't know how to pass the arguments. Can someone help me how to do this in GFortran?

My code is as below,

program example
    use :: iso_c_binding
implicit none

    integer(c_int), parameter :: rtld_lazy=1 ! value extracte from the C header file
    integer(c_int), parameter :: rtld_now=2 ! value extracte from the C header file
    !
    ! interface to linux API
    interface
        function dlopen(filename,mode) bind(c,name="dlopen")
            ! void *dlopen(const char *filename, int mode);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlopen
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function

        function dlsym(handle,name) bind(c,name="dlsym")
            ! void *dlsym(void *handle, const char *name);
            use iso_c_binding
            implicit none
            type(c_funptr) :: dlsym
            type(c_ptr), value :: handle
            character(c_char), intent(in) :: name(*)
        end function

        function dlclose(handle) bind(c,name="dlclose")
            ! int dlclose(void *handle);
            use iso_c_binding
            implicit none
            integer(c_int) :: dlclose
            type(c_ptr), value :: handle
        end function
    end interface

    ! Define interface of call-back routine.
    abstract interface
        subroutine called_proc (i, i2) bind(c)
            use, intrinsic :: iso_c_binding
            integer(c_int), intent(in) :: i
            integer(c_int), intent(out) :: i2
        end subroutine called_proc
    end interface

    ! testing the dynamic loading
    integer i, i2
    type(c_funptr) :: proc_addr
    type(c_ptr) :: handle1, handle2
    character(256) :: pName, lName

    procedure(called_proc), bind(c), pointer :: proc
    !
    i = 15

    handle1=dlopen("./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle1))then
        print*, 'Unable to load DLL ./test.so - First time'
        stop
    end if
    handle2=dlopen("./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle2))then
        print*, 'Unable to load DLL ./test.so - Second time'
        stop
    end if

    ! If I can use dlmopen() I dont know how to pass the arguments

    proc_addr=dlsym(handle, "t_times2"//c_null_char)
    if (.not. c_associated(proc_addr))then
        write(*,*) 'Unable to load the procedure t_times2'
        stop
    end if
    call c_f_procpointer( proc_addr, proc )
    call proc(i,i2)
    write(*,*) "t_times2, i2=", i2
    !
    proc_addr=dlsym( handle, "t_square"//c_null_char )
    if ( .not. c_associated(proc_addr) )then
        write(*,*)'Unable to load the procedure t_square'
        stop
    end if
    call c_f_procpointer(proc_addr, proc)
    call proc(i,i2)
    write(*,*) "t_square, i2=", i2
contains
end program example

Update: Based on the suggestion from Vladmir, I tried below but I get Unable to load DLL ./test.so - Third time,

        function dlmopen(lmid_t,filename,mode) bind(c,name="dlmopen")
            ! void *dlopen(const char *filename, int mode);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlopen
            integer(c_long), value :: lmid_t
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function


    handle3=dlmopen(1,"./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle3))then
        print*, 'Unable to load DLL ./test.so - Third time'
        stop
    end if
Selva
  • 951
  • 7
  • 23
  • Do you have an interface for `dlmopen`? – francescalus Feb 06 '19 at 09:36
  • Do you still need your old question https://stackoverflow.com/questions/53927166/load-a-so-library-in-fortran? Or Aren't there details to be added there to make it answerable? – Vladimir F Героям слава Feb 06 '19 at 09:36
  • Consider the [tag:posix] or related tags }perhaps something about dynamical libraries) to attract those who know these functions well. – Vladimir F Героям слава Feb 06 '19 at 09:39
  • @Selva, do you understand what `lmid` does? You should look up what `Lmid_t` is in your system headers but it cannot be guaranteed it will the same on other systems. – Vladimir F Героям слава Feb 06 '19 at 09:41
  • @VladimirF - Thanks for response. Yes they are similar and when I started it I didnt even know dlopen was present. Anyway, I think I will close that question. I will add posix tag. As for lmid, I have no idea what is lmid is and how to see the headers – Selva Feb 06 '19 at 09:45
  • @francescalus - Sorry, I dont know if it is present and how to look for it. I am very new to fortran – Selva Feb 06 '19 at 09:46
  • `lmid` is a handle of a `Lmid_t` type. On my computer, in `dlfcn.h`, `Lmid_t` is declared as `long int`, but it can be anything else elsewhere. Still it is useful to start with this assumption for the first tests. If it works, you can make a wrapper written in C later. – Vladimir F Героям слава Feb 06 '19 at 10:21
  • @VladimirF - Thanks a lot for suggestion - I am a novice, so I dont know where I can find the dlfcn.h. Anyway, I will try to search and also try with your suggestion on long_int – Selva Feb 06 '19 at 10:34
  • @VladimirF As you suggested, I created a new interface for dlmopen with c_long integer type and passed 1 as value. It is failing to load ;( I am not able to identify the location of dlfcn.h and you have any idea? – Selva Feb 06 '19 at 10:50

1 Answers1

1

This is the base that you can start from, it is a modification of your code and because of the #define macros it requires the cpp flags. You can change the defines to a normal parameter declaration if you want, but just copying the macros from the header is easier.

    use iso_c_binding

    implicit none

! from dlfcn.h
# define LM_ID_BASE 0   /* Initial namespace.  */
# define LM_ID_NEWLM    -1  /* For dlmopen: request new namespace.  */

    integer(c_long) :: dlist = LM_ID_NEWLM
    integer(c_int), parameter :: rtld_lazy=1 ! value extracte from the C header file
    integer(c_int), parameter :: rtld_now=2 ! value extracte from the C header file
    type(c_ptr) :: handle3     

    interface
        function dlmopen(lmid_t,filename,mode) bind(c,name="dlmopen")
            ! void *dlmopen (Lmid_t lmid, const char *filename, int flags);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlmopen
            integer(c_long), value :: lmid_t
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function
    end interface

    handle3=dlmopen(dlist,"test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle3))then
        print*, 'Unable to load DLL ./test.so - Third time'
        stop
    end if
end    

According to the manual, there are two principal values that you can pass as dlist, either LM_ID_BASE or LM_ID_NEWLM. Their values are defined in the header dlfcn.h that is located ammong other standard C and POSIX headers (/usr/include/ or similar). You should not just pass 1, but one of these two values which happen to be 0 and -1 on my computer.

  • Thanks Vladmir. This works but I get the same issue I faced with dlopen. It looks like some variables are shared between the .so although I have placed themin two different folders. I saw that rtld_local=8 should work but If I give that, I get the `'Unable to load DLL ./test.so - Third time` – Selva Feb 07 '19 at 10:20
  • Did you use the `LM_ID_NEWLM` value? I think you will have to create a new question solely about the sharing issue. It is something for POSIX experts, Fortran people generally don't know this stuff. – Vladimir F Героям слава Feb 07 '19 at 13:00
  • I did try with LM_ID_NEWLM but then same issue. My .so does load some secondary .so files using dlopen and not sure if it is because of that. I am not able to modify the .so source :( As you suggested I will ask a new question and mark this as answer. Thanks a lot for your support – Selva Feb 07 '19 at 13:57
  • @Selva If you know C, I would first try the same thing in C. If you don't know C, you don't have much choice... – Vladimir F Героям слава Feb 07 '19 at 14:06